From 90b0e0b3c0e32db76a91256bfae4b6104a0a1545 Mon Sep 17 00:00:00 2001 From: Hiroyuki Komatsu Date: Tue, 15 Oct 2024 06:45:17 +0000 Subject: [PATCH] Update CreateDirectory to skip the existence check on Windows. * This is a temporary workaround to avoid freeze of the host application. * https://github.com/google/mozc/issues/1076 PiperOrigin-RevId: 685979550 --- src/base/file_util.cc | 8 ++++++++ src/base/file_util.h | 8 +++++++- src/base/file_util_test.cc | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/base/file_util.cc b/src/base/file_util.cc index 4decbd5de..1c75bbd1a 100644 --- a/src/base/file_util.cc +++ b/src/base/file_util.cc @@ -198,10 +198,18 @@ absl::Status FileUtil::CreateDirectory(const std::string &path) { } absl::Status FileUtilImpl::CreateDirectory(const std::string &path) const { +#if !defined(_WIN32) + // On Windows, this check is skipped to avoid freeze of the host application. + // This platform dependent behavior is a temporary solution to avoid + // freeze of the host application. + // https://github.com/google/mozc/issues/1076 + // // If the path already exists, returns OkStatus and does nothing. if (const absl::Status status = DirectoryExists(path); status.ok()) { return absl::OkStatus(); } +#endif // !_WIN32 + #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 de2b43f36..3249eae5f 100644 --- a/src/base/file_util.h +++ b/src/base/file_util.h @@ -92,7 +92,13 @@ 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. + // If the directory already exists: + // - On Windows: returns an error status. + // - Others: returns OkStatus and does nothing. + // + // The above platform dependent behavior is a temporary solution to avoid + // freeze of the host application. + // https://github.com/google/mozc/issues/1076 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 ed6df61ae..f8675af39 100644 --- a/src/base/file_util_test.cc +++ b/src/base/file_util_test.cc @@ -74,8 +74,14 @@ TEST(FileUtilTest, CreateDirectory) { EXPECT_OK(FileUtil::CreateDirectory(dirpath)); EXPECT_OK(FileUtil::DirectoryExists(dirpath)); +#if !defined(_WIN32) + // On Windows, CreateDirectory does not return OK if the directory already + // exists. See the implementation of CreateDirectory in file_util.cc. + // https://github.com/google/mozc/issues/1076 + // // Create the same directory again. EXPECT_OK(FileUtil::CreateDirectory(dirpath)); +#endif // !_WIN32 // Delete the directory. ASSERT_OK(FileUtil::RemoveDirectory(dirpath));