Skip to content

Commit b7fa4c0

Browse files
committed
add support for access/faccessat
1 parent 97df067 commit b7fa4c0

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased]
77

88
<!--### Added-->
9-
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat, rename, renameat, mknodat, unlinkat, mkdirat, link, linkat, symlink, symlinkat}`
10-
([#497](https://github.com/nix-rust/nix/pull/551))
9+
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat, rename, renameat, mknodat, unlinkat, mkdirat, link, linkat, symlink, symlinkat, access, faccessat}`
10+
([#552](https://github.com/nix-rust/nix/pull/552), [#561](https://github.com/nix-rust/nix/pull/561))
1111

1212
### Changed
1313
- Marked `sys::mman::{ mmap, munmap, madvise, munlock, msync }` as unsafe.

src/unistd.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ impl ForkResult {
4444
}
4545
}
4646

47+
libc_bitflags!{
48+
pub flags AccessMode: c_int {
49+
R_OK,
50+
W_OK,
51+
X_OK,
52+
F_OK
53+
}
54+
}
55+
4756
/// Create a new child process duplicating the parent process ([see
4857
/// fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html)).
4958
///
@@ -775,6 +784,20 @@ pub fn unlinkat<P: ?Sized + NixPath>(fd: RawFd, pathname: &P, flags: AtFlags) ->
775784
Errno::result(res).map(drop)
776785
}
777786

787+
pub fn access<P: ?Sized + NixPath>(pathname: &P, mode: AccessMode) -> Result<()> {
788+
let res = try!(pathname.with_nix_path(|cstr| {
789+
unsafe { libc::access(cstr.as_ptr(), mode.bits()) }
790+
}));
791+
Errno::result(res).map(drop)
792+
}
793+
794+
pub fn faccessat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, mode: AccessMode, flags: AtFlags) -> Result<()> {
795+
let res = try!(pathname.with_nix_path(|cstr| {
796+
unsafe { libc::faccessat(dirfd, cstr.as_ptr(), mode.bits(), flags.bits()) }
797+
}));
798+
Errno::result(res).map(drop)
799+
}
800+
778801
#[inline]
779802
pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
780803
let res = try!(path.with_nix_path(|cstr| {

test/test_unistd.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,33 @@ fn test_mkdirat() {
102102
let path = tempdir.path().join("test_path");
103103

104104
let dirfd = fcntl::open(tempdir.path(),
105-
fcntl::OFlag::empty(),
106-
stat::Mode::empty());
105+
fcntl::OFlag::empty(),
106+
stat::Mode::empty());
107107

108108
mkdirat(dirfd.unwrap(),
109-
&path.file_name(),
110-
stat::Mode::empty()).unwrap();
109+
&path.file_name(),
110+
stat::Mode::empty()).unwrap();
111111
assert!(path.exists());
112112
}
113113

114+
#[test]
115+
fn test_access() {
116+
let tempdir = TempDir::new("nix-test_mkdirat").unwrap();
117+
118+
let dirfd = fcntl::open(tempdir.path().parent().unwrap(),
119+
fcntl::OFlag::empty(),
120+
stat::Mode::empty());
121+
122+
// if succeed, permissions are or ok
123+
access(tempdir.path(), R_OK | X_OK | W_OK).unwrap();
124+
125+
faccessat(dirfd.unwrap(),
126+
&tempdir.path().file_name(),
127+
R_OK | X_OK | W_OK,
128+
fcntl::AtFlags::empty()).unwrap();
129+
130+
}
131+
114132
macro_rules! execve_test_factory(
115133
($test_name:ident, $syscall:ident, $unix_sh:expr, $android_sh:expr) => (
116134
#[test]

0 commit comments

Comments
 (0)