Bug ID: 86910
           Summary: std::filesystem::create_directories doesn't set error
                    code or throw while violating postcondition.
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot
          Reporter: ssh at pobox dot com
  Target Milestone: ---

std::filesystem::create_directories should create all directories that don't
exists in the given path. It is not an error if some of the directories exist.
But they must be directories to fulfil the postcondition. 

The current implementation doens't signal an error if it didn't create the
directory because a file existed with the same name, so the given postcondition
is_directory(p) is violated but no error occurs.

#include <filesystem>
#include <fstream>
#include <iostream>
#include <system_error>
namespace fs = std::filesystem;
int main()
    fs::path p("testxyz");
    if(!fs::exists(p)) {
        std::ofstream file(p);
    std::error_code ec;
    if(fs::create_directories(p, ec))
        std::cout << "created" << std::endl;
        std::cout << "didn't create" << std::endl;
        std::cerr << "postcondition failed!" << std::endl;
    std::cerr << "error code " << (ec?"set":"not set") << std::endl;

Compiled on Ubuntu 18.04 with

g++-8 -std=c++17 -o create_dirs create_directories.cpp -lstdc++fs

The expected output would be:

didn't create
postcondition failed!
error code set

but current code gives:

didn't create
postcondition failed!
error code not set

The interface without ec should throw, but doesn't.

Reply via email to