1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
//! Unix specific traits that extend the traits in [`rsfs`]. //! //! These traits are separate from `rsfs` traits to ensure users of these traits opt-in to Unix //! specific functionality. //! //! # Examples //! //! This module allows checking and using filesystem modes: //! //! ``` //! use rsfs::*; //! use rsfs::unix_ext::*; //! # fn foo() -> std::io::Result<()> { //! let fs = rsfs::disk::FS; //! //! assert_eq!(fs.metadata("/")?.permissions().mode(), 0o755); //! # Ok(()) //! # } //! ``` //! //! We can also symlink files: //! //! ``` //! use rsfs::*; //! use rsfs::unix_ext::*; //! use rsfs::mem::FS; //! # fn foo() -> std::io::Result<()> { //! let fs = FS::new(); //! //! fs.symlink("a.txt", "b.txt")?; //! # Ok(()) //! # } //! ``` //! //! There are even more useful Unix extensions! //! //! [`rsfs`]: ../index.html use std::io::Result; use std::path::Path; /// Unix specific [`rsfs::DirBuilder`] extensions. /// /// [`rsfs::DirBuilder`]: ../trait.DirBuilder.html pub trait DirBuilderExt { /// Sets the mode bits to create new directories with. This option defaults to 0o777. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// let fs = FS::new(); /// /// let mut builder = fs.new_dirbuilder(); /// builder.mode(0o755); /// ``` fn mode(&mut self, mode: u32) -> &mut Self; } /// Unix specific [`rsfs::File`] extensions. /// /// [`rsfs::File`]: ../trait.File.html pub trait FileExt { /// Reads a number of bytes starting from the given offset, returning the number of bytes read. /// /// The offset is relative to the start of the file and this read does not affect the file's /// current cursor position. /// /// Note that, similar to `File::read`, it is not an error to return with a short read. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// let mut file = fs.open_file("foo.txt")?; /// let mut buffer = [0; 10]; /// /// // read up to 10 bytes starting from offset 10 in the file /// file.read_at(&mut buffer[..], 10)?; /// # Ok(()) /// # } /// ``` fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize>; /// Writes a number of bytes starting from the given offset, returning the number of bytes /// written. /// /// The offset is relative to the start of the file and this read does not affect the file's /// current cursor position. /// /// When writing beyond the end of a file, the file is zero extended to `offset`. /// /// Note that, similar to `File::write`, it is not an error to return with a short write. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// let mut file = fs.create_file("foo.txt")?; /// /// // write starting from offset 10 in the file, potentially zero extending the start /// file.write_at(b"some bytes", 10)?; /// # Ok(()) /// # } /// ``` fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize>; } /// Unix specific [`rsfs::OpenOptions`] extensions. /// /// [`rsfs::OpenOptions`]: ../trait.OpenOptions.html pub trait OpenOptionsExt { /// Sets the mode bits that a new file will be opened with. /// /// The default mode for new files is 0o666. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// let mut options = fs.new_openopts(); /// options.mode(0o600); // only owner can read/write /// let file = options.open("foo.txt")?; /// # Ok(()) /// # } /// ``` fn mode(&mut self, mode: u32) -> &mut Self; /// Pass custom flags to the `flags` argument of `open`. /// /// The bits that define the access mode are masked out with `O_ACCMODE` to ensure they do not /// interfere with the access mode set by Rust options. /// /// `custom_flags` can only set flags, not remove flags set by Rust options. This option /// overwrites any previously set custom flags. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// let mut options = fs.new_openopts(); /// options.write(true); /// if cfg!(unix) { /// options.custom_flags(0x8000); // O_NOFOLLOW (use libc in real code) /// } /// let file = options.open("foo.txt")?; /// # Ok(()) /// # } /// ``` fn custom_flags(&mut self, flags: i32) -> &mut Self; } /// Unix specific [`rsfs::Permissions`] extensions. /// /// [`rsfs::Permissions`]: ../trait.Permissions.html pub trait PermissionsExt { /// Returns the underlying Unix mode of these permissions. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// let file = fs.create_file("foo.txt")?; /// let metadata = file.metadata()?; /// let permissions = metadata.permissions(); /// /// println!("permission: {:o}", permissions.mode()); /// # Ok(()) /// # } /// ``` fn mode(&self) -> u32; /// Sets the underlying Unix mode for these permissions. /// /// This does not modify the filesystem. To modify the filesystem, use the filesystem's /// [`set_permissions`] function. /// /// [`set_permissions`]: ../trait.FS.html#tymethod.set_permissions /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// let file = fs.create_file("foo.txt")?; /// let metadata = file.metadata()?; /// let mut permissions = metadata.permissions(); /// /// permissions.set_mode(0o644); /// assert_eq!(permissions.mode(), 0o644); /// # Ok(()) /// # } /// ``` fn set_mode(&mut self, mode: u32); /// Creates a new Permissions from the given Unix mode. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::Permissions; /// /// let permissions = Permissions::from_mode(0o644); /// assert_eq!(permissions.mode(), 0o644); /// ``` fn from_mode(mode: u32) -> Self; } /// Unix specific [`rsfs::GenFS`] extensions. /// /// [`rsfs::GenFS`]: ../trait.GenFS.html pub trait GenFSExt { /// Creates a new symbolic link on the filesystem. /// /// The `dst` path will be a symbolic link pointing to the `src` path. /// /// # Examples /// /// ``` /// use rsfs::*; /// use rsfs::unix_ext::*; /// use rsfs::mem::FS; /// # fn foo() -> std::io::Result<()> { /// let fs = FS::new(); /// /// fs.symlink("a.txt", "b.txt")?; /// # Ok(()) /// # } /// ``` fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, dst: Q) -> Result<()>; }