datastore: add snapshot iterator and provide example
will be more used in the future, when the upend-datastore master plan comes in effect. also a preparatory work for namespaces Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
249dde8b63
commit
33eb23d57e
34
pbs-datastore/examples/ls-snapshots.rs
Normal file
34
pbs-datastore/examples/ls-snapshots.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{bail, Error};
|
||||
|
||||
use pbs_datastore::{ListGroups, ListSnapshots};
|
||||
|
||||
fn run() -> Result<(), Error> {
|
||||
let base: PathBuf = match std::env::args().skip(1).next() {
|
||||
Some(path) => path.into(),
|
||||
None => bail!("no path passed"),
|
||||
};
|
||||
|
||||
for group in ListGroups::new(base.to_owned())? {
|
||||
let group = group?;
|
||||
println!("found group {}", group);
|
||||
|
||||
let group_path = base.as_path().join(group.to_string());
|
||||
for snapshot in ListSnapshots::new(group, group_path)? {
|
||||
println!("\t{}", snapshot?);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
std::process::exit(match run() {
|
||||
Ok(_) => 0,
|
||||
Err(err) => {
|
||||
eprintln!("error: {}", err);
|
||||
1
|
||||
}
|
||||
});
|
||||
}
|
@ -19,7 +19,7 @@ use proxmox_sys::{task_log, task_warn};
|
||||
|
||||
use pbs_api_types::{
|
||||
Authid, ChunkOrder, DataStoreConfig, DatastoreTuning, GarbageCollectionStatus, HumanByte,
|
||||
Operation, BACKUP_ID_REGEX, BACKUP_TYPE_REGEX, UPID,
|
||||
Operation, BACKUP_DATE_REGEX, BACKUP_ID_REGEX, BACKUP_TYPE_REGEX, UPID,
|
||||
};
|
||||
use pbs_config::{open_backup_lockfile, BackupLockGuard, ConfigVersionCache};
|
||||
|
||||
@ -1065,6 +1065,51 @@ impl DataStore {
|
||||
}
|
||||
}
|
||||
|
||||
/// A iterator for all BackupDir's (Snapshots) in a BackupGroup
|
||||
pub struct ListSnapshots {
|
||||
group: BackupGroup,
|
||||
fd: proxmox_sys::fs::ReadDir,
|
||||
}
|
||||
|
||||
impl ListSnapshots {
|
||||
pub fn new(group: BackupGroup, group_path: PathBuf) -> Result<Self, Error> {
|
||||
Ok(ListSnapshots {
|
||||
fd: proxmox_sys::fs::read_subdir(libc::AT_FDCWD, &group_path)?,
|
||||
group,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for ListSnapshots {
|
||||
type Item = Result<BackupDir, Error>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
let item = self.fd.next()?;
|
||||
match item {
|
||||
Ok(ref entry) => {
|
||||
if let Ok(name) = entry.file_name().to_str() {
|
||||
match entry.file_type() {
|
||||
Some(nix::dir::Type::Directory) => {} // OK
|
||||
_ => continue,
|
||||
}
|
||||
if BACKUP_DATE_REGEX.is_match(name) {
|
||||
let backup_time = match proxmox_time::parse_rfc3339(&name) {
|
||||
Ok(time) => time,
|
||||
Err(err) => return Some(Err(err)),
|
||||
};
|
||||
|
||||
return Some(BackupDir::with_group(self.group.clone(), backup_time));
|
||||
}
|
||||
}
|
||||
continue; // file did not match regex or isn't valid utf-8
|
||||
}
|
||||
Err(err) => return Some(Err(err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A iterator for a (single) level of Backup Groups
|
||||
pub struct ListGroups {
|
||||
type_fd: proxmox_sys::fs::ReadDir,
|
||||
|
@ -204,7 +204,7 @@ pub use manifest::BackupManifest;
|
||||
pub use store_progress::StoreProgress;
|
||||
|
||||
mod datastore;
|
||||
pub use datastore::{check_backup_owner, DataStore, ListGroups};
|
||||
pub use datastore::{check_backup_owner, DataStore, ListGroups, ListSnapshots};
|
||||
|
||||
mod snapshot_reader;
|
||||
pub use snapshot_reader::SnapshotReader;
|
||||
|
Loading…
Reference in New Issue
Block a user