From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id D2338941E5 for ; Wed, 21 Sep 2022 10:13:24 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C8C4224D20 for ; Wed, 21 Sep 2022 10:12:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 21 Sep 2022 10:12:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D55F6442A9 for ; Wed, 21 Sep 2022 10:12:53 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pve-devel@lists.proxmox.com Date: Wed, 21 Sep 2022 10:12:40 +0200 Message-Id: <20220921081242.1139249-3-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220921081242.1139249-1-f.gruenbichler@proxmox.com> References: <20220921081242.1139249-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.148 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH proxmox-offline-mirror 2/4] snapshots: add diff command X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Sep 2022 08:13:24 -0000 Signed-off-by: Fabian Grünbichler --- src/bin/proxmox_offline_mirror_cmds/mirror.rs | 77 ++++++++++++++++++- src/mirror.rs | 15 +++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/bin/proxmox_offline_mirror_cmds/mirror.rs b/src/bin/proxmox_offline_mirror_cmds/mirror.rs index 3094146..348392b 100644 --- a/src/bin/proxmox_offline_mirror_cmds/mirror.rs +++ b/src/bin/proxmox_offline_mirror_cmds/mirror.rs @@ -3,7 +3,7 @@ use anyhow::{bail, format_err, Error}; use proxmox_section_config::SectionConfigData; use proxmox_subscription::SubscriptionStatus; use serde_json::Value; -use std::collections::HashMap; +use std::{collections::HashMap, path::PathBuf}; use proxmox_router::cli::{ format_and_print_result, get_output_format, CliCommand, CliCommandMap, CommandLineInterface, @@ -273,6 +273,73 @@ async fn garbage_collect(config: Option, id: String, _param: Value) -> R Ok(()) } + +#[api( + input: { + properties: { + config: { + type: String, + optional: true, + description: "Path to mirroring config file.", + }, + id: { + schema: MIRROR_ID_SCHEMA, + }, + snapshot: { + type: Snapshot, + }, + other_snapshot: { + type: Snapshot, + }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + } + }, + )] +/// Print differences between two snapshots. +async fn diff_snapshots( + config: Option, + id: String, + snapshot: Snapshot, + other_snapshot: Snapshot, + _param: Value, +) -> Result<(), Error> { + let config = config.unwrap_or_else(get_config_path); + + let (config, _digest) = proxmox_offline_mirror::config::config(&config)?; + let config: MirrorConfig = config.lookup("mirror", &id)?; + let mut diff = mirror::diff_snapshots(&config, &snapshot, &other_snapshot)?; + let sort = |(path, _): &(PathBuf, u64), (other_path, _): &(PathBuf, u64)| path.cmp(other_path); + diff.added.paths.sort_unstable_by(sort); + diff.changed.paths.sort_unstable_by(sort); + diff.removed.paths.sort_unstable_by(sort); + + println!("{other_snapshot} added {} file(s)", diff.added.paths.len()); + for (path, size) in diff.added.paths { + println!("\t{path:?}: +{size}b"); + } + + println!( + "\n{other_snapshot} removed {} file(s)", + diff.removed.paths.len() + ); + for (path, size) in diff.removed.paths { + println!("\t{path:?}: -{size}b"); + } + + println!( + "\n {} file(s) diff between {snapshot} and {other_snapshot}", + diff.changed.paths.len() + ); + for (path, size) in diff.changed.paths { + println!("\t{path:?}: +-{size}b"); + } + + Ok(()) +} + pub fn mirror_commands() -> CommandLineInterface { let snapshot_cmds = CliCommandMap::new() .insert( @@ -287,6 +354,14 @@ pub fn mirror_commands() -> CommandLineInterface { .insert( "remove", CliCommand::new(&API_METHOD_REMOVE_SNAPSHOT).arg_param(&["id", "snapshot"]), + ) + .insert( + "diff", + CliCommand::new(&API_METHOD_DIFF_SNAPSHOTS).arg_param(&[ + "id", + "snapshot", + "other_snapshot", + ]), ); let cmd_def = CliCommandMap::new() diff --git a/src/mirror.rs b/src/mirror.rs index 5126393..5bf9219 100644 --- a/src/mirror.rs +++ b/src/mirror.rs @@ -15,7 +15,7 @@ use crate::{ config::{MirrorConfig, SubscriptionKey}, convert_repo_line, pool::Pool, - types::{Snapshot, SNAPSHOT_REGEX}, + types::{Diff, Snapshot, SNAPSHOT_REGEX}, FetchResult, Progress, }; use proxmox_apt::{ @@ -745,3 +745,16 @@ pub fn gc(config: &MirrorConfig) -> Result<(usize, u64), Error> { pool.lock()?.gc() } + +/// Print differences between two snapshots +pub fn diff_snapshots( + config: &MirrorConfig, + snapshot: &Snapshot, + other_snapshot: &Snapshot, +) -> Result { + let pool = pool(config)?; + pool.lock()?.diff_dirs( + Path::new(&format!("{snapshot}")), + Path::new(&format!("{other_snapshot}")), + ) +} -- 2.30.2