std::filesy...mlink_status(3)

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

Okay so you're on the std::filesy...mlink_status(3) page from the C++ Standard Libary (yes, that spelling is what the header says, we are not fixing it πŸ’€). This is the C++17 bestie that tells you what kind of filesystem object a path is and what permissions it has. No cap, it's useful. 🫢


✨ NAME ✨

std::filesystem::status, std::filesystem::symlink_status β€” the C++17 filesystem functions that check out p and return a file status. One follows symlinks to their targets; one does not. Iykyk. πŸ’–


🌸 Synopsis 🌸

Defined in header <filesystem> ✨

(1) (since C++17)
std::filesystem::file_status status( const std::filesystem::path& p );

(2) (since C++17)
std::filesystem::file_status status( const std::filesystem::path& p,
                                     std::error_code& ec ) noexcept;

(3) (since C++17)
std::filesystem::file_status symlink_status( const std::filesystem::path& p );

(4) (since C++17)
std::filesystem::file_status symlink_status( const std::filesystem::path& p,
                                             std::error_code& ec ) noexcept;

1,2) These determine the type and attributes of the filesystem object identified by p as if by POSIX stat (symlinks are followed to their targets). In the following description, prms is the result of (m & perms::mask), where m is obtained as if by taking st_mode from the POSIX struct stat and converting it to the type std::filesystem::perms. Here's the whole moodboard: 🌈

  • If p is a regular file (as if by POSIX S_ISREG), returns file_status(file_type::regular, prms).
  • If p is a directory (as if by POSIX S_ISDIR), returns file_status(file_type::directory, prms).
  • If p is a block special file (as if by POSIX S_ISBLK), returns file_status(file_type::block, prms).
  • If p is a character special file (as if by POSIX S_ISCHR), returns file_status(file_type::character, prms).
  • If p is a fifo or pipe file (as if by POSIX S_ISFIFO), returns file_status(file_type::fifo, prms).
  • If p is a socket (as if by POSIX S_ISSOCK), returns file_status(file_type::socket, prms).
  • If p has an implementation-defined file type, returns file_status(file_type::A, prms) where A is the implementation-defined file_type constant for that type.
  • If p does not exist, returns file_status(file_type::not_found).
  • If p exists but file attributes cannot be determined, e.g. due to lack of permissions, returns file_status(file_type::unknown).
  • If errors prevent even knowing whether p exists, the non-throwing overload sets ec and returns file_status(file_type::none), and the throwing overload throws filesystem_error.
  • Otherwise, returns file_status(file_type::unknown, prms).

3,4) Same as (1,2) except that the behavior is as if the POSIX lstat is used (symlinks are not followed):

  • If p is a symlink, returns file_status(file_type::symlink).

Literally the no-follow mode, bestie. πŸ’…


πŸŽ€ Parameters πŸŽ€

  • p β€” path to examine.
  • ec β€” out-parameter for error reporting in the non-throwing overload.

πŸ’– Return value πŸ’–

The file status (a filesystem::file_status object). That's the whole return, no hidden extras. 🫢


😭 Exceptions 😭

Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.

  • 1,3) Throws std::filesystem::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.
  • 2,4) Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.
Heads up: the non-throwing overloads are the noexcept ones, so if you need to catch OS errors as exceptions, that's the 1,3 situation. πŸ’€

πŸ¦‹ Notes πŸ¦‹

The information provided by this function is usually also provided as a byproduct of directory iteration, and may be obtained by the member functions of filesystem::directory_entry. During directory iteration, calling status again is unnecessary. So don't do extra work for no reason, okay? πŸ’…


🌈 Example 🌈

Run this code πŸ‘‡

#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>

namespace fs = std::filesystem;

void demo_status(const fs::path& p, fs::file_status s)
{
    std::cout << p;
    // alternative: switch(s.type()) { case fs::file_type::regular: ...}
    if (fs::is_regular_file(s))
        std::cout << " is a regular file\n";
    if (fs::is_directory(s))
        std::cout << " is a directory\n";
    if (fs::is_block_file(s))
        std::cout << " is a block device\n";
    if (fs::is_character_file(s))
        std::cout << " is a character device\n";
    if (fs::is_fifo(s))
        std::cout << " is a named IPC pipe\n";
    if (fs::is_socket(s))
        std::cout << " is a named IPC socket\n";
    if (fs::is_symlink(s))
        std::cout << " is a symlink\n";
    if (!fs::exists(s))
        std::cout << " does not exist\n";
}

int main()
{
    // create files of different kinds
    fs::create_directory("sandbox");
    fs::create_directory("sandbox/dir");
    std::ofstream{"sandbox/file"}; // create regular file
    fs::create_symlink("file", "sandbox/symlink");

    mkfifo("sandbox/pipe", 0644);
    sockaddr_un addr;
    addr.sun_family = AF_UNIX;
    std::strcpy(addr.sun_path, "sandbox/sock");
    int fd = socket(PF_UNIX, SOCK_STREAM, 0);
    bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof addr);

    // demo different status accessors
    for (auto it{fs::directory_iterator("sandbox")}; it != fs::directory_iterator(); ++it)
        demo_status(*it, it->symlink_status()); // use cached status from directory entry
    demo_status("/dev/null", fs::status("/dev/null")); // direct calls to status
    demo_status("/dev/sda", fs::status("/dev/sda"));
    demo_status("sandbox/no", fs::status("/sandbox/no"));

    // cleanup (prefer std::unique_ptr-based custom deleters)
    close(fd);
    fs::remove_all("sandbox");
}

πŸ’€ Possible output πŸ’€

"sandbox/file" is a regular file
"sandbox/dir" is a directory
"sandbox/pipe" is a named IPC pipe
"sandbox/sock" is a named IPC socket
"sandbox/symlink" is a symlink
"/dev/null" is a character device
"/dev/sda" is a block device
"sandbox/no" does not exist

πŸ‘‘ See also πŸ‘‘

  • file_status (C++17) (class) β€” represents file type and permissions.
  • status_known (C++17) (function) β€” checks whether file status is known.
  • is_block_file (C++17) (function) β€” checks whether the given path refers to block device.
  • is_character_file (C++17) (function) β€” checks whether the given path refers to a character device.
  • is_directory (C++17) (function) β€” checks whether the given path refers to a directory.
  • is_fifo (C++17) (function) β€” checks whether the given path refers to a named pipe.
  • is_other (C++17) (function) β€” checks whether the argument refers to an other file.
  • is_regular_file (C++17) (function) β€” checks whether the argument refers to a regular file.
  • is_socket (C++17) (function) β€” checks whether the argument refers to a named IPC socket.
  • is_symlink (C++17) (function) β€” checks whether the argument refers to a symbolic link.
  • exists (C++17) (function) β€” checks whether path refers to existing file system object.
  • status β€” status of the file designated by this directory entry;
  • symlink_status β€” status of the file/symlink designated by this directory entry (public member function of std::filesystem::directory_entry).

And that's the tea πŸ“: this page is from http://cppreference.com, dated 2024.06.10, and the manual name is std::filesy...mlink_status(3). Go check your file types and stay iconic, bestie πŸ’–βœ¨

πŸ“¦ where this page comes from