Skip to content

Commit

Permalink
disk_interface: Restore toleration of missing files in RemoveFile on …
Browse files Browse the repository at this point in the history
…Windows

Revise the logic from commit 2d7f7e5 (Delete read-only files on
Windows, too, 2020-12-07) to check if `DeleteFile` failed due to the
file already missing.

Issue: ninja-build#1886
  • Loading branch information
bradking committed Feb 12, 2021
1 parent 02450a8 commit d2baa2c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/disk_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,18 @@ FileReader::Status RealDiskInterface::ReadFile(const string& path,
int RealDiskInterface::RemoveFile(const string& path) {
#ifdef _WIN32
DWORD attributes = GetFileAttributesA(path.c_str());
if (attributes == INVALID_FILE_ATTRIBUTES &&
GetLastError() == ERROR_FILE_NOT_FOUND) {
return 1;
}
if (attributes & FILE_ATTRIBUTE_READONLY) {
if (attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_READONLY)) {
// On non-Windows systems, remove() will happily delete read-only files.
// On Windows Ninja should behave the same.
// See https://github.com/ninja-build/ninja/issues/1886
SetFileAttributesA(path.c_str(), attributes & ~FILE_ATTRIBUTE_READONLY);
}
if (!DeleteFileA(path.c_str())) {
DWORD win_err = GetLastError();
if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) {
return 1;
}
Error("DeleteFileA(%s): %s", path.c_str(), GetLastErrorString().c_str());
return -1;
}
Expand Down

0 comments on commit d2baa2c

Please sign in to comment.