Skip to main content

agentmux_srv/
system_drive.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `SystemDrive` for a Windows srv started with a bare environment (a headless
5//! srv under a service manager, `env -i`). The shell's icon and thumbnail caches
6//! live in `%SystemDrive%\ProgramData\Microsoft\Windows\Caches`, and the shell
7//! expands that name from the process environment, not from the registry. With
8//! the variable missing, the text stays as it is and the shell makes a folder
9//! literally called `%SystemDrive%` in the current directory (#2524). srv sets
10//! the variable itself, from the Windows folder, so every child it starts
11//! (the file-icon helper first) inherits a real drive.
12
13/// The drive of a Windows folder path, as `C:`: a letter, a colon, nothing else.
14/// `None` for a path that doesn't start that way (a UNC path, an empty string).
15pub fn drive_of(path: &str) -> Option<String> {
16    let mut chars = path.chars();
17    let letter = chars.next().filter(char::is_ascii_alphabetic)?;
18    if chars.next() != Some(':') {
19        return None;
20    }
21    Some(format!("{}:", letter.to_ascii_uppercase()))
22}
23
24/// Give this process a `SystemDrive` when it has none. Call it before any
25/// thread or child exists. A value that is set, whatever it holds, is left alone.
26pub fn ensure() {
27    if std::env::var_os("SystemDrive").is_some() {
28        return;
29    }
30    if let Some(drive) = system_drive() {
31        std::env::set_var("SystemDrive", drive);
32    }
33}
34
35/// The drive Windows is installed on, from the Windows API (never from the
36/// environment, which is what is missing).
37#[cfg(windows)]
38pub fn system_drive() -> Option<String> {
39    use windows_sys::Win32::System::SystemInformation::GetSystemWindowsDirectoryW;
40    let mut buf = [0u16; 260];
41    // SAFETY: `buf` is valid for `buf.len()` u16s; the call writes at most that many.
42    let len = unsafe { GetSystemWindowsDirectoryW(buf.as_mut_ptr(), buf.len() as u32) } as usize;
43    if len == 0 || len > buf.len() {
44        return None;
45    }
46    drive_of(&String::from_utf16_lossy(&buf[..len]))
47}
48
49#[cfg(not(windows))]
50pub fn system_drive() -> Option<String> {
51    None
52}
53
54#[cfg(test)]
55mod tests {
56    use super::drive_of;
57
58    #[test]
59    fn a_windows_folder_gives_its_drive_in_capitals() {
60        assert_eq!(drive_of(r"C:\Windows").as_deref(), Some("C:"));
61        assert_eq!(drive_of(r"d:\WINNT").as_deref(), Some("D:"));
62        assert_eq!(drive_of("E:").as_deref(), Some("E:"));
63    }
64
65    #[test]
66    fn a_path_without_a_drive_gives_none() {
67        for p in ["", r"\\server\share\Windows", "Windows", "1:\\x", "C", "%SystemDrive%"] {
68            assert_eq!(drive_of(p), None, "{p:?}");
69        }
70    }
71
72    #[cfg(windows)]
73    #[test]
74    fn the_windows_api_names_a_real_drive() {
75        let drive = super::system_drive().expect("the Windows folder has a drive");
76        assert_eq!(drive.len(), 2);
77        assert!(std::path::Path::new(&format!("{drive}\\")).exists(), "{drive} is not a drive here");
78    }
79}