<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - std::filesystem::create_directory and std::filesystem::create_directories have inconsistent error handling"
   href="https://bugs.llvm.org/show_bug.cgi?id=41683">41683</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::filesystem::create_directory and std::filesystem::create_directories have inconsistent error handling
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ssh@pobox.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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 <a href="https://wandbox.org/permlink/Wa0Ie40k24rBwGmY">https://wandbox.org/permlink/Wa0Ie40k24rBwGmY</a>)

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 (<a href="https://cplusplus.github.io/LWG/issue2935">https://cplusplus.github.io/LWG/issue2935</a>) 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 <a href="https://github.com/cplusplus/draft/issues/2703">https://github.com/cplusplus/draft/issues/2703</a>).

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!</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>