Skip to content

Commit

Permalink
Update CreateDirectory to skip the existence check on Windows.
Browse files Browse the repository at this point in the history
* This is a temporary workaround to avoid freeze of the host application.
* #1076

PiperOrigin-RevId: 685979550
  • Loading branch information
hiroyuki-komatsu committed Oct 15, 2024
1 parent 1a88b2f commit 90b0e0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 7 additions & 1 deletion src/base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/base/file_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 90b0e0b

Please sign in to comment.