1 //! Unix-specific extensions to primitives in the `std::fs` module.
3 #![stable(feature = "rust1", since = "1.0.0")]
5 use crate::fs::{self, OpenOptions, Permissions};
9 use crate::sys::platform::fs::MetadataExt as UnixMetadataExt;
10 use crate::sys_common::{AsInner, AsInnerMut, FromInner};
12 /// Unix-specific extensions to [`File`].
14 /// [`File`]: ../../../../std/fs/struct.File.html
15 #[stable(feature = "file_offset", since = "1.15.0")]
17 /// Reads a number of bytes starting from a given offset.
19 /// Returns the number of bytes read.
21 /// The offset is relative to the start of the file and thus independent
22 /// from the current cursor.
24 /// The current file cursor is not affected by this function.
26 /// Note that similar to [`File::read`], it is not an error to return with a
29 /// [`File::read`]: ../../../../std/fs/struct.File.html#method.read
35 /// use std::fs::File;
36 /// use std::os::unix::prelude::FileExt;
38 /// fn main() -> io::Result<()> {
39 /// let mut buf = [0u8; 8];
40 /// let file = File::open("foo.txt")?;
42 /// // We now read 8 bytes from the offset 10.
43 /// let num_bytes_read = file.read_at(&mut buf, 10)?;
44 /// println!("read {} bytes: {:?}", num_bytes_read, buf);
48 #[stable(feature = "file_offset", since = "1.15.0")]
49 fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
51 /// Reads the exact number of byte required to fill `buf` from the given offset.
53 /// The offset is relative to the start of the file and thus independent
54 /// from the current cursor.
56 /// The current file cursor is not affected by this function.
58 /// Similar to [`Read::read_exact`] but uses [`read_at`] instead of `read`.
60 /// [`Read::read_exact`]: ../../../../std/io/trait.Read.html#method.read_exact
61 /// [`read_at`]: #tymethod.read_at
65 /// If this function encounters an error of the kind
66 /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
69 /// If this function encounters an "end of file" before completely filling
70 /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
71 /// The contents of `buf` are unspecified in this case.
73 /// If any other read error is encountered then this function immediately
74 /// returns. The contents of `buf` are unspecified in this case.
76 /// If this function returns an error, it is unspecified how many bytes it
77 /// has read, but it will never read more than would be necessary to
78 /// completely fill the buffer.
80 /// [`ErrorKind::Interrupted`]: ../../../../std/io/enum.ErrorKind.html#variant.Interrupted
81 /// [`ErrorKind::UnexpectedEof`]: ../../../../std/io/enum.ErrorKind.html#variant.UnexpectedEof
87 /// use std::fs::File;
88 /// use std::os::unix::prelude::FileExt;
90 /// fn main() -> io::Result<()> {
91 /// let mut buf = [0u8; 8];
92 /// let file = File::open("foo.txt")?;
94 /// // We now read exactly 8 bytes from the offset 10.
95 /// file.read_exact_at(&mut buf, 10)?;
96 /// println!("read {} bytes: {:?}", buf.len(), buf);
100 #[stable(feature = "rw_exact_all_at", since = "1.33.0")]
101 fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
102 while !buf.is_empty() {
103 match self.read_at(buf, offset) {
110 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
111 Err(e) => return Err(e),
115 Err(io::Error::new(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
121 /// Writes a number of bytes starting from a given offset.
123 /// Returns the number of bytes written.
125 /// The offset is relative to the start of the file and thus independent
126 /// from the current cursor.
128 /// The current file cursor is not affected by this function.
130 /// When writing beyond the end of the file, the file is appropriately
131 /// extended and the intermediate bytes are initialized with the value 0.
133 /// Note that similar to [`File::write`], it is not an error to return a
136 /// [`File::write`]: ../../../../std/fs/struct.File.html#write.v
141 /// use std::fs::File;
143 /// use std::os::unix::prelude::FileExt;
145 /// fn main() -> io::Result<()> {
146 /// let file = File::open("foo.txt")?;
148 /// // We now write at the offset 10.
149 /// file.write_at(b"sushi", 10)?;
153 #[stable(feature = "file_offset", since = "1.15.0")]
154 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
156 /// Attempts to write an entire buffer starting from a given offset.
158 /// The offset is relative to the start of the file and thus independent
159 /// from the current cursor.
161 /// The current file cursor is not affected by this function.
163 /// This method will continuously call [`write_at`] until there is no more data
164 /// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
165 /// returned. This method will not return until the entire buffer has been
166 /// successfully written or such an error occurs. The first error that is
167 /// not of [`ErrorKind::Interrupted`] kind generated from this method will be
172 /// This function will return the first error of
173 /// non-[`ErrorKind::Interrupted`] kind that [`write_at`] returns.
175 /// [`ErrorKind::Interrupted`]: ../../../../std/io/enum.ErrorKind.html#variant.Interrupted
176 /// [`write_at`]: #tymethod.write_at
181 /// use std::fs::File;
183 /// use std::os::unix::prelude::FileExt;
185 /// fn main() -> io::Result<()> {
186 /// let file = File::open("foo.txt")?;
188 /// // We now write at the offset 10.
189 /// file.write_all_at(b"sushi", 10)?;
193 #[stable(feature = "rw_exact_all_at", since = "1.33.0")]
194 fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
195 while !buf.is_empty() {
196 match self.write_at(buf, offset) {
198 return Err(io::Error::new(
199 io::ErrorKind::WriteZero,
200 "failed to write whole buffer",
207 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
208 Err(e) => return Err(e),
215 #[stable(feature = "file_offset", since = "1.15.0")]
216 impl FileExt for fs::File {
217 fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
218 self.as_inner().read_at(buf, offset)
220 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
221 self.as_inner().write_at(buf, offset)
225 /// Unix-specific extensions to [`fs::Permissions`].
227 /// [`fs::Permissions`]: ../../../../std/fs/struct.Permissions.html
228 #[stable(feature = "fs_ext", since = "1.1.0")]
229 pub trait PermissionsExt {
230 /// Returns the underlying raw `st_mode` bits that contain the standard
231 /// Unix permissions for this file.
236 /// use std::fs::File;
237 /// use std::os::unix::fs::PermissionsExt;
239 /// fn main() -> std::io::Result<()> {
240 /// let f = File::create("foo.txt")?;
241 /// let metadata = f.metadata()?;
242 /// let permissions = metadata.permissions();
244 /// println!("permissions: {:o}", permissions.mode());
247 #[stable(feature = "fs_ext", since = "1.1.0")]
248 fn mode(&self) -> u32;
250 /// Sets the underlying raw bits for this set of permissions.
255 /// use std::fs::File;
256 /// use std::os::unix::fs::PermissionsExt;
258 /// fn main() -> std::io::Result<()> {
259 /// let f = File::create("foo.txt")?;
260 /// let metadata = f.metadata()?;
261 /// let mut permissions = metadata.permissions();
263 /// permissions.set_mode(0o644); // Read/write for owner and read for others.
264 /// assert_eq!(permissions.mode(), 0o644);
267 #[stable(feature = "fs_ext", since = "1.1.0")]
268 fn set_mode(&mut self, mode: u32);
270 /// Creates a new instance of `Permissions` from the given set of Unix
276 /// use std::fs::Permissions;
277 /// use std::os::unix::fs::PermissionsExt;
279 /// // Read/write for owner and read for others.
280 /// let permissions = Permissions::from_mode(0o644);
281 /// assert_eq!(permissions.mode(), 0o644);
283 #[stable(feature = "fs_ext", since = "1.1.0")]
284 fn from_mode(mode: u32) -> Self;
287 #[stable(feature = "fs_ext", since = "1.1.0")]
288 impl PermissionsExt for Permissions {
289 fn mode(&self) -> u32 {
290 self.as_inner().mode()
293 fn set_mode(&mut self, mode: u32) {
294 *self = Permissions::from_inner(FromInner::from_inner(mode));
297 fn from_mode(mode: u32) -> Permissions {
298 Permissions::from_inner(FromInner::from_inner(mode))
302 /// Unix-specific extensions to [`fs::OpenOptions`].
304 /// [`fs::OpenOptions`]: ../../../../std/fs/struct.OpenOptions.html
305 #[stable(feature = "fs_ext", since = "1.1.0")]
306 pub trait OpenOptionsExt {
307 /// Sets the mode bits that a new file will be created with.
309 /// If a new file is created as part of an `OpenOptions::open` call then this
310 /// specified `mode` will be used as the permission bits for the new file.
311 /// If no `mode` is set, the default of `0o666` will be used.
312 /// The operating system masks out bits with the system's `umask`, to produce
313 /// the final permissions.
318 /// use std::fs::OpenOptions;
319 /// use std::os::unix::fs::OpenOptionsExt;
322 /// let mut options = OpenOptions::new();
323 /// options.mode(0o644); // Give read/write for owner and read for others.
324 /// let file = options.open("foo.txt");
327 #[stable(feature = "fs_ext", since = "1.1.0")]
328 fn mode(&mut self, mode: u32) -> &mut Self;
330 /// Pass custom flags to the `flags` argument of `open`.
332 /// The bits that define the access mode are masked out with `O_ACCMODE`, to
333 /// ensure they do not interfere with the access mode set by Rusts options.
335 /// Custom flags can only set flags, not remove flags set by Rusts options.
336 /// This options overwrites any previously set custom flags.
341 /// # #![feature(rustc_private)]
342 /// extern crate libc;
343 /// use std::fs::OpenOptions;
344 /// use std::os::unix::fs::OpenOptionsExt;
347 /// let mut options = OpenOptions::new();
348 /// options.write(true);
350 /// options.custom_flags(libc::O_NOFOLLOW);
352 /// let file = options.open("foo.txt");
355 #[stable(feature = "open_options_ext", since = "1.10.0")]
356 fn custom_flags(&mut self, flags: i32) -> &mut Self;
359 #[stable(feature = "fs_ext", since = "1.1.0")]
360 impl OpenOptionsExt for OpenOptions {
361 fn mode(&mut self, mode: u32) -> &mut OpenOptions {
362 self.as_inner_mut().mode(mode);
366 fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
367 self.as_inner_mut().custom_flags(flags);
372 /// Unix-specific extensions to [`fs::Metadata`].
374 /// [`fs::Metadata`]: ../../../../std/fs/struct.Metadata.html
375 #[stable(feature = "metadata_ext", since = "1.1.0")]
376 pub trait MetadataExt {
377 /// Returns the ID of the device containing the file.
384 /// use std::os::unix::fs::MetadataExt;
386 /// fn main() -> io::Result<()> {
387 /// let meta = fs::metadata("some_file")?;
388 /// let dev_id = meta.dev();
392 #[stable(feature = "metadata_ext", since = "1.1.0")]
393 fn dev(&self) -> u64;
394 /// Returns the inode number.
400 /// use std::os::unix::fs::MetadataExt;
403 /// fn main() -> io::Result<()> {
404 /// let meta = fs::metadata("some_file")?;
405 /// let inode = meta.ino();
409 #[stable(feature = "metadata_ext", since = "1.1.0")]
410 fn ino(&self) -> u64;
411 /// Returns the rights applied to this file.
417 /// use std::os::unix::fs::MetadataExt;
420 /// fn main() -> io::Result<()> {
421 /// let meta = fs::metadata("some_file")?;
422 /// let mode = meta.mode();
423 /// let user_has_write_access = mode & 0o200;
424 /// let user_has_read_write_access = mode & 0o600;
425 /// let group_has_read_access = mode & 0o040;
426 /// let others_have_exec_access = mode & 0o001;
430 #[stable(feature = "metadata_ext", since = "1.1.0")]
431 fn mode(&self) -> u32;
432 /// Returns the number of hard links pointing to this file.
438 /// use std::os::unix::fs::MetadataExt;
441 /// fn main() -> io::Result<()> {
442 /// let meta = fs::metadata("some_file")?;
443 /// let nb_hard_links = meta.nlink();
447 #[stable(feature = "metadata_ext", since = "1.1.0")]
448 fn nlink(&self) -> u64;
449 /// Returns the user ID of the owner of this file.
455 /// use std::os::unix::fs::MetadataExt;
458 /// fn main() -> io::Result<()> {
459 /// let meta = fs::metadata("some_file")?;
460 /// let user_id = meta.uid();
464 #[stable(feature = "metadata_ext", since = "1.1.0")]
465 fn uid(&self) -> u32;
466 /// Returns the group ID of the owner of this file.
472 /// use std::os::unix::fs::MetadataExt;
475 /// fn main() -> io::Result<()> {
476 /// let meta = fs::metadata("some_file")?;
477 /// let group_id = meta.gid();
481 #[stable(feature = "metadata_ext", since = "1.1.0")]
482 fn gid(&self) -> u32;
483 /// Returns the device ID of this file (if it is a special one).
489 /// use std::os::unix::fs::MetadataExt;
492 /// fn main() -> io::Result<()> {
493 /// let meta = fs::metadata("some_file")?;
494 /// let device_id = meta.rdev();
498 #[stable(feature = "metadata_ext", since = "1.1.0")]
499 fn rdev(&self) -> u64;
500 /// Returns the total size of this file in bytes.
506 /// use std::os::unix::fs::MetadataExt;
509 /// fn main() -> io::Result<()> {
510 /// let meta = fs::metadata("some_file")?;
511 /// let file_size = meta.size();
515 #[stable(feature = "metadata_ext", since = "1.1.0")]
516 fn size(&self) -> u64;
517 /// Returns the last access time of the file, in seconds since Unix Epoch.
523 /// use std::os::unix::fs::MetadataExt;
526 /// fn main() -> io::Result<()> {
527 /// let meta = fs::metadata("some_file")?;
528 /// let last_access_time = meta.atime();
532 #[stable(feature = "metadata_ext", since = "1.1.0")]
533 fn atime(&self) -> i64;
534 /// Returns the last access time of the file, in nanoseconds since [`atime`].
536 /// [`atime`]: #tymethod.atime
542 /// use std::os::unix::fs::MetadataExt;
545 /// fn main() -> io::Result<()> {
546 /// let meta = fs::metadata("some_file")?;
547 /// let nano_last_access_time = meta.atime_nsec();
551 #[stable(feature = "metadata_ext", since = "1.1.0")]
552 fn atime_nsec(&self) -> i64;
553 /// Returns the last modification time of the file, in seconds since Unix Epoch.
559 /// use std::os::unix::fs::MetadataExt;
562 /// fn main() -> io::Result<()> {
563 /// let meta = fs::metadata("some_file")?;
564 /// let last_modification_time = meta.mtime();
568 #[stable(feature = "metadata_ext", since = "1.1.0")]
569 fn mtime(&self) -> i64;
570 /// Returns the last modification time of the file, in nanoseconds since [`mtime`].
572 /// [`mtime`]: #tymethod.mtime
578 /// use std::os::unix::fs::MetadataExt;
581 /// fn main() -> io::Result<()> {
582 /// let meta = fs::metadata("some_file")?;
583 /// let nano_last_modification_time = meta.mtime_nsec();
587 #[stable(feature = "metadata_ext", since = "1.1.0")]
588 fn mtime_nsec(&self) -> i64;
589 /// Returns the last status change time of the file, in seconds since Unix Epoch.
595 /// use std::os::unix::fs::MetadataExt;
598 /// fn main() -> io::Result<()> {
599 /// let meta = fs::metadata("some_file")?;
600 /// let last_status_change_time = meta.ctime();
604 #[stable(feature = "metadata_ext", since = "1.1.0")]
605 fn ctime(&self) -> i64;
606 /// Returns the last status change time of the file, in nanoseconds since [`ctime`].
608 /// [`ctime`]: #tymethod.ctime
614 /// use std::os::unix::fs::MetadataExt;
617 /// fn main() -> io::Result<()> {
618 /// let meta = fs::metadata("some_file")?;
619 /// let nano_last_status_change_time = meta.ctime_nsec();
623 #[stable(feature = "metadata_ext", since = "1.1.0")]
624 fn ctime_nsec(&self) -> i64;
625 /// Returns the blocksize for filesystem I/O.
631 /// use std::os::unix::fs::MetadataExt;
634 /// fn main() -> io::Result<()> {
635 /// let meta = fs::metadata("some_file")?;
636 /// let blocksize = meta.blksize();
640 #[stable(feature = "metadata_ext", since = "1.1.0")]
641 fn blksize(&self) -> u64;
642 /// Returns the number of blocks allocated to the file, in 512-byte units.
644 /// Please note that this may be smaller than `st_size / 512` when the file has holes.
650 /// use std::os::unix::fs::MetadataExt;
653 /// fn main() -> io::Result<()> {
654 /// let meta = fs::metadata("some_file")?;
655 /// let blocks = meta.blocks();
659 #[stable(feature = "metadata_ext", since = "1.1.0")]
660 fn blocks(&self) -> u64;
663 #[stable(feature = "metadata_ext", since = "1.1.0")]
664 impl MetadataExt for fs::Metadata {
665 fn dev(&self) -> u64 {
668 fn ino(&self) -> u64 {
671 fn mode(&self) -> u32 {
674 fn nlink(&self) -> u64 {
677 fn uid(&self) -> u32 {
680 fn gid(&self) -> u32 {
683 fn rdev(&self) -> u64 {
686 fn size(&self) -> u64 {
689 fn atime(&self) -> i64 {
692 fn atime_nsec(&self) -> i64 {
695 fn mtime(&self) -> i64 {
698 fn mtime_nsec(&self) -> i64 {
701 fn ctime(&self) -> i64 {
704 fn ctime_nsec(&self) -> i64 {
707 fn blksize(&self) -> u64 {
710 fn blocks(&self) -> u64 {
715 /// Unix-specific extensions for [`FileType`].
717 /// Adds support for special Unix file types such as block/character devices,
718 /// pipes, and sockets.
720 /// [`FileType`]: ../../../../std/fs/struct.FileType.html
721 #[stable(feature = "file_type_ext", since = "1.5.0")]
722 pub trait FileTypeExt {
723 /// Returns `true` if this file type is a block device.
729 /// use std::os::unix::fs::FileTypeExt;
732 /// fn main() -> io::Result<()> {
733 /// let meta = fs::metadata("block_device_file")?;
734 /// let file_type = meta.file_type();
735 /// assert!(file_type.is_block_device());
739 #[stable(feature = "file_type_ext", since = "1.5.0")]
740 fn is_block_device(&self) -> bool;
741 /// Returns `true` if this file type is a char device.
747 /// use std::os::unix::fs::FileTypeExt;
750 /// fn main() -> io::Result<()> {
751 /// let meta = fs::metadata("char_device_file")?;
752 /// let file_type = meta.file_type();
753 /// assert!(file_type.is_char_device());
757 #[stable(feature = "file_type_ext", since = "1.5.0")]
758 fn is_char_device(&self) -> bool;
759 /// Returns `true` if this file type is a fifo.
765 /// use std::os::unix::fs::FileTypeExt;
768 /// fn main() -> io::Result<()> {
769 /// let meta = fs::metadata("fifo_file")?;
770 /// let file_type = meta.file_type();
771 /// assert!(file_type.is_fifo());
775 #[stable(feature = "file_type_ext", since = "1.5.0")]
776 fn is_fifo(&self) -> bool;
777 /// Returns `true` if this file type is a socket.
783 /// use std::os::unix::fs::FileTypeExt;
786 /// fn main() -> io::Result<()> {
787 /// let meta = fs::metadata("unix.socket")?;
788 /// let file_type = meta.file_type();
789 /// assert!(file_type.is_socket());
793 #[stable(feature = "file_type_ext", since = "1.5.0")]
794 fn is_socket(&self) -> bool;
797 #[stable(feature = "file_type_ext", since = "1.5.0")]
798 impl FileTypeExt for fs::FileType {
799 fn is_block_device(&self) -> bool {
800 self.as_inner().is(libc::S_IFBLK)
802 fn is_char_device(&self) -> bool {
803 self.as_inner().is(libc::S_IFCHR)
805 fn is_fifo(&self) -> bool {
806 self.as_inner().is(libc::S_IFIFO)
808 fn is_socket(&self) -> bool {
809 self.as_inner().is(libc::S_IFSOCK)
813 /// Unix-specific extension methods for [`fs::DirEntry`].
815 /// [`fs::DirEntry`]: ../../../../std/fs/struct.DirEntry.html
816 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
817 pub trait DirEntryExt {
818 /// Returns the underlying `d_ino` field in the contained `dirent`
825 /// use std::os::unix::fs::DirEntryExt;
827 /// if let Ok(entries) = fs::read_dir(".") {
828 /// for entry in entries {
829 /// if let Ok(entry) = entry {
830 /// // Here, `entry` is a `DirEntry`.
831 /// println!("{:?}: {}", entry.file_name(), entry.ino());
836 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
837 fn ino(&self) -> u64;
840 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
841 impl DirEntryExt for fs::DirEntry {
842 fn ino(&self) -> u64 {
843 self.as_inner().ino()
847 /// Creates a new symbolic link on the filesystem.
849 /// The `dst` path will be a symbolic link pointing to the `src` path.
853 /// On Windows, you must specify whether a symbolic link points to a file
854 /// or directory. Use `os::windows::fs::symlink_file` to create a
855 /// symbolic link to a file, or `os::windows::fs::symlink_dir` to create a
856 /// symbolic link to a directory. Additionally, the process must have
857 /// `SeCreateSymbolicLinkPrivilege` in order to be able to create a
863 /// use std::os::unix::fs;
865 /// fn main() -> std::io::Result<()> {
866 /// fs::symlink("a.txt", "b.txt")?;
870 #[stable(feature = "symlink", since = "1.1.0")]
871 pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
872 sys::fs::symlink(src.as_ref(), dst.as_ref())
875 /// Unix-specific extensions to [`fs::DirBuilder`].
877 /// [`fs::DirBuilder`]: ../../../../std/fs/struct.DirBuilder.html
878 #[stable(feature = "dir_builder", since = "1.6.0")]
879 pub trait DirBuilderExt {
880 /// Sets the mode to create new directories with. This option defaults to
886 /// use std::fs::DirBuilder;
887 /// use std::os::unix::fs::DirBuilderExt;
889 /// let mut builder = DirBuilder::new();
890 /// builder.mode(0o755);
892 #[stable(feature = "dir_builder", since = "1.6.0")]
893 fn mode(&mut self, mode: u32) -> &mut Self;
896 #[stable(feature = "dir_builder", since = "1.6.0")]
897 impl DirBuilderExt for fs::DirBuilder {
898 fn mode(&mut self, mode: u32) -> &mut fs::DirBuilder {
899 self.as_inner_mut().set_mode(mode);