std::filesy...:permissions(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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:

  1. Returns the file permissions info πŸŽ€
  2. 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 🌈

  1. File permissions info. (the tea β˜•)
  2. (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 uses std::filesystem::permissions (the free function) to change things, and the member permissions() 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