命名空间
变体
操作

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) (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)
确定文件属性
确定文件属性,检查符号链接目标
(函数) [编辑]