std::filesystem::permissions
Defined in header <filesystem> | ||
---|---|---|
void permissions(const std::filesystem::path& p, std::filesystem::perms prms, std::filesystem::perm_options opts = perm_options::replace); void permissions(const std::filesystem::path& p, std::filesystem::perms prms, std::error_code& ec) noexcept; void permissions(const std::filesystem::path& p, std::filesystem::perms prms, std::filesystem::perm_options opts, std::error_code& ec); | (since C++17) |
Changes access permissions of the file to which p
resolves, as if by POSIX fchmodat. Symlinks are followed unless perm_options::nofollow
is set in opts
.
The second signature behaves as if called with opts
set to perm_options::replace
.
The effects depend on prms
and opts
as follows:
- If
opts
isperm_options::replace
, file permissions are set to exactlyprms & std::filesystem::perms::mask
(meaning, every valid bit ofprms
is applied) - If
opts
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) - If
opts
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 of replace
, add
, or remove
to be set.
The non-throwing overload has no special action on error.
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 |
Return value
(none).
Exceptions
The overload that does not take a std::error_code&
parameter throws filesystem_error
on underlying OS API errors, constructed with p
as the first path argument and the OS error code as the error code argument. The overload taking a std::error_code&
parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear()
if no errors occur. Any overload not marked noexcept
may throw std::bad_alloc
if memory allocation fails.
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).
Example
#include <fstream> #include <bitset> #include <iostream> #include <filesystem> namespace fs = std::filesystem; void demo_perms(fs::perms p) { std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-") << ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-") << ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-") << ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-") << ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-") << ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-") << ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-") << ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-") << ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-") << '\n'; } int main() { std::ofstream("test.txt"); // create file std::cout << "Created file with permissions: "; demo_perms(fs::status("test.txt").permissions()); fs::permissions("test.txt", fs::perms::owner_all | fs::perms::group_all, fs::perm_options::add); std::cout << "After adding o+rwx and g+rwx: "; demo_perms(fs::status("test.txt").permissions()); fs::remove("test.txt"); }
Possible output:
Created file with permissions: rw-r--r-- After adding o+rwx and g+wrx: rwxrwxr--
See also
(C++17) | identifies file system permissions (enum) |
(C++17)(C++17) | determines file attributes determines file attributes, checking the symlink target (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/filesystem/permissions