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

Commit 858386d

Browse files
committed
Expose more details in FileReader::ReadFile signature
Return a status so callers can distinguish a missing file from an empty file. This allows our VirtualFileSystem test infrastructure to report as missing any file for which it has no entry.
1 parent f9487ac commit 858386d

7 files changed

+59
-24
lines changed

src/build.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -871,9 +871,17 @@ bool Builder::ExtractDeps(CommandRunner::Result* result,
871871
return false;
872872
}
873873

874-
string content = disk_interface_->ReadFile(depfile, err);
875-
if (!err->empty())
874+
// Read depfile content. Treat a missing depfile as empty.
875+
string content;
876+
switch (disk_interface_->ReadFile(depfile, &content, err)) {
877+
case DiskInterface::Okay:
878+
break;
879+
case DiskInterface::NotFound:
880+
err->clear();
881+
break;
882+
case DiskInterface::OtherError:
876883
return false;
884+
}
877885
if (content.empty())
878886
return true;
879887

src/disk_interface.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ bool RealDiskInterface::MakeDir(const string& path) {
229229
return true;
230230
}
231231

232-
string RealDiskInterface::ReadFile(const string& path, string* err) {
233-
string contents;
234-
int ret = ::ReadFile(path, &contents, err);
235-
if (ret == -ENOENT) {
236-
// Swallow ENOENT.
237-
err->clear();
232+
FileReader::Status RealDiskInterface::ReadFile(const string& path,
233+
string* contents,
234+
string* err) {
235+
switch (::ReadFile(path, contents, err)) {
236+
case 0: return Okay;
237+
case -ENOENT: return NotFound;
238+
default: return OtherError;
238239
}
239-
return contents;
240240
}
241241

242242
int RealDiskInterface::RemoveFile(const string& path) {

src/disk_interface.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ using namespace std;
2626
struct FileReader {
2727
virtual ~FileReader() {}
2828

29-
/// Read a file to a string. Fill in |err| on error.
30-
virtual string ReadFile(const string& path, string* err) = 0;
29+
/// Result of ReadFile.
30+
enum Status {
31+
Okay,
32+
NotFound,
33+
OtherError
34+
};
35+
36+
/// Read and store in given string. On success, return Okay.
37+
/// On error, return another Status and fill |err|.
38+
virtual Status ReadFile(const string& path, string* contents,
39+
string* err) = 0;
3140
};
3241

3342
/// Interface for accessing the disk.
@@ -69,7 +78,7 @@ struct RealDiskInterface : public DiskInterface {
6978
virtual TimeStamp Stat(const string& path, string* err) const;
7079
virtual bool MakeDir(const string& path);
7180
virtual bool WriteFile(const string& path, const string& contents);
72-
virtual string ReadFile(const string& path, string* err);
81+
virtual Status ReadFile(const string& path, string* contents, string* err);
7382
virtual int RemoveFile(const string& path);
7483

7584
/// Whether stat information can be cached. Only has an effect on Windows.

src/disk_interface_test.cc

+11-5
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,12 @@ TEST_F(DiskInterfaceTest, StatCache) {
157157

158158
TEST_F(DiskInterfaceTest, ReadFile) {
159159
string err;
160-
EXPECT_EQ("", disk_.ReadFile("foobar", &err));
161-
EXPECT_EQ("", err);
160+
std::string content;
161+
ASSERT_EQ(DiskInterface::NotFound,
162+
disk_.ReadFile("foobar", &content, &err));
163+
EXPECT_EQ("", content);
164+
EXPECT_NE("", err); // actual value is platform-specific
165+
err.clear();
162166

163167
const char* kTestFile = "testfile";
164168
FILE* f = fopen(kTestFile, "wb");
@@ -167,7 +171,9 @@ TEST_F(DiskInterfaceTest, ReadFile) {
167171
fprintf(f, "%s", kTestContent);
168172
ASSERT_EQ(0, fclose(f));
169173

170-
EXPECT_EQ(kTestContent, disk_.ReadFile(kTestFile, &err));
174+
ASSERT_EQ(DiskInterface::Okay,
175+
disk_.ReadFile(kTestFile, &content, &err));
176+
EXPECT_EQ(kTestContent, content);
171177
EXPECT_EQ("", err);
172178
}
173179

@@ -208,9 +214,9 @@ struct StatTest : public StateTestWithBuiltinRules,
208214
assert(false);
209215
return false;
210216
}
211-
virtual string ReadFile(const string& path, string* err) {
217+
virtual Status ReadFile(const string& path, string* contents, string* err) {
212218
assert(false);
213-
return "";
219+
return NotFound;
214220
}
215221
virtual int RemoveFile(const string& path) {
216222
assert(false);

src/graph.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,15 @@ bool ImplicitDepLoader::LoadDeps(Edge* edge, string* err) {
395395
bool ImplicitDepLoader::LoadDepFile(Edge* edge, const string& path,
396396
string* err) {
397397
METRIC_RECORD("depfile load");
398-
string content = disk_interface_->ReadFile(path, err);
399-
if (!err->empty()) {
398+
// Read depfile content. Treat a missing depfile as empty.
399+
string content;
400+
switch (disk_interface_->ReadFile(path, &content, err)) {
401+
case DiskInterface::Okay:
402+
break;
403+
case DiskInterface::NotFound:
404+
err->clear();
405+
break;
406+
case DiskInterface::OtherError:
400407
*err = "loading '" + path + "': " + *err;
401408
return false;
402409
}

src/test.cc

+9-4
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,17 @@ bool VirtualFileSystem::MakeDir(const string& path) {
164164
return true; // success
165165
}
166166

167-
string VirtualFileSystem::ReadFile(const string& path, string* err) {
167+
FileReader::Status VirtualFileSystem::ReadFile(const string& path,
168+
string* contents,
169+
string* err) {
168170
files_read_.push_back(path);
169171
FileMap::iterator i = files_.find(path);
170-
if (i != files_.end())
171-
return i->second.contents;
172-
return "";
172+
if (i != files_.end()) {
173+
*contents = i->second.contents;
174+
return Okay;
175+
}
176+
*err = strerror(ENOENT);
177+
return NotFound;
173178
}
174179

175180
int VirtualFileSystem::RemoveFile(const string& path) {

src/test.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct VirtualFileSystem : public DiskInterface {
145145
virtual TimeStamp Stat(const string& path, string* err) const;
146146
virtual bool WriteFile(const string& path, const string& contents);
147147
virtual bool MakeDir(const string& path);
148-
virtual string ReadFile(const string& path, string* err);
148+
virtual Status ReadFile(const string& path, string* contents, string* err);
149149
virtual int RemoveFile(const string& path);
150150

151151
/// An entry for a single in-memory file.

0 commit comments

Comments
 (0)