2018-03-01 20:07:42 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2020 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2018-03-01 20:07:42 -05:00
|
|
|
|
|
|
|
//! Subcommand to check the database and sample file dir for errors.
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
use crate::compare;
|
2021-10-26 13:12:19 -04:00
|
|
|
use crate::db::{self, CompositeId, SqlUuid};
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::dir;
|
2021-10-26 14:47:13 -04:00
|
|
|
use crate::json::SampleFileDirConfig;
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::raw;
|
|
|
|
use crate::recording;
|
2021-02-17 01:15:54 -05:00
|
|
|
use crate::schema;
|
2023-07-10 01:04:17 -04:00
|
|
|
use base::{err, Error};
|
2021-02-11 23:03:28 -05:00
|
|
|
use fnv::{FnvHashMap, FnvHashSet};
|
2019-07-12 14:05:36 -04:00
|
|
|
use nix::fcntl::AtFlags;
|
2020-03-19 23:46:25 -04:00
|
|
|
use rusqlite::params;
|
2019-07-12 14:05:36 -04:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2023-02-16 02:14:54 -05:00
|
|
|
use tracing::{error, info, warn};
|
2018-03-01 20:07:42 -05:00
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
pub compare_lens: bool,
|
2021-02-11 23:03:28 -05:00
|
|
|
pub trash_orphan_sample_files: bool,
|
|
|
|
pub delete_orphan_rows: bool,
|
|
|
|
pub trash_corrupt_rows: bool,
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:03:28 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Context {
|
|
|
|
rows_to_delete: FnvHashSet<CompositeId>,
|
2021-02-17 01:15:54 -05:00
|
|
|
files_to_trash: FnvHashSet<(i32, CompositeId)>, // (dir_id, composite_id)
|
2021-02-11 23:03:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(conn: &mut rusqlite::Connection, opts: &Options) -> Result<i32, Error> {
|
2021-02-11 13:45:56 -05:00
|
|
|
let mut printed_error = false;
|
|
|
|
|
2021-02-11 18:24:24 -05:00
|
|
|
info!("Checking SQLite database integrity...");
|
2021-02-11 23:27:12 -05:00
|
|
|
{
|
|
|
|
let mut stmt = conn.prepare("pragma integrity_check")?;
|
|
|
|
let mut rows = stmt.query(params![])?;
|
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let e: String = row.get(0)?;
|
2021-02-17 01:15:54 -05:00
|
|
|
if e == "ok" {
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-03 16:42:41 -04:00
|
|
|
error!(err = %e, "sqlite integrity error");
|
2021-02-11 23:27:12 -05:00
|
|
|
printed_error = true;
|
|
|
|
}
|
2021-02-11 18:24:24 -05:00
|
|
|
}
|
|
|
|
info!("...done");
|
|
|
|
|
2021-02-11 18:21:05 -05:00
|
|
|
// Compare stated schema version.
|
|
|
|
if let Err(e) = db::check_schema_version(conn) {
|
|
|
|
error!("Schema version is not as expected:\n{}", e);
|
|
|
|
printed_error = true;
|
|
|
|
} else {
|
|
|
|
info!("Schema at expected version {}.", db::EXPECTED_VERSION);
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
// Compare schemas.
|
|
|
|
{
|
|
|
|
let mut expected = rusqlite::Connection::open_in_memory()?;
|
|
|
|
db::init(&mut expected)?;
|
|
|
|
if let Some(diffs) = compare::get_diffs("actual", conn, "expected", &expected)? {
|
2021-02-11 13:45:56 -05:00
|
|
|
error!("Schema is not as expected:\n{}", &diffs);
|
|
|
|
printed_error = true;
|
2020-11-24 11:42:22 -05:00
|
|
|
} else {
|
2021-02-11 18:24:24 -05:00
|
|
|
info!("Schema is as expected.");
|
2019-07-11 11:07:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:21:05 -05:00
|
|
|
if printed_error {
|
|
|
|
warn!("The following analysis may be incorrect or encounter errors due to schema differences.");
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:29:16 -04:00
|
|
|
let (db_uuid, _config) = raw::read_meta(conn)?;
|
2018-03-01 20:07:42 -05:00
|
|
|
|
|
|
|
// Scan directories.
|
2021-02-11 18:04:07 -05:00
|
|
|
let mut dirs_by_id: FnvHashMap<i32, Dir> = FnvHashMap::default();
|
2018-03-01 20:07:42 -05:00
|
|
|
{
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut dir_stmt = conn.prepare(
|
|
|
|
r#"
|
2021-10-26 17:32:37 -04:00
|
|
|
select d.id, d.config, d.uuid, d.last_complete_open_id, o.uuid
|
2018-03-01 20:07:42 -05:00
|
|
|
from sample_file_dir d left join open o on (d.last_complete_open_id = o.id)
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
let mut garbage_stmt =
|
|
|
|
conn.prepare_cached("select composite_id from garbage where sample_file_dir_id = ?")?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = dir_stmt.query(params![])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
2018-03-01 20:07:42 -05:00
|
|
|
let mut meta = schema::DirMeta::default();
|
2019-05-31 19:19:04 -04:00
|
|
|
let dir_id: i32 = row.get(0)?;
|
2021-10-26 14:47:13 -04:00
|
|
|
let config: SampleFileDirConfig = row.get(1)?;
|
2021-10-26 13:12:19 -04:00
|
|
|
let dir_uuid: SqlUuid = row.get(2)?;
|
2019-05-31 19:19:04 -04:00
|
|
|
let open_id = row.get(3)?;
|
2021-10-26 13:12:19 -04:00
|
|
|
let open_uuid: SqlUuid = row.get(4)?;
|
2018-03-01 20:07:42 -05:00
|
|
|
meta.db_uuid.extend_from_slice(&db_uuid.as_bytes()[..]);
|
|
|
|
meta.dir_uuid.extend_from_slice(&dir_uuid.0.as_bytes()[..]);
|
|
|
|
{
|
2022-05-02 14:22:14 -04:00
|
|
|
let o = meta.last_complete_open.mut_or_insert_default();
|
2018-03-01 20:07:42 -05:00
|
|
|
o.id = open_id;
|
|
|
|
o.uuid.extend_from_slice(&open_uuid.0.as_bytes()[..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the directory (checking its metadata) and hold it open (for the lock).
|
2021-10-26 14:47:13 -04:00
|
|
|
let dir = dir::SampleFileDir::open(&config.path, &meta)
|
2023-07-10 01:04:17 -04:00
|
|
|
.map_err(|e| err!(e, msg("unable to open dir {}", config.path.display())))?;
|
2019-07-12 14:05:36 -04:00
|
|
|
let mut streams = read_dir(&dir, opts)?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = garbage_stmt.query(params![dir_id])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let id = CompositeId(row.get(0)?);
|
2018-03-09 10:31:48 -05:00
|
|
|
let s = streams.entry(id.stream()).or_insert_with(Stream::default);
|
2021-02-17 01:15:54 -05:00
|
|
|
s.recordings
|
|
|
|
.entry(id.recording())
|
|
|
|
.or_insert_with(Recording::default)
|
|
|
|
.garbage_row = true;
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
2021-02-11 18:04:07 -05:00
|
|
|
dirs_by_id.insert(dir_id, streams);
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 10:31:48 -05:00
|
|
|
// Scan known streams.
|
2021-02-11 23:03:28 -05:00
|
|
|
let mut ctx = Context::default();
|
2018-03-01 20:07:42 -05:00
|
|
|
{
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = conn.prepare(
|
|
|
|
r#"
|
2021-02-11 18:04:07 -05:00
|
|
|
select
|
|
|
|
id,
|
|
|
|
sample_file_dir_id,
|
|
|
|
cum_recordings
|
|
|
|
from
|
|
|
|
stream
|
|
|
|
where
|
|
|
|
sample_file_dir_id is not null
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = stmt.query(params![])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let stream_id = row.get(0)?;
|
|
|
|
let dir_id = row.get(1)?;
|
2021-02-11 18:04:07 -05:00
|
|
|
let cum_recordings = row.get(2)?;
|
|
|
|
let mut stream = match dirs_by_id.get_mut(&dir_id) {
|
2018-03-09 10:31:48 -05:00
|
|
|
None => Stream::default(),
|
2022-09-28 12:29:16 -04:00
|
|
|
Some(d) => d.remove(&stream_id).unwrap_or_default(),
|
2018-03-01 20:07:42 -05:00
|
|
|
};
|
2021-02-11 18:04:07 -05:00
|
|
|
stream.cum_recordings = Some(cum_recordings);
|
2021-02-11 23:03:28 -05:00
|
|
|
printed_error |= compare_stream(conn, dir_id, stream_id, opts, stream, &mut ctx)?;
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 10:31:48 -05:00
|
|
|
// Expect the rest to have only garbage.
|
2021-02-11 18:04:07 -05:00
|
|
|
for (&dir_id, streams) in &dirs_by_id {
|
2018-03-09 10:31:48 -05:00
|
|
|
for (&stream_id, stream) in streams {
|
2021-02-11 18:04:07 -05:00
|
|
|
for (&recording_id, r) in &stream.recordings {
|
2018-03-09 10:31:48 -05:00
|
|
|
let id = CompositeId::new(stream_id, recording_id);
|
2021-02-17 01:15:54 -05:00
|
|
|
if r.recording_row.is_some()
|
|
|
|
|| r.playback_row.is_some()
|
|
|
|
|| r.integrity_row
|
|
|
|
|| !r.garbage_row
|
|
|
|
{
|
|
|
|
error!(
|
|
|
|
"dir {} recording {} for unknown stream: {:#?}",
|
|
|
|
dir_id, id, r
|
|
|
|
);
|
2021-02-11 13:45:56 -05:00
|
|
|
printed_error = true;
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:03:28 -05:00
|
|
|
if !ctx.rows_to_delete.is_empty() || !ctx.files_to_trash.is_empty() {
|
|
|
|
let tx = conn.transaction()?;
|
|
|
|
if !ctx.rows_to_delete.is_empty() {
|
|
|
|
info!("Deleting {} recording rows", ctx.rows_to_delete.len());
|
2021-03-05 14:42:23 -05:00
|
|
|
let mut d1 = tx.prepare("delete from recording_playback where composite_id = ?")?;
|
|
|
|
let mut d2 = tx.prepare("delete from recording_integrity where composite_id = ?")?;
|
|
|
|
let mut d3 = tx.prepare("delete from recording where composite_id = ?")?;
|
2021-02-11 23:03:28 -05:00
|
|
|
for &id in &ctx.rows_to_delete {
|
|
|
|
d1.execute(params![id.0])?;
|
|
|
|
d2.execute(params![id.0])?;
|
|
|
|
d3.execute(params![id.0])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ctx.files_to_trash.is_empty() {
|
|
|
|
info!("Trashing {} recording files", ctx.files_to_trash.len());
|
|
|
|
let mut g = tx.prepare(
|
2021-02-17 01:15:54 -05:00
|
|
|
"insert or ignore into garbage (sample_file_dir_id, composite_id) values (?, ?)",
|
|
|
|
)?;
|
2021-02-11 23:03:28 -05:00
|
|
|
for (dir_id, composite_id) in &ctx.files_to_trash {
|
|
|
|
g.execute(params![dir_id, composite_id.0])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.commit()?;
|
|
|
|
}
|
|
|
|
|
2021-02-11 13:45:56 -05:00
|
|
|
Ok(if printed_error { 1 } else { 0 })
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
struct RecordingSummary {
|
|
|
|
bytes: u64,
|
|
|
|
video_samples: i32,
|
|
|
|
video_sync_samples: i32,
|
2020-11-24 11:42:22 -05:00
|
|
|
media_duration: i32,
|
2018-03-01 20:07:42 -05:00
|
|
|
flags: i32,
|
|
|
|
}
|
|
|
|
|
2018-03-09 10:31:48 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct Recording {
|
|
|
|
/// Present iff there is a file. When `args.compare_lens` is true, the length; otherwise 0.
|
|
|
|
file: Option<u64>,
|
|
|
|
|
|
|
|
/// Iff a `recording` row is present, a `RecordingSummary` from those fields.
|
|
|
|
recording_row: Option<RecordingSummary>,
|
|
|
|
|
|
|
|
/// Iff a `recording_playback` row is present, a `RecordingSummary` computed from the index.
|
|
|
|
/// This should match the recording row.
|
|
|
|
playback_row: Option<RecordingSummary>,
|
|
|
|
|
|
|
|
/// True iff a `recording_integrity` row is present.
|
|
|
|
integrity_row: bool,
|
|
|
|
|
|
|
|
/// True iff a `garbage` row is present.
|
|
|
|
garbage_row: bool,
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:04:07 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Stream {
|
|
|
|
recordings: FnvHashMap<i32, Recording>,
|
|
|
|
cum_recordings: Option<i32>,
|
|
|
|
}
|
|
|
|
|
2018-03-09 10:31:48 -05:00
|
|
|
type Dir = FnvHashMap<i32, Stream>;
|
|
|
|
|
2018-03-01 20:07:42 -05:00
|
|
|
fn summarize_index(video_index: &[u8]) -> Result<RecordingSummary, Error> {
|
2021-05-17 17:31:50 -04:00
|
|
|
let mut it = recording::SampleIndexIterator::default();
|
2020-11-24 11:42:22 -05:00
|
|
|
let mut media_duration = 0;
|
2018-03-01 20:07:42 -05:00
|
|
|
let mut video_samples = 0;
|
|
|
|
let mut video_sync_samples = 0;
|
|
|
|
let mut bytes = 0;
|
|
|
|
while it.next(video_index)? {
|
|
|
|
bytes += it.bytes as u64;
|
2020-11-24 11:42:22 -05:00
|
|
|
media_duration += it.duration_90k;
|
2018-03-01 20:07:42 -05:00
|
|
|
video_samples += 1;
|
|
|
|
video_sync_samples += it.is_key() as i32;
|
|
|
|
}
|
|
|
|
Ok(RecordingSummary {
|
2018-03-09 10:31:48 -05:00
|
|
|
bytes,
|
|
|
|
video_samples,
|
|
|
|
video_sync_samples,
|
2020-11-24 11:42:22 -05:00
|
|
|
media_duration,
|
2021-02-17 01:15:54 -05:00
|
|
|
flags: if it.duration_90k == 0 {
|
|
|
|
db::RecordingFlags::TrailingZero as i32
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
},
|
2018-03-01 20:07:42 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads through the given sample file directory.
|
|
|
|
/// Logs unexpected files and creates a hash map of the files found there.
|
|
|
|
/// If `opts.compare_lens` is set, the values are lengths; otherwise they're insignificant.
|
2019-07-12 14:05:36 -04:00
|
|
|
fn read_dir(d: &dir::SampleFileDir, opts: &Options) -> Result<Dir, Error> {
|
2018-03-09 10:31:48 -05:00
|
|
|
let mut dir = Dir::default();
|
2019-07-12 14:05:36 -04:00
|
|
|
let mut d = d.opendir()?;
|
|
|
|
let fd = d.as_raw_fd();
|
|
|
|
for e in d.iter() {
|
2018-03-01 20:07:42 -05:00
|
|
|
let e = e?;
|
|
|
|
let f = e.file_name();
|
2019-07-12 14:05:36 -04:00
|
|
|
match f.to_bytes() {
|
|
|
|
b"." | b".." | b"meta" => continue,
|
2021-02-17 01:15:54 -05:00
|
|
|
_ => {}
|
2018-03-01 20:07:42 -05:00
|
|
|
};
|
2019-07-12 14:05:36 -04:00
|
|
|
let id = match dir::parse_id(f.to_bytes()) {
|
2018-03-01 20:07:42 -05:00
|
|
|
Ok(id) => id,
|
|
|
|
Err(_) => {
|
2021-02-17 01:15:54 -05:00
|
|
|
error!(
|
|
|
|
"sample file directory contains file {:?} which isn't an id",
|
|
|
|
f
|
|
|
|
);
|
2018-03-01 20:07:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2019-07-12 14:05:36 -04:00
|
|
|
let len = if opts.compare_lens {
|
|
|
|
nix::sys::stat::fstatat(fd, f, AtFlags::empty())?.st_size as u64
|
2021-02-17 01:15:54 -05:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2018-03-09 10:31:48 -05:00
|
|
|
let stream = dir.entry(id.stream()).or_insert_with(Stream::default);
|
2021-02-17 01:15:54 -05:00
|
|
|
stream
|
|
|
|
.recordings
|
|
|
|
.entry(id.recording())
|
|
|
|
.or_insert_with(Recording::default)
|
|
|
|
.file = Some(len);
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
2018-03-09 10:31:48 -05:00
|
|
|
Ok(dir)
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
|
2018-03-09 10:31:48 -05:00
|
|
|
/// Looks through a known stream for errors.
|
2021-02-17 01:15:54 -05:00
|
|
|
fn compare_stream(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
dir_id: i32,
|
|
|
|
stream_id: i32,
|
|
|
|
opts: &Options,
|
|
|
|
mut stream: Stream,
|
|
|
|
ctx: &mut Context,
|
|
|
|
) -> Result<bool, Error> {
|
2018-03-09 10:31:48 -05:00
|
|
|
let start = CompositeId::new(stream_id, 0);
|
|
|
|
let end = CompositeId::new(stream_id, i32::max_value());
|
2021-02-11 13:45:56 -05:00
|
|
|
let mut printed_error = false;
|
2021-02-17 01:15:54 -05:00
|
|
|
let cum_recordings = stream
|
|
|
|
.cum_recordings
|
|
|
|
.expect("cum_recordings must be set on known stream");
|
2018-03-09 10:31:48 -05:00
|
|
|
|
|
|
|
// recording row.
|
|
|
|
{
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = conn.prepare_cached(
|
|
|
|
r#"
|
2018-03-09 10:31:48 -05:00
|
|
|
select
|
|
|
|
composite_id,
|
|
|
|
flags,
|
|
|
|
sample_file_bytes,
|
2020-11-24 11:42:22 -05:00
|
|
|
wall_duration_90k + media_duration_delta_90k,
|
2018-03-09 10:31:48 -05:00
|
|
|
video_samples,
|
|
|
|
video_sync_samples
|
|
|
|
from
|
|
|
|
recording
|
|
|
|
where
|
|
|
|
composite_id between ? and ?
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = stmt.query(params![start.0, end.0])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let id = CompositeId(row.get(0)?);
|
2018-03-09 10:31:48 -05:00
|
|
|
let s = RecordingSummary {
|
2019-05-31 19:19:04 -04:00
|
|
|
flags: row.get(1)?,
|
|
|
|
bytes: row.get::<_, i64>(2)? as u64,
|
2020-11-24 11:42:22 -05:00
|
|
|
media_duration: row.get(3)?,
|
2019-05-31 19:19:04 -04:00
|
|
|
video_samples: row.get(4)?,
|
|
|
|
video_sync_samples: row.get(5)?,
|
2018-03-09 10:31:48 -05:00
|
|
|
};
|
2021-02-17 01:15:54 -05:00
|
|
|
stream
|
|
|
|
.recordings
|
|
|
|
.entry(id.recording())
|
|
|
|
.or_insert_with(Recording::default)
|
|
|
|
.recording_row = Some(s);
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recording_playback row.
|
|
|
|
{
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = conn.prepare_cached(
|
|
|
|
r#"
|
2018-03-09 10:31:48 -05:00
|
|
|
select
|
|
|
|
composite_id,
|
|
|
|
video_index
|
|
|
|
from
|
|
|
|
recording_playback
|
|
|
|
where
|
|
|
|
composite_id between ? and ?
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = stmt.query(params![start.0, end.0])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let id = CompositeId(row.get(0)?);
|
|
|
|
let video_index: Vec<u8> = row.get(1)?;
|
2018-03-09 10:31:48 -05:00
|
|
|
let s = match summarize_index(&video_index) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
error!("id {} has bad video_index: {}", id, e);
|
2021-02-11 13:45:56 -05:00
|
|
|
printed_error = true;
|
2021-02-11 23:03:28 -05:00
|
|
|
if opts.trash_corrupt_rows {
|
|
|
|
ctx.rows_to_delete.insert(id);
|
|
|
|
ctx.files_to_trash.insert((dir_id, id));
|
|
|
|
}
|
2018-03-09 10:31:48 -05:00
|
|
|
continue;
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2018-03-09 10:31:48 -05:00
|
|
|
};
|
2021-02-17 01:15:54 -05:00
|
|
|
stream
|
|
|
|
.recordings
|
|
|
|
.entry(id.recording())
|
|
|
|
.or_insert_with(Recording::default)
|
|
|
|
.playback_row = Some(s);
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recording_integrity row.
|
|
|
|
{
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = conn.prepare_cached(
|
|
|
|
r#"
|
2018-03-09 10:31:48 -05:00
|
|
|
select
|
|
|
|
composite_id
|
|
|
|
from
|
|
|
|
recording_integrity
|
|
|
|
where
|
|
|
|
composite_id between ? and ?
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = stmt.query(params![start.0, end.0])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let id = CompositeId(row.get(0)?);
|
2021-02-17 01:15:54 -05:00
|
|
|
stream
|
|
|
|
.recordings
|
|
|
|
.entry(id.recording())
|
|
|
|
.or_insert_with(Recording::default)
|
|
|
|
.integrity_row = true;
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:04:07 -05:00
|
|
|
for (&id, recording) in &stream.recordings {
|
2018-03-09 10:31:48 -05:00
|
|
|
let id = CompositeId::new(stream_id, id);
|
2021-02-11 18:04:07 -05:00
|
|
|
|
|
|
|
// Files should have recording and playback rows if they aren't marked
|
|
|
|
// as garbage (deletion in progress) and aren't newer than
|
|
|
|
// cum_recordings (were being written when the process died).
|
|
|
|
let db_rows_expected = !recording.garbage_row && id.recording() < cum_recordings;
|
|
|
|
|
2018-03-09 10:31:48 -05:00
|
|
|
let r = match recording.recording_row {
|
2021-02-11 18:04:07 -05:00
|
|
|
Some(ref r) => {
|
|
|
|
if !db_rows_expected {
|
|
|
|
error!("Unexpected recording row for {}: {:#?}", id, recording);
|
|
|
|
printed_error = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2018-03-09 10:31:48 -05:00
|
|
|
None => {
|
2021-02-11 18:04:07 -05:00
|
|
|
if db_rows_expected {
|
2018-03-09 10:31:48 -05:00
|
|
|
error!("Missing recording row for {}: {:#?}", id, recording);
|
2021-02-11 23:03:28 -05:00
|
|
|
if opts.trash_orphan_sample_files {
|
|
|
|
ctx.files_to_trash.insert((dir_id, id));
|
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
if opts.delete_orphan_rows {
|
|
|
|
// also delete playback/integrity rows, if any.
|
2021-02-11 23:03:28 -05:00
|
|
|
ctx.rows_to_delete.insert(id);
|
|
|
|
}
|
2021-02-11 13:45:56 -05:00
|
|
|
printed_error = true;
|
2021-02-11 18:04:07 -05:00
|
|
|
} else if recording.playback_row.is_some() {
|
|
|
|
error!("Unexpected playback row for {}: {:#?}", id, recording);
|
2021-02-11 23:03:28 -05:00
|
|
|
if opts.delete_orphan_rows {
|
|
|
|
ctx.rows_to_delete.insert(id);
|
|
|
|
}
|
2021-02-11 18:04:07 -05:00
|
|
|
printed_error = true;
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
2018-03-01 20:07:42 -05:00
|
|
|
continue;
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2018-03-01 20:07:42 -05:00
|
|
|
};
|
2018-03-09 10:31:48 -05:00
|
|
|
match recording.playback_row {
|
|
|
|
Some(ref p) => {
|
|
|
|
if r != p {
|
2021-02-17 01:15:54 -05:00
|
|
|
error!(
|
|
|
|
"Recording {} summary doesn't match video_index: {:#?}",
|
|
|
|
id, recording
|
|
|
|
);
|
2021-02-11 13:45:56 -05:00
|
|
|
printed_error = true;
|
2018-03-09 10:31:48 -05:00
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2021-02-11 13:45:56 -05:00
|
|
|
None => {
|
|
|
|
error!("Recording {} missing playback row: {:#?}", id, recording);
|
|
|
|
printed_error = true;
|
2021-02-11 23:10:38 -05:00
|
|
|
if opts.trash_orphan_sample_files {
|
|
|
|
ctx.files_to_trash.insert((dir_id, id));
|
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
if opts.delete_orphan_rows {
|
|
|
|
// also delete recording/integrity rows, if any.
|
2021-02-11 23:10:38 -05:00
|
|
|
ctx.rows_to_delete.insert(id);
|
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
2018-03-09 10:31:48 -05:00
|
|
|
match recording.file {
|
2021-02-17 01:15:54 -05:00
|
|
|
Some(len) => {
|
|
|
|
if opts.compare_lens && r.bytes != len {
|
|
|
|
error!("Recording {} length mismatch: {:#?}", id, recording);
|
|
|
|
printed_error = true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 13:45:56 -05:00
|
|
|
None => {
|
|
|
|
error!("Recording {} missing file: {:#?}", id, recording);
|
2021-02-11 23:03:28 -05:00
|
|
|
if opts.delete_orphan_rows {
|
|
|
|
ctx.rows_to_delete.insert(id);
|
|
|
|
}
|
2021-02-11 13:45:56 -05:00
|
|
|
printed_error = true;
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 13:45:56 -05:00
|
|
|
Ok(printed_error)
|
2018-03-01 20:07:42 -05:00
|
|
|
}
|