2022-10-26 07:48:47 +00:00
|
|
|
// miri has some special hacks here that make things unused.
|
|
|
|
#![cfg_attr(miri, allow(unused))]
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::os::unix::prelude::*;
|
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
use crate::ffi::{CStr, OsStr, OsString};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::fmt;
|
2022-06-07 07:43:54 +00:00
|
|
|
use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::mem;
|
2022-10-17 05:24:44 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
target_os = "illumos"
|
|
|
|
))]
|
|
|
|
use crate::mem::MaybeUninit;
|
2021-07-01 04:44:30 +00:00
|
|
|
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::path::{Path, PathBuf};
|
|
|
|
use crate::ptr;
|
|
|
|
use crate::sync::Arc;
|
2022-09-22 08:40:43 +00:00
|
|
|
use crate::sys::common::small_c_string::run_path_with_cstr;
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys::fd::FileDesc;
|
|
|
|
use crate::sys::time::SystemTime;
|
|
|
|
use crate::sys::{cvt, cvt_r};
|
2021-07-01 04:44:30 +00:00
|
|
|
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
2021-07-06 03:28:10 +00:00
|
|
|
#[cfg(any(
|
|
|
|
all(target_os = "linux", target_env = "gnu"),
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2021-07-06 03:28:10 +00:00
|
|
|
))]
|
|
|
|
use crate::sys::weak::syscall;
|
2022-06-27 18:05:10 +00:00
|
|
|
#[cfg(any(target_os = "android", target_os = "macos"))]
|
2021-07-06 03:28:10 +00:00
|
|
|
use crate::sys::weak::weak;
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use libc::{c_int, mode_t};
|
2015-02-03 05:39:14 +00:00
|
|
|
|
2021-06-03 05:53:03 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2021-06-03 05:53:03 +00:00
|
|
|
all(target_os = "linux", target_env = "gnu")
|
|
|
|
))]
|
|
|
|
use libc::c_char;
|
2018-05-24 21:50:28 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "android"))]
|
2018-06-25 18:42:27 +00:00
|
|
|
use libc::dirfd;
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
|
|
|
|
use libc::fstatat64;
|
2022-01-11 19:09:52 +00:00
|
|
|
#[cfg(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
target_os = "illumos"
|
|
|
|
))]
|
|
|
|
use libc::readdir as readdir64;
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
use libc::readdir64;
|
|
|
|
#[cfg(any(target_os = "emscripten", target_os = "l4re"))]
|
|
|
|
use libc::readdir64_r;
|
2016-03-01 10:48:03 +00:00
|
|
|
#[cfg(not(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2018-05-24 21:50:28 +00:00
|
|
|
target_os = "linux",
|
|
|
|
target_os = "emscripten",
|
2017-02-21 22:41:28 +00:00
|
|
|
target_os = "solaris",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "illumos",
|
2017-08-18 09:50:20 +00:00
|
|
|
target_os = "l4re",
|
2019-04-07 14:39:54 +00:00
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox"
|
2019-12-22 22:42:04 +00:00
|
|
|
)))]
|
2016-02-15 00:24:02 +00:00
|
|
|
use libc::readdir_r as readdir64_r;
|
2016-02-21 09:04:14 +00:00
|
|
|
#[cfg(target_os = "android")]
|
2018-06-25 18:42:27 +00:00
|
|
|
use libc::{
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 20:58:38 +00:00
|
|
|
dirent as dirent64, fstat as fstat64, fstatat as fstatat64, ftruncate64, lseek64,
|
|
|
|
lstat as lstat64, off64_t, open as open64, stat as stat64,
|
2016-02-21 09:04:14 +00:00
|
|
|
};
|
2016-03-01 10:48:03 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "emscripten",
|
2017-08-18 09:50:20 +00:00
|
|
|
target_os = "l4re",
|
2016-03-01 10:48:03 +00:00
|
|
|
target_os = "android"
|
|
|
|
)))]
|
2016-02-15 00:09:57 +00:00
|
|
|
use libc::{
|
2016-02-15 00:27:18 +00:00
|
|
|
dirent as dirent64, fstat as fstat64, ftruncate as ftruncate64, lseek as lseek64,
|
|
|
|
lstat as lstat64, off_t as off64_t, open as open64, stat as stat64,
|
|
|
|
};
|
2016-03-01 10:48:03 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "l4re"))]
|
2022-01-11 19:09:52 +00:00
|
|
|
use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, stat64};
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
|
2021-12-18 14:30:30 +00:00
|
|
|
pub use crate::sys_common::fs::try_exists;
|
2019-03-29 22:57:14 +00:00
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub struct File(FileDesc);
|
|
|
|
|
2020-01-08 06:18:42 +00:00
|
|
|
// FIXME: This should be available on Linux with all `target_env`.
|
|
|
|
// But currently only glibc exposes `statx` fn and structs.
|
|
|
|
// We don't want to import unverified raw C structs here directly.
|
|
|
|
// https://github.com/rust-lang/rust/pull/67774
|
2019-10-13 23:17:15 +00:00
|
|
|
macro_rules! cfg_has_statx {
|
|
|
|
({ $($then_tt:tt)* } else { $($else_tt:tt)* }) => {
|
|
|
|
cfg_if::cfg_if! {
|
2020-01-08 06:18:42 +00:00
|
|
|
if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
|
2019-10-13 23:17:15 +00:00
|
|
|
$($then_tt)*
|
|
|
|
} else {
|
|
|
|
$($else_tt)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($($block_inner:tt)*) => {
|
2020-01-08 06:18:42 +00:00
|
|
|
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
2019-10-13 23:17:15 +00:00
|
|
|
{
|
|
|
|
$($block_inner)*
|
|
|
|
}
|
|
|
|
};
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 23:17:15 +00:00
|
|
|
cfg_has_statx! {{
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FileAttr {
|
|
|
|
stat: stat64,
|
|
|
|
statx_extra_fields: Option<StatxExtraFields>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct StatxExtraFields {
|
|
|
|
// This is needed to check if btime is supported by the filesystem.
|
|
|
|
stx_mask: u32,
|
|
|
|
stx_btime: libc::statx_timestamp,
|
2022-03-10 01:05:16 +00:00
|
|
|
// With statx, we can overcome 32-bit `time_t` too.
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
stx_atime: libc::statx_timestamp,
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
stx_ctime: libc::statx_timestamp,
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
stx_mtime: libc::statx_timestamp,
|
|
|
|
|
2019-10-13 23:17:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 01:05:16 +00:00
|
|
|
// We prefer `statx` on Linux if available, which contains file creation time,
|
|
|
|
// as well as 64-bit timestamps of all kinds.
|
|
|
|
// Default `stat64` contains no creation time and may have 32-bit `time_t`.
|
2019-10-13 23:17:15 +00:00
|
|
|
unsafe fn try_statx(
|
|
|
|
fd: c_int,
|
2021-06-03 05:53:03 +00:00
|
|
|
path: *const c_char,
|
2019-10-13 23:17:15 +00:00
|
|
|
flags: i32,
|
|
|
|
mask: u32,
|
|
|
|
) -> Option<io::Result<FileAttr>> {
|
2019-10-22 04:02:32 +00:00
|
|
|
use crate::sync::atomic::{AtomicU8, Ordering};
|
2019-10-13 23:17:15 +00:00
|
|
|
|
|
|
|
// Linux kernel prior to 4.11 or glibc prior to glibc 2.28 don't support `statx`
|
2019-10-22 04:02:32 +00:00
|
|
|
// We store the availability in global to avoid unnecessary syscalls.
|
|
|
|
// 0: Unknown
|
|
|
|
// 1: Not available
|
|
|
|
// 2: Available
|
|
|
|
static STATX_STATE: AtomicU8 = AtomicU8::new(0);
|
2019-10-13 23:17:15 +00:00
|
|
|
syscall! {
|
|
|
|
fn statx(
|
|
|
|
fd: c_int,
|
2021-06-03 05:53:03 +00:00
|
|
|
pathname: *const c_char,
|
2019-10-13 23:17:15 +00:00
|
|
|
flags: c_int,
|
|
|
|
mask: libc::c_uint,
|
|
|
|
statxbuf: *mut libc::statx
|
|
|
|
) -> c_int
|
|
|
|
}
|
|
|
|
|
2019-10-22 04:02:32 +00:00
|
|
|
match STATX_STATE.load(Ordering::Relaxed) {
|
|
|
|
0 => {
|
2021-05-02 21:55:22 +00:00
|
|
|
// It is a trick to call `statx` with null pointers to check if the syscall
|
2019-10-22 18:43:45 +00:00
|
|
|
// is available. According to the manual, it is expected to fail with EFAULT.
|
|
|
|
// We do this mainly for performance, since it is nearly hundreds times
|
2019-11-27 03:19:54 +00:00
|
|
|
// faster than a normal successful call.
|
2019-10-22 18:43:45 +00:00
|
|
|
let err = cvt(statx(0, ptr::null(), 0, libc::STATX_ALL, ptr::null_mut()))
|
2019-10-22 04:02:32 +00:00
|
|
|
.err()
|
|
|
|
.and_then(|e| e.raw_os_error());
|
2019-10-22 18:43:45 +00:00
|
|
|
// We don't check `err == Some(libc::ENOSYS)` because the syscall may be limited
|
|
|
|
// and returns `EPERM`. Listing all possible errors seems not a good idea.
|
2019-10-22 04:02:32 +00:00
|
|
|
// See: https://github.com/rust-lang/rust/issues/65662
|
2019-10-22 18:43:45 +00:00
|
|
|
if err != Some(libc::EFAULT) {
|
2019-10-22 04:02:32 +00:00
|
|
|
STATX_STATE.store(1, Ordering::Relaxed);
|
2019-10-22 18:43:45 +00:00
|
|
|
return None;
|
2019-10-13 23:17:15 +00:00
|
|
|
}
|
2019-10-22 18:43:45 +00:00
|
|
|
STATX_STATE.store(2, Ordering::Relaxed);
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
2019-10-22 18:43:45 +00:00
|
|
|
1 => return None,
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-10-22 04:02:32 +00:00
|
|
|
|
2019-10-22 18:43:45 +00:00
|
|
|
let mut buf: libc::statx = mem::zeroed();
|
|
|
|
if let Err(err) = cvt(statx(fd, path, flags, mask, &mut buf)) {
|
|
|
|
return Some(Err(err));
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
2019-10-22 18:43:45 +00:00
|
|
|
|
|
|
|
// We cannot fill `stat64` exhaustively because of private padding fields.
|
|
|
|
let mut stat: stat64 = mem::zeroed();
|
|
|
|
// `c_ulong` on gnu-mips, `dev_t` otherwise
|
|
|
|
stat.st_dev = libc::makedev(buf.stx_dev_major, buf.stx_dev_minor) as _;
|
|
|
|
stat.st_ino = buf.stx_ino as libc::ino64_t;
|
|
|
|
stat.st_nlink = buf.stx_nlink as libc::nlink_t;
|
|
|
|
stat.st_mode = buf.stx_mode as libc::mode_t;
|
|
|
|
stat.st_uid = buf.stx_uid as libc::uid_t;
|
|
|
|
stat.st_gid = buf.stx_gid as libc::gid_t;
|
|
|
|
stat.st_rdev = libc::makedev(buf.stx_rdev_major, buf.stx_rdev_minor) as _;
|
|
|
|
stat.st_size = buf.stx_size as off64_t;
|
|
|
|
stat.st_blksize = buf.stx_blksize as libc::blksize_t;
|
|
|
|
stat.st_blocks = buf.stx_blocks as libc::blkcnt64_t;
|
|
|
|
stat.st_atime = buf.stx_atime.tv_sec as libc::time_t;
|
|
|
|
// `i64` on gnu-x86_64-x32, `c_ulong` otherwise.
|
|
|
|
stat.st_atime_nsec = buf.stx_atime.tv_nsec as _;
|
|
|
|
stat.st_mtime = buf.stx_mtime.tv_sec as libc::time_t;
|
|
|
|
stat.st_mtime_nsec = buf.stx_mtime.tv_nsec as _;
|
|
|
|
stat.st_ctime = buf.stx_ctime.tv_sec as libc::time_t;
|
|
|
|
stat.st_ctime_nsec = buf.stx_ctime.tv_nsec as _;
|
|
|
|
|
|
|
|
let extra = StatxExtraFields {
|
|
|
|
stx_mask: buf.stx_mask,
|
|
|
|
stx_btime: buf.stx_btime,
|
2022-03-10 01:05:16 +00:00
|
|
|
// Store full times to avoid 32-bit `time_t` truncation.
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
stx_atime: buf.stx_atime,
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
stx_ctime: buf.stx_ctime,
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
stx_mtime: buf.stx_mtime,
|
2019-10-22 18:43:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(Ok(FileAttr { stat, statx_extra_fields: Some(extra) }))
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
2019-10-13 23:17:15 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FileAttr {
|
|
|
|
stat: stat64,
|
|
|
|
}
|
|
|
|
}}
|
2015-02-03 05:39:14 +00:00
|
|
|
|
2018-05-24 21:50:28 +00:00
|
|
|
// all DirEntry's will have a reference to this struct
|
|
|
|
struct InnerReadDir {
|
2015-02-20 17:46:56 +00:00
|
|
|
dirp: Dir,
|
2018-05-24 21:50:28 +00:00
|
|
|
root: PathBuf,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 19:03:27 +00:00
|
|
|
pub struct ReadDir {
|
|
|
|
inner: Arc<InnerReadDir>,
|
2020-10-09 16:59:39 +00:00
|
|
|
#[cfg(not(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "linux",
|
2020-10-09 16:59:39 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
)))]
|
2018-06-12 19:03:27 +00:00
|
|
|
end_of_stream: bool,
|
|
|
|
}
|
2018-05-24 21:50:28 +00:00
|
|
|
|
2015-02-20 17:46:56 +00:00
|
|
|
struct Dir(*mut libc::DIR);
|
|
|
|
|
|
|
|
unsafe impl Send for Dir {}
|
|
|
|
unsafe impl Sync for Dir {}
|
|
|
|
|
2022-03-08 21:36:01 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox"
|
|
|
|
))]
|
2015-02-03 05:39:14 +00:00
|
|
|
pub struct DirEntry {
|
2020-10-07 21:35:51 +00:00
|
|
|
dir: Arc<InnerReadDir>,
|
2022-03-08 21:36:01 +00:00
|
|
|
entry: dirent64_min,
|
2022-01-11 19:09:52 +00:00
|
|
|
// We need to store an owned copy of the entry name on platforms that use
|
|
|
|
// readdir() (not readdir_r()), because a) struct dirent may use a flexible
|
|
|
|
// array to store the name, b) it lives only until the next readdir() call.
|
2022-09-22 08:40:43 +00:00
|
|
|
name: crate::ffi::CString,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 21:36:01 +00:00
|
|
|
// Define a minimal subset of fields we need from `dirent64`, especially since
|
|
|
|
// we're not using the immediate `d_name` on these targets. Keeping this as an
|
|
|
|
// `entry` field in `DirEntry` helps reduce the `cfg` boilerplate elsewhere.
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox"
|
|
|
|
))]
|
|
|
|
struct dirent64_min {
|
|
|
|
d_ino: u64,
|
|
|
|
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
|
|
|
|
d_type: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox"
|
|
|
|
)))]
|
|
|
|
pub struct DirEntry {
|
|
|
|
dir: Arc<InnerReadDir>,
|
|
|
|
// The full entry includes a fixed-length `d_name`.
|
|
|
|
entry: dirent64,
|
|
|
|
}
|
|
|
|
|
2016-11-25 18:21:49 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2015-02-03 05:39:14 +00:00
|
|
|
pub struct OpenOptions {
|
2016-01-13 17:08:08 +00:00
|
|
|
// generic
|
2015-02-03 05:39:14 +00:00
|
|
|
read: bool,
|
|
|
|
write: bool,
|
2016-01-13 17:08:08 +00:00
|
|
|
append: bool,
|
|
|
|
truncate: bool,
|
|
|
|
create: bool,
|
|
|
|
create_new: bool,
|
|
|
|
// system-specific
|
2016-01-13 20:47:46 +00:00
|
|
|
custom_flags: i32,
|
2015-02-03 05:39:14 +00:00
|
|
|
mode: mode_t,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct FilePermissions {
|
|
|
|
mode: mode_t,
|
|
|
|
}
|
|
|
|
|
2022-09-10 10:29:01 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
pub struct FileTimes {
|
|
|
|
accessed: Option<SystemTime>,
|
|
|
|
modified: Option<SystemTime>,
|
|
|
|
}
|
2022-06-19 03:00:21 +00:00
|
|
|
|
2016-07-10 22:37:41 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2015-04-16 06:21:13 +00:00
|
|
|
pub struct FileType {
|
|
|
|
mode: mode_t,
|
|
|
|
}
|
|
|
|
|
2016-11-25 18:21:49 +00:00
|
|
|
#[derive(Debug)]
|
2015-04-28 00:29:35 +00:00
|
|
|
pub struct DirBuilder {
|
|
|
|
mode: mode_t,
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:17:15 +00:00
|
|
|
cfg_has_statx! {{
|
|
|
|
impl FileAttr {
|
|
|
|
fn from_stat64(stat: stat64) -> Self {
|
|
|
|
Self { stat, statx_extra_fields: None }
|
|
|
|
}
|
2022-03-10 01:05:16 +00:00
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
pub fn stx_mtime(&self) -> Option<&libc::statx_timestamp> {
|
|
|
|
if let Some(ext) = &self.statx_extra_fields {
|
|
|
|
if (ext.stx_mask & libc::STATX_MTIME) != 0 {
|
|
|
|
return Some(&ext.stx_mtime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
pub fn stx_atime(&self) -> Option<&libc::statx_timestamp> {
|
|
|
|
if let Some(ext) = &self.statx_extra_fields {
|
|
|
|
if (ext.stx_mask & libc::STATX_ATIME) != 0 {
|
|
|
|
return Some(&ext.stx_atime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
pub fn stx_ctime(&self) -> Option<&libc::statx_timestamp> {
|
|
|
|
if let Some(ext) = &self.statx_extra_fields {
|
|
|
|
if (ext.stx_mask & libc::STATX_CTIME) != 0 {
|
|
|
|
return Some(&ext.stx_ctime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2019-10-13 23:17:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
impl FileAttr {
|
|
|
|
fn from_stat64(stat: stat64) -> Self {
|
|
|
|
Self { stat }
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-13 23:17:15 +00:00
|
|
|
}}
|
2019-10-04 08:17:23 +00:00
|
|
|
|
2019-10-13 23:17:15 +00:00
|
|
|
impl FileAttr {
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn size(&self) -> u64 {
|
|
|
|
self.stat.st_size as u64
|
|
|
|
}
|
|
|
|
pub fn perm(&self) -> FilePermissions {
|
2017-09-17 21:42:58 +00:00
|
|
|
FilePermissions { mode: (self.stat.st_mode as mode_t) }
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn file_type(&self) -> FileType {
|
|
|
|
FileType { mode: self.stat.st_mode as mode_t }
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 15:31:56 +00:00
|
|
|
#[cfg(target_os = "netbsd")]
|
|
|
|
impl FileAttr {
|
|
|
|
pub fn modified(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64))
|
2016-02-21 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn accessed(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64))
|
2016-02-21 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn created(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64))
|
2016-02-21 15:31:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
#[cfg(not(target_os = "netbsd"))]
|
2016-01-13 01:24:16 +00:00
|
|
|
impl FileAttr {
|
2021-10-19 15:59:59 +00:00
|
|
|
#[cfg(not(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon")))]
|
2016-01-13 01:24:16 +00:00
|
|
|
pub fn modified(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
cfg_has_statx! {
|
|
|
|
if let Some(mtime) = self.stx_mtime() {
|
|
|
|
return Ok(SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64))
|
2016-01-13 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(any(target_os = "vxworks", target_os = "espidf"))]
|
2020-10-07 18:38:25 +00:00
|
|
|
pub fn modified(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(self.stat.st_mtime as i64, 0))
|
2020-10-07 18:38:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 15:59:59 +00:00
|
|
|
#[cfg(target_os = "horizon")]
|
|
|
|
pub fn modified(&self) -> io::Result<SystemTime> {
|
|
|
|
Ok(SystemTime::from(self.stat.st_mtim))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon")))]
|
2016-01-13 01:24:16 +00:00
|
|
|
pub fn accessed(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
cfg_has_statx! {
|
|
|
|
if let Some(atime) = self.stx_atime() {
|
|
|
|
return Ok(SystemTime::new(atime.tv_sec, atime.tv_nsec as i64));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64))
|
2016-01-13 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 01:55:01 +00:00
|
|
|
#[cfg(any(target_os = "vxworks", target_os = "espidf"))]
|
2020-10-07 18:38:25 +00:00
|
|
|
pub fn accessed(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(self.stat.st_atime as i64, 0))
|
2020-10-07 18:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 01:55:01 +00:00
|
|
|
#[cfg(target_os = "horizon")]
|
|
|
|
pub fn accessed(&self) -> io::Result<SystemTime> {
|
|
|
|
Ok(SystemTime::from(self.stat.st_atim))
|
|
|
|
}
|
|
|
|
|
2019-05-13 07:13:07 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "freebsd",
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "macos",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "ios",
|
|
|
|
target_os = "watchos",
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
))]
|
2016-01-13 01:24:16 +00:00
|
|
|
pub fn created(&self) -> io::Result<SystemTime> {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64))
|
2016-01-13 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 07:13:07 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "freebsd",
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "macos",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "ios",
|
|
|
|
target_os = "watchos",
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
)))]
|
2016-01-13 01:24:16 +00:00
|
|
|
pub fn created(&self) -> io::Result<SystemTime> {
|
2019-10-13 23:17:15 +00:00
|
|
|
cfg_has_statx! {
|
2019-10-04 08:17:23 +00:00
|
|
|
if let Some(ext) = &self.statx_extra_fields {
|
|
|
|
return if (ext.stx_mask & libc::STATX_BTIME) != 0 {
|
2022-03-10 01:05:16 +00:00
|
|
|
Ok(SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64))
|
2019-10-04 08:17:23 +00:00
|
|
|
} else {
|
2021-08-08 08:04:33 +00:00
|
|
|
Err(io::const_io_error!(
|
2021-06-15 12:27:31 +00:00
|
|
|
io::ErrorKind::Uncategorized,
|
2021-08-08 08:04:33 +00:00
|
|
|
"creation time is not available for the filesystem",
|
2019-10-04 08:17:23 +00:00
|
|
|
))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:04:33 +00:00
|
|
|
Err(io::const_io_error!(
|
2021-03-19 00:39:20 +00:00
|
|
|
io::ErrorKind::Unsupported,
|
2021-08-08 08:04:33 +00:00
|
|
|
"creation time is not available on this platform \
|
2016-01-13 01:24:16 +00:00
|
|
|
currently",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
impl AsInner<stat64> for FileAttr {
|
|
|
|
fn as_inner(&self) -> &stat64 {
|
|
|
|
&self.stat
|
|
|
|
}
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
impl FilePermissions {
|
2017-08-15 13:42:11 +00:00
|
|
|
pub fn readonly(&self) -> bool {
|
|
|
|
// check if any class (owner, group, others) has write permission
|
|
|
|
self.mode & 0o222 == 0
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn set_readonly(&mut self, readonly: bool) {
|
|
|
|
if readonly {
|
2017-08-15 13:42:11 +00:00
|
|
|
// remove write permission for all classes; equivalent to `chmod a-w <file>`
|
2015-02-03 05:39:14 +00:00
|
|
|
self.mode &= !0o222;
|
|
|
|
} else {
|
2017-08-15 13:42:11 +00:00
|
|
|
// add write permission for all classes; equivalent to `chmod a+w <file>`
|
2015-02-03 05:39:14 +00:00
|
|
|
self.mode |= 0o222;
|
|
|
|
}
|
|
|
|
}
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
pub fn mode(&self) -> u32 {
|
|
|
|
self.mode as u32
|
|
|
|
}
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 03:00:21 +00:00
|
|
|
impl FileTimes {
|
|
|
|
pub fn set_accessed(&mut self, t: SystemTime) {
|
2022-09-10 10:29:01 +00:00
|
|
|
self.accessed = Some(t);
|
2022-06-19 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_modified(&mut self, t: SystemTime) {
|
2022-09-10 10:29:01 +00:00
|
|
|
self.modified = Some(t);
|
2022-06-19 03:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
impl FileType {
|
|
|
|
pub fn is_dir(&self) -> bool {
|
|
|
|
self.is(libc::S_IFDIR)
|
|
|
|
}
|
|
|
|
pub fn is_file(&self) -> bool {
|
|
|
|
self.is(libc::S_IFREG)
|
|
|
|
}
|
|
|
|
pub fn is_symlink(&self) -> bool {
|
|
|
|
self.is(libc::S_IFLNK)
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:16:25 +00:00
|
|
|
pub fn is(&self, mode: mode_t) -> bool {
|
|
|
|
self.mode & libc::S_IFMT == mode
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
impl FromInner<u32> for FilePermissions {
|
|
|
|
fn from_inner(mode: u32) -> FilePermissions {
|
2015-02-03 05:39:14 +00:00
|
|
|
FilePermissions { mode: mode as mode_t }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 18:38:56 +00:00
|
|
|
impl fmt::Debug for ReadDir {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-10-16 18:38:56 +00:00
|
|
|
// This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
|
|
|
|
// Thus the result will be e g 'ReadDir("/home")'
|
2018-06-12 19:03:27 +00:00
|
|
|
fmt::Debug::fmt(&*self.inner.root, f)
|
2016-10-16 18:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
impl Iterator for ReadDir {
|
|
|
|
type Item = io::Result<DirEntry>;
|
|
|
|
|
2020-04-13 23:37:22 +00:00
|
|
|
#[cfg(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "linux",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
target_os = "illumos"
|
|
|
|
))]
|
2016-01-21 16:30:22 +00:00
|
|
|
fn next(&mut self) -> Option<io::Result<DirEntry>> {
|
|
|
|
unsafe {
|
|
|
|
loop {
|
2022-01-11 19:09:52 +00:00
|
|
|
// As of POSIX.1-2017, readdir() is not required to be thread safe; only
|
|
|
|
// readdir_r() is. However, readdir_r() cannot correctly handle platforms
|
|
|
|
// with unlimited or variable NAME_MAX. Many modern platforms guarantee
|
|
|
|
// thread safety for readdir() as long an individual DIR* is not accessed
|
|
|
|
// concurrently, which is sufficient for Rust.
|
2016-07-12 04:43:53 +00:00
|
|
|
super::os::set_errno(0);
|
2022-01-11 19:09:52 +00:00
|
|
|
let entry_ptr = readdir64(self.inner.dirp.0);
|
2016-01-21 16:30:22 +00:00
|
|
|
if entry_ptr.is_null() {
|
2021-05-02 21:55:22 +00:00
|
|
|
// null can mean either the end is reached or an error occurred.
|
2016-07-12 04:43:53 +00:00
|
|
|
// So we had to clear errno beforehand to check for an error now.
|
|
|
|
return match super::os::errno() {
|
|
|
|
0 => None,
|
|
|
|
e => Some(Err(Error::from_raw_os_error(e))),
|
|
|
|
};
|
2016-01-21 16:30:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 05:24:44 +00:00
|
|
|
// The dirent64 struct is a weird imaginary thing that isn't ever supposed
|
|
|
|
// to be worked with by value. Its trailing d_name field is declared
|
|
|
|
// variously as [c_char; 256] or [c_char; 1] on different systems but
|
|
|
|
// either way that size is meaningless; only the offset of d_name is
|
|
|
|
// meaningful. The dirent64 pointers that libc returns from readdir64 are
|
|
|
|
// allowed to point to allocations smaller _or_ LARGER than implied by the
|
|
|
|
// definition of the struct.
|
|
|
|
//
|
|
|
|
// As such, we need to be even more careful with dirent64 than if its
|
|
|
|
// contents were "simply" partially initialized data.
|
|
|
|
//
|
|
|
|
// Like for uninitialized contents, converting entry_ptr to `&dirent64`
|
|
|
|
// would not be legal. However, unique to dirent64 is that we don't even
|
|
|
|
// get to use `addr_of!((*entry_ptr).d_name)` because that operation
|
|
|
|
// requires the full extent of *entry_ptr to be in bounds of the same
|
|
|
|
// allocation, which is not necessarily the case here.
|
|
|
|
//
|
|
|
|
// Absent any other way to obtain a pointer to `(*entry_ptr).d_name`
|
|
|
|
// legally in Rust analogously to how it would be done in C, we instead
|
|
|
|
// need to make our own non-libc allocation that conforms to the weird
|
|
|
|
// imaginary definition of dirent64, and use that for a field offset
|
|
|
|
// computation.
|
|
|
|
macro_rules! offset_ptr {
|
|
|
|
($entry_ptr:expr, $field:ident) => {{
|
|
|
|
const OFFSET: isize = {
|
|
|
|
let delusion = MaybeUninit::<dirent64>::uninit();
|
|
|
|
let entry_ptr = delusion.as_ptr();
|
|
|
|
unsafe {
|
|
|
|
ptr::addr_of!((*entry_ptr).$field)
|
|
|
|
.cast::<u8>()
|
|
|
|
.offset_from(entry_ptr.cast::<u8>())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if true {
|
|
|
|
// Cast to the same type determined by the else branch.
|
|
|
|
$entry_ptr.byte_offset(OFFSET).cast::<_>()
|
|
|
|
} else {
|
|
|
|
#[allow(deref_nullptr)]
|
|
|
|
{
|
|
|
|
ptr::addr_of!((*ptr::null::<dirent64>()).$field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
// d_name is guaranteed to be null-terminated.
|
|
|
|
let name = CStr::from_ptr(offset_ptr!(entry_ptr, d_name).cast());
|
|
|
|
let name_bytes = name.to_bytes();
|
|
|
|
if name_bytes == b"." || name_bytes == b".." {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-29 16:23:28 +00:00
|
|
|
|
2022-03-08 21:36:01 +00:00
|
|
|
let entry = dirent64_min {
|
2022-10-17 05:24:44 +00:00
|
|
|
d_ino: *offset_ptr!(entry_ptr, d_ino) as u64,
|
2022-03-08 21:36:01 +00:00
|
|
|
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
|
2022-10-17 05:24:44 +00:00
|
|
|
d_type: *offset_ptr!(entry_ptr, d_type) as u8,
|
2022-03-08 21:36:01 +00:00
|
|
|
};
|
|
|
|
|
2022-10-17 05:24:44 +00:00
|
|
|
return Some(Ok(DirEntry {
|
2022-03-08 21:36:01 +00:00
|
|
|
entry,
|
2022-10-17 05:24:44 +00:00
|
|
|
name: name.to_owned(),
|
2020-10-07 21:35:51 +00:00
|
|
|
dir: Arc::clone(&self.inner),
|
2022-10-17 05:24:44 +00:00
|
|
|
}));
|
2016-01-21 16:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 23:37:22 +00:00
|
|
|
#[cfg(not(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "linux",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
target_os = "illumos"
|
|
|
|
)))]
|
2015-02-03 05:39:14 +00:00
|
|
|
fn next(&mut self) -> Option<io::Result<DirEntry>> {
|
2018-06-12 19:03:27 +00:00
|
|
|
if self.end_of_stream {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2015-12-02 18:31:29 +00:00
|
|
|
unsafe {
|
2020-10-07 21:35:51 +00:00
|
|
|
let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
|
2015-12-02 18:31:29 +00:00
|
|
|
let mut entry_ptr = ptr::null_mut();
|
|
|
|
loop {
|
2021-08-31 18:11:42 +00:00
|
|
|
let err = readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr);
|
|
|
|
if err != 0 {
|
2018-06-12 19:03:27 +00:00
|
|
|
if entry_ptr.is_null() {
|
|
|
|
// We encountered an error (which will be returned in this iteration), but
|
|
|
|
// we also reached the end of the directory stream. The `end_of_stream`
|
|
|
|
// flag is enabled to make sure that we return `None` in the next iteration
|
|
|
|
// (instead of looping forever)
|
|
|
|
self.end_of_stream = true;
|
|
|
|
}
|
2021-08-31 18:11:42 +00:00
|
|
|
return Some(Err(Error::from_raw_os_error(err)));
|
2015-12-02 18:31:29 +00:00
|
|
|
}
|
|
|
|
if entry_ptr.is_null() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if ret.name_bytes() != b"." && ret.name_bytes() != b".." {
|
|
|
|
return Some(Ok(ret));
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:46:56 +00:00
|
|
|
impl Drop for Dir {
|
2015-02-03 05:39:14 +00:00
|
|
|
fn drop(&mut self) {
|
2015-02-20 17:46:56 +00:00
|
|
|
let r = unsafe { libc::closedir(self.0) };
|
2022-08-09 22:19:13 +00:00
|
|
|
assert!(
|
|
|
|
r == 0 || crate::io::Error::last_os_error().kind() == crate::io::ErrorKind::Interrupted,
|
|
|
|
"unexpected error during closedir: {:?}",
|
|
|
|
crate::io::Error::last_os_error()
|
|
|
|
);
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
pub fn path(&self) -> PathBuf {
|
2022-01-20 21:54:45 +00:00
|
|
|
self.dir.root.join(self.file_name_os_str())
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn file_name(&self) -> OsString {
|
2022-01-20 21:54:45 +00:00
|
|
|
self.file_name_os_str().to_os_string()
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 03:51:54 +00:00
|
|
|
#[cfg(all(
|
|
|
|
any(target_os = "linux", target_os = "emscripten", target_os = "android"),
|
|
|
|
not(miri)
|
|
|
|
))]
|
2018-05-24 21:50:28 +00:00
|
|
|
pub fn metadata(&self) -> io::Result<FileAttr> {
|
2020-10-07 21:35:51 +00:00
|
|
|
let fd = cvt(unsafe { dirfd(self.dir.dirp.0) })?;
|
2022-01-20 21:54:45 +00:00
|
|
|
let name = self.name_cstr().as_ptr();
|
2019-10-04 08:17:23 +00:00
|
|
|
|
2019-10-13 23:17:15 +00:00
|
|
|
cfg_has_statx! {
|
2019-10-04 08:17:23 +00:00
|
|
|
if let Some(ret) = unsafe { try_statx(
|
|
|
|
fd,
|
|
|
|
name,
|
|
|
|
libc::AT_SYMLINK_NOFOLLOW | libc::AT_STATX_SYNC_AS_STAT,
|
|
|
|
libc::STATX_ALL,
|
|
|
|
) } {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 21:50:28 +00:00
|
|
|
let mut stat: stat64 = unsafe { mem::zeroed() };
|
2019-10-04 08:17:23 +00:00
|
|
|
cvt(unsafe { fstatat64(fd, name, &mut stat, libc::AT_SYMLINK_NOFOLLOW) })?;
|
|
|
|
Ok(FileAttr::from_stat64(stat))
|
2018-05-24 21:50:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 03:51:54 +00:00
|
|
|
#[cfg(any(
|
|
|
|
not(any(target_os = "linux", target_os = "emscripten", target_os = "android")),
|
|
|
|
miri
|
|
|
|
))]
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn metadata(&self) -> io::Result<FileAttr> {
|
|
|
|
lstat(&self.path())
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:38:25 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "haiku",
|
|
|
|
target_os = "vxworks"
|
|
|
|
))]
|
2016-09-25 04:38:56 +00:00
|
|
|
pub fn file_type(&self) -> io::Result<FileType> {
|
2022-01-30 00:58:18 +00:00
|
|
|
self.metadata().map(|m| m.file_type())
|
2016-09-25 04:38:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:38:25 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "haiku",
|
|
|
|
target_os = "vxworks"
|
|
|
|
)))]
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn file_type(&self) -> io::Result<FileType> {
|
2015-12-02 18:31:29 +00:00
|
|
|
match self.entry.d_type {
|
|
|
|
libc::DT_CHR => Ok(FileType { mode: libc::S_IFCHR }),
|
|
|
|
libc::DT_FIFO => Ok(FileType { mode: libc::S_IFIFO }),
|
|
|
|
libc::DT_LNK => Ok(FileType { mode: libc::S_IFLNK }),
|
|
|
|
libc::DT_REG => Ok(FileType { mode: libc::S_IFREG }),
|
|
|
|
libc::DT_SOCK => Ok(FileType { mode: libc::S_IFSOCK }),
|
|
|
|
libc::DT_DIR => Ok(FileType { mode: libc::S_IFDIR }),
|
|
|
|
libc::DT_BLK => Ok(FileType { mode: libc::S_IFBLK }),
|
2022-01-30 00:58:18 +00:00
|
|
|
_ => self.metadata().map(|m| m.file_type()),
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 18:31:29 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2016-01-21 16:30:22 +00:00
|
|
|
target_os = "linux",
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "android",
|
2016-09-25 04:38:56 +00:00
|
|
|
target_os = "solaris",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "illumos",
|
2016-10-20 23:23:43 +00:00
|
|
|
target_os = "haiku",
|
2017-08-18 09:50:20 +00:00
|
|
|
target_os = "l4re",
|
2018-07-30 13:50:51 +00:00
|
|
|
target_os = "fuchsia",
|
2020-10-07 18:38:25 +00:00
|
|
|
target_os = "redox",
|
2021-07-29 17:18:22 +00:00
|
|
|
target_os = "vxworks",
|
2021-10-19 15:59:59 +00:00
|
|
|
target_os = "espidf",
|
|
|
|
target_os = "horizon"
|
2019-04-07 14:39:54 +00:00
|
|
|
))]
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
pub fn ino(&self) -> u64 {
|
|
|
|
self.entry.d_ino as u64
|
2015-12-02 18:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "dragonfly"
|
|
|
|
))]
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
pub fn ino(&self) -> u64 {
|
|
|
|
self.entry.d_fileno as u64
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 18:31:29 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2015-12-23 17:56:42 +00:00
|
|
|
target_os = "netbsd",
|
2016-01-27 05:37:46 +00:00
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "freebsd",
|
2019-05-13 07:13:07 +00:00
|
|
|
target_os = "dragonfly"
|
|
|
|
))]
|
2015-12-02 18:31:29 +00:00
|
|
|
fn name_bytes(&self) -> &[u8] {
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::slice;
|
2015-02-03 05:39:14 +00:00
|
|
|
unsafe {
|
2019-02-10 19:23:21 +00:00
|
|
|
slice::from_raw_parts(
|
|
|
|
self.entry.d_name.as_ptr() as *const u8,
|
|
|
|
self.entry.d_namlen as usize,
|
|
|
|
)
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-20 21:54:45 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "ios",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2022-01-20 21:54:45 +00:00
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "dragonfly"
|
|
|
|
)))]
|
2016-01-21 16:30:22 +00:00
|
|
|
fn name_bytes(&self) -> &[u8] {
|
2022-01-20 21:54:45 +00:00
|
|
|
self.name_cstr().to_bytes()
|
2021-12-18 14:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "linux",
|
2021-12-18 14:30:30 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox"
|
|
|
|
)))]
|
|
|
|
fn name_cstr(&self) -> &CStr {
|
|
|
|
unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()) }
|
|
|
|
}
|
2022-01-20 21:54:45 +00:00
|
|
|
#[cfg(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "linux",
|
2022-01-20 21:54:45 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox"
|
|
|
|
))]
|
2021-12-18 14:30:30 +00:00
|
|
|
fn name_cstr(&self) -> &CStr {
|
|
|
|
&self.name
|
2016-01-21 16:30:22 +00:00
|
|
|
}
|
2021-03-27 18:25:52 +00:00
|
|
|
|
|
|
|
pub fn file_name_os_str(&self) -> &OsStr {
|
|
|
|
OsStr::from_bytes(self.name_bytes())
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenOptions {
|
|
|
|
pub fn new() -> OpenOptions {
|
|
|
|
OpenOptions {
|
2016-01-13 17:08:08 +00:00
|
|
|
// generic
|
2015-02-03 05:39:14 +00:00
|
|
|
read: false,
|
|
|
|
write: false,
|
2016-01-13 17:08:08 +00:00
|
|
|
append: false,
|
|
|
|
truncate: false,
|
|
|
|
create: false,
|
|
|
|
create_new: false,
|
|
|
|
// system-specific
|
|
|
|
custom_flags: 0,
|
2015-02-11 22:40:09 +00:00
|
|
|
mode: 0o666,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 17:08:08 +00:00
|
|
|
pub fn read(&mut self, read: bool) {
|
|
|
|
self.read = read;
|
|
|
|
}
|
|
|
|
pub fn write(&mut self, write: bool) {
|
|
|
|
self.write = write;
|
|
|
|
}
|
|
|
|
pub fn append(&mut self, append: bool) {
|
|
|
|
self.append = append;
|
|
|
|
}
|
|
|
|
pub fn truncate(&mut self, truncate: bool) {
|
|
|
|
self.truncate = truncate;
|
|
|
|
}
|
|
|
|
pub fn create(&mut self, create: bool) {
|
|
|
|
self.create = create;
|
|
|
|
}
|
|
|
|
pub fn create_new(&mut self, create_new: bool) {
|
|
|
|
self.create_new = create_new;
|
|
|
|
}
|
|
|
|
|
2016-01-13 20:47:46 +00:00
|
|
|
pub fn custom_flags(&mut self, flags: i32) {
|
|
|
|
self.custom_flags = flags;
|
|
|
|
}
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
pub fn mode(&mut self, mode: u32) {
|
|
|
|
self.mode = mode as mode_t;
|
|
|
|
}
|
2016-01-13 17:08:08 +00:00
|
|
|
|
|
|
|
fn get_access_mode(&self) -> io::Result<c_int> {
|
|
|
|
match (self.read, self.write, self.append) {
|
|
|
|
(true, false, false) => Ok(libc::O_RDONLY),
|
|
|
|
(false, true, false) => Ok(libc::O_WRONLY),
|
|
|
|
(true, true, false) => Ok(libc::O_RDWR),
|
|
|
|
(false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND),
|
|
|
|
(true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND),
|
|
|
|
(false, false, false) => Err(Error::from_raw_os_error(libc::EINVAL)),
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 17:08:08 +00:00
|
|
|
fn get_creation_mode(&self) -> io::Result<c_int> {
|
|
|
|
match (self.write, self.append) {
|
2016-01-15 18:04:53 +00:00
|
|
|
(true, false) => {}
|
|
|
|
(false, false) => {
|
|
|
|
if self.truncate || self.create || self.create_new {
|
|
|
|
return Err(Error::from_raw_os_error(libc::EINVAL));
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2016-01-15 18:04:53 +00:00
|
|
|
(_, true) => {
|
|
|
|
if self.truncate && !self.create_new {
|
|
|
|
return Err(Error::from_raw_os_error(libc::EINVAL));
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
2016-01-13 17:08:08 +00:00
|
|
|
|
|
|
|
Ok(match (self.create, self.truncate, self.create_new) {
|
|
|
|
(false, false, false) => 0,
|
|
|
|
(true, false, false) => libc::O_CREAT,
|
|
|
|
(false, true, false) => libc::O_TRUNC,
|
|
|
|
(true, true, false) => libc::O_CREAT | libc::O_TRUNC,
|
|
|
|
(_, _, true) => libc::O_CREAT | libc::O_EXCL,
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl File {
|
|
|
|
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(path, |path| File::open_c(path, opts))
|
2015-04-03 22:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
|
2016-01-13 17:08:08 +00:00
|
|
|
let flags = libc::O_CLOEXEC
|
2016-03-23 03:01:37 +00:00
|
|
|
| opts.get_access_mode()?
|
|
|
|
| opts.get_creation_mode()?
|
2016-01-13 17:08:08 +00:00
|
|
|
| (opts.custom_flags as c_int & !libc::O_ACCMODE);
|
2020-05-05 14:43:57 +00:00
|
|
|
// The third argument of `open64` is documented to have type `mode_t`. On
|
|
|
|
// some platforms (like macOS, where `open64` is actually `open`), `mode_t` is `u16`.
|
|
|
|
// However, since this is a variadic function, C integer promotion rules mean that on
|
|
|
|
// the ABI level, this still gets passed as `c_int` (aka `u32` on Unix platforms).
|
2016-02-15 00:27:18 +00:00
|
|
|
let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode as c_int) })?;
|
2021-07-01 04:44:30 +00:00
|
|
|
Ok(File(unsafe { FileDesc::from_raw_fd(fd) }))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_attr(&self) -> io::Result<FileAttr> {
|
2021-07-01 04:44:30 +00:00
|
|
|
let fd = self.as_raw_fd();
|
2019-10-04 08:17:23 +00:00
|
|
|
|
2019-10-13 23:17:15 +00:00
|
|
|
cfg_has_statx! {
|
2019-10-04 08:17:23 +00:00
|
|
|
if let Some(ret) = unsafe { try_statx(
|
|
|
|
fd,
|
2021-06-03 05:53:03 +00:00
|
|
|
b"\0" as *const _ as *const c_char,
|
2019-10-04 08:17:23 +00:00
|
|
|
libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT,
|
|
|
|
libc::STATX_ALL,
|
|
|
|
) } {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
let mut stat: stat64 = unsafe { mem::zeroed() };
|
2019-10-04 08:17:23 +00:00
|
|
|
cvt(unsafe { fstat64(fd, &mut stat) })?;
|
|
|
|
Ok(FileAttr::from_stat64(stat))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fsync(&self) -> io::Result<()> {
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt_r(|| unsafe { os_fsync(self.as_raw_fd()) })?;
|
2019-04-19 18:42:52 +00:00
|
|
|
return Ok(());
|
|
|
|
|
2022-03-23 16:05:01 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
|
2019-04-19 18:42:52 +00:00
|
|
|
unsafe fn os_fsync(fd: c_int) -> c_int {
|
|
|
|
libc::fcntl(fd, libc::F_FULLFSYNC)
|
|
|
|
}
|
2022-03-23 16:05:01 +00:00
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "watchos")))]
|
2019-04-19 18:42:52 +00:00
|
|
|
unsafe fn os_fsync(fd: c_int) -> c_int {
|
|
|
|
libc::fsync(fd)
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn datasync(&self) -> io::Result<()> {
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt_r(|| unsafe { os_datasync(self.as_raw_fd()) })?;
|
2015-02-03 05:39:14 +00:00
|
|
|
return Ok(());
|
|
|
|
|
2022-03-23 16:05:01 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
|
2015-02-03 05:39:14 +00:00
|
|
|
unsafe fn os_datasync(fd: c_int) -> c_int {
|
|
|
|
libc::fcntl(fd, libc::F_FULLFSYNC)
|
|
|
|
}
|
2020-10-13 13:57:31 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd"
|
|
|
|
))]
|
2015-02-03 05:39:14 +00:00
|
|
|
unsafe fn os_datasync(fd: c_int) -> c_int {
|
|
|
|
libc::fdatasync(fd)
|
|
|
|
}
|
2020-10-13 13:57:31 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "ios",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "netbsd",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "watchos",
|
2020-10-13 13:57:31 +00:00
|
|
|
)))]
|
2015-02-03 05:39:14 +00:00
|
|
|
unsafe fn os_datasync(fd: c_int) -> c_int {
|
|
|
|
libc::fsync(fd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn truncate(&self, size: u64) -> io::Result<()> {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 20:58:38 +00:00
|
|
|
let size: off64_t =
|
|
|
|
size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
|
|
|
|
cvt_r(|| unsafe { ftruncate64(self.as_raw_fd(), size) }).map(drop)
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0.read(buf)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.0.read_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
|
|
|
self.0.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:11:33 +00:00
|
|
|
pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
|
|
|
|
self.0.read_at(buf, offset)
|
|
|
|
}
|
|
|
|
|
2022-08-11 14:52:29 +00:00
|
|
|
pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
|
2022-05-13 14:06:36 +00:00
|
|
|
self.0.read_buf(cursor)
|
2021-05-09 05:04:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.write(buf)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.0.write_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
|
|
|
self.0.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:11:33 +00:00
|
|
|
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
|
|
|
|
self.0.write_at(buf, offset)
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn flush(&self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
let (whence, pos) = match pos {
|
2016-07-18 12:31:26 +00:00
|
|
|
// Casting to `i64` is fine, too large values will end up as
|
|
|
|
// negative which will cause an error in `lseek64`.
|
|
|
|
SeekFrom::Start(off) => (libc::SEEK_SET, off as i64),
|
|
|
|
SeekFrom::End(off) => (libc::SEEK_END, off),
|
|
|
|
SeekFrom::Current(off) => (libc::SEEK_CUR, off),
|
2015-02-03 05:39:14 +00:00
|
|
|
};
|
2022-04-07 11:07:46 +00:00
|
|
|
let n = cvt(unsafe { lseek64(self.as_raw_fd(), pos as off64_t, whence) })?;
|
2015-02-03 05:39:14 +00:00
|
|
|
Ok(n as u64)
|
|
|
|
}
|
|
|
|
|
2016-01-21 05:24:23 +00:00
|
|
|
pub fn duplicate(&self) -> io::Result<File> {
|
|
|
|
self.0.duplicate().map(File)
|
|
|
|
}
|
|
|
|
|
2016-11-19 17:53:54 +00:00
|
|
|
pub fn set_permissions(&self, perm: FilePermissions) -> io::Result<()> {
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt_r(|| unsafe { libc::fchmod(self.as_raw_fd(), perm.mode) })?;
|
2016-11-19 17:53:54 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-06-19 03:00:21 +00:00
|
|
|
|
|
|
|
pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
|
2022-09-10 10:29:01 +00:00
|
|
|
#[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "horizon")))]
|
|
|
|
let to_timespec = |time: Option<SystemTime>| {
|
|
|
|
match time {
|
|
|
|
Some(time) if let Some(ts) = time.t.to_timespec() => Ok(ts),
|
|
|
|
Some(time) if time > crate::sys::time::UNIX_EPOCH => Err(io::const_io_error!(io::ErrorKind::InvalidInput, "timestamp is too large to set as a file time")),
|
|
|
|
Some(_) => Err(io::const_io_error!(io::ErrorKind::InvalidInput, "timestamp is too small to set as a file time")),
|
|
|
|
None => Ok(libc::timespec { tv_sec: 0, tv_nsec: libc::UTIME_OMIT as _ }),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "horizon")))]
|
|
|
|
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
|
2022-06-19 03:00:21 +00:00
|
|
|
cfg_if::cfg_if! {
|
2022-08-07 17:30:05 +00:00
|
|
|
if #[cfg(any(target_os = "redox", target_os = "espidf", target_os = "horizon"))] {
|
2022-07-21 04:14:24 +00:00
|
|
|
// Redox doesn't appear to support `UTIME_OMIT`.
|
2022-08-07 17:30:05 +00:00
|
|
|
// ESP-IDF and HorizonOS do not support `futimens` at all and the behavior for those OS is therefore
|
2022-08-03 19:07:25 +00:00
|
|
|
// the same as for Redox.
|
2022-07-30 20:28:17 +00:00
|
|
|
drop(times);
|
|
|
|
Err(io::const_io_error!(
|
2022-07-21 04:14:24 +00:00
|
|
|
io::ErrorKind::Unsupported,
|
|
|
|
"setting file times not supported",
|
2022-07-30 20:28:17 +00:00
|
|
|
))
|
2022-07-21 04:14:24 +00:00
|
|
|
} else if #[cfg(any(target_os = "android", target_os = "macos"))] {
|
|
|
|
// futimens requires macOS 10.13, and Android API level 19
|
2022-06-19 03:00:21 +00:00
|
|
|
cvt(unsafe {
|
|
|
|
weak!(fn futimens(c_int, *const libc::timespec) -> c_int);
|
2022-07-12 01:23:34 +00:00
|
|
|
match futimens.get() {
|
2022-09-10 10:29:01 +00:00
|
|
|
Some(futimens) => futimens(self.as_raw_fd(), times.as_ptr()),
|
2022-07-12 01:23:34 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
None => {
|
|
|
|
fn ts_to_tv(ts: &libc::timespec) -> libc::timeval {
|
|
|
|
libc::timeval {
|
|
|
|
tv_sec: ts.tv_sec,
|
|
|
|
tv_usec: (ts.tv_nsec / 1000) as _
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 10:29:01 +00:00
|
|
|
let timevals = [ts_to_tv(×[0]), ts_to_tv(×[1])];
|
2022-06-19 03:00:21 +00:00
|
|
|
libc::futimes(self.as_raw_fd(), timevals.as_ptr())
|
2022-07-12 01:23:34 +00:00
|
|
|
}
|
|
|
|
// futimes requires even newer Android.
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
None => return Err(io::const_io_error!(
|
|
|
|
io::ErrorKind::Unsupported,
|
|
|
|
"setting file times requires Android API level >= 19",
|
|
|
|
)),
|
|
|
|
}
|
2022-06-19 03:00:21 +00:00
|
|
|
})?;
|
2022-07-30 20:28:17 +00:00
|
|
|
Ok(())
|
2022-06-19 03:00:21 +00:00
|
|
|
} else {
|
2022-09-10 10:29:01 +00:00
|
|
|
cvt(unsafe { libc::futimens(self.as_raw_fd(), times.as_ptr()) })?;
|
2022-07-30 20:28:17 +00:00
|
|
|
Ok(())
|
2022-06-19 03:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 00:29:35 +00:00
|
|
|
impl DirBuilder {
|
|
|
|
pub fn new() -> DirBuilder {
|
|
|
|
DirBuilder { mode: 0o777 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mkdir(&self, p: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) }).map(|_| ()))
|
2015-04-28 00:29:35 +00:00
|
|
|
}
|
|
|
|
|
std: Deprecate all std::os::*::raw types
This commit is an implementation of [RFC 1415][rfc] which deprecates all types
in the `std::os::*::raw` modules.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1415-trim-std-os.md
Many of the types in these modules don't actually have a canonical platform
representation, for example the definition of `stat` on 32-bit Linux will change
depending on whether C code is compiled with LFS support or not. Unfortunately
the current types in `std::os::*::raw` are billed as "compatible with C", which
in light of this means it isn't really possible.
To make matters worse, platforms like Android sometimes define these types as
*smaller* than the way they're actually represented in the `stat` structure
itself. This means that when methods like `DirEntry::ino` are called on Android
the result may be truncated as we're tied to returning a `ino_t` type, not the
underlying type.
The commit here incorporates two backwards-compatible components:
* Deprecate all `raw` types that aren't in `std::os::raw`
* Expand the `std::os::*::fs::MetadataExt` trait on all platforms for method
accessors of all fields. The fields now returned widened types which are the
same across platforms (consistency across platforms is not required, however,
it's just convenient).
and two also backwards-incompatible components:
* Change the definition of all `std::os::*::raw` type aliases to
correspond to the newly widened types that are being returned on each
platform.
* Change the definition of `std::os::*::raw::stat` on Linux to match the LFS
definitions rather than the standard ones.
The breaking changes here will specifically break code that assumes that `libc`
and `std` agree on the definition of `std::os::*::raw` types, or that the `std`
types are faithful representations of the types in C. An [audit] has been
performed of crates.io to determine the fallout which was determined two be
minimal, with the two found cases of breakage having been fixed now.
[audit]: https://github.com/rust-lang/rfcs/pull/1415#issuecomment-180645582
---
Ok, so after all that, we're finally able to support LFS on Linux! This commit
then simultaneously starts using `stat64` and friends on Linux to ensure that we
can open >4GB files on 32-bit Linux. Yay!
Closes #28978
Closes #30050
Closes #31549
2016-02-05 01:16:47 +00:00
|
|
|
pub fn set_mode(&mut self, mode: u32) {
|
|
|
|
self.mode = mode as mode_t;
|
2015-04-28 00:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
impl AsInner<FileDesc> for File {
|
|
|
|
fn as_inner(&self) -> &FileDesc {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInnerMut<FileDesc> for File {
|
|
|
|
fn as_inner_mut(&mut self) -> &mut FileDesc {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoInner<FileDesc> for File {
|
|
|
|
fn into_inner(self) -> FileDesc {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromInner<FileDesc> for File {
|
|
|
|
fn from_inner(file_desc: FileDesc) -> Self {
|
|
|
|
Self(file_desc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsFd for File {
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
self.0.as_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRawFd for File {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
self.0.as_raw_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoRawFd for File {
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
self.0.into_raw_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromRawFd for File {
|
|
|
|
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
|
|
|
Self(FromRawFd::from_raw_fd(raw_fd))
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:27:19 +00:00
|
|
|
impl fmt::Debug for File {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-09-24 08:08:56 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "solaris"
|
|
|
|
))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_path(fd: c_int) -> Option<PathBuf> {
|
|
|
|
let mut p = PathBuf::from("/proc/self/fd");
|
|
|
|
p.push(&fd.to_string());
|
|
|
|
readlink(&p).ok()
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:23:14 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn get_path(fd: c_int) -> Option<PathBuf> {
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
// FIXME: The use of PATH_MAX is generally not encouraged, but it
|
2017-03-12 18:13:35 +00:00
|
|
|
// is inevitable in this case because macOS defines `fcntl` with
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
// `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
|
|
|
|
// alternatives. If a better method is invented, it should be used
|
|
|
|
// instead.
|
2015-07-10 14:23:14 +00:00
|
|
|
let mut buf = vec![0; libc::PATH_MAX as usize];
|
|
|
|
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
|
|
|
|
if n == -1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let l = buf.iter().position(|&c| c == 0).unwrap();
|
|
|
|
buf.truncate(l as usize);
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
buf.shrink_to_fit();
|
2015-07-10 14:23:14 +00:00
|
|
|
Some(PathBuf::from(OsString::from_vec(buf)))
|
|
|
|
}
|
|
|
|
|
2022-06-17 19:31:59 +00:00
|
|
|
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
|
|
|
|
fn get_path(fd: c_int) -> Option<PathBuf> {
|
|
|
|
let info = Box::<libc::kinfo_file>::new_zeroed();
|
|
|
|
let mut info = unsafe { info.assume_init() };
|
|
|
|
info.kf_structsize = mem::size_of::<libc::kinfo_file>() as libc::c_int;
|
|
|
|
let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) };
|
|
|
|
if n == -1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() };
|
|
|
|
Some(PathBuf::from(OsString::from_vec(buf)))
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:38:25 +00:00
|
|
|
#[cfg(target_os = "vxworks")]
|
|
|
|
fn get_path(fd: c_int) -> Option<PathBuf> {
|
|
|
|
let mut buf = vec![0; libc::PATH_MAX as usize];
|
|
|
|
let n = unsafe { libc::ioctl(fd, libc::FIOGETNAME, buf.as_ptr()) };
|
|
|
|
if n == -1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let l = buf.iter().position(|&c| c == 0).unwrap();
|
|
|
|
buf.truncate(l as usize);
|
|
|
|
Some(PathBuf::from(OsString::from_vec(buf)))
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:49:48 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "vxworks",
|
2022-06-17 19:31:59 +00:00
|
|
|
all(target_os = "freebsd", target_arch = "x86_64"),
|
2022-09-24 08:08:56 +00:00
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "solaris"
|
2021-07-29 16:49:48 +00:00
|
|
|
)))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_path(_fd: c_int) -> Option<PathBuf> {
|
|
|
|
// FIXME(#24570): implement this for other Unix platforms
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-08-28 09:26:59 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "vxworks"
|
|
|
|
))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_mode(fd: c_int) -> Option<(bool, bool)> {
|
|
|
|
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
|
|
|
|
if mode == -1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
match mode & libc::O_ACCMODE {
|
|
|
|
libc::O_RDONLY => Some((true, false)),
|
|
|
|
libc::O_RDWR => Some((true, true)),
|
|
|
|
libc::O_WRONLY => Some((false, true)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 09:26:59 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "vxworks"
|
|
|
|
)))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_mode(_fd: c_int) -> Option<(bool, bool)> {
|
|
|
|
// FIXME(#24570): implement this for other Unix platforms
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
let fd = self.as_raw_fd();
|
2015-05-17 20:17:26 +00:00
|
|
|
let mut b = f.debug_struct("File");
|
|
|
|
b.field("fd", &fd);
|
2015-04-19 09:27:19 +00:00
|
|
|
if let Some(path) = get_path(fd) {
|
2015-05-17 20:17:26 +00:00
|
|
|
b.field("path", &path);
|
2015-04-19 09:27:19 +00:00
|
|
|
}
|
|
|
|
if let Some((read, write)) = get_mode(fd) {
|
2015-05-17 20:17:26 +00:00
|
|
|
b.field("read", &read).field("write", &write);
|
2015-04-19 09:27:19 +00:00
|
|
|
}
|
|
|
|
b.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
pub fn readdir(path: &Path) -> io::Result<ReadDir> {
|
|
|
|
let ptr = run_path_with_cstr(path, |p| unsafe { Ok(libc::opendir(p.as_ptr())) })?;
|
|
|
|
if ptr.is_null() {
|
|
|
|
Err(Error::last_os_error())
|
|
|
|
} else {
|
|
|
|
let root = path.to_path_buf();
|
|
|
|
let inner = InnerReadDir { dirp: Dir(ptr), root };
|
|
|
|
Ok(ReadDir {
|
|
|
|
inner: Arc::new(inner),
|
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
)))]
|
|
|
|
end_of_stream: false,
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unlink(p: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| cvt(unsafe { libc::unlink(p.as_ptr()) }).map(|_| ()))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(old, |old| {
|
|
|
|
run_path_with_cstr(new, |new| {
|
|
|
|
cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) }).map(|_| ())
|
|
|
|
})
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) }).map(|_| ()))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rmdir(p: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| cvt(unsafe { libc::rmdir(p.as_ptr()) }).map(|_| ()))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readlink(p: &Path) -> io::Result<PathBuf> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |c_path| {
|
|
|
|
let p = c_path.as_ptr();
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
let mut buf = Vec::with_capacity(256);
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
loop {
|
|
|
|
let buf_read =
|
|
|
|
cvt(unsafe { libc::readlink(p, buf.as_mut_ptr() as *mut _, buf.capacity()) })?
|
|
|
|
as usize;
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
unsafe {
|
|
|
|
buf.set_len(buf_read);
|
|
|
|
}
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
if buf_read != buf.capacity() {
|
|
|
|
buf.shrink_to_fit();
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
return Ok(PathBuf::from(OsString::from_vec(buf)));
|
|
|
|
}
|
Reduce the reliance on `PATH_MAX`
- Rewrite `std::sys::fs::readlink` not to rely on `PATH_MAX`
It currently has the following problems:
1. It uses `_PC_NAME_MAX` to query the maximum length of a file path in
the underlying system. However, the meaning of the constant is the
maximum length of *a path component*, not a full path. The correct
constant should be `_PC_PATH_MAX`.
2. `pathconf` *may* fail if the referred file does not exist. This can
be problematic if the file which the symbolic link points to does not
exist, but the link itself does exist. In this case, the current
implementation resorts to the hard-coded value of `1024`, which is not
ideal.
3. There may exist a platform where there is no limit on file path
lengths in general. That's the reaon why GNU Hurd doesn't define
`PATH_MAX` at all, in addition to having `pathconf` always returning
`-1`. In these platforms, the content of the symbolic link can be
silently truncated if the length exceeds the hard-coded limit mentioned
above.
4. The value obtained by `pathconf` may be outdated at the point of
actually calling `readlink`. This is inherently racy.
This commit introduces a loop that gradually increases the length of the
buffer passed to `readlink`, eliminating the need of `pathconf`.
- Remove the arbitrary memory limit of `std::sys::fs::realpath`
As per POSIX 2013, `realpath` will return a malloc'ed buffer if the
second argument is a null pointer.[1]
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
- Comment on functions that are still using `PATH_MAX`
There are some functions that only work in terms of `PATH_MAX`, such as
`F_GETPATH` in OS X. Comments on them for posterity.
2015-08-19 04:11:40 +00:00
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
// Trigger the internal buffer resizing logic of `Vec` by requiring
|
|
|
|
// more space than the current capacity. The length is guaranteed to be
|
|
|
|
// the same as the capacity due to the if statement above.
|
|
|
|
buf.reserve(1);
|
|
|
|
}
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 22:40:08 +00:00
|
|
|
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(original, |original| {
|
|
|
|
run_path_with_cstr(link, |link| {
|
|
|
|
cvt(unsafe { libc::symlink(original.as_ptr(), link.as_ptr()) }).map(|_| ())
|
|
|
|
})
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 22:40:08 +00:00
|
|
|
pub fn link(original: &Path, link: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(original, |original| {
|
|
|
|
run_path_with_cstr(link, |link| {
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(any(target_os = "vxworks", target_os = "redox", target_os = "android", target_os = "espidf", target_os = "horizon"))] {
|
|
|
|
// VxWorks, Redox and ESP-IDF lack `linkat`, so use `link` instead. POSIX leaves
|
|
|
|
// it implementation-defined whether `link` follows symlinks, so rely on the
|
|
|
|
// `symlink_hard_link` test in library/std/src/fs/tests.rs to check the behavior.
|
|
|
|
// Android has `linkat` on newer versions, but we happen to know `link`
|
|
|
|
// always has the correct behavior, so it's here as well.
|
|
|
|
cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?;
|
|
|
|
} else if #[cfg(target_os = "macos")] {
|
|
|
|
// On MacOS, older versions (<=10.9) lack support for linkat while newer
|
|
|
|
// versions have it. We want to use linkat if it is available, so we use weak!
|
|
|
|
// to check. `linkat` is preferable to `link` because it gives us a flag to
|
|
|
|
// specify how symlinks should be handled. We pass 0 as the flags argument,
|
|
|
|
// meaning it shouldn't follow symlinks.
|
|
|
|
weak!(fn linkat(c_int, *const c_char, c_int, *const c_char, c_int) -> c_int);
|
|
|
|
|
|
|
|
if let Some(f) = linkat.get() {
|
|
|
|
cvt(unsafe { f(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?;
|
|
|
|
} else {
|
|
|
|
cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Where we can, use `linkat` instead of `link`; see the comment above
|
|
|
|
// this one for details on why.
|
|
|
|
cvt(unsafe { libc::linkat(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stat(p: &Path) -> io::Result<FileAttr> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| {
|
|
|
|
cfg_has_statx! {
|
|
|
|
if let Some(ret) = unsafe { try_statx(
|
|
|
|
libc::AT_FDCWD,
|
|
|
|
p.as_ptr(),
|
|
|
|
libc::AT_STATX_SYNC_AS_STAT,
|
|
|
|
libc::STATX_ALL,
|
|
|
|
) } {
|
|
|
|
return ret;
|
|
|
|
}
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
let mut stat: stat64 = unsafe { mem::zeroed() };
|
|
|
|
cvt(unsafe { stat64(p.as_ptr(), &mut stat) })?;
|
|
|
|
Ok(FileAttr::from_stat64(stat))
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lstat(p: &Path) -> io::Result<FileAttr> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| {
|
|
|
|
cfg_has_statx! {
|
|
|
|
if let Some(ret) = unsafe { try_statx(
|
|
|
|
libc::AT_FDCWD,
|
|
|
|
p.as_ptr(),
|
|
|
|
libc::AT_SYMLINK_NOFOLLOW | libc::AT_STATX_SYNC_AS_STAT,
|
|
|
|
libc::STATX_ALL,
|
|
|
|
) } {
|
|
|
|
return ret;
|
|
|
|
}
|
2019-10-04 08:17:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 08:40:43 +00:00
|
|
|
let mut stat: stat64 = unsafe { mem::zeroed() };
|
|
|
|
cvt(unsafe { lstat64(p.as_ptr(), &mut stat) })?;
|
|
|
|
Ok(FileAttr::from_stat64(stat))
|
|
|
|
})
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
|
2022-09-22 08:40:43 +00:00
|
|
|
let r = run_path_with_cstr(p, |path| unsafe {
|
|
|
|
Ok(libc::realpath(path.as_ptr(), ptr::null_mut()))
|
|
|
|
})?;
|
|
|
|
if r.is_null() {
|
|
|
|
return Err(io::Error::last_os_error());
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
2022-09-22 08:40:43 +00:00
|
|
|
Ok(PathBuf::from(OsString::from_vec(unsafe {
|
|
|
|
let buf = CStr::from_ptr(r).to_bytes().to_vec();
|
|
|
|
libc::free(r as *mut _);
|
|
|
|
buf
|
|
|
|
})))
|
2015-04-16 06:21:13 +00:00
|
|
|
}
|
2015-07-10 08:54:00 +00:00
|
|
|
|
2019-04-11 15:02:13 +00:00
|
|
|
fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
|
|
|
|
use crate::fs::File;
|
2021-03-26 17:21:35 +00:00
|
|
|
use crate::sys_common::fs::NOT_FILE_ERROR;
|
2019-04-11 15:02:13 +00:00
|
|
|
|
|
|
|
let reader = File::open(from)?;
|
|
|
|
let metadata = reader.metadata()?;
|
|
|
|
if !metadata.is_file() {
|
2021-03-26 17:21:35 +00:00
|
|
|
return Err(NOT_FILE_ERROR);
|
2019-04-11 15:02:13 +00:00
|
|
|
}
|
|
|
|
Ok((reader, metadata))
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
fn open_to_and_set_permissions(
|
|
|
|
to: &Path,
|
|
|
|
reader_metadata: crate::fs::Metadata,
|
|
|
|
) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
|
|
|
|
use crate::fs::OpenOptions;
|
|
|
|
let writer = OpenOptions::new().open(to)?;
|
|
|
|
let writer_metadata = writer.metadata()?;
|
|
|
|
Ok((writer, writer_metadata))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "espidf"))]
|
2019-04-11 15:02:13 +00:00
|
|
|
fn open_to_and_set_permissions(
|
2019-02-28 09:25:54 +00:00
|
|
|
to: &Path,
|
2019-04-11 15:02:13 +00:00
|
|
|
reader_metadata: crate::fs::Metadata,
|
|
|
|
) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
|
|
|
|
use crate::fs::OpenOptions;
|
2019-02-28 09:25:54 +00:00
|
|
|
use crate::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
|
|
|
|
2019-04-11 15:02:13 +00:00
|
|
|
let perm = reader_metadata.permissions();
|
2019-02-28 09:25:54 +00:00
|
|
|
let writer = OpenOptions::new()
|
|
|
|
// create the file with the correct mode right away
|
|
|
|
.mode(perm.mode())
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(to)?;
|
|
|
|
let writer_metadata = writer.metadata()?;
|
|
|
|
if writer_metadata.is_file() {
|
|
|
|
// Set the correct file permissions, in case the file already existed.
|
|
|
|
// Don't set the permissions on already existing non-files like
|
|
|
|
// pipes/FIFOs or device nodes.
|
|
|
|
writer.set_permissions(perm)?;
|
|
|
|
}
|
2019-04-11 15:02:13 +00:00
|
|
|
Ok((writer, writer_metadata))
|
2019-02-28 09:25:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 19:47:17 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "macos",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "ios",
|
|
|
|
target_os = "watchos",
|
2019-03-03 19:47:17 +00:00
|
|
|
)))]
|
2015-07-10 08:54:00 +00:00
|
|
|
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
|
2019-04-11 15:02:13 +00:00
|
|
|
let (mut reader, reader_metadata) = open_from(from)?;
|
|
|
|
let (mut writer, _) = open_to_and_set_permissions(to, reader_metadata)?;
|
2015-07-10 08:54:00 +00:00
|
|
|
|
2019-02-28 09:25:54 +00:00
|
|
|
io::copy(&mut reader, &mut writer)
|
2015-07-10 08:54:00 +00:00
|
|
|
}
|
2018-05-15 13:25:09 +00:00
|
|
|
|
2018-05-16 08:17:06 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2018-05-15 13:25:09 +00:00
|
|
|
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
|
2019-04-11 15:02:13 +00:00
|
|
|
let (mut reader, reader_metadata) = open_from(from)?;
|
2020-08-14 20:39:04 +00:00
|
|
|
let max_len = u64::MAX;
|
2019-04-11 15:02:13 +00:00
|
|
|
let (mut writer, _) = open_to_and_set_permissions(to, reader_metadata)?;
|
2018-05-16 08:17:06 +00:00
|
|
|
|
2020-10-15 01:07:57 +00:00
|
|
|
use super::kernel_copy::{copy_regular_files, CopyResult};
|
|
|
|
|
2020-10-06 23:01:12 +00:00
|
|
|
match copy_regular_files(reader.as_raw_fd(), writer.as_raw_fd(), max_len) {
|
2020-12-02 22:35:40 +00:00
|
|
|
CopyResult::Ended(bytes) => Ok(bytes),
|
|
|
|
CopyResult::Error(e, _) => Err(e),
|
2020-11-12 22:39:49 +00:00
|
|
|
CopyResult::Fallback(written) => match io::copy::generic_copy(&mut reader, &mut writer) {
|
|
|
|
Ok(bytes) => Ok(bytes + written),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
},
|
2018-05-15 13:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 19:47:17 +00:00
|
|
|
|
2022-03-23 16:05:01 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
|
2019-03-03 19:47:17 +00:00
|
|
|
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
|
2019-04-11 15:02:13 +00:00
|
|
|
use crate::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
2019-03-03 19:47:17 +00:00
|
|
|
const COPYFILE_ACL: u32 = 1 << 0;
|
|
|
|
const COPYFILE_STAT: u32 = 1 << 1;
|
|
|
|
const COPYFILE_XATTR: u32 = 1 << 2;
|
|
|
|
const COPYFILE_DATA: u32 = 1 << 3;
|
|
|
|
|
|
|
|
const COPYFILE_SECURITY: u32 = COPYFILE_STAT | COPYFILE_ACL;
|
|
|
|
const COPYFILE_METADATA: u32 = COPYFILE_SECURITY | COPYFILE_XATTR;
|
|
|
|
const COPYFILE_ALL: u32 = COPYFILE_METADATA | COPYFILE_DATA;
|
|
|
|
|
|
|
|
const COPYFILE_STATE_COPIED: u32 = 8;
|
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
type copyfile_state_t = *mut libc::c_void;
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
type copyfile_flags_t = u32;
|
|
|
|
|
|
|
|
extern "C" {
|
2019-02-28 09:25:54 +00:00
|
|
|
fn fcopyfile(
|
|
|
|
from: libc::c_int,
|
|
|
|
to: libc::c_int,
|
2019-03-03 19:47:17 +00:00
|
|
|
state: copyfile_state_t,
|
|
|
|
flags: copyfile_flags_t,
|
|
|
|
) -> libc::c_int;
|
|
|
|
fn copyfile_state_alloc() -> copyfile_state_t;
|
|
|
|
fn copyfile_state_free(state: copyfile_state_t) -> libc::c_int;
|
|
|
|
fn copyfile_state_get(
|
|
|
|
state: copyfile_state_t,
|
|
|
|
flag: u32,
|
|
|
|
dst: *mut libc::c_void,
|
|
|
|
) -> libc::c_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FreeOnDrop(copyfile_state_t);
|
|
|
|
impl Drop for FreeOnDrop {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// The code below ensures that `FreeOnDrop` is never a null pointer
|
|
|
|
unsafe {
|
|
|
|
// `copyfile_state_free` returns -1 if the `to` or `from` files
|
2019-11-27 03:19:54 +00:00
|
|
|
// cannot be closed. However, this is not considered this an
|
2019-03-03 19:47:17 +00:00
|
|
|
// error.
|
|
|
|
copyfile_state_free(self.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:02:13 +00:00
|
|
|
// MacOS prior to 10.12 don't support `fclonefileat`
|
|
|
|
// We store the availability in a global to avoid unnecessary syscalls
|
|
|
|
static HAS_FCLONEFILEAT: AtomicBool = AtomicBool::new(true);
|
|
|
|
syscall! {
|
|
|
|
fn fclonefileat(
|
|
|
|
srcfd: libc::c_int,
|
|
|
|
dst_dirfd: libc::c_int,
|
2021-06-03 05:53:03 +00:00
|
|
|
dst: *const c_char,
|
2019-04-11 15:02:13 +00:00
|
|
|
flags: libc::c_int
|
|
|
|
) -> libc::c_int
|
|
|
|
}
|
|
|
|
|
|
|
|
let (reader, reader_metadata) = open_from(from)?;
|
|
|
|
|
|
|
|
// Opportunistically attempt to create a copy-on-write clone of `from`
|
|
|
|
// using `fclonefileat`.
|
|
|
|
if HAS_FCLONEFILEAT.load(Ordering::Relaxed) {
|
2022-09-22 08:40:43 +00:00
|
|
|
let clonefile_result = run_path_with_cstr(to, |to| {
|
|
|
|
cvt(unsafe { fclonefileat(reader.as_raw_fd(), libc::AT_FDCWD, to.as_ptr(), 0) })
|
|
|
|
});
|
2019-04-11 15:02:13 +00:00
|
|
|
match clonefile_result {
|
|
|
|
Ok(_) => return Ok(reader_metadata.len()),
|
|
|
|
Err(err) => match err.raw_os_error() {
|
|
|
|
// `fclonefileat` will fail on non-APFS volumes, if the
|
|
|
|
// destination already exists, or if the source and destination
|
|
|
|
// are on different devices. In all these cases `fcopyfile`
|
|
|
|
// should succeed.
|
|
|
|
Some(libc::ENOTSUP) | Some(libc::EEXIST) | Some(libc::EXDEV) => (),
|
|
|
|
Some(libc::ENOSYS) => HAS_FCLONEFILEAT.store(false, Ordering::Relaxed),
|
|
|
|
_ => return Err(err),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to using `fcopyfile` if `fclonefileat` does not succeed.
|
|
|
|
let (writer, writer_metadata) = open_to_and_set_permissions(to, reader_metadata)?;
|
2019-03-03 19:47:17 +00:00
|
|
|
|
|
|
|
// We ensure that `FreeOnDrop` never contains a null pointer so it is
|
|
|
|
// always safe to call `copyfile_state_free`
|
|
|
|
let state = unsafe {
|
|
|
|
let state = copyfile_state_alloc();
|
|
|
|
if state.is_null() {
|
|
|
|
return Err(crate::io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
FreeOnDrop(state)
|
|
|
|
};
|
|
|
|
|
2019-02-28 09:25:54 +00:00
|
|
|
let flags = if writer_metadata.is_file() { COPYFILE_ALL } else { COPYFILE_DATA };
|
|
|
|
|
|
|
|
cvt(unsafe { fcopyfile(reader.as_raw_fd(), writer.as_raw_fd(), state.0, flags) })?;
|
2019-03-03 19:47:17 +00:00
|
|
|
|
|
|
|
let mut bytes_copied: libc::off_t = 0;
|
|
|
|
cvt(unsafe {
|
|
|
|
copyfile_state_get(
|
|
|
|
state.0,
|
|
|
|
COPYFILE_STATE_COPIED,
|
|
|
|
&mut bytes_copied as *mut libc::off_t as *mut libc::c_void,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(bytes_copied as u64)
|
|
|
|
}
|
2021-04-29 20:47:28 +00:00
|
|
|
|
2021-09-15 01:29:38 +00:00
|
|
|
pub fn chown(path: &Path, uid: u32, gid: u32) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(path, |path| {
|
|
|
|
cvt(unsafe { libc::chown(path.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) })
|
|
|
|
.map(|_| ())
|
|
|
|
})
|
2021-09-15 01:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fchown(fd: c_int, uid: u32, gid: u32) -> io::Result<()> {
|
|
|
|
cvt(unsafe { libc::fchown(fd, uid as libc::uid_t, gid as libc::gid_t) })?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lchown(path: &Path, uid: u32, gid: u32) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(path, |path| {
|
|
|
|
cvt(unsafe { libc::lchown(path.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) })
|
|
|
|
.map(|_| ())
|
|
|
|
})
|
2021-09-15 01:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 23:37:57 +00:00
|
|
|
#[cfg(not(any(target_os = "fuchsia", target_os = "vxworks")))]
|
2021-04-29 20:47:28 +00:00
|
|
|
pub fn chroot(dir: &Path) -> io::Result<()> {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(dir, |dir| cvt(unsafe { libc::chroot(dir.as_ptr()) }).map(|_| ()))
|
2021-04-29 20:47:28 +00:00
|
|
|
}
|
2021-12-18 14:30:30 +00:00
|
|
|
|
|
|
|
pub use remove_dir_impl::remove_dir_all;
|
|
|
|
|
2021-10-19 15:59:59 +00:00
|
|
|
// Fallback for REDOX, ESP-ID, Horizon, and Miri
|
|
|
|
#[cfg(any(target_os = "redox", target_os = "espidf", target_os = "horizon", miri))]
|
2021-12-18 14:30:30 +00:00
|
|
|
mod remove_dir_impl {
|
|
|
|
pub use crate::sys_common::fs::remove_dir_all;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modern implementation using openat(), unlinkat() and fdopendir()
|
2021-10-19 15:59:59 +00:00
|
|
|
#[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "horizon", miri)))]
|
2021-12-18 14:30:30 +00:00
|
|
|
mod remove_dir_impl {
|
2022-09-22 08:40:43 +00:00
|
|
|
use super::{lstat, Dir, DirEntry, InnerReadDir, ReadDir};
|
2021-12-18 14:30:30 +00:00
|
|
|
use crate::ffi::CStr;
|
|
|
|
use crate::io;
|
|
|
|
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
|
|
|
|
use crate::os::unix::prelude::{OwnedFd, RawFd};
|
|
|
|
use crate::path::{Path, PathBuf};
|
|
|
|
use crate::sync::Arc;
|
2022-09-22 08:40:43 +00:00
|
|
|
use crate::sys::common::small_c_string::run_path_with_cstr;
|
2021-12-18 14:30:30 +00:00
|
|
|
use crate::sys::{cvt, cvt_r};
|
2022-02-28 11:30:23 +00:00
|
|
|
|
2022-03-27 18:18:01 +00:00
|
|
|
#[cfg(not(all(target_os = "macos", not(target_arch = "aarch64")),))]
|
2021-12-18 14:30:30 +00:00
|
|
|
use libc::{fdopendir, openat, unlinkat};
|
2022-03-27 18:18:01 +00:00
|
|
|
#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
|
2022-02-28 11:30:23 +00:00
|
|
|
use macos_weak::{fdopendir, openat, unlinkat};
|
|
|
|
|
2022-03-27 18:18:01 +00:00
|
|
|
#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
|
2022-02-28 11:30:23 +00:00
|
|
|
mod macos_weak {
|
|
|
|
use crate::sys::weak::weak;
|
|
|
|
use libc::{c_char, c_int, DIR};
|
|
|
|
|
|
|
|
fn get_openat_fn() -> Option<unsafe extern "C" fn(c_int, *const c_char, c_int) -> c_int> {
|
|
|
|
weak!(fn openat(c_int, *const c_char, c_int) -> c_int);
|
|
|
|
openat.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_openat() -> bool {
|
|
|
|
get_openat_fn().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn openat(dirfd: c_int, pathname: *const c_char, flags: c_int) -> c_int {
|
|
|
|
get_openat_fn().map(|openat| openat(dirfd, pathname, flags)).unwrap_or_else(|| {
|
|
|
|
crate::sys::unix::os::set_errno(libc::ENOSYS);
|
|
|
|
-1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn fdopendir(fd: c_int) -> *mut DIR {
|
2022-03-28 19:44:19 +00:00
|
|
|
#[cfg(all(target_os = "macos", target_arch = "x86"))]
|
|
|
|
weak!(fn fdopendir(c_int) -> *mut DIR, "fdopendir$INODE64$UNIX2003");
|
|
|
|
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
|
2022-02-28 11:30:23 +00:00
|
|
|
weak!(fn fdopendir(c_int) -> *mut DIR, "fdopendir$INODE64");
|
|
|
|
fdopendir.get().map(|fdopendir| fdopendir(fd)).unwrap_or_else(|| {
|
|
|
|
crate::sys::unix::os::set_errno(libc::ENOSYS);
|
|
|
|
crate::ptr::null_mut()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn unlinkat(dirfd: c_int, pathname: *const c_char, flags: c_int) -> c_int {
|
|
|
|
weak!(fn unlinkat(c_int, *const c_char, c_int) -> c_int);
|
|
|
|
unlinkat.get().map(|unlinkat| unlinkat(dirfd, pathname, flags)).unwrap_or_else(|| {
|
|
|
|
crate::sys::unix::os::set_errno(libc::ENOSYS);
|
|
|
|
-1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 14:30:30 +00:00
|
|
|
|
|
|
|
pub fn openat_nofollow_dironly(parent_fd: Option<RawFd>, p: &CStr) -> io::Result<OwnedFd> {
|
|
|
|
let fd = cvt_r(|| unsafe {
|
|
|
|
openat(
|
|
|
|
parent_fd.unwrap_or(libc::AT_FDCWD),
|
|
|
|
p.as_ptr(),
|
|
|
|
libc::O_CLOEXEC | libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_DIRECTORY,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fdreaddir(dir_fd: OwnedFd) -> io::Result<(ReadDir, RawFd)> {
|
|
|
|
let ptr = unsafe { fdopendir(dir_fd.as_raw_fd()) };
|
|
|
|
if ptr.is_null() {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
let dirp = Dir(ptr);
|
|
|
|
// file descriptor is automatically closed by libc::closedir() now, so give up ownership
|
|
|
|
let new_parent_fd = dir_fd.into_raw_fd();
|
|
|
|
// a valid root is not needed because we do not call any functions involving the full path
|
|
|
|
// of the DirEntrys.
|
|
|
|
let dummy_root = PathBuf::new();
|
|
|
|
Ok((
|
|
|
|
ReadDir {
|
|
|
|
inner: Arc::new(InnerReadDir { dirp, root: dummy_root }),
|
|
|
|
#[cfg(not(any(
|
2022-01-14 19:19:09 +00:00
|
|
|
target_os = "android",
|
2022-01-11 19:09:52 +00:00
|
|
|
target_os = "linux",
|
2021-12-18 14:30:30 +00:00
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
)))]
|
|
|
|
end_of_stream: false,
|
|
|
|
},
|
|
|
|
new_parent_fd,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "haiku",
|
2022-01-20 11:43:54 +00:00
|
|
|
target_os = "vxworks",
|
2021-12-18 14:30:30 +00:00
|
|
|
))]
|
|
|
|
fn is_dir(_ent: &DirEntry) -> Option<bool> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "haiku",
|
2022-01-20 11:43:54 +00:00
|
|
|
target_os = "vxworks",
|
2021-12-18 14:30:30 +00:00
|
|
|
)))]
|
|
|
|
fn is_dir(ent: &DirEntry) -> Option<bool> {
|
|
|
|
match ent.entry.d_type {
|
|
|
|
libc::DT_UNKNOWN => None,
|
|
|
|
libc::DT_DIR => Some(true),
|
|
|
|
_ => Some(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 02:51:53 +00:00
|
|
|
fn remove_dir_all_recursive(parent_fd: Option<RawFd>, path: &CStr) -> io::Result<()> {
|
2022-02-28 09:34:06 +00:00
|
|
|
// try opening as directory
|
2022-03-08 02:51:53 +00:00
|
|
|
let fd = match openat_nofollow_dironly(parent_fd, &path) {
|
2022-04-20 00:46:50 +00:00
|
|
|
Err(err) if matches!(err.raw_os_error(), Some(libc::ENOTDIR | libc::ELOOP)) => {
|
2022-02-28 09:34:06 +00:00
|
|
|
// not a directory - don't traverse further
|
2022-04-20 00:46:50 +00:00
|
|
|
// (for symlinks, older Linux kernels may return ELOOP instead of ENOTDIR)
|
2022-02-28 09:34:06 +00:00
|
|
|
return match parent_fd {
|
|
|
|
// unlink...
|
|
|
|
Some(parent_fd) => {
|
2022-03-08 02:51:53 +00:00
|
|
|
cvt(unsafe { unlinkat(parent_fd, path.as_ptr(), 0) }).map(drop)
|
2022-02-28 09:34:06 +00:00
|
|
|
}
|
|
|
|
// ...unless this was supposed to be the deletion root directory
|
|
|
|
None => Err(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
result => result?,
|
|
|
|
};
|
2021-12-18 14:30:30 +00:00
|
|
|
|
|
|
|
// open the directory passing ownership of the fd
|
|
|
|
let (dir, fd) = fdreaddir(fd)?;
|
|
|
|
for child in dir {
|
|
|
|
let child = child?;
|
2022-03-08 02:51:53 +00:00
|
|
|
let child_name = child.name_cstr();
|
2021-12-18 14:30:30 +00:00
|
|
|
match is_dir(&child) {
|
|
|
|
Some(true) => {
|
2022-03-08 02:51:53 +00:00
|
|
|
remove_dir_all_recursive(Some(fd), child_name)?;
|
2021-12-18 14:30:30 +00:00
|
|
|
}
|
|
|
|
Some(false) => {
|
2022-03-08 02:51:53 +00:00
|
|
|
cvt(unsafe { unlinkat(fd, child_name.as_ptr(), 0) })?;
|
2021-12-18 14:30:30 +00:00
|
|
|
}
|
2022-02-28 09:34:06 +00:00
|
|
|
None => {
|
|
|
|
// POSIX specifies that calling unlink()/unlinkat(..., 0) on a directory can succeed
|
|
|
|
// if the process has the appropriate privileges. This however can causing orphaned
|
|
|
|
// directories requiring an fsck e.g. on Solaris and Illumos. So we try recursing
|
|
|
|
// into it first instead of trying to unlink() it.
|
2022-03-08 02:51:53 +00:00
|
|
|
remove_dir_all_recursive(Some(fd), child_name)?;
|
2022-02-28 09:34:06 +00:00
|
|
|
}
|
2021-12-18 14:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlink the directory after removing its contents
|
|
|
|
cvt(unsafe {
|
2022-03-08 02:51:53 +00:00
|
|
|
unlinkat(parent_fd.unwrap_or(libc::AT_FDCWD), path.as_ptr(), libc::AT_REMOVEDIR)
|
2021-12-18 14:30:30 +00:00
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-28 11:30:23 +00:00
|
|
|
fn remove_dir_all_modern(p: &Path) -> io::Result<()> {
|
2021-12-18 14:30:30 +00:00
|
|
|
// We cannot just call remove_dir_all_recursive() here because that would not delete a passed
|
|
|
|
// symlink. No need to worry about races, because remove_dir_all_recursive() does not recurse
|
|
|
|
// into symlinks.
|
|
|
|
let attr = lstat(p)?;
|
|
|
|
if attr.file_type().is_symlink() {
|
|
|
|
crate::fs::remove_file(p)
|
|
|
|
} else {
|
2022-09-22 08:40:43 +00:00
|
|
|
run_path_with_cstr(p, |p| remove_dir_all_recursive(None, &p))
|
2021-12-18 14:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 11:30:23 +00:00
|
|
|
|
2022-03-27 18:18:01 +00:00
|
|
|
#[cfg(not(all(target_os = "macos", not(target_arch = "aarch64"))))]
|
2022-02-28 11:30:23 +00:00
|
|
|
pub fn remove_dir_all(p: &Path) -> io::Result<()> {
|
|
|
|
remove_dir_all_modern(p)
|
|
|
|
}
|
|
|
|
|
2022-03-27 18:18:01 +00:00
|
|
|
#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
|
2022-02-28 11:30:23 +00:00
|
|
|
pub fn remove_dir_all(p: &Path) -> io::Result<()> {
|
|
|
|
if macos_weak::has_openat() {
|
|
|
|
// openat() is available with macOS 10.10+, just like unlinkat() and fdopendir()
|
|
|
|
remove_dir_all_modern(p)
|
|
|
|
} else {
|
|
|
|
// fall back to classic implementation
|
|
|
|
crate::sys_common::fs::remove_dir_all(p)
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 14:30:30 +00:00
|
|
|
}
|