From a33b81305889e45823b36b3c96cc8db97e5d3b41 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 12 Feb 2021 10:17:36 -0500 Subject: disk_interface: Improve wrapping of comment in RemoveFile --- src/disk_interface.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/disk_interface.cc b/src/disk_interface.cc index 8d4cc7f..4753201 100644 --- a/src/disk_interface.cc +++ b/src/disk_interface.cc @@ -272,9 +272,9 @@ int RealDiskInterface::RemoveFile(const string& path) { return 1; } if (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 + // On non-Windows systems, remove() will happily delete read-only files. + // On Windows Ninja should behave the same: + // https://github.com/ninja-build/ninja/issues/1886 SetFileAttributes(path.c_str(), attributes & ~FILE_ATTRIBUTE_READONLY); } if (!DeleteFile(path.c_str())) { -- cgit v0.12 From c7e3e5ef45c1877d12d6365fbfb68fa840c1518b Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 12 Feb 2021 11:34:22 -0500 Subject: disk_interface: Do not query bits of INVALID_FILE_ATTRIBUTES --- src/disk_interface.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/disk_interface.cc b/src/disk_interface.cc index 4753201..6290680 100644 --- a/src/disk_interface.cc +++ b/src/disk_interface.cc @@ -267,11 +267,11 @@ FileReader::Status RealDiskInterface::ReadFile(const string& path, int RealDiskInterface::RemoveFile(const string& path) { #ifdef _WIN32 DWORD attributes = GetFileAttributes(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) { + if (GetLastError() == ERROR_FILE_NOT_FOUND) { + return 1; + } + } else if (attributes & FILE_ATTRIBUTE_READONLY) { // On non-Windows systems, remove() will happily delete read-only files. // On Windows Ninja should behave the same: // https://github.com/ninja-build/ninja/issues/1886 -- cgit v0.12 From 5392e0e7bf7095039ae6bd69c0a8dfc77ef80a05 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 12 Feb 2021 11:37:23 -0500 Subject: disk_interface: Restore toleration of missing files in RemoveFile on Windows Revise the logic from commit 2d7f7e55 (Delete read-only files on Windows, too, 2020-12-07) to check if `GetFileAttributes` or `DeleteFile` failed due either variant of the file/path-not-found error. Issue: #1886 --- src/disk_interface.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/disk_interface.cc b/src/disk_interface.cc index 6290680..a9497cb 100644 --- a/src/disk_interface.cc +++ b/src/disk_interface.cc @@ -268,16 +268,23 @@ int RealDiskInterface::RemoveFile(const string& path) { #ifdef _WIN32 DWORD attributes = GetFileAttributes(path.c_str()); if (attributes == INVALID_FILE_ATTRIBUTES) { - if (GetLastError() == ERROR_FILE_NOT_FOUND) { + DWORD win_err = GetLastError(); + if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) { return 1; } } else if (attributes & FILE_ATTRIBUTE_READONLY) { // On non-Windows systems, remove() will happily delete read-only files. // On Windows Ninja should behave the same: // https://github.com/ninja-build/ninja/issues/1886 + // Skip error checking. If this fails, accept whatever happens below. SetFileAttributes(path.c_str(), attributes & ~FILE_ATTRIBUTE_READONLY); } if (!DeleteFile(path.c_str())) { + DWORD win_err = GetLastError(); + if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) { + return 1; + } + // Report as remove(), not DeleteFile(), for cross-platform consistency. Error("remove(%s): %s", path.c_str(), GetLastErrorString().c_str()); return -1; } -- cgit v0.12