0

I would like to check, if a file is read only or writeable, possibly without actually opening it, using the new std::filesystem api.

I was able to find a documentation about permissions with an example code about how to query a specific file's owner/group/others read/write/exetuce permissions.

How is it possible to find out the read permission for the user that is running the program?

An other process could lock the file as well, which may also make it not writeable.

There is a similar question, but the answer uses a C api, which I would like to avoid.

Iter Ator
  • 8,226
  • 20
  • 73
  • 164
  • 2
    If you want something as detailed as checking for locks, you just have to try opening it—any answer ahead of time would likely be outdated by the time you relied on it anyway. – Davis Herring Dec 18 '21 at 16:07
  • You should specify an operation system and filesystem. On Linux or FAT other process may not lock files. – 273K Dec 18 '21 at 16:10
  • I am looking for a closs platform solution. If there is no other way, then I am allowed to open the file – Iter Ator Dec 18 '21 at 16:13

1 Answers1

0

Yes, it is. Read this documentatioin for detail.

here is a small snippet for same:

(std::filesystem::status(file_path).permissions() & std::filesystem::perms::owner_read != std::filesystem::perms::none) 

if val is true then, its readable.

  • Please read the question and comments carefully, OP wants to know not whether the file is read-only by permissions but whether it's possible to open in non-read-only mode at the moment (which may be impossible e.g. due to locks), for which it's best to try to open the file in appropriate mode and see whether it's successful. That said, even for just permission side of the story your code returns whether the owner can read the file, not whether current user can do something other than read. – YurkoFlisk Jul 20 '22 at 22:45