Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::permissions

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      void permissions(conststd::filesystem::path& p,

                       std::filesystem::perms prms,

                       std::filesystem::perm_options opts= perm_options::replace);
      (1)(since C++17)
      void permissions(conststd::filesystem::path& p,

                       std::filesystem::perms prms,

                       std::error_code& ec)noexcept;
      (2)(since C++17)
      void permissions(conststd::filesystem::path& p,

                       std::filesystem::perms prms,
                       std::filesystem::perm_options opts,

                       std::error_code& ec);
      (3)(since C++17)

      Changes access permissions of the file to whichp resolves, as if by POSIXfchmodat. Symlinks are followed unlessperm_options::nofollow is set inopts.

      The second signature behaves as if called withopts set toperm_options::replace.

      The effects depend onprms andopts as follows:

      • Ifopts isperm_options::replace, file permissions are set to exactlyprms&std::filesystem::perms::mask (meaning, every valid bit ofprms is applied).
      • Ifopts isperm_options::add, the file permissions are set to exactlystatus(p).permissions()|(prms& perms::mask) (meaning, any valid bit that is set inprms, but not in the file's current permissions is added to the file's permissions).
      • Ifopts isperm_options::remove, the file permissions are set to exactlystatus(p).permissions()& ~(prms& perms::mask) (meaning, any valid bit that is clear inprms, but set in the file's current permissions is cleared in the file's permissions).

      opts is required to have only one ofreplace,add, orremove to be set.

      The non-throwing overload has no special action on error.

      Contents

      [edit]Parameters

      p - path to examine
      prms - permissions to set, add, or remove
      opts - options controlling the action taken by this function
      ec - out-parameter for error reporting in the non-throwing overload

      [edit]Return value

      (none)

      [edit]Exceptions

      Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.

      1) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument and the OS error code as the error code argument.
      2,3) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur.

      [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){usingstd::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)
      identifies file system permissions
      (enum)[edit]
      (C++17)(C++17)
      determines file attributes
      determines file attributes, checking the symlink target
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/permissions&oldid=157969"

      [8]ページ先頭

      ©2009-2025 Movatter.jp