Skip to main content

ozymandias/
lib.rs

1extern crate fs_err as fs;
2
3use anyhow::{anyhow, Context};
4use std::path::Path;
5
6pub mod backup;
7mod chunks_reader;
8pub mod medium;
9pub mod model;
10mod pipe;
11pub mod restore;
12mod streaming_crypto;
13
14/// File permissions. `None` when files are sourced from non-Unix systems.
15type Permissions = Option<u32>;
16
17#[allow(non_upper_case_globals)]
18const KiB: usize = 1024;
19#[allow(non_upper_case_globals)]
20const MiB: usize = 1024 * KiB;
21
22/// General buffer size for filesystem IO operations.
23const IO_BUFFER_SIZE: usize = 2 * MiB;
24
25/// Size of a chunk in a file being backed up.
26/// Files are split into chunks to support a limited degree
27/// of diffing across multiple versions of a large file.
28const CHUNK_SIZE: usize = 1 * MiB;
29
30/// Maximum chunk size that will be accepted by the restore system.
31/// This limit exists to avoid OOM situations.
32const MAX_RESTORE_CHUNK_SIZE: usize = 256 * MiB;
33
34/// Approximate maximum size of a block.
35/// A block is a collection of file chunks from
36/// one or more files.
37///
38/// (This is "approximate" because the actual size
39/// is allowed to exceed this value by less
40/// than the chunk size.)
41const APPROX_MAX_BLOCK_SIZE: usize = 64 * MiB;
42
43fn get_file_name(path: &Path) -> anyhow::Result<String> {
44    path.file_name()
45        .context("missing file name")?
46        .to_str()
47        .ok_or_else(|| anyhow!("invalid UTF-8 in path: {}", path.display()))
48        .map(str::to_owned)
49}
50
51#[cfg(unix)]
52fn get_file_permissions(path: &Path) -> anyhow::Result<Permissions> {
53    let metadata = fs::metadata(path)?;
54    use std::os::unix::fs::MetadataExt;
55    Ok(Some(metadata.mode()))
56}
57
58#[cfg(not(unix))]
59fn get_file_permissions(_path: &Path) -> anyhow::Result<Permissions> {
60    Ok(None)
61}