Craft Anything: understand how any software works, and craft your own.
Rust
A failed adb download or capture doesn't leave a folder that blocks the retry (#266)
BFlat9a829f7c31c1
diff --git a/crates/engine/src/tools/adb_tools.rs b/crates/engine/src/tools/adb_tools.rsindex 3fe35317..d0dcc6d5 100644--- a/crates/engine/src/tools/adb_tools.rs+++ b/crates/engine/src/tools/adb_tools.rs@@ -13,7 +13,7 @@//! which reports exactly that) returns a clear `EngineMissing`; when the device is missing or//! unauthorised they return `NotFound` / `NoPermission`. Nothing ever hangs.-use super::artifact_tools::create_absent_dir;+use super::artifact_tools::{UnlessWritten, create_absent_dir};use super::native_common::spec;use crate::tool::{Ctx, Effects, Requires, ToolOutput, ToolSpec, Toolset, parse_params, schema_of};use craft_anything_engines::adb::{self, Adb, Device};@@ -958,6 +958,7 @@ fn adb_app_download(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Toollet serial = serial_of(&p.serial)?;let package = adb::validate_package(&p.package)?;let outdir = create_absent_dir(&p.output_directory)?;+ let made = UnlessWritten::new(&outdir);let adb = adb_for(ctx)?.timeout(Duration::from_secs(300)).idle_timeout(Duration::from_secs(120));ensure_online(&adb, serial)?;let path_text = shell_text(&adb, serial, &format!("pm path {package}"), "pm path")?;@@ -992,6 +993,7 @@ fn adb_app_download(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Tool)).coverage(Coverage::complete(pulled.len() as u64)).build()?;+ made.keep();done(e)}@@ -1007,6 +1009,7 @@ fn adb_file_download(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Toolet serial = serial_of(&p.serial)?;let path = adb::validate_device_path(&p.device_path)?;let outdir = create_absent_dir(&p.output_directory)?;+ let made = UnlessWritten::new(&outdir);let adb = adb_for(ctx)?.timeout(Duration::from_secs(300)).idle_timeout(Duration::from_secs(120));ensure_online(&adb, serial)?;let out = adb.targeted(serial, ["pull", path, &outdir.to_string_lossy()])?;@@ -1021,6 +1024,7 @@ fn adb_file_download(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Too.location(Location::new("path", outdir.to_string_lossy())).coverage(Coverage::complete(files.len() as u64)).build()?;+ made.keep();done(e)}@@ -1028,6 +1032,7 @@ fn adb_screenshot_take(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Tlet p: OutputDirParams = parse_params(params)?;let serial = serial_of(&p.serial)?;let outdir = create_absent_dir(&p.output_directory)?;+ let made = UnlessWritten::new(&outdir);let adb = adb_for(ctx)?.timeout(Duration::from_secs(60));ensure_online(&adb, serial)?;let out = adb.exec_out(serial, "screencap -p")?;@@ -1047,6 +1052,7 @@ fn adb_screenshot_take(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, T.location(Location::new("path", file.to_string_lossy())).coverage(Coverage::complete(1)).build()?;+ made.keep();done(e)}@@ -1054,6 +1060,7 @@ fn adb_bugreport_save(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Tolet p: OutputDirParams = parse_params(params)?;let serial = serial_of(&p.serial)?;let outdir = create_absent_dir(&p.output_directory)?;+ let made = UnlessWritten::new(&outdir);// Bug reports can take minutes; keep a generous wall deadline but an idle deadline so a truly// stuck run still ends.let adb = adb_for(ctx)?.timeout(Duration::from_secs(600)).idle_timeout(Duration::from_secs(180));@@ -1071,6 +1078,7 @@ fn adb_bugreport_save(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, To.location(Location::new("path", outdir.to_string_lossy())).coverage(Coverage::complete(files.len() as u64)).build()?;+ made.keep();done(e)}@@ -1418,6 +1426,31 @@ mod tests {}}+ /// #234: a call that fails before writing anything leaves no folder, so the same call can be+ /// tried again; a folder with something in it, or a kept one, stays.+ #[test]+ fn a_failed_call_leaves_its_output_folder_free() {+ let (e, d) = engine_without_adb();+ if !adb_locatable(&e) {+ for id in ["adb_screenshot_take", "adb_bugreport_save", "adb_file_download", "adb_app_download"] {+ let out = d.path().join(id);+ let args = json!({"serial": "emulator-5554", "package": "com.example.app", "device_path": "/sdcard/x", "output_directory": out});+ assert_eq!(call_err(&e, id, args.clone()).code, ErrorCode::EngineMissing, "{id}");+ assert!(!out.exists(), "{id} left {out:?} behind");+ assert_eq!(call_err(&e, id, args).code, ErrorCode::EngineMissing, "{id}: the retry was blocked");+ }+ }+ let (empty, written, kept) = (d.path().join("empty"), d.path().join("written"), d.path().join("kept"));+ for dir in [&empty, &written, &kept] {+ std::fs::create_dir(dir).unwrap();+ }+ std::fs::write(written.join("part"), b"x").unwrap();+ drop(UnlessWritten::new(&empty));+ drop(UnlessWritten::new(&written));+ UnlessWritten::new(&kept).keep();+ assert!(!empty.exists() && written.exists() && kept.exists());+ }+#[test]fn inspect_client_never_needs_adb() {let (e, _d) = engine_without_adb();diff --git a/crates/engine/src/tools/artifact_tools.rs b/crates/engine/src/tools/artifact_tools.rsindex 0a3c3ad5..0c959a78 100644--- a/crates/engine/src/tools/artifact_tools.rs+++ b/crates/engine/src/tools/artifact_tools.rs@@ -256,6 +256,32 @@ pub(crate) fn create_absent_dir(p: &str) -> Result<PathBuf, ToolError> {Ok(path)}+/// Removes a folder from [`create_absent_dir`] when the tool stops before writing anything into+/// it, so the same call can be retried (#234). Call `keep` once the tool has succeeded.+pub(crate) struct UnlessWritten<'a> {+ dir: &'a Path,+ keep: bool,+}++impl<'a> UnlessWritten<'a> {+ pub(crate) fn new(dir: &'a Path) -> Self {+ UnlessWritten { dir, keep: false }+ }++ pub(crate) fn keep(mut self) {+ self.keep = true;+ }+}++impl Drop for UnlessWritten<'_> {+ fn drop(&mut self) {+ if !self.keep {+ // remove_dir only removes an empty folder, so anything written stays.+ let _ = std::fs::remove_dir(self.dir);+ }+ }+}+/// Turn an archive member name into safe relative components, or explain why not.pub(crate) fn safe_components(name: &str) -> Result<Vec<String>, &'static str> {if name.contains('\0') {