stdfilesystemperms(3)
std::filesystem::perms - std::filesystem::perms
Synopsis
Defined in header <filesystem> enum class perms; (since C++17) This type represents file access permissions. perms satisfies the requirements of BitmaskType (which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type) Access permissions model POSIX_permission_bits, and any individual file permissions (as reported by status) are a combination of some of the following bits:
Member constants
Member constant Value (octal) POSIX equivalent Meaning
none 0 no permission bits are set
owner_read 0400 S_IRUSR File owner has read permission
owner_write 0200 S_IWUSR File owner has write permission
owner_exec 0100 S_IXUSR File owner has execute/search permission
owner_all 0700 S_IRWXU File owner has read, write, and execute/search permissions
Additionally, the following constants of this type are defined, which do not represent permissions:
Member constant Value (hex) Meaning unknown 0xFFFF Unknown permissions (e.g. when file_status is created without permissions)
Notes
Permissions may not necessarily be implemented as bits, but they are treated that way conceptually. Some permission bits may be ignored on some systems, and changing some bits may automatically change others (e.g. on platforms without owner/group/all distinction, setting any of the three write bits set all three)
Example
// Run this code
#include <fstream>
#include <bitset>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
void demo_perms(fs::perms p)
{
std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
<< ‘\n’;
}
int main()
{
std::ofstream("test.txt"); // create file
std::cout << "Created file with permissions: ";
demo_perms(fs::status("test.txt").permissions());
fs::permissions("test.txt",
fs::perms::owner_all | fs::perms::group_all,
fs::perm_options::add);
std::cout << "After adding o+rwx and g+rwx: ";
demo_perms(fs::status("test.txt").permissions());
fs::remove("test.txt");
}
Possible output:
See also
status determines file attributes
symlink_status determines file attributes, checking the symlink target