platform: a leading ~ names the Windows user profile when HOME is unset

This commit is contained in:
DeFiDude committed 2026-11-15 09:52:49 -07:00
1 parent c4df19970d
commit b2b30181e2
7 files changed
+49 -35

No files matched your search

+29
View File
@@ -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<PathBuf> {
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<OsString>, userprofile: Option<OsString>) -> Option<PathBuf> {
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");
+5 -3
View File
@@ -3443,10 +3443,12 @@ fn parse_hash16_list(key: &str, list: Option<Vec<String>>) -> Result<Vec<[u8; 16
}
fn expand_home_path(path: &str) -> 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)
}
+1 -21
View File
@@ -285,12 +285,7 @@ fn abspath(path: &str, cwd: &str) -> String {
}
fn process_home() -> Option<String> {
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<String>, userprofile: Option<String>) -> Option<String> {
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]
+6 -5
View File
@@ -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<[u8;
fn rncp_allowed_identity_file_candidates() -> Vec<PathBuf> {
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"));
}
+2 -2
View File
@@ -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()
+5 -3
View File
@@ -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(),
}
+1 -1
View File
@@ -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());