use std::path::{Path, PathBuf};
use cuno_core::Env;
pub fn temp_dir(env: &Env) -> Option<PathBuf> {
["TMP", "TEMP", "USERPROFILE"]
.iter()
.find_map(|name| env.get(name))
.map(PathBuf::from)
}
#[cfg(windows)]
#[allow(unsafe_code)]
pub fn ascii_short_path(path: &Path) -> Option<PathBuf> {
use std::ffi::OsString;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use windows_sys::Win32::Storage::FileSystem::GetShortPathNameW;
let wide: Vec<u16> = path.as_os_str().encode_wide().chain([0]).collect();
let needed = unsafe { GetShortPathNameW(wide.as_ptr(), std::ptr::null_mut(), 0) };
if needed == 0 {
return None;
}
let mut buffer = vec![0u16; needed as usize];
let written = unsafe { GetShortPathNameW(wide.as_ptr(), buffer.as_mut_ptr(), needed) };
if written == 0 || written >= needed {
return None;
}
buffer.truncate(written as usize);
let short = PathBuf::from(OsString::from_wide(&buffer));
short.to_str().is_some_and(str::is_ascii).then_some(short)
}
#[cfg(windows)]
#[allow(unsafe_code)]
pub fn fits_ansi_code_page(path: &Path) -> bool {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Globalization::{CP_ACP, WC_NO_BEST_FIT_CHARS, WideCharToMultiByte};
let wide: Vec<u16> = path.as_os_str().encode_wide().collect();
let Ok(len) = i32::try_from(wide.len()) else {
return false;
};
if len == 0 {
return true;
}
let mut defaulted = 0;
let written = unsafe {
WideCharToMultiByte(
CP_ACP,
WC_NO_BEST_FIT_CHARS,
wide.as_ptr(),
len,
std::ptr::null_mut(),
0,
std::ptr::null(),
&mut defaulted,
)
};
written > 0 && defaulted == 0
}
#[cfg(not(windows))]
pub fn fits_ansi_code_page(_path: &Path) -> bool {
true
}
#[cfg(not(windows))]
pub fn ascii_short_path(_path: &Path) -> Option<PathBuf> {
None
}
#[cfg(test)]
mod tests {
use super::*;
use cuno_core::NameCase;
#[test]
fn the_temporary_directory_is_tmp_then_temp_then_userprofile() {
let env = |pairs: &[(&str, &str)]| {
Env::from_pairs(
pairs.iter().map(|(n, v)| (n.into(), v.into())),
NameCase::Insensitive,
)
};
let all = env(&[
("USERPROFILE", "C:/u"),
("TEMP", "C:/temp"),
("TMP", "C:/tmp"),
]);
assert_eq!(temp_dir(&all), Some(PathBuf::from("C:/tmp")));
let no_tmp = env(&[("USERPROFILE", "C:/u"), ("TEMP", "C:/temp")]);
assert_eq!(temp_dir(&no_tmp), Some(PathBuf::from("C:/temp")));
let profile = env(&[("USERPROFILE", "C:/u")]);
assert_eq!(temp_dir(&profile), Some(PathBuf::from("C:/u")));
assert_eq!(temp_dir(&env(&[])), None);
}
#[cfg(windows)]
#[test]
fn ascii_fits_the_code_page_and_a_rare_ideograph_does_not() {
assert!(fits_ansi_code_page(Path::new("C:/Users/runner/Temp")));
assert!(!fits_ansi_code_page(Path::new("C:/Users/\u{20B9F}")));
}
#[cfg(windows)]
#[test]
fn a_japanese_directory_has_an_ascii_short_name() {
let root = tempfile::tempdir().unwrap();
let dir = root.path().join("δΈζ");
std::fs::create_dir(&dir).unwrap();
assert_eq!(ascii_short_path(&root.path().join("η‘γ")), None);
let Some(short) = ascii_short_path(&dir) else {
return;
};
assert!(short.to_str().unwrap().is_ascii(), "{}", short.display());
assert_ne!(short, dir);
assert!(short.is_dir());
}
}