From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-offline-mirror 2/4] snapshots: add diff command
Date: Wed, 21 Sep 2022 10:12:40 +0200 [thread overview]
Message-ID: <20220921081242.1139249-3-f.gruenbichler@proxmox.com> (raw)
In-Reply-To: <20220921081242.1139249-1-f.gruenbichler@proxmox.com>
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
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<String>, 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<String>,
+ 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<Diff, Error> {
+ let pool = pool(config)?;
+ pool.lock()?.diff_dirs(
+ Path::new(&format!("{snapshot}")),
+ Path::new(&format!("{other_snapshot}")),
+ )
+}
--
2.30.2
next prev parent reply other threads:[~2022-09-21 8:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-21 8:12 [pve-devel] [PATCH proxmox-offline-mirror 0/4] extend/add commands Fabian Grünbichler
2022-09-21 8:12 ` [pve-devel] [PATCH proxmox-offline-mirror 1/4] pool: add diff and list helpers Fabian Grünbichler
2022-09-21 8:12 ` Fabian Grünbichler [this message]
2022-09-21 8:12 ` [pve-devel] [PATCH proxmox-offline-mirror 3/4] medium: add diff command Fabian Grünbichler
2022-09-21 8:12 ` [pve-devel] [PATCH proxmox-offline-mirror 4/4] cli: allow listing snapshots of all mirrors Fabian Grünbichler
2022-09-26 7:51 ` [pve-devel] applied-series: [PATCH proxmox-offline-mirror 0/4] extend/add commands Wolfgang Bumiller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220921081242.1139249-3-f.gruenbichler@proxmox.com \
--to=f.gruenbichler@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox