From 9b492eb2563f859bae1479b22ee70dbd6ff9ce8a Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 4 Mar 2019 13:51:36 +0100 Subject: [PATCH] src/backup/datastore.rs: protect BackupDir fields, impl new() --- src/api2/admin/datastore.rs | 31 ++++++++++++++----------------- src/api2/admin/datastore/catar.rs | 5 +---- src/backup/datastore.rs | 16 ++++++++++++++-- src/bin/proxmox-backup-client.rs | 16 +++++----------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 0d02b19c..e1fd5adf 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -24,7 +24,7 @@ fn group_backups(backup_list: Vec) -> HashMap, &BackupInfo) -> String> ( ){ let mut hash = HashSet::new(); for info in list { - let local_time = info.backup_dir.backup_time.with_timezone(&Local); + let local_time = info.backup_dir.backup_time().with_timezone(&Local); if hash.len() >= keep as usize { break; } let backup_id = info.backup_dir.relative_path(); let sel_id: String = select_id(local_time, &info); @@ -70,15 +70,15 @@ fn list_groups( for (_group_id, mut list) in group_hash { - list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time)); // new backups first + list.sort_unstable_by(|a, b| b.backup_dir.backup_time().cmp(&a.backup_dir.backup_time())); // new backups first let info = &list[0]; - let group = &info.backup_dir.group; + let group = info.backup_dir.group(); groups.push(json!({ "backup-type": group.backup_type(), "backup-id": group.backup_id(), - "last-backup": info.backup_dir.backup_time.timestamp(), + "last-backup": info.backup_dir.backup_time().timestamp(), "backup-count": list.len() as u64, "files": info.files, })); @@ -99,10 +99,7 @@ fn delete_snapshots ( let backup_time = tools::required_integer_param(¶m, "backup-time")?; let backup_time = Local.timestamp(backup_time, 0); - let snapshot = BackupDir { - group: BackupGroup::new(backup_type, backup_id), - backup_time, - }; + let snapshot = BackupDir::new(BackupGroup::new(backup_type, backup_id), backup_time); let datastore = DataStore::lookup_datastore(store)?; @@ -134,7 +131,7 @@ fn list_snapshots ( let group_snapshots = match group_hash.get_mut(&group_id) { Some(data) => { // new backups first - data.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time)); + data.sort_unstable_by(|a, b| b.backup_dir.backup_time().cmp(&a.backup_dir.backup_time())); data } None => bail!("Backup group '{}' does not exists.", group_id), @@ -144,12 +141,12 @@ fn list_snapshots ( for info in group_snapshots { - let group = &info.backup_dir.group; + let group = info.backup_dir.group(); snapshots.push(json!({ "backup-type": group.backup_type(), "backup-id": group.backup_id(), - "backup-time": info.backup_dir.backup_time.timestamp(), + "backup-time": info.backup_dir.backup_time().timestamp(), "files": info.files, })); } @@ -177,7 +174,7 @@ fn prune( let mut mark = HashSet::new(); - list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time)); // new backups first + list.sort_unstable_by(|a, b| b.backup_dir.backup_time().cmp(&a.backup_dir.backup_time())); // new backups first if let Some(keep_last) = param["keep-last"].as_u64() { list.iter().take(keep_last as usize).for_each(|info| { @@ -212,7 +209,7 @@ fn prune( let mut remove_list: Vec<&BackupInfo> = list.iter() .filter(|info| !mark.contains(&info.backup_dir.relative_path())).collect(); - remove_list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time)); // oldest backups first + remove_list.sort_unstable_by(|a, b| a.backup_dir.backup_time().cmp(&b.backup_dir.backup_time())); // oldest backups first for info in remove_list { datastore.remove_backup_dir(&info.backup_dir)?; @@ -329,9 +326,9 @@ fn get_backup_list( for info in datastore.list_backups()? { list.push(json!({ - "backup-type": info.backup_dir.group.backup_type(), - "backup-id": info.backup_dir.group.backup_id(), - "backup-time": info.backup_dir.backup_time.timestamp(), + "backup-type": info.backup_dir.group().backup_type(), + "backup-id": info.backup_dir.group().backup_id(), + "backup-time": info.backup_dir.backup_time().timestamp(), "files": info.files, })); } diff --git a/src/api2/admin/datastore/catar.rs b/src/api2/admin/datastore/catar.rs index 710622f5..aac34446 100644 --- a/src/api2/admin/datastore/catar.rs +++ b/src/api2/admin/datastore/catar.rs @@ -145,10 +145,7 @@ fn download_catar( let datastore = DataStore::lookup_datastore(store)?; - let backup_dir = BackupDir { - group: BackupGroup::new(backup_type, backup_id), - backup_time, - }; + let backup_dir = BackupDir::new(BackupGroup::new(backup_type, backup_id), backup_time); let mut path = backup_dir.relative_path(); diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index 6a3c3ba1..b87f3a08 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -77,13 +77,25 @@ impl BackupGroup { #[derive(Debug)] pub struct BackupDir { /// Backup group - pub group: BackupGroup, + group: BackupGroup, /// Backup timestamp - pub backup_time: DateTime, + backup_time: DateTime, } impl BackupDir { + pub fn new(group: BackupGroup, backup_time: DateTime) -> Self { + Self { group, backup_time } + } + + pub fn group(&self) -> &BackupGroup { + &self.group + } + + pub fn backup_time(&self) -> DateTime { + self.backup_time + } + pub fn parse(path: &str) -> Result { let cap = SNAPSHOT_PATH_REGEX.captures(path) diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index a2692159..8cd83a19 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -112,10 +112,7 @@ fn list_backups( let epoch = item["backup-time"].as_i64().unwrap(); let backup_time = Local.timestamp(epoch, 0); - let backup_dir = BackupDir { - group: BackupGroup::new(btype, id), - backup_time - }; + let backup_dir = BackupDir::new(BackupGroup::new(btype, id), backup_time); let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect(); @@ -208,10 +205,7 @@ fn list_snapshots( let epoch = item["backup-time"].as_i64().unwrap(); let backup_time = Local.timestamp(epoch, 0); - let snapshot = BackupDir { - group: BackupGroup::new(btype, id), - backup_time, - }; + let snapshot = BackupDir::new(BackupGroup::new(btype, id), backup_time); let path = snapshot.relative_path().to_str().unwrap().to_owned(); @@ -239,9 +233,9 @@ fn forget_snapshots( let snapshot = BackupDir::parse(path)?; let query = tools::json_object_to_query(json!({ - "backup-type": snapshot.group.backup_type(), - "backup-id": snapshot.group.backup_id(), - "backup-time": snapshot.backup_time.timestamp(), + "backup-type": snapshot.group().backup_type(), + "backup-id": snapshot.group().backup_id(), + "backup-time": snapshot.backup_time().timestamp(), }))?; let mut client = HttpClient::new(&repo.host, &repo.user);