|
| 1 | +//! Compression-related methods, all used in `axoasset::Local` |
| 2 | +
|
| 3 | +use crate::error::*; |
| 4 | +use camino::Utf8Path; |
| 5 | +use flate2::{write::ZlibEncoder, Compression, GzBuilder}; |
| 6 | +use std::{ |
| 7 | + fs::{self, DirEntry}, |
| 8 | + io::BufReader, |
| 9 | +}; |
| 10 | +use xz2::write::XzEncoder; |
| 11 | +use zip::ZipWriter; |
| 12 | + |
| 13 | +/// Internal tar-file compression algorithms |
| 14 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 15 | +pub(crate) enum CompressionImpl { |
| 16 | + /// .gz |
| 17 | + Gzip, |
| 18 | + /// .xz |
| 19 | + Xzip, |
| 20 | + /// .zstd |
| 21 | + Zstd, |
| 22 | +} |
| 23 | + |
| 24 | +pub(crate) fn tar_dir( |
| 25 | + src_path: &Utf8Path, |
| 26 | + dest_path: &Utf8Path, |
| 27 | + compression: &CompressionImpl, |
| 28 | +) -> Result<()> { |
| 29 | + // Set up the archive/compression |
| 30 | + // The contents of the zip (e.g. a tar) |
| 31 | + let dir_name = src_path.file_name().unwrap(); |
| 32 | + let zip_contents_name = format!("{dir_name}.tar"); |
| 33 | + let final_zip_file = match fs::File::create(dest_path) { |
| 34 | + Ok(file) => file, |
| 35 | + Err(details) => { |
| 36 | + return Err(AxoassetError::LocalAssetWriteNewFailed { |
| 37 | + dest_path: dest_path.to_string(), |
| 38 | + details, |
| 39 | + }) |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + match compression { |
| 44 | + CompressionImpl::Gzip => { |
| 45 | + // Wrap our file in compression |
| 46 | + let zip_output = GzBuilder::new() |
| 47 | + .filename(zip_contents_name) |
| 48 | + .write(final_zip_file, Compression::default()); |
| 49 | + |
| 50 | + // Write the tar to the compression stream |
| 51 | + let mut tar = tar::Builder::new(zip_output); |
| 52 | + |
| 53 | + // Add the whole dir to the tar |
| 54 | + if let Err(details) = tar.append_dir_all(dir_name, src_path) { |
| 55 | + return Err(AxoassetError::LocalAssetArchive { |
| 56 | + reason: format!("failed to copy directory into tar: {src_path} => {dir_name}",), |
| 57 | + details, |
| 58 | + }); |
| 59 | + } |
| 60 | + // Finish up the tarring |
| 61 | + let zip_output = match tar.into_inner() { |
| 62 | + Ok(out) => out, |
| 63 | + Err(details) => { |
| 64 | + return Err(AxoassetError::LocalAssetArchive { |
| 65 | + reason: format!("failed to write tar: {dest_path}"), |
| 66 | + details, |
| 67 | + }) |
| 68 | + } |
| 69 | + }; |
| 70 | + // Finish up the compression |
| 71 | + let _zip_file = match zip_output.finish() { |
| 72 | + Ok(file) => file, |
| 73 | + Err(details) => { |
| 74 | + return Err(AxoassetError::LocalAssetArchive { |
| 75 | + reason: format!("failed to write archive: {dest_path}"), |
| 76 | + details, |
| 77 | + }) |
| 78 | + } |
| 79 | + }; |
| 80 | + // Drop the file to close it |
| 81 | + } |
| 82 | + CompressionImpl::Xzip => { |
| 83 | + let zip_output = XzEncoder::new(final_zip_file, 9); |
| 84 | + // Write the tar to the compression stream |
| 85 | + let mut tar = tar::Builder::new(zip_output); |
| 86 | + |
| 87 | + // Add the whole dir to the tar |
| 88 | + if let Err(details) = tar.append_dir_all(dir_name, src_path) { |
| 89 | + return Err(AxoassetError::LocalAssetArchive { |
| 90 | + reason: format!("failed to copy directory into tar: {src_path} => {dir_name}",), |
| 91 | + details, |
| 92 | + }); |
| 93 | + } |
| 94 | + // Finish up the tarring |
| 95 | + let zip_output = match tar.into_inner() { |
| 96 | + Ok(out) => out, |
| 97 | + Err(details) => { |
| 98 | + return Err(AxoassetError::LocalAssetArchive { |
| 99 | + reason: format!("failed to write tar: {dest_path}"), |
| 100 | + details, |
| 101 | + }) |
| 102 | + } |
| 103 | + }; |
| 104 | + // Finish up the compression |
| 105 | + let _zip_file = match zip_output.finish() { |
| 106 | + Ok(file) => file, |
| 107 | + Err(details) => { |
| 108 | + return Err(AxoassetError::LocalAssetArchive { |
| 109 | + reason: format!("failed to write archive: {dest_path}"), |
| 110 | + details, |
| 111 | + }) |
| 112 | + } |
| 113 | + }; |
| 114 | + // Drop the file to close it |
| 115 | + } |
| 116 | + CompressionImpl::Zstd => { |
| 117 | + // Wrap our file in compression |
| 118 | + let zip_output = ZlibEncoder::new(final_zip_file, Compression::default()); |
| 119 | + |
| 120 | + // Write the tar to the compression stream |
| 121 | + let mut tar = tar::Builder::new(zip_output); |
| 122 | + |
| 123 | + // Add the whole dir to the tar |
| 124 | + if let Err(details) = tar.append_dir_all(dir_name, src_path) { |
| 125 | + return Err(AxoassetError::LocalAssetArchive { |
| 126 | + reason: format!("failed to copy directory into tar: {src_path} => {dir_name}",), |
| 127 | + details, |
| 128 | + }); |
| 129 | + } |
| 130 | + // Finish up the tarring |
| 131 | + let zip_output = match tar.into_inner() { |
| 132 | + Ok(out) => out, |
| 133 | + Err(details) => { |
| 134 | + return Err(AxoassetError::LocalAssetArchive { |
| 135 | + reason: format!("failed to write tar: {dest_path}"), |
| 136 | + details, |
| 137 | + }) |
| 138 | + } |
| 139 | + }; |
| 140 | + // Finish up the compression |
| 141 | + let _zip_file = match zip_output.finish() { |
| 142 | + Ok(file) => file, |
| 143 | + Err(details) => { |
| 144 | + return Err(AxoassetError::LocalAssetArchive { |
| 145 | + reason: format!("failed to write archive: {dest_path}"), |
| 146 | + details, |
| 147 | + }) |
| 148 | + } |
| 149 | + }; |
| 150 | + // Drop the file to close it |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + Ok(()) |
| 155 | +} |
| 156 | + |
| 157 | +pub(crate) fn zip_dir(src_path: &Utf8Path, dest_path: &Utf8Path) -> Result<()> { |
| 158 | + // Set up the archive/compression |
| 159 | + let final_zip_file = match fs::File::create(dest_path) { |
| 160 | + Ok(file) => file, |
| 161 | + Err(details) => { |
| 162 | + return Err(AxoassetError::LocalAssetWriteNewFailed { |
| 163 | + dest_path: dest_path.to_string(), |
| 164 | + details, |
| 165 | + }) |
| 166 | + } |
| 167 | + }; |
| 168 | + |
| 169 | + // Wrap our file in compression |
| 170 | + let mut zip = ZipWriter::new(final_zip_file); |
| 171 | + |
| 172 | + let dir = match std::fs::read_dir(src_path) { |
| 173 | + Ok(dir) => dir, |
| 174 | + Err(details) => { |
| 175 | + return Err(AxoassetError::LocalAssetReadFailed { |
| 176 | + origin_path: src_path.to_string(), |
| 177 | + details, |
| 178 | + }) |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + for entry in dir { |
| 183 | + if let Err(details) = copy_into_zip(entry, &mut zip) { |
| 184 | + return Err(AxoassetError::LocalAssetArchive { |
| 185 | + reason: format!("failed to create file in zip: {dest_path}"), |
| 186 | + details, |
| 187 | + }); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Finish up the compression |
| 192 | + let _zip_file = match zip.finish() { |
| 193 | + Ok(file) => file, |
| 194 | + Err(details) => { |
| 195 | + return Err(AxoassetError::LocalAssetArchive { |
| 196 | + reason: format!("failed to write archive: {dest_path}"), |
| 197 | + details: details.into(), |
| 198 | + }) |
| 199 | + } |
| 200 | + }; |
| 201 | + // Drop the file to close it |
| 202 | + Ok(()) |
| 203 | +} |
| 204 | + |
| 205 | +/// Copies a file into a provided `ZipWriter`. Mostly factored out so that we can bunch up |
| 206 | +/// a bunch of `std::io::Error`s without having to individually handle them. |
| 207 | +fn copy_into_zip( |
| 208 | + entry: std::result::Result<DirEntry, std::io::Error>, |
| 209 | + zip: &mut ZipWriter<fs::File>, |
| 210 | +) -> std::result::Result<(), std::io::Error> { |
| 211 | + let entry = entry?; |
| 212 | + if entry.file_type()?.is_file() { |
| 213 | + let options = |
| 214 | + zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored); |
| 215 | + let file = fs::File::open(entry.path())?; |
| 216 | + let mut buf = BufReader::new(file); |
| 217 | + let file_name = entry.file_name(); |
| 218 | + // FIXME: ...don't do this lossy conversion? |
| 219 | + let utf8_file_name = file_name.to_string_lossy(); |
| 220 | + zip.start_file(utf8_file_name.clone(), options)?; |
| 221 | + std::io::copy(&mut buf, zip)?; |
| 222 | + } else { |
| 223 | + todo!("implement zip subdirs! (or was this a symlink?)"); |
| 224 | + } |
| 225 | + Ok(()) |
| 226 | +} |
0 commit comments