mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-11-23 02:57:43 -05:00
Small helper for writing sample files safely.
Handles partial writes + checksumming.
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user