Namespaces
Variants

std::filesystem:: perms

From cppreference.net
定義済みヘッダー <filesystem>
enum class perms ;
(C++17以降)

この型はファイルアクセス権限を表します。

perms BitmaskType の要件を満たす(これはビット演算子 operator & operator | operator ^ operator~ operator & = operator | = 、および operator ^ = がこの型に対して定義されていることを意味する)。 none は空のビットマスクを表し、他のすべての列挙子はそれぞれ異なるビットマスク要素を表す。

アクセス権限モデル POSIX permission bits 、および個々のファイル権限( filesystem::status によって報告される)は、以下のビットの組み合わせです:

目次

メンバー定数

メンバー定数 値(8進数) POSIX 相当値 意味
none 0 パーミッションビットが設定されていない
owner_read 0400 S_IRUSR ファイル所有者に読み取り権限がある
owner_write 0200 S_IWUSR ファイル所有者に書き込み権限がある
owner_exec 0100 S_IXUSR ファイル所有者に実行/検索権限がある
owner_all 0700 S_IRWXU ファイル所有者に読み取り、書き込み、実行/検索権限がある

次と同等: owner_read | owner_write | owner_exec

group_read 040 S_IRGRP ファイルのユーザーグループに読み取り権限がある
group_write 020 S_IWGRP ファイルのユーザーグループに書き込み権限がある
group_exec 010 S_IXGRP ファイルのユーザーグループに実行/検索権限がある
group_all 070 S_IRWXG ファイルのユーザーグループに読み取り、書き込み、実行/検索権限がある

次と同等: group_read | group_write | group_exec

others_read 04 S_IROTH 他のユーザーに読み取り権限がある
others_write 02 S_IWOTH 他のユーザーに書き込み権限がある
others_exec 01 S_IXOTH 他のユーザーに実行/検索権限がある
others_all 07 S_IRWXO 他のユーザーに読み取り、書き込み、実行/検索権限がある

次と同等: others_read | others_write | others_exec

all 0777 すべてのユーザーに読み取り、書き込み、実行/検索権限がある

次と同等: owner_all | group_all | others_all

set_uid 04000 S_ISUID 実行時にユーザーIDをファイル所有者のユーザーIDに設定する
set_gid 02000 S_ISGID 実行時にグループIDをファイルのユーザーグループIDに設定する
sticky_bit 01000 S_ISVTX 実装定義の意味を持つが、POSIX XSIではディレクトリに設定された場合、他のユーザーに書き込み権限があってもファイル所有者のみがファイルを削除できることを指定する( / tmp で使用される)
mask 07777 すべての有効なパーミッションビット

次と同等: all | set_uid | set_gid | sticky_bit

さらに、この型で定義されている以下の定数は、権限を表すものではありません:

メンバー定数 値 (16進数) 意味
unknown 0xFFFF 不明なパーミッション(例: filesystem::file_status がパーミッションなしで作成された場合)

注記

権限は必ずしもビットとして実装されるわけではありませんが、概念的にはそのように扱われます。

一部のパーミッションビットは一部のシステムで無視される可能性があり、一部のビットを変更すると自動的に他のビットが変更される場合があります(例:所有者/グループ/全ユーザーの区別がないプラットフォームでは、3つの書き込みビットのいずれかを設定すると、3つすべてが設定されます)。

#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)
ファイル属性を決定する
シンボリックリンクのターゲットをチェックしてファイル属性を決定する
(function)
ファイルアクセス権限を変更する
(function)