diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 81ebd294..7471a847 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -65,7 +65,7 @@ fn list_groups( 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); @@ -105,7 +105,8 @@ fn list_snapshot_files ( 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)) } @@ -144,27 +145,13 @@ fn list_snapshots ( 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 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 backup_list = group.list_backups(&base_path)?; let mut snapshots = vec![]; - for info in group_snapshots { - - let group = info.backup_dir.group(); - + for info in backup_list { snapshots.push(json!({ "backup-type": group.backup_type(), "backup-id": group.backup_id(), @@ -188,7 +175,7 @@ fn prune( 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); @@ -355,7 +342,9 @@ fn get_backup_list( 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!({ "backup-type": info.backup_dir.group().backup_type(), "backup-id": info.backup_dir.group().backup_id(), diff --git a/src/backup/backup_info.rs b/src/backup/backup_info.rs index dfdf3a3f..85447e8c 100644 --- a/src/backup/backup_info.rs +++ b/src/backup/backup_info.rs @@ -2,6 +2,7 @@ use crate::tools; use failure::*; use regex::Regex; +use std::os::unix::io::RawFd; use chrono::{DateTime, TimeZone, Local}; @@ -77,6 +78,27 @@ impl BackupGroup { relative_path } + + pub fn list_backups(&self, base_path: &Path) -> Result, 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::>()?; + 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) @@ -161,13 +183,7 @@ impl BackupInfo { let mut path = base_path.to_owned(); path.push(backup_dir.relative_path()); - let mut files = vec![]; - - 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(()) - })?; + let files = list_backup_files(libc::AT_FDCWD, &path)?; Ok(files) } @@ -183,19 +199,11 @@ impl BackupInfo { if file_type != nix::dir::Type::Directory { return Ok(()); } let dt = backup_time.parse::>()?; + 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| { - 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, - }); + list.push(BackupInfo { backup_dir, files }); Ok(()) }) @@ -204,3 +212,15 @@ impl BackupInfo { Ok(list) } } + +fn list_backup_files(dirfd: RawFd, path: &P) -> Result, 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) +} diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index b24ae412..8696cc3a 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -168,16 +168,6 @@ impl DataStore { } } - pub fn list_backups(&self) -> Result, Error> { - let path = self.base_path(); - BackupInfo::list_backups(&path) - } - - pub fn list_files(&self, backup_dir: &BackupDir) -> Result, Error> { - let path = self.base_path(); - BackupInfo::list_files(&path, backup_dir) - } - pub fn list_images(&self) -> Result, Error> { let base = self.base_path();