Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::perms

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      enumclass perms;
      (since C++17)

      This type represents file access permissions.

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

      Access permissions modelPOSIX permission bits, and any individual file permissions (as reported byfilesystem::status) are a combination of some of the following bits:

      Contents

      [edit]Member constants

      Member constantValue (octal)POSIX equivalentMeaning
      none0No permission bits are set
      owner_read0400S_IRUSRFile owner has read permission
      owner_write0200S_IWUSRFile owner has write permission
      owner_exec0100S_IXUSRFile owner has execute/search permission
      owner_all0700S_IRWXUFile owner has read, write, and execute/search permissions

      Equivalent toowner_read| owner_write| owner_exec

      group_read040S_IRGRPThe file's user group has read permission
      group_write020S_IWGRPThe file's user group has write permission
      group_exec010S_IXGRPThe file's user group has execute/search permission
      group_all070S_IRWXGThe file's user group has read, write, and execute/search permissions

      Equivalent togroup_read| group_write| group_exec

      others_read04S_IROTHOther users have read permission
      others_write02S_IWOTHOther users have write permission
      others_exec01S_IXOTHOther users have execute/search permission
      others_all07S_IRWXOOther users have read, write, and execute/search permissions

      Equivalent toothers_read| others_write| others_exec

      all0777All users have read, write, and execute/search permissions

      Equivalent toowner_all| group_all| others_all

      set_uid04000S_ISUIDSet user ID to file owner user ID on execution
      set_gid02000S_ISGIDSet group ID to file's user group ID on execution
      sticky_bit01000S_ISVTXImplementation-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)
      mask07777All valid permission bits.

      Equivalent toall| set_uid| set_gid| sticky_bit

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

      Member constantValue (hex)Meaning
      unknown0xFFFFUnknown permissions (e.g. whenfilesystem::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

      Run this code
      #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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/perms&oldid=158115"

      [8]ページ先頭

      ©2009-2025 Movatter.jp