std::filesystem::permissions
來自 cppreference.com
< cpp | filesystem
定義於標頭檔案 <filesystem> |
||
void permissions( const std::filesystem::path& p, std::filesystem::perms prms, |
(1) | (C++17 起) |
void permissions( const std::filesystem::path& p, std::filesystem::perms prms, |
(2) | (C++17 起) |
void permissions( const std::filesystem::path& p, std::filesystem::perms prms, |
(3) | (C++17 起) |
更改檔案(p 解析到的檔案)的訪問許可權,如同使用 POSIX 的 fchmodat
。除非在 opts 中設定了 perm_options::nofollow
,否則會追蹤符號連結。
第二個簽名函式的行為與呼叫時將 opts 設定為 perm_options::replace 相同。
效果取決於 prms 和 opts,具體如下:
- 如果 opts 是 perm_options::replace,檔案許可權將精確設定為 prms & std::filesystem::perms::mask(意味著 prms 中每個有效位都將被應用)。
- 如果 opts 是 perm_options::add,檔案許可權將精確設定為 status(p).permissions() | (prms & perms::mask)(意味著 prms 中任何有效位如果已設定但不在檔案當前許可權中,將被新增到檔案許可權中)。
- 如果 opts 是 perm_options::remove,檔案許可權將精確設定為 status(p).permissions() & ~(prms & perms::mask)(意味著 prms 中任何有效位如果已清除但在檔案當前許可權中已設定,將被從檔案許可權中清除)。
opts 必須僅設定 replace
、add
或 remove
中的一個。
不拋異常的過載在發生錯誤時沒有特殊操作。
目錄 |
[編輯] 引數
p | - | 要檢查的路徑 |
prms | - | 要設定、新增或移除的許可權 |
opts | - | 控制此函式所執行操作的選項 |
ec | - | 非丟擲過載中用於錯誤報告的出參 |
[編輯] 返回值
(無)
[編輯] 異常
任何未標記為 noexcept
的過載都可能在記憶體分配失敗時丟擲 std::bad_alloc。
[編輯] 注意
許可權不一定以位的形式實現,但它們在概念上被這樣對待。
某些許可權位在某些系統上可能會被忽略,更改某些位可能會自動更改其他位(例如,在沒有所有者/組/所有區別的平臺上,設定任何一個寫入位都會設定所有三個)。
[編輯] 示例
執行此程式碼
#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) |
確定檔案屬性 確定檔案屬性,檢查符號連結目標 (函式) |