Skip to content

Commit 0d6cc18

Browse files
bors[bot]SteveLauC
andauthored
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&section=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 <[email protected]>
2 parents b456181 + 04e409b commit 0d6cc18

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2424
([#1833](https://github.com/nix-rust/nix/pull/1833))
2525
- Added `faccessat(2)` on illumos
2626
([#1841](https://github.com/nix-rust/nix/pull/1841))
27+
- Added `eaccess()` on FreeBSD, DragonFly and Linux (glibc and musl).
28+
([#1842](https://github.com/nix-rust/nix/pull/1842))
2729

2830
### Changed
2931

src/unistd.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,6 +2931,27 @@ pub fn faccessat<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: Acce
29312931
})?;
29322932
Errno::result(res).map(drop)
29332933
}
2934+
2935+
/// Checks the file named by `path` for accessibility according to the flags given
2936+
/// by `mode` using effective UID, effective GID and supplementary group lists.
2937+
///
2938+
/// # References
2939+
///
2940+
/// * [FreeBSD man page](https://www.freebsd.org/cgi/man.cgi?query=eaccess&sektion=2&n=1)
2941+
/// * [Linux man page](https://man7.org/linux/man-pages/man3/euidaccess.3.html)
2942+
#[cfg(any(
2943+
all(target_os = "linux", not(target_env = "uclibc")),
2944+
target_os = "freebsd",
2945+
target_os = "dragonfly"
2946+
))]
2947+
pub fn eaccess<P: ?Sized + NixPath>(path: &P, mode: AccessFlags) -> Result<()> {
2948+
let res = path.with_nix_path(|cstr| {
2949+
unsafe {
2950+
libc::eaccess(cstr.as_ptr(), mode.bits)
2951+
}
2952+
})?;
2953+
Errno::result(res).map(drop)
2954+
}
29342955
}
29352956

29362957
feature! {

test/test_unistd.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ fn test_getpeereid_invalid_fd() {
13101310
}
13111311

13121312
#[test]
1313-
#[cfg(not(any(target_os = "illumos", target_os = "redox")))]
1313+
#[cfg(not(target_os = "redox"))]
13141314
fn test_faccessat_none_not_existing() {
13151315
use nix::fcntl::AtFlags;
13161316
let tempdir = tempfile::tempdir().unwrap();
@@ -1324,7 +1324,7 @@ fn test_faccessat_none_not_existing() {
13241324
}
13251325

13261326
#[test]
1327-
#[cfg(not(any(target_os = "illumos", target_os = "redox")))]
1327+
#[cfg(not(target_os = "redox"))]
13281328
fn test_faccessat_not_existing() {
13291329
use nix::fcntl::AtFlags;
13301330
let tempdir = tempfile::tempdir().unwrap();
@@ -1344,7 +1344,7 @@ fn test_faccessat_not_existing() {
13441344
}
13451345

13461346
#[test]
1347-
#[cfg(not(any(target_os = "illumos", target_os = "redox")))]
1347+
#[cfg(not(target_os = "redox"))]
13481348
fn test_faccessat_none_file_exists() {
13491349
use nix::fcntl::AtFlags;
13501350
let tempdir = tempfile::tempdir().unwrap();
@@ -1360,7 +1360,7 @@ fn test_faccessat_none_file_exists() {
13601360
}
13611361

13621362
#[test]
1363-
#[cfg(not(any(target_os = "illumos", target_os = "redox")))]
1363+
#[cfg(not(target_os = "redox"))]
13641364
fn test_faccessat_file_exists() {
13651365
use nix::fcntl::AtFlags;
13661366
let tempdir = tempfile::tempdir().unwrap();
@@ -1376,3 +1376,32 @@ fn test_faccessat_file_exists() {
13761376
)
13771377
.is_ok());
13781378
}
1379+
1380+
#[test]
1381+
#[cfg(any(
1382+
all(target_os = "linux", not(target_env = "uclibc")),
1383+
target_os = "freebsd",
1384+
target_os = "dragonfly"
1385+
))]
1386+
fn test_eaccess_not_existing() {
1387+
let tempdir = tempdir().unwrap();
1388+
let dir = tempdir.path().join("does_not_exist.txt");
1389+
assert_eq!(
1390+
eaccess(&dir, AccessFlags::F_OK).err().unwrap(),
1391+
Errno::ENOENT
1392+
);
1393+
}
1394+
1395+
#[test]
1396+
#[cfg(any(
1397+
all(target_os = "linux", not(target_env = "uclibc")),
1398+
target_os = "freebsd",
1399+
target_os = "dragonfly"
1400+
))]
1401+
fn test_eaccess_file_exists() {
1402+
let tempdir = tempdir().unwrap();
1403+
let path = tempdir.path().join("does_exist.txt");
1404+
let _file = File::create(path.clone()).unwrap();
1405+
eaccess(&path, AccessFlags::R_OK | AccessFlags::W_OK)
1406+
.expect("assertion failed");
1407+
}

0 commit comments

Comments
 (0)