std/sys/fs/
unix.rs

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