0

I have a file with paths and permissions in POSIX octal notation. The file paths must exist and permissions are checked as per the manifest file. If mismatch, set new filesystem permissions as per manifest. Is there an easy conversion between std::filesystem::perms and POSIX permissions?

As checked std::filesystem::permissions() has no integral type overload. The values in perms are non sequential.

I looked into BitmaskType named requirement, std::to_underlying and std::underlying_type.

The current workaround initializes std::array<std::filesystem::perms, 12> manually with all possible individual (single bit) permissions, but is dependent on the element positions and if upstream makes changes to the perms enum class, the code will break.

Iterating over the array works by setting corresponding bits in std::bitset<12>, checking each bit and casting the bitset to an unsigned integral type.

This is necessary since std::filesystem::perms is an enum class and unable to iterate over.

user16217248
  • 3,119
  • 19
  • 19
  • 37
  • Rightly or wrongly, the standard library rarely provides platform - specific APIs – Paul Sanders Apr 27 '23 at 09:39
  • The size of the `array` and `bitset` is 12. I should mention that `size_t POSIX_PERMS_SIZE=12;`. – Nikolay Kosev Apr 27 '23 at 09:41
  • 2
    it is highly improbable, that `upstream makes changes to the perms enum class` as this is a structure that is used by trillions of systems since decades. Be sure, that quite a lot will complain if a change to this enum gets announced :-) – Synopsis Apr 27 '23 at 09:42

0 Answers0