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