From ed11516a03583602fbaae2748f3d428542096136 Mon Sep 17 00:00:00 2001 From: Fredrik Medley Date: Fri, 15 Dec 2017 21:37:37 +0100 Subject: Fix disk_interface_test.cc on Windows for 64-bit timestamp subdir/subsubdir/.. seems to get the time of subdir/subsubdir on NTFS (Windows 7), not the time of subdir. --- src/disk_interface.cc | 10 ++++++++++ src/disk_interface_test.cc | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/disk_interface.cc b/src/disk_interface.cc index 4b4c4c7..aba6ab2 100644 --- a/src/disk_interface.cc +++ b/src/disk_interface.cc @@ -112,6 +112,11 @@ bool StatAllFilesInDir(const string& dir, map* stamps, } do { string lowername = ffd.cFileName; + if (lowername == "..") { + // Seems to just copy the timestamp for ".." from ".", which is wrong. + // This is the case at least on NTFS under Windows 7. + continue; + } transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower); stamps->insert(make_pair(lowername, TimeStampFromFileTime(ffd.ftLastWriteTime))); @@ -164,6 +169,11 @@ TimeStamp RealDiskInterface::Stat(const string& path, string* err) const { string dir = DirName(path); string base(path.substr(dir.size() ? dir.size() + 1 : 0)); + if (base == "..") { + // StatAllFilesInDir does not report any information for base = "..". + base = "."; + dir = path; + } transform(dir.begin(), dir.end(), dir.begin(), ::tolower); transform(base.begin(), base.end(), base.begin(), ::tolower); diff --git a/src/disk_interface_test.cc b/src/disk_interface_test.cc index d7fb8f8..81aa63a 100644 --- a/src/disk_interface_test.cc +++ b/src/disk_interface_test.cc @@ -87,6 +87,8 @@ TEST_F(DiskInterfaceTest, StatExistingDir) { string err; ASSERT_TRUE(disk_.MakeDir("subdir")); ASSERT_TRUE(disk_.MakeDir("subdir/subsubdir")); + EXPECT_GT(disk_.Stat("..", &err), 1); + EXPECT_EQ("", err); EXPECT_GT(disk_.Stat(".", &err), 1); EXPECT_EQ("", err); EXPECT_GT(disk_.Stat("subdir", &err), 1); @@ -105,7 +107,6 @@ TEST_F(DiskInterfaceTest, StatExistingDir) { #ifdef _WIN32 TEST_F(DiskInterfaceTest, StatCache) { string err; - disk_.AllowStatCache(true); ASSERT_TRUE(Touch("file1")); ASSERT_TRUE(Touch("fiLE2")); @@ -115,6 +116,10 @@ TEST_F(DiskInterfaceTest, StatCache) { ASSERT_TRUE(Touch("subdir\\SUBFILE2")); ASSERT_TRUE(Touch("subdir\\SUBFILE3")); + disk_.AllowStatCache(false); + TimeStamp parent_stat_uncached = disk_.Stat("..", &err); + disk_.AllowStatCache(true); + EXPECT_GT(disk_.Stat("FIle1", &err), 1); EXPECT_EQ("", err); EXPECT_GT(disk_.Stat("file1", &err), 1); @@ -125,6 +130,8 @@ TEST_F(DiskInterfaceTest, StatCache) { EXPECT_GT(disk_.Stat("sUbdir\\suBFile1", &err), 1); EXPECT_EQ("", err); + EXPECT_GT(disk_.Stat("..", &err), 1); + EXPECT_EQ("", err); EXPECT_GT(disk_.Stat(".", &err), 1); EXPECT_EQ("", err); EXPECT_GT(disk_.Stat("subdir", &err), 1); @@ -138,6 +145,8 @@ TEST_F(DiskInterfaceTest, StatCache) { EXPECT_EQ(disk_.Stat("subdir", &err), disk_.Stat("subdir/subsubdir/..", &err)); EXPECT_EQ("", err); + EXPECT_EQ(disk_.Stat("..", &err), parent_stat_uncached); + EXPECT_EQ("", err); EXPECT_EQ(disk_.Stat("subdir/subsubdir", &err), disk_.Stat("subdir/subsubdir/.", &err)); EXPECT_EQ("", err); -- cgit v0.12 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