From 6c864097ef11da366fb4070e6ab9f34d6a293766 Mon Sep 17 00:00:00 2001 From: Fredrik Medley Date: Tue, 19 Dec 2017 14:49:07 +0100 Subject: Fix stat when subdirectory is a file Make sure that stat on Windows, both with and without cache, returns "missing file" when running stat on notadir/foo where notadir is a file. --- src/disk_interface.cc | 3 ++- src/disk_interface_test.cc | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/disk_interface.cc b/src/disk_interface.cc index aba6ab2..0a5991d 100644 --- a/src/disk_interface.cc +++ b/src/disk_interface.cc @@ -105,7 +105,8 @@ bool StatAllFilesInDir(const string& dir, map* stamps, if (find_handle == INVALID_HANDLE_VALUE) { DWORD win_err = GetLastError(); - if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) + if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND || + win_err == ERROR_DIRECTORY) // File and not a directory return true; *err = "FindFirstFileExA(" + dir + "): " + GetLastErrorString(); return false; diff --git a/src/disk_interface_test.cc b/src/disk_interface_test.cc index 81aa63a..5f7e468 100644 --- a/src/disk_interface_test.cc +++ b/src/disk_interface_test.cc @@ -63,6 +63,27 @@ TEST_F(DiskInterfaceTest, StatMissingFile) { EXPECT_EQ("", err); } +#ifdef _WIN32 +TEST_F(DiskInterfaceTest, StatMissingFileWithCache) { + string err; + disk_.AllowStatCache(true); + + EXPECT_EQ(0, disk_.Stat("nosuchfile", &err)); + EXPECT_EQ("", err); + + // On Windows, the errno for a file in a nonexistent directory + // is different. + EXPECT_EQ(0, disk_.Stat("nosuchdir/nosuchfile", &err)); + EXPECT_EQ("", err); + + // On POSIX systems, the errno is different if a component of the + // path prefix is not a directory. + ASSERT_TRUE(Touch("notadir")); + EXPECT_EQ(0, disk_.Stat("notadir/nosuchfile", &err)); + EXPECT_EQ("", err); +} +#endif + TEST_F(DiskInterfaceTest, StatBadPath) { string err; #ifdef _WIN32 -- cgit v0.12