pub(crate) mod desktop_app;
pub use desktop_app::launch as launch_desktop;
pub mod desktop_commands;
mod detached;
pub mod environment;
mod executable;
pub use executable::current_executable;
mod hidden_input;
pub(crate) mod install;
pub(crate) mod net;
pub(crate) mod path_serde;
pub(crate) mod paths;
pub(crate) mod program;
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
use unix as os;
#[cfg(windows)]
use windows as os;
pub(crate) use detached::{DetachedChild, spawn_detached, spawn_run, spawn_watched};
pub use hidden_input::{SecretInput, secret_input, stdin_is_terminal};
pub use os::HiddenInput;
pub use os::stop_requested;
pub(crate) use os::{
CRLF_LINE_ENDINGS, Child, ENV_NAMES_IGNORE_CASE, PrivateTempDir, TEMP_VARIABLES,
USER_VARIABLES, create_private_dir, create_private_dir_all, file_identity, hide_console_window,
home_directory, host_name, ipc, is_owner_only, is_private_dir, is_private_file,
kill_saved_group, kill_saved_session, path_from_bytes, private_named_dir, private_options,
private_tempdir_in, process_start_time, release_free_memory, remove_tree,
resolve_system_aliases, restrict_executable_file, restrict_file, running_process_start_time,
spawn_gated, spawn_now, spawn_windowless, sync_dir, transient_file_access, venv_bin_dir,
};
pub(crate) use os::{PRIVATE_DIRECTORY, PRIVATE_FILE};
#[derive(Debug, Clone)]
pub struct Footprint {
_release: std::sync::Arc<Release>,
}
impl Footprint {
pub(crate) fn new() -> Self {
Self {
_release: std::sync::Arc::new(Release),
}
}
}
#[derive(Debug)]
struct Release;
impl Drop for Release {
fn drop(&mut self) {
release_free_memory();
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct FileIdentity {
volume: u64,
index: u64,
}
pub use artifactize_tools::launch::{ProcessEnd, ProcessOutcome};
pub(crate) const FILE_LOCK_RETRY_INTERVAL: std::time::Duration =
std::time::Duration::from_millis(25);
pub(crate) use artifactize_tools::files::{
EntryName, FileKind, canonicalize, entry_kind, link_target, open_directory, open_entry,
open_no_follow, read_dir,
};
pub use artifactize_tools::files::{current_directory, open_nonblocking, path_text};
pub fn file_uri_path(uri: &str) -> Option<std::path::PathBuf> {
url::Url::parse(uri)
.ok()?
.to_file_path()
.ok()
.filter(|path| path.is_absolute())
}
pub fn absolute_folder(text: &str) -> Option<std::path::PathBuf> {
artifactize_tools::files::path_from_text(text)
.ok()
.filter(|path| path.is_absolute())
}
pub fn is_directory(path: &std::path::Path) -> bool {
path.is_dir()
}
pub fn target_exists(path: &std::path::Path) -> std::io::Result<bool> {
paths::target_exists(path)
}
pub fn is_regular_file(path: &std::path::Path) -> bool {
path.is_file()
}
pub fn open_regular_input(path: &std::path::Path) -> std::io::Result<Option<std::fs::File>> {
let file = open_nonblocking(path)?;
Ok(file.metadata()?.is_file().then_some(file))
}
pub fn open_regular(path: &std::path::Path) -> std::io::Result<std::fs::File> {
let file = open_no_follow(std::fs::OpenOptions::new().read(true), path)?;
if file.metadata()?.is_file() {
Ok(file)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"not a regular file",
))
}
}
pub(crate) fn local_day(instant: time::OffsetDateTime) -> time::Date {
let offset = chrono::DateTime::from_timestamp(instant.unix_timestamp(), instant.nanosecond())
.map(|utc| utc.with_timezone(&chrono::Local).offset().local_minus_utc())
.and_then(|seconds| time::UtcOffset::from_whole_seconds(seconds).ok())
.unwrap_or(time::UtcOffset::UTC);
instant.to_offset(offset).date()
}
pub(crate) fn clean_program_text(bytes: &[u8]) -> String {
String::from_utf8(crate::runtime::clean_output(bytes)).expect("clean output is UTF-8")
}
pub fn output_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> {
String::from_utf8_lossy(bytes)
}
pub fn output_lines(bytes: &[u8]) -> String {
output_text(bytes).replace("\r\n", "\n")
}
pub fn os_text(value: &std::ffi::OsStr) -> Option<&str> {
value.to_str()
}
pub(crate) fn display_name(value: &std::ffi::OsStr) -> String {
value.to_string_lossy().into_owned()
}
pub(crate) fn output_line(bytes: &[u8]) -> &[u8] {
bytes
.strip_suffix(b"\r\n")
.or_else(|| bytes.strip_suffix(b"\n"))
.unwrap_or(bytes)
}
pub(crate) fn path_from_line(bytes: &[u8]) -> std::path::PathBuf {
let path = if CRLF_LINE_ENDINGS {
bytes
.strip_suffix(b"\r\n")
.or_else(|| bytes.strip_suffix(b"\n"))
.unwrap_or(bytes)
} else {
bytes.strip_suffix(b"\n").unwrap_or(bytes)
};
path_from_bytes(path)
}
pub(crate) fn temporary_directory() -> std::path::PathBuf {
std::env::temp_dir()
}
const STATE_HOME_OVERRIDE: &str = "ARTIFACTIZE_STATE_HOME";
pub(crate) fn state_home_variables() -> impl Iterator<Item = &'static str> {
std::iter::once(STATE_HOME_OVERRIDE)
.chain(os::STATE_VARIABLES.iter().copied())
.chain(os::HOME_VARIABLES.iter().copied())
}
pub(crate) fn state_directory() -> Option<std::path::PathBuf> {
resolve_state_directory(
environment::var(STATE_HOME_OVERRIDE).map(Into::into),
os::STATE_VARIABLES
.iter()
.find_map(|name| environment::var(name).filter(|v| !v.is_empty()))
.map(Into::into),
home_directory(),
)
}
fn resolve_state_directory(
artifactize: Option<std::path::PathBuf>,
state: Option<std::path::PathBuf>,
home: Option<std::path::PathBuf>,
) -> Option<std::path::PathBuf> {
artifactize
.filter(|p| !p.as_os_str().is_empty())
.or_else(|| {
state
.filter(|p| !p.as_os_str().is_empty())
.map(|p| p.join("artifactize"))
})
.or_else(|| {
home.filter(|p| !p.as_os_str().is_empty())
.map(|p| p.join(".local/state/artifactize"))
})
}
pub(crate) fn user_name() -> Option<String> {
USER_VARIABLES
.iter()
.find_map(|name| environment::var_text(name).filter(|value| !value.is_empty()))
}
pub(crate) fn fingerprint_line_ending(stdout: &[u8]) -> (&[u8], &'static str) {
if CRLF_LINE_ENDINGS {
(
stdout
.strip_suffix(b"\r\n")
.or_else(|| stdout.strip_suffix(b"\n"))
.unwrap_or(stdout),
"LF or CRLF",
)
} else {
(stdout.strip_suffix(b"\n").unwrap_or(stdout), "LF")
}
}
pub(crate) fn path_kind(path: &std::path::Path) -> std::io::Result<FileKind> {
match (path.parent(), path.file_name()) {
(Some(parent), Some(name)) => entry_kind(&open_directory(parent)?, name),
_ => open_directory(path).map(|_| FileKind::Directory),
}
}
pub(crate) fn program_path(command: &str) -> std::io::Result<std::path::PathBuf> {
#[cfg(windows)]
if command.starts_with(r"\\?\") {
return Ok(command.into());
}
artifactize_tools::files::path_from_text(command)
}
pub(crate) fn program_text(command: &str) -> String {
program_path(command).map_or_else(|_| command.to_owned(), |path| path_text(&path))
}
pub(crate) fn serialize_program<S: serde::Serializer>(
command: &str,
serializer: S,
) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&program_text(command))
}
pub(crate) fn serialize_paths<S: serde::Serializer>(
paths: &[std::path::PathBuf],
serializer: S,
) -> Result<S::Ok, S::Error> {
use serde::Serialize;
paths
.iter()
.map(path_text)
.collect::<Vec<_>>()
.serialize(serializer)
}
pub(crate) fn normalize_external_path(raw: &str) -> String {
let trimmed = raw.trim();
if trimmed.is_empty() {
return String::new();
}
path_text(std::path::Path::new(trimmed)).replace('\\', "/")
}
pub(crate) fn registry_platform() -> &'static str {
match (std::env::consts::OS, std::env::consts::ARCH) {
("macos", "aarch64") => "darwin-aarch64",
("macos", "x86_64") => "darwin-x86_64",
("linux", "aarch64") => "linux-aarch64",
("linux", "x86_64") => "linux-x86_64",
("windows", "aarch64") => "windows-aarch64",
("windows", "x86_64") => "windows-x86_64",
_ => "unknown",
}
}
pub(crate) fn install_executable(
target_dir: &std::path::Path,
name: &str,
bytes: &[u8],
) -> std::io::Result<std::path::PathBuf> {
use std::io::Write;
let mut file = tempfile::NamedTempFile::new_in(target_dir)?;
restrict_executable_file(file.as_file())?;
file.write_all(bytes)?;
file.flush()?;
file.as_file().sync_all()?;
let final_path = target_dir.join(name);
paths::replace_open_file(file, &final_path)?;
sync_dir(target_dir)?;
Ok(final_path)
}
#[cfg(test)]
mod process_tests;
#[cfg(test)]
pub(crate) use process_tests::pipe_grace_armed;
#[cfg(test)]
mod program_tests {
#[test]
fn a_declared_program_is_written_with_forward_slashes() {
assert_eq!(super::program_text("pnpm"), "pnpm");
assert_eq!(super::program_text("tools/run.sh"), "tools/run.sh");
#[cfg(windows)]
assert_eq!(super::program_text(r"tools\run.cmd"), "tools/run.cmd");
}
}
#[cfg(test)]
mod private_directory_tests {
use super::*;
#[test]
fn kept_temporary_directory_transfers_cleanup_responsibility() {
let parent = crate::test_os::tempdir();
let directory = private_tempdir_in("kept-", parent.path()).unwrap();
let path = directory.keep();
assert!(path.is_dir());
assert!(is_private_dir(&path).unwrap());
remove_tree(&path).unwrap();
assert!(!path.exists());
}
}
#[cfg(test)]
mod state_directory_tests {
use super::*;
use std::path::PathBuf;
#[test]
fn state_home_precedence_and_empty_values() {
let artifactize = PathBuf::from("custom/state");
let state = PathBuf::from("state");
let home = PathBuf::from("reviewer");
assert_eq!(
resolve_state_directory(
Some(artifactize.clone()),
Some(state.clone()),
Some(home.clone())
),
Some(artifactize)
);
assert_eq!(
resolve_state_directory(None, Some(state.clone()), Some(home.clone())),
Some(state.join("artifactize"))
);
assert_eq!(
resolve_state_directory(None, None, Some(home.clone())),
Some(home.join(".local/state/artifactize"))
);
let empty = Some(PathBuf::new());
assert_eq!(
resolve_state_directory(empty.clone(), empty.clone(), Some(home.clone())),
Some(home.join(".local/state/artifactize"))
);
assert_eq!(
resolve_state_directory(empty.clone(), empty.clone(), empty),
None
);
assert_eq!(resolve_state_directory(None, None, None), None);
}
}
mod console;
pub use console::pause_after_help;
mod interaction;
pub use interaction::paths_equal;
pub(crate) use interaction::{is_within, label, private_anonymous_file};
pub(crate) fn entry_exists(path: &std::path::Path) -> std::io::Result<bool> {
let Some(parent) = path.parent() else {
return Ok(false);
};
let directory = match open_directory(parent) {
Ok(directory) => directory,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
Err(error) => return Err(error),
};
let Some(name) = path.file_name() else {
return Ok(false);
};
match entry_kind(&directory, name) {
Ok(_) => Ok(true),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error),
}
}
pub(crate) fn marker_metadata(path: &std::path::Path) -> std::io::Result<std::fs::Metadata> {
path.symlink_metadata()
}
#[cfg(test)]
pub(crate) fn successful_outcome() -> ProcessOutcome {
ProcessOutcome::from_native(std::process::ExitStatus::default())
}
#[cfg(test)]
mod stop_tests {
use super::*;
use std::{path::PathBuf, time::Duration};
const READY: &str = "ARTIFACTIZE_TEST_STOP_PROBE_READY";
const WATCHDOG: Duration = Duration::from_secs(15);
#[tokio::test]
async fn a_terminal_interrupt_completes_stop_requested() {
let root = crate::test_os::tempdir();
let ready = root.path().join("ready");
let mut child = crate::test_os::test_command(
"platform::stop_tests::terminal_interrupt_probe",
&[(READY, ready.clone().into_os_string())],
);
crate::test_os::new_group(&mut child);
let mut child = crate::test_os::spawn_test(&mut tokio::process::Command::from(child));
crate::test_os::wait_until(WATCHDOG, || ready.is_file());
crate::test_os::interrupt(child.id());
assert!(child.wait().await.success());
}
#[tokio::test]
async fn terminal_interrupt_probe() {
let Some(ready) = crate::test_os::environment_variable(READY) else {
return;
};
let stop = stop_requested().unwrap();
std::fs::write(PathBuf::from(ready), "subscribed").unwrap();
stop.await;
}
}