Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::filesystem::create_directory and std::filesystem::create_directories have inconsistent error handling #41028

Open
gulrak opened this issue May 1, 2019 · 2 comments
Labels
bugzilla Issues migrated from bugzilla filesystem C++17 std::filesystem libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@gulrak
Copy link

gulrak commented May 1, 2019

Bugzilla Link 41683
Version 8.0
OS Linux
CC @mclow

Extended Description

std::filesystem::create_directories and std::filesystem::create_directory behave differently in case there is a regular file with the name of the directory to create.
While implementing a std::filesystem compatible library myself I found this while running my tests against the libc++ implementation:

std::filesystem::create_directories(p) throws filesystem_error wir "File exists"
std::filesystem::create_directories(p,ec) sets the ec to a corresponding error and returns false

std::filesystem::create_directory(p) reports false
std::filesystem::create_directory(p,ec) reports false and no error in ec

So for this code:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main()
{
    std::error_code ec;
    std::cout << fs::create_directory("foo.txt", ec);
    std::cout << (ec ? " error" : " no error") << std::endl;
    ec.clear();
    std::cout << fs::create_directories("foo.txt", ec);
    std::cout << (ec ? " error" : " no error") << std::endl;
}

with an existing text file "foo.txt" the current result is:

0 no error
0 error

(see runnable example here https://wandbox.org/permlink/Wa0Ie40k24rBwGmY)

No matter which of these two behaviours is the correct one, they should behave the same, in regard to this situation.

I know there is LWG issue 2935 (https://cplusplus.github.io/LWG/issue2935) that requested a change to handling this situation as no error, but I'm with Nicolai Josuttis, claiming this is not a good idea (see WG21 P1164R1, approved in Kona and adopted with cplusplus/draft#2703).

So in the end, all four operations should handle the case as an error.

(As additional information: GCCs libstdc++ had this behaviour swapped but inconsistent too, and adopted the P1164R1 solution.)

Thanks for your work!

@mclow
Copy link
Contributor

mclow commented May 1, 2019

If you take a look at https://libcxx.llvm.org/cxx2a_status.html, you'll see that neither LWG issue 2935 nor P1164 have been implemented (yet)

@gulrak
Copy link
Author

gulrak commented May 1, 2019

True, and I know that, but my concern is about the -std=c++17 behaviour (and I should have written that, sorry, wandbox resets the language to 2a when switching versions, und I accidently permalinked a 2a one) and I just wanted to give a most complete picture, after I was pointed to 2935 when reporting the similar problem to GCC before we agreed that they should handle the case in the same way.

So this is about, the different behaviour of the two, regarding this situation, and with the reference of the issues I wanted to collect arguments for one or the other solution, just a consistent one.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@philnik777 philnik777 added the filesystem C++17 std::filesystem label Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla filesystem C++17 std::filesystem libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

3 participants