diff --git a/io/assets/src/setup.rs b/io/assets/src/setup.rs index 2fc0954d3..a691f48ac 100644 --- a/io/assets/src/setup.rs +++ b/io/assets/src/setup.rs @@ -70,12 +70,7 @@ pub fn ffmpeg_tool(name: &str, fetch: bool) -> Result { let members = [format!("{top}/bin/ffmpeg.exe"), format!("{top}/bin/ffprobe.exe"), format!("{top}/LICENSE")]; let part = dir.with_file_name(format!("ffmpeg.part{}", std::process::id())); let fetched = fetch_archive(&part, &url, &name_zip, FFMPEG_SUM, true, &members.each_ref().map(String::as_str)); - if fetched.is_ok() && !bin.is_file() { - let _ = std::fs::rename(&part, &dir); - } - let _ = std::fs::remove_dir_all(&part); - fetched?; - if bin.is_file() { Ok(bin) } else { Err(format!("couldn't put FFmpeg in {}", dir.display())) } + settle(&part, &dir, &bin, fetched, "FFmpeg") } /// The Python of the venv in an install folder: .venv/bin/python, or .venv\Scripts\python.exe on Windows @@ -113,12 +108,29 @@ pub fn uv() -> Result { let part = dir.with_file_name(format!("uv.part{}", std::process::id())); let url = format!("https://github.com/astral-sh/uv/releases/download/{UV}/{name}"); let fetched = fetch_archive(&part, &url, &name, sum, os != "windows", &[]); + settle(&part, &dir, &bin, fetched, "uv") +} + +/// A fetch's end: its `part` folder moved in as `dir` when it's whole and nothing got there first, the part removed, +/// and, once the tool is in place, the parts stopped fetches left beside it swept (`NAME.part`, from a Ctrl+C or +/// a killed process: FFmpeg's held a 114 MB zip, and each run made a new one). A part another process is still +/// writing stays on Windows, where its open file can't be removed; elsewhere that fetch fails and finds the tool in +/// place, which counts as done. +fn settle(part: &Path, dir: &Path, bin: &Path, fetched: Result<(), String>, what: &str) -> Result { if fetched.is_ok() && !bin.is_file() { - let _ = std::fs::rename(&part, &dir); + let _ = std::fs::rename(part, dir); + } + let _ = std::fs::remove_dir_all(part); + if bin.is_file() { + let stale = dir.file_name().map(|n| format!("{}.part", n.to_string_lossy())).unwrap_or_default(); + let beside = dir.parent().map(std::fs::read_dir).into_iter().flatten().flatten().flatten(); + for e in beside.filter(|e| !stale.is_empty() && e.file_name().to_string_lossy().starts_with(&stale)) { + let _ = std::fs::remove_dir_all(e.path()); + } + return Ok(bin.to_path_buf()); } - let _ = std::fs::remove_dir_all(&part); fetched?; - if bin.is_file() { Ok(bin) } else { Err(format!("couldn't put uv in {}", dir.display())) } + Err(format!("couldn't put {what} in {}", dir.display())) } /// A release archive `name` from `url` downloaded into `part` with curl (resumed across its tries: a dropped connection @@ -175,3 +187,42 @@ pub fn run_setup(script: &Path, var: &str, dir: &Path) -> Result