Add ReadFileOrDie test util function.

This fixes a compilation error; I'd left it out of the previous commit adding
a sqlite-test.cc method which depends on it.
This commit is contained in:
Scott Lamb 2016-01-08 21:39:42 -08:00
parent 9af7eb8c14
commit d00c0b2f12
4 changed files with 43 additions and 3 deletions

View File

@ -79,6 +79,19 @@ class RealFile : public File {
return 0; return 0;
} }
int Read(void *buf, size_t size, size_t *bytes_read) final {
ssize_t ret;
while ((ret = read(fd_, buf, size)) == -1 && errno == EINTR)
;
if (ret < 0) {
return errno;
}
*bytes_read = static_cast<size_t>(ret);
return 0;
}
int Stat(struct stat *buf) final { return (fstat(fd_, buf) < 0) ? errno : 0; }
int Sync() final { return (fsync(fd_) < 0) ? errno : 0; } int Sync() final { return (fsync(fd_) < 0) ? errno : 0; }
int Truncate(off_t length) final { int Truncate(off_t length) final {
@ -86,9 +99,6 @@ class RealFile : public File {
} }
int Write(re2::StringPiece data, size_t *bytes_written) final { int Write(re2::StringPiece data, size_t *bytes_written) final {
if (fd_ < 0) {
return EBADF;
}
ssize_t ret; ssize_t ret;
while ((ret = write(fd_, data.data(), data.size())) == -1 && errno == EINTR) while ((ret = write(fd_, data.data(), data.size())) == -1 && errno == EINTR)
; ;

View File

@ -71,6 +71,13 @@ class File {
// ftruncate(), returning 0 on success or errno>0 on failure. // ftruncate(), returning 0 on success or errno>0 on failure.
virtual int Truncate(off_t length) = 0; virtual int Truncate(off_t length) = 0;
// read(), returning 0 on success or errno>0 on failure.
// On success, |bytes_read| will be updated.
virtual int Read(void *buf, size_t count, size_t *bytes_read) = 0;
// fstat(), returning 0 on success or errno>0 on failure.
virtual int Stat(struct stat *buf) = 0;
// Write to the file, returning 0 on success or errno>0 on failure. // Write to the file, returning 0 on success or errno>0 on failure.
// On success, |bytes_written| will be updated. // On success, |bytes_written| will be updated.
virtual int Write(re2::StringPiece data, size_t *bytes_written) = 0; virtual int Write(re2::StringPiece data, size_t *bytes_written) = 0;

View File

@ -118,4 +118,24 @@ void WriteFileOrDie(const std::string &path, re2::StringPiece contents) {
CHECK_EQ(ret, 0) << "close " << path << ": " << strerror(ret); CHECK_EQ(ret, 0) << "close " << path << ": " << strerror(ret);
} }
std::string ReadFileOrDie(const std::string &path) {
std::unique_ptr<File> f;
int ret = GetRealFilesystem()->Open(path.c_str(), O_RDONLY, &f);
CHECK_EQ(ret, 0) << "open " << path << ": " << strerror(ret);
struct stat statbuf;
ret = f->Stat(&statbuf);
CHECK_EQ(ret, 0) << "fstat " << path << ": " << strerror(ret);
std::string out(statbuf.st_size, '0');
off_t bytes_read_total = 0;
size_t bytes_read;
while (bytes_read_total < statbuf.st_size) {
ret = f->Read(&out[bytes_read_total], statbuf.st_size - bytes_read_total,
&bytes_read);
CHECK_EQ(ret, 0) << "read " << path << ": " << strerror(ret);
CHECK_GT(bytes_read, 0);
bytes_read_total += bytes_read;
}
return out;
}
} // namespace moonfire_nvr } // namespace moonfire_nvr

View File

@ -46,6 +46,9 @@ std::string PrepareTempDirOrDie(const std::string &test_name);
// Write the given file contents to the given path, or die. // Write the given file contents to the given path, or die.
void WriteFileOrDie(const std::string &path, re2::StringPiece contents); void WriteFileOrDie(const std::string &path, re2::StringPiece contents);
// Read the contents of the given path, or die.
std::string ReadFileOrDie(const std::string &path);
// A scoped log sink for testing that the right log messages are sent. // A scoped log sink for testing that the right log messages are sent.
// Modelled after glog's "mock-log.h", which is not exported. // Modelled after glog's "mock-log.h", which is not exported.
// Use as follows: // Use as follows: