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