diff --git a/crates/rns-runtime/src/platform.rs b/crates/rns-runtime/src/platform.rs index b703a58..2be4173 100644 --- a/crates/rns-runtime/src/platform.rs +++ b/crates/rns-runtime/src/platform.rs @@ -1,3 +1,4 @@ +use std::ffi::OsString; use std::path::{Path, PathBuf}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -94,6 +95,24 @@ pub fn resolve_config_dir(configdir: Option<&str>) -> PathBuf { } } +/// The directory a leading `~` names: `HOME`, or on Windows `USERPROFILE` +/// when `HOME` is unset or empty. +pub fn home_dir() -> Option { + let home = std::env::var_os("HOME"); + if cfg!(windows) { + windows_home(home, std::env::var_os("USERPROFILE")) + } else { + home.map(PathBuf::from) + } +} + +fn windows_home(home: Option, userprofile: Option) -> Option { + home.into_iter() + .chain(userprofile) + .find(|dir| !dir.is_empty()) + .map(PathBuf::from) +} + pub struct StoragePaths { pub config_dir: PathBuf, pub interface_dir: PathBuf, @@ -164,6 +183,16 @@ mod tests { ); } + #[test] + fn windows_home_falls_back_to_the_user_profile() { + let profile = Some(OsString::from(r"C:\Users\me")); + let home = |home: Option<&str>| windows_home(home.map(OsString::from), profile.clone()); + assert_eq!(home(None), Some(PathBuf::from(r"C:\Users\me"))); + assert_eq!(home(Some("")), Some(PathBuf::from(r"C:\Users\me"))); + assert_eq!(home(Some(r"H:\")), Some(PathBuf::from(r"H:\"))); + assert_eq!(windows_home(None, None), None); + } + #[test] fn test_storage_paths() { let config_dir = PathBuf::from("/tmp/test_config"); diff --git a/crates/rns-runtime/src/reticulum.rs b/crates/rns-runtime/src/reticulum.rs index 09bc266..e1c176b 100644 --- a/crates/rns-runtime/src/reticulum.rs +++ b/crates/rns-runtime/src/reticulum.rs @@ -3443,10 +3443,12 @@ fn parse_hash16_list(key: &str, list: Option>) -> Result PathBuf { - if (path == "~" || path.starts_with("~/")) - && let Ok(home) = std::env::var("HOME") + if let Some(rest) = path.strip_prefix('~') + && (rest.is_empty() || rest.starts_with(std::path::is_separator)) + && let Some(home) = crate::platform::home_dir() + && let Ok(home) = home.into_os_string().into_string() { - return PathBuf::from(format!("{home}{}", &path[1..])); + return PathBuf::from(format!("{home}{rest}")); } PathBuf::from(path) } diff --git a/crates/rns-runtime/src/rncp.rs b/crates/rns-runtime/src/rncp.rs index 22417e5..b205231 100644 --- a/crates/rns-runtime/src/rncp.rs +++ b/crates/rns-runtime/src/rncp.rs @@ -285,12 +285,7 @@ fn abspath(path: &str, cwd: &str) -> String { } fn process_home() -> Option { - let home = std::env::var("HOME").ok(); - if cfg!(windows) { - ntpath::home(home, std::env::var("USERPROFILE").ok()) - } else { - home - } + crate::platform::home_dir().and_then(|home| home.into_os_string().into_string().ok()) } fn process_cwd() -> String { @@ -490,13 +485,6 @@ mod ntpath { } } - /// `HOME`, else `USERPROFILE`, where Windows keeps it. - pub(super) fn home(home: Option, userprofile: Option) -> Option { - home.into_iter() - .chain(userprofile) - .find(|dir| !dir.is_empty()) - } - /// `ntpath.basename`. pub(super) fn basename(path: &str) -> &str { let (_, _, tail) = splitroot(path); @@ -2461,14 +2449,6 @@ mod tests { assert_eq!(ntpath::expanduser("~/files", home), r"C:\Users\me/files"); assert_eq!(ntpath::expanduser(r"~other\files", home), r"~other\files"); assert_eq!(ntpath::expanduser(r"~\files", None), r"~\files"); - let profile = Some(r"C:\Users\me".to_string()); - assert_eq!(ntpath::home(None, profile.clone()), profile); - assert_eq!(ntpath::home(Some(String::new()), profile.clone()), profile); - assert_eq!( - ntpath::home(Some(r"H:\".into()), profile), - Some(r"H:\".into()) - ); - assert_eq!(ntpath::home(None, None), None); } #[test] diff --git a/crates/rns-tools/src/commands/rncp.rs b/crates/rns-tools/src/commands/rncp.rs index 00a472f..a08f93d 100644 --- a/crates/rns-tools/src/commands/rncp.rs +++ b/crates/rns-tools/src/commands/rncp.rs @@ -28,10 +28,12 @@ use rns_identity::destination::{DestType, Destination, Direction}; use rns_transport::messages::{OutboundRequest, TransportMessage}; fn expand_tilde(p: &str) -> PathBuf { - if let Some(rest) = p.strip_prefix("~/") - && let Some(home) = std::env::var_os("HOME") + if let Some(rest) = p + .strip_prefix('~') + .and_then(|rest| rest.strip_prefix(std::path::is_separator)) + && let Some(home) = rns_runtime::platform::home_dir() { - return PathBuf::from(home).join(rest); + return home.join(rest); } PathBuf::from(p) } @@ -1176,8 +1178,7 @@ fn allowed_identities(cli: &[String], candidates: &[PathBuf]) -> Result Vec { let mut paths = vec![PathBuf::from("/etc/rncp/allowed_identities")]; - if let Some(home) = std::env::var_os("HOME") { - let home = PathBuf::from(home); + if let Some(home) = rns_runtime::platform::home_dir() { paths.push(home.join(".config/rncp/allowed_identities")); paths.push(home.join(".rncp/allowed_identities")); } diff --git a/crates/rns-tools/src/commands/rnid/main.rs b/crates/rns-tools/src/commands/rnid/main.rs index e6acc85..8ab7afa 100644 --- a/crates/rns-tools/src/commands/rnid/main.rs +++ b/crates/rns-tools/src/commands/rnid/main.rs @@ -493,12 +493,12 @@ fn expanduser(path: &str) -> String { let Some(rest) = path.strip_prefix('~') else { return path.to_string(); }; - let (user, tail) = match rest.find('/') { + let (user, tail) = match rest.find(std::path::is_separator) { Some(i) => (&rest[..i], &rest[i..]), None => (rest, ""), }; let home = if user.is_empty() { - std::env::var("HOME").ok() + rns_runtime::platform::home_dir().and_then(|home| home.into_os_string().into_string().ok()) } else { std::fs::read_to_string("/etc/passwd") .ok() diff --git a/crates/rns-tools/src/commands/rnstatus.rs b/crates/rns-tools/src/commands/rnstatus.rs index b518149..67aed04 100644 --- a/crates/rns-tools/src/commands/rnstatus.rs +++ b/crates/rns-tools/src/commands/rnstatus.rs @@ -563,9 +563,11 @@ fn remote_inputs(remote: &str, identity: Option<&Path>) -> Result<([u8; 16], Ide /// Python `os.path.expanduser` for a leading `~`. fn expand_user(path: &Path) -> PathBuf { let text = path.to_string_lossy(); - match (text.strip_prefix('~'), std::env::var_os("HOME")) { - (Some(rest), Some(home)) if rest.is_empty() || rest.starts_with('/') => { - PathBuf::from(format!("{}{rest}", PathBuf::from(home).display())) + match (text.strip_prefix('~'), rns_runtime::platform::home_dir()) { + (Some(rest), Some(home)) + if rest.is_empty() || rest.starts_with(std::path::is_separator) => + { + PathBuf::from(format!("{}{rest}", home.display())) } _ => path.to_path_buf(), } diff --git a/crates/rns-tools/src/commands/rnx.rs b/crates/rns-tools/src/commands/rnx.rs index 28c6daa..d428fb0 100644 --- a/crates/rns-tools/src/commands/rnx.rs +++ b/crates/rns-tools/src/commands/rnx.rs @@ -225,7 +225,7 @@ async fn listen(args: &Args, interrupt: &ShutdownSignal) -> ! { } } } - let home = std::env::var_os("HOME").map(PathBuf::from); + let home = rns_runtime::platform::home_dir(); let file = rns_tools::rnx::allowed_identity_files(home.as_deref()) .into_iter() .find(|f| f.is_file());