[llvm-bugs] [Bug 41683] New: std::filesystem::create_directory and std::filesystem::create_directories have inconsistent error handling

via llvm-bugs llvm-bugs at lists.llvm.org
Wed May 1 01:02:39 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=41683

            Bug ID: 41683
           Summary: std::filesystem::create_directory and
                    std::filesystem::create_directories have inconsistent
                    error handling
           Product: libc++
           Version: 8.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ssh at pobox.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

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 https://github.com/cplusplus/draft/issues/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!

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190501/8f7723fe/attachment.html>


More information about the llvm-bugs mailing list