| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::cache::Loaded; |
| 5 | | use oxy_core::objects::NodeKind; |
| 6 | | use oxy_core::repo::Repo; |
| 7 | | use oxy_core::state::FileContent; |
| 8 | | use std::io::Write; |
| 9 | | use std::path::{Path, PathBuf}; |
| 10 | |
|
| 11 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 12 | | pub enum Format { |
| 13 | | TarZst, |
| 14 | | Zip, |
| 15 | | } |
| 16 | |
|
| 17 | | impl Format { |
| 18 | 4× | pub fn parse(ext: &str) -> Option<Format> { |
| 19 | 4× | match ext { |
| 20 | 4× | "tar.zst" => Some(Format::TarZst), |
| 21 | 3× | "zip" => Some(Format::Zip), |
| 22 | 0× | _ => None, |
| 23 | | } |
| 24 | 4× | } |
| 25 | |
|
| 26 | 8× | pub fn ext(self) -> &'static str { |
| 27 | 8× | match self { |
| 28 | 2× | Format::TarZst => "tar.zst", |
| 29 | 6× | Format::Zip => "zip", |
| 30 | | } |
| 31 | 8× | } |
| 32 | |
|
| 33 | 4× | pub fn mime(self) -> &'static str { |
| 34 | 4× | match self { |
| 35 | 1× | Format::TarZst => "application/zstd", |
| 36 | 3× | Format::Zip => "application/zip", |
| 37 | | } |
| 38 | 4× | } |
| 39 | | } |
| 40 | |
|
| 41 | |
|
| 42 | | type Member = (String, Vec<u8>, bool, bool); |
| 43 | |
|
| 44 | |
|
| 45 | 4× | fn entries(repo: &Repo, loaded: &Loaded) -> anyhow::Result<Vec<Member>> { |
| 46 | 4× | let mut out = Vec::new(); |
| 47 | 15× | for (path, f) in &loaded.snapshot.files { |
| 48 | 15× | let bytes = match (&f.kind, &f.content) { |
| 49 | 5× | (NodeKind::Dir, _) | (_, FileContent::Dir) => continue, |
| 50 | 9× | (_, FileContent::Text { bytes, .. }) => bytes.clone(), |
| 51 | 1× | (_, FileContent::Opaque { blobs }) => match blobs.first() { |
| 52 | 1× | Some(b) => repo.read_blob(b)?, |
| 53 | 0× | None => vec![], |
| 54 | | }, |
| 55 | | }; |
| 56 | 10× | out.push((path.clone(), bytes, f.mode.exec, f.kind == NodeKind::Symlink)); |
| 57 | | } |
| 58 | 4× | Ok(out) |
| 59 | 4× | } |
| 60 | |
|
| 61 | |
|
| 62 | 4× | pub fn build(repo: &Repo, loaded: &Loaded, prefix: &str, format: Format, dest: &Path) -> anyhow::Result<()> { |
| 63 | 4× | if let Some(dir) = dest.parent() { |
| 64 | 4× | std::fs::create_dir_all(dir)?; |
| 65 | 0× | } |
| 66 | 4× | let tmp = dest.with_extension("partial"); |
| 67 | 4× | let files = entries(repo, loaded)?; |
| 68 | 4× | match format { |
| 69 | | Format::TarZst => { |
| 70 | 1× | let out = std::fs::File::create(&tmp)?; |
| 71 | 1× | let enc = zstd::Encoder::new(out, 19)?; |
| 72 | 1× | let mut tar = tar::Builder::new(enc); |
| 73 | 2× | for (path, bytes, exec, symlink) in &files { |
| 74 | 2× | let name = format!("{prefix}/{path}"); |
| 75 | 2× | let mut h = tar::Header::new_gnu(); |
| 76 | 2× | h.set_mtime(0); |
| 77 | 2× | if *symlink { |
| 78 | 0× | h.set_entry_type(tar::EntryType::Symlink); |
| 79 | 0× | h.set_size(0); |
| 80 | 0× | h.set_mode(0o777); |
| 81 | 0× | tar.append_link(&mut h, &name, String::from_utf8_lossy(bytes).as_ref())?; |
| 82 | | } else { |
| 83 | 2× | h.set_size(bytes.len() as u64); |
| 84 | 2× | h.set_mode(if *exec { 0o755 } else { 0o644 }); |
| 85 | 2× | tar.append_data(&mut h, &name, bytes.as_slice())?; |
| 86 | | } |
| 87 | | } |
| 88 | 1× | tar.into_inner()?.finish()?.flush()?; |
| 89 | | } |
| 90 | | Format::Zip => { |
| 91 | 3× | let out = std::fs::File::create(&tmp)?; |
| 92 | 3× | let mut zip = zip::ZipWriter::new(out); |
| 93 | 8× | for (path, bytes, exec, symlink) in &files { |
| 94 | 8× | let name = format!("{prefix}/{path}"); |
| 95 | 8× | let opts = zip::write::SimpleFileOptions::default() |
| 96 | 8× | .compression_method(zip::CompressionMethod::Deflated) |
| 97 | 8× | .unix_permissions(if *exec { 0o755 } else { 0o644 }); |
| 98 | 8× | if *symlink { |
| 99 | 0× | zip.add_symlink(name, String::from_utf8_lossy(bytes).as_ref(), opts)?; |
| 100 | | } else { |
| 101 | 8× | zip.start_file(name, opts)?; |
| 102 | 8× | zip.write_all(bytes)?; |
| 103 | | } |
| 104 | | } |
| 105 | 3× | zip.finish()?.flush()?; |
| 106 | | } |
| 107 | | } |
| 108 | 4× | std::fs::rename(&tmp, dest)?; |
| 109 | 4× | Ok(()) |
| 110 | 4× | } |
| 111 | |
|
| 112 | |
|
| 113 | 4× | pub fn cache_path(data: &Path, state: &str, prefix: &str, format: Format) -> PathBuf { |
| 114 | 4× | data.join("cache").join("archives").join(format!("{state}-{prefix}.{}", format.ext())) |
| 115 | 4× | } |