Add ability to write archives to stdout

This commit is contained in:
Nathan Fisher 2024-01-23 14:31:21 -05:00
parent 9938e7252a
commit ce1c383f44
2 changed files with 36 additions and 1 deletions

View File

@ -40,3 +40,5 @@ to see Haggis implemented in other languages.
- [x] Override user/group when extracting archives
- [x] Automatically detect zstd compressed archives
- [ ] Add path to error message when passing between threads
- [x] Add ability to write archives to stdout
- [ ] Add ability to read archives from stdin

View File

@ -3,7 +3,7 @@
use std::{
collections::HashMap,
fs,
io::{BufWriter, Read, Seek, Write},
io::{self, BufWriter, Read, Seek, Write},
sync::Mutex,
};
#[cfg(feature = "parallel")]
@ -72,6 +72,21 @@ pub fn create_archive(
Ok(())
}
/// Creates a haggis archive and writes it to stdout
/// # Errors
/// Returns `crate::Error` if io fails
pub fn create_archive_stdout(
files: &[String],
algorithm: Algorithm,
uid: Option<u32>,
gid: Option<u32>,
) -> Result<(), Error> {
let stdout = io::stdout().lock();
let mut writer = BufWriter::new(stdout);
stream_archive(&mut writer, files, algorithm, uid, gid)?;
Ok(())
}
/// Streams a haggis archive over something which implements `Write`
/// # Errors
/// Returns `crate::Error` if io fails or several other error conditions
@ -130,6 +145,24 @@ pub fn par_create_archive(
Ok(())
}
/// Creates a Haggis archive from a list of file, processing each file in parallel
/// and writing the resulting archive to stdout
/// # Errors
/// Returns `crate::Error` if io fails of one of several other conditions
#[cfg(feature = "parallel")]
pub fn par_create_archive_stdout(
files: &[String],
algorithm: Algorithm,
sender: &Sender<Message>,
uid: Option<u32>,
gid: Option<u32>,
) -> Result<(), Error> {
let stdout = io::stdout().lock();
let writer = BufWriter::new(stdout);
par_stream_archive(writer, files, algorithm, uid, gid)?;
Ok(())
}
/// Streams a Haggis archive from a list of files, processing each file in parallel
/// # Errors
/// Returns `crate::Error` if io fails or several other error conditions