std::filesystem::file_type(3)

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

NAME ✨

std::filesystem::file_type β€” literally the enum that tells you what kind of thing a path is pointing at 🫢 (this is the std::filesystem::file_type(3) page from the C++ Standard Library, bestie, page 3, we're fancy here)


Synopsis πŸ’…

Defined in header <filesystem>:

enum class file_type {

    none = /* unspecified */,
    not_found = /* unspecified */,
    regular = /* unspecified */,
    directory = /* unspecified */,
    symlink = /* unspecified */,
    block = /* unspecified */,      (since C++17)
    character = /* unspecified */,
    fifo = /* unspecified */,
    socket = /* unspecified */,
    unknown = /* unspecified */,
    /* implementation-defined */

};

Okay so file_type defines constants that indicate a type of a file or directory a path refers to. The values of the enumerators are distinct, no cap, they are all their own little icons πŸŽ€


Constants 🌈

  • none β€” indicates that the file status has not been evaluated yet, or an error occurred when evaluating it 😭
  • not_found β€” indicates that the file was not found (and heads up: this is not considered an error, she's just not there, and that's fine πŸ’œ)
  • regular β€” a regular file
  • directory β€” a directory
  • symlink β€” a symbolic link
  • block β€” a block special file
  • character β€” a character special file
  • fifo β€” a FIFO (also known as a pipe) file
  • socket β€” a socket file
  • implementation-defined extra β€” an additional implementation-defined constant for each implementation-defined additional file type supported by the implementation (for example, MSVC STL defines junction for NTFS junctions πŸ‘€ iconic of them honestly)
  • unknown β€” the file exists but its type could not be determined πŸ’€

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;
    switch (s.type())
    {
        case fs::file_type::none:
            std::cout << " has `not-evaluated-yet` type";
            break;
        case fs::file_type::not_found:
            std::cout << " does not exist";
            break;
        case fs::file_type::regular:
            std::cout << " is a regular file";
            break;
        case fs::file_type::directory:
            std::cout << " is a directory";
            break;
        case fs::file_type::symlink:
            std::cout << " is a symlink";
            break;
        case fs::file_type::block:
            std::cout << " is a block device";
            break;
        case fs::file_type::character:
            std::cout << " is a character device";
            break;
        case fs::file_type::fifo:
            std::cout << " is a named IPC pipe";
            break;
        case fs::file_type::socket:
            std::cout << " is a named IPC socket";
            break;
        case fs::file_type::unknown:
            std::cout << " has `unknown` type";
            break;
        default:
            std::cout << " has `implementation-defined` type";
            break;
    }
    std::cout << '\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");
}
Also, in the cleanup there's a little note: prefer std::unique_ptr-based custom deleters 🫢 so like, do that in your real code, mmkay?

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

It's giving "I asked the filesystem who it was and it answered" ✨


See also πŸ‘‘

  • file_status (C++17) β€” represents file type and permissions (class)
  • is_block_file (C++17) β€” checks whether the given path refers to block device (function)
  • is_character_file (C++17) β€” checks whether the given path refers to a character device (function)
  • is_directory (C++17) β€” checks whether the given path refers to a directory (function)
  • is_fifo (C++17) β€” checks whether the given path refers to a named pipe (function)
  • is_other (C++17) β€” checks whether the argument refers to an other file (function)
  • is_socket (C++17) β€” checks whether the argument refers to a named IPC socket (function)
  • is_symlink (C++17) β€” checks whether the argument refers to a symbolic link (function)
  • is_regular_file (C++17) β€” checks whether the directory entry refers to a regular file (public member function of std::filesystem::directory_entry)

Okay bestie that's the whole page, no details left behind, we ate and left no crumbs πŸ’–

source: http://cppreference.com Β· version/date: 2024.06.10 Β· and the page we just did was std::filesystem::file_type(3), mwah πŸ’…πŸŽ€

πŸ“¦ where this page comes from