use std::path::Path;
use std::ptr::null_mut;
use anyhow::{Context, Result, bail};
use windows_sys::Win32::Foundation::{ERROR_FILE_NOT_FOUND, ERROR_SUCCESS};
use windows_sys::Win32::System::Registry::{
HKEY_CURRENT_USER, REG_EXPAND_SZ, RRF_NOEXPAND, RRF_RT_REG_EXPAND_SZ, RRF_RT_REG_SZ, RegGetValueW,
RegSetKeyValueW,
};
use windows_sys::Win32::UI::WindowsAndMessaging::{
HWND_BROADCAST, SMTO_ABORTIFHUNG, SendMessageTimeoutW, WM_SETTINGCHANGE,
};
pub fn ensure(dir: &Path) -> Result<bool> {
let dir = dir.to_str().context("the folder name is not valid Unicode")?;
let Some(updated) = with_entry(&read()?, dir) else {
return Ok(false);
};
write(&updated)?;
announce();
Ok(true)
}
fn with_entry(paths: &str, dir: &str) -> Option<String> {
let trim = |s: &str| s.trim().trim_end_matches(['\\', '/']).to_ascii_lowercase();
if paths.split(';').any(|entry| trim(entry) == trim(dir)) {
return None;
}
let kept = paths.trim_end_matches(';');
Some(if kept.is_empty() {
dir.to_string()
} else {
format!("{kept};{dir}")
})
}
fn wide(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
}
fn read() -> Result<String> {
let (key, name) = (wide("Environment"), wide("Path"));
let flags = RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ | RRF_NOEXPAND;
let mut bytes = 0u32;
let status = unsafe {
RegGetValueW(
HKEY_CURRENT_USER,
key.as_ptr(),
name.as_ptr(),
flags,
null_mut(),
null_mut(),
&mut bytes,
)
};
if status == ERROR_FILE_NOT_FOUND {
return Ok(String::new());
}
if status != ERROR_SUCCESS {
bail!("could not read the user PATH (Windows error {status})");
}
let mut buffer = vec![0u16; bytes as usize / 2 + 1];
let mut bytes = (buffer.len() * 2) as u32;
let status = unsafe {
RegGetValueW(
HKEY_CURRENT_USER,
key.as_ptr(),
name.as_ptr(),
flags,
null_mut(),
buffer.as_mut_ptr().cast(),
&mut bytes,
)
};
if status != ERROR_SUCCESS {
bail!("could not read the user PATH (Windows error {status})");
}
let len = buffer.iter().position(|&unit| unit == 0).unwrap_or(buffer.len());
String::from_utf16(&buffer[..len]).context("the user PATH is not valid Unicode")
}
fn write(value: &str) -> Result<()> {
let (key, name, data) = (wide("Environment"), wide("Path"), wide(value));
let status = unsafe {
RegSetKeyValueW(
HKEY_CURRENT_USER,
key.as_ptr(),
name.as_ptr(),
REG_EXPAND_SZ,
data.as_ptr().cast(),
(data.len() * 2) as u32,
)
};
if status != ERROR_SUCCESS {
bail!("could not change the user PATH (Windows error {status})");
}
Ok(())
}
fn announce() {
let area = wide("Environment");
unsafe {
SendMessageTimeoutW(
HWND_BROADCAST,
WM_SETTINGCHANGE,
0,
area.as_ptr() as isize,
SMTO_ABORTIFHUNG,
1000,
null_mut(),
);
}
}
#[cfg(test)]
mod tests {
use super::with_entry;
#[test]
fn adds_a_missing_entry_at_the_end() {
assert_eq!(with_entry("", r"C:\sne").as_deref(), Some(r"C:\sne"));
assert_eq!(
with_entry(r"C:\a;C:\b", r"C:\sne").as_deref(),
Some(r"C:\a;C:\b;C:\sne")
);
assert_eq!(with_entry(r"C:\a;", r"C:\sne").as_deref(), Some(r"C:\a;C:\sne"));
}
#[test]
fn leaves_a_path_that_already_has_the_entry() {
assert_eq!(with_entry(r"C:\a;C:\sne;C:\b", r"C:\sne"), None);
assert_eq!(with_entry(r"C:\a;c:\SNE\", r"C:\sne"), None);
}
}