From 8c70e3eb18586a0d0feccca571eb6c4a5ffe5382 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 5 Aug 2019 13:22:19 +0200 Subject: [PATCH] src/api2/admin/datastore.rs - files: return data from index.json --- src/api2/admin/datastore.rs | 26 ++++++++++++++++++++++---- src/bin/proxmox-backup-client.rs | 22 +++++++++++++--------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index c313ee82..b0c74419 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -12,7 +12,7 @@ use chrono::{DateTime, Datelike, TimeZone, Local}; use std::path::PathBuf; use std::sync::Arc; -use proxmox::tools::{try_block, fs::file_set_contents}; +use proxmox::tools::{try_block, fs::file_get_contents, fs::file_set_contents}; use crate::config::datastore; @@ -22,6 +22,25 @@ use crate::server::WorkerTask; use hyper::{header, Body, Response, StatusCode}; use hyper::http::request::Parts; +fn read_backup_index(store: &DataStore, backup_dir: &BackupDir) -> Result { + + let mut path = store.base_path(); + path.push(backup_dir.relative_path()); + path.push("index.json.blob"); + + let raw_data = file_get_contents(&path)?; + let data = DataBlob::from_raw(raw_data)?.decode(None)?; + let mut result: Value = serde_json::from_reader(&mut &data[..])?; + + let result = result["files"].take(); + + if result == Value::Null { + bail!("missing 'files' property in backup index {:?}", path); + } + + Ok(result) +} + fn group_backups(backup_list: Vec) -> HashMap> { let mut group_hash = HashMap::new(); @@ -105,10 +124,9 @@ fn list_snapshot_files ( let datastore = DataStore::lookup_datastore(store)?; - let path = datastore.base_path(); - let files = BackupInfo::list_files(&path, &snapshot)?; + let files = read_backup_index(&datastore, &snapshot)?; - Ok(json!(files)) + Ok(files) } fn delete_snapshots ( diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 42e30198..907e6d7f 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -394,7 +394,7 @@ fn list_snapshot_files( let path = format!("api2/json/admin/datastore/{}/files", repo.store()); - let result = client.get(&path, Some(json!({ + let mut result = client.get(&path, Some(json!({ "backup-type": snapshot.group().backup_type(), "backup-id": snapshot.group().backup_id(), "backup-time": snapshot.backup_time().timestamp(), @@ -402,15 +402,18 @@ fn list_snapshot_files( record_repository(&repo); - let list: Vec = result["data"].as_array().unwrap().iter() - .map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect(); + let list: Value = result["data"].take(); if output_format == "text" { - for file in list { - println!("{}", file); + for item in list.as_array().unwrap().iter() { + println!( + "{} {}", + strip_server_file_expenstion(item["filename"].as_str().unwrap()), + item["size"].as_u64().unwrap_or(0), + ); } } else { - format_and_print_result(&list.into(), &output_format); + format_and_print_result(&list, &output_format); } Ok(Value::Null) @@ -642,10 +645,11 @@ fn create_backup( // create index.json let file_list = file_list.iter() - .fold(json!({}), |mut acc, (filename, stats)| { - acc[filename] = json!({ + .fold(vec![], |mut acc, (filename, stats)| { + acc.push(json!({ + "filename": filename, "size": stats.size, - }); + })); acc });