1use libc::{c_int, mkdirat, renameat, unlinkat};
2
3cfg_select! {
4 not(any(
5 all(target_os = "linux", not(target_env = "musl")),
6 target_os = "l4re",
7 target_os = "hurd",
8 )) => {
9 use libc::{fstatat as fstatat64, openat as openat64};
10 }
11 _ => {
12 use libc::{fstatat64, openat64};
13 }
14}
15
16use super::{open64, stat64};
17use crate::ffi::CStr;
18use crate::os::fd::{AsFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
19#[cfg(target_family = "unix")]
20use crate::os::unix::io::{AsRawFd, FromRawFd};
21#[cfg(target_os = "wasi")]
22use crate::os::wasi::io::{AsRawFd, FromRawFd};
23use crate::path::Path;
24use crate::sys::fd::FileDesc;
25use crate::sys::fs::OpenOptions;
26use crate::sys::fs::unix::{File, FileAttr, debug_path_fd};
27use crate::sys::helpers::run_path_with_cstr;
28use crate::sys::{AsInner, FromInner, IntoInner, cvt, cvt_r};
29use crate::{fmt, fs, io, mem};
30
31const TRAVERSE_DIRECTORY: i32 = cfg_select! {
32 any(target_os = "freebsd", target_os = "aix") => libc::O_EXEC,
33 any(target_os = "linux", target_os = "android", target_os = "l4re") => libc::O_PATH,
34 target_os = "illumos" => libc::O_SEARCH,
35 _ => libc::O_RDONLY,
36};
37
38pub struct Dir(pub(super) OwnedFd);
39
40impl Dir {
41 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<Self> {
42 run_path_with_cstr(path, &|path| Self::open_with_c(path, opts))
43 }
44
45 pub fn open_for_traversal(path: &Path) -> io::Result<Self> {
46 run_path_with_cstr(path, &|path| Self::open_traversal_c(path))
47 }
48
49 pub fn duplicate(&self) -> io::Result<Self> {
50 Ok(Self(self.0.try_clone()?))
51 }
52
53 pub fn open_file(&self, path: &Path, opts: &OpenOptions) -> io::Result<File> {
54 run_path_with_cstr(path.as_ref(), &|path| self.open_file_c(path, opts, 0))
55 .map(FileDesc::from_inner)
56 .map(File)
57 }
58
59 pub fn self_metadata(&self) -> io::Result<FileAttr> {
60 let fd = self.0.as_raw_fd();
62 let f = mem::ManuallyDrop::new(File(
63 unsafe { FileDesc::from_raw_fd(fd) },
65 ));
66 f.file_attr()
67 }
68
69 pub fn remove_file(&self, path: &Path) -> io::Result<()> {
70 run_path_with_cstr(path, &|path| self.remove_c(path, false))
71 }
72
73 pub fn rename(&self, from: &Path, to_dir: &Self, to: &Path) -> io::Result<()> {
74 run_path_with_cstr(from, &|from| {
75 run_path_with_cstr(to, &|to| self.rename_c(from, to_dir, to))
76 })
77 }
78
79 pub fn open_dir(&self, path: &Path, opts: &OpenOptions) -> io::Result<Self> {
80 run_path_with_cstr(path, &|path| self.open_file_c(path, opts, libc::O_DIRECTORY)).map(Self)
81 }
82
83 pub fn create_dir(&self, path: &Path) -> io::Result<()> {
84 run_path_with_cstr(path.as_ref(), &|path| self.create_dir_c(path))
85 }
86
87 pub fn remove_dir(&self, path: &Path) -> io::Result<()> {
88 run_path_with_cstr(path, &|path| self.remove_c(path, true))
89 }
90
91 pub fn metadata(&self, path: &Path) -> io::Result<FileAttr> {
92 run_path_with_cstr(path, &|path| {
93 self.metadata_c(path, false)
94 })
95 }
96
97 pub fn symlink_metadata(&self, path: &Path) -> io::Result<FileAttr> {
98 run_path_with_cstr(path, &|path| self.metadata_c(path, true))
99 }
100
101 fn open_with_c(path: &CStr, opts: &OpenOptions) -> io::Result<Self> {
102 let flags = libc::O_CLOEXEC
103 | libc::O_DIRECTORY
104 | opts.get_access_mode()?
105 | opts.get_creation_mode()?
106 | (opts.custom_flags as c_int & !libc::O_ACCMODE);
107 let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode as c_int) })?;
108 Ok(Self(unsafe { OwnedFd::from_raw_fd(fd) }))
109 }
110
111 fn open_traversal_c(path: &CStr) -> io::Result<Self> {
112 let flags = libc::O_CLOEXEC | libc::O_DIRECTORY | TRAVERSE_DIRECTORY;
113 let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, 0) })?;
114 Ok(Self(unsafe { OwnedFd::from_raw_fd(fd) }))
115 }
116
117 fn open_file_c(
118 &self,
119 path: &CStr,
120 opts: &OpenOptions,
121 extra_flags: c_int,
122 ) -> io::Result<OwnedFd> {
123 let flags = libc::O_CLOEXEC
124 | opts.get_access_mode()?
125 | opts.get_creation_mode()?
126 | (opts.custom_flags as c_int & !libc::O_ACCMODE)
127 | extra_flags;
128 let fd = cvt_r(|| unsafe {
129 openat64(self.0.as_raw_fd(), path.as_ptr(), flags, opts.mode as c_int)
130 })?;
131 Ok(unsafe { OwnedFd::from_raw_fd(fd) })
132 }
133
134 fn remove_c(&self, path: &CStr, remove_dir: bool) -> io::Result<()> {
135 cvt(unsafe {
136 unlinkat(
137 self.0.as_raw_fd(),
138 path.as_ptr(),
139 if remove_dir { libc::AT_REMOVEDIR } else { 0 },
140 )
141 })
142 .map(|_| ())
143 }
144
145 fn rename_c(&self, from: &CStr, to_dir: &Self, to: &CStr) -> io::Result<()> {
146 cvt(unsafe {
147 renameat(self.0.as_raw_fd(), from.as_ptr(), to_dir.0.as_raw_fd(), to.as_ptr())
148 })
149 .map(|_| ())
150 }
151
152 fn create_dir_c(&self, path: &CStr) -> io::Result<()> {
153 cvt(unsafe { mkdirat(self.0.as_raw_fd(), path.as_ptr(), 0o777) }).map(|_| ())
154 }
155
156 pub(super) fn metadata_c(&self, path: &CStr, symlink_nofollow: bool) -> io::Result<FileAttr> {
157 let fd = self.0.as_raw_fd();
158 let flag = if symlink_nofollow { libc::AT_SYMLINK_NOFOLLOW } else { 0 };
159
160 cfg_has_statx! {
161 if let Some(ret) = unsafe { super::try_statx(
162 fd,
163 path.as_ptr(),
164 flag | libc::AT_STATX_SYNC_AS_STAT,
165 libc::STATX_BASIC_STATS | libc::STATX_BTIME,
166 ) } {
167 return ret;
168 }
169 }
170
171 let mut stat: stat64 = unsafe { mem::zeroed() };
172 cvt(unsafe { fstatat64(fd, path.as_ptr(), &mut stat, flag) })?;
173 Ok(FileAttr::from_stat64(stat))
174 }
175}
176
177impl fmt::Debug for Dir {
178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 let fd = self.0.as_raw_fd();
180 let mut b = debug_path_fd(fd, f, "Dir");
181 b.finish()
182 }
183}
184
185#[unstable(feature = "dirfd", issue = "120426")]
186impl AsRawFd for fs::Dir {
187 fn as_raw_fd(&self) -> RawFd {
188 self.as_inner().0.as_raw_fd()
189 }
190}
191
192#[unstable(feature = "dirfd", issue = "120426")]
193impl IntoRawFd for fs::Dir {
194 fn into_raw_fd(self) -> RawFd {
195 self.into_inner().0.into_raw_fd()
196 }
197}
198
199#[unstable(feature = "dirfd", issue = "120426")]
200impl FromRawFd for fs::Dir {
201 unsafe fn from_raw_fd(fd: RawFd) -> Self {
202 Self::from_inner(Dir(unsafe { FromRawFd::from_raw_fd(fd) }))
203 }
204}
205
206#[unstable(feature = "dirfd", issue = "120426")]
207impl AsFd for fs::Dir {
208 fn as_fd(&self) -> BorrowedFd<'_> {
209 self.as_inner().0.as_fd()
210 }
211}
212
213#[unstable(feature = "dirfd", issue = "120426")]
214impl From<fs::Dir> for OwnedFd {
215 fn from(value: fs::Dir) -> Self {
216 value.into_inner().0
217 }
218}
219
220#[unstable(feature = "dirfd", issue = "120426")]
221impl From<OwnedFd> for fs::Dir {
222 fn from(value: OwnedFd) -> Self {
223 Self::from_inner(Dir(value))
224 }
225}