std::filesy...:permissions(3)
π std::filesystem::file_status::permissions π
okay so bestie, welcome to the std::filesystem::file_status::permissions page β it lives in the C++ Standard Library manual (std::filesy...:permissions(3), yes the title got truncated, she's mysterious like that β¨). This is the little duo of member functions that let you look at and change the permission bits attached to a file status. Lowkey iconic, highkey useful. π«Ά
β¨ NAME β¨
std::filesystem::file_status::permissions β and again, in case you missed it: std::filesystem::file_status::permissions. She said her name twice, main character energy π
πΈ SYNOPSIS πΈ
std::filesystem::perms permissions() const noexcept; (1) (since C++17)
void permissions( std::filesystem::perms perm ) noexcept; (2) (since C++17)
These two access the file permission info, and ngl that's the whole vibe:
- Returns the file permissions info π
- Sets the file permissions to perm π
Both are noexcept, both are C++17 girlies, and both are here to serve.
π¦ PARAMETERS π¦
- perm β the file permissions to set to. that's it, that's the whole parameter, she's low maintenance π
π RETURN VALUE π
- File permissions info. (the tea β)
- (none) β literally nothing, void, she gives you nothing back and honestly? respect π
π§ 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--
look at her go π she created a file, read her own perms like a pro, added u+rwx and g+rwx, printed it again, and cleaned up after herself. that's a full self-care routine right there πβ¨
ngl this example usesstd::filesystem::permissions(the free function) to change things, and the memberpermissions()to read them β keep an eye on which one you're calling, bestie π
and that's the page!! mwah π this little writeup came to you from http://cppreference.com, dated 2024.06.10, filed under std::filesy...:permissions(3) β go forth and chmod responsibly, queen ππ
π¦ 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