8000 Minimald scaffolding, including sessions actor, session actor, session store + generics, and RPC tests by twitchyliquid64 · Pull Request #253 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ hex = "0.4"
indicatif = "0.18"
indoc = "^2.0"
jaq-all = "0.1.0"
libc = "0.2"
lzma-rs = "0.3"

# Revert back to crates.io dep once 0.18 is released.
Expand Down Expand Up @@ -98,7 +99,7 @@ tonic-prost-build = "0.14.6"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
url = "2" # keep in sync with reqwest
uuid = { version = "1.23", features = ["v4", "serde"] }
uuid = { version = "1.23", features = ["v4", "v7", "serde"] }
zstd = "0.13"

moka = { version = "0.12", default-features = false, features = ["sync"] }
Expand All @@ -125,5 +126,6 @@ orchestrator = { version = "0.0.1", path = "crates/orchestrator" }
remote-proto = { version = "0.0.1", path = "crates/remote-proto" }
remote-client = { version = "0.0.1", path = "crates/remote-client" }
sandbox2 = { version = "0.0.1", path = "crates/sandbox2" }
sessions = { version = "0.0.1", path = "crates/sessions" }

stdlib = { version = "0.0.15", path = "crates/stdlib" }
3 changes: 3 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ google-cloud-storage.workspace = true
resolv-conf.workspace = true

ot.workspace = true

[target.'cfg(target_os = "linux")'.dependencies]
libc.workspace = true
3 changes: 3 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub mod jq;
mod remote_storage;
pub use remote_storage::RemoteStorage;

#[cfg(target_os = "linux")]
pub mod renameat2;

pub mod repo_spec;
mod spec_hash;
pub use spec_hash::SpecHash;
Expand Down
113 changes: 113 additions & 0 deletions crates/common/src/renameat2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//! Direct wrapper around the Linux `renameat2(2)` syscall.
//!
//! Unlike `rename(2)`, `renameat2` accepts flags such as `RENAME_EXCHANGE`
//! that atomically swap two paths — useful for cache finalization where the
//! existing entry must be replaced without ever leaving the destination empty.

use std::ffi::CString;
use std::io;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;

#[cfg(target_arch = "x86_64")]
const SYS_RENAMEAT2: i64 = 316;

#[cfg(target_arch = "aarch64")]
8000 const SYS_RENAMEAT2: i64 = 276;

/// Special value used as a directory fd to indicate the current working directory.
pub const AT_FDCWD: i32 = -100;

/// Flag for `renameat2`: don't overwrite the target.
pub const RENAME_NOREPLACE: u32 = 1 << 0;
/// Flag for `renameat2`: atomically exchange the two paths.
pub const RENAME_EXCHANGE: u32 = 1 << 1;
/// Flag for `renameat2`: create a whiteout object at the source.
pub const RENAME_WHITEOUT: u32 = 1 << 2;

/// Invokes the `renameat2(2)` syscall directly.
pub fn renameat2<P: AsRef<Path>>(
olddirfd: i32,
oldpath: P,
newdirfd: i32,
newpath: P,
flags: u32,
) -> io::Result<()> {
let oldpath_c = path_to_cstring(oldpath.as_ref())?;
let newpath_c = path_to_cstring(newpath.as_ref())?;

// SAFETY: `oldpath_c` and `newpath_c` are owned `CString`s that remain live
// for the duration of the call, so their `.as_ptr()` values are valid,
// NUL-terminated C strings. `olddirfd` and `newdirfd` are either real file
// descriptors supplied by the caller or `AT_FDCWD`, both of which the
// kernel accepts. `flags` is a `u32` bitmask of the `RENAME_*` constants
// documented in `renameat2(2)`; unknown bits are rejected by the kernel
// with `EINVAL` rather than causing UB. The syscall returns `-1` on error
// (with errno set) or a non-negative value on success, which we inspect
// immediately below.
let ret = unsafe {
libc::syscall(
SYS_RENAMEAT2,
olddirfd,
oldpath_c.as_ptr(),
newdirfd,
newpath_c.as_ptr(),
flags,
)
};
Comment thread
twitchyliquid64 marked this conversation as resolved.
Resolved

if ret == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}

/// Convenience wrapper around [`renameat2`] for the common case where both
/// paths are resolved relative to the current working directory.
pub fn renameat2_cwd<P: AsRef<Path>>(oldpath: P, newpath: P, flags: u32) -> io::Result<()> {
renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, flags)
}

fn path_to_cstring(path: &Path) -> io::Result<CString> {
CString::new(path.as_os_str().as_bytes())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "path contained null byte"))
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;

#[test]
fn test_rename_noreplace() {
let tmp = TempDir::new().unwrap();
let old = tmp.path().join("old");
let new = tmp.path().join("new");

fs::write(&old, "source").unwrap();
fs::write(&new, "destination").unwrap();

let err = renameat2_cwd(&old, &new, RENAME_NOREPLACE).unwrap_err();
assert_eq!(err.raw_os_error(), Some(libc::EEXIST));

assert_eq!(fs::read_to_string(&new).unwrap(), "destination");
assert_eq!(fs::read_to_string(&old).unwrap(), "source");
}

