debug: move outfile_or_stdout to module for reuse

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Tested-by: Hannes Laimer <h.laimer@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2022-05-23 16:11:34 +02:00 committed by Thomas Lamprecht
parent 53435bc4d5
commit b11693b2f7
2 changed files with 22 additions and 15 deletions

View File

@ -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(())
}

View File

@ -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<_>)
}
}