src/api2/admin/datastore.rs - files: return data from index.json
This commit is contained in:
		@ -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<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    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<BackupInfo>) -> HashMap<String, Vec<BackupInfo>> {
 | 
			
		||||
 | 
			
		||||
    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 (
 | 
			
		||||
 | 
			
		||||
@ -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<String> = 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
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user