diff --git a/src/build_log.cc b/src/build_log.cc index b814d3306c..890a3cd3b3 100644 --- a/src/build_log.cc +++ b/src/build_log.cc @@ -237,7 +237,7 @@ LoadStatus BuildLog::Load(const std::string& path, std::string* err) { } if (invalid_log_version) { fclose(file); - _unlink(path.c_str()); + platformAwareUnlink(path.c_str()); // Don't report this as a failure. A missing build log will cause // us to rebuild the outputs anyway. return LOAD_NOT_FOUND; @@ -373,7 +373,7 @@ bool BuildLog::Recompact(const std::string& path, const BuildLogUser& user, entries_.erase(output); fclose(f); - if (_unlink(path.c_str()) < 0) { + if (platformAwareUnlink(path.c_str()) < 0) { *err = strerror(errno); return false; } @@ -430,7 +430,7 @@ bool BuildLog::Restat(const StringPiece path, } fclose(f); - if (_unlink(path.str_) < 0) { + if (platformAwareUnlink(path.str_) < 0) { *err = strerror(errno); return false; } diff --git a/src/build_log_perftest.cc b/src/build_log_perftest.cc index 88b5e3dccd..869112f080 100644 --- a/src/build_log_perftest.cc +++ b/src/build_log_perftest.cc @@ -144,7 +144,7 @@ int main() { printf("min %dms max %dms avg %.1fms\n", min, max, total / times.size()); - _unlink(kTestFilename); + platformAwareUnlink(kTestFilename); return 0; } diff --git a/src/build_log_test.cc b/src/build_log_test.cc index 273347319e..47de8b5d0c 100644 --- a/src/build_log_test.cc +++ b/src/build_log_test.cc @@ -34,10 +34,10 @@ const char kTestFilename[] = "BuildLogTest-tempfile"; struct BuildLogTest : public StateTestWithBuiltinRules, public BuildLogUser { virtual void SetUp() { // In case a crashing test left a stale file behind. - _unlink(kTestFilename); + platformAwareUnlink(kTestFilename); } virtual void TearDown() { - _unlink(kTestFilename); + platformAwareUnlink(kTestFilename); } virtual bool IsPathDead(StringPiece s) const { return false; } }; diff --git a/src/clean_test.cc b/src/clean_test.cc index dd7628402b..39aede37a1 100644 --- a/src/clean_test.cc +++ b/src/clean_test.cc @@ -469,11 +469,11 @@ TEST_F(CleanTest, CleanDepFileAndRspFileWithSpaces) { struct CleanDeadTest : public CleanTest, public BuildLogUser{ virtual void SetUp() { // In case a crashing test left a stale file behind. - _unlink(kTestFilename); + platformAwareUnlink(kTestFilename); CleanTest::SetUp(); } virtual void TearDown() { - _unlink(kTestFilename); + platformAwareUnlink(kTestFilename); } virtual bool IsPathDead(StringPiece) const { return false; } }; diff --git a/src/deps_log.cc b/src/deps_log.cc index 483c238b2a..fa59ef20cc 100644 --- a/src/deps_log.cc +++ b/src/deps_log.cc @@ -179,7 +179,7 @@ LoadStatus DepsLog::Load(const string& path, State* state, string* err) { else *err = "bad deps log signature or version; starting over"; fclose(f); - _unlink(path.c_str()); + platformAwareUnlink(path.c_str()); // Don't report this as a failure. An empty deps log will cause // us to rebuild the outputs anyway. return LOAD_SUCCESS; @@ -331,7 +331,7 @@ bool DepsLog::Recompact(const string& path, string* err) { // OpenForWrite() opens for append. Make sure it's not appending to a // left-over file from a previous recompaction attempt that crashed somehow. - _unlink(temp_path.c_str()); + platformAwareUnlink(temp_path.c_str()); DepsLog new_log; if (!new_log.OpenForWrite(temp_path, err)) @@ -363,7 +363,7 @@ bool DepsLog::Recompact(const string& path, string* err) { deps_.swap(new_log.deps_); nodes_.swap(new_log.nodes_); - if (_unlink(path.c_str()) < 0) { + if (platformAwareUnlink(path.c_str()) < 0) { *err = strerror(errno); return false; } diff --git a/src/deps_log_test.cc b/src/deps_log_test.cc index 845a7743bf..61a9dcf029 100644 --- a/src/deps_log_test.cc +++ b/src/deps_log_test.cc @@ -33,9 +33,9 @@ const char kTestFilename[] = "DepsLogTest-tempfile"; struct DepsLogTest : public testing::Test { virtual void SetUp() { // In case a crashing test left a stale file behind. - _unlink(kTestFilename); + platformAwareUnlink(kTestFilename); } - virtual void TearDown() { _unlink(kTestFilename); } + virtual void TearDown() { platformAwareUnlink(kTestFilename); } }; TEST_F(DepsLogTest, WriteRead) { diff --git a/src/msvc_helper_main-win32.cc b/src/msvc_helper_main-win32.cc index 98bfa9e82f..972982c642 100644 --- a/src/msvc_helper_main-win32.cc +++ b/src/msvc_helper_main-win32.cc @@ -54,23 +54,23 @@ void WriteDepFileOrDie(const char* object_path, const CLParser& parse) { string depfile_path = string(object_path) + ".d"; FILE* depfile = fopen(depfile_path.c_str(), "w"); if (!depfile) { - _unlink(object_path); + platformAwareUnlink(object_path); Fatal("opening %s: %s", depfile_path.c_str(), GetLastErrorString().c_str()); } if (fprintf(depfile, "%s: ", object_path) < 0) { - _unlink(object_path); + platformAwareUnlink(object_path); fclose(depfile); - _unlink(depfile_path.c_str()); + platformAwareUnlink(depfile_path.c_str()); Fatal("writing %s", depfile_path.c_str()); } const set& headers = parse.includes_; for (set::const_iterator i = headers.begin(); i != headers.end(); ++i) { if (fprintf(depfile, "%s\n", EscapeForDepfile(*i).c_str()) < 0) { - _unlink(object_path); + platformAwareUnlink(object_path); fclose(depfile); - _unlink(depfile_path.c_str()); + platformAwareUnlink(depfile_path.c_str()); Fatal("writing %s", depfile_path.c_str()); } } diff --git a/src/test.cc b/src/test.cc index eb8c02e350..e9aaafa3bb 100644 --- a/src/test.cc +++ b/src/test.cc @@ -254,7 +254,7 @@ ScopedFilePath& ScopedFilePath::operator=(ScopedFilePath&& other) noexcept { ScopedFilePath::~ScopedFilePath() { if (!released_) { - _unlink(path_.c_str()); + platformAwareUnlink(path_.c_str()); } } diff --git a/src/util.h b/src/util.h index 3e56edd69e..b3869cbf4e 100644 --- a/src/util.h +++ b/src/util.h @@ -135,4 +135,12 @@ inline To FunctionCast(From from) { } #endif +inline int platformAwareUnlink(const char* filename) { + #ifdef _WIN32 + return _unlink(filename); + #else + return unlink(filename); + #endif +} + #endif // NINJA_UTIL_H_