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