2022-04-14 11:27:53 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
2020-12-17 12:05:00 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
use nix::dir::Dir;
|
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::fs::lock_dir_noblock_shared;
|
|
|
|
|
2022-05-09 13:39:29 +00:00
|
|
|
use pbs_api_types::{BackupNamespace, Operation};
|
|
|
|
|
2021-09-27 07:58:20 +00:00
|
|
|
use crate::backup_info::BackupDir;
|
|
|
|
use crate::dynamic_index::DynamicIndexReader;
|
2022-04-14 11:27:53 +00:00
|
|
|
use crate::fixed_index::FixedIndexReader;
|
|
|
|
use crate::index::IndexFile;
|
2021-09-27 07:58:20 +00:00
|
|
|
use crate::manifest::{archive_type, ArchiveType, CLIENT_LOG_BLOB_NAME, MANIFEST_BLOB_NAME};
|
|
|
|
use crate::DataStore;
|
2021-07-06 11:26:35 +00:00
|
|
|
|
2020-12-17 12:05:00 +00:00
|
|
|
/// Helper to access the contents of a datastore backup snapshot
|
|
|
|
///
|
|
|
|
/// This make it easy to iterate over all used chunks and files.
|
|
|
|
pub struct SnapshotReader {
|
2020-12-18 11:10:31 +00:00
|
|
|
snapshot: BackupDir,
|
2021-03-16 09:43:20 +00:00
|
|
|
datastore_name: String,
|
2020-12-17 12:05:00 +00:00
|
|
|
file_list: Vec<String>,
|
|
|
|
locked_dir: Dir,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SnapshotReader {
|
|
|
|
/// Lock snapshot, reads the manifest and returns a new instance
|
2022-04-19 08:38:46 +00:00
|
|
|
pub fn new(
|
|
|
|
datastore: Arc<DataStore>,
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: BackupNamespace,
|
2022-04-19 08:38:46 +00:00
|
|
|
snapshot: pbs_api_types::BackupDir,
|
|
|
|
) -> Result<Self, Error> {
|
2022-05-09 13:39:29 +00:00
|
|
|
Self::new_do(datastore.backup_dir(ns, snapshot)?)
|
|
|
|
}
|
2022-04-19 08:38:46 +00:00
|
|
|
|
2022-05-09 13:39:29 +00:00
|
|
|
pub(crate) fn new_do(snapshot: BackupDir) -> Result<Self, Error> {
|
|
|
|
let datastore = snapshot.datastore();
|
2022-04-20 13:30:04 +00:00
|
|
|
let snapshot_path = snapshot.full_path();
|
2020-12-17 12:05:00 +00:00
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
let locked_dir =
|
|
|
|
lock_dir_noblock_shared(&snapshot_path, "snapshot", "locked by another operation")?;
|
2020-12-17 12:05:00 +00:00
|
|
|
|
2021-03-16 09:43:20 +00:00
|
|
|
let datastore_name = datastore.name().to_string();
|
|
|
|
|
2020-12-17 12:05:00 +00:00
|
|
|
let manifest = match datastore.load_manifest(&snapshot) {
|
|
|
|
Ok((manifest, _)) => manifest,
|
|
|
|
Err(err) => {
|
2022-04-14 11:27:53 +00:00
|
|
|
bail!(
|
|
|
|
"manifest load error on datastore '{}' snapshot '{}' - {}",
|
|
|
|
datastore_name,
|
|
|
|
snapshot,
|
|
|
|
err
|
|
|
|
);
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-15 13:38:27 +00:00
|
|
|
let mut client_log_path = snapshot_path;
|
2020-12-17 12:05:00 +00:00
|
|
|
client_log_path.push(CLIENT_LOG_BLOB_NAME);
|
|
|
|
|
|
|
|
let mut file_list = Vec::new();
|
|
|
|
file_list.push(MANIFEST_BLOB_NAME.to_string());
|
2022-04-14 11:27:53 +00:00
|
|
|
for item in manifest.files() {
|
|
|
|
file_list.push(item.filename.clone());
|
|
|
|
}
|
2020-12-17 12:05:00 +00:00
|
|
|
if client_log_path.exists() {
|
|
|
|
file_list.push(CLIENT_LOG_BLOB_NAME.to_string());
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
Ok(Self {
|
|
|
|
snapshot,
|
|
|
|
datastore_name,
|
|
|
|
file_list,
|
|
|
|
locked_dir,
|
|
|
|
})
|
2020-12-18 11:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the snapshot directory
|
|
|
|
pub fn snapshot(&self) -> &BackupDir {
|
|
|
|
&self.snapshot
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 09:43:20 +00:00
|
|
|
/// Return the datastore name
|
|
|
|
pub fn datastore_name(&self) -> &str {
|
|
|
|
&self.datastore_name
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:05:00 +00:00
|
|
|
/// Returns the list of files the snapshot refers to.
|
|
|
|
pub fn file_list(&self) -> &Vec<String> {
|
|
|
|
&self.file_list
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Opens a file inside the snapshot (using openat) for reading
|
|
|
|
pub fn open_file(&self, filename: &str) -> Result<File, Error> {
|
|
|
|
let raw_fd = nix::fcntl::openat(
|
|
|
|
self.locked_dir.as_raw_fd(),
|
|
|
|
Path::new(filename),
|
|
|
|
nix::fcntl::OFlag::O_RDONLY,
|
|
|
|
nix::sys::stat::Mode::empty(),
|
|
|
|
)?;
|
|
|
|
let file = unsafe { File::from_raw_fd(raw_fd) };
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
|
2022-02-17 13:50:40 +00:00
|
|
|
/// Returns an iterator for all chunks not skipped by `skip_fn`.
|
2022-04-14 11:27:53 +00:00
|
|
|
pub fn chunk_iterator<F: Fn(&[u8; 32]) -> bool>(
|
|
|
|
&self,
|
|
|
|
skip_fn: F,
|
|
|
|
) -> Result<SnapshotChunkIterator<F>, Error> {
|
2022-02-17 13:50:40 +00:00
|
|
|
SnapshotChunkIterator::new(self, skip_fn)
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterates over all chunks used by a backup snapshot
|
|
|
|
///
|
|
|
|
/// Note: The iterator returns a `Result`, and the iterator state is
|
|
|
|
/// undefined after the first error. So it make no sense to continue
|
|
|
|
/// iteration after the first error.
|
2022-04-14 11:27:53 +00:00
|
|
|
pub struct SnapshotChunkIterator<'a, F: Fn(&[u8; 32]) -> bool> {
|
2020-12-17 12:05:00 +00:00
|
|
|
snapshot_reader: &'a SnapshotReader,
|
|
|
|
todo_list: Vec<String>,
|
2022-02-17 13:50:40 +00:00
|
|
|
skip_fn: F,
|
2021-06-18 09:29:10 +00:00
|
|
|
current_index: Option<(Arc<Box<dyn IndexFile + Send>>, usize, Vec<(usize, u64)>)>,
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
impl<'a, F: Fn(&[u8; 32]) -> bool> Iterator for SnapshotChunkIterator<'a, F> {
|
2020-12-17 12:05:00 +00:00
|
|
|
type Item = Result<[u8; 32], Error>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2021-10-08 09:19:37 +00:00
|
|
|
proxmox_lang::try_block!({
|
2020-12-17 12:05:00 +00:00
|
|
|
loop {
|
|
|
|
if self.current_index.is_none() {
|
|
|
|
if let Some(filename) = self.todo_list.pop() {
|
|
|
|
let file = self.snapshot_reader.open_file(&filename)?;
|
2021-06-18 09:29:10 +00:00
|
|
|
let index: Box<dyn IndexFile + Send> = match archive_type(&filename)? {
|
2020-12-17 12:05:00 +00:00
|
|
|
ArchiveType::FixedIndex => Box::new(FixedIndexReader::new(file)?),
|
|
|
|
ArchiveType::DynamicIndex => Box::new(DynamicIndexReader::new(file)?),
|
2022-04-14 11:27:53 +00:00
|
|
|
_ => bail!(
|
|
|
|
"SnapshotChunkIterator: got unknown file type - internal error"
|
|
|
|
),
|
2020-12-17 12:05:00 +00:00
|
|
|
};
|
2021-06-18 09:29:10 +00:00
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(
|
|
|
|
self.snapshot_reader.datastore_name(),
|
|
|
|
Some(Operation::Read),
|
|
|
|
)?;
|
|
|
|
let order =
|
|
|
|
datastore.get_chunks_in_order(&index, &self.skip_fn, |_| Ok(()))?;
|
2021-06-18 09:29:10 +00:00
|
|
|
|
|
|
|
self.current_index = Some((Arc::new(index), 0, order));
|
2020-12-17 12:05:00 +00:00
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 09:29:10 +00:00
|
|
|
let (index, pos, list) = self.current_index.take().unwrap();
|
|
|
|
if pos < list.len() {
|
|
|
|
let (real_pos, _) = list[pos];
|
|
|
|
let digest = *index.index_digest(real_pos).unwrap();
|
|
|
|
self.current_index = Some((index, pos + 1, list));
|
2020-12-17 12:05:00 +00:00
|
|
|
return Ok(Some(digest));
|
|
|
|
} else {
|
|
|
|
// pop next index
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 11:27:53 +00:00
|
|
|
})
|
|
|
|
.transpose()
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
impl<'a, F: Fn(&[u8; 32]) -> bool> SnapshotChunkIterator<'a, F> {
|
2022-02-17 13:50:40 +00:00
|
|
|
pub fn new(snapshot_reader: &'a SnapshotReader, skip_fn: F) -> Result<Self, Error> {
|
2020-12-17 12:05:00 +00:00
|
|
|
let mut todo_list = Vec::new();
|
|
|
|
|
|
|
|
for filename in snapshot_reader.file_list() {
|
|
|
|
match archive_type(filename)? {
|
|
|
|
ArchiveType::FixedIndex | ArchiveType::DynamicIndex => {
|
|
|
|
todo_list.push(filename.to_owned());
|
2022-04-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
ArchiveType::Blob => { /* no chunks, do nothing */ }
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
Ok(Self {
|
|
|
|
snapshot_reader,
|
|
|
|
todo_list,
|
|
|
|
current_index: None,
|
|
|
|
skip_fn,
|
|
|
|
})
|
2020-12-17 12:05:00 +00:00
|
|
|
}
|
|
|
|
}
|