Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::filesystem::perms — permission bitmask

std::filesystem::perms represents file permission bits. Combine flags with |, inspect them with &, and distinguish an empty permission set from unknown permissions.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <filesystem>
using P = std::filesystem::perms;
P bits = P::owner_read | P::owner_write;
bool readable = (bits & P::owner_read) != P::none;

std::filesystem::perms from <filesystem> is an enum class bitmask type. Prefixes owner_, group_, and others_ identify owner, group, and others; suffixes read, write, exec mean read, write, and execute/search. owner_all, group_all, and others_all combine each category's three permissions.

perms::none is zero; perms::unknown means unknown permissions, not no permissions. perms::all (0777) covers ordinary permissions; perms::mask (07777) additionally covers set_uid, set_gid, and the operating-system-dependent sticky_bit.

Combine flags with |; test (bits & flag) != perms::none. For a composite flag, this checks for any matching bit; requiring all uses (bits & flag) == flag. Exclude perms::unknown before interpreting a retrieved mask.

A mask alone changes no file. std::filesystem::permissions accepts it with std::filesystem::perm_options: exactly one of replace (the default), add, remove, optionally combined with nofollow, which selects the symbolic link itself instead of its target. Operation support depends on the platform. The example manipulates values only, without filesystem access.

Examples

#include <filesystem>
#include <iostream>

int main() {
    using P = std::filesystem::perms;
    constexpr P bits = P::owner_read | P::owner_write | P::group_read;
    static_assert((bits & P::owner_write) != P::none);
    static_assert((bits & P::others_read) == P::none);

    std::cout << std::boolalpha
              << ((bits & P::owner_write) != P::none) << '\n'
              << ((bits & P::others_read) != P::none) << '\n';
}

Result

true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources