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 artifactize_foundation::platform::private::{
FileIdentity, PRIVATE_DIRECTORY, PRIVATE_FILE, PrivateTempDir, create_private_dir,
create_private_dir_all, file_identity, is_owner_only, is_private_dir, is_private_file,
private_named_dir, private_options, private_tempdir_in, remove_tree, restrict_file, sync_dir,
};
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, TEMP_VARIABLES, USER_VARIABLES,
hide_console_window, home_directory, host_name, ipc, kill_saved_group, kill_saved_session,
path_from_bytes, release_free_memory, resolve_system_aliases, spawn_gated,
transient_file_access,
};
#[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();
}
}
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 use artifactize_foundation::platform::files::open_regular;
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)
}
#[cfg(test)]
#[path = "platform/tests/process.rs"]
mod process_tests;
#[cfg(test)]
pub(crate) use process_tests::pipe_grace_armed;
#[cfg(test)]
#[path = "platform/tests/program.rs"]
mod program_tests;
#[cfg(test)]
#[path = "platform/tests/private_directory.rs"]
mod private_directory_tests;
#[cfg(test)]
#[path = "platform/tests/state_directory.rs"]
mod state_directory_tests;
mod console;
pub use console::pause_after_help;
mod interaction;
pub(crate) use interaction::private_anonymous_file;
pub use interaction::{is_within, paths_equal};
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),
}
}
#[cfg(test)]
#[path = "platform/tests/stop.rs"]
mod stop_tests;