From 78d7aea35ef838a88d85a79a0acf36c5506d76ed Mon Sep 17 00:00:00 2001 From: jr Date: Fri, 19 Apr 2019 23:52:59 +0200 Subject: Add a access(2) wrapper --- src/unistd.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/unistd.rs') diff --git a/src/unistd.rs b/src/unistd.rs index c3b2b61e..f855c0af 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -2334,3 +2334,28 @@ mod setres { Errno::result(res).map(drop) } } + +libc_bitflags!{ + /// Options for access() + pub struct AccessFlags : c_int { + /// Test for existence of file. + F_OK; + /// Test for read permission. + R_OK; + /// Test for write permission. + W_OK; + /// Test for execute (search) permission. + X_OK; + } +} + +/// Checks the file named by `path` for accessibility according to the flags given by `amode` +/// See [access(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html) +pub fn access(path: &P, amode: AccessFlags) -> Result<()> { + let res = path.with_nix_path(|cstr| { + unsafe { + libc::access(cstr.as_ptr(), amode.bits) + } + })?; + Errno::result(res).map(drop) +} -- cgit v1.2.3