diff --git a/src/service/client.rs b/src/service/client.rs index 7675c08..8c10973 100644 --- a/src/service/client.rs +++ b/src/service/client.rs @@ -36,9 +36,16 @@ impl Client { Proxy::new(&self.connection,BUS_NAME,if packages {"/org/archipelago/System1/HostExtensions"} else {MANAGER_PATH}, if packages {"org.archipelago.System1.HostExtensions"} else {MANAGER_INTERFACE}).map_err(error) } - fn options(&self) -> Dictionary { - Dictionary::from([("request_id".into(),text(&self.request_id)),("interactive".into(),self.interactive.into()),("offline".into(),self.offline.into())]) + fn options(&self) -> Dictionary { self.options_for(&self.request_id) } + fn options_for(&self, request: &str) -> Dictionary { + Dictionary::from([("request_id".into(),text(request)),("interactive".into(),self.interactive.into()),("offline".into(),self.offline.into())]) } + /// One command may import several archives, and every import is a separate + /// durable request: reusing the command token made the journal reject the + /// second archive ("request ID reused with different canonical inputs"). + /// The derived token keeps the UUID shape, so a retried command reuses the + /// same per-archive identity instead of creating a second one. + fn archive_request(&self, index: usize) -> String { derived_request(&self.request_id, index) } fn operation(&self, packages: bool, method: &str, body: &B) -> Result { let proxy = self.proxy(packages)?; self.operation_on(&proxy, method, body) @@ -138,14 +145,14 @@ impl Client { /// Import one local archive through the protected FD path. The daemon keeps /// the copied bytes, so the caller's path is never an authority and the CLI /// must not hand it over. - fn import(&self, path: &str) -> Result { + fn import(&self, path: &str, request: &str) -> Result { use std::os::fd::AsFd; let file = fs::File::open(path).map_err(|e| format!("Cannot open local package {path}: {e}"))?; if !file.metadata().map_err(error)?.is_file() { return Err(format!("Not a regular package archive: {path}")); } let fd = zbus::zvariant::Fd::from(file.as_fd()); - self.operation(true,"ImportPackage",&(fd,self.options())) + self.operation(true,"ImportPackage",&(fd,self.options_for(request))) } /// `extension add` accepts repository names, local archives or both. Every /// archive is imported first (its exact bytes and identity come from the @@ -156,8 +163,8 @@ impl Client { let context = state["context_id"].as_str().ok_or("Missing package context")?.to_owned(); let mut changes = Vec::new(); let mut imported = Vec::new(); - for path in &files { - let result = self.import(path)?; + for (index, path) in files.iter().enumerate() { + let result = self.import(path, &self.archive_request(index))?; let input_id = result["input_id"].as_str().ok_or("Import returned no input identity")?; if let Some(package) = result.get("package") { imported.push(package.clone()); } changes.push(Dictionary::from([("op".into(),text("add-local")),("input_id".into(),text(input_id))])); @@ -266,7 +273,7 @@ impl Client { ["update",mode,"--plan",id] if matches!(*mode,"apply" | "download") => self.operation(false,if *mode == "apply" {"Stage"} else {"Download"},&(id,self.options())), ["extension","status"] => self.package_state(), - ["extension","import",path] => self.import(path), + ["extension","import",path] => { let request = self.archive_request(0); self.import(path, &request) }, ["extension","add-local",input] => { let state = self.package_state()?; let context = state["context_id"].as_str().ok_or("Missing package context")?; @@ -548,6 +555,20 @@ pub fn error_exit_code(message: &str) -> u8 { .map(|code| code.exit_code()).unwrap_or(1) } +/// Per-archive token derived from the command's request id. Only the trailing +/// hex digits change, so the UUID version/variant nibbles and the retry +/// semantics of the original token stay intact. +fn derived_request(base: &str, index: usize) -> String { + let hex: Vec = base.chars().filter(|c| c.is_ascii_hexdigit()).take(32).collect(); + if hex.len() != 32 { return base.to_owned(); } + let mut hex = hex; + let tag = format!("{:02x}", index & 0xff); + hex[30] = tag.chars().next().unwrap_or('0'); + hex[31] = tag.chars().nth(1).unwrap_or('0'); + let s: String = hex.iter().collect(); + format!("{}-{}-{}-{}-{}", &s[0..8], &s[8..12], &s[12..16], &s[16..20], &s[20..32]) +} + fn offline_applies(args: &[&str]) -> bool { matches!(args.first().copied(), Some("update" | "extension" | "overlay")) } @@ -586,6 +607,17 @@ mod interface_tests { assert!(karg_edits(&args).is_err()); } } + #[test] + fn every_imported_archive_gets_its_own_request_token() { + let base = "1c762b56-8503-4a57-b1bd-88a0fda4842d"; + let tokens: Vec = (0..4).map(|i| derived_request(base, i)).collect(); + for (i, token) in tokens.iter().enumerate() { + assert!(archipelago_contracts::system_bus::request_id(token), "index {i}: {token}"); + assert!(tokens[..i].iter().all(|prior| prior != token), "duplicate token at {i}"); + } + assert_eq!(derived_request("not-a-uuid", 2), "not-a-uuid"); + } + #[test] fn offline_is_limited_to_commands_that_can_use_a_cache() { assert!(offline_applies(&["update", "check"]));