From 9eae781ab844012a8e7949ab00606e7fea397f08 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 5 Jul 2019 12:39:25 +0200 Subject: [PATCH] src/bin/pxar.rs - extract: allow to read from stdin --- src/bin/pxar.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/bin/pxar.rs b/src/bin/pxar.rs index 5561b7a6..cf586e66 100644 --- a/src/bin/pxar.rs +++ b/src/bin/pxar.rs @@ -84,11 +84,24 @@ fn dump_archive( let file = std::fs::File::open(archive)?; let mut reader = std::io::BufReader::new(file); dump_archive_from_reader(&mut reader, feature_flags)?; - }; + } Ok(Value::Null) } +fn extract_archive_from_reader(reader: &mut R, target: &str, feature_flags: u64, verbose: bool) -> Result<(), Error> { + let mut decoder = pxar::SequentialDecoder::new(reader, feature_flags, |path| { + if verbose { + println!("{:?}", path); + } + Ok(()) + }); + + decoder.restore(Path::new(target))?; + + Ok(()) +} + fn extract_archive( param: Value, _info: &ApiMethod, @@ -102,9 +115,6 @@ fn extract_archive( let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false); let no_acls = param["no-acls"].as_bool().unwrap_or(false); - let file = std::fs::File::open(archive)?; - - let mut reader = std::io::BufReader::new(file); let mut feature_flags = pxar::CA_FORMAT_DEFAULT; if no_xattrs { feature_flags ^= pxar::CA_FORMAT_WITH_XATTRS; @@ -116,14 +126,16 @@ fn extract_archive( feature_flags ^= pxar::CA_FORMAT_WITH_ACL; } - let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags, |path| { - if verbose { - println!("{:?}", path); - } - Ok(()) - }); - - decoder.restore(Path::new(target))?; + if archive == "-" { + let stdin = std::io::stdin(); + let mut reader = stdin.lock(); + extract_archive_from_reader(&mut reader, target, feature_flags, verbose)?; + } else { + println!("PXAR dump: {}", archive); + let file = std::fs::File::open(archive)?; + let mut reader = std::io::BufReader::new(file); + extract_archive_from_reader(&mut reader, target, feature_flags, verbose)?; + } Ok(Value::Null) }