[pbs-devel] [PATCH proxmox-backup 2/3] debug: move outfile_or_stdout to module for reuse

Fabian Grünbichler f.gruenbichler at proxmox.com
Mon May 23 16:11:34 CEST 2022


Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 src/bin/proxmox_backup_debug/inspect.rs | 18 +++---------------
 src/bin/proxmox_backup_debug/mod.rs     | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/src/bin/proxmox_backup_debug/inspect.rs b/src/bin/proxmox_backup_debug/inspect.rs
index 37bc6e05..e50c50cc 100644
--- a/src/bin/proxmox_backup_debug/inspect.rs
+++ b/src/bin/proxmox_backup_debug/inspect.rs
@@ -1,7 +1,6 @@
 use std::collections::HashSet;
 use std::fs::File;
-use std::io::{stdout, Read, Seek, SeekFrom, Write};
-use std::panic::{RefUnwindSafe, UnwindSafe};
+use std::io::{Read, Seek, SeekFrom, Write};
 use std::path::Path;
 
 use anyhow::{bail, format_err, Error};
@@ -27,18 +26,6 @@ use pbs_datastore::index::IndexFile;
 use pbs_datastore::DataBlob;
 use pbs_tools::crypt_config::CryptConfig;
 
-// Returns either a new file, if a path is given, or stdout, if no path is given.
-fn outfile_or_stdout<P: AsRef<Path>>(
-    path: Option<P>,
-) -> std::io::Result<Box<dyn Write + Send + Sync + Unpin + RefUnwindSafe + UnwindSafe>> {
-    if let Some(path) = path {
-        let f = File::create(path)?;
-        Ok(Box::new(f) as Box<_>)
-    } else {
-        Ok(Box::new(stdout()) as Box<_>)
-    }
-}
-
 /// Decodes a blob and writes its content either to stdout or into a file
 fn decode_blob(
     mut output_path: Option<&Path>,
@@ -61,7 +48,8 @@ fn decode_blob(
         _ => output_path,
     };
 
-    outfile_or_stdout(output_path)?.write_all(blob.decode(crypt_conf_opt, digest)?.as_slice())?;
+    crate::outfile_or_stdout(output_path)?
+        .write_all(blob.decode(crypt_conf_opt, digest)?.as_slice())?;
     Ok(())
 }
 
diff --git a/src/bin/proxmox_backup_debug/mod.rs b/src/bin/proxmox_backup_debug/mod.rs
index f092c585..31bc68c3 100644
--- a/src/bin/proxmox_backup_debug/mod.rs
+++ b/src/bin/proxmox_backup_debug/mod.rs
@@ -1,3 +1,22 @@
+use std::{
+    fs::File,
+    io::{stdout, Write},
+    panic::{RefUnwindSafe, UnwindSafe},
+    path::Path,
+};
+
 pub mod api;
 pub mod inspect;
 pub mod recover;
+
+// Returns either a new file, if a path is given, or stdout, if no path is given.
+pub(crate) fn outfile_or_stdout<P: AsRef<Path>>(
+    path: Option<P>,
+) -> std::io::Result<Box<dyn Write + Send + Sync + Unpin + RefUnwindSafe + UnwindSafe>> {
+    if let Some(path) = path {
+        let f = File::create(path)?;
+        Ok(Box::new(f) as Box<_>)
+    } else {
+        Ok(Box::new(stdout()) as Box<_>)
+    }
+}
-- 
2.30.2






More information about the pbs-devel mailing list