Namespaces
Variants
Actions

std::filesystem::directory_entry::is_socket

From cppreference.com
 
 
 
 
bool is_socket() const;
(1) (since C++17)
bool is_socket( std::error_code& ec ) const noexcept;
(2) (since C++17)

Checks whether the pointed-to object is a named socket. Effectively returns:

Contents

[edit] Parameters

ec - out-parameter for error reporting in the non-throwing overload

[edit] Return value

true if the referred-to filesystem object is a named socket, false otherwise.

[edit] Exceptions

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

1) 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) 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.

[edit] Example

#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
 
namespace fs = std::filesystem;
 
void print_entry_type(const std::filesystem::directory_entry& entry)
{
    std::cout << entry.path() << ": ";
 
    if (!entry.exists())
        std::cout << "does not exist ";
    if (entry.is_block_file())
        std::cout << "is a block device ";
    if (entry.is_character_file())
        std::cout << "is a character device ";
    if (entry.is_directory())
        std::cout << "is a directory ";
    if (entry.is_fifo())
        std::cout << "is a named IPC pipe ";
    if (entry.is_regular_file())
        std::cout << "is a regular file ";
    if (entry.is_socket())
        std::cout << "is a named IPC socket ";
    if (entry.is_symlink())
        std::cout << "(a symlink)";
    if (entry.is_other())
        std::cout << "(an `other` file)";
 
    std::cout << '\n';
}
 
template<typename Type, typename Fun>
class scoped_cleanup
{
    std::unique_ptr<Type, std::function<void(const Type*)>> u;
 
public:
    scoped_cleanup(Type* ptr, Fun fun) : u{ptr, std::move(fun)} {}
};
 
int main()
{
    // Create files of different kinds.
    std::filesystem::current_path(fs::temp_directory_path());
    const std::filesystem::path sandbox{"sandbox"};
    scoped_cleanup remove_all_at_exit{&sandbox, [](const fs::path* p)
    {
        std::cout << "cleanup: remove_all(" << *p << ")\n";
        fs::remove_all(*p);
    }};
    std::filesystem::create_directory(sandbox);
    std::ofstream{sandbox/"file"}; // Creates a regular file
    std::filesystem::create_directory(sandbox/"dir");
 
    mkfifo((sandbox/"pipe").string().data(), 0644);
    struct sockaddr_un addr; addr.sun_family = AF_UNIX;