public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Maximiliano Sandoval <m.sandoval@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup] inspect: don't panic when writing to a broken pipe
Date: Mon, 14 Apr 2025 16:02:44 +0200	[thread overview]
Message-ID: <20250414140244.539303-1-m.sandoval@proxmox.com> (raw)

println [panics] if writing to stdout fails. This can be reproduced by
calling

```
proxmox-backup-debug inspect file {FIXED_INDEX_FILE} | head
```

for example.

[panics] https://doc.rust-lang.org/std/macro.println.html#panics

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---

This raises the question of where else the same change should be done and
whether it is worth it. Imho wanting to see the first 10 or so chunks of an
index file for debugging purposes should come often enough that this should be
fine here.

 src/bin/proxmox_backup_debug/inspect.rs | 70 ++++++++++++++++---------
 1 file changed, 44 insertions(+), 26 deletions(-)

diff --git a/src/bin/proxmox_backup_debug/inspect.rs b/src/bin/proxmox_backup_debug/inspect.rs
index 321080be3..c75871da9 100644
--- a/src/bin/proxmox_backup_debug/inspect.rs
+++ b/src/bin/proxmox_backup_debug/inspect.rs
@@ -203,16 +203,21 @@ fn inspect_chunk(
     };
 
     if output_format == "text" {
-        println!("CRC: {}", val["crc"]);
-        println!("encryption: {}", val["encryption"]);
-        println!("is-compressed: {}", val["is-compressed"]);
-        println!("size: {}", val["size"]);
-        if let Some(refs) = val["referenced-by"].as_array() {
-            println!("referenced by:");
-            for reference in refs {
-                println!("  {}", reference);
+        fn print_to_stdout(val: Value) -> std::io::Result<()> {
+            let mut stdout = std::io::stdout();
+            writeln!(&mut stdout, "CRC: {}", val["crc"])?;
+            writeln!(&mut stdout, "encryption: {}", val["encryption"])?;
+            writeln!(&mut stdout, "is-compressed: {}", val["is-compressed"])?;
+            writeln!(&mut stdout, "size: {}", val["size"])?;
+            if let Some(refs) = val["referenced-by"].as_array() {
+                writeln!(&mut stdout, "referenced by:")?;
+                for reference in refs {
+                    writeln!(&mut stdout, "  {}", reference)?;
+                }
             }
+            Ok(())
         }
+        let _ = print_to_stdout(val);
     } else {
         format_and_print_result(&val, &output_format);
     }
@@ -311,19 +316,25 @@ fn inspect_file(
     };
 
     if output_format == "text" {
-        println!("size: {}", val["size"]);
-        if let Some(encryption) = val["encryption"].as_str() {
-            println!("encryption: {}", encryption);
-        }
-        if let Some(ctime) = val["ctime"].as_str() {
-            println!("creation time: {}", ctime);
-        }
-        if let Some(chunks) = val["chunk-digests"].as_array() {
-            println!("chunks:");
-            for chunk in chunks {
-                println!("  {}", chunk);
+        fn print_to_stdout(val: Value) -> std::io::Result<()> {
+            let mut stdout = std::io::stdout();
+            writeln!(&mut stdout, "size: {}", val["size"])?;
+            if let Some(encryption) = val["encryption"].as_str() {
+                writeln!(&mut stdout, "encryption: {}", encryption)?;
             }
+            if let Some(ctime) = val["ctime"].as_str() {
+                writeln!(&mut stdout, "creation time: {}", ctime)?;
+            }
+            if let Some(chunks) = val["chunk-digests"].as_array() {
+                writeln!(&mut stdout, "chunks:")?;
+                for chunk in chunks {
+                    writeln!(&mut stdout, "  {}", chunk)?;
+                }
+            }
+            Ok(())
         }
+
+        let _ = print_to_stdout(val);
     } else {
         format_and_print_result(&val, &output_format);
     }
@@ -458,14 +469,21 @@ fn inspect_device(device: String, param: Value) -> Result<(), Error> {
     std::fs::remove_dir(std::path::Path::new(&tmp_mount_path))?;
 
     if output_format == "text" {
-        println!("Device contains {ds_count} stores");
-        println!("---------------");
-        for s in stores {
-            println!(
-                "Datastore at {} | VM: {}, CT: {}, HOST: {}, NS: {}",
-                s["path"], s["vm-count"], s["ct-count"], s["host-count"], s["ns-count"]
-            );
+        fn print_to_stdout(ds_count: i32, stores: &[Value]) -> std::io::Result<()> {
+            let mut stdout = std::io::stdout();
+
+            writeln!(&mut stdout, "Device contains {ds_count} stores")?;
+            writeln!(&mut stdout, "---------------")?;
+            for s in stores {
+                writeln!(
+                    &mut stdout,
+                    "Datastore at {} | VM: {}, CT: {}, HOST: {}, NS: {}",
+                    s["path"], s["vm-count"], s["ct-count"], s["host-count"], s["ns-count"]
+                )?;
+            }
+            Ok(())
         }
+        let _ = print_to_stdout(ds_count, &stores);
     } else {
         format_and_print_result(
             &json!({"store_count": stores.len(), "stores": stores}),
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


             reply	other threads:[~2025-04-14 14:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-14 14:02 Maximiliano Sandoval [this message]
2025-04-15  6:26 ` 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=20250414140244.539303-1-m.sandoval@proxmox.com \
    --to=m.sandoval@proxmox.com \
    --cc=pbs-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal