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
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."))?;

View File

@@ -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<()> {

View File

@@ -13,8 +13,7 @@ pub struct LocalDb {
impl LocalDb {
pub fn open() -> Result<Self> {
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")?;