refactor(cache): name cache entries without touching the network

generate_cache_filename derived a distro tarball's extension by
resolving its URL - a naming function with a network dependency and
a silent unwrap_or_default degradation. Naming is now pure string
shaping: distro artifacts are gzipped tarballs, so the name says
.tar.gz, and a future artifact format changes extraction and this
line together. Same filenames as before for every existing entry;
a caller skipping resolve_source now gets a deterministic key
instead of a lookup.
This commit is contained in:
vhaudiquet committed 2026-10-30 00:45:00 +01:00
1 parent 479dfb79d0
commit 89a1028d46
1 file changed
+9 -26
+9 -26
View File
@@ -12,7 +12,7 @@ use std::path::{Path, PathBuf};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use crate::distro::{map_arch, resolve_distro_url, resolve_source, Distro, ImageSource}; use crate::distro::{map_arch, resolve_source, Distro, ImageSource};
use crate::download::{digest_sidecar, download_image}; use crate::download::{digest_sidecar, download_image};
use crate::error::Error; use crate::error::Error;
use crate::extract::extract_tarball; use crate::extract::extract_tarball;
@@ -377,20 +377,18 @@ fn generate_cache_filename(source: &ImageSource, arch: &str) -> String {
Distro::Alpine => "alpine", Distro::Alpine => "alpine",
}; };
let distro_arch = map_arch(*distro, arch); let distro_arch = map_arch(*distro, arch);
// The extension comes from the URL. `version` is the // Pure naming: no URL resolution, no network, ever — a
// resolved value (resolve_source ran first), so this // path derivation is a string shape, not an I/O decision.
// derivation is offline: numbers and patch-resolved // The version here is whatever the caller carries
// strings pass through without lookups. A resolution // (resolve_source resolves before prepare, so internal
// failure here degrades to the plain .tar.gz name rather // paths always name numbers). Dist artifacts are gzipped
// than blocking every cache-path computation. // tarballs; a new artifact format changes extraction and
let url = resolve_distro_url(distro, version.as_deref(), arch).unwrap_or_default(); // this line together.
let ext = get_tarball_extension(&url);
format!( format!(
"{}-{}-{}.{}", "{}-{}-{}.tar.gz",
distro_name, distro_name,
version.as_deref().unwrap_or("latest"), version.as_deref().unwrap_or("latest"),
distro_arch, distro_arch,
ext
) )
} }
ImageSource::OciImage { ImageSource::OciImage {
@@ -606,18 +604,3 @@ mod tests {
); );
} }
} }
fn get_tarball_extension(url: &str) -> &str {
// Extract extension from URL (e.g., .tar.gz, .tar.xz, .tar.zst)
if url.ends_with(".tar.zst") {
"tar.zst"
} else if url.ends_with(".tar.xz") {
"tar.xz"
} else if url.ends_with(".tar.gz") {
"tar.gz"
} else if url.ends_with(".tar.bz2") {
"tar.bz2"
} else {
"tar.gz" // default
}
}