Skip to content

Commit 74a4293

Browse files
committed
add support for access/faccessat
1 parent 7332b5a commit 74a4293

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
([#611](https://github.com/nix-rust/nix/pull/609)
1111
- Added `AioCb::from_boxed_slice`
1212
([#582](https://github.com/nix-rust/nix/pull/582)
13-
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat, rename, renameat, mknodat, unlinkat, mkdirat, link, linkat, symlink, symlinkat}`
14-
([#497](https://github.com/nix-rust/nix/pull/551))
13+
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat, rename, renameat, mknodat, unlinkat, mkdirat, link, linkat, symlink, symlinkat, access, faccessat}`
14+
([#552](https://github.com/nix-rust/nix/pull/552), [#561](https://github.com/nix-rust/nix/pull/561))
1515
- Added `nix::pty::{grantpt, posix_openpt, ptsname/ptsname_r, unlockpt}`
1616
([#556](https://github.com/nix-rust/nix/pull/556)
1717

src/unistd.rs

Lines changed: 27 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
///
@@ -787,6 +796,24 @@ pub fn unlinkat<P: ?Sized + NixPath>(fd: RawFd, pathname: &P, flags: AtFlags) ->
787796
Errno::result(res).map(drop)
788797
}
789798

799+
/// check user's permissions for a file
800+
/// ([see access(2)](http://man7.org/linux/man-pages/man2/access.2.html))
801+
pub fn access<P: ?Sized + NixPath>(pathname: &P, mode: AccessMode) -> Result<()> {
802+
let res = try!(pathname.with_nix_path(|cstr| {
803+
unsafe { libc::access(cstr.as_ptr(), mode.bits()) }
804+
}));
805+
Errno::result(res).map(drop)
806+
}
807+
808+
/// check user's permissions for a file
809+
/// ([see faccessat(2)](http://man7.org/linux/man-pages/man2/faccessat.2.html))
810+
pub fn faccessat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, mode: AccessMode, flags: AtFlags) -> Result<()> {
811+
let res = try!(pathname.with_nix_path(|cstr| {
812+
unsafe { libc::faccessat(dirfd, cstr.as_ptr(), mode.bits(), flags.bits()) }
813+
}));
814+
Errno::result(res).map(drop)
815+
}
816+
790817
#[inline]
791818
pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
792819
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)