std::filesystem::permissions
| Defined in header <filesystem> |  |  | 
|---|---|---|
| void permissions(const std::filesystem::path& p, std::filesystem::perms prms); void permissions(const std::filesystem::path& p, std::filesystem::perms prms, std::error_code& ec); |  | (since C++17) | 
更改下列文件的访问权限:p决议,好像是由POSIX决定的弗莫达特.符号链接在下列情况下prms::resolve_symlinks已经设定好了。
影响取决于prms详情如下:
- 如果两者都没有perms::add_perms也不perms::remove_perms,则文件权限设置为完全正确。prms &std::filesystem::perms::mask%28含义,每一个有效位prms应用%29
- 如果perms::add_perms,文件权限设置为status(p).permissions() | (prms & perms::mask)%28意思是指在prms,但在文件%27s中没有将当前权限添加到文件%27s权限%29中。
- 如果perms::remove_perms,则文件权限设置为完全正确。status(p).permissions() & ~(prms & perms::mask)%28意思是指在prms,但是在文件%27s中设置的当前权限在文件%27s权限%29中被清除。
- 如果两者都是perms::add_perms和perms::remove_perms设置,则会发生错误。
非抛出过载对错误没有特殊的作用。
参数
| p | - | path to examine | 
|---|---|---|
| prms | - | permissions to set, add, or remove | 
| ec | - | out-parameter for error reporting in the non-throwing overload | 
返回值
%280%29
例外
不占用std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用p作为第一个参数和操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载
noexcept规格:
noexcept
注记
权限可能不一定被实现为位,但它们在概念上被这样对待。
在某些系统上,某些权限位可能被忽略,而更改某些位可能会自动更改其他的%28例如。在没有所有者/组/所有区别的平台上,设置三位写入位中的任意一位设置所有三%29。
例
二次
#include <fstream>
#include <bitset>
#include <iostream>
#include <filesystem>
namespace fs = std::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+wrx:  rwxrwxr--二次
另见
| perms (C++17) | identifies file system permissions (enum) | 
|---|---|
| statussymlink_status (C++17)(C++17) | determines file attributesdetermines file attributes, checking the symlink target (function) | 
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

