mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-01-27 14:43:19 -05:00
read-only operation, database locking
These are meant to ease safe side-by-side testing with the upcoming Rust implementation.
This commit is contained in:
parent
9f087eefa9
commit
b50050358c
@ -36,6 +36,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/file.h>
|
||||||
#include <sys/queue.h>
|
#include <sys/queue.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -86,6 +87,10 @@ class RealFile : public File {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Lock(int operation) final {
|
||||||
|
return (flock(fd_, operation) < 0) ? errno : 0;
|
||||||
|
}
|
||||||
|
|
||||||
int Open(const char *path, int flags, int *fd) final {
|
int Open(const char *path, int flags, int *fd) final {
|
||||||
return Open(path, flags, 0, fd);
|
return Open(path, flags, 0, fd);
|
||||||
}
|
}
|
||||||
|
@ -67,6 +67,9 @@ class File {
|
|||||||
// Already closed is considered a success.
|
// Already closed is considered a success.
|
||||||
virtual int Close() = 0;
|
virtual int Close() = 0;
|
||||||
|
|
||||||
|
// flock(), returning 0 on success or errno>0 on failure.
|
||||||
|
virtual int Lock(int operation) = 0;
|
||||||
|
|
||||||
// openat(), returning 0 on success or errno>0 on failure.
|
// openat(), returning 0 on success or errno>0 on failure.
|
||||||
virtual int Open(const char *path, int flags, int *fd) = 0;
|
virtual int Open(const char *path, int flags, int *fd) = 0;
|
||||||
virtual int Open(const char *path, int flags, std::unique_ptr<File> *f) = 0;
|
virtual int Open(const char *path, int flags, std::unique_ptr<File> *f) = 0;
|
||||||
|
@ -59,6 +59,7 @@ using moonfire_nvr::StrCat;
|
|||||||
DEFINE_int32(http_port, 0, "");
|
DEFINE_int32(http_port, 0, "");
|
||||||
DEFINE_string(db_dir, "", "");
|
DEFINE_string(db_dir, "", "");
|
||||||
DEFINE_string(sample_file_dir, "", "");
|
DEFINE_string(sample_file_dir, "", "");
|
||||||
|
DEFINE_bool(read_only, false, "");
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -152,10 +153,28 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
env.sample_file_dir = sample_file_dir.release();
|
env.sample_file_dir = sample_file_dir.release();
|
||||||
|
|
||||||
|
std::unique_ptr<moonfire_nvr::File> db_dir;
|
||||||
|
std::string db_dirname = FLAGS_db_dir;
|
||||||
|
ret = moonfire_nvr::GetRealFilesystem()->Open(
|
||||||
|
db_dirname.c_str(), O_DIRECTORY | O_RDONLY, &db_dir);
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG(ERROR) << "Unable to open --db_dir=" << db_dirname << ": "
|
||||||
|
<< strerror(ret) << "; exiting.";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
bool read_only = FLAGS_read_only;
|
||||||
|
ret = db_dir->Lock((read_only ? LOCK_SH : LOCK_EX) | LOCK_NB);
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG(ERROR) << "Unable to lock --db_dir=" << db_dirname << ": "
|
||||||
|
<< strerror(ret) << "; exiting.";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
moonfire_nvr::Database db;
|
moonfire_nvr::Database db;
|
||||||
std::string error_msg;
|
std::string error_msg;
|
||||||
std::string db_path = StrCat(FLAGS_db_dir, "/db");
|
std::string db_path = StrCat(FLAGS_db_dir, "/db");
|
||||||
if (!db.Open(db_path.c_str(), SQLITE_OPEN_READWRITE, &error_msg)) {
|
if (!db.Open(db_path.c_str(),
|
||||||
|
read_only ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE,
|
||||||
|
&error_msg)) {
|
||||||
LOG(ERROR) << error_msg << "; exiting.";
|
LOG(ERROR) << error_msg << "; exiting.";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -171,10 +190,13 @@ int main(int argc, char** argv) {
|
|||||||
<< ", running with version " << event_get_version();
|
<< ", running with version " << event_get_version();
|
||||||
base = CHECK_NOTNULL(event_base_new());
|
base = CHECK_NOTNULL(event_base_new());
|
||||||
|
|
||||||
std::unique_ptr<moonfire_nvr::Nvr> nvr(new moonfire_nvr::Nvr(&env));
|
std::unique_ptr<moonfire_nvr::Nvr> nvr;
|
||||||
if (!nvr->Init(&error_msg)) {
|
if (!read_only) {
|
||||||
LOG(ERROR) << "Unable to initialize: " << error_msg << "; exiting.";
|
nvr.reset(new moonfire_nvr::Nvr(&env));
|
||||||
exit(1);
|
if (!nvr->Init(&error_msg)) {
|
||||||
|
LOG(ERROR) << "Unable to initialize: " << error_msg << "; exiting.";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
evhttp* http = CHECK_NOTNULL(evhttp_new(base));
|
evhttp* http = CHECK_NOTNULL(evhttp_new(base));
|
||||||
|
@ -137,6 +137,7 @@ class MockFile : public File {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MOCK_METHOD1(Lock, int(int));
|
||||||
MOCK_METHOD3(Open, int(const char *, int, int *));
|
MOCK_METHOD3(Open, int(const char *, int, int *));
|
||||||
MOCK_METHOD4(Open, int(const char *, int, mode_t, int *));
|
MOCK_METHOD4(Open, int(const char *, int, mode_t, int *));
|
||||||
MOCK_METHOD3(OpenRaw, int(const char *, int, File **));
|
MOCK_METHOD3(OpenRaw, int(const char *, int, File **));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user