I have successfully used both stat()
& access()
separately to determine if a user has either read or read/write access to a directory.
Either is equivalent for your needs. access()
is a cleaner wrapper if you're not going to do anything with the stat structure that you populate.
Just be mindful that you are creating a race when doing this. The permissions can change between calling stat()/access()
and when you actually try and use the directory. Hell, the directory could even be deleted and recreated in that time.
It's better to just try and open what you need and check for EPERM
. Checking stat()
or access()
will not guarantee that a subsequent operation won't return EPERM.