Skip to main content

fs_err/os/
unix.rs

1/// Unix-specific extensions to wrappers in fs-err for [`std::fs`] types.
2pub mod fs {
3    use std::io;
4    use std::path::Path;
5
6    #[allow(unused_imports)]
7    use crate::{Error, ErrorKind};
8    use crate::{SourceDestError, SourceDestErrorKind};
9
10    /// Creates a new symbolic link on the filesystem.
11    ///
12    /// The `link` path will be a symbolic link pointing to the `original` path.
13    ///
14    /// Wrapper for [`std::os::unix::fs::symlink`].
15    pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
16        let original = original.as_ref();
17        let link = link.as_ref();
18        std::os::unix::fs::symlink(original, link).map_err(|err| {
19            SourceDestError::build(err, SourceDestErrorKind::Symlink, link, original)
20        })
21    }
22
23    /// Change the owner and group of the specified path.
24    ///
25    /// Specifying either the uid or gid as `None` will leave it unchanged.
26    ///
27    /// Wrapper for [`std::os::unix::fs::chown`]
28    #[cfg(rustc_1_73)]
29    pub fn chown<P: AsRef<Path>>(path: P, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {
30        let path = path.as_ref();
31        std::os::unix::fs::chown(path, uid, gid)
32            .map_err(|err| Error::build(err, ErrorKind::Chown, path))
33    }
34
35    /// Change the owner and group of the specified path, without dereferencing symbolic links.
36    ///
37    /// Identical to [`chown`], except that if called on a symbolic link, this will change the owner
38    /// and group of the link itself rather than the owner and group of the link target.
39    ///
40    /// Wrapper for [`std::os::unix::fs::lchown`]
41    #[cfg(rustc_1_73)]
42    pub fn lchown<P: AsRef<Path>>(path: P, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {
43        let path = path.as_ref();
44        std::os::unix::fs::lchown(path, uid, gid)
45            .map_err(|err| Error::build(err, ErrorKind::Lchown, path))
46    }
47
48    /// Change the root directory of the current process to the specified path.
49    ///
50    /// This typically requires privileges, such as root or a specific capability.
51    ///
52    /// Wrapper for [`std::os::unix::fs::chroot`]
53    #[cfg(rustc_1_56)]
54    pub fn chroot<P: AsRef<Path>>(path: P) -> io::Result<()> {
55        let path = path.as_ref();
56        std::os::unix::fs::chroot(path).map_err(|err| Error::build(err, ErrorKind::Chroot, path))
57    }
58
59    /// Wrapper for [`std::os::unix::fs::FileExt`].
60    ///
61    /// The std traits might be extended in the future (See issue [#49961](https://github.com/rust-lang/rust/issues/49961#issuecomment-382751777)).
62    /// This trait is sealed and can not be implemented by other crates.
63    pub trait FileExt: crate::Sealed {
64        /// Wrapper for [`std::os::unix::fs::FileExt::read_at`].
65        fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
66        /// Wrapper for [`std::os::unix::fs::FileExt::read_exact_at`].
67        fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()>;
68        /// Wrapper for [`std::os::unix::fs::FileExt::write_at`].
69        fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
70        /// Wrapper for [`std::os::unix::fs::FileExt::write_all_at`].
71        fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()>;
72    }
73
74    /// Wrapper for [`std::os::unix::fs::OpenOptionsExt`].
75    ///
76    /// The std traits might be extended in the future (See issue [#49961](https://github.com/rust-lang/rust/issues/49961#issuecomment-382751777)).
77    /// This trait is sealed and can not be implemented by other crates.
78    pub trait OpenOptionsExt: crate::Sealed {
79        /// Wrapper for [`std::os::unix::fs::OpenOptionsExt::mode`].
80        fn mode(&mut self, mode: u32) -> &mut Self;
81        /// Wrapper for [`std::os::unix::fs::OpenOptionsExt::custom_flags`].
82        fn custom_flags(&mut self, flags: i32) -> &mut Self;
83    }
84}