#[test]
fn test_rename_exchange() {
let tmp = TempDir::new().unwrap();
let path1 = tmp.path().join("a");
let path2 = tmp.path().join("b");

fs::write(&path1, "content1").unwrap();
fs::write(&path2, "content2").unwrap();

renameat2_cwd(&path1, &path2, RENAME_EXCHANGE).unwrap();

assert_eq!(fs::read_to_string(&path1).unwrap(), "content2");
assert_eq!(fs::read_to_string(&path2).unwrap(), "content1");
}
}
4 changes: 1 addition & 3 deletions crates/lcache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ rust-version = "1.91"
license = "MIT OR Apache-2.0"
publish.workspace = true

[dev-dependencies]

[dependencies]
tracing.workspace = true
blake3.workspace = true
Expand All @@ -20,4 +18,4 @@ serde_json.workspace = true
common = { workspace = true }
graph = { workspace = true }

libc = "0.2"
libc.workspace = true
90 changes: 1 addition & 89 deletions crates/lcache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Implementations of the local cache: storing artifacts keyed by [SpecHash].

use common::SpecHash;
use common::renameat2::{AT_FDCWD, RENAME_EXCHANGE, renameat2};
Comment thread
twitchyliquid64 marked this conversation as resolved.
Resolved

use std::ffi::CString;
use std::io;
use std::os::unix::ffi::OsStrExt;

use std::fmt;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -409,100 +408,13 @@ impl<'a> graph::BinProvider for CacheBinProvider<'a> {
}
}

#[cfg(target_arch = "x86_64")]
const SYS_RENAMEAT2: i64 = 316;

#[cfg(target_arch = "aarch64")]
const SYS_RENAMEAT2: i64 = 276;

// Special value for AT_FDCWD
const AT_FDCWD: i32 = -100;

// Flags for renameat2
pub const RENAME_NOREPLACE: u32 = 1 << 0; // Don't overwrite target
pub const RENAME_EXCHANGE: u32 = 1 << 1; // Atomically exchange paths
pub const RENAME_WHITEOUT: u32 = 1 << 2; // Create whiteout object

pub fn renameat2<P: AsRef<Path>>(
olddirfd: i32,
oldpath: P,
newdirfd: i32,
newpath: P,
flags: u32,
) -> io::Result<()> {
let oldpath_c = path_to_cstring(oldpath.as_ref())?;
let newpath_c = path_to_cstring(newpath.as_ref())?;

let ret = unsafe {
libc::syscall(
SYS_RENAMEAT2,
olddirfd,
oldpath_c.as_ptr(),
newdirfd,
newpath_c.as_ptr(),
flags,
)
};

if ret == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}

// Helper for the common case of renaming with AT_FDCWD
pub fn renameat2_cwd<P: AsRef<Path>>(oldpath: P, newpath: P, flags: u32) -> io::Result<()> {
renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, flags)
}

fn path_to_cstring(path: &Path) -> io::Result<CString> {
CString::new(path.as_os_str().as_bytes())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "path contained null byte"))
}

#[cfg(test)]
mod tests {
use crate::entry_meta::MetaInner;

use super::*;
use std::fs;
use tempfile::TempDir;

#[test]
fn test_rename_noreplace() {
let old = "/tmp/test_old";
let new = "/tmp/test_new";

fs::write(old, "test").unwrap();

// Should succeed
renameat2_cwd(old, new, RENAME_NOREPLACE).unwrap();
assert!(Path::new(new).exists());

// Cleanup
fs::remove_file(new).unwrap();
}

#[test]
fn test_rename_exchange() {
let path1 = "/tmp/test_exchange1";
let path2 = "/tmp/test_exchange2";

fs::write(path1, "content1").unwrap();
fs::write(path2, "content2").unwrap();

// Exchange the files
renameat2_cwd(path1, path2, RENAME_EXCHANGE).unwrap();

assert_eq!(fs::read_to_string(path1).unwrap(), "content2");
assert_eq!(fs::read_to_string(path2).unwrap(), "content1");

// Cleanup
fs::remove_file(path1).unwrap();
fs::remove_file(path2).unwrap();
}

#[test]
fn smoketest_folder() {
let tmp_dir = TempDir::new().unwrap();
Expand Down
10 changes: 8 additions & 2 deletions crates/minimald/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ edition.workspace = true
publish.workspace = true

[dependencies]
camino.workspace = true
clap.workspace = true
clap_complete.workspace = true
constcat.workspace = true
dirs.workspace = true
futures.workspace = true

russh.workspace = true
serde.workspace = true
serde_json.workspace = true
uuid.workspace = true

futures.workspace = true
tokio.workspace = true
tokio-util.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true

ot.workspace = true
sessions.workspace = true
stdlib.workspace = true

[dev-dependencies]
tempfile.workspace = true
Loading
Loading
0