|
| 1 | +use std::{ |
| 2 | + io::{self, Cursor, Seek, Write}, |
| 3 | + path::Path, |
| 4 | +}; |
| 5 | +use tar::Builder; |
| 6 | +use tmc_langs_framework::{Compression, TmcError}; |
| 7 | +use tmc_langs_util::file_util; |
| 8 | +use zip::{write::FileOptions, ZipWriter}; |
| 9 | +use zstd::Encoder; |
| 10 | + |
| 11 | +pub enum ArchiveBuilder<W: Write + Seek> { |
| 12 | + Tar(Builder<W>), |
| 13 | + TarZstd(W, Builder<Cursor<Vec<u8>>>), |
| 14 | + Zip(ZipWriter<W>), |
| 15 | +} |
| 16 | + |
| 17 | +impl<W: Write + Seek> ArchiveBuilder<W> { |
| 18 | + pub fn new(writer: W, compression: Compression) -> Self { |
| 19 | + match compression { |
| 20 | + Compression::Tar => Self::Tar(Builder::new(writer)), |
| 21 | + Compression::TarZstd => Self::TarZstd(writer, Builder::new(Cursor::new(vec![]))), |
| 22 | + Compression::Zip => Self::Zip(ZipWriter::new(writer)), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn add_directory(&mut self, path: &str) -> Result<(), TmcError> { |
| 27 | + log::trace!("adding directory {}", path); |
| 28 | + match self { |
| 29 | + Self::Tar(builder) => { |
| 30 | + builder |
| 31 | + .append_dir_all(path, path) |
| 32 | + .map_err(TmcError::TarWrite)?; |
| 33 | + } |
| 34 | + Self::TarZstd(_, builder) => { |
| 35 | + builder |
| 36 | + .append_dir_all(path, path) |
| 37 | + .map_err(TmcError::TarWrite)?; |
| 38 | + } |
| 39 | + Self::Zip(builder) => { |
| 40 | + builder.add_directory(path, FileOptions::default().unix_permissions(0o755))? |
| 41 | + } |
| 42 | + } |
| 43 | + Ok(()) |
| 44 | + } |
| 45 | + |
| 46 | + pub fn add_file(&mut self, source: &Path, target: &str) -> Result<(), TmcError> { |
| 47 | + log::trace!("writing file {} as {}", source.display(), target); |
| 48 | + match self { |
| 49 | + Self::Tar(builder) => builder |
| 50 | + .append_path_with_name(source, target) |
| 51 | + .map_err(TmcError::TarWrite)?, |
| 52 | + Self::TarZstd(_, builder) => builder |
| 53 | + .append_path_with_name(source, target) |
| 54 | + .map_err(TmcError::TarWrite)?, |
| 55 | + Self::Zip(builder) => { |
| 56 | + let bytes = file_util::read_file(source)?; |
| 57 | + builder.start_file(target, FileOptions::default().unix_permissions(0o755))?; |
| 58 | + builder |
| 59 | + .write_all(&bytes) |
| 60 | + .map_err(|e| TmcError::ZipWrite(source.into(), e))?; |
| 61 | + } |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | + |
| 66 | + pub fn finish(self) -> Result<W, TmcError> { |
| 67 | + let res = match self { |
| 68 | + Self::Tar(builder) => builder.into_inner().map_err(TmcError::TarWrite)?, |
| 69 | + Self::TarZstd(writer, builder) => { |
| 70 | + let mut tar_data = builder.into_inner().map_err(TmcError::TarWrite)?; |
| 71 | + let mut encoder = Encoder::new(writer, 0).map_err(TmcError::ZstdWrite)?; |
| 72 | + io::copy(&mut tar_data, &mut encoder).map_err(TmcError::ZstdWrite)?; |
| 73 | + encoder.finish().map_err(TmcError::ZstdWrite)? |
| 74 | + } |
| 75 | + Self::Zip(mut builder) => builder.finish()?, |
| 76 | + }; |
| 77 | + Ok(res) |
| 78 | + } |
| 79 | +} |
0 commit comments