|
| 1 | +// This file is part of the uutils tar package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use crate::errors::TarError; |
| 7 | +use crate::CompressionMode; |
| 8 | +use std::fs::File; |
| 9 | +use std::io::{Read, Write}; |
| 10 | +use std::path::Path; |
| 11 | + |
| 12 | +pub fn open_archive_reader( |
| 13 | + archive_path: &Path, |
| 14 | + compression: CompressionMode, |
| 15 | +) -> Result<Box<dyn Read>, TarError> { |
| 16 | + let file = File::open(archive_path).map_err(|e| TarError::from_io_error(e, archive_path))?; |
| 17 | + |
| 18 | + match compression { |
| 19 | + CompressionMode::None => Ok(Box::new(file)), |
| 20 | + CompressionMode::Zstd => { |
| 21 | + let decoder = zstd::stream::read::Decoder::new(file).map_err(|e| { |
| 22 | + TarError::InvalidArchive(format!( |
| 23 | + "Failed to initialize zstd decoder for '{}': {}", |
| 24 | + archive_path.display(), |
| 25 | + e |
| 26 | + )) |
| 27 | + })?; |
| 28 | + Ok(Box::new(decoder)) |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub struct ArchiveWriter { |
| 34 | + inner: ArchiveWriterInner, |
| 35 | +} |
| 36 | + |
| 37 | +enum ArchiveWriterInner { |
| 38 | + Plain(File), |
| 39 | + Zstd(zstd::stream::write::Encoder<'static, File>), |
| 40 | +} |
| 41 | + |
| 42 | +impl ArchiveWriter { |
| 43 | + pub fn create(archive_path: &Path, compression: CompressionMode) -> Result<Self, TarError> { |
| 44 | + let file = File::create(archive_path).map_err(|e| { |
| 45 | + TarError::TarOperationError(format!( |
| 46 | + "Cannot create archive '{}': {}", |
| 47 | + archive_path.display(), |
| 48 | + e |
| 49 | + )) |
| 50 | + })?; |
| 51 | + |
| 52 | + let inner = match compression { |
| 53 | + CompressionMode::None => ArchiveWriterInner::Plain(file), |
| 54 | + CompressionMode::Zstd => { |
| 55 | + let encoder = zstd::stream::write::Encoder::new(file, 0).map_err(|e| { |
| 56 | + TarError::TarOperationError(format!( |
| 57 | + "Failed to initialize zstd encoder for '{}': {}", |
| 58 | + archive_path.display(), |
| 59 | + e |
| 60 | + )) |
| 61 | + })?; |
| 62 | + ArchiveWriterInner::Zstd(encoder) |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + Ok(Self { inner }) |
| 67 | + } |
| 68 | + |
| 69 | + pub fn finish(self) -> Result<(), TarError> { |
| 70 | + match self.inner { |
| 71 | + ArchiveWriterInner::Plain(mut file) => file.flush().map_err(TarError::from), |
| 72 | + ArchiveWriterInner::Zstd(encoder) => encoder.finish().map(|_| ()).map_err(|e| { |
| 73 | + TarError::TarOperationError(format!("Failed to finalize zstd archive: {e}")) |
| 74 | + }), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Write for ArchiveWriter { |
| 80 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 81 | + match &mut self.inner { |
| 82 | + ArchiveWriterInner::Plain(file) => file.write(buf), |
| 83 | + ArchiveWriterInner::Zstd(encoder) => encoder.write(buf), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fn flush(&mut self) -> std::io::Result<()> { |
| 88 | + match &mut self.inner { |
| 89 | + ArchiveWriterInner::Plain(file) => file.flush(), |
| 90 | + ArchiveWriterInner::Zstd(encoder) => encoder.flush(), |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments