在线文档教程
C++
文件系统 | Filesystem

std::filesystem::perms

STD::文件系统::perms

Defined in header
enum class perms;(since C++17)

此类型表示文件访问权限。perms满足…的要求BitmaskType%28,这意味着按位运算符operator&,,,operator|,,,operator^,,,operator~,,,operator&=,,,operator|=,和operator^=为此类型%29定义。

访问权限模型POSIX权限位报告的任何单个文件权限%28地位%29是以下几个比特的组合:

成员常数

Member constantValue (octal)POSIX equivalentMeaning
none​0​no permission bits are set
owner_read0400S_IRUSRFile owner has read permission
owner_write0200S_IWUSRFile owner has write permission
owner_exec0100S_IXUSRFile owner has execute/search permission
owner_all0700S_IRWXUFile owner has read, write, and execute/search permissions Equivalent to owner_read | owner_write | owner_exec.
group_read040S_IRGRPThe file's user group has read permission
group_write020S_IWGRPThe file's user group has write permission
group_exec010S_IXGRPThe file's user group has execute/search permission
group_all070S_IRWXGThe file's user group has read, write, and execute/search permissions Equivalent to group_read | group_write | group_exec.
others_read04S_IROTHOther users have read permission
others_write02S_IWOTHOther users have write permission
others_exec01S_IXOTHOther users have execute/search permission
others_all07S_IRWXOOther users have read, write, and execute/search permissions Equivalent to others_read | others_write | others_exec.
all0777All users have read, write, and execute/search permissions Equivalent to owner_all | group_all | others_all.
set_uid04000S_ISUIDSet user ID to file owner user ID on execution
set_gid02000S_ISGIDSet group ID to file's user group ID on execution
sticky_bit01000S_ISVTXImplementation-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)
mask07777All valid permission bits. Equivalent to all | set_uid | set_gid | sticky_bit.

此外,定义了以下此类型的常量,这些常量不表示权限:

Member constantValue (hex)Meaning
unknown0xFFFFUnknown permissions (e.g. when file_status is created without permissions)
add_perms0x10000Control bit that instructs permissions to add, but not clear permission bits.
remove_perms0x20000Control bit that instructs permissions to clear, but not add permission bits
resolve_symlinks0x40000Control bit that instructs permissions to resolve symlinks

注记

权限可能不一定被实现为位,但它们在概念上被这样对待。

在某些系统上,某些权限位可能被忽略,而更改某些位可能会自动更改其他的%28例如。在没有所有者/组/所有区别的平台上,设置三位写入位中的任意一位设置所有三%29。

二次

#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::add_perms | fs::perms::owner_all | fs::perms::group_all 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--

二次

另见

statussymlink_status (C++17)(C++17)determines file attributesdetermines file attributes, checking the symlink target (function)
permissions (C++17)modifies file access permissions (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/file system/perms