From 722441c3914e978a4d36c146d9dbea7f9b098dfe Mon Sep 17 00:00:00 2001 From: Siavash Sameni Date: Thu, 26 Mar 2026 22:27:49 +0400 Subject: [PATCH] Add WARZONE_HOME env var for separate user data directories All data paths now use keystore::data_dir() which checks WARZONE_HOME first, falls back to ~/.warzone. This avoids the HOME override hack that breaks rustup/cargo. Usage: WARZONE_HOME=/tmp/bob warzone init Co-Authored-By: Claude Opus 4.6 (1M context) --- warzone/crates/warzone-client/src/cli/init.rs | 10 ++-------- warzone/crates/warzone-client/src/keystore.rs | 14 ++++++++++++-- warzone/crates/warzone-client/src/storage.rs | 3 +-- warzone/test | 6 ++++++ 4 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 warzone/test diff --git a/warzone/crates/warzone-client/src/cli/init.rs b/warzone/crates/warzone-client/src/cli/init.rs index 8612941..a47dc51 100644 --- a/warzone/crates/warzone-client/src/cli/init.rs +++ b/warzone/crates/warzone-client/src/cli/init.rs @@ -58,10 +58,7 @@ pub fn run() -> Result<()> { // Store bundle locally for later registration let bundle_bytes = bincode::serialize(&bundle)?; - let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); - let bundle_path = std::path::Path::new(&home) - .join(".warzone") - .join("bundle.bin"); + let bundle_path = crate::keystore::data_dir().join("bundle.bin"); std::fs::write(&bundle_path, &bundle_bytes)?; println!("\nTo register with a server, run:"); @@ -81,10 +78,7 @@ pub async fn register_with_server(server_url: &str) -> Result<()> { let pub_id = identity.public_identity(); let fp = pub_id.fingerprint.to_string(); - let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); - let bundle_path = std::path::Path::new(&home) - .join(".warzone") - .join("bundle.bin"); + let bundle_path = crate::keystore::data_dir().join("bundle.bin"); let bundle_bytes = std::fs::read(&bundle_path) .map_err(|_| anyhow::anyhow!("No bundle found. Run `warzone init` first."))?; diff --git a/warzone/crates/warzone-client/src/keystore.rs b/warzone/crates/warzone-client/src/keystore.rs index 29f7bf6..399628b 100644 --- a/warzone/crates/warzone-client/src/keystore.rs +++ b/warzone/crates/warzone-client/src/keystore.rs @@ -6,9 +6,19 @@ use std::path::PathBuf; use warzone_protocol::identity::Seed; +/// Get the warzone data directory. Respects WARZONE_HOME env var, +/// falls back to ~/.warzone. +pub fn data_dir() -> PathBuf { + if let Ok(wz) = std::env::var("WARZONE_HOME") { + PathBuf::from(wz) + } else { + let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); + PathBuf::from(home).join(".warzone") + } +} + fn seed_path() -> PathBuf { - let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); - PathBuf::from(home).join(".warzone").join("identity.seed") + data_dir().join("identity.seed") } pub fn save_seed(seed: &Seed) -> anyhow::Result<()> { diff --git a/warzone/crates/warzone-client/src/storage.rs b/warzone/crates/warzone-client/src/storage.rs index ed4d526..0aa8f95 100644 --- a/warzone/crates/warzone-client/src/storage.rs +++ b/warzone/crates/warzone-client/src/storage.rs @@ -13,8 +13,7 @@ pub struct LocalDb { impl LocalDb { pub fn open() -> Result { - let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); - let path = std::path::Path::new(&home).join(".warzone").join("db"); + let path = crate::keystore::data_dir().join("db"); let db = sled::open(&path).context("failed to open local database")?; let sessions = db.open_tree("sessions")?; let pre_keys = db.open_tree("pre_keys")?; diff --git a/warzone/test b/warzone/test new file mode 100644 index 0000000..6f1c9a6 --- /dev/null +++ b/warzone/test @@ -0,0 +1,6 @@ + 1. glimpse 2. into 3. same 4. buffalo + 5. wise 6. catalog 7. gift 8. ginger + 9. profit 10. nut 11. palace 12. document +13. display 14. seat 15. lizard 16. they +17. medal 18. market 19. oak 20. occur +21. gospel 22. ahead 23. before 24. execute