std::filesystem::perms(3)
π NAME π
okay so this page is std::filesystem::perms β and yes, the NAME literally says it again: std::filesystem::perms - std::filesystem::perms. Weβre in the C++ Standard Libary manual, bestie, and this is the whole permission-bit situation. β¨
β¨ Synopsis β¨
Defined in header <filesystem>
enum class perms; (since C++17)
This type represents file access permissions. no cap.
perms satisfies the requirements of BitmaskType (which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type). none represents the empty bitmask; every other enumerator represents a distinct bitmask element.
Access permissions model POSIX permission bits, and any individual file permissions (as reported by filesystem::status) are a combination of some of the following bits:
π Member constants π
The tableβs columns are Value (octal), POSIX equivalent, and Meaning. Hereβs the whole bitmask menu, slay:
noneβ value (octal):0; POSIX equivalent: β; meaning: No permission bits are set.owner_readβ value (octal):0400; POSIX equivalent:S_IRUSR; meaning: File owner has read permission.owner_writeβ value (octal):0200; POSIX equivalent:S_IWUSR; meaning: File owner has write permission.owner_execβ value (octal):0100; POSIX equivalent:S_IXUSR; meaning: File owner has execute/search permission.owner_allβ value (octal):0700; POSIX equivalent:S_IRWXU; meaning: File owner has read, write, and execute/search permissions. Equivalent toowner_read | owner_write | owner_exec.group_readβ value (octal):040; POSIX equivalent:S_IRGRP; meaning: The fileβs user group has read permission.group_writeβ value (octal):020; POSIX equivalent:S_IWGRP; meaning: The fileβs user group has write permission.group_execβ value (octal):010; POSIX equivalent:S_IXGRP; meaning: The fileβs user group has execute/search permission.group_allβ value (octal):070; POSIX equivalent:S_IRWXG; meaning: The fileβs user group has read, write, and execute/search permissions. Equivalent togroup_read | group_write | group_exec.others_readβ value (octal):04; POSIX equivalent:S_IROTH; meaning: Other users have read permission.others_writeβ value (octal):02; POSIX equivalent:S_IWOTH; meaning: Other users have write permission.others_execβ value (octal):01; POSIX equivalent:S_IXOTH; meaning: Other users have execute/search permission.others_allβ value (octal):07; POSIX equivalent:S_IRWXO; meaning: Other users have read, write, and execute/search permissions. Equivalent toothers_read | others_write | others_exec.allβ value (octal):0777; POSIX equivalent: β; meaning: All users have read, write, and execute/search permissions. Equivalent toowner_all | group_all | others_all.set_uidβ value (octal):04000; POSIX equivalent:S_ISUID; meaning: Set user ID to file owner user ID on execution.set_gidβ value (octal):02000; POSIX equivalent:S_ISGID; meaning: Set group ID to fileβs user group ID on execution.sticky_bitβ value (octal):01000; POSIX equivalent:S_ISVTX; meaning: Implementation-defined meaning, but POSIX XSI specifies that when set on a directory, only file owners may delete files even if the directory is writeable to others (used with/tmp).maskβ value (octal):07777; POSIX equivalent: β; meaning: All valid permission bits. Equivalent toall | set_uid | set_gid | sticky_bit.
Additionally, the following constants of this type are defined, which do not represent permissions:
unknownβ value (hex):0xFFFF; meaning: Unknown permissions (e.g. whenfilesystem::file_statusis created without permissions).
πΈ Notes πΈ
Permissions may not necessarily be implemented as bits, but they are treated that way conceptually. iykyk β¨
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). fr fr, thatβs the whole caveat.
π Example π
The page says // Run this code right before this, and hereβs the code, bestie:
#include <filesystem>
#include <fstream>
#include <iostream>
void demo_perms(std::filesystem::perms p)
{
using std::filesystem::perms;
auto show = [=](char op, perms perm)
{
std::cout << (perms::none == (perm & p) ? '-' : op);
};
show('r', perms::owner_read);
show('w', perms::owner_write);
show('x', perms::owner_exec);
show('r', perms::group_read);
show('w', perms::group_write);
show('x', perms::group_exec);
show('r', perms::others_read);
show('w', perms::others_write);
show('x', perms::others_exec);
std::cout << '\n';
}
int main()
{
std::ofstream("test.txt"); // create file
std::cout << "Created file with permissions: ";
demo_perms(std::filesystem::status("test.txt").permissions());
std::filesystem::permissions(
"test.txt",
std::filesystem::perms::owner_all |
std::filesystem::perms::group_all,
std::filesystem::perm_options::add
);
std::cout << "After adding u+rwx and g+rwx: ";
demo_perms(std::filesystem::status("test.txt").permissions());
std::filesystem::remove("test.txt");
}
β Possible output β
Created file with permissions: rw-r--r--
After adding u+rwx and g+wrx: rwxrwxr--
π See also π
statusβ determines file attributes;(C++17),(function).symlink_statusβ determines file attributes, checking the symlink target;(C++17),(function).permissionsβ modifies file access permissions;(C++17),(function).
source/version: http://cppreference.com β’ date: 2024.06.10 β’ page: std::filesystem::perms(3) β thatβs the footer vibe, and we are so out ππ«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms