2018-03-04 15:24:24 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-06-04 22:17:32 -04:00
|
|
|
// Copyright (C) 2021 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception
|
2016-11-25 17:34:00 -05:00
|
|
|
|
|
|
|
//! Sample file directory management.
|
|
|
|
//!
|
2021-04-10 20:34:52 -04:00
|
|
|
//! This mostly includes opening a directory and looking for recordings within it.
|
|
|
|
//! Updates to the directory happen through [crate::writer].
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2021-06-04 22:17:32 -04:00
|
|
|
mod reader;
|
|
|
|
|
2019-07-05 00:22:45 -04:00
|
|
|
use crate::coding;
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::db::CompositeId;
|
2019-07-12 00:59:01 -04:00
|
|
|
use crate::schema;
|
2020-11-23 03:23:03 -05:00
|
|
|
use cstr::cstr;
|
2021-02-17 01:15:54 -05:00
|
|
|
use failure::{bail, format_err, Error, Fail};
|
2018-12-28 22:53:29 -05:00
|
|
|
use log::warn;
|
2019-07-12 00:59:01 -04:00
|
|
|
use nix::sys::statvfs::Statvfs;
|
2021-02-17 01:15:54 -05:00
|
|
|
use nix::{
|
|
|
|
fcntl::{FlockArg, OFlag},
|
|
|
|
sys::stat::Mode,
|
|
|
|
NixPath,
|
|
|
|
};
|
|
|
|
use protobuf::Message;
|
2020-04-18 01:41:55 -04:00
|
|
|
use std::ffi::CStr;
|
2016-11-25 17:34:00 -05:00
|
|
|
use std::fs;
|
2019-07-12 00:59:01 -04:00
|
|
|
use std::io::{Read, Write};
|
2021-06-04 22:17:32 -04:00
|
|
|
use std::ops::Range;
|
2019-07-12 14:05:36 -04:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2018-02-22 19:35:34 -05:00
|
|
|
use std::sync::Arc;
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2019-07-05 00:22:45 -04:00
|
|
|
/// The fixed length of a directory's `meta` file.
|
|
|
|
///
|
2021-04-10 20:34:52 -04:00
|
|
|
/// See `DirMeta` comments within `proto/schema.proto` for more explanation.
|
2019-07-05 00:22:45 -04:00
|
|
|
const FIXED_DIR_META_LEN: usize = 512;
|
|
|
|
|
2018-02-12 01:45:51 -05:00
|
|
|
/// A sample file directory. Typically one per physical disk drive.
|
2016-11-25 17:34:00 -05:00
|
|
|
///
|
2021-04-10 20:34:52 -04:00
|
|
|
/// If the directory is used for writing, [crate::writer::start_syncer] should be
|
|
|
|
/// called to start a background thread. This thread manages deleting files and
|
|
|
|
/// writing new files. It synces the directory and commits these operations to
|
|
|
|
/// the database in the correct order to maintain the invariants described in
|
|
|
|
/// `design/schema.md`.
|
2018-02-12 01:45:51 -05:00
|
|
|
#[derive(Debug)]
|
2016-11-25 17:34:00 -05:00
|
|
|
pub struct SampleFileDir {
|
2021-04-10 20:34:52 -04:00
|
|
|
/// The open file descriptor for the directory. The worker created by
|
|
|
|
/// [crate::writer::start_syncer] uses it to create files and sync the
|
|
|
|
/// directory. Other threads use it to open sample files for reading during
|
|
|
|
/// video serving.
|
2021-06-04 22:17:32 -04:00
|
|
|
pub(crate) fd: Arc<Fd>,
|
|
|
|
|
|
|
|
reader: reader::Reader,
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2021-04-10 20:34:52 -04:00
|
|
|
/// The on-disk filename of a recording file within the sample file directory.
|
|
|
|
/// This is the [`CompositeId`](crate::db::CompositeId) as 16 hexadigits. It's
|
|
|
|
/// null-terminated so it can be passed to system calls without copying.
|
2019-07-12 00:59:01 -04:00
|
|
|
pub(crate) struct CompositeIdPath([u8; 17]);
|
|
|
|
|
|
|
|
impl CompositeIdPath {
|
|
|
|
pub(crate) fn from(id: CompositeId) -> Self {
|
|
|
|
let mut buf = [0u8; 17];
|
|
|
|
write!(&mut buf[..16], "{:016x}", id.0).expect("can't format id to pathname buf");
|
|
|
|
CompositeIdPath(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NixPath for CompositeIdPath {
|
2021-02-17 01:15:54 -05:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
16
|
|
|
|
}
|
2019-07-12 00:59:01 -04:00
|
|
|
|
|
|
|
fn with_nix_path<T, F>(&self, f: F) -> Result<T, nix::Error>
|
2021-02-17 01:15:54 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(&CStr) -> T,
|
|
|
|
{
|
2019-07-12 00:59:01 -04:00
|
|
|
let p = CStr::from_bytes_with_nul(&self.0[..]).expect("no interior nuls");
|
|
|
|
Ok(f(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:34:00 -05:00
|
|
|
/// A file descriptor associated with a directory (not necessarily the sample file dir).
|
2018-02-12 01:45:51 -05:00
|
|
|
#[derive(Debug)]
|
2019-07-12 00:59:01 -04:00
|
|
|
pub struct Fd(std::os::unix::io::RawFd);
|
|
|
|
|
|
|
|
impl std::os::unix::io::AsRawFd for Fd {
|
2021-02-17 01:15:54 -05:00
|
|
|
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
|
|
|
|
self.0
|
|
|
|
}
|
2019-07-12 00:59:01 -04:00
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
|
|
|
|
impl Drop for Fd {
|
|
|
|
fn drop(&mut self) {
|
2019-07-12 00:59:01 -04:00
|
|
|
if let Err(e) = nix::unistd::close(self.0) {
|
2016-11-25 17:34:00 -05:00
|
|
|
warn!("Unable to close sample file dir: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Fd {
|
|
|
|
/// Opens the given path as a directory.
|
2020-04-18 01:41:55 -04:00
|
|
|
pub fn open<P: ?Sized + NixPath>(path: &P, mkdir: bool) -> Result<Fd, nix::Error> {
|
2019-07-12 00:59:01 -04:00
|
|
|
if mkdir {
|
2020-04-18 01:41:55 -04:00
|
|
|
match nix::unistd::mkdir(path, nix::sys::stat::Mode::S_IRWXU) {
|
2021-02-17 01:15:54 -05:00
|
|
|
Ok(()) | Err(nix::Error::Sys(nix::errno::Errno::EEXIST)) => {}
|
2019-07-12 00:59:01 -04:00
|
|
|
Err(e) => return Err(e),
|
2018-02-12 01:45:51 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 01:41:55 -04:00
|
|
|
let fd = nix::fcntl::open(path, OFlag::O_DIRECTORY | OFlag::O_RDONLY, Mode::empty())?;
|
2016-11-25 17:34:00 -05:00
|
|
|
Ok(Fd(fd))
|
|
|
|
}
|
|
|
|
|
2021-04-10 20:34:52 -04:00
|
|
|
/// `fsync`s this directory, causing all file metadata to be committed to permanent storage.
|
2019-07-12 00:59:01 -04:00
|
|
|
pub(crate) fn sync(&self) -> Result<(), nix::Error> {
|
|
|
|
nix::unistd::fsync(self.0)
|
2018-03-01 12:26:03 -05:00
|
|
|
}
|
|
|
|
|
2016-11-25 17:34:00 -05:00
|
|
|
/// Locks the directory with the specified `flock` operation.
|
2019-07-12 00:59:01 -04:00
|
|
|
pub fn lock(&self, arg: FlockArg) -> Result<(), nix::Error> {
|
|
|
|
nix::fcntl::flock(self.0, arg)
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
2017-02-05 22:58:41 -05:00
|
|
|
|
2021-04-10 20:34:52 -04:00
|
|
|
/// Returns information about the filesystem on which this directory lives.
|
2019-07-12 00:59:01 -04:00
|
|
|
pub fn statfs(&self) -> Result<nix::sys::statvfs::Statvfs, nix::Error> {
|
|
|
|
nix::sys::statvfs::fstatvfs(self)
|
2017-02-05 22:58:41 -05:00
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2018-03-01 12:26:03 -05:00
|
|
|
/// Reads `dir`'s metadata. If none is found, returns an empty proto.
|
|
|
|
pub(crate) fn read_meta(dir: &Fd) -> Result<schema::DirMeta, Error> {
|
|
|
|
let mut meta = schema::DirMeta::default();
|
2019-07-12 14:05:36 -04:00
|
|
|
let mut f = match crate::fs::openat(dir.0, cstr!("meta"), OFlag::O_RDONLY, Mode::empty()) {
|
2018-03-01 12:26:03 -05:00
|
|
|
Err(e) => {
|
2019-07-12 00:59:01 -04:00
|
|
|
if e == nix::Error::Sys(nix::errno::Errno::ENOENT) {
|
2018-03-01 12:26:03 -05:00
|
|
|
return Ok(meta);
|
|
|
|
}
|
|
|
|
return Err(e.into());
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2018-03-01 12:26:03 -05:00
|
|
|
Ok(f) => f,
|
|
|
|
};
|
|
|
|
let mut data = Vec::new();
|
|
|
|
f.read_to_end(&mut data)?;
|
2019-07-05 00:22:45 -04:00
|
|
|
let (len, pos) = coding::decode_varint32(&data, 0)
|
|
|
|
.map_err(|_| format_err!("Unable to decode varint length in meta file"))?;
|
|
|
|
if data.len() != FIXED_DIR_META_LEN || len as usize + pos > FIXED_DIR_META_LEN {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
|
|
|
"Expected a {}-byte file with a varint length of a DirMeta message; got \
|
|
|
|
a {}-byte file with length {}",
|
|
|
|
FIXED_DIR_META_LEN,
|
|
|
|
data.len(),
|
|
|
|
len
|
|
|
|
);
|
2019-07-05 00:22:45 -04:00
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
let data = &data[pos..pos + len as usize];
|
2018-03-01 12:26:03 -05:00
|
|
|
let mut s = protobuf::CodedInputStream::from_bytes(&data);
|
2021-02-17 01:15:54 -05:00
|
|
|
meta.merge_from(&mut s)
|
|
|
|
.map_err(|e| e.context("Unable to parse metadata proto"))?;
|
2018-03-01 12:26:03 -05:00
|
|
|
Ok(meta)
|
|
|
|
}
|
|
|
|
|
2021-04-10 20:34:52 -04:00
|
|
|
/// Writes `dirfd`'s metadata, clobbering existing data.
|
2019-07-12 14:05:36 -04:00
|
|
|
pub(crate) fn write_meta(dirfd: RawFd, meta: &schema::DirMeta) -> Result<(), Error> {
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut data = meta
|
|
|
|
.write_length_delimited_to_bytes()
|
|
|
|
.expect("proto3->vec is infallible");
|
2019-07-05 00:22:45 -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-05 00:22:45 -04:00
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
data.resize(FIXED_DIR_META_LEN, 0); // pad to required length.
|
|
|
|
let mut f = crate::fs::openat(
|
|
|
|
dirfd,
|
|
|
|
cstr!("meta"),
|
|
|
|
OFlag::O_CREAT | OFlag::O_WRONLY,
|
|
|
|
Mode::S_IRUSR | Mode::S_IWUSR,
|
|
|
|
)
|
|
|
|
.map_err(|e| e.context("Unable to open meta file"))?;
|
|
|
|
let stat = f
|
|
|
|
.metadata()
|
|
|
|
.map_err(|e| e.context("Unable to stat meta file"))?;
|
2019-07-05 00:22:45 -04:00
|
|
|
if stat.len() == 0 {
|
|
|
|
// Need to sync not only the data but also the file metadata and dirent.
|
2021-02-17 01:15:54 -05:00
|
|
|
f.write_all(&data)
|
|
|
|
.map_err(|e| e.context("Unable to write to meta file"))?;
|
|
|
|
f.sync_all()
|
|
|
|
.map_err(|e| e.context("Unable to sync meta file"))?;
|
2021-02-11 13:45:56 -05:00
|
|
|
nix::unistd::fsync(dirfd).map_err(|e| e.context("Unable to sync dir"))?;
|
2019-07-05 00:22:45 -04:00
|
|
|
} else if stat.len() == FIXED_DIR_META_LEN as u64 {
|
|
|
|
// Just syncing the data will suffice; existing metadata and dirent are fine.
|
2021-02-17 01:15:54 -05:00
|
|
|
f.write_all(&data)
|
|
|
|
.map_err(|e| e.context("Unable to write to meta file"))?;
|
|
|
|
f.sync_data()
|
|
|
|
.map_err(|e| e.context("Unable to sync meta file"))?;
|
2019-07-05 00:22:45 -04:00
|
|
|
} else {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
|
|
|
"Existing meta file is {}-byte; expected {}",
|
|
|
|
stat.len(),
|
|
|
|
FIXED_DIR_META_LEN
|
|
|
|
);
|
2019-07-05 00:22:45 -04:00
|
|
|
}
|
2018-03-01 12:26:03 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:34:00 -05:00
|
|
|
impl SampleFileDir {
|
2018-02-15 02:10:10 -05:00
|
|
|
/// Opens the directory using the given metadata.
|
|
|
|
///
|
|
|
|
/// `db_meta.in_progress_open` should be filled if the directory should be opened in read/write
|
|
|
|
/// mode; absent in read-only mode.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub fn open(path: &str, db_meta: &schema::DirMeta) -> Result<Arc<SampleFileDir>, Error> {
|
2018-02-15 02:10:10 -05:00
|
|
|
let read_write = db_meta.in_progress_open.is_some();
|
|
|
|
let s = SampleFileDir::open_self(path, false)?;
|
2019-07-12 00:59:01 -04:00
|
|
|
s.fd.lock(if read_write {
|
2021-02-17 01:15:54 -05:00
|
|
|
FlockArg::LockExclusiveNonblock
|
|
|
|
} else {
|
|
|
|
FlockArg::LockSharedNonblock
|
|
|
|
})
|
|
|
|
.map_err(|e| e.context(format!("unable to lock dir {}", path)))?;
|
2021-02-11 13:45:56 -05:00
|
|
|
let dir_meta = read_meta(&s.fd).map_err(|e| e.context("unable to read meta file"))?;
|
2018-02-15 02:10:10 -05:00
|
|
|
if !SampleFileDir::consistent(db_meta, &dir_meta) {
|
2021-02-17 01:15:54 -05:00
|
|
|
let serialized = db_meta
|
|
|
|
.write_length_delimited_to_bytes()
|
|
|
|
.expect("proto3->vec is infallible");
|
|
|
|
bail!(
|
|
|
|
"metadata mismatch.\ndb: {:#?}\ndir: {:#?}\nserialized db: {:#?}",
|
|
|
|
db_meta,
|
|
|
|
&dir_meta,
|
|
|
|
&serialized
|
|
|
|
);
|
2018-02-15 02:10:10 -05:00
|
|
|
}
|
|
|
|
if db_meta.in_progress_open.is_some() {
|
|
|
|
s.write_meta(db_meta)?;
|
|
|
|
}
|
|
|
|
Ok(s)
|
2018-02-12 01:45:51 -05:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:10:10 -05:00
|
|
|
/// Returns true if the existing directory and database metadata are consistent; the directory
|
|
|
|
/// is then openable.
|
2019-07-05 00:22:45 -04:00
|
|
|
pub(crate) fn consistent(db_meta: &schema::DirMeta, dir_meta: &schema::DirMeta) -> bool {
|
2021-02-17 01:15:54 -05:00
|
|
|
if dir_meta.db_uuid != db_meta.db_uuid {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if dir_meta.dir_uuid != db_meta.dir_uuid {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-15 02:10:10 -05:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
if db_meta.last_complete_open.is_some()
|
|
|
|
&& (db_meta.last_complete_open != dir_meta.last_complete_open
|
|
|
|
&& db_meta.last_complete_open != dir_meta.in_progress_open)
|
|
|
|
{
|
2018-02-15 02:10:10 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if db_meta.last_complete_open.is_none() && dir_meta.last_complete_open.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
pub(crate) fn create(
|
|
|
|
path: &str,
|
|
|
|
db_meta: &schema::DirMeta,
|
|
|
|
) -> Result<Arc<SampleFileDir>, Error> {
|
2018-02-15 02:10:10 -05:00
|
|
|
let s = SampleFileDir::open_self(path, true)?;
|
2021-02-11 13:45:56 -05:00
|
|
|
s.fd.lock(FlockArg::LockExclusiveNonblock)
|
|
|
|
.map_err(|e| e.context(format!("unable to lock dir {}", path)))?;
|
2018-03-01 12:26:03 -05:00
|
|
|
let old_meta = read_meta(&s.fd)?;
|
2018-02-15 02:10:10 -05:00
|
|
|
|
|
|
|
// Verify metadata. We only care that it hasn't been completely opened.
|
|
|
|
// Partial opening by this or another database is fine; we won't overwrite anything.
|
|
|
|
if old_meta.last_complete_open.is_some() {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
|
|
|
"Can't create dir at path {}: is already in use:\n{:?}",
|
|
|
|
path,
|
|
|
|
old_meta
|
|
|
|
);
|
2018-02-15 02:10:10 -05:00
|
|
|
}
|
2019-07-12 14:05:36 -04:00
|
|
|
if !s.is_empty()? {
|
2018-03-01 15:24:32 -05:00
|
|
|
bail!("Can't create dir at path {} with existing files", path);
|
|
|
|
}
|
2018-02-15 02:10:10 -05:00
|
|
|
s.write_meta(db_meta)?;
|
|
|
|
Ok(s)
|
2018-02-12 01:45:51 -05:00
|
|
|
}
|
|
|
|
|
2019-07-12 14:05:36 -04:00
|
|
|
pub(crate) fn opendir(&self) -> Result<nix::dir::Dir, nix::Error> {
|
2021-02-17 01:15:54 -05:00
|
|
|
nix::dir::Dir::openat(
|
|
|
|
self.fd.as_raw_fd(),
|
|
|
|
".",
|
|
|
|
OFlag::O_DIRECTORY | OFlag::O_RDONLY,
|
|
|
|
Mode::empty(),
|
|
|
|
)
|
2019-07-12 14:05:36 -04:00
|
|
|
}
|
|
|
|
|
2018-03-01 15:24:32 -05:00
|
|
|
/// Determines if the directory is empty, aside form metadata.
|
2019-07-12 14:05:36 -04:00
|
|
|
pub(crate) fn is_empty(&self) -> Result<bool, Error> {
|
|
|
|
let mut dir = self.opendir()?;
|
|
|
|
for e in dir.iter() {
|
2018-03-01 15:24:32 -05:00
|
|
|
let e = e?;
|
2019-07-12 14:05:36 -04:00
|
|
|
match e.file_name().to_bytes() {
|
2018-03-01 15:24:32 -05:00
|
|
|
b"." | b".." => continue,
|
2021-02-17 01:15:54 -05:00
|
|
|
b"meta" => continue, // existing metadata is fine.
|
2018-03-01 15:24:32 -05:00
|
|
|
_ => return Ok(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2018-02-12 01:45:51 -05:00
|
|
|
fn open_self(path: &str, create: bool) -> Result<Arc<SampleFileDir>, Error> {
|
2021-06-04 22:17:32 -04:00
|
|
|
let fd = Arc::new(Fd::open(path, create)?);
|
|
|
|
let reader = reader::Reader::spawn(path, fd.clone());
|
2021-06-04 23:25:19 -04:00
|
|
|
Ok(Arc::new(SampleFileDir { fd, reader }))
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Opens the given sample file for reading.
|
2021-06-04 22:17:32 -04:00
|
|
|
pub fn open_file(&self, composite_id: CompositeId, range: Range<u64>) -> reader::FileStream {
|
|
|
|
self.reader.open_file(composite_id, range)
|
2018-02-15 02:10:10 -05:00
|
|
|
}
|
|
|
|
|
2019-07-12 00:59:01 -04:00
|
|
|
pub fn create_file(&self, composite_id: CompositeId) -> Result<fs::File, nix::Error> {
|
|
|
|
let p = CompositeIdPath::from(composite_id);
|
2021-02-17 01:15:54 -05:00
|
|
|
crate::fs::openat(
|
|
|
|
self.fd.0,
|
|
|
|
&p,
|
|
|
|
OFlag::O_WRONLY | OFlag::O_EXCL | OFlag::O_CREAT,
|
|
|
|
Mode::S_IRUSR | Mode::S_IWUSR,
|
|
|
|
)
|
2018-03-04 15:24:24 -05:00
|
|
|
}
|
|
|
|
|
2018-03-01 00:21:47 -05:00
|
|
|
pub(crate) fn write_meta(&self, meta: &schema::DirMeta) -> Result<(), Error> {
|
2019-07-12 14:05:36 -04:00
|
|
|
write_meta(self.fd.0, meta)
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
pub fn statfs(&self) -> Result<Statvfs, nix::Error> {
|
|
|
|
self.fd.statfs()
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
|
|
|
|
/// Unlinks the given sample file within this directory.
|
2019-07-12 00:59:01 -04:00
|
|
|
pub(crate) fn unlink_file(&self, id: CompositeId) -> Result<(), nix::Error> {
|
|
|
|
let p = CompositeIdPath::from(id);
|
2019-07-17 17:32:09 -04:00
|
|
|
nix::unistd::unlinkat(Some(self.fd.0), &p, nix::unistd::UnlinkatFlags::NoRemoveDir)
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Syncs the directory itself.
|
2019-07-12 00:59:01 -04:00
|
|
|
pub(crate) fn sync(&self) -> Result<(), nix::Error> {
|
2018-03-01 12:26:03 -05:00
|
|
|
self.fd.sync()
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:22:45 -04:00
|
|
|
/// Parses a composite id filename.
|
2018-02-20 13:11:10 -05:00
|
|
|
///
|
2021-04-10 20:34:52 -04:00
|
|
|
/// These are exactly 16 bytes, lowercase hex, as created by [CompositeIdPath].
|
2018-03-01 20:07:42 -05:00
|
|
|
pub(crate) fn parse_id(id: &[u8]) -> Result<CompositeId, ()> {
|
2018-02-20 13:11:10 -05:00
|
|
|
if id.len() != 16 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
let mut v: u64 = 0;
|
2021-05-17 17:31:50 -04:00
|
|
|
for b in id {
|
2021-02-17 01:15:54 -05:00
|
|
|
v = (v << 4)
|
2021-05-17 17:31:50 -04:00
|
|
|
| match b {
|
2021-02-17 01:15:54 -05:00
|
|
|
b @ b'0'..=b'9' => b - b'0',
|
|
|
|
b @ b'a'..=b'f' => b - b'a' + 10,
|
|
|
|
_ => return Err(()),
|
|
|
|
} as u64;
|
2018-02-20 13:11:10 -05:00
|
|
|
}
|
|
|
|
Ok(CompositeId(v as i64))
|
|
|
|
}
|
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-05 00:22:45 -04:00
|
|
|
use super::*;
|
|
|
|
|
2018-02-20 13:11:10 -05:00
|
|
|
#[test]
|
|
|
|
fn parse_id() {
|
|
|
|
use super::parse_id;
|
|
|
|
assert_eq!(parse_id(b"0000000000000000").unwrap().0, 0);
|
|
|
|
assert_eq!(parse_id(b"0000000100000002").unwrap().0, 0x0000000100000002);
|
|
|
|
parse_id(b"").unwrap_err();
|
|
|
|
parse_id(b"meta").unwrap_err();
|
|
|
|
parse_id(b"0").unwrap_err();
|
|
|
|
parse_id(b"000000010000000x").unwrap_err();
|
|
|
|
}
|
2019-07-05 00:22:45 -04:00
|
|
|
|
|
|
|
/// Ensures that a DirMeta with all fields filled fits within the maximum size.
|
|
|
|
#[test]
|
|
|
|
fn max_len_meta() {
|
|
|
|
let mut meta = schema::DirMeta::new();
|
|
|
|
let fake_uuid = &[0u8; 16][..];
|
|
|
|
meta.db_uuid.extend_from_slice(fake_uuid);
|
|
|
|
meta.dir_uuid.extend_from_slice(fake_uuid);
|
2020-11-23 03:23:03 -05:00
|
|
|
{
|
|
|
|
let o = meta.last_complete_open.set_default();
|
|
|
|
o.id = u32::max_value();
|
|
|
|
o.uuid.extend_from_slice(fake_uuid);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let o = meta.in_progress_open.set_default();
|
|
|
|
o.id = u32::max_value();
|
|
|
|
o.uuid.extend_from_slice(fake_uuid);
|
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
let data = meta
|
|
|
|
.write_length_delimited_to_bytes()
|
|
|
|
.expect("proto3->vec is infallible");
|
|
|
|
assert!(
|
|
|
|
data.len() <= FIXED_DIR_META_LEN,
|
|
|
|
"{} vs {}",
|
|
|
|
data.len(),
|
|
|
|
FIXED_DIR_META_LEN
|
|
|
|
);
|
2019-07-05 00:22:45 -04:00
|
|
|
}
|
2016-12-30 00:05:57 -05:00
|
|
|
}
|