std::filesystem::perm_options

 
 
 
定义于头文件 <filesystem>
enum class perm_options {

    replace = /* unspecified */,
    add = /* unspecified */,
    remove = /* unspecified */,
    nofollow = /* unspecified */

};
(C++17 起)

此类型代表控制函数 permissions() 行为的可用选项。

perm_options 满足满足位掩码类型 (BitmaskType) 的要求(表示位运算符 operator&operator|operator^operator~operator&=operator|=operator^= 对此类型定义)

成员常量

addremovereplace 至多可以存在一个,否则 permissions 函数的行为未定义。

成员常量 含义
replace 以给 permissions() 的参数完全替换权限(默认行为)
add 以当前权限和参数的逐位或替换权限
remove 以当前权限和参数取反结果的逐位与替换权限
nofollow permissions 将更改符号链接自身,而非其所代表的文件

示例

#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"); // 创建文件
 
    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");
}

可能的输出:

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

参阅

修改文件访问权限
(函数)
(C++17)
标识文件系统权限
(枚举)