Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add eaccess on freebsd, dragonfly and linux #1842

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
rtzoeller marked this conversation as resolved.
Show resolved Hide resolved
/// * [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! {
Expand Down
37 changes: 33 additions & 4 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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");
}