mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
craft-update: do not self-update extracted nextest test binaries (#1503)
* fix(update): treat extracted nextest archives as development builds * fix(update): match only binaries listed by bounded nextest inventory
This commit is contained in:
@@ -157,9 +157,11 @@ const CARGO_CACHE_MARKER: &str = "CACHEDIR.TAG";
|
||||
/// `build.build-dir` set, `.rustc_info.json` and those subdirectories live elsewhere and the
|
||||
/// profile directory has only Cargo's `.cargo-lock` file, which is accepted too.
|
||||
/// `CACHEDIR.TAG` alone isn't Cargo-specific (the same signature marks any cache directory), so
|
||||
/// it only corroborates that profile layout.
|
||||
/// it only corroborates that profile layout. Extracted nextest archives retain their
|
||||
/// `nextest/binaries-metadata.json` inventory rather than Cargo cache files; those are
|
||||
/// also build trees, even when the archive is unpacked outside its original target directory.
|
||||
fn is_development_build(exe: &Path, opted_in: bool) -> bool {
|
||||
!opted_in && exe.ancestors().skip(1).any(is_cargo_build_dir)
|
||||
!opted_in && exe.ancestors().skip(1).any(|dir| is_cargo_build_dir(dir) || is_nextest_binary(dir, exe))
|
||||
}
|
||||
|
||||
/// Whether `dir` is a Cargo target root (by its rustc-info cache marker) or a profile output
|
||||
@@ -182,6 +184,35 @@ fn is_cargo_build_dir(dir: &Path) -> bool {
|
||||
profile_parent.parent().is_some_and(|root| root.join(CARGO_CACHE_MARKER).is_file())
|
||||
}
|
||||
|
||||
// An archive inventory identifies only its listed binaries, not every installation below
|
||||
// its extraction directory. Bound the metadata read and refuse symlink markers.
|
||||
fn is_nextest_binary(root: &Path, exe: &Path) -> bool {
|
||||
use std::io::Read;
|
||||
const LIMIT: u64 = 1024 * 1024;
|
||||
let marker = root.join("nextest/binaries-metadata.json");
|
||||
let Ok(metadata) = std::fs::symlink_metadata(&marker) else { return false };
|
||||
if !metadata.file_type().is_file() || metadata.len() > LIMIT {
|
||||
return false;
|
||||
}
|
||||
let Ok(file) = std::fs::File::open(&marker) else { return false };
|
||||
let mut bytes = Vec::new();
|
||||
if file.take(LIMIT + 1).read_to_end(&mut bytes).is_err() || bytes.len() as u64 > LIMIT {
|
||||
return false;
|
||||
}
|
||||
let Ok(inventory) = serde_json::from_slice::<serde_json::Value>(&bytes) else { return false };
|
||||
let Some(build) = inventory.get("rust-build-meta") else { return false };
|
||||
let Some(target) = build.get("target-directory").and_then(|v| v.as_str()) else { return false };
|
||||
let original = build.get("build-directory").and_then(|v| v.as_str()).unwrap_or(target);
|
||||
let Some(binaries) = inventory.get("rust-binaries").and_then(|v| v.as_object()) else { return false };
|
||||
binaries.values().any(|binary| {
|
||||
let Some(path) = binary.get("binary-path").and_then(|v| v.as_str()) else { return false };
|
||||
let Ok(relative) = Path::new(path).strip_prefix(original) else { return false };
|
||||
!relative.as_os_str().is_empty()
|
||||
&& relative.components().all(|part| matches!(part, std::path::Component::Normal(_)))
|
||||
&& root.join(relative) == exe
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether this installed copy can update itself. [`release_installs`] says whether a given
|
||||
/// release has a file to update it from.
|
||||
pub fn is_self_updating(app: &App) -> bool {
|
||||
@@ -3786,6 +3817,42 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nextest_archive_binaries_do_not_update_themselves() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path().join("relocated-artifacts");
|
||||
let exe = root.join("debug/build/artcraft-launcher-native/hash/out/artcraft_launcher_native-hash");
|
||||
std::fs::create_dir_all(exe.parent().unwrap()).unwrap();
|
||||
std::fs::write(&exe, "ELF").unwrap();
|
||||
let metadata = root.join("nextest/binaries-metadata.json");
|
||||
std::fs::create_dir_all(metadata.parent().unwrap()).unwrap();
|
||||
// Names alone are not evidence of a Cargo build tree.
|
||||
assert!(!is_development_build(&exe, false));
|
||||
std::fs::write(&metadata, "{}").unwrap();
|
||||
assert!(!is_development_build(&exe, false), "empty inventory is not evidence");
|
||||
let inventory = serde_json::json!({
|
||||
"rust-build-meta": {"target-directory": "/original/target"},
|
||||
"rust-binaries": {"launcher": {"binary-path": "/original/target/debug/build/artcraft-launcher-native/hash/out/artcraft_launcher_native-hash"}}
|
||||
});
|
||||
std::fs::write(&metadata, inventory.to_string()).unwrap();
|
||||
assert!(is_development_build(&exe, false));
|
||||
assert_eq!(current_at(&crate::suite::LAUNCHER, &exe, false), None);
|
||||
assert!(!is_development_build(&exe, true), "explicit developer opt-in remains available");
|
||||
let installed = root.join("bin/artcraft-launcher");
|
||||
assert!(!is_development_build(&installed, false), "an unlisted descendant installation is not a test binary");
|
||||
std::fs::remove_file(&metadata).unwrap();
|
||||
std::fs::create_dir(&metadata).unwrap();
|
||||
assert!(!is_development_build(&exe, false), "metadata must be a file");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
std::fs::remove_dir(&metadata).unwrap();
|
||||
let external = dir.path().join("inventory.json");
|
||||
std::fs::write(&external, inventory.to_string()).unwrap();
|
||||
std::os::unix::fs::symlink(external, &metadata).unwrap();
|
||||
assert!(!is_development_build(&exe, false), "symlink inventory is not evidence");
|
||||
}
|
||||
}
|
||||
|
||||
/// A Cargo build tree is not recognised by names: an installed copy in a folder literally
|
||||
/// called `target` stays an installed copy.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user