1use anyhow::{Context, Result, anyhow};
20use serde_json::Value;
21use std::collections::HashMap;
22use std::fs;
23use std::io::Write;
24use std::path::{Path, PathBuf};
25use std::sync::{Arc, Mutex, OnceLock};
26
27pub fn config_dir() -> Result<PathBuf> {
32 if let Ok(home) = std::env::var("WIRE_HOME") {
33 return Ok(PathBuf::from(home).join("config").join("wire"));
34 }
35 dirs::config_dir()
36 .map(|d| d.join("wire"))
37 .ok_or_else(|| anyhow!("could not resolve XDG_CONFIG_HOME — set WIRE_HOME"))
38}
39
40pub fn state_dir() -> Result<PathBuf> {
44 if let Ok(home) = std::env::var("WIRE_HOME") {
45 return Ok(PathBuf::from(home).join("state").join("wire"));
46 }
47 dirs::state_dir()
48 .or_else(dirs::data_local_dir)
49 .map(|d| d.join("wire"))
50 .ok_or_else(|| anyhow!("could not resolve XDG_STATE_HOME — set WIRE_HOME"))
51}
52
53pub fn private_key_path() -> Result<PathBuf> {
54 Ok(config_dir()?.join("private.key"))
55}
56pub fn agent_card_path() -> Result<PathBuf> {
57 Ok(config_dir()?.join("agent-card.json"))
58}
59pub fn trust_path() -> Result<PathBuf> {
60 Ok(config_dir()?.join("trust.json"))
61}
62pub fn config_toml_path() -> Result<PathBuf> {
63 Ok(config_dir()?.join("config.toml"))
64}
65pub fn inbox_dir() -> Result<PathBuf> {
66 Ok(state_dir()?.join("inbox"))
67}
68pub fn outbox_dir() -> Result<PathBuf> {
69 Ok(state_dir()?.join("outbox"))
70}
71
72static OUTBOX_LOCKS: OnceLock<Mutex<HashMap<PathBuf, Arc<Mutex<()>>>>> = OnceLock::new();
84
85fn outbox_lock(path: &Path) -> Arc<Mutex<()>> {
86 let registry = OUTBOX_LOCKS.get_or_init(|| Mutex::new(HashMap::new()));
87 let mut g = registry.lock().expect("OUTBOX_LOCKS poisoned");
88 g.entry(path.to_path_buf())
89 .or_insert_with(|| Arc::new(Mutex::new(())))
90 .clone()
91}
92
93pub fn append_outbox_record(peer: &str, record_bytes: &[u8]) -> Result<PathBuf> {
110 ensure_dirs()?;
111 let normalized = crate::agent_card::bare_handle(peer);
112 let path = outbox_dir()?.join(format!("{normalized}.jsonl"));
113 let lock = outbox_lock(&path);
114 let _g = lock.lock().expect("outbox per-path mutex poisoned");
115 let mut f = fs::OpenOptions::new()
116 .create(true)
117 .append(true)
118 .open(&path)
119 .with_context(|| format!("opening outbox {path:?}"))?;
120 let mut buf = Vec::with_capacity(record_bytes.len() + 1);
121 buf.extend_from_slice(record_bytes);
122 buf.push(b'\n');
123 f.write_all(&buf)
124 .with_context(|| format!("appending to {path:?}"))?;
125 Ok(path)
126}
127
128pub fn is_initialized() -> Result<bool> {
130 Ok(private_key_path()?.exists() && agent_card_path()?.exists())
131}
132
133pub fn ensure_dirs() -> Result<()> {
135 let cfg = config_dir()?;
136 fs::create_dir_all(&cfg).with_context(|| format!("creating {cfg:?}"))?;
137 fs::create_dir_all(state_dir()?)?;
138 fs::create_dir_all(inbox_dir()?)?;
139 fs::create_dir_all(outbox_dir()?)?;
140 set_dir_mode_0700(&cfg)?;
141 Ok(())
142}
143
144#[cfg(unix)]
145fn set_dir_mode_0700(path: &Path) -> Result<()> {
146 use std::os::unix::fs::PermissionsExt;
147 let mut perms = fs::metadata(path)?.permissions();
148 perms.set_mode(0o700);
149 fs::set_permissions(path, perms)?;
150 Ok(())
151}
152
153#[cfg(not(unix))]
154fn set_dir_mode_0700(_: &Path) -> Result<()> {
155 Ok(())
156}
157
158pub fn write_private_key(seed: &[u8; 32]) -> Result<()> {
160 let path = private_key_path()?;
161 fs::write(&path, seed).with_context(|| format!("writing {path:?}"))?;
162 set_file_mode_0600(&path)?;
163 Ok(())
164}
165
166#[cfg(unix)]
167fn set_file_mode_0600(path: &Path) -> Result<()> {
168 use std::os::unix::fs::PermissionsExt;
169 let mut perms = fs::metadata(path)?.permissions();
170 perms.set_mode(0o600);
171 fs::set_permissions(path, perms)?;
172 Ok(())
173}
174
175#[cfg(not(unix))]
176fn set_file_mode_0600(_: &Path) -> Result<()> {
177 Ok(())
178}
179
180pub fn read_private_key() -> Result<[u8; 32]> {
182 let path = private_key_path()?;
183 let bytes = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
184 if bytes.len() != 32 {
185 return Err(anyhow!(
186 "private key file has wrong length ({} != 32)",
187 bytes.len()
188 ));
189 }
190 let mut seed = [0u8; 32];
191 seed.copy_from_slice(&bytes);
192 Ok(seed)
193}
194
195pub fn write_agent_card(card: &Value) -> Result<()> {
196 let path = agent_card_path()?;
197 let body = serde_json::to_vec_pretty(card)?;
198 fs::write(&path, body).with_context(|| format!("writing {path:?}"))?;
199 Ok(())
200}
201
202pub fn read_agent_card() -> Result<Value> {
203 let path = agent_card_path()?;
204 let body = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
205 Ok(serde_json::from_slice(&body)?)
206}
207
208pub fn write_trust(trust: &Value) -> Result<()> {
209 let path = trust_path()?;
210 let body = serde_json::to_vec_pretty(trust)?;
211 fs::write(&path, body).with_context(|| format!("writing {path:?}"))?;
212 Ok(())
213}
214
215pub fn read_trust() -> Result<Value> {
216 let path = trust_path()?;
217 if !path.exists() {
218 return Ok(crate::trust::empty_trust());
219 }
220 let body = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
221 Ok(serde_json::from_slice(&body)?)
222}
223
224pub fn relay_state_path() -> Result<PathBuf> {
229 Ok(config_dir()?.join("relay.json"))
230}
231
232pub fn read_relay_state() -> Result<Value> {
233 let path = relay_state_path()?;
234 if !path.exists() {
235 return Ok(serde_json::json!({"self": Value::Null, "peers": {}}));
236 }
237 let body = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
238 Ok(serde_json::from_slice(&body)?)
239}
240
241pub fn write_relay_state(state: &Value) -> Result<()> {
242 let path = relay_state_path()?;
243 let body = serde_json::to_vec_pretty(state)?;
244 fs::write(&path, body).with_context(|| format!("writing {path:?}"))?;
245 set_file_mode_0600(&path)?;
246 Ok(())
247}
248
249fn relay_state_lock_path() -> Result<PathBuf> {
254 Ok(config_dir()?.join("relay.lock"))
255}
256
257pub fn update_relay_state<F>(modifier: F) -> Result<()>
272where
273 F: FnOnce(&mut Value) -> Result<()>,
274{
275 use fs2::FileExt;
276 let lock_path = relay_state_lock_path()?;
277 if let Some(parent) = lock_path.parent() {
278 fs::create_dir_all(parent).with_context(|| format!("creating {parent:?}"))?;
279 }
280 let lock_file = fs::OpenOptions::new()
283 .create(true)
284 .read(true)
285 .write(true)
286 .open(&lock_path)
287 .with_context(|| format!("opening {lock_path:?}"))?;
288 lock_file
289 .lock_exclusive()
290 .with_context(|| format!("flock {lock_path:?}"))?;
291
292 let mut state = read_relay_state()?;
295 let result = modifier(&mut state);
296 let write_result = if result.is_ok() {
297 write_relay_state(&state)
298 } else {
299 Ok(())
300 };
301 let _ = fs2::FileExt::unlock(&lock_file);
304 result?;
305 write_result?;
306 Ok(())
307}
308
309#[cfg(test)]
314pub(crate) mod test_support {
315 use std::sync::Mutex;
316
317 pub static ENV_LOCK: Mutex<()> = Mutex::new(());
318
319 pub fn with_temp_home<F: FnOnce()>(f: F) {
320 let _guard = ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner());
322 let tmp = std::env::temp_dir().join(format!("wire-test-{}", rand::random::<u32>()));
323 unsafe { std::env::set_var("WIRE_HOME", &tmp) };
325 let _ = std::fs::remove_dir_all(&tmp);
326 let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
327 unsafe { std::env::remove_var("WIRE_HOME") };
328 let _ = std::fs::remove_dir_all(&tmp);
329 if let Err(e) = result {
330 std::panic::resume_unwind(e);
331 }
332 }
333}
334
335#[cfg(test)]
336mod tests {
337 use super::*;
338 use serde_json::json;
339
340 fn with_temp_home<F: FnOnce()>(f: F) {
341 super::test_support::with_temp_home(f)
342 }
343
344 #[test]
345 fn config_dir_honors_wire_home() {
346 with_temp_home(|| {
347 let dir = config_dir().unwrap();
348 assert!(dir.ends_with("wire"), "got {dir:?}");
349 assert!(dir.to_string_lossy().contains("wire-test-"));
350 });
351 }
352
353 #[test]
354 fn ensure_dirs_creates_layout() {
355 with_temp_home(|| {
356 ensure_dirs().unwrap();
357 assert!(config_dir().unwrap().is_dir());
358 assert!(state_dir().unwrap().is_dir());
359 assert!(inbox_dir().unwrap().is_dir());
360 assert!(outbox_dir().unwrap().is_dir());
361 });
362 }
363
364 #[test]
365 fn private_key_roundtrip() {
366 with_temp_home(|| {
367 ensure_dirs().unwrap();
368 let seed = [42u8; 32];
369 write_private_key(&seed).unwrap();
370 let read_back = read_private_key().unwrap();
371 assert_eq!(seed, read_back);
372 });
373 }
374
375 #[test]
376 fn agent_card_roundtrip() {
377 with_temp_home(|| {
378 ensure_dirs().unwrap();
379 let card = json!({"did": "did:wire:paul", "name": "Paul"});
380 write_agent_card(&card).unwrap();
381 let read_back = read_agent_card().unwrap();
382 assert_eq!(card, read_back);
383 });
384 }
385
386 #[test]
387 fn trust_returns_empty_when_missing() {
388 with_temp_home(|| {
389 ensure_dirs().unwrap();
390 let t = read_trust().unwrap();
391 assert_eq!(t["version"], 1);
392 assert!(t["agents"].is_object());
393 });
394 }
395
396 #[test]
397 fn update_relay_state_writes_through_lock() {
398 with_temp_home(|| {
404 ensure_dirs().unwrap();
405 let initial = json!({"self": null, "peers": {}});
407 write_relay_state(&initial).unwrap();
408 super::update_relay_state(|state| {
410 state["self"] = json!({
411 "relay_url": "https://test",
412 "slot_id": "abc",
413 "slot_token": "tok",
414 });
415 Ok(())
416 })
417 .unwrap();
418 let after = read_relay_state().unwrap();
420 assert_eq!(after["self"]["relay_url"], "https://test");
421 assert_eq!(after["self"]["slot_id"], "abc");
422 });
423 }
424
425 #[test]
426 fn update_relay_state_modifier_error_does_not_clobber() {
427 with_temp_home(|| {
431 ensure_dirs().unwrap();
432 let initial = json!({"self": {"relay_url": "https://prior"}, "peers": {}});
433 write_relay_state(&initial).unwrap();
434 let result = super::update_relay_state(|state| {
435 state["self"] = json!({"relay_url": "https://NEVER_PERSIST"});
437 anyhow::bail!("simulated mid-RMW error")
439 });
440 assert!(result.is_err());
441 let after = read_relay_state().unwrap();
442 assert_eq!(
443 after["self"]["relay_url"], "https://prior",
444 "state on disk must not reflect aborted modifier"
445 );
446 });
447 }
448
449 #[test]
450 fn is_initialized_true_only_after_both_files_written() {
451 with_temp_home(|| {
452 ensure_dirs().unwrap();
453 assert!(!is_initialized().unwrap());
454 write_private_key(&[0u8; 32]).unwrap();
455 assert!(!is_initialized().unwrap()); write_agent_card(&json!({"did": "did:wire:paul"})).unwrap();
457 assert!(is_initialized().unwrap());
458 });
459 }
460
461 #[cfg(unix)]
462 #[test]
463 fn append_outbox_record_normalizes_fqdn_to_bare_handle() {
464 with_temp_home(|| {
468 let path_fqdn = append_outbox_record(
469 "bob@wireup.net",
470 b"{\"kind\":1100}",
471 )
472 .unwrap();
473 let path_bare = append_outbox_record("bob", b"{\"kind\":1100}").unwrap();
474 assert_eq!(path_fqdn, path_bare, "FQDN form should normalize to bare");
476 assert!(
477 path_fqdn.file_name().unwrap().to_string_lossy() == "bob.jsonl",
478 "expected bob.jsonl, got {path_fqdn:?}"
479 );
480 let outbox = outbox_dir().unwrap();
482 assert!(
483 !outbox.join("bob@wireup.net.jsonl").exists(),
484 "FQDN-named file must not be created"
485 );
486 let body = std::fs::read_to_string(&path_bare).unwrap();
488 assert_eq!(body.matches("kind").count(), 2, "got: {body}");
489 });
490 }
491
492 #[test]
493 fn private_key_is_mode_0600() {
494 use std::os::unix::fs::PermissionsExt;
495 with_temp_home(|| {
496 ensure_dirs().unwrap();
497 write_private_key(&[1u8; 32]).unwrap();
498 let mode = fs::metadata(private_key_path().unwrap())
499 .unwrap()
500 .permissions()
501 .mode();
502 assert_eq!(mode & 0o777, 0o600, "got {:o}", mode & 0o777);
503 });
504 }
505}