Skip to content

<filesystem>: std::filesystem::exists fails when testing path naming unix socket #4077

Open
@vasama

Description

@vasama

Description

std::filesystem::exists(p), where p is a path pointing to a unix socket (e.g. p = "./test.sock"), throws an exception with the message exists: unknown error: "./test.sock".

Command-line test case

C:\temp> cat test.cpp
#include <exception>
#include <filesystem>
#include <iostream>
#include <cassert>

#ifndef _WINSOCKAPI_
#       define _WINSOCKAPI_
#endif

#include <WinSock2.h>
#include <ws2ipdef.h>
#include <afunix.h>

#pragma comment(lib, "ws2_32.lib")

int main()
{
        WSADATA wsa_data;
        assert(WSAStartup(MAKEWORD(2, 2), &wsa_data) == 0);

        auto socket = WSASocketW(AF_UNIX, SOCK_STREAM, 0, 0, 0, 0);
        assert(socket != SOCKET_ERROR);
        sockaddr_un addr;
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, "./test.sock", sizeof(addr.sun_path));
        assert(bind(socket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != SOCKET_ERROR);
        assert(listen(socket, 1) != SOCKET_ERROR);

        try
        {
                (void)std::filesystem::exists("./test.sock");
        }
        catch (std::exception const& e)
        {
                std::cout << e.what() << std::endl;
        }
}
​C:\temp> cl /std:c++17 /EHsc /Gr test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32822 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 14.37.32822.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj
​C:\temp> ./test.exe
exists: unknown error: "./test.sock"

Expected behavior

I expect the call to std::filesystem::exists to return true.

STL version

Microsoft Visual Studio Community 2022
Version 17.8.0 Preview 1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfilesystemC++17 filesystem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions