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) <noreply@anthropic.com>
This commit is contained in:
Siavash Sameni
2026-03-26 22:27:49 +04:00
parent 94b845eb5b
commit 722441c391
4 changed files with 21 additions and 12 deletions

View File

@@ -58,10 +58,7 @@ pub fn run() -> Result<()> {
// Store bundle locally for later registration // Store bundle locally for later registration
let bundle_bytes = bincode::serialize(&bundle)?; let bundle_bytes = bincode::serialize(&bundle)?;
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); let bundle_path = crate::keystore::data_dir().join("bundle.bin");
let bundle_path = std::path::Path::new(&home)
.join(".warzone")
.join("bundle.bin");
std::fs::write(&bundle_path, &bundle_bytes)?; std::fs::write(&bundle_path, &bundle_bytes)?;
println!("\nTo register with a server, run:"); 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 pub_id = identity.public_identity();
let fp = pub_id.fingerprint.to_string(); let fp = pub_id.fingerprint.to_string();
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); let bundle_path = crate::keystore::data_dir().join("bundle.bin");
let bundle_path = std::path::Path::new(&home)
.join(".warzone")
.join("bundle.bin");
let bundle_bytes = std::fs::read(&bundle_path) let bundle_bytes = std::fs::read(&bundle_path)
.map_err(|_| anyhow::anyhow!("No bundle found. Run `warzone init` first."))?; .map_err(|_| anyhow::anyhow!("No bundle found. Run `warzone init` first."))?;

View File

@@ -6,9 +6,19 @@ use std::path::PathBuf;
use warzone_protocol::identity::Seed; 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 { fn seed_path() -> PathBuf {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); data_dir().join("identity.seed")
PathBuf::from(home).join(".warzone").join("identity.seed")
} }
pub fn save_seed(seed: &Seed) -> anyhow::Result<()> { pub fn save_seed(seed: &Seed) -> anyhow::Result<()> {

View File

@@ -13,8 +13,7 @@ pub struct LocalDb {
impl LocalDb { impl LocalDb {
pub fn open() -> Result<Self> { pub fn open() -> Result<Self> {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); let path = crate::keystore::data_dir().join("db");
let path = std::path::Path::new(&home).join(".warzone").join("db");
let db = sled::open(&path).context("failed to open local database")?; let db = sled::open(&path).context("failed to open local database")?;
let sessions = db.open_tree("sessions")?; let sessions = db.open_tree("sessions")?;
let pre_keys = db.open_tree("pre_keys")?; let pre_keys = db.open_tree("pre_keys")?;

6
warzone/test Normal file
View File

@@ -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