首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::perms

Defined in header <filesystem>

enum class perms;

(since C++17)

此类型表示文件访问权限。perms满足…的要求BitmaskType%28,这意味着按位运算符operator&,,,operator|,,,operator^,,,operator~,,,operator&=,,,operator|=,和operator^=为此类型%29定义。

访问权限模型POSIX权限位报告的任何单个文件权限%28地位%29是以下几个比特的组合:

成员常数

Member constant

Value (octal)

POSIX equivalent

Meaning

none

​0​

no permission bits are set

owner_read

0400

S_IRUSR

File owner has read permission

owner_write

0200

S_IWUSR

File owner has write permission

owner_exec

0100

S_IXUSR

File owner has execute/search permission

owner_all

0700

S_IRWXU

File owner has read, write, and execute/search permissions Equivalent to owner_read | owner_write | owner_exec.

group_read

040

S_IRGRP

The file's user group has read permission

group_write

020

S_IWGRP

The file's user group has write permission

group_exec

010

S_IXGRP

The file's user group has execute/search permission

group_all

070

S_IRWXG

The file's user group has read, write, and execute/search permissions Equivalent to group_read | group_write | group_exec.

others_read

04

S_IROTH

Other users have read permission

others_write

02

S_IWOTH

Other users have write permission

others_exec

01

S_IXOTH

Other users have execute/search permission

others_all

07

S_IRWXO

Other users have read, write, and execute/search permissions Equivalent to others_read | others_write | others_exec.

all

0777

All users have read, write, and execute/search permissions Equivalent to owner_all | group_all | others_all.

set_uid

04000

S_ISUID

Set user ID to file owner user ID on execution

set_gid

02000

S_ISGID

Set group ID to file's user group ID on execution

sticky_bit

01000

S_ISVTX

Implementation-defined meaning, but POSIX XSI specifies that when set on a directory, only file owners may delete files even if the directory is writeable to others (used with /tmp)

mask

07777

All valid permission bits. Equivalent to all | set_uid | set_gid | sticky_bit.

此外,定义了以下此类型的常量,这些常量不表示权限:

Member constant

Value (hex)

Meaning

unknown

0xFFFF

Unknown permissions (e.g. when file_status is created without permissions)

add_perms

0x10000

Control bit that instructs permissions to add, but not clear permission bits.

remove_perms

0x20000

Control bit that instructs permissions to clear, but not add permission bits

resolve_symlinks

0x40000

Control bit that instructs permissions to resolve symlinks

注记

权限可能不一定被实现为位,但它们在概念上被这样对待。

在某些系统上,某些权限位可能被忽略,而更改某些位可能会自动更改其他的%28例如。在没有所有者/组/所有区别的平台上,设置三位写入位中的任意一位设置所有三%29。

二次

代码语言:javascript
复制
#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");
}

二次

可能的产出:

二次

代码语言:javascript
复制
Created file with permissions: rw-r--r--
After adding o+rwx and g+wrx:  rwxrwxr--

二次

另见

statussymlink_status (C++17)(C++17)

determines file attributesdetermines file attributes, checking the symlink target (function)

permissions (C++17)

modifies file access permissions (function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券