From 09b649d0ac85b323ce6c674f70f1c99f791971bf Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Fri, 27 Nov 2026 01:27:19 +0000 Subject: [PATCH] fix: encode raw bytes in bundle locators `offline::write` now percent-encodes each relative path from its raw bytes. It used `Path::to_string_lossy` before, which turned a non-UTF-8 directory byte into the Unicode replacement character, so the locator named the wrong path. --- changelog.d/+offline-locator-bytes.bugfix.md | 2 + src/offline.rs | 55 ++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 changelog.d/+offline-locator-bytes.bugfix.md diff --git a/changelog.d/+offline-locator-bytes.bugfix.md b/changelog.d/+offline-locator-bytes.bugfix.md new file mode 100644 index 00000000..3378e639 --- /dev/null +++ b/changelog.d/+offline-locator-bytes.bugfix.md @@ -0,0 +1,2 @@ +`offline::write` percent-encodes the raw bytes of each relative +path, so a non-UTF-8 directory stays a distinct locator. diff --git a/src/offline.rs b/src/offline.rs index e10584f2..7cdff54b 100644 --- a/src/offline.rs +++ b/src/offline.rs @@ -8,6 +8,7 @@ use sha2::{Digest, Sha256}; use std::collections::{BTreeMap, BTreeSet}; use std::fs::File; use std::io::Read; +use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; /// One file in an offline bundle. @@ -102,9 +103,9 @@ fn files_under(dir: &Path, out: &mut Vec) -> Result<(), Fail> { Ok(()) } -fn encode_locator(text: &str) -> String { +fn encode_locator(bytes: &[u8]) -> String { let mut out = String::new(); - for byte in text.bytes() { + for &byte in bytes { if byte.is_ascii_alphanumeric() || matches!(byte, b'/' | b'.' | b'_' | b'-') { out.push(byte as char); } else { @@ -233,7 +234,7 @@ pub fn write(root: &Path, scope: &Scope<'_>, assets: &[OfflineAsset]) -> Result< entries.insert(Entry { url: format!( "eb-stack-planner:{}", - encode_locator(&rel.to_string_lossy()) + encode_locator(rel.as_os_str().as_bytes()) ), sha256: digest.clone(), local_name: local_name(&path)?, @@ -273,7 +274,7 @@ pub fn write(root: &Path, scope: &Scope<'_>, assets: &[OfflineAsset]) -> Result< entries.insert(Entry { url: format!( "easybuild-source:{}", - encode_locator(&rel.to_string_lossy()) + encode_locator(rel.as_os_str().as_bytes()) ), sha256: digest.clone(), local_name: local_name(&path)?, @@ -470,6 +471,52 @@ mod tests { ); } + #[test] + fn a_non_utf8_directory_keeps_its_bytes_in_the_locator() { + use std::ffi::OsString; + use std::os::unix::ffi::OsStringExt; + + let root = tempfile::tempdir().unwrap(); + let source = root.path().join("sources"); + let odd = source.join(OsString::from_vec(vec![0xff])); + std::fs::create_dir_all(&odd).unwrap(); + let payload = odd.join("data.txt"); + std::fs::write(&payload, b"payload\n").unwrap(); + + let planner = root.path().join("bundle/planner"); + let planned = planner.join(OsString::from_vec(vec![0xfe])); + std::fs::create_dir_all(&planned).unwrap(); + std::fs::write(planned.join("plan.txt"), b"plan\n").unwrap(); + + let mut site = minimal_site(); + site.filesystems.source_paths = vec![source.to_str().unwrap().to_string()]; + let scope = Scope { + site: &site, + site_file: None, + target: site.target("cpu").unwrap(), + build_type: None, + run_id: "run".into(), + log_stamp: "20261121_220000".into(), + }; + let bundle = root.path().join("bundle"); + let manifest = write(&bundle, &scope, &[]).unwrap(); + let urls: Vec<_> = manifest + .files + .iter() + .map(|entry| entry.url.as_str()) + .collect(); + assert_eq!( + urls, + [ + "easybuild-source:%FF/data.txt", + "eb-stack-planner:%FE/plan.txt", + ] + ); + assert_eq!(manifest.files[0].local_name, "data.txt"); + assert_eq!(manifest.files[0].sha256, sha256(&payload).unwrap()); + assert_eq!(manifest.files[1].local_name, "plan.txt"); + } + #[test] fn object_store_reaps_stale_local_temp_and_keeps_foreign_temp() { let _guard = atomic_test_support::guard();