xtask: web-fonts writes its scratch table in a private tempfile folder

This commit is contained in:
ruchirguitar committed 2026-12-03 14:25:33 +05:30
1 parent a1ef841a71
commit e2e4b3b917
3 files changed
+8 -21

No files matched your search

Generated
+1
View File
@@ -10420,6 +10420,7 @@ version = "0.25.1"
dependencies = [
"craft-fonts-build",
"serde_json",
"tempfile",
]
[[package]]
+3
View File
@@ -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]
+4 -21
View File
@@ -52,7 +52,9 @@ fn serve(list_path: &Path, dir: &Path, site: &Path, required: Option<bool>) -> 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<bool>) -> 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<PathBuf, String> {
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>), String> {