fs.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. use crate::{
  2. c_str::CStr,
  3. header::{
  4. fcntl::O_CREAT,
  5. unistd::{SEEK_CUR, SEEK_END, SEEK_SET},
  6. },
  7. io,
  8. platform::{types::*, Pal, Sys},
  9. };
  10. use core::ops::Deref;
  11. pub struct File {
  12. pub fd: c_int,
  13. /// To avoid self referential FILE struct that needs both a reader and a writer,
  14. /// make "reference" files that share fd but don't close on drop.
  15. pub reference: bool,
  16. }
  17. impl File {
  18. pub fn new(fd: c_int) -> Self {
  19. Self {
  20. fd,
  21. reference: false,
  22. }
  23. }
  24. pub fn open(path: &CStr, oflag: c_int) -> io::Result<Self> {
  25. match Sys::open(path, oflag, 0) {
  26. -1 => Err(io::last_os_error()),
  27. ok => Ok(Self::new(ok)),
  28. }
  29. }
  30. pub fn create(path: &CStr, oflag: c_int, mode: mode_t) -> io::Result<Self> {
  31. match Sys::open(path, oflag | O_CREAT, mode) {
  32. -1 => Err(io::last_os_error()),
  33. ok => Ok(Self::new(ok)),
  34. }
  35. }
  36. pub fn sync_all(&self) -> io::Result<()> {
  37. match Sys::fsync(self.fd) {
  38. -1 => Err(io::last_os_error()),
  39. _ok => Ok(()),
  40. }
  41. }
  42. pub fn set_len(&self, size: u64) -> io::Result<()> {
  43. match Sys::ftruncate(self.fd, size as off_t) {
  44. -1 => Err(io::last_os_error()),
  45. _ok => Ok(()),
  46. }
  47. }
  48. pub fn try_clone(&self) -> io::Result<Self> {
  49. match Sys::dup(self.fd) {
  50. -1 => Err(io::last_os_error()),
  51. ok => Ok(Self::new(ok)),
  52. }
  53. }
  54. /// Create a new file pointing to the same underlying descriptor. This file
  55. /// will know it's a "reference" and won't close the fd. It will, however,
  56. /// not prevent the original file from closing the fd.
  57. pub unsafe fn get_ref(&self) -> Self {
  58. Self {
  59. fd: self.fd,
  60. reference: true,
  61. }
  62. }
  63. }
  64. impl io::Read for &File {
  65. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  66. match Sys::read(self.fd, buf) {
  67. -1 => Err(io::last_os_error()),
  68. ok => Ok(ok as usize),
  69. }
  70. }
  71. }
  72. impl io::Write for &File {
  73. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  74. match Sys::write(self.fd, buf) {
  75. -1 => Err(io::last_os_error()),
  76. ok => Ok(ok as usize),
  77. }
  78. }
  79. fn flush(&mut self) -> io::Result<()> {
  80. Ok(())
  81. }
  82. }
  83. impl io::Seek for &File {
  84. fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
  85. let (offset, whence) = match pos {
  86. io::SeekFrom::Start(start) => (start as off_t, SEEK_SET),
  87. io::SeekFrom::Current(current) => (current as off_t, SEEK_CUR),
  88. io::SeekFrom::End(end) => (end as off_t, SEEK_END),
  89. };
  90. match Sys::lseek(self.fd, offset, whence) {
  91. -1 => Err(io::last_os_error()),
  92. ok => Ok(ok as u64),
  93. }
  94. }
  95. }
  96. impl io::Read for File {
  97. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  98. (&mut &*self).read(buf)
  99. }
  100. }
  101. impl io::Write for File {
  102. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  103. (&mut &*self).write(buf)
  104. }
  105. fn flush(&mut self) -> io::Result<()> {
  106. (&mut &*self).flush()
  107. }
  108. }
  109. impl io::Seek for File {
  110. fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
  111. (&mut &*self).seek(pos)
  112. }
  113. }
  114. impl Deref for File {
  115. type Target = c_int;
  116. fn deref(&self) -> &Self::Target {
  117. &self.fd
  118. }
  119. }
  120. impl Drop for File {
  121. fn drop(&mut self) {
  122. if !self.reference {
  123. let _ = Sys::close(self.fd);
  124. }
  125. }
  126. }