Skip to content

Commit

Permalink
Created a function to inject the correct unlink function dending on t…
Browse files Browse the repository at this point in the history
…he platform.
  • Loading branch information
atamagaii committed Jan 21, 2025
1 parent b008588 commit d6ba5ed
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/build_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/build_log_perftest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/build_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};
Expand Down
4 changes: 2 additions & 2 deletions src/clean_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};
Expand Down
6 changes: 3 additions & 3 deletions src/deps_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/deps_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/msvc_helper_main-win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>& headers = parse.includes_;
for (set<string>::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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ ScopedFilePath& ScopedFilePath::operator=(ScopedFilePath&& other) noexcept {

ScopedFilePath::~ScopedFilePath() {
if (!released_) {
_unlink(path_.c_str());
platformAwareUnlink(path_.c_str());
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_

0 comments on commit d6ba5ed

Please sign in to comment.