artifactize-core 0.27.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
//! Windows owner-only DACLs, Job Objects, and console modes and events.
//! Pinned file access lives in artifactize-tools.

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;

/// The Windows heap returns freed memory by itself.
pub(crate) fn release_free_memory() {}

/// Whether `error` is Windows transiently refusing a metadata or open probe on a file that
/// another handle is still finalizing: emptying or deleting a WAL `-wal`/`-shm` sidecar, for
/// one, leaves the name `ERROR_ACCESS_DENIED` until that handle closes, even though no one
/// holds an incompatible lock. The condition clears on its own, so callers retry briefly
/// rather than surface it; any other error (a real permission problem, a missing file) is not
/// this and must not be retried as if it were.
pub(crate) fn transient_file_access(error: &io::Error) -> bool {
    error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32)
}

/// Windows compares environment variable names without regard to case.
pub(crate) const ENV_NAMES_IGNORE_CASE: bool = true;

/// The variable naming the user's home directory, which Unix-style tools read on Windows too.
pub(crate) const HOME_VARIABLES: &[&str] = &["HOME", "USERPROFILE"];

/// Variables programs read their temporary directory from.
pub(crate) const TEMP_VARIABLES: &[&str] = &["TMPDIR", "TMP", "TEMP"];

/// Variables naming a per-user state directory, in order; artifactize keeps its state in an
/// `artifactize` folder below the first one set, and otherwise below `HOME`.
pub(crate) const STATE_VARIABLES: &[&str] = &["XDG_STATE_HOME", "LOCALAPPDATA"];

/// The variables naming the signed-in user, in order; Windows sets `USERNAME`, not `USER`.
pub(crate) const USER_VARIABLES: &[&str] = &["USER", "USERNAME"];

/// The user's home directory: `HOME` when set, as Unix-style tools set it, else the
/// profile folder.
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)
}

/// A path from the bytes a tool such as git prints, which are UTF-8 on Windows.
pub(crate) fn path_from_bytes(bytes: &[u8]) -> std::path::PathBuf {
    String::from_utf8_lossy(bytes).into_owned().into()
}

/// Windows programs end a line with CRLF, as Python's print does there.
pub(crate) const CRLF_LINE_ENDINGS: bool = true;

/// Whether a terminal that cannot hide input is still read, visibly, after a warning:
/// mintty (Git Bash) hands Windows programs a pipe that only looks like a terminal, with no
/// console echo to turn off.
pub const VISIBLE_INPUT_FALLBACK: bool = true;

pub(crate) fn host_name() -> Option<String> {
    let mut size = 0;
    // SAFETY: a null buffer only asks for the size, including the terminating NUL.
    unsafe { GetComputerNameExW(ComputerNameDnsHostname, ptr::null_mut(), &mut size) };
    let mut buffer = vec![0_u16; size as usize];
    // SAFETY: the buffer holds `size` UTF-16 units.
    if unsafe { GetComputerNameExW(ComputerNameDnsHostname, buffer.as_mut_ptr(), &mut size) } == 0 {
        return None;
    }
    buffer.truncate(size as usize);
    Some(String::from_utf16_lossy(&buffer))
}

/// The absolute form of `path`; Windows has no aliased system roots of the macOS kind, and
/// 8.3 names are resolved by canonicalization.
pub(crate) fn resolve_system_aliases(path: &Path) -> io::Result<std::path::PathBuf> {
    std::path::absolute(path)
}

/// Register for Ctrl-C, Ctrl-Break and console close now; the future completes at the first.
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() => {},
        }
    })
}

/// Turns console echo off until dropped, so a pasted secret is not shown.
pub struct HiddenInput {
    console: HANDLE,
    mode: CONSOLE_MODE,
}

impl HiddenInput {
    pub fn new() -> io::Result<Self> {
        // SAFETY: GetStdHandle only reads the process's standard handle table.
        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;
        // SAFETY: a standard handle and a valid out pointer.
        if unsafe { GetConsoleMode(console, &mut mode) } == 0 {
            return Err(io::Error::last_os_error());
        }
        // SAFETY: the same console input handle.
        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) {
        // SAFETY: restores the mode read in `new` on the same handle.
        unsafe { SetConsoleMode(self.console, self.mode) };
    }
}

#[cfg(test)]
mod tests;