std::experimental::filesystem::perms
來自 cppreference.com
< cpp | experimental | fs
定義於標頭檔案 <experimental/filesystem> |
||
enum class perms; |
(檔案系統 TS) | |
此型別表示檔案訪問許可權。`perms` 滿足 BitmaskType 的要求(這意味著位運算子 operator&、operator|、operator^、operator~、operator&=、operator|= 和 operator^= 為此型別定義)。
訪問許可權模擬 POSIX 許可權位,任何單個檔案許可權(如 status 報告)是以下某些位的組合
目錄 |
[編輯] 成員常量
成員常量 | 值(八進位制) | POSIX 等效 | 含義 |
---|---|---|---|
無
|
0 | 未設定任何許可權位 | |
owner_read
|
0400 | S_IRUSR | 檔案所有者有讀取許可權 |
owner_write
|
0200 | S_IWUSR | 檔案所有者有寫入許可權 |
owner_exec
|
0100 | S_IXUSR | 檔案所有者有執行/搜尋許可權 |
owner_all
|
0700 | S_IRWXU | 檔案所有者有讀取、寫入和執行/搜尋許可權 等效於 owner_read | owner_write | owner_exec |
group_read
|
040 | S_IRGRP | 檔案使用者組有讀取許可權 |
group_write
|
020 | S_IWGRP | 檔案使用者組有寫入許可權 |
group_exec
|
010 | S_IXGRP | 檔案使用者組有執行/搜尋許可權 |
group_all
|
070 | S_IRWXG | 檔案使用者組有讀取、寫入和執行/搜尋許可權 等效於 group_read | group_write | group_exec |
others_read
|
04 | S_IROTH | 其他使用者有讀取許可權 |
others_write
|
02 | S_IWOTH | 其他使用者有寫入許可權 |
others_exec
|
01 | S_IXOTH | 其他使用者有執行/搜尋許可權 |
others_all
|
07 | S_IRWXO | 其他使用者有讀取、寫入和執行/搜尋許可權 等效於 others_read | others_write | others_exec |
all
|
0777 | 所有使用者都有讀取、寫入和執行/搜尋許可權 等效於 owner_all | group_all | others_all | |
set_uid
|
04000 | S_ISUID | 執行時將使用者 ID 設定為檔案所有者使用者 ID |
set_gid
|
02000 | S_ISGID | 執行時將組 ID 設定為檔案使用者組 ID |
sticky_bit
|
01000 | S_ISVTX | 實現定義的含義,但 POSIX XSI 規定,當在目錄上設定時,即使目錄可供其他使用者寫入,也只有檔案所有者才能刪除檔案(與 /tmp 一起使用) |
mask
|
07777 | 所有有效的許可權位 等效於 all | set_uid | set_gid | sticky_bit |
此外,還定義了以下不表示許可權的此型別常量
成員常量 | 值(十六進位制) | 含義 |
---|---|---|
未知
|
0xFFFF | 未知許可權(例如,當建立 file_status 時沒有許可權) |
add_perms
|
0x10000 | 控制位,指示 permissions 新增許可權位,但不清除許可權位 |
remove_perms
|
0x20000 | 控制位,指示 permissions 清除許可權位,但不新增許可權位 |
resolve_symlinks
|
0x40000 | 控制位,指示 permissions 解析符號連結 |
[編輯] 註釋
許可權不一定以位的形式實現,但它們在概念上被這樣對待。
某些許可權位在某些系統上可能會被忽略,更改某些位可能會自動更改其他位(例如,在沒有所有者/組/所有區別的平臺上,設定任何一個寫入位都會設定所有三個)。
[編輯] 示例
執行此程式碼
#include <bitset> #include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::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+rwx: rwxrwxr--
[編輯] 另請參閱
確定檔案屬性 確定檔案屬性,檢查符號連結目標 (函式) | |
修改檔案訪問許可權 (函式) |