mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-11-20 09:56:07 -05:00
upgrade to 2018 Rust edition
This is mostly just "cargo fix --edition" + Cargo.toml changes. There's one fix for upgrading to NLL in db/writer.rs: Writer::previously_opened wouldn't build with NLL because of a double-borrow the previous borrow checker somehow didn't catch. Restructure to avoid it. I'll put elective NLL changes in a following commit.
This commit is contained in:
@@ -3,6 +3,7 @@ name = "moonfire-db"
|
||||
version = "0.0.1"
|
||||
authors = ["Scott Lamb <slamb@slamb.org>"]
|
||||
readme = "../README.md"
|
||||
edition = "2018"
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
// 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 base::strutil;
|
||||
use crate::base::strutil;
|
||||
use blake2_rfc::blake2b::blake2b;
|
||||
use failure::Error;
|
||||
use fnv::FnvHashMap;
|
||||
@@ -730,10 +730,10 @@ fn lookup_session(conn: &Connection, hash: &SessionHash) -> Result<Session, Erro
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use db;
|
||||
use crate::db;
|
||||
use rusqlite::Connection;
|
||||
use super::*;
|
||||
use testutil;
|
||||
use crate::testutil;
|
||||
|
||||
#[test]
|
||||
fn open_empty_db() {
|
||||
|
||||
10
db/check.rs
10
db/check.rs
@@ -30,14 +30,14 @@
|
||||
|
||||
//! Subcommand to check the database and sample file dir for errors.
|
||||
|
||||
use db::{self, CompositeId, FromSqlUuid};
|
||||
use dir;
|
||||
use crate::db::{self, CompositeId, FromSqlUuid};
|
||||
use crate::dir;
|
||||
use failure::Error;
|
||||
use fnv::FnvHashMap;
|
||||
use raw;
|
||||
use recording;
|
||||
use crate::raw;
|
||||
use crate::recording;
|
||||
use rusqlite::{self, types::ToSql};
|
||||
use schema;
|
||||
use crate::schema;
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::fs;
|
||||
|
||||
|
||||
36
db/db.rs
36
db/db.rs
@@ -52,18 +52,18 @@
|
||||
//! A list of mutations is built up in-memory and occasionally flushed to reduce SSD write
|
||||
//! cycles.
|
||||
|
||||
use auth;
|
||||
use base::clock::{self, Clocks};
|
||||
use dir;
|
||||
use crate::auth;
|
||||
use crate::base::clock::{self, Clocks};
|
||||
use crate::dir;
|
||||
use failure::Error;
|
||||
use fnv::{self, FnvHashMap, FnvHashSet};
|
||||
use lru_cache::LruCache;
|
||||
use openssl::hash;
|
||||
use parking_lot::{Mutex,MutexGuard};
|
||||
use raw;
|
||||
use recording::{self, TIME_UNITS_PER_SEC};
|
||||
use crate::raw;
|
||||
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
||||
use rusqlite::{self, types::ToSql};
|
||||
use schema;
|
||||
use crate::schema;
|
||||
use std::collections::{BTreeMap, VecDeque};
|
||||
use std::cell::RefCell;
|
||||
use std::cmp;
|
||||
@@ -309,11 +309,11 @@ impl SampleFileDir {
|
||||
}
|
||||
}
|
||||
|
||||
pub use auth::Request;
|
||||
pub use auth::RawSessionId;
|
||||
pub use auth::Session;
|
||||
pub use auth::User;
|
||||
pub use auth::UserChange;
|
||||
pub use crate::auth::Request;
|
||||
pub use crate::auth::RawSessionId;
|
||||
pub use crate::auth::Session;
|
||||
pub use crate::auth::User;
|
||||
pub use crate::auth::UserChange;
|
||||
|
||||
/// In-memory state about a camera.
|
||||
#[derive(Debug)]
|
||||
@@ -735,13 +735,13 @@ impl StreamStateChanger {
|
||||
/// Applies the change to the given `streams_by_id`. The caller is expected to set
|
||||
/// `Camera::streams` to the return value.
|
||||
fn apply(mut self, streams_by_id: &mut BTreeMap<i32, Stream>) -> [Option<i32>; 2] {
|
||||
for (id, mut stream) in self.streams.drain(..) {
|
||||
for (id, stream) in self.streams.drain(..) {
|
||||
use ::std::collections::btree_map::Entry;
|
||||
match (streams_by_id.entry(id), stream) {
|
||||
(Entry::Vacant(mut e), Some(new)) => { e.insert(new); },
|
||||
(Entry::Vacant(e), Some(new)) => { e.insert(new); },
|
||||
(Entry::Vacant(_), None) => {},
|
||||
(Entry::Occupied(mut e), Some(new)) => { e.insert(new); },
|
||||
(Entry::Occupied(mut e), None) => { e.remove(); },
|
||||
(Entry::Occupied(e), None) => { e.remove(); },
|
||||
};
|
||||
}
|
||||
self.sids
|
||||
@@ -874,7 +874,7 @@ impl LockedDatabase {
|
||||
for dir in self.sample_file_dirs_by_id.values() {
|
||||
raw::mark_sample_files_deleted(&tx, &dir.garbage_unlinked)?;
|
||||
}
|
||||
for (&stream_id, mut r) in &mut new_ranges {
|
||||
for (&stream_id, r) in &mut new_ranges {
|
||||
*r = raw::get_range(&tx, stream_id)?;
|
||||
}
|
||||
{
|
||||
@@ -1921,11 +1921,11 @@ impl<'db, C: Clocks + Clone> ::std::ops::DerefMut for DatabaseGuard<'db, C> {
|
||||
mod tests {
|
||||
extern crate tempdir;
|
||||
|
||||
use base::clock;
|
||||
use recording::{self, TIME_UNITS_PER_SEC};
|
||||
use crate::base::clock;
|
||||
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
||||
use rusqlite::Connection;
|
||||
use std::collections::BTreeMap;
|
||||
use testutil;
|
||||
use crate::testutil;
|
||||
use super::*;
|
||||
use super::adjust_days; // non-public.
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -32,11 +32,11 @@
|
||||
//!
|
||||
//! This includes opening files for serving, rotating away old files, and saving new files.
|
||||
|
||||
use db::CompositeId;
|
||||
use crate::db::CompositeId;
|
||||
use failure::{Error, Fail};
|
||||
use libc::{self, c_char};
|
||||
use protobuf::{self, Message};
|
||||
use schema;
|
||||
use crate::schema;
|
||||
use std::ffi;
|
||||
use std::fs;
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
@@ -65,4 +65,4 @@ pub mod writer;
|
||||
// #[cfg(test)] is not passed on to dependencies.
|
||||
pub mod testutil;
|
||||
|
||||
pub use db::*;
|
||||
pub use crate::db::*;
|
||||
|
||||
@@ -30,10 +30,10 @@
|
||||
|
||||
//! Raw database access: SQLite statements which do not touch any cached state.
|
||||
|
||||
use db::{self, CompositeId, FromSqlUuid};
|
||||
use crate::db::{self, CompositeId, FromSqlUuid};
|
||||
use failure::{Error, ResultExt};
|
||||
use fnv::FnvHashSet;
|
||||
use recording;
|
||||
use crate::recording;
|
||||
use rusqlite::{self, types::ToSql};
|
||||
use std::ops::Range;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -28,8 +28,8 @@
|
||||
// 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 coding::{append_varint32, decode_varint32, unzigzag32, zigzag32};
|
||||
use db;
|
||||
use crate::coding::{append_varint32, decode_varint32, unzigzag32, zigzag32};
|
||||
use crate::db;
|
||||
use failure::Error;
|
||||
use regex::Regex;
|
||||
use std::ops;
|
||||
@@ -492,8 +492,8 @@ impl Segment {
|
||||
// Note: this inner loop uses try! rather than ? for performance. Don't change these
|
||||
// lines without reading https://github.com/rust-lang/rust/issues/37939 and running
|
||||
// mp4::bench::build_index.
|
||||
try!(f(&it));
|
||||
have_frame = try!(it.next(data));
|
||||
r#try!(f(&it));
|
||||
have_frame = r#try!(it.next(data));
|
||||
}
|
||||
if key_frame < self.key_frames {
|
||||
bail!("recording {}: expected {} key frames, found only {}",
|
||||
@@ -505,9 +505,9 @@ impl Segment {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use base::clock::RealClocks;
|
||||
use crate::base::clock::RealClocks;
|
||||
use super::*;
|
||||
use testutil::{self, TestDb};
|
||||
use crate::testutil::{self, TestDb};
|
||||
|
||||
#[test]
|
||||
fn test_parse_time() {
|
||||
|
||||
@@ -28,9 +28,9 @@
|
||||
// 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 base::clock::Clocks;
|
||||
use db;
|
||||
use dir;
|
||||
use crate::base::clock::Clocks;
|
||||
use crate::db;
|
||||
use crate::dir;
|
||||
use fnv::FnvHashMap;
|
||||
use mylog;
|
||||
use rusqlite;
|
||||
@@ -40,7 +40,7 @@ use std::thread;
|
||||
use tempdir::TempDir;
|
||||
use time;
|
||||
use uuid::Uuid;
|
||||
use writer;
|
||||
use crate::writer;
|
||||
|
||||
static INIT: sync::Once = sync::ONCE_INIT;
|
||||
|
||||
@@ -62,7 +62,7 @@ pub fn init() {
|
||||
h.install().unwrap();
|
||||
env::set_var("TZ", "America/Los_Angeles");
|
||||
time::tzset();
|
||||
::auth::set_test_config();
|
||||
crate::auth::set_test_config();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ impl<C: Clocks + Clone> TestDb<C> {
|
||||
/// There will no backing sample file, so it won't be possible to generate a full `.mp4`.
|
||||
pub fn insert_recording_from_encoder(&self, r: db::RecordingToInsert)
|
||||
-> db::ListRecordingsRow {
|
||||
use recording::{self, TIME_UNITS_PER_SEC};
|
||||
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
||||
let mut db = self.db.lock();
|
||||
let video_sample_entry_id = db.insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
@@ -153,7 +153,7 @@ impl<C: Clocks + Clone> TestDb<C> {
|
||||
// For benchmarking
|
||||
#[cfg(feature="nightly")]
|
||||
pub fn add_dummy_recordings_to_db(db: &db::Database, num: usize) {
|
||||
use recording::{self, TIME_UNITS_PER_SEC};
|
||||
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
||||
let mut data = Vec::new();
|
||||
data.extend_from_slice(include_bytes!("testdata/video_sample_index.bin"));
|
||||
let mut db = db.lock();
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
///
|
||||
/// See `guide/schema.md` for more information.
|
||||
|
||||
use db;
|
||||
use crate::db;
|
||||
use failure::Error;
|
||||
use rusqlite::{self, types::ToSql};
|
||||
|
||||
|
||||
@@ -30,9 +30,9 @@
|
||||
|
||||
/// Upgrades a version 0 schema to a version 1 schema.
|
||||
|
||||
use db;
|
||||
use crate::db;
|
||||
use failure::Error;
|
||||
use recording;
|
||||
use crate::recording;
|
||||
use rusqlite::{self, types::ToSql};
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
@@ -30,11 +30,11 @@
|
||||
|
||||
/// Upgrades a version 1 schema to a version 2 schema.
|
||||
|
||||
use dir;
|
||||
use crate::dir;
|
||||
use failure::Error;
|
||||
use libc;
|
||||
use rusqlite::{self, types::ToSql};
|
||||
use schema::DirMeta;
|
||||
use crate::schema::DirMeta;
|
||||
use std::fs;
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use uuid::Uuid;
|
||||
@@ -332,7 +332,7 @@ fn verify_dir_contents(sample_file_path: &str, tx: &rusqlite::Transaction) -> Re
|
||||
let mut rows = stmt.query(&[] as &[&ToSql])?;
|
||||
while let Some(row) = rows.next() {
|
||||
let row = row?;
|
||||
let uuid: ::db::FromSqlUuid = row.get_checked(0)?;
|
||||
let uuid: crate::db::FromSqlUuid = row.get_checked(0)?;
|
||||
if !files.remove(&uuid.0) {
|
||||
bail!("{} is missing from dir {}!", uuid.0, sample_file_path);
|
||||
}
|
||||
@@ -343,7 +343,7 @@ fn verify_dir_contents(sample_file_path: &str, tx: &rusqlite::Transaction) -> Re
|
||||
let mut rows = stmt.query(&[] as &[&ToSql])?;
|
||||
while let Some(row) = rows.next() {
|
||||
let row = row?;
|
||||
let uuid: ::db::FromSqlUuid = row.get_checked(0)?;
|
||||
let uuid: crate::db::FromSqlUuid = row.get_checked(0)?;
|
||||
files.remove(&uuid.0);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,11 +32,11 @@
|
||||
/// Note that a version 2 schema is never actually used; so we know the upgrade from version 1 was
|
||||
/// completed, and possibly an upgrade from 2 to 3 is half-finished.
|
||||
|
||||
use db::{self, FromSqlUuid};
|
||||
use dir;
|
||||
use crate::db::{self, FromSqlUuid};
|
||||
use crate::dir;
|
||||
use failure::Error;
|
||||
use libc;
|
||||
use schema;
|
||||
use crate::schema;
|
||||
use std::io::{self, Write};
|
||||
use std::mem;
|
||||
use std::sync::Arc;
|
||||
|
||||
40
db/writer.rs
40
db/writer.rs
@@ -32,13 +32,13 @@
|
||||
//!
|
||||
//! This includes opening files for serving, rotating away old files, and saving new files.
|
||||
|
||||
use base::clock::{self, Clocks};
|
||||
use db::{self, CompositeId};
|
||||
use dir;
|
||||
use crate::base::clock::{self, Clocks};
|
||||
use crate::db::{self, CompositeId};
|
||||
use crate::dir;
|
||||
use failure::Error;
|
||||
use fnv::FnvHashMap;
|
||||
use parking_lot::Mutex;
|
||||
use recording;
|
||||
use crate::recording;
|
||||
use openssl::hash;
|
||||
use std::cmp;
|
||||
use std::io;
|
||||
@@ -588,12 +588,13 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Writer<'a, C, D> {
|
||||
}
|
||||
|
||||
/// Opens a new writer.
|
||||
/// This returns a writer that violates the invariant that `unflushed_sample` is `Some`.
|
||||
/// The caller (`write`) is responsible for correcting this.
|
||||
fn open(&mut self) -> Result<&mut InnerWriter<D::File>, Error> {
|
||||
/// On successful return, `self.state` will be `WriterState::Open(w)` with `w` violating the
|
||||
/// invariant that `unflushed_sample` is `Some`. The caller (`write`) is responsible for
|
||||
/// correcting this.
|
||||
fn open(&mut self) -> Result<(), Error> {
|
||||
let prev = match self.state {
|
||||
WriterState::Unopened => None,
|
||||
WriterState::Open(ref mut w) => return Ok(w),
|
||||
WriterState::Open(_) => return Ok(()),
|
||||
WriterState::Closed(prev) => Some(prev),
|
||||
};
|
||||
let (id, r) = self.db.lock().add_recording(self.stream_id, db::RecordingToInsert {
|
||||
@@ -614,12 +615,9 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Writer<'a, C, D> {
|
||||
local_start: recording::Time(i64::max_value()),
|
||||
adjuster: ClockAdjuster::new(prev.map(|p| p.local_time_delta.0)),
|
||||
unflushed_sample: None,
|
||||
});
|
||||
match self.state {
|
||||
WriterState::Open(ref mut w) => Ok(w),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn previously_opened(&self) -> Result<bool, Error> {
|
||||
Ok(match self.state {
|
||||
@@ -633,7 +631,11 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Writer<'a, C, D> {
|
||||
/// `local_time` should be the local clock's time as of when this packet was received.
|
||||
pub fn write(&mut self, pkt: &[u8], local_time: recording::Time, pts_90k: i64,
|
||||
is_key: bool) -> Result<(), Error> {
|
||||
let w = self.open()?;
|
||||
self.open()?;
|
||||
let w = match self.state {
|
||||
WriterState::Open(ref mut w) => w,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
// Note w's invariant that `unflushed_sample` is `None` may currently be violated.
|
||||
// We must restore it on all success or error paths.
|
||||
@@ -739,16 +741,16 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Drop for Writer<'a, C, D> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use base::clock::SimulatedClocks;
|
||||
use db::{self, CompositeId};
|
||||
use crate::base::clock::SimulatedClocks;
|
||||
use crate::db::{self, CompositeId};
|
||||
use parking_lot::Mutex;
|
||||
use recording;
|
||||
use crate::recording;
|
||||
use std::collections::VecDeque;
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc;
|
||||
use super::{ClockAdjuster, Writer};
|
||||
use testutil;
|
||||
use crate::testutil;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MockDir(Arc<Mutex<VecDeque<MockDirAction>>>);
|
||||
|
||||
Reference in New Issue
Block a user