Small helper for writing sample files safely.

Handles partial writes + checksumming.
This commit is contained in:
Scott Lamb
2016-01-09 17:16:55 -08:00
parent 30e0f73ae0
commit 48d0473a4c
5 changed files with 306 additions and 6 deletions

View File

@@ -45,13 +45,14 @@
#include <event2/buffer.h>
#include <event2/http.h>
#include <glog/logging.h>
#include <gmock/gmock.h>
#include <re2/stringpiece.h>
#include "common.h"
namespace moonfire_nvr {
// Represents an open file. All methods but Close() are thread-safe.
// Represents an open file descriptor. All methods but Close() are thread-safe.
class File {
public:
// Close the file, ignoring the result.
@@ -61,11 +62,10 @@ class File {
// Already closed is considered a success.
virtual int Close() = 0;
// fsync(), returning 0 on success or errno>0 on failure.
virtual int Sync() = 0;
// ftruncate(), returning 0 on success or errno>0 on failure.
virtual int Truncate(off_t length) = 0;
// openat(), returning 0 on success or errno>0 on failure.
virtual int Open(const char *path, int flags, std::unique_ptr<File> *f) = 0;
virtual int Open(const char *path, int flags, mode_t mode,
std::unique_ptr<File> *f) = 0;
// read(), returning 0 on success or errno>0 on failure.
// On success, |bytes_read| will be updated.
@@ -74,11 +74,48 @@ class File {
// fstat(), returning 0 on success or errno>0 on failure.
virtual int Stat(struct stat *buf) = 0;
// fsync(), returning 0 on success or errno>0 on failure.
virtual int Sync() = 0;
// ftruncate(), returning 0 on success or errno>0 on failure.
virtual int Truncate(off_t length) = 0;
// Write to the file, returning 0 on success or errno>0 on failure.
// On success, |bytes_written| will be updated.
virtual int Write(re2::StringPiece data, size_t *bytes_written) = 0;
};
class MockFile : public File {
public:
MOCK_METHOD0(Close, int());
// Open is wrapped here because gmock's SetArgPointee doesn't work well with
// std::unique_ptr.
int Open(const char *path, int flags, std::unique_ptr<File> *f) final {
File *f_tmp = nullptr;
int ret = OpenRaw(path, flags, &f_tmp);
f->reset(f_tmp);
return ret;
}
int Open(const char *path, int flags, mode_t mode,
std::unique_ptr<File> *f) final {
File *f_tmp = nullptr;
int ret = OpenRaw(path, flags, mode, &f_tmp);
f->reset(f_tmp);
return ret;
}
MOCK_METHOD3(OpenRaw, int(const char *, int, File **));
MOCK_METHOD4(OpenRaw, int(const char *, int, mode_t, File **));
MOCK_METHOD3(Read, int(void *, size_t, size_t *));
MOCK_METHOD1(Stat, int(struct stat *));
MOCK_METHOD0(Sync, int());
MOCK_METHOD2(Write, int(re2::StringPiece, size_t *));
MOCK_METHOD1(Truncate, int(off_t));
};
// Interface to the local filesystem. There's typically one per program,
// but it's an abstract class for testability. Thread-safe.
class Filesystem {