diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-10-13 02:07:31 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-13 02:07:31 +0000 |
commit | 0d6cc1844da238ca35989b95f80e33980b56db02 (patch) | |
tree | 79ee845303b605b239aec4f723d9c6b91de038ac | |
parent | b45618134e004992e3263543671e9f89ef189c06 (diff) | |
parent | 04e409b376615080e1d910e302b28b144dcbff71 (diff) | |
download | nix-0d6cc1844da238ca35989b95f80e33980b56db02.zip |
Merge #1842
1842: add eaccess on freebsd, dragonfly and linux r=rtzoeller a=SteveLauC
#### man pages
* [FreeBSD](https://www.freebsd.org/cgi/man.cgi?query=eaccess&sektion=2&n=1)
* [DragonFly](https://man.dragonflybsd.org/?command=access§ion=2)
* [Linux](https://man7.org/linux/man-pages/man3/euidaccess.3.html)
#### difference between `eaccess` and `access/faccessat`
IMHO, `eaccess` uses effective identifiers to perform the permission check while `access/faccessat` use real IDs.
Fixes #1373
Co-authored-by: Steve Lau <stevelauc@outlook.com>
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/unistd.rs | 21 | ||||
-rw-r--r-- | test/test_unistd.rs | 37 |
3 files changed, 56 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d45b963..b1f74a89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ([#1833](https://github.com/nix-rust/nix/pull/1833)) - Added `faccessat(2)` on illumos ([#1841](https://github.com/nix-rust/nix/pull/1841)) +- Added `eaccess()` on FreeBSD, DragonFly and Linux (glibc and musl). + ([#1842](https://github.com/nix-rust/nix/pull/1842)) ### Changed diff --git a/src/unistd.rs b/src/unistd.rs index 2197d2bc..ca07b34a 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -2931,6 +2931,27 @@ pub fn faccessat<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: Acce })?; Errno::result(res).map(drop) } + +/// Checks the file named by `path` for accessibility according to the flags given +/// by `mode` using effective UID, effective GID and supplementary group lists. +/// +/// # References +/// +/// * [FreeBSD man page](https://www.freebsd.org/cgi/man.cgi?query=eaccess&sektion=2&n=1) +/// * [Linux man page](https://man7.org/linux/man-pages/man3/euidaccess.3.html) +#[cfg(any( + all(target_os = "linux", not(target_env = "uclibc")), + target_os = "freebsd", + target_os = "dragonfly" +))] +pub fn eaccess<P: ?Sized + NixPath>(path: &P, mode: AccessFlags) -> Result<()> { + let res = path.with_nix_path(|cstr| { + unsafe { + libc::eaccess(cstr.as_ptr(), mode.bits) + } + })?; + Errno::result(res).map(drop) +} } feature! { diff --git a/test/test_unistd.rs b/test/test_unistd.rs index eee10103..8ad6340c 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -1310,7 +1310,7 @@ fn test_getpeereid_invalid_fd() { } #[test] -#[cfg(not(any(target_os = "illumos", target_os = "redox")))] +#[cfg(not(target_os = "redox"))] fn test_faccessat_none_not_existing() { use nix::fcntl::AtFlags; let tempdir = tempfile::tempdir().unwrap(); @@ -1324,7 +1324,7 @@ fn test_faccessat_none_not_existing() { } #[test] -#[cfg(not(any(target_os = "illumos", target_os = "redox")))] +#[cfg(not(target_os = "redox"))] fn test_faccessat_not_existing() { use nix::fcntl::AtFlags; let tempdir = tempfile::tempdir().unwrap(); @@ -1344,7 +1344,7 @@ fn test_faccessat_not_existing() { } #[test] -#[cfg(not(any(target_os = "illumos", target_os = "redox")))] +#[cfg(not(target_os = "redox"))] fn test_faccessat_none_file_exists() { use nix::fcntl::AtFlags; let tempdir = tempfile::tempdir().unwrap(); @@ -1360,7 +1360,7 @@ fn test_faccessat_none_file_exists() { } #[test] -#[cfg(not(any(target_os = "illumos", target_os = "redox")))] +#[cfg(not(target_os = "redox"))] fn test_faccessat_file_exists() { use nix::fcntl::AtFlags; let tempdir = tempfile::tempdir().unwrap(); @@ -1376,3 +1376,32 @@ fn test_faccessat_file_exists() { ) .is_ok()); } + +#[test] +#[cfg(any( + all(target_os = "linux", not(target_env = "uclibc")), + target_os = "freebsd", + target_os = "dragonfly" +))] +fn test_eaccess_not_existing() { + let tempdir = tempdir().unwrap(); + let dir = tempdir.path().join("does_not_exist.txt"); + assert_eq!( + eaccess(&dir, AccessFlags::F_OK).err().unwrap(), + Errno::ENOENT + ); +} + +#[test] +#[cfg(any( + all(target_os = "linux", not(target_env = "uclibc")), + target_os = "freebsd", + target_os = "dragonfly" +))] +fn test_eaccess_file_exists() { + let tempdir = tempdir().unwrap(); + let path = tempdir.path().join("does_exist.txt"); + let _file = File::create(path.clone()).unwrap(); + eaccess(&path, AccessFlags::R_OK | AccessFlags::W_OK) + .expect("assertion failed"); +} |