|
| 1 | +use std::io; |
| 2 | + |
| 3 | +/// Unix-specific extensions to [`fs::File`]. |
| 4 | +#[cfg(unix)] |
| 5 | +pub trait FileExt { |
| 6 | + /// Reads a number of bytes starting from a given offset. |
| 7 | + fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>; |
| 8 | + |
| 9 | + /// Like `read_at`, except that it reads into a slice of buffers. |
| 10 | + #[cfg(feature = "unix_file_vectored_at")] |
| 11 | + fn read_vectored_at( |
| 12 | + &self, |
| 13 | + bufs: &mut [std::io::IoSliceMut<'_>], |
| 14 | + offset: u64, |
| 15 | + ) -> io::Result<usize> { |
| 16 | + io::default_read_vectored(|b| self.read_at(b, offset), bufs) |
| 17 | + } |
| 18 | + |
| 19 | + /// Reads the exact number of bytes required to fill `buf` from the given offset. |
| 20 | + fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> { |
| 21 | + while !buf.is_empty() { |
| 22 | + match self.read_at(buf, offset) { |
| 23 | + Ok(0) => break, |
| 24 | + Ok(n) => { |
| 25 | + let tmp = buf; |
| 26 | + buf = &mut tmp[n..]; |
| 27 | + offset += n as u64; |
| 28 | + } |
| 29 | + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 30 | + Err(e) => return Err(e), |
| 31 | + } |
| 32 | + } |
| 33 | + if !buf.is_empty() { |
| 34 | + Err(io::Error::new( |
| 35 | + io::ErrorKind::UnexpectedEof, |
| 36 | + "failed to fill whole buffer", |
| 37 | + )) |
| 38 | + } else { |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Writes a number of bytes starting from a given offset. |
| 44 | + fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>; |
| 45 | + |
| 46 | + /// Like `write_at`, except that it writes from a slice of buffers. |
| 47 | + #[cfg(feature = "unix_file_vectored_at")] |
| 48 | + fn write_vectored_at(&self, bufs: &[std::io::IoSlice<'_>], offset: u64) -> io::Result<usize> { |
| 49 | + io::default_write_vectored(|b| self.write_at(b, offset), bufs) |
| 50 | + } |
| 51 | + |
| 52 | + /// Attempts to write an entire buffer starting from a given offset. |
| 53 | + fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> { |
| 54 | + while !buf.is_empty() { |
| 55 | + match self.write_at(buf, offset) { |
| 56 | + Ok(0) => { |
| 57 | + return Err(io::Error::new( |
| 58 | + io::ErrorKind::WriteZero, |
| 59 | + "failed to write whole buffer", |
| 60 | + )); |
| 61 | + } |
| 62 | + Ok(n) => { |
| 63 | + buf = &buf[n..]; |
| 64 | + offset += n as u64 |
| 65 | + } |
| 66 | + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 67 | + Err(e) => return Err(e), |
| 68 | + } |
| 69 | + } |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// WASI-specific extensions to [`fs::File`]. |
| 75 | +#[cfg(target_os = "wasi")] |
| 76 | +pub trait FileExt { |
| 77 | + /// Reads a number of bytes starting from a given offset. |
| 78 | + fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> { |
| 79 | + let bufs = &mut [std::io::IoSliceMut::new(buf)]; |
| 80 | + self.read_vectored_at(bufs, offset) |
| 81 | + } |
| 82 | + |
| 83 | + /// Reads a number of bytes starting from a given offset. |
| 84 | + fn read_vectored_at( |
| 85 | + &self, |
| 86 | + bufs: &mut [std::io::IoSliceMut<'_>], |
| 87 | + offset: u64, |
| 88 | + ) -> io::Result<usize>; |
| 89 | + |
| 90 | + /// Reads the exact number of byte required to fill `buf` from the given offset. |
| 91 | + fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> { |
| 92 | + while !buf.is_empty() { |
| 93 | + match self.read_at(buf, offset) { |
| 94 | + Ok(0) => break, |
| 95 | + Ok(n) => { |
| 96 | + let tmp = buf; |
| 97 | + buf = &mut tmp[n..]; |
| 98 | + offset += n as u64; |
| 99 | + } |
| 100 | + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 101 | + Err(e) => return Err(e), |
| 102 | + } |
| 103 | + } |
| 104 | + if !buf.is_empty() { |
| 105 | + Err(io::Error::new( |
| 106 | + io::ErrorKind::UnexpectedEof, |
| 107 | + "failed to fill whole buffer", |
| 108 | + )) |
| 109 | + } else { |
| 110 | + Ok(()) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// Writes a number of bytes starting from a given offset. |
| 115 | + fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> { |
| 116 | + let bufs = &[std::io::IoSlice::new(buf)]; |
| 117 | + self.write_vectored_at(bufs, offset) |
| 118 | + } |
| 119 | + |
| 120 | + /// Writes a number of bytes starting from a given offset. |
| 121 | + fn write_vectored_at(&self, bufs: &[std::io::IoSlice<'_>], offset: u64) -> io::Result<usize>; |
| 122 | + |
| 123 | + /// Attempts to write an entire buffer starting from a given offset. |
| 124 | + fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> { |
| 125 | + while !buf.is_empty() { |
| 126 | + match self.write_at(buf, offset) { |
| 127 | + Ok(0) => { |
| 128 | + return Err(io::Error::new( |
| 129 | + io::ErrorKind::WriteZero, |
| 130 | + "failed to write whole buffer", |
| 131 | + )); |
| 132 | + } |
| 133 | + Ok(n) => { |
| 134 | + buf = &buf[n..]; |
| 135 | + offset += n as u64 |
| 136 | + } |
| 137 | + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 138 | + Err(e) => return Err(e), |
| 139 | + } |
| 140 | + } |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + |
| 144 | + /// Returns the current position within the file. |
| 145 | + fn tell(&self) -> io::Result<u64>; |
| 146 | + |
| 147 | + /// Adjust the flags associated with this file. |
| 148 | + fn fdstat_set_flags(&self, flags: u16) -> std::io::Result<()>; |
| 149 | + |
| 150 | + /// Adjust the rights associated with this file. |
| 151 | + fn fdstat_set_rights(&self, rights: u64, inheriting: u64) -> std::io::Result<()>; |
| 152 | + |
| 153 | + /// Provide file advisory information on a file descriptor. |
| 154 | + fn advise(&self, offset: u64, len: u64, advice: u8) -> std::io::Result<()>; |
| 155 | + |
| 156 | + /// Force the allocation of space in a file. |
| 157 | + fn allocate(&self, offset: u64, len: u64) -> std::io::Result<()>; |
| 158 | + |
| 159 | + /// Create a directory. |
| 160 | + fn create_directory<P: AsRef<std::path::Path>>(&self, dir: P) -> std::io::Result<()>; |
| 161 | + |
| 162 | + /// Read the contents of a symbolic link. |
| 163 | + fn read_link<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<std::path::PathBuf>; |
| 164 | + |
| 165 | + /// Return the attributes of a file or directory. |
| 166 | + fn metadata_at<P: AsRef<std::path::Path>>( |
| 167 | + &self, |
| 168 | + lookup_flags: u32, |
| 169 | + path: P, |
| 170 | + ) -> io::Result<std::fs::Metadata>; |
| 171 | + |
| 172 | + /// Unlink a file. |
| 173 | + fn remove_file<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<()>; |
| 174 | + |
| 175 | + /// Remove a directory. |
| 176 | + fn remove_directory<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<()>; |
| 177 | +} |
| 178 | + |
| 179 | +/// Windows-specific extensions to [`fs::File`]. |
| 180 | +#[cfg(windows)] |
| 181 | +pub trait FileExt { |
| 182 | + /// Seeks to a given position and reads a number of bytes. |
| 183 | + fn seek_read(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>; |
| 184 | + |
| 185 | + /// Seeks to a given position and writes a number of bytes. |
| 186 | + fn seek_write(&self, buf: &[u8], offset: u64) -> io::Result<usize>; |
| 187 | +} |
0 commit comments