std::filesy...permissions(3)
β¨ NAME β¨
std::filesystem::permissions β okay bestie, the function is literally named std::filesystem::permissions, and this is its page from the C++ Standard Libary manual. The page tag is std::filesy...permissions(3). Itβs giving permissions main character energy. π
πΈ Synopsis πΈ
Defined in header <filesystem>:
(1) (since C++17)
void permissions( const std::filesystem::path& p,
std::filesystem::perms prms,
std::filesystem::perm_options opts =
perm_options::replace );
(2) (since C++17)
void permissions( const std::filesystem::path& p,
std::filesystem::perms prms,
std::error_code& ec ) noexcept;
(3) (since C++17)
void permissions( const std::filesystem::path& p,
std::filesystem::perms prms,
std::filesystem::perm_options opts,
std::error_code& ec );
Okay so this function changes the access permissions of the file that p resolves to, 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
optsisperm_options::replace, file permissions are set to exactlyprms & std::filesystem::perms::mask(meaning, every valid bit ofprmsis applied). - If
optsisperm_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
optsisperm_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. No mixing and matching, bestie β thatβs the rule π«Ά
The non-throwing overload has no special action on error.
π Parameters π
pβ path to examineprmsβ permissions to set, add, or removeoptsβ options controlling the action taken by this functionecβ out-parameter for error reporting in the non-throwing overload
π Return value π
(none) β literally no return value, not even a little one. π«Ά
π¨ Exceptions π¨
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
- 1) Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - 2,3) Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π§ 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). Lowkey chaotic, but thatβs the deal π
π» Example π»
Run this code, bestie:
// 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--
π See also π
These are the related refs, with their exact badge groups because the original badge cluster is a little scrambled:
permsβ identifies file system permissionsstatusβ determines file attributessymlink_statusβ determines file attributes, checking the symlink target
Exact badge groups from the page: (C++17) (enum); (C++17) (function); (C++17).
source/version/date: http://cppreference.com, 2024.06.10, manual tag std::filesy...permissions(3) β kiss kiss, you ate this page, periodt π«Άβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms