Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/filesystem/perm options"

From cppreference.com
m (link to ja)
m (fmt, capitalized 1st letter)
 
(4 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{cpp/filesystem/navbar}}
 
{{cpp/filesystem/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | filesystem}}
+
{{dcl header|filesystem}}
{{dcl | since=c++17 | 1=
+
{{dcl|since=c++17|1=
 
enum class perm_options {
 
enum class perm_options {
 
     replace = /* unspecified */,
 
     replace = /* unspecified */,
Line 13: Line 13:
 
{{dcl end}}
 
{{dcl end}}
  
This type represents available options that control the behavior of the function {{ltt|cpp/filesystem/permissions|permissions()}}.
+
This type represents available options that control the behavior of the function {{lc|std::filesystem::permissions()}}.
  
{{tt|perm_options}} satisfies the requirements of  {{named req|BitmaskType}} (which means the bitwise operators {{c|operator&}}, {{c|operator{{!}}}}, {{c|operator^}}, {{c|operator~}}, {{c|operator&{{=}}}}, {{c|operator{{!}}{{=}}}}, and {{c|operator^{{=}}}} are defined for this type)
+
{{satisfies bitmask|perm_options|no none}}
  
 
===Member constants===
 
===Member constants===
 
At most one of {{tt|add}}, {{tt|remove}}, {{tt|replace}} may be present, otherwise the behavior of the permissions function is undefined.
 
At most one of {{tt|add}}, {{tt|remove}}, {{tt|replace}} may be present, otherwise the behavior of the permissions function is undefined.
{| class="wikitable" style="text-align:left; font-size:85%; max-width:90em;"
+
{|class="wikitable" style="text-align:left; font-size:85%; max-width:90em;"
 
|-
 
|-
! Member constant
+
!Member constant
! Meaning
+
!Meaning
 
|-
 
|-
| {{tt|replace}}  
+
|{{tt|replace}}  
| Permissions will be completely replaced by the argument to {{tt|permissions()}} (default behavior)
+
|Permissions will be completely replaced by the argument to {{tt|permissions()}} (default behavior)
 
|-
 
|-
| {{tt|add}}  
+
|{{tt|add}}  
| permissions will be replaced by the bitwise OR of the argument and the current permissions
+
|Permissions will be replaced by the bitwise OR of the argument and the current permissions
 
|-
 
|-
| {{tt|remove}}
+
|{{tt|remove}}
| permissions will be replaced by the bitwise AND of the negated argument and current permissions
+
|Permissions will be replaced by the bitwise AND of the negated argument and current permissions
 
|-
 
|-
| {{tt|nofollow}}
+
|{{tt|nofollow}}
| permissions will be changed on the symlink itself, rather than on the file it resolves to
+
|Permissions will be changed on the symlink itself, rather than on the file it resolves to
 
|}
 
|}
  
Line 42: Line 42:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/filesystem/dsc permissions}}
+
{{dsc inc|cpp/filesystem/dsc permissions}}
{{dsc inc | cpp/filesystem/dsc perms}}
+
{{dsc inc|cpp/filesystem/dsc perms}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|es|ja|zh}}

Latest revision as of 10:30, 6 September 2023

 
 
 
Defined in header <filesystem>
enum class perm_options {

    replace = /* unspecified */,
    add = /* unspecified */,
    remove = /* unspecified */,
    nofollow = /* unspecified */

};
(since C++17)

This type represents available options that control the behavior of the function std::filesystem::permissions().

perm_options satisfies the requirements of BitmaskType (which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type).

[edit] Member constants

At most one of add, remove, replace may be present, otherwise the behavior of the permissions function is undefined.

Member constant Meaning
replace Permissions will be completely replaced by the argument to permissions() (default behavior)
add Permissions will be replaced by the bitwise OR of the argument and the current permissions
remove Permissions will be replaced by the bitwise AND of the negated argument and current permissions
nofollow Permissions will be changed on the symlink itself, rather than on the file it resolves to

[edit] Example

#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");
}

Possible output:

Created file with permissions: rw-r--r--
After adding u+rwx and g+wrx:  rwxrwxr--

[edit] See also

modifies file access permissions
(function) [edit]
(C++17)
identifies file system permissions
(enum) [edit]