mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-11-28 05:04:15 -05:00
new command to initialize a database
This commit is contained in:
@@ -99,7 +99,7 @@ struct File {
|
||||
pub fn run() -> Result<(), Error> {
|
||||
let args: Args = super::parse_args(USAGE)?;
|
||||
super::install_logger(false);
|
||||
let (_db_dir, conn) = super::open_conn(&args.flag_db_dir, true)?;
|
||||
let (_db_dir, conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::ReadOnly)?;
|
||||
let mut files = Vec::new();
|
||||
for e in fs::read_dir(&args.flag_sample_file_dir)? {
|
||||
let e = e?;
|
||||
|
||||
75
src/cmds/init.rs
Normal file
75
src/cmds/init.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use db;
|
||||
use error::Error;
|
||||
|
||||
static USAGE: &'static str = r#"
|
||||
Initializes a database.
|
||||
|
||||
Usage:
|
||||
|
||||
moonfire-nvr init [options]
|
||||
moonfire-nvr init --help
|
||||
|
||||
Options:
|
||||
|
||||
--db-dir=DIR Set the directory holding the SQLite3 index database.
|
||||
This is typically on a flash device.
|
||||
[default: /var/lib/moonfire-nvr/db]
|
||||
"#;
|
||||
|
||||
#[derive(Debug, RustcDecodable)]
|
||||
struct Args {
|
||||
flag_db_dir: String,
|
||||
}
|
||||
|
||||
pub fn run() -> Result<(), Error> {
|
||||
let args: Args = super::parse_args(USAGE)?;
|
||||
super::install_logger(false);
|
||||
let (_db_dir, mut conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::Create)?;
|
||||
|
||||
// Check if the database has already been initialized.
|
||||
let cur_ver = db::get_schema_version(&conn)?;
|
||||
if let Some(v) = cur_ver {
|
||||
info!("Database is already initialized with schema version {}.", v);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
conn.execute_batch(r#"
|
||||
pragma journal_mode = wal;
|
||||
pragma page_size = 16384;
|
||||
"#)?;
|
||||
let tx = conn.transaction()?;
|
||||
tx.execute_batch(include_str!("../schema.sql"))?;
|
||||
tx.commit()?;
|
||||
info!("Database initialized.");
|
||||
Ok(())
|
||||
}
|
||||
@@ -40,25 +40,28 @@ use slog_term;
|
||||
use std::path::Path;
|
||||
|
||||
mod check;
|
||||
mod init;
|
||||
mod run;
|
||||
mod ts;
|
||||
mod upgrade;
|
||||
|
||||
#[derive(Debug, RustcDecodable)]
|
||||
pub enum Command {
|
||||
Run,
|
||||
Upgrade,
|
||||
Check,
|
||||
Init,
|
||||
Run,
|
||||
Ts,
|
||||
Upgrade,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
pub fn run(&self) -> Result<(), Error> {
|
||||
match *self {
|
||||
Command::Run => run::run(),
|
||||
Command::Upgrade => upgrade::run(),
|
||||
Command::Check => check::run(),
|
||||
Command::Init => init::run(),
|
||||
Command::Run => run::run(),
|
||||
Command::Ts => ts::run(),
|
||||
Command::Upgrade => upgrade::run(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,21 +76,29 @@ fn install_logger(async: bool) {
|
||||
slog_stdlog::set_logger(slog::Logger::root(drain.ignore_err(), None)).unwrap();
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum OpenMode {
|
||||
ReadOnly,
|
||||
ReadWrite,
|
||||
Create
|
||||
}
|
||||
|
||||
/// Locks and opens the database.
|
||||
/// The returned `dir::Fd` holds the lock and should be kept open as long as the `Connection` is.
|
||||
fn open_conn(db_dir: &str, read_only: bool) -> Result<(dir::Fd, rusqlite::Connection), Error> {
|
||||
fn open_conn(db_dir: &str, mode: OpenMode) -> Result<(dir::Fd, rusqlite::Connection), Error> {
|
||||
let dir = dir::Fd::open(db_dir)?;
|
||||
dir.lock(if read_only { libc::LOCK_SH } else { libc::LOCK_EX } | libc::LOCK_NB)
|
||||
let ro = mode == OpenMode::ReadOnly;
|
||||
dir.lock(if ro { libc::LOCK_SH } else { libc::LOCK_EX } | libc::LOCK_NB)
|
||||
.map_err(|e| Error{description: format!("db dir {:?} already in use; can't get {} lock",
|
||||
db_dir,
|
||||
if read_only { "shared" } else { "exclusive" }),
|
||||
if ro { "shared" } else { "exclusive" }),
|
||||
cause: Some(Box::new(e))})?;
|
||||
let conn = rusqlite::Connection::open_with_flags(
|
||||
Path::new(&db_dir).join("db"),
|
||||
if read_only {
|
||||
rusqlite::SQLITE_OPEN_READ_ONLY
|
||||
} else {
|
||||
rusqlite::SQLITE_OPEN_READ_WRITE
|
||||
match mode {
|
||||
OpenMode::ReadOnly => rusqlite::SQLITE_OPEN_READ_ONLY,
|
||||
OpenMode::ReadWrite => rusqlite::SQLITE_OPEN_READ_WRITE,
|
||||
OpenMode::Create => rusqlite::SQLITE_OPEN_READ_WRITE | rusqlite::SQLITE_OPEN_CREATE,
|
||||
} |
|
||||
// rusqlite::Connection is not Sync, so there's no reason to tell SQLite3 to use the
|
||||
// serialized threading mode.
|
||||
|
||||
@@ -73,7 +73,9 @@ pub fn run() -> Result<(), Error> {
|
||||
// that signals will be blocked in all threads.
|
||||
let signal = chan_signal::notify(&[chan_signal::Signal::INT, chan_signal::Signal::TERM]);
|
||||
super::install_logger(true);
|
||||
let (_db_dir, conn) = super::open_conn(&args.flag_db_dir, args.flag_read_only)?;
|
||||
let (_db_dir, conn) = super::open_conn(
|
||||
&args.flag_db_dir,
|
||||
if args.flag_read_only { super::OpenMode::ReadOnly } else { super::OpenMode::ReadWrite })?;
|
||||
let db = Arc::new(db::Database::new(conn).unwrap());
|
||||
let dir = dir::SampleFileDir::new(&args.flag_sample_file_dir, db.clone()).unwrap();
|
||||
info!("Database is loaded.");
|
||||
|
||||
@@ -87,7 +87,7 @@ fn set_journal_mode(conn: &rusqlite::Connection, requested: &str) -> Result<(),
|
||||
pub fn run() -> Result<(), Error> {
|
||||
let args: Args = super::parse_args(USAGE)?;
|
||||
super::install_logger(false);
|
||||
let (_db_dir, mut conn) = super::open_conn(&args.flag_db_dir, false)?;
|
||||
let (_db_dir, mut conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::ReadWrite)?;
|
||||
|
||||
{
|
||||
assert_eq!(UPGRADERS.len(), db::EXPECTED_VERSION as usize);
|
||||
|
||||
Reference in New Issue
Block a user