名稱空間
變體
操作

std::filesystem::perm_options

來自 cppreference.com
 
 
 
定義於標頭檔案 <filesystem>
enum class perm_options {

    replace = /* 未指定 */,
    add = /* 未指定 */,
    remove = /* 未指定 */,
    nofollow = /* 未指定 */

};
(C++17 起)

此型別表示可用的選項,用於控制函式 std::filesystem::permissions() 的行為。

perm_options 滿足 BitmaskType 的要求(這意味著為該型別定義了按位運算子 operator&operator|operator^operator~operator&=operator|=operator^=)。

[編輯] 成員常量

addremovereplace 最多隻能存在一個,否則許可權函式的行為未定義。

成員常量 含義
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)
標識檔案系統許可權
(列舉) [編輯]