Skip to main content

std/sys/fs/unix/
dir.rs

1use libc::c_int;
2
3cfg_select! {
4    not(
5        any(
6            all(target_os = "linux", not(target_env = "musl")),
7            target_os = "l4re",
8            target_os = "android",
9            target_os = "hurd",
10        )
11    ) => {
12        use libc::{open as open64, openat as openat64};
13    }
14    _ => {
15        use libc::{open64, openat64};
16    }
17}
18
19use crate::ffi::CStr;
20use crate::os::fd::{AsFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
21#[cfg(target_family = "unix")]
22use crate::os::unix::io::{AsRawFd, FromRawFd};
23#[cfg(target_os = "wasi")]
24use crate::os::wasi::io::{AsRawFd, FromRawFd};
25use crate::path::Path;
26use crate::sys::fd::FileDesc;
27use crate::sys::fs::OpenOptions;
28use crate::sys::fs::unix::{File, FileAttr, debug_path_fd};
29use crate::sys::helpers::run_path_with_cstr;
30use crate::sys::{AsInner, FromInner, IntoInner, cvt_r};
31use crate::{fmt, fs, io};
32
33pub struct Dir(OwnedFd);
34
35impl Dir {
36    pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<Self> {
37        run_path_with_cstr(path, &|path| Self::open_with_c(path, opts))
38    }
39
40    pub fn open_file(&self, path: &Path, opts: &OpenOptions) -> io::Result<File> {
41        run_path_with_cstr(path.as_ref(), &|path| self.open_file_c(path, &opts))
42    }
43
44    pub fn metadata(&self) -> io::Result<FileAttr> {
45        // Reuse the implementation for files, which should work for all FDs.
46        let fd = self.0.as_raw_fd();
47        let f = core::mem::ManuallyDrop::new(File(
48            // SAFETY: we borrowed `self` so the FD will not be closed while this function runs.
49            unsafe { FileDesc::from_raw_fd(fd) },
50        ));
51        f.file_attr()
52    }
53
54    pub fn open_with_c(path: &CStr, opts: &OpenOptions) -> io::Result<Self> {
55        let flags = libc::O_CLOEXEC
56            | libc::O_DIRECTORY
57            | opts.get_access_mode()?
58            | opts.get_creation_mode()?
59            | (opts.custom_flags as c_int & !libc::O_ACCMODE);
60        let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode as c_int) })?;
61        Ok(Self(unsafe { OwnedFd::from_raw_fd(fd) }))
62    }
63
64    fn open_file_c(&self, path: &CStr, opts: &OpenOptions) -> io::Result<File> {
65        let flags = libc::O_CLOEXEC
66            | opts.get_access_mode()?
67            | opts.get_creation_mode()?
68            | (opts.custom_flags as c_int & !libc::O_ACCMODE);
69        let fd = cvt_r(|| unsafe {
70            openat64(self.0.as_raw_fd(), path.as_ptr(), flags, opts.mode as c_int)
71        })?;
72        Ok(File(unsafe { FileDesc::from_raw_fd(fd) }))
73    }
74}
75
76impl fmt::Debug for Dir {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        let fd = self.0.as_raw_fd();
79        let mut b = debug_path_fd(fd, f, "Dir");
80        b.finish()
81    }
82}
83
84#[unstable(feature = "dirfd", issue = "120426")]
85impl AsRawFd for fs::Dir {
86    fn as_raw_fd(&self) -> RawFd {
87        self.as_inner().0.as_raw_fd()
88    }
89}
90
91#[unstable(feature = "dirfd", issue = "120426")]
92impl IntoRawFd for fs::Dir {
93    fn into_raw_fd(self) -> RawFd {
94        self.into_inner().0.into_raw_fd()
95    }
96}
97
98#[unstable(feature = "dirfd", issue = "120426")]
99impl FromRawFd for fs::Dir {
100    unsafe fn from_raw_fd(fd: RawFd) -> Self {
101        Self::from_inner(Dir(unsafe { FromRawFd::from_raw_fd(fd) }))
102    }
103}
104
105#[unstable(feature = "dirfd", issue = "120426")]
106impl AsFd for fs::Dir {
107    fn as_fd(&self) -> BorrowedFd<'_> {
108        self.as_inner().0.as_fd()
109    }
110}
111
112#[unstable(feature = "dirfd", issue = "120426")]
113impl From<fs::Dir> for OwnedFd {
114    fn from(value: fs::Dir) -> Self {
115        value.into_inner().0
116    }
117}
118
119#[unstable(feature = "dirfd", issue = "120426")]
120impl From<OwnedFd> for fs::Dir {
121    fn from(value: OwnedFd) -> Self {
122        Self::from_inner(Dir(value))
123    }
124}