std::filesystem::perms

来自cppreference.com
 
 
 
在标头 <filesystem> 定义
enumclass perms;
(C++17 起)

此类型表示文件访问权限。

perms 满足位掩码类型(BitmaskType) 的规定(这意味着此类型定义了按位运算符 operator&operator|operator^operator~operator&=operator|=operator^= 等)。none 代表空的位掩码;别的每个枚举项均代表一个不同的位掩码元素。

访问权限模仿 POSIX 权限位,而且任何单独的文件权限(由 filesystem::status 报告)都是下列位的某些组合:

目录

[编辑]成员常量

成员常量 值(八进制) POSIX 等价 含义
none0 未设权限位
owner_read0400S_IRUSR 文件拥有者拥有读权限
owner_write0200S_IWUSR 文件拥有者拥有写权限
owner_exec0100S_IXUSR 文件拥有着拥有执行/查找权限
owner_all0700S_IRWXU 文件拥有者拥有读、写、执行/查找权限

等价于 owner_read | owner_write | owner_exec

group_read040S_IRGRP 文件用户组拥有读权限
group_write020S_IWGRP 文件用户组拥有写权限
group_exec010S_IXGRP 文件用户组拥有执行/查找权限
group_all070S_IRWXG 文件用户组拥有读、写、执行/查找权限

等价于 group_read | group_write | group_exec

others_read04S_IROTH 其他用户拥有读权限
others_write02S_IWOTH 其他用户拥有写权限
others_exec01S_IXOTH 其他用户拥有执行/查找权限
others_all07S_IRWXO 其他用户拥有拥有读、写及执行/查找权限

等价于 others_read | others_write | others_exec

all0777 所有用户拥有读、写及执行/查找权限

等价于 owner_all | group_all | others_all

set_uid04000S_ISUID 在执行时设置用户 ID 为文件拥有者的用户 ID
set_gid02000S_ISGID 在执行时设置组 ID 为文件用户的组 ID
sticky_bit01000S_ISVTX 实现定义的含义,不过 POSIX XSI 指定在设置于目录时,唯有文件拥有者可以删除文件,即使该目录对其他人可写(与 /tmp 一并使用)
mask07777 所有有效权限位。

等价于 all | set_uid | set_gid | sticky_bit

另外,定义此类型的下列常量,它不表示权限:

成员常量 值(十六进制) 含义
unknown0xFFFF 未知权限(例如当 filesystem::file_status 以不带权限创建时)

[编辑]注解

权限不必用位实现,但概念上用该方式处理它们。

某些系统上某些权限位可能被忽略,而更改某些位可能自动影响其他位(例如在无拥有者/群/全体区别的平台上,设置三者任一部分都会写入三者全体)。

[编辑]示例

#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");// 创建文件   std::cout<<"创建文件的权限: "; 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<<"添加 u+rwx 和 g+rwx 后: "; demo_perms(std::filesystem::status("test.txt").permissions());   std::filesystem::remove("test.txt");}

可能的输出:

创建文件的权限: rw-r--r-- 添加 u+rwx 和 g+rwx 后: rwxrwxr--

[编辑]参阅

(C++17)(C++17)
确定文件属性
确定文件属性,检查符号链接目标
(函数)[编辑]
修改文件访问权限
(函数)[编辑]
close