src/backup/backup_info.rs: implement list_backup for BackupGroup, cleanups

This commit is contained in:
Dietmar Maurer 2019-05-11 10:19:34 +02:00
parent f9578f3c79
commit c09775011a
3 changed files with 48 additions and 49 deletions

View File

@ -65,7 +65,7 @@ fn list_groups(
let datastore = DataStore::lookup_datastore(store)?; let datastore = DataStore::lookup_datastore(store)?;
let backup_list = datastore.list_backups()?; let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
let group_hash = group_backups(backup_list); let group_hash = group_backups(backup_list);
@ -105,7 +105,8 @@ fn list_snapshot_files (
let datastore = DataStore::lookup_datastore(store)?; let datastore = DataStore::lookup_datastore(store)?;
let files = datastore.list_files(&snapshot)?; let path = datastore.base_path();
let files = BackupInfo::list_files(&path, &snapshot)?;
Ok(json!(files)) Ok(json!(files))
} }
@ -144,27 +145,13 @@ fn list_snapshots (
let datastore = DataStore::lookup_datastore(store)?; let datastore = DataStore::lookup_datastore(store)?;
let backup_list = datastore.list_backups()?; let base_path = datastore.base_path();
let mut group_hash = group_backups(backup_list); let backup_list = group.list_backups(&base_path)?;
let group_id = group.group_path().to_str().unwrap().to_owned();
let group_snapshots = match group_hash.get_mut(&group_id) {
Some(data) => {
// new backups first
BackupInfo::sort_list(data, false);
data
}
None => bail!("Backup group '{}' does not exists.", group_id),
};
let mut snapshots = vec![]; let mut snapshots = vec![];
for info in group_snapshots { for info in backup_list {
let group = info.backup_dir.group();
snapshots.push(json!({ snapshots.push(json!({
"backup-type": group.backup_type(), "backup-type": group.backup_type(),
"backup-id": group.backup_id(), "backup-id": group.backup_id(),
@ -188,7 +175,7 @@ fn prune(
println!("Starting prune on store {}", store); println!("Starting prune on store {}", store);
let backup_list = datastore.list_backups()?; let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
let group_hash = group_backups(backup_list); let group_hash = group_backups(backup_list);
@ -355,7 +342,9 @@ fn get_backup_list(
let mut list = vec![]; let mut list = vec![];
for info in datastore.list_backups()? { let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
for info in backup_list {
list.push(json!({ list.push(json!({
"backup-type": info.backup_dir.group().backup_type(), "backup-type": info.backup_dir.group().backup_type(),
"backup-id": info.backup_dir.group().backup_id(), "backup-id": info.backup_dir.group().backup_id(),

View File

@ -2,6 +2,7 @@ use crate::tools;
use failure::*; use failure::*;
use regex::Regex; use regex::Regex;
use std::os::unix::io::RawFd;
use chrono::{DateTime, TimeZone, Local}; use chrono::{DateTime, TimeZone, Local};
@ -77,6 +78,27 @@ impl BackupGroup {
relative_path relative_path
} }
pub fn list_backups(&self, base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
let mut list = vec![];
let mut path = base_path.to_owned();
path.push(self.group_path());
tools::scandir(libc::AT_FDCWD, &path, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
if file_type != nix::dir::Type::Directory { return Ok(()); }
let dt = backup_time.parse::<DateTime<Local>>()?;
let backup_dir = BackupDir::new(self.backup_type.clone(), self.backup_id.clone(), dt.timestamp());
let files = list_backup_files(l2_fd, backup_time)?;
list.push(BackupInfo { backup_dir, files });
Ok(())
})?;
Ok(list)
}
} }
/// Uniquely identify a Backup (relative to data store) /// Uniquely identify a Backup (relative to data store)
@ -161,13 +183,7 @@ impl BackupInfo {
let mut path = base_path.to_owned(); let mut path = base_path.to_owned();
path.push(backup_dir.relative_path()); path.push(backup_dir.relative_path());
let mut files = vec![]; let files = list_backup_files(libc::AT_FDCWD, &path)?;
tools::scandir(libc::AT_FDCWD, &path, &BACKUP_FILE_REGEX, |_, filename, file_type| {
if file_type != nix::dir::Type::File { return Ok(()); }
files.push(filename.to_owned());
Ok(())
})?;
Ok(files) Ok(files)
} }
@ -183,19 +199,11 @@ impl BackupInfo {
if file_type != nix::dir::Type::Directory { return Ok(()); } if file_type != nix::dir::Type::Directory { return Ok(()); }
let dt = backup_time.parse::<DateTime<Local>>()?; let dt = backup_time.parse::<DateTime<Local>>()?;
let backup_dir = BackupDir::new(backup_type, backup_id, dt.timestamp());
let mut files = vec![]; let files = list_backup_files(l2_fd, backup_time)?;
tools::scandir(l2_fd, backup_time, &BACKUP_FILE_REGEX, |_, filename, file_type| { list.push(BackupInfo { backup_dir, files });
if file_type != nix::dir::Type::File { return Ok(()); }
files.push(filename.to_owned());
Ok(())
})?;
list.push(BackupInfo {
backup_dir: BackupDir::new(backup_type, backup_id, dt.timestamp()),
files,
});
Ok(()) Ok(())
}) })
@ -204,3 +212,15 @@ impl BackupInfo {
Ok(list) Ok(list)
} }
} }
fn list_backup_files<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<Vec<String>, Error> {
let mut files = vec![];
tools::scandir(dirfd, path, &BACKUP_FILE_REGEX, |_, filename, file_type| {
if file_type != nix::dir::Type::File { return Ok(()); }
files.push(filename.to_owned());
Ok(())
})?;
Ok(files)
}

View File

@ -168,16 +168,6 @@ impl DataStore {
} }
} }
pub fn list_backups(&self) -> Result<Vec<BackupInfo>, Error> {
let path = self.base_path();
BackupInfo::list_backups(&path)
}
pub fn list_files(&self, backup_dir: &BackupDir) -> Result<Vec<String>, Error> {
let path = self.base_path();
BackupInfo::list_files(&path, backup_dir)
}
pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> { pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
let base = self.base_path(); let base = self.base_path();