gulrak / filesystem

An implementation of C++17 std::filesystem for C++11 /C++14/C++17/C++20 on Windows, macOS, Linux and FreeBSD.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

create_directories should return false when the path already exists

Alzathar opened this issue · comments

Describe the bug
For the methods filesystem::create_directories, the document N4687 says (see 30.10.15.6)

Returns: true if a new directory was created, otherwise false. The signature with argument ec returns false if an error occurs.

However, the current implementation in v1.3.0 returns true everytime.

To Reproduce

// should return false as the folder already exists
std::cout << boolalpha << std::filesystem::create_directories(std::filesystem::current_path()) << std::endl

Expected behavior
In case the path already exists and this is a directory, the returned value should be false. This is the behaviour implemented within VS2019 (16.4) and Xcode 11.

Additional context
A way to fix this issue could be to test if the path already exists. For example

// Around line 3400
GHC_INLINE bool create_directories(const path& p, std::error_code& ec) noexcept
{
    path current;
    ec.clear();
    // BEGIN_PROPOSITION
    std::error_code tec;
    auto fs = status(p, tec);
    if (status_known(fs) && exists(fs) && is_directory(fs)) {
        return false;
    }
    // END_PROPOSITION
    for (path::string_type part : p) {
        current /= part;
//...

Thank you, another missing test case found.

This fixes the issue. Thank you very much!