Skip to main content

microsandbox_utils/
lib.rs

1//! Shared constants and utilities for the microsandbox project.
2
3pub mod size;
4pub mod ttl_reverse_index;
5pub mod wake_pipe;
6
7//--------------------------------------------------------------------------------------------------
8// Constants: Directory Layout
9//--------------------------------------------------------------------------------------------------
10
11/// Name of the microsandbox home directory (relative to user's home).
12pub const BASE_DIR_NAME: &str = ".microsandbox";
13
14/// Subdirectory for shared libraries (libkrunfw).
15pub const LIB_SUBDIR: &str = "lib";
16
17/// Subdirectory for helper binaries.
18pub const BIN_SUBDIR: &str = "bin";
19
20/// Subdirectory for the database.
21pub const DB_SUBDIR: &str = "db";
22
23/// Subdirectory for OCI layer cache.
24pub const CACHE_SUBDIR: &str = "cache";
25
26/// Subdirectory for per-sandbox state.
27pub const SANDBOXES_SUBDIR: &str = "sandboxes";
28
29/// Subdirectory for named volumes.
30pub const VOLUMES_SUBDIR: &str = "volumes";
31
32/// Subdirectory for logs.
33pub const LOGS_SUBDIR: &str = "logs";
34
35/// Subdirectory for secrets.
36pub const SECRETS_SUBDIR: &str = "secrets";
37
38/// Subdirectory for TLS certificates.
39pub const TLS_SUBDIR: &str = "tls";
40
41/// Subdirectory for SSH keys.
42pub const SSH_SUBDIR: &str = "ssh";
43
44//--------------------------------------------------------------------------------------------------
45// Constants: Binary Names
46//--------------------------------------------------------------------------------------------------
47
48/// Guest agent binary name.
49pub const AGENTD_BINARY: &str = "agentd";
50
51/// CLI binary name.
52pub const MSB_BINARY: &str = "msb";
53
54//--------------------------------------------------------------------------------------------------
55// Constants: Versions
56//--------------------------------------------------------------------------------------------------
57
58/// Version for downloading prebuilt release artifacts.
59///
60/// This tracks the published crate/package version so the SDK and the
61/// downloaded runtime bundle stay aligned.
62pub const PREBUILT_VERSION: &str = env!("CARGO_PKG_VERSION");
63
64/// libkrunfw release version. Keep in sync with justfile.
65pub const LIBKRUNFW_VERSION: &str = "5.2.1";
66
67/// libkrunfw ABI version (soname major). Keep in sync with justfile.
68pub const LIBKRUNFW_ABI: &str = "5";
69
70//--------------------------------------------------------------------------------------------------
71// Constants: Filenames
72//--------------------------------------------------------------------------------------------------
73
74/// Database filename.
75pub const DB_FILENAME: &str = "msb.db";
76
77/// SQLite PRAGMAs applied to every connection for concurrent-access safety.
78///
79/// - WAL mode prevents `SQLITE_BUSY` when multiple processes access the DB
80/// - 5-second busy timeout retries on transient lock contention
81/// - Foreign key enforcement is off by default in SQLite
82pub const SQLITE_PRAGMAS: &str =
83    "PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000; PRAGMA foreign_keys = ON;";
84
85/// Global configuration filename.
86pub const CONFIG_FILENAME: &str = "config.json";
87
88/// Project-local sandbox configuration filename.
89pub const SANDBOXFILE_NAME: &str = "Sandboxfile";
90
91//--------------------------------------------------------------------------------------------------
92// Constants: GitHub
93//--------------------------------------------------------------------------------------------------
94
95/// GitHub organization.
96pub const GITHUB_ORG: &str = "superradcompany";
97
98/// Main repository name.
99pub const MICROSANDBOX_REPO: &str = "microsandbox";
100
101//--------------------------------------------------------------------------------------------------
102// Functions
103//--------------------------------------------------------------------------------------------------
104
105/// Returns the platform-specific libkrunfw filename.
106pub fn libkrunfw_filename(os: &str) -> String {
107    if os == "macos" {
108        format!("libkrunfw.{LIBKRUNFW_ABI}.dylib")
109    } else {
110        format!("libkrunfw.so.{LIBKRUNFW_VERSION}")
111    }
112}
113
114/// Returns the GitHub release download URL for libkrunfw.
115pub fn libkrunfw_download_url(version: &str, arch: &str, os: &str) -> String {
116    let (target_os, ext) = if os == "macos" {
117        ("darwin", "dylib")
118    } else {
119        ("linux", "so")
120    };
121
122    format!(
123        "https://github.com/{GITHUB_ORG}/{MICROSANDBOX_REPO}/releases/download/v{version}/libkrunfw-{target_os}-{arch}.{ext}"
124    )
125}
126
127/// Returns the GitHub release download URL for the agentd binary.
128pub fn agentd_download_url(version: &str, arch: &str) -> String {
129    format!(
130        "https://github.com/{GITHUB_ORG}/{MICROSANDBOX_REPO}/releases/download/v{version}/{AGENTD_BINARY}-{arch}"
131    )
132}
133
134/// Returns the GitHub release download URL for the microsandbox bundle tarball.
135pub fn bundle_download_url(version: &str, arch: &str, os: &str) -> String {
136    let target_os = if os == "macos" { "darwin" } else { "linux" };
137    format!(
138        "https://github.com/{GITHUB_ORG}/{MICROSANDBOX_REPO}/releases/download/v{version}/{MICROSANDBOX_REPO}-{target_os}-{arch}.tar.gz"
139    )
140}