std::experi...permissions(3)
πΈ NAME πΈ
std::experimental::filesystem::permissions β std::experimental::filesystem::permissions
okay bestie, this is the C++ Standard Libary manual page for std::experimental::filesystem::permissions β yes, βLibaryβ is the manual name, we are respecting the lore and not fixing it π
β¨ Synopsis β¨
Defined in header <experimental/filesystem>
void permissions( const path& p, perms prms );
(filesystem TS)
void permissions( const path& p, perms prms, error_code& ec );
So what does it do? It changes the access permissions of the file that p resolves to, as if by POSIX fchmodat.
Symlinks are followed if prms::resolve_symlinks is set.
The effects depend on prms like this, bestie:
- If neither
perms::add_permsnorperms::remove_permsis set, the file permissions are set to exactlyprms & fs::perms::maskβ meaning every valid bit ofprmsis applied. - If
perms::add_permsis set, the file permissions are set to exactlystatus(p).permissions() | (prms & perms::mask)β meaning any valid bit that is set inprmsbut not in the fileβs current permissions is added to the fileβs permissions. - If
perms::remove_permsis set, the file permissions are set to exactlystatus(p).permissions() & ~(prms & perms::mask)β meaning any valid bit that is clear inprmsbut set in the fileβs current permissions is cleared in the fileβs permissions. - If both
perms::add_permsandperms::remove_permsare set, an error occurs. π«
The non-throwing overload has no special action on error.
π Parameters π
pβ path to examineprmsβ permissions to set, add, or removeecβ out-parameter for error reporting in the non-throwing overload
π¦ Return value π¦
(none)
literally none, not even a crumb, no cap.
π₯ Exceptions π₯
The overload that does not take an error_code& parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails.
The overload taking an 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. This overload has noexcept specification: noexcept.
π 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. iykyk, permission lore is wild sometimes.
π Example π
// Run this code
#include <bitset>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::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::add_perms |
fs::perms::owner_all |
fs::perms::group_all);
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+rwx: rwxrwxr--
π See also π
permsβ identifies file system permissions (enum)statusβ determines file attributessymlink_statusβ determines file attributes, checking the symlink target (function)
π§ Categories π§
- Noindexed pages
- unconditionally noexcept
π Hidden categories π
- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template
π aaaand thatβs the whole page, bestie! source/version: http://cppreference.com; date: 2024.06.10 β mwah, go handle those permissions responsibly π
π¦ 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