pub mod copy;
pub mod log_text;
pub mod size;
pub mod ttl_reverse_index;
pub mod wake_pipe;
pub const BASE_DIR_NAME: &str = ".microsandbox";
pub const LIB_SUBDIR: &str = "lib";
pub const BIN_SUBDIR: &str = "bin";
pub const DB_SUBDIR: &str = "db";
pub const CACHE_SUBDIR: &str = "cache";
pub const SANDBOXES_SUBDIR: &str = "sandboxes";
pub const VOLUMES_SUBDIR: &str = "volumes";
pub const SNAPSHOTS_SUBDIR: &str = "snapshots";
pub const LOGS_SUBDIR: &str = "logs";
pub const SECRETS_SUBDIR: &str = "secrets";
pub const TLS_SUBDIR: &str = "tls";
pub const SSH_SUBDIR: &str = "ssh";
pub const RUN_SUBDIR: &str = "run";
pub const METRICS_RUN_SUBDIR: &str = "metrics";
pub const METRICS_REGISTRY_NAME_FILENAME: &str = "registry-v1.name";
pub const METRICS_SHM_PREFIX: &str = "/msb-met";
pub const AGENTD_BINARY: &str = "agentd";
pub const MSB_BINARY: &str = "msb";
pub const PREBUILT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const LIBKRUNFW_VERSION: &str = "5.2.1";
pub const LIBKRUNFW_ABI: &str = "5";
pub const DB_FILENAME: &str = "msb.db";
pub const CONFIG_FILENAME: &str = "config.json";
pub const SANDBOXFILE_NAME: &str = "Sandboxfile";
pub const GITHUB_ORG: &str = "superradcompany";
pub const MICROSANDBOX_REPO: &str = "microsandbox";
pub fn stable_hash_path(path: &std::path::Path) -> String {
let bytes = path.as_os_str().as_encoded_bytes();
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
format!("{hash:016x}")
}
pub fn resolve_home() -> std::path::PathBuf {
if let Some(path) = std::env::var_os("MSB_HOME") {
return std::path::PathBuf::from(path);
}
dirs::home_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
.join(BASE_DIR_NAME)
}
pub fn libkrunfw_filename(os: &str) -> String {
if os == "macos" {
format!("libkrunfw.{LIBKRUNFW_ABI}.dylib")
} else {
format!("libkrunfw.so.{LIBKRUNFW_VERSION}")
}
}
pub fn libkrunfw_download_url(version: &str, arch: &str, os: &str) -> String {
let (target_os, ext) = if os == "macos" {
("darwin", "dylib")
} else {
("linux", "so")
};
format!(
"https://github.com/{GITHUB_ORG}/{MICROSANDBOX_REPO}/releases/download/v{version}/libkrunfw-{target_os}-{arch}.{ext}"
)
}
pub fn agentd_download_url(version: &str, arch: &str) -> String {
format!(
"https://github.com/{GITHUB_ORG}/{MICROSANDBOX_REPO}/releases/download/v{version}/{AGENTD_BINARY}-{arch}"
)
}
pub fn bundle_download_url(version: &str, arch: &str, os: &str) -> String {
let target_os = if os == "macos" { "darwin" } else { "linux" };
format!(
"https://github.com/{GITHUB_ORG}/{MICROSANDBOX_REPO}/releases/download/v{version}/{MICROSANDBOX_REPO}-{target_os}-{arch}.tar.gz"
)
}
#[cfg(feature = "http-client")]
pub fn http_client() -> ureq::Agent {
ureq::Agent::config_builder()
.tls_config(
ureq::tls::TlsConfig::builder()
.root_certs(ureq::tls::RootCerts::PlatformVerifier)
.build(),
)
.build()
.new_agent()
}
pub fn looks_like_local_path_text(s: &str) -> bool {
s == "." || s == ".." || s.starts_with('/') || s.starts_with("./") || s.starts_with("../")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resolve_home_respects_env_override() {
let custom = std::path::PathBuf::from("/tmp/msb-home-resolve-test-12345");
unsafe { std::env::set_var("MSB_HOME", &custom) };
let resolved = resolve_home();
unsafe { std::env::remove_var("MSB_HOME") };
assert_eq!(resolved, custom);
}
}