Skip to content

Commit

Permalink
Revert "Fix race condition in MakeDirs (#644)"
Browse files Browse the repository at this point in the history
This reverts commit ebd6175.
  • Loading branch information
keith committed Jan 22, 2022
1 parent 21ef740 commit 2e7b9a5
Showing 1 changed file with 2 additions and 33 deletions.
35 changes: 2 additions & 33 deletions tools/common/file_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <sys/types.h>
#include <unistd.h>

#include <iostream>
#include <string>

#ifdef __APPLE__
Expand Down Expand Up @@ -101,13 +100,7 @@ bool MakeDirs(const std::string &path, int mode) {
struct stat dir_stats;
if (stat(path.c_str(), &dir_stats) == 0) {
// Return true if the directory already exists.
if (!S_ISDIR(dir_stats.st_mode)) {
std::cerr << "error: path exists and isn't a directory "
<< strerror(errno) << " (" << path.c_str() << ") \n";
return false;
} else {
return true;
}
return S_ISDIR(dir_stats.st_mode);
}

// Recurse to create the parent directory.
Expand All @@ -116,29 +109,5 @@ bool MakeDirs(const std::string &path, int mode) {
}

// Create the directory that was requested.
int mkdir_ret = mkdir(path.c_str(), mode);
if (mkdir_ret == 0) {
return true;
}

// Save the mkdir errno for better reporting.
int mkdir_errno = errno;

// Deal with a race condition when 2 `MakeDirs` are running at the same time:
// one `mkdir` invocation will fail in each recursive call at different
// points. Don't recurse here to avoid an infinite loop in failure cases.
if (errno == EEXIST && stat(path.c_str(), &dir_stats) == 0) {
if (!S_ISDIR(dir_stats.st_mode)) {
std::cerr << "error: path exists and isn't a directory "
<< strerror(errno) << " (" << path.c_str() << ") \n";
return false;
} else {
// Return true if the directory already exists.
return true;
}
}

std::cerr << "error:" << strerror(mkdir_errno) << " (" << path.c_str()
<< ") \n";
return false;
return mkdir(path.c_str(), mode) == 0;
}

0 comments on commit 2e7b9a5

Please sign in to comment.