std::filesystem::perm_options
来自 cppreference.cn
< cpp | filesystem
定义于头文件 <filesystem> |
||
enum class perm_options { replace = /* 未指定 */, |
(C++17 起) | |
此类型表示可用的选项,用于控制函数 std::filesystem::permissions() 的行为。
perm_options
满足 BitmaskType 的要求(这意味着为该类型定义了按位运算符 operator&、operator|、operator^、operator~、operator&=、operator|= 和 operator^=)。
[编辑] 成员常量
add
、remove
、replace
最多只能存在一个,否则权限函数的行为未定义。
成员常量 | 含义 |
---|---|
replace
|
权限将被 permissions() 的参数完全替换(默认行为) |
add
|
权限将被参数与当前权限的按位或替换 |
remove
|
权限将被参数的按位非与当前权限的按位与替换 |
nofollow
|
权限将在符号链接本身上更改,而不是在其解析到的文件上更改 |
[编辑] 示例
运行此代码
#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"); }
可能的输出
Created file with permissions: rw-r--r-- After adding u+rwx and g+wrx: rwxrwxr--
[编辑] 参阅
(C++17) |
修改文件访问权限 (函数) |
(C++17) |
标识文件系统权限 (枚举) |