From e2e4b3b917725cded27f9871e15a52d7915182fa Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Thu, 3 Dec 2026 14:25:33 +0530 Subject: [PATCH] xtask: web-fonts writes its scratch table in a private tempfile folder --- Cargo.lock | 1 + xtask/Cargo.toml | 3 +++ xtask/src/web_fonts.rs | 25 ++++--------------------- 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1f40a61ee..383ccf4059 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10420,6 +10420,7 @@ version = "0.25.1" dependencies = [ "craft-fonts-build", "serde_json", + "tempfile", ] [[package]] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 53678d8216..d72887dfde 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -12,6 +12,9 @@ workspace = true serde_json = { workspace = true } # The interface faces the web site serves under fonts/ui/, written as crates/text/build.rs serves them. craft-fonts-build = { workspace = true } +# The private scratch folder web-fonts writes the served-faces table to. Already in Cargo.lock +# (the apps use it), so this adds no new crate. +tempfile = "3" # The files of other crates that this crate compiles, for CI's test selection. [package.metadata.craft] diff --git a/xtask/src/web_fonts.rs b/xtask/src/web_fonts.rs index 5c2309b982..d30ef58c8e 100644 --- a/xtask/src/web_fonts.rs +++ b/xtask/src/web_fonts.rs @@ -52,7 +52,9 @@ fn serve(list_path: &Path, dir: &Path, site: &Path, required: Option) -> R let faces: Vec<(String, String)> = list::parse_list(&list)?.into_iter().filter(|e| e.startup || e.demand).map(|e| (e.family.to_owned(), e.style.to_owned())).collect(); // build.rs's table is the one the app uses; this copy, and Embed's hash cache beside it, go. - let scratch = new_scratch_dir()?; + // A private folder this call creates (random name, created exclusively, owner-only on Unix) + // and removes when `scratch` drops, so nothing another process made is reused or removed. + let scratch = tempfile::Builder::new().prefix("vectorcraft-web-fonts-").tempdir().map_err(|e| format!("scratch folder: {e}"))?; let embed = craft_fonts_build::Embed::new().dir(dir); let embed = match required { Some(required) => embed.required(required), @@ -61,29 +63,10 @@ fn serve(list_path: &Path, dir: &Path, site: &Path, required: Option) -> R let served = embed .filter(move |f| faces.iter().any(|(family, style)| f.family == *family && f.style == *style)) .serve(site.join(SERVED_URL), SERVED_URL) - .write(scratch.join("served.rs")); - let _ = std::fs::remove_dir_all(&scratch); + .write(scratch.path().join("served.rs")); served.map(|faces| faces.len()).map_err(|e| e.to_string()) } -/// A new, empty folder in the temp directory that this call created, so removing it afterwards -/// removes nothing another process or run made: `create_dir` fails on a folder that already -/// exists, and the next name is tried. -fn new_scratch_dir() -> Result { - static RUN: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); - let mut last = String::new(); - for _ in 0..100 { - let run = RUN.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - let path = std::env::temp_dir().join(format!("vectorcraft-web-fonts-{}-{run}", std::process::id())); - match std::fs::create_dir(&path) { - Ok(()) => return Ok(path), - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => last = format!("{}: {e}", path.display()), - Err(e) => return Err(format!("{}: {e}", path.display())), - } - } - Err(format!("no free scratch folder in the temp directory ({last})")) -} - /// Copy the faces `list_path` names from the craft-fonts checkout `dir` into `site` (at their /// `fonts/…` URLs). Returns how many were copied and the listed faces the checkout lacks. fn copy(list_path: &Path, dir: &Path, site: &Path) -> Result<(usize, Vec), String> {