diff --git a/src/base/file_util.cc b/src/base/file_util.cc index bd85d095b..7782f8301 100644 --- a/src/base/file_util.cc +++ b/src/base/file_util.cc @@ -198,6 +198,10 @@ absl::Status FileUtil::CreateDirectory(const std::string &path) { } absl::Status FileUtilImpl::CreateDirectory(const std::string &path) const { + // If the path already exists, returns OkStatus and does nothing. + if (const absl::Status status = DirectoryExists(path); status.ok()) { + return absl::OkStatus(); + } #if defined(_WIN32) const std::wstring wide = win32::Utf8ToWide(path); if (wide.empty()) { diff --git a/src/base/file_util.h b/src/base/file_util.h index fb4fee217..de2b43f36 100644 --- a/src/base/file_util.h +++ b/src/base/file_util.h @@ -92,6 +92,7 @@ class FileUtil { ~FileUtil() = delete; // Creates a directory. Does not create directories in the way to the path. + // If the directory already exists, returns OkStatus and does nothing. static absl::Status CreateDirectory(const std::string &path); // Removes an empty directory. diff --git a/src/base/file_util_test.cc b/src/base/file_util_test.cc index 12dd23363..4fda35bc0 100644 --- a/src/base/file_util_test.cc +++ b/src/base/file_util_test.cc @@ -74,6 +74,9 @@ TEST(FileUtilTest, CreateDirectory) { EXPECT_OK(FileUtil::CreateDirectory(dirpath)); EXPECT_OK(FileUtil::DirectoryExists(dirpath)); + // Create the same directory again. + EXPECT_OK(FileUtil::CreateDirectory(dirpath)); + // Delete the directory. ASSERT_OK(FileUtil::RemoveDirectory(dirpath)); ASSERT_FALSE(FileUtil::FileExists(dirpath).ok());