From 27c9affb230464f9cb87240b6859cc5f0768a332 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 7 Jan 2020 15:18:36 +0100 Subject: [PATCH] src/bin/proxmox-backup-client.rs: cleanup - factor out api_datastore_latest_snapshot() --- src/bin/proxmox-backup-client.rs | 60 +++++++++++++------------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 832e8ef3..984eb30c 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -1,7 +1,7 @@ use failure::*; use nix::unistd::{fork, ForkResult, pipe}; use std::os::unix::io::RawFd; -use chrono::{Local, Utc, TimeZone}; +use chrono::{Local, DateTime, Utc, TimeZone}; use std::path::{Path, PathBuf}; use std::collections::{HashSet, HashMap}; use std::ffi::OsStr; @@ -201,6 +201,26 @@ async fn api_datastore_list_snapshots( Ok(list) } +async fn api_datastore_latest_snapshot( + client: &HttpClient, + store: &str, + group: BackupGroup, +) -> Result<(String, String, DateTime), Error> { + + let mut list = api_datastore_list_snapshots(client, store, Some(group.clone())).await?; + + if list.is_empty() { + bail!("backup group {:?} does not contain any snapshots.", group.group_path()); + } + + list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); + + let backup_time = Utc.timestamp(list[0].backup_time, 0); + + Ok((group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time)) +} + + async fn backup_directory>( client: &BackupWriter, dir_path: P, @@ -1132,17 +1152,7 @@ async fn restore(param: Value) -> Result { let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { let group = BackupGroup::parse(path)?; - - let mut list = api_datastore_list_snapshots(&client, repo.store(), Some(group.clone())).await?; - - if list.is_empty() { - bail!("backup group '{}' does not contain any snapshots:", path); - } - list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); - - let backup_time = Utc.timestamp(list[0].backup_time, 0); - - (group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time) + api_datastore_latest_snapshot(&client, repo.store(), group).await? } else { let snapshot = BackupDir::parse(path)?; (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) @@ -1913,18 +1923,7 @@ async fn mount_do(param: Value, pipe: Option) -> Result { let path = tools::required_string_param(¶m, "snapshot")?; let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { let group = BackupGroup::parse(path)?; - - let mut list = api_datastore_list_snapshots(&client, repo.store(), Some(group.clone())).await?; - - if list.is_empty() { - bail!("backup group '{}' does not contain any snapshots:", path); - } - - list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); - - let backup_time = Utc.timestamp(list[0].backup_time, 0); - - (group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time) + api_datastore_latest_snapshot(&client, repo.store(), group).await? } else { let snapshot = BackupDir::parse(path)?; (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) @@ -2033,18 +2032,7 @@ async fn catalog_shell(param: Value) -> Result<(), Error> { let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { let group = BackupGroup::parse(path)?; - - let mut list = api_datastore_list_snapshots(&client, repo.store(), Some(group.clone())).await?; - - if list.is_empty() { - bail!("backup group '{}' does not contain any snapshots:", path); - } - - list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); - - let backup_time = Utc.timestamp(list[0].backup_time, 0); - - (group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time) + api_datastore_latest_snapshot(&client, repo.store(), group).await? } else { let snapshot = BackupDir::parse(path)?; (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())