std::filesystem::perm_options

Defined in header <filesystem>
enum class perm_options {
    replace = /* unspecified */,
    add = /* unspecified */,
    remove = /* unspecified */,
    nofollow = /* unspecified */
};
(since C++17)

This type represents available options that control the behavior of the function permissions().

perm_options satisfies the requirements of BitmaskType (which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type).

Member constants

At most one of add, remove, replace may be present, otherwise the behavior of the permissions function is undefined.

Member constant Meaning
replace Permissions will be completely replaced by the argument to permissions() (default behavior)
add permissions will be replaced by the bitwise OR of the argument and the current permissions
remove permissions will be replaced by the bitwise AND of the negated argument and current permissions
nofollow permissions will be changed on the symlink itself, rather than on the file it resolves to

Example

#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:

Created file with permissions: rw-r--r--
After adding o+rwx and g+wrx:  rwxrwxr--

See also

(C++17)
modifies file access permissions
(function)
(C++17)
identifies file system permissions
(enum)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/filesystem/perm_options