命名空间
变体
操作

std::filesystem::permissions

来自 cppreference.cn
 
 
 
定义于头文件 <filesystem>
void permissions( const std::filesystem::path& p,

                  std::filesystem::perms prms,

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

                  std::filesystem::perms prms,

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

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

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

更改 p 解析到的文件的访问权限,如同 POSIX fchmodat 的行为。除非在 perm_options::nofollow 中设置了 opts,否则会跟随符号链接。

第二个签名版本的行为如同调用时 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)
确定文件属性
确定文件属性,检查符号链接目标
(函数) [编辑]