pub(crate) mod ipc;
#[cfg(test)]
#[path = "windows/tests/process.rs"]
mod process;
mod security;
use std::{future::Future, io, path::Path, ptr};
use tokio::signal::windows::{ctrl_break, ctrl_c, ctrl_close};
use windows_sys::Win32::{
Foundation::{ERROR_ACCESS_DENIED, HANDLE, INVALID_HANDLE_VALUE},
System::{
Console::{
CONSOLE_MODE, ENABLE_ECHO_INPUT, GetConsoleMode, GetStdHandle, STD_INPUT_HANDLE,
SetConsoleMode,
},
SystemInformation::{ComputerNameDnsHostname, GetComputerNameExW},
},
};
pub(crate) use artifactize_foundation::platform::private::user_identity;
pub(crate) use artifactize_foundation::platform::process::{
Child, hide_console_window, kill_saved_group, kill_saved_session, spawn_detached, spawn_gated,
spawn_run, spawn_watched,
};
pub(crate) use security::private_pipe;
pub(crate) fn release_free_memory() {}
pub(crate) fn transient_file_access(error: &io::Error) -> bool {
error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32)
}
pub(crate) const ENV_NAMES_IGNORE_CASE: bool = true;
pub(crate) const HOME_VARIABLES: &[&str] = &["HOME", "USERPROFILE"];
pub(crate) const TEMP_VARIABLES: &[&str] = &["TMPDIR", "TMP", "TEMP"];
pub(crate) const STATE_VARIABLES: &[&str] = &["XDG_STATE_HOME", "LOCALAPPDATA"];
pub(crate) const USER_VARIABLES: &[&str] = &["USER", "USERNAME"];
pub(crate) fn home_directory() -> Option<std::path::PathBuf> {
HOME_VARIABLES
.iter()
.copied()
.find_map(|name| std::env::var_os(name).filter(|path| !path.is_empty()))
.map(Into::into)
}
pub(crate) fn path_from_bytes(bytes: &[u8]) -> std::path::PathBuf {
String::from_utf8_lossy(bytes).into_owned().into()
}
pub(crate) const CRLF_LINE_ENDINGS: bool = true;
pub const VISIBLE_INPUT_FALLBACK: bool = true;
pub(crate) fn host_name() -> Option<String> {
let mut size = 0;
unsafe { GetComputerNameExW(ComputerNameDnsHostname, ptr::null_mut(), &mut size) };
let mut buffer = vec![0_u16; size as usize];
if unsafe { GetComputerNameExW(ComputerNameDnsHostname, buffer.as_mut_ptr(), &mut size) } == 0 {
return None;
}
buffer.truncate(size as usize);
Some(String::from_utf16_lossy(&buffer))
}
pub(crate) fn resolve_system_aliases(path: &Path) -> io::Result<std::path::PathBuf> {
std::path::absolute(path)
}
pub fn stop_requested() -> io::Result<impl Future<Output = ()> + Send + 'static> {
let mut interrupt = ctrl_c()?;
let mut brk = ctrl_break()?;
let mut close = ctrl_close()?;
Ok(async move {
tokio::select! {
_ = interrupt.recv() => {},
_ = brk.recv() => {},
_ = close.recv() => {},
}
})
}
pub struct HiddenInput {
console: HANDLE,
mode: CONSOLE_MODE,
}
impl HiddenInput {
pub fn new() -> io::Result<Self> {
let console = unsafe { GetStdHandle(STD_INPUT_HANDLE) };
if console.is_null() || console == INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
}
let mut mode = 0;
if unsafe { GetConsoleMode(console, &mut mode) } == 0 {
return Err(io::Error::last_os_error());
}
if unsafe { SetConsoleMode(console, mode & !ENABLE_ECHO_INPUT) } == 0 {
return Err(io::Error::last_os_error());
}
Ok(Self { console, mode })
}
}
impl Drop for HiddenInput {
fn drop(&mut self) {
unsafe { SetConsoleMode(self.console, self.mode) };
}
}
#[cfg(test)]
mod tests;