Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/filesystem/perms"

From cppreference.com
(+)
 
m (fmt, http -> https, capitalized 1st letter, .)
 
(8 intermediate revisions by 7 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++11 | 1=
+
{{dcl|since=c++17|1=
 
enum class perms;
 
enum class perms;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
This type represents file access permissions. {{tt|perms}} satisfies the requirements of  {{concept|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)
+
This type represents file access permissions.
  
Access permissions model [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html POSIX permission bits], and any individual file permissions (as reported by {{rlp|status}}) are a combination of some of the following bits:
+
{{satisfies bitmask|perms}}
 +
 
 +
Access permissions model [https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html POSIX permission bits], and any individual file permissions (as reported by {{lc|std::filesystem::status|filesystem::status}}) are a combination of some of the following bits:
  
 
===Member constants===
 
===Member constants===
{| 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
! Value (octal)
+
!Value (octal)
! POSIX equivalent
+
!POSIX equivalent
! Meaning
+
!Meaning
 
|-
 
|-
| {{tt|none}}
+
|{{tt|none}}
| {{c|0}}
+
|{{c|0}}
 
|
 
|
| no permission bits are set
+
|No permission bits are set
 
|-
 
|-
| {{tt|owner_read}}
+
|{{tt|owner_read}}
| {{c|0400}}
+
|{{c|0400}}
| {{c|S_IRUSR}}
+
|{{c|S_IRUSR}}
| File owner has read permission
+
|File owner has read permission
 
|-
 
|-
| {{tt|owner_write}}
+
|{{tt|owner_write}}
| {{c|0200}}
+
|{{c|0200}}
| {{c|S_IWUSR}}
+
|{{c|S_IWUSR}}
| File owner has write permission
+
|File owner has write permission
 
|-
 
|-
| {{tt|owner_exec}}
+
|{{tt|owner_exec}}
| {{c|0100}}
+
|{{c|0100}}
| {{c|S_IXUSR}}
+
|{{c|S_IXUSR}}
| File owner has execute/search permission
+
|File owner has execute/search permission
 
|-
 
|-
| {{tt|owner_all}}
+
|{{tt|owner_all}}
| {{c|0700}}
+
|{{c|0700}}
| {{c|S_IRWXU}}
+
|{{c|S_IRWXU}}
| File owner has read, write, and execute/search permissions
+
|File owner has read, write, and execute/search permissions
 
Equivalent to {{c|owner_read {{!}} owner_write {{!}} owner_exec}}
 
Equivalent to {{c|owner_read {{!}} owner_write {{!}} owner_exec}}
 
|-
 
|-
| {{tt|group_read}}
+
|{{tt|group_read}}
| {{c|040}}
+
|{{c|040}}
| {{c|S_IRGRP}}
+
|{{c|S_IRGRP}}
| The file's user group has read permission
+
|The file's user group has read permission
 
|-
 
|-
| {{tt|group_write}}
+
|{{tt|group_write}}
| {{c|020}}
+
|{{c|020}}
| {{c|S_IWGRP}}
+
|{{c|S_IWGRP}}
| The file's user group has write permission
+
|The file's user group has write permission
 
|-
 
|-
| {{tt|group_exec}}
+
|{{tt|group_exec}}
| {{c|010}}
+
|{{c|010}}
| {{c|S_IXGRP}}
+
|{{c|S_IXGRP}}
| The file's user group has execute/search permission
+
|The file's user group has execute/search permission
 
|-
 
|-
| {{tt|group_all}}
+
|{{tt|group_all}}
| {{c|070}}
+
|{{c|070}}
| {{c|S_IRWXG}}
+
|{{c|S_IRWXG}}
| The file's user group has read, write, and execute/search permissions
+
|The file's user group has read, write, and execute/search permissions
 
Equivalent to {{c|group_read {{!}} group_write {{!}} group_exec}}
 
Equivalent to {{c|group_read {{!}} group_write {{!}} group_exec}}
 
|-
 
|-
| {{tt|others_read}}
+
|{{tt|others_read}}
| {{c|04}}
+
|{{c|04}}
| {{c|S_IROTH}}
+
|{{c|S_IROTH}}
| Other users have read permission
+
|Other users have read permission
 
|-
 
|-
| {{tt|others_write}}
+
|{{tt|others_write}}
| {{c|02}}
+
|{{c|02}}
| {{c|S_IWOTH}}
+
|{{c|S_IWOTH}}
| Other users have write permission
+
|Other users have write permission
 
|-
 
|-
| {{tt|others_exec}}
+
|{{tt|others_exec}}
| {{c|01}}
+
|{{c|01}}
| {{c|S_IXOTH}}
+
|{{c|S_IXOTH}}
| Other users have execute/search permission
+
|Other users have execute/search permission
 
|-
 
|-
| {{tt|others_all}}
+
|{{tt|others_all}}
| {{c|07}}
+
|{{c|07}}
| {{c|S_IRWXO}}
+
|{{c|S_IRWXO}}
| Other users have read, write, and execute/search permissions
+
|Other users have read, write, and execute/search permissions
 
Equivalent to {{c|others_read {{!}} others_write {{!}} others_exec}}
 
Equivalent to {{c|others_read {{!}} others_write {{!}} others_exec}}
 
|-
 
|-
| {{tt|all}}
+
|{{tt|all}}
| {{c|0777}}
+
|{{c|0777}}
 
|
 
|
| All users have read, write, and execute/search permissions
+
|All users have read, write, and execute/search permissions
 
Equivalent to {{c|owner_all {{!}} group_all {{!}} others_all}}
 
Equivalent to {{c|owner_all {{!}} group_all {{!}} others_all}}
 
|-
 
|-
| {{tt|set_uid}}
+
|{{tt|set_uid}}
| {{c|04000}}
+
|{{c|04000}}
| {{c|S_ISUID}}
+
|{{c|S_ISUID}}
| Set user ID to file owner user ID on execution
+
|Set user ID to file owner user ID on execution
 
|-
 
|-
| {{tt|set_gid}}
+
|{{tt|set_gid}}
| {{c|02000}}
+
|{{c|02000}}
| {{c|S_ISGID}}
+
|{{c|S_ISGID}}
| Set group ID to file's user group ID on execution
+
|Set group ID to file's user group ID on execution
 
|-
 
|-
| {{tt|sticky_bit}}
+
|{{tt|sticky_bit}}
| {{c|01000}}
+
|{{c|01000}}
| {{c|S_ISVTX}}
+
|{{c|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 {{c|/tmp}})
+
|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 {{c|/tmp}})
 
|-
 
|-
| {{tt|mask}}
+
|{{tt|mask}}
| {{c|07777}}
+
|{{c|07777}}
 
|
 
|
| All valid permission bits.
+
|All valid permission bits.
 
Equivalent to {{c|all {{!}} set_uid {{!}} set_gid {{!}} sticky_bit}}
 
Equivalent to {{c|all {{!}} set_uid {{!}} set_gid {{!}} sticky_bit}}
 
|}
 
|}
Line 118: Line 120:
 
Additionally, the following constants of this type are defined, which do not represent permissions:
 
Additionally, the following constants of this type are defined, which do not represent permissions:
  
{| 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
! Value (hex)
+
!Value (hex)
! Meaning
+
!Meaning
 
|-
 
|-
| {{tt|unknown}}
+
|{{tt|unknown}}
| {{c|0xFFFF}}
+
|{{c|0xFFFF}}
| Unknown permissions (e.g. when {{rlp|file_status}} is created without permissions)
+
|Unknown permissions (e.g. when {{lc|std::filesystem::file_status|filesystem::file_status}} is created without permissions)
|-
+
| {{tt|add_perms}}
+
| {{c|0x10000}}
+
| Control bit that instructs {{rlp|permissions}} to add, but not clear permission bits.
+
|-
+
| {{tt|remove_perms}}
+
| {{c|0x20000}}
+
| Control bit that instructs {{rlp|permissions}} to clear, but not add permission bits
+
|-
+
| {{tt|resolve_symlinks}}
+
| {{c|0x40000}}
+
| Control bit that instructs {{rlp|permissions}} to resolve symlinks
+
 
|}
 
|}
  
Line 144: Line 134:
 
Permissions may not necessarily be implemented as bits, but they are treated that way conceptually.
 
Permissions may not necessarily be implemented as bits, but they are treated that way conceptually.
  
Some permission bits may be ignored on some systems, and changing some bits may automatically change others (e.g. on platforms without owner/group/all distinction, setting any of the three write bits set all three)
+
Some permission bits may be ignored on some systems, and changing some bits may automatically change others (e.g. on platforms without owner/group/all distinction, setting any of the three write bits set all three).
  
 
===Example===
 
===Example===
Line 151: Line 141:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/filesystem/dsc status}}
+
{{dsc inc|cpp/filesystem/dsc status}}
{{dsc inc | cpp/filesystem/dsc permissions}}
+
{{dsc inc|cpp/filesystem/dsc permissions}}
 
{{dsc end}}
 
{{dsc end}}
 +
 +
{{langlinks|es|ja|zh}}

Latest revision as of 10:38, 6 September 2023

 
 
 
Defined in header <filesystem>
enum class perms;
(since C++17)

This type represents file access permissions.

perms satisfies the requirements of BitmaskType (which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type). none represents the empty bitmask; every other enumerator represents a distinct bitmask element.

Access permissions model POSIX permission bits, and any individual file permissions (as reported by filesystem::status) are a combination of some of the following bits:

Contents

[edit] Member constants

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

Additionally, the following constants of this type are defined, which do not represent permissions:

Member constant Value (hex) Meaning
unknown 0xFFFF Unknown permissions (e.g. when filesystem::file_status is created without permissions)

[edit] Notes

Permissions may not necessarily be implemented as bits, but they are treated that way conceptually.

Some permission bits may be ignored on some systems, and changing some bits may automatically change others (e.g. on platforms without owner/group/all distinction, setting any of the three write bits set all three).

[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

(C++17)(C++17)
determines file attributes
determines file attributes, checking the symlink target
(function) [edit]
modifies file access permissions
(function) [edit]