"access" system call in rust std library?


I’m looking for the “access” system call, which allows to check if the user has read or write (or execute) access to a given path. It kind of seems like the sort of thing that should exist in std::path right alongside exists, but I don’t see anything. Is this something I’ll have to get from the libc crate? A safe interface would be handy…


Maybe this is what you are looking for:


Considering exists, well… exists, I would think it’s just an omission (nobody asked for such a functionality before). That said, such a feature would inherently have a race condition, specifically Time of check to time of use vulnerability - just try doing whatever you want. Instead of checking if you can write, simply write.

Also, see Simple Permission Checks.


@kunerd, no metadata calls stat, which is a different system call.

@xfix, in this case, I’m trying to determine what if anything went wrong. At the moment I am using exists as a crude approximation, but then I will not report the error properly. I’m translating code that I wrote in C, and getting the corner cases right was challenging in the first place. I could try opening for read, but then I’d need to identify first whether it is a directory or a file, which means yet more system calls (which also are slower than access). It’s an error path so speed doesn’t particularly matter, but code clarity does, and access tests what I want to test.


You can find safe and Rust-y idiomatic low-level Unix API bindings in the amazing nix crate… except I just checked it and this particular function wasn’t there, so I implemented it and made a PR: https://github.com/nix-rust/nix/pull/605


Part of the problem is cross platform. Everything related to permissions is vastly different on Windows because it only has full blown ACLs and it does not have the primitive chmod/access stuff of posix platforms (and no the versions provided by the CRT are not equivalent and merely deal with the readonly file attribute bit). While providing a unix specific extension trait for access would be useful, any sort of cross platform version would have to be held off until someone actually figures out Windows ACLs and develops a solid third party crate for it.


Not really an issue, Windows also has well defined APIs that is pretty much identical to access, I think (to be exact a pair of GetFileSecurity and AccessCheck) for that purpose.