diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc --- a/llvm/lib/Support/Unix/Path.inc +++ b/llvm/lib/Support/Unix/Path.inc @@ -409,8 +409,15 @@ StringRef p = path.toNullTerminatedStringRef(path_storage); if (::mkdir(p.begin(), Perms) == -1) { - if (errno != EEXIST || !IgnoreExisting) - return std::error_code(errno, std::generic_category()); + if (errno == EEXIST && IgnoreExisting) { + // Only ignore existing directory. + bool is_dir; + if (std::error_code ec = is_directory(p, is_dir)) + return ec; + if (is_dir) + return std::error_code(); + } + return std::error_code(errno, std::generic_category()); } return std::error_code(); diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -1012,6 +1012,14 @@ // Restore umask to be safe. ::umask(OldUmask); + + // Test create directory on top of a file. + int FD; + SmallString<64> TempPath; + ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); + ::close(FD); + ASSERT_EQ(fs::create_directory(Twine(TempPath)), errc::file_exists); + #endif #ifdef _WIN32