名稱空間
變體
操作

std::filesystem::permissions

來自 cppreference.com
 
 
 
定義於標頭檔案 <filesystem>
void permissions( const std::filesystem::path& p,

                  std::filesystem::perms prms,

                  std::filesystem::perm_options opts = perm_options::replace );
(1) (C++17 起)
void permissions( const std::filesystem::path& p,

                  std::filesystem::perms prms,

                  std::error_code& ec ) noexcept;
(2) (C++17 起)
void permissions( const std::filesystem::path& p,

                  std::filesystem::perms prms,
                  std::filesystem::perm_options opts,

                  std::error_code& ec );
(3) (C++17 起)

更改檔案(p 解析到的檔案)的訪問許可權,如同使用 POSIX 的 fchmodat。除非在 opts 中設定了 perm_options::nofollow,否則會追蹤符號連結。

第二個簽名函式的行為與呼叫時將 opts 設定為 perm_options::replace 相同。

效果取決於 prmsopts,具體如下:

  • 如果 optsperm_options::replace,檔案許可權將精確設定為 prms & std::filesystem::perms::mask(意味著 prms 中每個有效位都將被應用)。
  • 如果 optsperm_options::add,檔案許可權將精確設定為 status(p).permissions() | (prms & perms::mask)(意味著 prms 中任何有效位如果已設定但不在檔案當前許可權中,將被新增到檔案許可權中)。
  • 如果 optsperm_options::remove,檔案許可權將精確設定為 status(p).permissions() & ~(prms & perms::mask)(意味著 prms 中任何有效位如果已清除但在檔案當前許可權中已設定,將被從檔案許可權中清除)。

opts 必須僅設定 replaceaddremove 中的一個。

不拋異常的過載在發生錯誤時沒有特殊操作。

目錄

[編輯] 引數

p - 要檢查的路徑
prms - 要設定、新增或移除的許可權
opts - 控制此函式所執行操作的選項
ec - 非丟擲過載中用於錯誤報告的出參

[編輯] 返回值

(無)

[編輯] 異常

任何未標記為 noexcept 的過載都可能在記憶體分配失敗時丟擲 std::bad_alloc

1) 當底層作業系統 API 發生錯誤時,丟擲 std::filesystem::filesystem_error,其中 p 作為第一個路徑引數,作業系統錯誤碼作為錯誤碼引數。
2,3) 如果作業系統 API 呼叫失敗,將 std::error_code& 引數設定為作業系統 API 錯誤碼;如果沒有錯誤發生,則執行 ec.clear()

[編輯] 注意

許可權不一定以位的形式實現,但它們在概念上被這樣對待。

某些許可權位在某些系統上可能會被忽略,更改某些位可能會自動更改其他位(例如,在沒有所有者/組/所有區別的平臺上,設定任何一個寫入位都會設定所有三個)。

[編輯] 示例

#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)(C++17)
確定檔案屬性
確定檔案屬性,檢查符號連結目標
(函式) [編輯]