2019-07-05 00:22:45 -04:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2019 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2019-07-05 00:22:45 -04:00
|
|
|
|
|
|
|
/// Upgrades a version 4 schema to a version 5 schema.
|
|
|
|
///
|
|
|
|
/// This just handles the directory meta files. If they're already in the new format, great.
|
|
|
|
/// Otherwise, verify they are consistent with the database then upgrade them.
|
|
|
|
use crate::db::FromSqlUuid;
|
|
|
|
use crate::{dir, schema};
|
2020-11-23 03:23:03 -05:00
|
|
|
use cstr::cstr;
|
2021-02-17 01:15:54 -05:00
|
|
|
use failure::{bail, Error, Fail};
|
2019-07-25 01:18:44 -04:00
|
|
|
use log::info;
|
2019-07-12 00:59:01 -04:00
|
|
|
use nix::fcntl::{FlockArg, OFlag};
|
|
|
|
use nix::sys::stat::Mode;
|
2020-11-23 03:23:03 -05:00
|
|
|
use protobuf::Message;
|
2019-07-05 00:22:45 -04:00
|
|
|
use rusqlite::params;
|
|
|
|
use std::io::{Read, Write};
|
2019-07-12 00:59:01 -04:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-07-25 01:18:44 -04:00
|
|
|
use uuid::Uuid;
|
2019-07-05 00:22:45 -04:00
|
|
|
|
|
|
|
const FIXED_DIR_META_LEN: usize = 512;
|
|
|
|
|
2019-07-25 01:18:44 -04:00
|
|
|
/// Maybe upgrades the `meta` file, returning if an upgrade happened (and thus a sync is needed).
|
|
|
|
fn maybe_upgrade_meta(dir: &dir::Fd, db_meta: &schema::DirMeta) -> Result<bool, Error> {
|
|
|
|
let tmp_path = cstr!("meta.tmp");
|
|
|
|
let meta_path = cstr!("meta");
|
|
|
|
let mut f = crate::fs::openat(dir.as_raw_fd(), meta_path, OFlag::O_RDONLY, Mode::empty())?;
|
|
|
|
let mut data = Vec::new();
|
|
|
|
f.read_to_end(&mut data)?;
|
|
|
|
if data.len() == FIXED_DIR_META_LEN {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut s = protobuf::CodedInputStream::from_bytes(&data);
|
|
|
|
let mut dir_meta = schema::DirMeta::new();
|
2021-02-17 01:15:54 -05:00
|
|
|
dir_meta
|
|
|
|
.merge_from(&mut s)
|
2019-07-25 01:18:44 -04:00
|
|
|
.map_err(|e| e.context("Unable to parse metadata proto: {}"))?;
|
2021-09-22 15:39:02 -04:00
|
|
|
if let Err(e) = dir::SampleFileDir::check_consistent(&db_meta, &dir_meta) {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
2021-09-22 15:39:02 -04:00
|
|
|
"Inconsistent db_meta={:?} dir_meta={:?}: {}",
|
2021-02-17 01:15:54 -05:00
|
|
|
&db_meta,
|
2021-09-22 15:39:02 -04:00
|
|
|
&dir_meta,
|
|
|
|
e
|
2021-02-17 01:15:54 -05:00
|
|
|
);
|
2019-07-25 01:18:44 -04:00
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut f = crate::fs::openat(
|
|
|
|
dir.as_raw_fd(),
|
|
|
|
tmp_path,
|
|
|
|
OFlag::O_CREAT | OFlag::O_TRUNC | OFlag::O_WRONLY,
|
|
|
|
Mode::S_IRUSR | Mode::S_IWUSR,
|
|
|
|
)?;
|
|
|
|
let mut data = dir_meta
|
|
|
|
.write_length_delimited_to_bytes()
|
|
|
|
.expect("proto3->vec is infallible");
|
2019-07-25 01:18:44 -04:00
|
|
|
if data.len() > FIXED_DIR_META_LEN {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
|
|
|
"Length-delimited DirMeta message requires {} bytes, over limit of {}",
|
|
|
|
data.len(),
|
|
|
|
FIXED_DIR_META_LEN
|
|
|
|
);
|
2019-07-25 01:18:44 -04:00
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
data.resize(FIXED_DIR_META_LEN, 0); // pad to required length.
|
2019-07-25 01:18:44 -04:00
|
|
|
f.write_all(&data)?;
|
|
|
|
f.sync_all()?;
|
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
nix::fcntl::renameat(
|
|
|
|
Some(dir.as_raw_fd()),
|
|
|
|
tmp_path,
|
|
|
|
Some(dir.as_raw_fd()),
|
|
|
|
meta_path,
|
|
|
|
)?;
|
2019-07-25 01:18:44 -04:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Looks for uuid-based filenames and deletes them.
|
|
|
|
///
|
|
|
|
/// The v1->v3 migration failed to remove garbage files prior to 433be217. Let's have a clean slate
|
|
|
|
/// at v5.
|
|
|
|
///
|
|
|
|
/// Returns true if something was done (and thus a sync is needed).
|
|
|
|
fn maybe_cleanup_garbage_uuids(dir: &dir::Fd) -> Result<bool, Error> {
|
|
|
|
let mut need_sync = false;
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut dir2 = nix::dir::Dir::openat(
|
|
|
|
dir.as_raw_fd(),
|
|
|
|
".",
|
|
|
|
OFlag::O_DIRECTORY | OFlag::O_RDONLY,
|
|
|
|
Mode::empty(),
|
|
|
|
)?;
|
2019-07-25 01:18:44 -04:00
|
|
|
for e in dir2.iter() {
|
|
|
|
let e = e?;
|
|
|
|
let f = e.file_name();
|
|
|
|
info!("file: {}", f.to_str().unwrap());
|
|
|
|
let f_str = match f.to_str() {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
if Uuid::parse_str(f_str).is_ok() {
|
|
|
|
info!("removing leftover garbage file {}", f_str);
|
2021-02-17 01:15:54 -05:00
|
|
|
nix::unistd::unlinkat(
|
|
|
|
Some(dir.as_raw_fd()),
|
|
|
|
f,
|
|
|
|
nix::unistd::UnlinkatFlags::NoRemoveDir,
|
|
|
|
)?;
|
2019-07-25 01:18:44 -04:00
|
|
|
need_sync = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(need_sync)
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:22:45 -04:00
|
|
|
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
|
|
|
let db_uuid: FromSqlUuid =
|
|
|
|
tx.query_row_and_then(r"select uuid from meta", params![], |row| row.get(0))?;
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = tx.prepare(
|
|
|
|
r#"
|
2019-07-05 00:22:45 -04:00
|
|
|
select
|
|
|
|
d.path,
|
|
|
|
d.uuid,
|
|
|
|
d.last_complete_open_id,
|
|
|
|
o.uuid
|
|
|
|
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
|
|
|
"#,
|
|
|
|
)?;
|
2019-07-05 00:22:45 -04:00
|
|
|
let mut rows = stmt.query(params![])?;
|
|
|
|
while let Some(row) = rows.next()? {
|
2021-05-17 13:50:12 -04:00
|
|
|
let path = row.get_ref(0)?.as_str()?;
|
2019-07-25 01:18:44 -04:00
|
|
|
info!("path: {}", path);
|
2019-07-05 00:22:45 -04:00
|
|
|
let dir_uuid: FromSqlUuid = row.get(1)?;
|
|
|
|
let open_id: Option<u32> = row.get(2)?;
|
|
|
|
let open_uuid: Option<FromSqlUuid> = row.get(3)?;
|
|
|
|
let mut db_meta = schema::DirMeta::new();
|
|
|
|
db_meta.db_uuid.extend_from_slice(&db_uuid.0.as_bytes()[..]);
|
2021-02-17 01:15:54 -05:00
|
|
|
db_meta
|
|
|
|
.dir_uuid
|
|
|
|
.extend_from_slice(&dir_uuid.0.as_bytes()[..]);
|
2019-07-05 00:22:45 -04:00
|
|
|
match (open_id, open_uuid) {
|
|
|
|
(Some(id), Some(uuid)) => {
|
2020-11-23 03:23:03 -05:00
|
|
|
let mut o = db_meta.last_complete_open.set_default();
|
2019-07-05 00:22:45 -04:00
|
|
|
o.id = id;
|
|
|
|
o.uuid.extend_from_slice(&uuid.0.as_bytes()[..]);
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
|
|
|
(None, None) => {}
|
2019-07-05 00:22:45 -04:00
|
|
|
_ => bail!("open table missing id"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let dir = dir::Fd::open(path, false)?;
|
2021-02-11 13:45:56 -05:00
|
|
|
dir.lock(FlockArg::LockExclusiveNonblock)
|
|
|
|
.map_err(|e| e.context(format!("unable to lock dir {}", path)))?;
|
2019-07-25 01:18:44 -04:00
|
|
|
|
|
|
|
let mut need_sync = maybe_upgrade_meta(&dir, &db_meta)?;
|
|
|
|
if maybe_cleanup_garbage_uuids(&dir)? {
|
|
|
|
need_sync = true;
|
2019-07-05 00:22:45 -04:00
|
|
|
}
|
2019-07-25 01:18:44 -04:00
|
|
|
|
|
|
|
if need_sync {
|
|
|
|
dir.sync()?;
|
2019-07-05 00:22:45 -04:00
|
|
|
}
|
2019-07-25 01:18:44 -04:00
|
|
|
info!("done with path: {}", path);
|
2019-07-05 00:22:45 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|