Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 4eb8309

Browse files
committedJun 15, 2014
add some statcache tests
1 parent 726afc8 commit 4eb8309

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed
 

‎src/disk_interface.cc

+11-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ TimeStamp StatSingleFile(const string& path, bool quiet) {
8181
return TimeStampFromFileTime(attrs.ftLastWriteTime);
8282
}
8383

84-
void StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
84+
bool StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
8585
bool quiet) {
8686
// FindExInfoBasic is 30% faster than FindExInfoStandard.
8787
WIN32_FIND_DATAA ffd;
@@ -90,11 +90,13 @@ void StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
9090

9191
if (hFind == INVALID_HANDLE_VALUE) {
9292
DWORD err = GetLastError();
93-
if (err != ERROR_FILE_NOT_FOUND && err != ERROR_PATH_NOT_FOUND && !quiet) {
93+
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
94+
return true;
95+
if (!quiet) {
9496
Error("FindFirstFileExA(%s): %s", dir.c_str(),
9597
GetLastErrorString().c_str());
9698
}
97-
return;
99+
return false;
98100
}
99101
do {
100102
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
@@ -105,6 +107,7 @@ void StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
105107
TimeStampFromFileTime(ffd.ftLastWriteTime)));
106108
} while (FindNextFileA(hFind, &ffd));
107109
FindClose(hFind);
110+
return true;
108111
}
109112

110113
} // namespace
@@ -153,7 +156,10 @@ TimeStamp RealDiskInterface::Stat(const string& path) {
153156
Cache::iterator ci = cache_.find(dir);
154157
if (ci == cache_.end()) {
155158
DirCache* dc = new DirCache;
156-
StatAllFilesInDir(dir.empty() ? "." : dir, dc, quiet_);
159+
if (!StatAllFilesInDir(dir.empty() ? "." : dir, dc, quiet_)) {
160+
delete dc;
161+
return -1;
162+
}
157163
ci = cache_.insert(make_pair(dir, dc)).first;
158164
}
159165
DirCache::iterator di = ci->second->find(base);
@@ -231,7 +237,7 @@ int RealDiskInterface::RemoveFile(const string& path) {
231237
}
232238
}
233239

234-
void RealDiskInterface::AllowCache(bool allow) {
240+
void RealDiskInterface::AllowStatCache(bool allow) {
235241
#ifdef _WIN32
236242
use_cache_ = allow;
237243
if (!use_cache_)

‎src/disk_interface.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ struct RealDiskInterface : public DiskInterface {
7272
bool quiet_;
7373

7474
/// Whether stat information can be cached.
75-
void AllowCache(bool allow);
75+
void AllowStatCache(bool allow);
76+
77+
private:
7678
#ifdef _WIN32
7779
/// Whether stat information can be cached.
7880
bool use_cache_;

‎src/disk_interface_test.cc

+27
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,33 @@ TEST_F(DiskInterfaceTest, StatExistingFile) {
7676
EXPECT_GT(disk_.Stat("file"), 1);
7777
}
7878

79+
#ifdef _WIN32
80+
TEST_F(DiskInterfaceTest, StatCache) {
81+
disk_.AllowStatCache(true);
82+
83+
ASSERT_TRUE(Touch("file1"));
84+
ASSERT_TRUE(Touch("fiLE2"));
85+
ASSERT_TRUE(disk_.MakeDir("subdir"));
86+
ASSERT_TRUE(Touch("subdir\\subfile1"));
87+
ASSERT_TRUE(Touch("subdir\\SUBFILE2"));
88+
ASSERT_TRUE(Touch("subdir\\SUBFILE3"));
89+
90+
EXPECT_GT(disk_.Stat("FIle1"), 1);
91+
EXPECT_GT(disk_.Stat("file1"), 1);
92+
93+
EXPECT_GT(disk_.Stat("subdir/subfile2"), 1);
94+
EXPECT_GT(disk_.Stat("sUbdir\\suBFile1"), 1);
95+
96+
// Test error cases.
97+
disk_.quiet_ = true;
98+
string bad_path("cc:\\foo");
99+
EXPECT_EQ(-1, disk_.Stat(bad_path));
100+
EXPECT_EQ(-1, disk_.Stat(bad_path));
101+
EXPECT_EQ(0, disk_.Stat("nosuchfile"));
102+
EXPECT_EQ(0, disk_.Stat("nosuchdir/nosuchfile"));
103+
}
104+
#endif
105+
79106
TEST_F(DiskInterfaceTest, ReadFile) {
80107
string err;
81108
EXPECT_EQ("", disk_.ReadFile("foobar", &err));

‎src/ninja.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ int NinjaMain::RunBuild(int argc, char** argv) {
889889
return 1;
890890
}
891891

892-
disk_interface_.AllowCache(g_experimental_win_statcache);
892+
disk_interface_.AllowStatCache(g_experimental_win_statcache);
893893

894894
Builder builder(&state_, config_, &build_log_, &deps_log_, &disk_interface_);
895895
for (size_t i = 0; i < targets.size(); ++i) {
@@ -905,7 +905,7 @@ int NinjaMain::RunBuild(int argc, char** argv) {
905905
}
906906

907907
// Make sure restat rules do not see stale timestamps.
908-
disk_interface_.AllowCache(false);
908+
disk_interface_.AllowStatCache(false);
909909

910910
if (builder.AlreadyUpToDate()) {
911911
printf("ninja: no work to do.\n");

0 commit comments

Comments
 (0)
This repository has been archived.