0

I'm asking the cross-platform version of this question:

C++: How to check if a file/directory is readable? (PHP equivalent: is_readable)

I have a filesystem::path of a file (let's ignore for simplicity the case of directories, symlinks etc.), and I want to determine whether it is readable by my process - but without trying to open it myself.

I want to only use standard-library functions. Can I do this, or will I have to resort to OS-specific calls?

Note I want to be able to tell non-readability apart from other issues such as physical failure during read, non-existence, and such. No-exceptions code is preferable.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Please clarify "is readable"? How is this different from existing? Does this apply to a locked file? Is this checking permission to the file? Does "is readable" mean there are no read errors? What about write-only files, how do they fit into the definition? – Thomas Matthews Apr 07 '22 at 17:58
  • "*I want to determine whether it is readable by my process - but without trying to open it myself*" - sorry, can't be done (well, not easily). The best option is to simply try to open the file for read access and check if the open succeeded or failed. There are too many complexities to deal with if you try to manually handle security policies, handle access permissions, etc that can get in the way of opening a file. So just let the OS deal with all of that for you. – Remy Lebeau Apr 07 '22 at 19:12
  • @ThomasMatthews: The file may not be readable if the program does not have permissions to read it. Regarding locking - TBH I don't mind, i.e. if you have a suggestion that answers "false" on read-locked file then I'm fine with that. Write-only files are not readable. – einpoklum Apr 07 '22 at 19:19
  • @RemyLebeau: The first sentence sounds like an answer. However, it's a bit strange that the filesystem library does not have something like POSIX `stat()` (I mean the functionality, not the exact choice of API ). – einpoklum Apr 07 '22 at 19:21
  • @einpoklum `stat()` can tell you if a file exists or not, but it can't tell you whether your program can open the file for reading. And even if it could, it would be a [TOCTOU race condition](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) since another program could open the file and block read access before your program tries to open the file. – Remy Lebeau Apr 07 '22 at 19:27
  • @RemyLebeau: Sorry, I meant `access()`. – einpoklum Apr 07 '22 at 19:29
  • @einpoklum using `access()` would still lead to a TOCTOU race (and even security vulnerabilties, which are warned about in the `access()` manpage). – Remy Lebeau Apr 07 '22 at 19:31
  • @RemyLebeau: I realize that it's not a guarantee of anything. But so is `is_regular_file()` or `is_directory()` and so on. i.e. not a problem for me. – einpoklum Apr 07 '22 at 22:29
  • @einpoklum querying `is_regular_file()` and `is_directory()` are much less of a race condition, though (and certainly not a security risk). A file/folder's type doesn't change during its lifetime. And it is quite rare that a file would be deleted and then a folder with the exact same name created in its place, or vice versa. – Remy Lebeau Apr 07 '22 at 22:39

0 Answers0