]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unix/fs.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / library / std / src / sys / unix / fs.rs
1 // miri has some special hacks here that make things unused.
2 #![cfg_attr(miri, allow(unused))]
3
4 use crate::os::unix::prelude::*;
5
6 use crate::ffi::{CStr, OsStr, OsString};
7 use crate::fmt;
8 use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
9 use crate::mem;
10 #[cfg(any(
11 target_os = "android",
12 target_os = "linux",
13 target_os = "solaris",
14 target_os = "fuchsia",
15 target_os = "redox",
16 target_os = "illumos",
17 target_os = "nto",
18 ))]
19 use crate::mem::MaybeUninit;
20 use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
21 use crate::path::{Path, PathBuf};
22 use crate::ptr;
23 use crate::sync::Arc;
24 use crate::sys::common::small_c_string::run_path_with_cstr;
25 use crate::sys::fd::FileDesc;
26 use crate::sys::time::SystemTime;
27 use crate::sys::{cvt, cvt_r};
28 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
29
30 #[cfg(any(
31 all(target_os = "linux", target_env = "gnu"),
32 target_os = "macos",
33 target_os = "ios",
34 target_os = "watchos",
35 ))]
36 use crate::sys::weak::syscall;
37 #[cfg(any(target_os = "android", target_os = "macos", target_os = "solaris"))]
38 use crate::sys::weak::weak;
39
40 use libc::{c_int, mode_t};
41
42 #[cfg(any(
43 target_os = "macos",
44 target_os = "ios",
45 target_os = "watchos",
46 target_os = "solaris",
47 all(target_os = "linux", target_env = "gnu")
48 ))]
49 use libc::c_char;
50 #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "android"))]
51 use libc::dirfd;
52 #[cfg(any(target_os = "linux", target_os = "emscripten"))]
53 use libc::fstatat64;
54 #[cfg(any(
55 target_os = "android",
56 target_os = "solaris",
57 target_os = "fuchsia",
58 target_os = "redox",
59 target_os = "illumos",
60 target_os = "nto",
61 ))]
62 use libc::readdir as readdir64;
63 #[cfg(target_os = "linux")]
64 use libc::readdir64;
65 #[cfg(any(target_os = "emscripten", target_os = "l4re"))]
66 use libc::readdir64_r;
67 #[cfg(not(any(
68 target_os = "android",
69 target_os = "linux",
70 target_os = "emscripten",
71 target_os = "solaris",
72 target_os = "illumos",
73 target_os = "l4re",
74 target_os = "fuchsia",
75 target_os = "redox",
76 target_os = "nto",
77 )))]
78 use libc::readdir_r as readdir64_r;
79 #[cfg(target_os = "android")]
80 use libc::{
81 dirent as dirent64, fstat as fstat64, fstatat as fstatat64, ftruncate64, lseek64,
82 lstat as lstat64, off64_t, open as open64, stat as stat64,
83 };
84 #[cfg(not(any(
85 target_os = "linux",
86 target_os = "emscripten",
87 target_os = "l4re",
88 target_os = "android"
89 )))]
90 use libc::{
91 dirent as dirent64, fstat as fstat64, ftruncate as ftruncate64, lseek as lseek64,
92 lstat as lstat64, off_t as off64_t, open as open64, stat as stat64,
93 };
94 #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "l4re"))]
95 use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, stat64};
96
97 pub use crate::sys_common::fs::try_exists;
98
99 pub struct File(FileDesc);
100
101 // FIXME: This should be available on Linux with all `target_env`.
102 // But currently only glibc exposes `statx` fn and structs.
103 // We don't want to import unverified raw C structs here directly.
104 // https://github.com/rust-lang/rust/pull/67774
105 macro_rules! cfg_has_statx {
106 ({ $($then_tt:tt)* } else { $($else_tt:tt)* }) => {
107 cfg_if::cfg_if! {
108 if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
109 $($then_tt)*
110 } else {
111 $($else_tt)*
112 }
113 }
114 };
115 ($($block_inner:tt)*) => {
116 #[cfg(all(target_os = "linux", target_env = "gnu"))]
117 {
118 $($block_inner)*
119 }
120 };
121 }
122
123 cfg_has_statx! {{
124 #[derive(Clone)]
125 pub struct FileAttr {
126 stat: stat64,
127 statx_extra_fields: Option<StatxExtraFields>,
128 }
129
130 #[derive(Clone)]
131 struct StatxExtraFields {
132 // This is needed to check if btime is supported by the filesystem.
133 stx_mask: u32,
134 stx_btime: libc::statx_timestamp,
135 // With statx, we can overcome 32-bit `time_t` too.
136 #[cfg(target_pointer_width = "32")]
137 stx_atime: libc::statx_timestamp,
138 #[cfg(target_pointer_width = "32")]
139 stx_ctime: libc::statx_timestamp,
140 #[cfg(target_pointer_width = "32")]
141 stx_mtime: libc::statx_timestamp,
142
143 }
144
145 // We prefer `statx` on Linux if available, which contains file creation time,
146 // as well as 64-bit timestamps of all kinds.
147 // Default `stat64` contains no creation time and may have 32-bit `time_t`.
148 unsafe fn try_statx(
149 fd: c_int,
150 path: *const c_char,
151 flags: i32,
152 mask: u32,
153 ) -> Option<io::Result<FileAttr>> {
154 use crate::sync::atomic::{AtomicU8, Ordering};
155
156 // Linux kernel prior to 4.11 or glibc prior to glibc 2.28 don't support `statx`.
157 // We check for it on first failure and remember availability to avoid having to
158 // do it again.
159 #[repr(u8)]
160 enum STATX_STATE{ Unknown = 0, Present, Unavailable }
161 static STATX_SAVED_STATE: AtomicU8 = AtomicU8::new(STATX_STATE::Unknown as u8);
162
163 syscall! {
164 fn statx(
165 fd: c_int,
166 pathname: *const c_char,
167 flags: c_int,
168 mask: libc::c_uint,
169 statxbuf: *mut libc::statx
170 ) -> c_int
171 }
172
173 if STATX_SAVED_STATE.load(Ordering::Relaxed) == STATX_STATE::Unavailable as u8 {
174 return None;
175 }
176
177 let mut buf: libc::statx = mem::zeroed();
178 if let Err(err) = cvt(statx(fd, path, flags, mask, &mut buf)) {
179 if STATX_SAVED_STATE.load(Ordering::Relaxed) == STATX_STATE::Present as u8 {
180 return Some(Err(err));
181 }
182
183 // Availability not checked yet.
184 //
185 // First try the cheap way.
186 if err.raw_os_error() == Some(libc::ENOSYS) {
187 STATX_SAVED_STATE.store(STATX_STATE::Unavailable as u8, Ordering::Relaxed);
188 return None;
189 }
190
191 // Error other than `ENOSYS` is not a good enough indicator -- it is
192 // known that `EPERM` can be returned as a result of using seccomp to
193 // block the syscall.
194 // Availability is checked by performing a call which expects `EFAULT`
195 // if the syscall is usable.
196 // See: https://github.com/rust-lang/rust/issues/65662
197 // FIXME this can probably just do the call if `EPERM` was received, but
198 // previous iteration of the code checked it for all errors and for now
199 // this is retained.
200 // FIXME what about transient conditions like `ENOMEM`?
201 let err2 = cvt(statx(0, ptr::null(), 0, libc::STATX_ALL, ptr::null_mut()))
202 .err()
203 .and_then(|e| e.raw_os_error());
204 if err2 == Some(libc::EFAULT) {
205 STATX_SAVED_STATE.store(STATX_STATE::Present as u8, Ordering::Relaxed);
206 return Some(Err(err));
207 } else {
208 STATX_SAVED_STATE.store(STATX_STATE::Unavailable as u8, Ordering::Relaxed);
209 return None;
210 }
211 }
212
213 // We cannot fill `stat64` exhaustively because of private padding fields.
214 let mut stat: stat64 = mem::zeroed();
215 // `c_ulong` on gnu-mips, `dev_t` otherwise
216 stat.st_dev = libc::makedev(buf.stx_dev_major, buf.stx_dev_minor) as _;
217 stat.st_ino = buf.stx_ino as libc::ino64_t;
218 stat.st_nlink = buf.stx_nlink as libc::nlink_t;
219 stat.st_mode = buf.stx_mode as libc::mode_t;
220 stat.st_uid = buf.stx_uid as libc::uid_t;
221 stat.st_gid = buf.stx_gid as libc::gid_t;
222 stat.st_rdev = libc::makedev(buf.stx_rdev_major, buf.stx_rdev_minor) as _;
223 stat.st_size = buf.stx_size as off64_t;
224 stat.st_blksize = buf.stx_blksize as libc::blksize_t;
225 stat.st_blocks = buf.stx_blocks as libc::blkcnt64_t;
226 stat.st_atime = buf.stx_atime.tv_sec as libc::time_t;
227 // `i64` on gnu-x86_64-x32, `c_ulong` otherwise.
228 stat.st_atime_nsec = buf.stx_atime.tv_nsec as _;
229 stat.st_mtime = buf.stx_mtime.tv_sec as libc::time_t;
230 stat.st_mtime_nsec = buf.stx_mtime.tv_nsec as _;
231 stat.st_ctime = buf.stx_ctime.tv_sec as libc::time_t;
232 stat.st_ctime_nsec = buf.stx_ctime.tv_nsec as _;
233
234 let extra = StatxExtraFields {
235 stx_mask: buf.stx_mask,
236 stx_btime: buf.stx_btime,
237 // Store full times to avoid 32-bit `time_t` truncation.
238 #[cfg(target_pointer_width = "32")]
239 stx_atime: buf.stx_atime,
240 #[cfg(target_pointer_width = "32")]
241 stx_ctime: buf.stx_ctime,
242 #[cfg(target_pointer_width = "32")]
243 stx_mtime: buf.stx_mtime,
244 };
245
246 Some(Ok(FileAttr { stat, statx_extra_fields: Some(extra) }))
247 }
248
249 } else {
250 #[derive(Clone)]
251 pub struct FileAttr {
252 stat: stat64,
253 }
254 }}
255
256 // all DirEntry's will have a reference to this struct
257 struct InnerReadDir {
258 dirp: Dir,
259 root: PathBuf,
260 }
261
262 pub struct ReadDir {
263 inner: Arc<InnerReadDir>,
264 end_of_stream: bool,
265 }
266
267 impl ReadDir {
268 fn new(inner: InnerReadDir) -> Self {
269 Self { inner: Arc::new(inner), end_of_stream: false }
270 }
271 }
272
273 struct Dir(*mut libc::DIR);
274
275 unsafe impl Send for Dir {}
276 unsafe impl Sync for Dir {}
277
278 #[cfg(any(
279 target_os = "android",
280 target_os = "linux",
281 target_os = "solaris",
282 target_os = "illumos",
283 target_os = "fuchsia",
284 target_os = "redox",
285 target_os = "nto",
286 ))]
287 pub struct DirEntry {
288 dir: Arc<InnerReadDir>,
289 entry: dirent64_min,
290 // We need to store an owned copy of the entry name on platforms that use
291 // readdir() (not readdir_r()), because a) struct dirent may use a flexible
292 // array to store the name, b) it lives only until the next readdir() call.
293 name: crate::ffi::CString,
294 }
295
296 // Define a minimal subset of fields we need from `dirent64`, especially since
297 // we're not using the immediate `d_name` on these targets. Keeping this as an
298 // `entry` field in `DirEntry` helps reduce the `cfg` boilerplate elsewhere.
299 #[cfg(any(
300 target_os = "android",
301 target_os = "linux",
302 target_os = "solaris",
303 target_os = "illumos",
304 target_os = "fuchsia",
305 target_os = "redox",
306 target_os = "nto",
307 ))]
308 struct dirent64_min {
309 d_ino: u64,
310 #[cfg(not(any(target_os = "solaris", target_os = "illumos", target_os = "nto")))]
311 d_type: u8,
312 }
313
314 #[cfg(not(any(
315 target_os = "android",
316 target_os = "linux",
317 target_os = "solaris",
318 target_os = "illumos",
319 target_os = "fuchsia",
320 target_os = "redox",
321 target_os = "nto",
322 )))]
323 pub struct DirEntry {
324 dir: Arc<InnerReadDir>,
325 // The full entry includes a fixed-length `d_name`.
326 entry: dirent64,
327 }
328
329 #[derive(Clone, Debug)]
330 pub struct OpenOptions {
331 // generic
332 read: bool,
333 write: bool,
334 append: bool,
335 truncate: bool,
336 create: bool,
337 create_new: bool,
338 // system-specific
339 custom_flags: i32,
340 mode: mode_t,
341 }
342
343 #[derive(Clone, PartialEq, Eq, Debug)]
344 pub struct FilePermissions {
345 mode: mode_t,
346 }
347
348 #[derive(Copy, Clone, Debug, Default)]
349 pub struct FileTimes {
350 accessed: Option<SystemTime>,
351 modified: Option<SystemTime>,
352 }
353
354 #[derive(Copy, Clone, Eq, Debug)]
355 pub struct FileType {
356 mode: mode_t,
357 }
358
359 impl PartialEq for FileType {
360 fn eq(&self, other: &Self) -> bool {
361 self.masked() == other.masked()
362 }
363 }
364
365 impl core::hash::Hash for FileType {
366 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
367 self.masked().hash(state);
368 }
369 }
370
371 #[derive(Debug)]
372 pub struct DirBuilder {
373 mode: mode_t,
374 }
375
376 cfg_has_statx! {{
377 impl FileAttr {
378 fn from_stat64(stat: stat64) -> Self {
379 Self { stat, statx_extra_fields: None }
380 }
381
382 #[cfg(target_pointer_width = "32")]
383 pub fn stx_mtime(&self) -> Option<&libc::statx_timestamp> {
384 if let Some(ext) = &self.statx_extra_fields {
385 if (ext.stx_mask & libc::STATX_MTIME) != 0 {
386 return Some(&ext.stx_mtime);
387 }
388 }
389 None
390 }
391
392 #[cfg(target_pointer_width = "32")]
393 pub fn stx_atime(&self) -> Option<&libc::statx_timestamp> {
394 if let Some(ext) = &self.statx_extra_fields {
395 if (ext.stx_mask & libc::STATX_ATIME) != 0 {
396 return Some(&ext.stx_atime);
397 }
398 }
399 None
400 }
401
402 #[cfg(target_pointer_width = "32")]
403 pub fn stx_ctime(&self) -> Option<&libc::statx_timestamp> {
404 if let Some(ext) = &self.statx_extra_fields {
405 if (ext.stx_mask & libc::STATX_CTIME) != 0 {
406 return Some(&ext.stx_ctime);
407 }
408 }
409 None
410 }
411 }
412 } else {
413 impl FileAttr {
414 fn from_stat64(stat: stat64) -> Self {
415 Self { stat }
416 }
417 }
418 }}
419
420 impl FileAttr {
421 pub fn size(&self) -> u64 {
422 self.stat.st_size as u64
423 }
424 pub fn perm(&self) -> FilePermissions {
425 FilePermissions { mode: (self.stat.st_mode as mode_t) }
426 }
427
428 pub fn file_type(&self) -> FileType {
429 FileType { mode: self.stat.st_mode as mode_t }
430 }
431 }
432
433 #[cfg(target_os = "netbsd")]
434 impl FileAttr {
435 pub fn modified(&self) -> io::Result<SystemTime> {
436 Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64))
437 }
438
439 pub fn accessed(&self) -> io::Result<SystemTime> {
440 Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64))
441 }
442
443 pub fn created(&self) -> io::Result<SystemTime> {
444 Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64))
445 }
446 }
447
448 #[cfg(not(any(target_os = "netbsd", target_os = "nto")))]
449 impl FileAttr {
450 #[cfg(not(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon")))]
451 pub fn modified(&self) -> io::Result<SystemTime> {
452 #[cfg(target_pointer_width = "32")]
453 cfg_has_statx! {
454 if let Some(mtime) = self.stx_mtime() {
455 return Ok(SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64));
456 }
457 }
458
459 Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64))
460 }
461
462 #[cfg(any(target_os = "vxworks", target_os = "espidf"))]
463 pub fn modified(&self) -> io::Result<SystemTime> {
464 Ok(SystemTime::new(self.stat.st_mtime as i64, 0))
465 }
466
467 #[cfg(target_os = "horizon")]
468 pub fn modified(&self) -> io::Result<SystemTime> {
469 Ok(SystemTime::from(self.stat.st_mtim))
470 }
471
472 #[cfg(not(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon")))]
473 pub fn accessed(&self) -> io::Result<SystemTime> {
474 #[cfg(target_pointer_width = "32")]
475 cfg_has_statx! {
476 if let Some(atime) = self.stx_atime() {
477 return Ok(SystemTime::new(atime.tv_sec, atime.tv_nsec as i64));
478 }
479 }
480
481 Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64))
482 }
483
484 #[cfg(any(target_os = "vxworks", target_os = "espidf"))]
485 pub fn accessed(&self) -> io::Result<SystemTime> {
486 Ok(SystemTime::new(self.stat.st_atime as i64, 0))
487 }
488
489 #[cfg(target_os = "horizon")]
490 pub fn accessed(&self) -> io::Result<SystemTime> {
491 Ok(SystemTime::from(self.stat.st_atim))
492 }
493
494 #[cfg(any(
495 target_os = "freebsd",
496 target_os = "openbsd",
497 target_os = "macos",
498 target_os = "ios",
499 target_os = "watchos",
500 ))]
501 pub fn created(&self) -> io::Result<SystemTime> {
502 Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64))
503 }
504
505 #[cfg(not(any(
506 target_os = "freebsd",
507 target_os = "openbsd",
508 target_os = "macos",
509 target_os = "ios",
510 target_os = "watchos",
511 )))]
512 pub fn created(&self) -> io::Result<SystemTime> {
513 cfg_has_statx! {
514 if let Some(ext) = &self.statx_extra_fields {
515 return if (ext.stx_mask & libc::STATX_BTIME) != 0 {
516 Ok(SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64))
517 } else {
518 Err(io::const_io_error!(
519 io::ErrorKind::Uncategorized,
520 "creation time is not available for the filesystem",
521 ))
522 };
523 }
524 }
525
526 Err(io::const_io_error!(
527 io::ErrorKind::Unsupported,
528 "creation time is not available on this platform \
529 currently",
530 ))
531 }
532 }
533
534 #[cfg(target_os = "nto")]
535 impl FileAttr {
536 pub fn modified(&self) -> io::Result<SystemTime> {
537 Ok(SystemTime::new(self.stat.st_mtim.tv_sec, self.stat.st_mtim.tv_nsec))
538 }
539
540 pub fn accessed(&self) -> io::Result<SystemTime> {
541 Ok(SystemTime::new(self.stat.st_atim.tv_sec, self.stat.st_atim.tv_nsec))
542 }
543
544 pub fn created(&self) -> io::Result<SystemTime> {
545 Ok(SystemTime::new(self.stat.st_ctim.tv_sec, self.stat.st_ctim.tv_nsec))
546 }
547 }
548
549 impl AsInner<stat64> for FileAttr {
550 fn as_inner(&self) -> &stat64 {
551 &self.stat
552 }
553 }
554
555 impl FilePermissions {
556 pub fn readonly(&self) -> bool {
557 // check if any class (owner, group, others) has write permission
558 self.mode & 0o222 == 0
559 }
560
561 pub fn set_readonly(&mut self, readonly: bool) {
562 if readonly {
563 // remove write permission for all classes; equivalent to `chmod a-w <file>`
564 self.mode &= !0o222;
565 } else {
566 // add write permission for all classes; equivalent to `chmod a+w <file>`
567 self.mode |= 0o222;
568 }
569 }
570 pub fn mode(&self) -> u32 {
571 self.mode as u32
572 }
573 }
574
575 impl FileTimes {
576 pub fn set_accessed(&mut self, t: SystemTime) {
577 self.accessed = Some(t);
578 }
579
580 pub fn set_modified(&mut self, t: SystemTime) {
581 self.modified = Some(t);
582 }
583 }
584
585 impl FileType {
586 pub fn is_dir(&self) -> bool {
587 self.is(libc::S_IFDIR)
588 }
589 pub fn is_file(&self) -> bool {
590 self.is(libc::S_IFREG)
591 }
592 pub fn is_symlink(&self) -> bool {
593 self.is(libc::S_IFLNK)
594 }
595
596 pub fn is(&self, mode: mode_t) -> bool {
597 self.masked() == mode
598 }
599
600 fn masked(&self) -> mode_t {
601 self.mode & libc::S_IFMT
602 }
603 }
604
605 impl FromInner<u32> for FilePermissions {
606 fn from_inner(mode: u32) -> FilePermissions {
607 FilePermissions { mode: mode as mode_t }
608 }
609 }
610
611 impl fmt::Debug for ReadDir {
612 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
613 // This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
614 // Thus the result will be e g 'ReadDir("/home")'
615 fmt::Debug::fmt(&*self.inner.root, f)
616 }
617 }
618
619 impl Iterator for ReadDir {
620 type Item = io::Result<DirEntry>;
621
622 #[cfg(any(
623 target_os = "android",
624 target_os = "linux",
625 target_os = "solaris",
626 target_os = "fuchsia",
627 target_os = "redox",
628 target_os = "illumos",
629 target_os = "nto",
630 ))]
631 fn next(&mut self) -> Option<io::Result<DirEntry>> {
632 if self.end_of_stream {
633 return None;
634 }
635
636 unsafe {
637 loop {
638 // As of POSIX.1-2017, readdir() is not required to be thread safe; only
639 // readdir_r() is. However, readdir_r() cannot correctly handle platforms
640 // with unlimited or variable NAME_MAX. Many modern platforms guarantee
641 // thread safety for readdir() as long an individual DIR* is not accessed
642 // concurrently, which is sufficient for Rust.
643 super::os::set_errno(0);
644 let entry_ptr = readdir64(self.inner.dirp.0);
645 if entry_ptr.is_null() {
646 // We either encountered an error, or reached the end. Either way,
647 // the next call to next() should return None.
648 self.end_of_stream = true;
649
650 // To distinguish between errors and end-of-directory, we had to clear
651 // errno beforehand to check for an error now.
652 return match super::os::errno() {
653 0 => None,
654 e => Some(Err(Error::from_raw_os_error(e))),
655 };
656 }
657
658 // The dirent64 struct is a weird imaginary thing that isn't ever supposed
659 // to be worked with by value. Its trailing d_name field is declared
660 // variously as [c_char; 256] or [c_char; 1] on different systems but
661 // either way that size is meaningless; only the offset of d_name is
662 // meaningful. The dirent64 pointers that libc returns from readdir64 are
663 // allowed to point to allocations smaller _or_ LARGER than implied by the
664 // definition of the struct.
665 //
666 // As such, we need to be even more careful with dirent64 than if its
667 // contents were "simply" partially initialized data.
668 //
669 // Like for uninitialized contents, converting entry_ptr to `&dirent64`
670 // would not be legal. However, unique to dirent64 is that we don't even
671 // get to use `addr_of!((*entry_ptr).d_name)` because that operation
672 // requires the full extent of *entry_ptr to be in bounds of the same
673 // allocation, which is not necessarily the case here.
674 //
675 // Absent any other way to obtain a pointer to `(*entry_ptr).d_name`
676 // legally in Rust analogously to how it would be done in C, we instead
677 // need to make our own non-libc allocation that conforms to the weird
678 // imaginary definition of dirent64, and use that for a field offset
679 // computation.
680 macro_rules! offset_ptr {
681 ($entry_ptr:expr, $field:ident) => {{
682 const OFFSET: isize = {
683 let delusion = MaybeUninit::<dirent64>::uninit();
684 let entry_ptr = delusion.as_ptr();
685 unsafe {
686 ptr::addr_of!((*entry_ptr).$field)
687 .cast::<u8>()
688 .offset_from(entry_ptr.cast::<u8>())
689 }
690 };
691 if true {
692 // Cast to the same type determined by the else branch.
693 $entry_ptr.byte_offset(OFFSET).cast::<_>()
694 } else {
695 #[allow(deref_nullptr)]
696 {
697 ptr::addr_of!((*ptr::null::<dirent64>()).$field)
698 }
699 }
700 }};
701 }
702
703 // d_name is guaranteed to be null-terminated.
704 let name = CStr::from_ptr(offset_ptr!(entry_ptr, d_name).cast());
705 let name_bytes = name.to_bytes();
706 if name_bytes == b"." || name_bytes == b".." {
707 continue;
708 }
709
710 let entry = dirent64_min {
711 d_ino: *offset_ptr!(entry_ptr, d_ino) as u64,
712 #[cfg(not(any(
713 target_os = "solaris",
714 target_os = "illumos",
715 target_os = "nto",
716 )))]
717 d_type: *offset_ptr!(entry_ptr, d_type) as u8,
718 };
719
720 return Some(Ok(DirEntry {
721 entry,
722 name: name.to_owned(),
723 dir: Arc::clone(&self.inner),
724 }));
725 }
726 }
727 }
728
729 #[cfg(not(any(
730 target_os = "android",
731 target_os = "linux",
732 target_os = "solaris",
733 target_os = "fuchsia",
734 target_os = "redox",
735 target_os = "illumos",
736 target_os = "nto",
737 )))]
738 fn next(&mut self) -> Option<io::Result<DirEntry>> {
739 if self.end_of_stream {
740 return None;
741 }
742
743 unsafe {
744 let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
745 let mut entry_ptr = ptr::null_mut();
746 loop {
747 let err = readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr);
748 if err != 0 {
749 if entry_ptr.is_null() {
750 // We encountered an error (which will be returned in this iteration), but
751 // we also reached the end of the directory stream. The `end_of_stream`
752 // flag is enabled to make sure that we return `None` in the next iteration
753 // (instead of looping forever)
754 self.end_of_stream = true;
755 }
756 return Some(Err(Error::from_raw_os_error(err)));
757 }
758 if entry_ptr.is_null() {
759 return None;
760 }
761 if ret.name_bytes() != b"." && ret.name_bytes() != b".." {
762 return Some(Ok(ret));
763 }
764 }
765 }
766 }
767 }
768
769 impl Drop for Dir {
770 fn drop(&mut self) {
771 let r = unsafe { libc::closedir(self.0) };
772 assert!(
773 r == 0 || crate::io::Error::last_os_error().kind() == crate::io::ErrorKind::Interrupted,
774 "unexpected error during closedir: {:?}",
775 crate::io::Error::last_os_error()
776 );
777 }
778 }
779
780 impl DirEntry {
781 pub fn path(&self) -> PathBuf {
782 self.dir.root.join(self.file_name_os_str())
783 }
784
785 pub fn file_name(&self) -> OsString {
786 self.file_name_os_str().to_os_string()
787 }
788
789 #[cfg(all(
790 any(target_os = "linux", target_os = "emscripten", target_os = "android"),
791 not(miri)
792 ))]
793 pub fn metadata(&self) -> io::Result<FileAttr> {
794 let fd = cvt(unsafe { dirfd(self.dir.dirp.0) })?;
795 let name = self.name_cstr().as_ptr();
796
797 cfg_has_statx! {
798 if let Some(ret) = unsafe { try_statx(
799 fd,
800 name,
801 libc::AT_SYMLINK_NOFOLLOW | libc::AT_STATX_SYNC_AS_STAT,
802 libc::STATX_ALL,
803 ) } {
804 return ret;
805 }
806 }
807
808 let mut stat: stat64 = unsafe { mem::zeroed() };
809 cvt(unsafe { fstatat64(fd, name, &mut stat, libc::AT_SYMLINK_NOFOLLOW) })?;
810 Ok(FileAttr::from_stat64(stat))
811 }
812
813 #[cfg(any(
814 not(any(target_os = "linux", target_os = "emscripten", target_os = "android")),
815 miri
816 ))]
817 pub fn metadata(&self) -> io::Result<FileAttr> {
818 lstat(&self.path())
819 }
820
821 #[cfg(any(
822 target_os = "solaris",
823 target_os = "illumos",
824 target_os = "haiku",
825 target_os = "vxworks",
826 target_os = "nto",
827 ))]
828 pub fn file_type(&self) -> io::Result<FileType> {
829 self.metadata().map(|m| m.file_type())
830 }
831
832 #[cfg(not(any(
833 target_os = "solaris",
834 target_os = "illumos",
835 target_os = "haiku",
836 target_os = "vxworks",
837 target_os = "nto",
838 )))]
839 pub fn file_type(&self) -> io::Result<FileType> {
840 match self.entry.d_type {
841 libc::DT_CHR => Ok(FileType { mode: libc::S_IFCHR }),
842 libc::DT_FIFO => Ok(FileType { mode: libc::S_IFIFO }),
843 libc::DT_LNK => Ok(FileType { mode: libc::S_IFLNK }),
844 libc::DT_REG => Ok(FileType { mode: libc::S_IFREG }),
845 libc::DT_SOCK => Ok(FileType { mode: libc::S_IFSOCK }),
846 libc::DT_DIR => Ok(FileType { mode: libc::S_IFDIR }),
847 libc::DT_BLK => Ok(FileType { mode: libc::S_IFBLK }),
848 _ => self.metadata().map(|m| m.file_type()),
849 }
850 }
851
852 #[cfg(any(
853 target_os = "macos",
854 target_os = "ios",
855 target_os = "watchos",
856 target_os = "linux",
857 target_os = "emscripten",
858 target_os = "android",
859 target_os = "solaris",
860 target_os = "illumos",
861 target_os = "haiku",
862 target_os = "l4re",
863 target_os = "fuchsia",
864 target_os = "redox",
865 target_os = "vxworks",
866 target_os = "espidf",
867 target_os = "horizon",
868 target_os = "nto",
869 ))]
870 pub fn ino(&self) -> u64 {
871 self.entry.d_ino as u64
872 }
873
874 #[cfg(any(
875 target_os = "freebsd",
876 target_os = "openbsd",
877 target_os = "netbsd",
878 target_os = "dragonfly"
879 ))]
880 pub fn ino(&self) -> u64 {
881 self.entry.d_fileno as u64
882 }
883
884 #[cfg(any(
885 target_os = "macos",
886 target_os = "ios",
887 target_os = "watchos",
888 target_os = "netbsd",
889 target_os = "openbsd",
890 target_os = "freebsd",
891 target_os = "dragonfly"
892 ))]
893 fn name_bytes(&self) -> &[u8] {
894 use crate::slice;
895 unsafe {
896 slice::from_raw_parts(
897 self.entry.d_name.as_ptr() as *const u8,
898 self.entry.d_namlen as usize,
899 )
900 }
901 }
902 #[cfg(not(any(
903 target_os = "macos",
904 target_os = "ios",
905 target_os = "watchos",
906 target_os = "netbsd",
907 target_os = "openbsd",
908 target_os = "freebsd",
909 target_os = "dragonfly"
910 )))]
911 fn name_bytes(&self) -> &[u8] {
912 self.name_cstr().to_bytes()
913 }
914
915 #[cfg(not(any(
916 target_os = "android",
917 target_os = "linux",
918 target_os = "solaris",
919 target_os = "illumos",
920 target_os = "fuchsia",
921 target_os = "redox",
922 target_os = "nto",
923 )))]
924 fn name_cstr(&self) -> &CStr {
925 unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()) }
926 }
927 #[cfg(any(
928 target_os = "android",
929 target_os = "linux",
930 target_os = "solaris",
931 target_os = "illumos",
932 target_os = "fuchsia",
933 target_os = "redox",
934 target_os = "nto",
935 ))]
936 fn name_cstr(&self) -> &CStr {
937 &self.name
938 }
939
940 pub fn file_name_os_str(&self) -> &OsStr {
941 OsStr::from_bytes(self.name_bytes())
942 }
943 }
944
945 impl OpenOptions {
946 pub fn new() -> OpenOptions {
947 OpenOptions {
948 // generic
949 read: false,
950 write: false,
951 append: false,
952 truncate: false,
953 create: false,
954 create_new: false,
955 // system-specific
956 custom_flags: 0,
957 mode: 0o666,
958 }
959 }
960
961 pub fn read(&mut self, read: bool) {
962 self.read = read;
963 }
964 pub fn write(&mut self, write: bool) {
965 self.write = write;
966 }
967 pub fn append(&mut self, append: bool) {
968 self.append = append;
969 }
970 pub fn truncate(&mut self, truncate: bool) {
971 self.truncate = truncate;
972 }
973 pub fn create(&mut self, create: bool) {
974 self.create = create;
975 }
976 pub fn create_new(&mut self, create_new: bool) {
977 self.create_new = create_new;
978 }
979
980 pub fn custom_flags(&mut self, flags: i32) {
981 self.custom_flags = flags;
982 }
983 pub fn mode(&mut self, mode: u32) {
984 self.mode = mode as mode_t;
985 }
986
987 fn get_access_mode(&self) -> io::Result<c_int> {
988 match (self.read, self.write, self.append) {
989 (true, false, false) => Ok(libc::O_RDONLY),
990 (false, true, false) => Ok(libc::O_WRONLY),
991 (true, true, false) => Ok(libc::O_RDWR),
992 (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND),
993 (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND),
994 (false, false, false) => Err(Error::from_raw_os_error(libc::EINVAL)),
995 }
996 }
997
998 fn get_creation_mode(&self) -> io::Result<c_int> {
999 match (self.write, self.append) {
1000 (true, false) => {}
1001 (false, false) => {
1002 if self.truncate || self.create || self.create_new {
1003 return Err(Error::from_raw_os_error(libc::EINVAL));
1004 }
1005 }
1006 (_, true) => {
1007 if self.truncate && !self.create_new {
1008 return Err(Error::from_raw_os_error(libc::EINVAL));
1009 }
1010 }
1011 }
1012
1013 Ok(match (self.create, self.truncate, self.create_new) {
1014 (false, false, false) => 0,
1015 (true, false, false) => libc::O_CREAT,
1016 (false, true, false) => libc::O_TRUNC,
1017 (true, true, false) => libc::O_CREAT | libc::O_TRUNC,
1018 (_, _, true) => libc::O_CREAT | libc::O_EXCL,
1019 })
1020 }
1021 }
1022
1023 impl File {
1024 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
1025 run_path_with_cstr(path, |path| File::open_c(path, opts))
1026 }
1027
1028 pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
1029 let flags = libc::O_CLOEXEC
1030 | opts.get_access_mode()?
1031 | opts.get_creation_mode()?
1032 | (opts.custom_flags as c_int & !libc::O_ACCMODE);
1033 // The third argument of `open64` is documented to have type `mode_t`. On
1034 // some platforms (like macOS, where `open64` is actually `open`), `mode_t` is `u16`.
1035 // However, since this is a variadic function, C integer promotion rules mean that on
1036 // the ABI level, this still gets passed as `c_int` (aka `u32` on Unix platforms).
1037 let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode as c_int) })?;
1038 Ok(File(unsafe { FileDesc::from_raw_fd(fd) }))
1039 }
1040
1041 pub fn file_attr(&self) -> io::Result<FileAttr> {
1042 let fd = self.as_raw_fd();
1043
1044 cfg_has_statx! {
1045 if let Some(ret) = unsafe { try_statx(
1046 fd,
1047 b"\0" as *const _ as *const c_char,
1048 libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT,
1049 libc::STATX_ALL,
1050 ) } {
1051 return ret;
1052 }
1053 }
1054
1055 let mut stat: stat64 = unsafe { mem::zeroed() };
1056 cvt(unsafe { fstat64(fd, &mut stat) })?;
1057 Ok(FileAttr::from_stat64(stat))
1058 }
1059
1060 pub fn fsync(&self) -> io::Result<()> {
1061 cvt_r(|| unsafe { os_fsync(self.as_raw_fd()) })?;
1062 return Ok(());
1063
1064 #[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
1065 unsafe fn os_fsync(fd: c_int) -> c_int {
1066 libc::fcntl(fd, libc::F_FULLFSYNC)
1067 }
1068 #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "watchos")))]
1069 unsafe fn os_fsync(fd: c_int) -> c_int {
1070 libc::fsync(fd)
1071 }
1072 }
1073
1074 pub fn datasync(&self) -> io::Result<()> {
1075 cvt_r(|| unsafe { os_datasync(self.as_raw_fd()) })?;
1076 return Ok(());
1077
1078 #[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
1079 unsafe fn os_datasync(fd: c_int) -> c_int {
1080 libc::fcntl(fd, libc::F_FULLFSYNC)
1081 }
1082 #[cfg(any(
1083 target_os = "freebsd",
1084 target_os = "linux",
1085 target_os = "android",
1086 target_os = "netbsd",
1087 target_os = "openbsd",
1088 target_os = "nto",
1089 ))]
1090 unsafe fn os_datasync(fd: c_int) -> c_int {
1091 libc::fdatasync(fd)
1092 }
1093 #[cfg(not(any(
1094 target_os = "android",
1095 target_os = "freebsd",
1096 target_os = "ios",
1097 target_os = "linux",
1098 target_os = "macos",
1099 target_os = "netbsd",
1100 target_os = "openbsd",
1101 target_os = "watchos",
1102 target_os = "nto",
1103 )))]
1104 unsafe fn os_datasync(fd: c_int) -> c_int {
1105 libc::fsync(fd)
1106 }
1107 }
1108
1109 pub fn truncate(&self, size: u64) -> io::Result<()> {
1110 let size: off64_t =
1111 size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
1112 cvt_r(|| unsafe { ftruncate64(self.as_raw_fd(), size) }).map(drop)
1113 }
1114
1115 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
1116 self.0.read(buf)
1117 }
1118
1119 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
1120 self.0.read_vectored(bufs)
1121 }
1122
1123 #[inline]
1124 pub fn is_read_vectored(&self) -> bool {
1125 self.0.is_read_vectored()
1126 }
1127
1128 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
1129 self.0.read_at(buf, offset)
1130 }
1131
1132 pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
1133 self.0.read_buf(cursor)
1134 }
1135
1136 pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
1137 self.0.read_vectored_at(bufs, offset)
1138 }
1139
1140 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
1141 self.0.write(buf)
1142 }
1143
1144 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
1145 self.0.write_vectored(bufs)
1146 }
1147
1148 #[inline]
1149 pub fn is_write_vectored(&self) -> bool {
1150 self.0.is_write_vectored()
1151 }
1152
1153 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
1154 self.0.write_at(buf, offset)
1155 }
1156
1157 pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
1158 self.0.write_vectored_at(bufs, offset)
1159 }
1160
1161 pub fn flush(&self) -> io::Result<()> {
1162 Ok(())
1163 }
1164
1165 pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
1166 let (whence, pos) = match pos {
1167 // Casting to `i64` is fine, too large values will end up as
1168 // negative which will cause an error in `lseek64`.
1169 SeekFrom::Start(off) => (libc::SEEK_SET, off as i64),
1170 SeekFrom::End(off) => (libc::SEEK_END, off),
1171 SeekFrom::Current(off) => (libc::SEEK_CUR, off),
1172 };
1173 let n = cvt(unsafe { lseek64(self.as_raw_fd(), pos as off64_t, whence) })?;
1174 Ok(n as u64)
1175 }
1176
1177 pub fn duplicate(&self) -> io::Result<File> {
1178 self.0.duplicate().map(File)
1179 }
1180
1181 pub fn set_permissions(&self, perm: FilePermissions) -> io::Result<()> {
1182 cvt_r(|| unsafe { libc::fchmod(self.as_raw_fd(), perm.mode) })?;
1183 Ok(())
1184 }
1185
1186 pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
1187 #[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "horizon")))]
1188 let to_timespec = |time: Option<SystemTime>| {
1189 match time {
1190 Some(time) if let Some(ts) = time.t.to_timespec() => Ok(ts),
1191 Some(time) if time > crate::sys::time::UNIX_EPOCH => Err(io::const_io_error!(io::ErrorKind::InvalidInput, "timestamp is too large to set as a file time")),
1192 Some(_) => Err(io::const_io_error!(io::ErrorKind::InvalidInput, "timestamp is too small to set as a file time")),
1193 None => Ok(libc::timespec { tv_sec: 0, tv_nsec: libc::UTIME_OMIT as _ }),
1194 }
1195 };
1196 #[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "horizon")))]
1197 let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
1198 cfg_if::cfg_if! {
1199 if #[cfg(any(target_os = "redox", target_os = "espidf", target_os = "horizon"))] {
1200 // Redox doesn't appear to support `UTIME_OMIT`.
1201 // ESP-IDF and HorizonOS do not support `futimens` at all and the behavior for those OS is therefore
1202 // the same as for Redox.
1203 drop(times);
1204 Err(io::const_io_error!(
1205 io::ErrorKind::Unsupported,
1206 "setting file times not supported",
1207 ))
1208 } else if #[cfg(any(target_os = "android", target_os = "macos"))] {
1209 // futimens requires macOS 10.13, and Android API level 19
1210 cvt(unsafe {
1211 weak!(fn futimens(c_int, *const libc::timespec) -> c_int);
1212 match futimens.get() {
1213 Some(futimens) => futimens(self.as_raw_fd(), times.as_ptr()),
1214 #[cfg(target_os = "macos")]
1215 None => {
1216 fn ts_to_tv(ts: &libc::timespec) -> libc::timeval {
1217 libc::timeval {
1218 tv_sec: ts.tv_sec,
1219 tv_usec: (ts.tv_nsec / 1000) as _
1220 }
1221 }
1222 let timevals = [ts_to_tv(&times[0]), ts_to_tv(&times[1])];
1223 libc::futimes(self.as_raw_fd(), timevals.as_ptr())
1224 }
1225 // futimes requires even newer Android.
1226 #[cfg(target_os = "android")]
1227 None => return Err(io::const_io_error!(
1228 io::ErrorKind::Unsupported,
1229 "setting file times requires Android API level >= 19",
1230 )),
1231 }
1232 })?;
1233 Ok(())
1234 } else {
1235 cvt(unsafe { libc::futimens(self.as_raw_fd(), times.as_ptr()) })?;
1236 Ok(())
1237 }
1238 }
1239 }
1240 }
1241
1242 impl DirBuilder {
1243 pub fn new() -> DirBuilder {
1244 DirBuilder { mode: 0o777 }
1245 }
1246
1247 pub fn mkdir(&self, p: &Path) -> io::Result<()> {
1248 run_path_with_cstr(p, |p| cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) }).map(|_| ()))
1249 }
1250
1251 pub fn set_mode(&mut self, mode: u32) {
1252 self.mode = mode as mode_t;
1253 }
1254 }
1255
1256 impl AsInner<FileDesc> for File {
1257 fn as_inner(&self) -> &FileDesc {
1258 &self.0
1259 }
1260 }
1261
1262 impl AsInnerMut<FileDesc> for File {
1263 fn as_inner_mut(&mut self) -> &mut FileDesc {
1264 &mut self.0
1265 }
1266 }
1267
1268 impl IntoInner<FileDesc> for File {
1269 fn into_inner(self) -> FileDesc {
1270 self.0
1271 }
1272 }
1273
1274 impl FromInner<FileDesc> for File {
1275 fn from_inner(file_desc: FileDesc) -> Self {
1276 Self(file_desc)
1277 }
1278 }
1279
1280 impl AsFd for File {
1281 fn as_fd(&self) -> BorrowedFd<'_> {
1282 self.0.as_fd()
1283 }
1284 }
1285
1286 impl AsRawFd for File {
1287 fn as_raw_fd(&self) -> RawFd {
1288 self.0.as_raw_fd()
1289 }
1290 }
1291
1292 impl IntoRawFd for File {
1293 fn into_raw_fd(self) -> RawFd {
1294 self.0.into_raw_fd()
1295 }
1296 }
1297
1298 impl FromRawFd for File {
1299 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
1300 Self(FromRawFd::from_raw_fd(raw_fd))
1301 }
1302 }
1303
1304 impl fmt::Debug for File {
1305 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1306 #[cfg(any(
1307 target_os = "linux",
1308 target_os = "netbsd",
1309 target_os = "illumos",
1310 target_os = "solaris"
1311 ))]
1312 fn get_path(fd: c_int) -> Option<PathBuf> {
1313 let mut p = PathBuf::from("/proc/self/fd");
1314 p.push(&fd.to_string());
1315 readlink(&p).ok()
1316 }
1317
1318 #[cfg(target_os = "macos")]
1319 fn get_path(fd: c_int) -> Option<PathBuf> {
1320 // FIXME: The use of PATH_MAX is generally not encouraged, but it
1321 // is inevitable in this case because macOS defines `fcntl` with
1322 // `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
1323 // alternatives. If a better method is invented, it should be used
1324 // instead.
1325 let mut buf = vec![0; libc::PATH_MAX as usize];
1326 let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
1327 if n == -1 {
1328 return None;
1329 }
1330 let l = buf.iter().position(|&c| c == 0).unwrap();
1331 buf.truncate(l as usize);
1332 buf.shrink_to_fit();
1333 Some(PathBuf::from(OsString::from_vec(buf)))
1334 }
1335
1336 #[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
1337 fn get_path(fd: c_int) -> Option<PathBuf> {
1338 let info = Box::<libc::kinfo_file>::new_zeroed();
1339 let mut info = unsafe { info.assume_init() };
1340 info.kf_structsize = mem::size_of::<libc::kinfo_file>() as libc::c_int;
1341 let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) };
1342 if n == -1 {
1343 return None;
1344 }
1345 let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() };
1346 Some(PathBuf::from(OsString::from_vec(buf)))
1347 }
1348
1349 #[cfg(target_os = "vxworks")]
1350 fn get_path(fd: c_int) -> Option<PathBuf> {
1351 let mut buf = vec![0; libc::PATH_MAX as usize];
1352 let n = unsafe { libc::ioctl(fd, libc::FIOGETNAME, buf.as_ptr()) };
1353 if n == -1 {
1354 return None;
1355 }
1356 let l = buf.iter().position(|&c| c == 0).unwrap();
1357 buf.truncate(l as usize);
1358 Some(PathBuf::from(OsString::from_vec(buf)))
1359 }
1360
1361 #[cfg(not(any(
1362 target_os = "linux",
1363 target_os = "macos",
1364 target_os = "vxworks",
1365 all(target_os = "freebsd", target_arch = "x86_64"),
1366 target_os = "netbsd",
1367 target_os = "illumos",
1368 target_os = "solaris"
1369 )))]
1370 fn get_path(_fd: c_int) -> Option<PathBuf> {
1371 // FIXME(#24570): implement this for other Unix platforms
1372 None
1373 }
1374
1375 #[cfg(any(
1376 target_os = "linux",
1377 target_os = "macos",
1378 target_os = "freebsd",
1379 target_os = "netbsd",
1380 target_os = "openbsd",
1381 target_os = "vxworks"
1382 ))]
1383 fn get_mode(fd: c_int) -> Option<(bool, bool)> {
1384 let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
1385 if mode == -1 {
1386 return None;
1387 }
1388 match mode & libc::O_ACCMODE {
1389 libc::O_RDONLY => Some((true, false)),
1390 libc::O_RDWR => Some((true, true)),
1391 libc::O_WRONLY => Some((false, true)),
1392 _ => None,
1393 }
1394 }
1395
1396 #[cfg(not(any(
1397 target_os = "linux",
1398 target_os = "macos",
1399 target_os = "freebsd",
1400 target_os = "netbsd",
1401 target_os = "openbsd",
1402 target_os = "vxworks"
1403 )))]
1404 fn get_mode(_fd: c_int) -> Option<(bool, bool)> {
1405 // FIXME(#24570): implement this for other Unix platforms
1406 None
1407 }
1408
1409 let fd = self.as_raw_fd();
1410 let mut b = f.debug_struct("File");
1411 b.field("fd", &fd);
1412 if let Some(path) = get_path(fd) {
1413 b.field("path", &path);
1414 }
1415 if let Some((read, write)) = get_mode(fd) {
1416 b.field("read", &read).field("write", &write);
1417 }
1418 b.finish()
1419 }
1420 }
1421
1422 pub fn readdir(path: &Path) -> io::Result<ReadDir> {
1423 let ptr = run_path_with_cstr(path, |p| unsafe { Ok(libc::opendir(p.as_ptr())) })?;
1424 if ptr.is_null() {
1425 Err(Error::last_os_error())
1426 } else {
1427 let root = path.to_path_buf();
1428 let inner = InnerReadDir { dirp: Dir(ptr), root };
1429 Ok(ReadDir::new(inner))
1430 }
1431 }
1432
1433 pub fn unlink(p: &Path) -> io::Result<()> {
1434 run_path_with_cstr(p, |p| cvt(unsafe { libc::unlink(p.as_ptr()) }).map(|_| ()))
1435 }
1436
1437 pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1438 run_path_with_cstr(old, |old| {
1439 run_path_with_cstr(new, |new| {
1440 cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) }).map(|_| ())
1441 })
1442 })
1443 }
1444
1445 pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
1446 run_path_with_cstr(p, |p| cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) }).map(|_| ()))
1447 }
1448
1449 pub fn rmdir(p: &Path) -> io::Result<()> {
1450 run_path_with_cstr(p, |p| cvt(unsafe { libc::rmdir(p.as_ptr()) }).map(|_| ()))
1451 }
1452
1453 pub fn readlink(p: &Path) -> io::Result<PathBuf> {
1454 run_path_with_cstr(p, |c_path| {
1455 let p = c_path.as_ptr();
1456
1457 let mut buf = Vec::with_capacity(256);
1458
1459 loop {
1460 let buf_read =
1461 cvt(unsafe { libc::readlink(p, buf.as_mut_ptr() as *mut _, buf.capacity()) })?
1462 as usize;
1463
1464 unsafe {
1465 buf.set_len(buf_read);
1466 }
1467
1468 if buf_read != buf.capacity() {
1469 buf.shrink_to_fit();
1470
1471 return Ok(PathBuf::from(OsString::from_vec(buf)));
1472 }
1473
1474 // Trigger the internal buffer resizing logic of `Vec` by requiring
1475 // more space than the current capacity. The length is guaranteed to be
1476 // the same as the capacity due to the if statement above.
1477 buf.reserve(1);
1478 }
1479 })
1480 }
1481
1482 pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
1483 run_path_with_cstr(original, |original| {
1484 run_path_with_cstr(link, |link| {
1485 cvt(unsafe { libc::symlink(original.as_ptr(), link.as_ptr()) }).map(|_| ())
1486 })
1487 })
1488 }
1489
1490 pub fn link(original: &Path, link: &Path) -> io::Result<()> {
1491 run_path_with_cstr(original, |original| {
1492 run_path_with_cstr(link, |link| {
1493 cfg_if::cfg_if! {
1494 if #[cfg(any(target_os = "vxworks", target_os = "redox", target_os = "android", target_os = "espidf", target_os = "horizon"))] {
1495 // VxWorks, Redox and ESP-IDF lack `linkat`, so use `link` instead. POSIX leaves
1496 // it implementation-defined whether `link` follows symlinks, so rely on the
1497 // `symlink_hard_link` test in library/std/src/fs/tests.rs to check the behavior.
1498 // Android has `linkat` on newer versions, but we happen to know `link`
1499 // always has the correct behavior, so it's here as well.
1500 cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?;
1501 } else if #[cfg(any(target_os = "macos", target_os = "solaris"))] {
1502 // MacOS (<=10.9) and Solaris 10 lack support for linkat while newer
1503 // versions have it. We want to use linkat if it is available, so we use weak!
1504 // to check. `linkat` is preferable to `link` because it gives us a flag to
1505 // specify how symlinks should be handled. We pass 0 as the flags argument,
1506 // meaning it shouldn't follow symlinks.
1507 weak!(fn linkat(c_int, *const c_char, c_int, *const c_char, c_int) -> c_int);
1508
1509 if let Some(f) = linkat.get() {
1510 cvt(unsafe { f(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?;
1511 } else {
1512 cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?;
1513 };
1514 } else {
1515 // Where we can, use `linkat` instead of `link`; see the comment above
1516 // this one for details on why.
1517 cvt(unsafe { libc::linkat(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?;
1518 }
1519 }
1520 Ok(())
1521 })
1522 })
1523 }
1524
1525 pub fn stat(p: &Path) -> io::Result<FileAttr> {
1526 run_path_with_cstr(p, |p| {
1527 cfg_has_statx! {
1528 if let Some(ret) = unsafe { try_statx(
1529 libc::AT_FDCWD,
1530 p.as_ptr(),
1531 libc::AT_STATX_SYNC_AS_STAT,
1532 libc::STATX_ALL,
1533 ) } {
1534 return ret;
1535 }
1536 }
1537
1538 let mut stat: stat64 = unsafe { mem::zeroed() };
1539 cvt(unsafe { stat64(p.as_ptr(), &mut stat) })?;
1540 Ok(FileAttr::from_stat64(stat))
1541 })
1542 }
1543
1544 pub fn lstat(p: &Path) -> io::Result<FileAttr> {
1545 run_path_with_cstr(p, |p| {
1546 cfg_has_statx! {
1547 if let Some(ret) = unsafe { try_statx(
1548 libc::AT_FDCWD,
1549 p.as_ptr(),
1550 libc::AT_SYMLINK_NOFOLLOW | libc::AT_STATX_SYNC_AS_STAT,
1551 libc::STATX_ALL,
1552 ) } {
1553 return ret;
1554 }
1555 }
1556
1557 let mut stat: stat64 = unsafe { mem::zeroed() };
1558 cvt(unsafe { lstat64(p.as_ptr(), &mut stat) })?;
1559 Ok(FileAttr::from_stat64(stat))
1560 })
1561 }
1562
1563 pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
1564 let r = run_path_with_cstr(p, |path| unsafe {
1565 Ok(libc::realpath(path.as_ptr(), ptr::null_mut()))
1566 })?;
1567 if r.is_null() {
1568 return Err(io::Error::last_os_error());
1569 }
1570 Ok(PathBuf::from(OsString::from_vec(unsafe {
1571 let buf = CStr::from_ptr(r).to_bytes().to_vec();
1572 libc::free(r as *mut _);
1573 buf
1574 })))
1575 }
1576
1577 fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
1578 use crate::fs::File;
1579 use crate::sys_common::fs::NOT_FILE_ERROR;
1580
1581 let reader = File::open(from)?;
1582 let metadata = reader.metadata()?;
1583 if !metadata.is_file() {
1584 return Err(NOT_FILE_ERROR);
1585 }
1586 Ok((reader, metadata))
1587 }
1588
1589 #[cfg(target_os = "espidf")]
1590 fn open_to_and_set_permissions(
1591 to: &Path,
1592 reader_metadata: crate::fs::Metadata,
1593 ) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
1594 use crate::fs::OpenOptions;
1595 let writer = OpenOptions::new().open(to)?;
1596 let writer_metadata = writer.metadata()?;
1597 Ok((writer, writer_metadata))
1598 }
1599
1600 #[cfg(not(target_os = "espidf"))]
1601 fn open_to_and_set_permissions(
1602 to: &Path,
1603 reader_metadata: crate::fs::Metadata,
1604 ) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
1605 use crate::fs::OpenOptions;
1606 use crate::os::unix::fs::{OpenOptionsExt, PermissionsExt};
1607
1608 let perm = reader_metadata.permissions();
1609 let writer = OpenOptions::new()
1610 // create the file with the correct mode right away
1611 .mode(perm.mode())
1612 .write(true)
1613 .create(true)
1614 .truncate(true)
1615 .open(to)?;
1616 let writer_metadata = writer.metadata()?;
1617 if writer_metadata.is_file() {
1618 // Set the correct file permissions, in case the file already existed.
1619 // Don't set the permissions on already existing non-files like
1620 // pipes/FIFOs or device nodes.
1621 writer.set_permissions(perm)?;
1622 }
1623 Ok((writer, writer_metadata))
1624 }
1625
1626 #[cfg(not(any(
1627 target_os = "linux",
1628 target_os = "android",
1629 target_os = "macos",
1630 target_os = "ios",
1631 target_os = "watchos",
1632 )))]
1633 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
1634 let (mut reader, reader_metadata) = open_from(from)?;
1635 let (mut writer, _) = open_to_and_set_permissions(to, reader_metadata)?;
1636
1637 io::copy(&mut reader, &mut writer)
1638 }
1639
1640 #[cfg(any(target_os = "linux", target_os = "android"))]
1641 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
1642 let (mut reader, reader_metadata) = open_from(from)?;
1643 let max_len = u64::MAX;
1644 let (mut writer, _) = open_to_and_set_permissions(to, reader_metadata)?;
1645
1646 use super::kernel_copy::{copy_regular_files, CopyResult};
1647
1648 match copy_regular_files(reader.as_raw_fd(), writer.as_raw_fd(), max_len) {
1649 CopyResult::Ended(bytes) => Ok(bytes),
1650 CopyResult::Error(e, _) => Err(e),
1651 CopyResult::Fallback(written) => match io::copy::generic_copy(&mut reader, &mut writer) {
1652 Ok(bytes) => Ok(bytes + written),
1653 Err(e) => Err(e),
1654 },
1655 }
1656 }
1657
1658 #[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
1659 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
1660 use crate::sync::atomic::{AtomicBool, Ordering};
1661
1662 const COPYFILE_ACL: u32 = 1 << 0;
1663 const COPYFILE_STAT: u32 = 1 << 1;
1664 const COPYFILE_XATTR: u32 = 1 << 2;
1665 const COPYFILE_DATA: u32 = 1 << 3;
1666
1667 const COPYFILE_SECURITY: u32 = COPYFILE_STAT | COPYFILE_ACL;
1668 const COPYFILE_METADATA: u32 = COPYFILE_SECURITY | COPYFILE_XATTR;
1669 const COPYFILE_ALL: u32 = COPYFILE_METADATA | COPYFILE_DATA;
1670
1671 const COPYFILE_STATE_COPIED: u32 = 8;
1672
1673 #[allow(non_camel_case_types)]
1674 type copyfile_state_t = *mut libc::c_void;
1675 #[allow(non_camel_case_types)]
1676 type copyfile_flags_t = u32;
1677
1678 extern "C" {
1679 fn fcopyfile(
1680 from: libc::c_int,
1681 to: libc::c_int,
1682 state: copyfile_state_t,
1683 flags: copyfile_flags_t,
1684 ) -> libc::c_int;
1685 fn copyfile_state_alloc() -> copyfile_state_t;
1686 fn copyfile_state_free(state: copyfile_state_t) -> libc::c_int;
1687 fn copyfile_state_get(
1688 state: copyfile_state_t,
1689 flag: u32,
1690 dst: *mut libc::c_void,
1691 ) -> libc::c_int;
1692 }
1693
1694 struct FreeOnDrop(copyfile_state_t);
1695 impl Drop for FreeOnDrop {
1696 fn drop(&mut self) {
1697 // The code below ensures that `FreeOnDrop` is never a null pointer
1698 unsafe {
1699 // `copyfile_state_free` returns -1 if the `to` or `from` files
1700 // cannot be closed. However, this is not considered this an
1701 // error.
1702 copyfile_state_free(self.0);
1703 }
1704 }
1705 }
1706
1707 // MacOS prior to 10.12 don't support `fclonefileat`
1708 // We store the availability in a global to avoid unnecessary syscalls
1709 static HAS_FCLONEFILEAT: AtomicBool = AtomicBool::new(true);
1710 syscall! {
1711 fn fclonefileat(
1712 srcfd: libc::c_int,
1713 dst_dirfd: libc::c_int,
1714 dst: *const c_char,
1715 flags: libc::c_int
1716 ) -> libc::c_int
1717 }
1718
1719 let (reader, reader_metadata) = open_from(from)?;
1720
1721 // Opportunistically attempt to create a copy-on-write clone of `from`
1722 // using `fclonefileat`.
1723 if HAS_FCLONEFILEAT.load(Ordering::Relaxed) {
1724 let clonefile_result = run_path_with_cstr(to, |to| {
1725 cvt(unsafe { fclonefileat(reader.as_raw_fd(), libc::AT_FDCWD, to.as_ptr(), 0) })
1726 });
1727 match clonefile_result {
1728 Ok(_) => return Ok(reader_metadata.len()),
1729 Err(err) => match err.raw_os_error() {
1730 // `fclonefileat` will fail on non-APFS volumes, if the
1731 // destination already exists, or if the source and destination
1732 // are on different devices. In all these cases `fcopyfile`
1733 // should succeed.
1734 Some(libc::ENOTSUP) | Some(libc::EEXIST) | Some(libc::EXDEV) => (),
1735 Some(libc::ENOSYS) => HAS_FCLONEFILEAT.store(false, Ordering::Relaxed),
1736 _ => return Err(err),
1737 },
1738 }
1739 }
1740
1741 // Fall back to using `fcopyfile` if `fclonefileat` does not succeed.
1742 let (writer, writer_metadata) = open_to_and_set_permissions(to, reader_metadata)?;
1743
1744 // We ensure that `FreeOnDrop` never contains a null pointer so it is
1745 // always safe to call `copyfile_state_free`
1746 let state = unsafe {
1747 let state = copyfile_state_alloc();
1748 if state.is_null() {
1749 return Err(crate::io::Error::last_os_error());
1750 }
1751 FreeOnDrop(state)
1752 };
1753
1754 let flags = if writer_metadata.is_file() { COPYFILE_ALL } else { COPYFILE_DATA };
1755
1756 cvt(unsafe { fcopyfile(reader.as_raw_fd(), writer.as_raw_fd(), state.0, flags) })?;
1757
1758 let mut bytes_copied: libc::off_t = 0;
1759 cvt(unsafe {
1760 copyfile_state_get(
1761 state.0,
1762 COPYFILE_STATE_COPIED,
1763 &mut bytes_copied as *mut libc::off_t as *mut libc::c_void,
1764 )
1765 })?;
1766 Ok(bytes_copied as u64)
1767 }
1768
1769 pub fn chown(path: &Path, uid: u32, gid: u32) -> io::Result<()> {
1770 run_path_with_cstr(path, |path| {
1771 cvt(unsafe { libc::chown(path.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) })
1772 .map(|_| ())
1773 })
1774 }
1775
1776 pub fn fchown(fd: c_int, uid: u32, gid: u32) -> io::Result<()> {
1777 cvt(unsafe { libc::fchown(fd, uid as libc::uid_t, gid as libc::gid_t) })?;
1778 Ok(())
1779 }
1780
1781 pub fn lchown(path: &Path, uid: u32, gid: u32) -> io::Result<()> {
1782 run_path_with_cstr(path, |path| {
1783 cvt(unsafe { libc::lchown(path.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) })
1784 .map(|_| ())
1785 })
1786 }
1787
1788 #[cfg(not(any(target_os = "fuchsia", target_os = "vxworks")))]
1789 pub fn chroot(dir: &Path) -> io::Result<()> {
1790 run_path_with_cstr(dir, |dir| cvt(unsafe { libc::chroot(dir.as_ptr()) }).map(|_| ()))
1791 }
1792
1793 pub use remove_dir_impl::remove_dir_all;
1794
1795 // Fallback for REDOX, ESP-ID, Horizon, and Miri
1796 #[cfg(any(
1797 target_os = "redox",
1798 target_os = "espidf",
1799 target_os = "horizon",
1800 target_os = "nto",
1801 miri
1802 ))]
1803 mod remove_dir_impl {
1804 pub use crate::sys_common::fs::remove_dir_all;
1805 }
1806
1807 // Modern implementation using openat(), unlinkat() and fdopendir()
1808 #[cfg(not(any(
1809 target_os = "redox",
1810 target_os = "espidf",
1811 target_os = "horizon",
1812 target_os = "nto",
1813 miri
1814 )))]
1815 mod remove_dir_impl {
1816 use super::{lstat, Dir, DirEntry, InnerReadDir, ReadDir};
1817 use crate::ffi::CStr;
1818 use crate::io;
1819 use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
1820 use crate::os::unix::prelude::{OwnedFd, RawFd};
1821 use crate::path::{Path, PathBuf};
1822 use crate::sys::common::small_c_string::run_path_with_cstr;
1823 use crate::sys::{cvt, cvt_r};
1824
1825 #[cfg(not(any(
1826 all(target_os = "linux", target_env = "gnu"),
1827 all(target_os = "macos", not(target_arch = "aarch64"))
1828 )))]
1829 use libc::{fdopendir, openat, unlinkat};
1830 #[cfg(all(target_os = "linux", target_env = "gnu"))]
1831 use libc::{fdopendir, openat64 as openat, unlinkat};
1832 #[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
1833 use macos_weak::{fdopendir, openat, unlinkat};
1834
1835 #[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
1836 mod macos_weak {
1837 use crate::sys::weak::weak;
1838 use libc::{c_char, c_int, DIR};
1839
1840 fn get_openat_fn() -> Option<unsafe extern "C" fn(c_int, *const c_char, c_int) -> c_int> {
1841 weak!(fn openat(c_int, *const c_char, c_int) -> c_int);
1842 openat.get()
1843 }
1844
1845 pub fn has_openat() -> bool {
1846 get_openat_fn().is_some()
1847 }
1848
1849 pub unsafe fn openat(dirfd: c_int, pathname: *const c_char, flags: c_int) -> c_int {
1850 get_openat_fn().map(|openat| openat(dirfd, pathname, flags)).unwrap_or_else(|| {
1851 crate::sys::unix::os::set_errno(libc::ENOSYS);
1852 -1
1853 })
1854 }
1855
1856 pub unsafe fn fdopendir(fd: c_int) -> *mut DIR {
1857 #[cfg(all(target_os = "macos", target_arch = "x86"))]
1858 weak!(fn fdopendir(c_int) -> *mut DIR, "fdopendir$INODE64$UNIX2003");
1859 #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
1860 weak!(fn fdopendir(c_int) -> *mut DIR, "fdopendir$INODE64");
1861 fdopendir.get().map(|fdopendir| fdopendir(fd)).unwrap_or_else(|| {
1862 crate::sys::unix::os::set_errno(libc::ENOSYS);
1863 crate::ptr::null_mut()
1864 })
1865 }
1866
1867 pub unsafe fn unlinkat(dirfd: c_int, pathname: *const c_char, flags: c_int) -> c_int {
1868 weak!(fn unlinkat(c_int, *const c_char, c_int) -> c_int);
1869 unlinkat.get().map(|unlinkat| unlinkat(dirfd, pathname, flags)).unwrap_or_else(|| {
1870 crate::sys::unix::os::set_errno(libc::ENOSYS);
1871 -1
1872 })
1873 }
1874 }
1875
1876 pub fn openat_nofollow_dironly(parent_fd: Option<RawFd>, p: &CStr) -> io::Result<OwnedFd> {
1877 let fd = cvt_r(|| unsafe {
1878 openat(
1879 parent_fd.unwrap_or(libc::AT_FDCWD),
1880 p.as_ptr(),
1881 libc::O_CLOEXEC | libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_DIRECTORY,
1882 )
1883 })?;
1884 Ok(unsafe { OwnedFd::from_raw_fd(fd) })
1885 }
1886
1887 fn fdreaddir(dir_fd: OwnedFd) -> io::Result<(ReadDir, RawFd)> {
1888 let ptr = unsafe { fdopendir(dir_fd.as_raw_fd()) };
1889 if ptr.is_null() {
1890 return Err(io::Error::last_os_error());
1891 }
1892 let dirp = Dir(ptr);
1893 // file descriptor is automatically closed by libc::closedir() now, so give up ownership
1894 let new_parent_fd = dir_fd.into_raw_fd();
1895 // a valid root is not needed because we do not call any functions involving the full path
1896 // of the `DirEntry`s.
1897 let dummy_root = PathBuf::new();
1898 let inner = InnerReadDir { dirp, root: dummy_root };
1899 Ok((ReadDir::new(inner), new_parent_fd))
1900 }
1901
1902 #[cfg(any(
1903 target_os = "solaris",
1904 target_os = "illumos",
1905 target_os = "haiku",
1906 target_os = "vxworks",
1907 ))]
1908 fn is_dir(_ent: &DirEntry) -> Option<bool> {
1909 None
1910 }
1911
1912 #[cfg(not(any(
1913 target_os = "solaris",
1914 target_os = "illumos",
1915 target_os = "haiku",
1916 target_os = "vxworks",
1917 )))]
1918 fn is_dir(ent: &DirEntry) -> Option<bool> {
1919 match ent.entry.d_type {
1920 libc::DT_UNKNOWN => None,
1921 libc::DT_DIR => Some(true),
1922 _ => Some(false),
1923 }
1924 }
1925
1926 fn remove_dir_all_recursive(parent_fd: Option<RawFd>, path: &CStr) -> io::Result<()> {
1927 // try opening as directory
1928 let fd = match openat_nofollow_dironly(parent_fd, &path) {
1929 Err(err) if matches!(err.raw_os_error(), Some(libc::ENOTDIR | libc::ELOOP)) => {
1930 // not a directory - don't traverse further
1931 // (for symlinks, older Linux kernels may return ELOOP instead of ENOTDIR)
1932 return match parent_fd {
1933 // unlink...
1934 Some(parent_fd) => {
1935 cvt(unsafe { unlinkat(parent_fd, path.as_ptr(), 0) }).map(drop)
1936 }
1937 // ...unless this was supposed to be the deletion root directory
1938 None => Err(err),
1939 };
1940 }
1941 result => result?,
1942 };
1943
1944 // open the directory passing ownership of the fd
1945 let (dir, fd) = fdreaddir(fd)?;
1946 for child in dir {
1947 let child = child?;
1948 let child_name = child.name_cstr();
1949 match is_dir(&child) {
1950 Some(true) => {
1951 remove_dir_all_recursive(Some(fd), child_name)?;
1952 }
1953 Some(false) => {
1954 cvt(unsafe { unlinkat(fd, child_name.as_ptr(), 0) })?;
1955 }
1956 None => {
1957 // POSIX specifies that calling unlink()/unlinkat(..., 0) on a directory can succeed
1958 // if the process has the appropriate privileges. This however can causing orphaned
1959 // directories requiring an fsck e.g. on Solaris and Illumos. So we try recursing
1960 // into it first instead of trying to unlink() it.
1961 remove_dir_all_recursive(Some(fd), child_name)?;
1962 }
1963 }
1964 }
1965
1966 // unlink the directory after removing its contents
1967 cvt(unsafe {
1968 unlinkat(parent_fd.unwrap_or(libc::AT_FDCWD), path.as_ptr(), libc::AT_REMOVEDIR)
1969 })?;
1970 Ok(())
1971 }
1972
1973 fn remove_dir_all_modern(p: &Path) -> io::Result<()> {
1974 // We cannot just call remove_dir_all_recursive() here because that would not delete a passed
1975 // symlink. No need to worry about races, because remove_dir_all_recursive() does not recurse
1976 // into symlinks.
1977 let attr = lstat(p)?;
1978 if attr.file_type().is_symlink() {
1979 crate::fs::remove_file(p)
1980 } else {
1981 run_path_with_cstr(p, |p| remove_dir_all_recursive(None, &p))
1982 }
1983 }
1984
1985 #[cfg(not(all(target_os = "macos", not(target_arch = "aarch64"))))]
1986 pub fn remove_dir_all(p: &Path) -> io::Result<()> {
1987 remove_dir_all_modern(p)
1988 }
1989
1990 #[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
1991 pub fn remove_dir_all(p: &Path) -> io::Result<()> {
1992 if macos_weak::has_openat() {
1993 // openat() is available with macOS 10.10+, just like unlinkat() and fdopendir()
1994 remove_dir_all_modern(p)
1995 } else {
1996 // fall back to classic implementation
1997 crate::sys_common::fs::remove_dir_all(p)
1998 }
1999 }
2000 }