名稱空間
變體
操作

std::filesystem::perms

來自 cppreference.com
 
 
 
定義於標頭檔案 <filesystem>
enum class perms;
(C++17 起)

此型別表示檔案訪問許可權。

perms 滿足 BitmaskType 的要求(這意味著此型別定義了按位運算子 operator&operator|operator^operator~operator&=operator|=operator^=)。none 表示空位掩碼;其他每個列舉器都表示一個不同的位掩碼元素。

訪問許可權模型 POSIX 許可權位,任何單個檔案許可權(由 filesystem::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 未知許可權(例如,當建立 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"); // 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)
確定檔案屬性
確定檔案屬性,檢查符號連結目標
(函式) [編輯]
修改檔案訪問許可權
(函式) [編輯]