use std::collections::BTreeMap;
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use lean_ctx_protocol::cloud::gate::{LockedChange, under_locked};
use sha2::{Digest as _, Sha256};
use super::GateError;
pub(super) const CARGO_CONFIGS: [&str; 2] = [".cargo/config", ".cargo/config.toml"];
#[cfg(test)]
pub(super) fn exe(name: &str) -> String {
format!("{name}{}", std::env::consts::EXE_SUFFIX)
}
#[cfg(not(windows))]
pub(super) fn system_dirs() -> Vec<PathBuf> {
["/usr/local/bin", "/opt/homebrew/bin", "/usr/bin", "/bin"]
.iter()
.map(PathBuf::from)
.collect()
}
#[cfg(windows)]
pub(super) fn system_dirs() -> Vec<PathBuf> {
let mut dirs = Vec::new();
if let Some(windows) = win::windows_dir() {
let system32 = windows.join("System32");
dirs.extend([
system32.clone(),
windows,
system32.join("Wbem"),
system32.join("WindowsPowerShell").join("v1.0"),
]);
}
if let Some(pf) = win::program_files() {
dirs.push(pf.join("Git").join("cmd"));
}
dirs
}
pub(super) fn system_path() -> std::ffi::OsString {
std::env::join_paths(system_dirs()).unwrap_or_default()
}
pub(super) fn shell() -> Result<PathBuf, GateError> {
#[cfg(windows)]
{
let sh = win::program_files()
.map(|pf| pf.join("Git").join("bin").join("sh.exe"))
.filter(|sh| sh.is_file())
.ok_or_else(|| {
GateError::Check(
"the gate runs its commands in Git for Windows' sh (Program Files\\Git\\bin\\sh.exe), which is missing".into(),
)
})?;
Ok(sh)
}
#[cfg(not(windows))]
{
Ok(PathBuf::from("/bin/sh"))
}
}
#[cfg(windows)]
mod win {
use std::ffi::{OsString, c_void};
use std::os::windows::ffi::OsStringExt as _;
use std::path::PathBuf;
#[repr(C)]
struct Guid {
data1: u32,
data2: u16,
data3: u16,
data4: [u8; 8],
}
const FOLDERID_PROGRAM_FILES: Guid = Guid {
data1: 0x905E_63B6,
data2: 0xC1BF,
data3: 0x494E,
data4: [0xB2, 0x9C, 0x65, 0xB7, 0x32, 0xD3, 0xD2, 0x1A],
};
const FOLDERID_PROGRAM_DATA: Guid = Guid {
data1: 0x62AB_5D82,
data2: 0xFDC1,
data3: 0x4DC3,
data4: [0xA9, 0xDD, 0x07, 0x0D, 0x1D, 0x49, 0x5D, 0x97],
};
#[repr(C)]
#[derive(Default)]
struct BasicLimits {
per_process_user_time_limit: i64,
per_job_user_time_limit: i64,
limit_flags: u32,
minimum_working_set_size: usize,
maximum_working_set_size: usize,
active_process_limit: u32,
affinity: usize,
priority_class: u32,
scheduling_class: u32,
}
#[repr(C)]
#[derive(Default)]
struct IoCounters {
counts: [u64; 6],
}
#[repr(C)]
#[derive(Default)]
struct ExtendedLimits {
basic: BasicLimits,
io: IoCounters,
process_memory_limit: usize,
job_memory_limit: usize,
peak_process_memory_used: usize,
peak_job_memory_used: usize,
}
const JOB_OBJECT_EXTENDED_LIMIT_INFORMATION: u32 = 9;
const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: u32 = 0x2000;
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetSystemWindowsDirectoryW(buffer: *mut u16, size: u32) -> u32;
fn CreateJobObjectW(attributes: *mut c_void, name: *const u16) -> *mut c_void;
fn SetInformationJobObject(
job: *mut c_void,
class: u32,
info: *const c_void,
length: u32,
) -> i32;
fn AssignProcessToJobObject(job: *mut c_void, process: *mut c_void) -> i32;
fn TerminateJobObject(job: *mut c_void, exit_code: u32) -> i32;
fn CloseHandle(handle: *mut c_void) -> i32;
}
#[link(name = "ntdll")]
unsafe extern "system" {
fn NtResumeProcess(process: *mut c_void) -> i32;
}
pub(super) const CREATE_SUSPENDED: u32 = 0x0000_0004;
#[link(name = "shell32")]
unsafe extern "system" {
fn SHGetKnownFolderPath(
id: *const Guid,
flags: u32,
token: *mut c_void,
path: *mut *mut u16,
) -> i32;
}
#[link(name = "ole32")]
unsafe extern "system" {
fn CoTaskMemFree(memory: *mut c_void);
}
pub(super) fn windows_dir() -> Option<PathBuf> {
let mut buf = vec![0u16; 1024];
let len = u32::try_from(buf.len()).ok()?;
let n = unsafe { GetSystemWindowsDirectoryW(buf.as_mut_ptr(), len) };
let n = usize::try_from(n).ok()?;
(n > 0 && n < buf.len()).then(|| PathBuf::from(OsString::from_wide(&buf[..n])))
}
pub(super) fn program_files() -> Option<PathBuf> {
known_folder(&FOLDERID_PROGRAM_FILES)
}
pub(super) fn program_data() -> Option<PathBuf> {
known_folder(&FOLDERID_PROGRAM_DATA)
}
fn known_folder(id: &Guid) -> Option<PathBuf> {
let mut raw: *mut u16 = std::ptr::null_mut();
let hr = unsafe {
SHGetKnownFolderPath(
std::ptr::from_ref(id),
0,
std::ptr::null_mut(),
&raw mut raw,
)
};
let path = (hr == 0 && !raw.is_null()).then(|| {
let mut len = 0usize;
unsafe {
while *raw.add(len) != 0 {
len += 1;
}
PathBuf::from(OsString::from_wide(std::slice::from_raw_parts(raw, len)))
}
});
unsafe { CoTaskMemFree(raw.cast()) };
path
}
pub(super) struct Job(*mut c_void);
impl Job {
pub(super) fn new() -> Option<Self> {
let handle = unsafe { CreateJobObjectW(std::ptr::null_mut(), std::ptr::null()) };
if handle.is_null() {
return None;
}
let job = Self(handle);
let mut limits = ExtendedLimits::default();
limits.basic.limit_flags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
let size = u32::try_from(std::mem::size_of::<ExtendedLimits>()).ok()?;
let ok = unsafe {
SetInformationJobObject(
job.0,
JOB_OBJECT_EXTENDED_LIMIT_INFORMATION,
(&raw const limits).cast(),
size,
)
};
(ok != 0).then_some(job)
}
pub(super) fn assign_and_resume(&self, child: &std::process::Child) -> bool {
use std::os::windows::io::AsRawHandle as _;
let process = child.as_raw_handle().cast();
if unsafe { AssignProcessToJobObject(self.0, process) } == 0 {
return false;
}
unsafe { NtResumeProcess(process) == 0 }
}
pub(super) fn terminate(&self) {
unsafe {
TerminateJobObject(self.0, 1);
}
}
}
impl Drop for Job {
fn drop(&mut self) {
unsafe {
CloseHandle(self.0);
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GateDirs {
pub work: PathBuf,
pub runs: PathBuf,
}
impl GateDirs {
#[must_use]
pub fn for_instance(data_dir: &Path) -> Self {
let work = data_dir.join("g6");
Self {
work,
runs: default_runs(),
}
}
}
fn default_runs() -> PathBuf {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt as _;
let uid = unsafe { libc::getuid() };
let run = PathBuf::from(format!("/run/user/{uid}"));
if std::fs::symlink_metadata(&run).is_ok_and(|m| m.is_dir() && m.uid() == uid) {
return run;
}
}
#[cfg(windows)]
{
dirs::data_local_dir().map_or_else(std::env::temp_dir, |local| local.join("Temp"))
}
#[cfg(not(windows))]
{
std::fs::canonicalize("/tmp").unwrap_or_else(|_| PathBuf::from("/tmp"))
}
}
pub(super) fn resolved(path: &Path) -> std::io::Result<PathBuf> {
std::fs::canonicalize(path).map(lean_ctx::core::pathutil::strip_verbatim)
}
#[cfg(unix)]
fn private_mode(dir: &Path) -> Result<(), GateError> {
use std::os::unix::fs::{MetadataExt as _, PermissionsExt as _};
let meta = std::fs::symlink_metadata(dir)
.map_err(|e| GateError::Check(format!("{}: {e}", dir.display())))?;
let uid = unsafe { libc::getuid() };
if meta.file_type().is_symlink() || !meta.is_dir() || meta.uid() != uid {
return Err(GateError::Check(format!(
"{} is no directory of this user; the gate refuses to work in it",
dir.display()
)));
}
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))
.map_err(|e| GateError::Check(format!("{}: {e}", dir.display())))
}
#[cfg(not(unix))]
fn private_mode(_dir: &Path) -> Result<(), GateError> {
Ok(())
}
pub(super) fn make_private_dir(dir: &Path) -> Result<(), GateError> {
std::fs::create_dir_all(dir)
.map_err(|e| GateError::Check(format!("the gate's directory {}: {e}", dir.display())))?;
private_mode(dir)
}
pub(super) fn private_dir(work: &Path) -> Result<tempfile::TempDir, GateError> {
make_private_dir(work)?;
let work = resolved(work).map_err(|e| GateError::Check(format!("the gate's work dir: {e}")))?;
tempfile::Builder::new()
.prefix("g6-")
.tempdir_in(&work)
.map_err(|e| GateError::Check(format!("a fresh directory: {e}")))
}
pub(super) fn cargo_run_dir(runs: &Path) -> Result<tempfile::TempDir, GateError> {
match std::fs::symlink_metadata(runs) {
Ok(meta) if meta.is_dir() => {}
Ok(_) => {
return Err(GateError::Check(format!(
"{} is no directory; the gate refuses to run in it",
runs.display()
)));
}
Err(_) => make_private_dir(runs)?,
}
let canonical =
resolved(runs).map_err(|e| GateError::Check(format!("the gate's run directory: {e}")))?;
for dir in canonical.ancestors() {
for name in CARGO_CONFIGS {
let file = dir.join(Path::new(name).components().collect::<PathBuf>());
if std::fs::symlink_metadata(&file).is_ok() {
return Err(GateError::Check(format!(
"{} lies above the gate's run directory, and cargo would read it in the rerun; the gate refuses to run under it",
file.display()
)));
}
}
}
tempfile::Builder::new()
.prefix("lean-ctx-g6-run-")
.tempdir_in(&canonical)
.map_err(|e| GateError::Check(format!("a fresh directory: {e}")))
}
#[cfg(unix)]
pub(super) fn user_home() -> Result<PathBuf, GateError> {
use std::ffi::{CStr, OsStr};
use std::os::unix::ffi::OsStrExt as _;
let mut buf = vec![0 as libc::c_char; 16 * 1024];
let mut pwd: libc::passwd = unsafe { std::mem::zeroed() };
let mut found: *mut libc::passwd = std::ptr::null_mut();
let rc = unsafe {
libc::getpwuid_r(
libc::getuid(),
&raw mut pwd,
buf.as_mut_ptr(),
buf.len(),
&raw mut found,
)
};
if rc != 0 || found.is_null() || pwd.pw_dir.is_null() {
return Err(GateError::Check(
"this user has no passwd entry, so the gate cannot tell its home".into(),
));
}
let dir = unsafe { CStr::from_ptr(pwd.pw_dir) };
let home = PathBuf::from(OsStr::from_bytes(dir.to_bytes()));
if !home.is_absolute() {
return Err(GateError::Check(
"this user's passwd home is not absolute".into(),
));
}
Ok(home)
}
#[cfg(not(unix))]
pub(super) fn user_home() -> Result<PathBuf, GateError> {
dirs::home_dir()
.filter(|home| home.is_absolute())
.ok_or_else(|| GateError::Check("this user has no profile directory".into()))
}
pub(super) fn toolchain_dirs(
home: &Path,
cargo_home: Option<&std::ffi::OsStr>,
rustup_home: Option<&std::ffi::OsStr>,
) -> Result<(PathBuf, PathBuf), GateError> {
let fixed = (home.join(".cargo"), home.join(".rustup"));
let same = |given: &Path, fixed: &Path| {
given == fixed
|| std::fs::canonicalize(given)
.is_ok_and(|g| std::fs::canonicalize(fixed).ok().as_ref() == Some(&g))
};
for (name, given, fixed) in [
("CARGO_HOME", cargo_home, &fixed.0),
("RUSTUP_HOME", rustup_home, &fixed.1),
] {
if let Some(given) = given.filter(|g| !g.is_empty())
&& !same(Path::new(given), fixed)
{
return Err(GateError::Check(format!(
"{name} is {}, not {}; the gate runs only rustup's toolchain of this user's home, so it refuses the run",
Path::new(given).display(),
fixed.display()
)));
}
}
Ok(fixed)
}
#[cfg(not(test))]
fn run_toolchain(home: &Path) -> Result<(PathBuf, PathBuf), GateError> {
toolchain_dirs(
home,
std::env::var_os("CARGO_HOME").as_deref(),
std::env::var_os("RUSTUP_HOME").as_deref(),
)
}
#[cfg(test)]
fn run_toolchain(home: &Path) -> Result<(PathBuf, PathBuf), GateError> {
if home.join(".cargo").join("bin").join(exe("cargo")).is_file() {
return toolchain_dirs(
home,
std::env::var_os("CARGO_HOME").as_deref(),
std::env::var_os("RUSTUP_HOME").as_deref(),
);
}
let path = std::env::var_os("PATH").unwrap_or_default();
let cargo_dir = std::env::split_paths(&path)
.find(|dir| dir.is_absolute() && dir.join(exe("cargo")).is_file())
.and_then(|bin| bin.parent().map(Path::to_path_buf))
.ok_or_else(|| GateError::Check("no cargo for the gate's test runs".into()))?;
let rustup = std::env::var_os("RUSTUP_HOME")
.filter(|v| !v.is_empty())
.map_or_else(|| home.join(".rustup"), PathBuf::from);
Ok((cargo_dir, rustup))
}
pub(super) fn run_env(
scratch: &Path,
work: &Path,
) -> Result<Vec<(String, std::ffi::OsString)>, GateError> {
let home = user_home()?;
let (rustup_cargo, rustup) = run_toolchain(&home)?;
make_private_dir(work)?;
let work = resolved(work).map_err(|e| GateError::Check(format!("the gate's work dir: {e}")))?;
let cargo_home = work.join("cargo-home");
make_private_dir(&cargo_home)?;
for name in ["config", "config.toml", "credentials", "credentials.toml"] {
let file = cargo_home.join(name);
if std::fs::symlink_metadata(&file).is_ok() {
return Err(GateError::Check(format!(
"{} is in the gate's CARGO_HOME, which the gate never writes; refused",
file.display()
)));
}
}
let tmp = scratch.join("tmp");
make_private_dir(&tmp)?;
let mut dirs = vec![home.join(".local").join("bin"), rustup_cargo.join("bin")];
dirs.extend(system_dirs());
let path = std::env::join_paths(&dirs)
.map_err(|e| GateError::Check(format!("the run's PATH: {e}")))?;
let mut env: Vec<(String, std::ffi::OsString)> = vec![
("PATH".into(), path),
("HOME".into(), home.clone().into()),
("LANG".into(), "C.UTF-8".into()),
("CARGO_HOME".into(), cargo_home.into()),
("CARGO_TARGET_DIR".into(), scratch.join("target").into()),
("CARGO_TERM_COLOR".into(), "never".into()),
("TMPDIR".into(), tmp.clone().into()),
];
#[cfg(windows)]
{
env.push(("USERPROFILE".into(), home.clone().into()));
env.push(("TEMP".into(), tmp.clone().into()));
env.push(("TMP".into(), tmp.into()));
if let Some(windows) = win::windows_dir() {
env.push(("SystemRoot".into(), windows.into()));
}
if let Some(data) = win::program_data() {
env.push(("ProgramData".into(), data.into()));
}
}
if rustup.is_dir() {
env.push(("RUSTUP_HOME".into(), rustup.into()));
}
Ok(env)
}
pub(super) fn run_command(
dir: &Path,
command: &str,
env: &[(String, std::ffi::OsString)],
log: &mut std::fs::File,
deadline: Instant,
) -> Result<i32, GateError> {
let out = log
.try_clone()
.map_err(|e| GateError::Check(format!("the run log: {e}")))?;
let err = log
.try_clone()
.map_err(|e| GateError::Check(format!("the run log: {e}")))?;
let mut cmd = Command::new(shell()?);
#[cfg(windows)]
let _script = {
let tmp = env
.iter()
.find(|(k, _)| k == "TMPDIR")
.map(|(_, v)| PathBuf::from(v))
.ok_or_else(|| GateError::Check("the run has no temp directory".into()))?;
let mut file = tempfile::Builder::new()
.prefix("g6-command-")
.suffix(".sh")
.tempfile_in(&tmp)
.map_err(|e| GateError::Check(format!("the command file: {e}")))?;
file.write_all(command.as_bytes())
.map_err(|e| GateError::Check(format!("the command file: {e}")))?;
let path = file.into_temp_path();
cmd.arg(&*path);
path
};
#[cfg(not(windows))]
cmd.arg("-c").arg(command);
cmd.env_clear()
.envs(env.iter().map(|(k, v)| (k, v)))
.current_dir(dir)
.stdin(Stdio::null())
.stdout(out)
.stderr(err);
#[cfg(unix)]
{
use std::os::unix::process::CommandExt as _;
cmd.process_group(0);
}
#[cfg(windows)]
let job = {
use std::os::windows::process::CommandExt as _;
cmd.creation_flags(win::CREATE_SUSPENDED);
win::Job::new().ok_or_else(|| GateError::Check("no job object for the run".into()))?
};
let mut child = cmd
.spawn()
.map_err(|e| GateError::Check(format!("the command did not start: {e}")))?;
#[cfg(windows)]
if !job.assign_and_resume(&child) {
let _ = child.kill();
let _ = child.wait();
return Err(GateError::Check(
"the run could not be held in its job object".into(),
));
}
loop {
match child.try_wait() {
Ok(Some(status)) => return Ok(status.code().unwrap_or(-1)),
Ok(None) if Instant::now() >= deadline => {
#[cfg(unix)]
if let Ok(group) = i32::try_from(child.id()) {
unsafe {
libc::kill(-group, libc::SIGKILL);
}
}
#[cfg(windows)]
job.terminate();
let _ = child.kill();
let _ = child.wait();
let _ = writeln!(log, "\n[G6: the run passed its time limit and was stopped]");
return Ok(-1);
}
Ok(None) => std::thread::sleep(Duration::from_millis(50)),
Err(e) => return Err(GateError::Check(format!("waiting for the command: {e}"))),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Stamp {
dev: u64,
ino: u64,
mode: u32,
ctime: i64,
ctime_ns: i64,
}
impl Stamp {
#[cfg(unix)]
fn of(meta: &std::fs::Metadata) -> Self {
use std::os::unix::fs::MetadataExt as _;
Self {
dev: meta.dev(),
ino: meta.ino(),
mode: meta.mode(),
ctime: meta.ctime(),
ctime_ns: meta.ctime_nsec(),
}
}
#[cfg(windows)]
fn of(meta: &std::fs::Metadata) -> Self {
use std::os::windows::fs::MetadataExt as _;
let written = meta.last_write_time();
Self {
mode: meta.file_attributes(),
ctime: i64::try_from(written / 10_000_000).unwrap_or(i64::MAX),
ctime_ns: i64::try_from(written % 10_000_000).unwrap_or_default() * 100,
..Self::default()
}
}
#[cfg(not(any(unix, windows)))]
fn of(_meta: &std::fs::Metadata) -> Self {
Self::default()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Held {
File(String, Stamp),
Link(String, Stamp),
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(super) struct LockedFiles {
held: BTreeMap<String, Held>,
dirs: BTreeMap<String, Option<Stamp>>,
root: Option<(u64, u64)>,
chain: Vec<(PathBuf, Option<Stamp>)>,
}
#[cfg(unix)]
fn movable(dir: &Path) -> bool {
use std::os::unix::ffi::OsStrExt as _;
use std::os::unix::fs::MetadataExt as _;
let Some(parent) = dir.parent() else {
return false;
};
let uid = unsafe { libc::getuid() };
if std::fs::metadata(parent).is_ok_and(|m| m.uid() == uid) {
return true;
}
let Ok(c) = std::ffi::CString::new(parent.as_os_str().as_bytes()) else {
return true;
};
unsafe { libc::access(c.as_ptr(), libc::W_OK) == 0 }
}
#[cfg(not(unix))]
fn movable(_dir: &Path) -> bool {
true
}
fn chain(cradle: &Path) -> Vec<(PathBuf, Option<Stamp>)> {
cradle
.ancestors()
.enumerate()
.map(|(i, dir)| {
let stamp = std::fs::symlink_metadata(dir)
.ok()
.filter(std::fs::Metadata::is_dir)
.map(|m| {
let s = Stamp::of(&m);
if i == 0 || (cfg!(unix) && !MoveWatch::SEES_RENAMES && movable(dir)) {
s
} else {
Stamp {
dev: s.dev,
ino: s.ino,
..Stamp::default()
}
}
});
(dir.to_path_buf(), stamp)
})
.collect()
}
#[cfg(target_os = "linux")]
pub(super) struct MoveWatch(std::os::fd::OwnedFd);
#[cfg(target_os = "linux")]
impl MoveWatch {
pub(super) fn new(cradle: &Path) -> Result<Self, GateError> {
use std::os::fd::{AsRawFd as _, FromRawFd as _};
use std::os::unix::ffi::OsStrExt as _;
let raw = unsafe { libc::inotify_init1(libc::IN_NONBLOCK | libc::IN_CLOEXEC) };
if raw < 0 {
return Err(GateError::Check(format!(
"watching the run directory: {}",
std::io::Error::last_os_error()
)));
}
let fd = unsafe { std::os::fd::OwnedFd::from_raw_fd(raw) };
for dir in cradle.ancestors() {
let c = std::ffi::CString::new(dir.as_os_str().as_bytes())
.map_err(|_| GateError::Check("a run path with a NUL byte".into()))?;
let wd = unsafe {
libc::inotify_add_watch(
fd.as_raw_fd(),
c.as_ptr(),
libc::IN_MOVE_SELF | libc::IN_DELETE_SELF | libc::IN_ONLYDIR,
)
};
if wd < 0 {
return Err(GateError::Check(format!(
"watching {}: {}",
dir.display(),
std::io::Error::last_os_error()
)));
}
}
Ok(Self(fd))
}
pub(super) fn moved(&self) -> bool {
use std::os::fd::AsRawFd as _;
let mut buf = [0u8; 4096];
let n = unsafe { libc::read(self.0.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len()) };
!(n < 0 && std::io::Error::last_os_error().kind() == std::io::ErrorKind::WouldBlock)
}
}
#[cfg(target_os = "linux")]
impl MoveWatch {
const SEES_RENAMES: bool = true;
}
#[cfg(target_vendor = "apple")]
pub(super) struct MoveWatch {
queue: std::os::fd::OwnedFd,
_dirs: Vec<std::os::fd::OwnedFd>,
}
#[cfg(target_vendor = "apple")]
impl MoveWatch {
const SEES_RENAMES: bool = true;
pub(super) fn new(cradle: &Path) -> Result<Self, GateError> {
use std::os::fd::{AsRawFd as _, FromRawFd as _, OwnedFd};
use std::os::unix::ffi::OsStrExt as _;
let fail = |what: &Path| {
GateError::Check(format!(
"watching {}: {}",
what.display(),
std::io::Error::last_os_error()
))
};
let raw = unsafe { libc::kqueue() };
if raw < 0 {
return Err(fail(cradle));
}
let queue = unsafe { OwnedFd::from_raw_fd(raw) };
let mut dirs = Vec::new();
for dir in cradle.ancestors() {
let c = std::ffi::CString::new(dir.as_os_str().as_bytes())
.map_err(|_| GateError::Check("a run path with a NUL byte".into()))?;
let fd = unsafe {
libc::open(
c.as_ptr(),
libc::O_EVTONLY | libc::O_DIRECTORY | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(fail(dir));
}
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
let ident = usize::try_from(fd.as_raw_fd()).map_err(|_| fail(dir))?;
let change = libc::kevent {
ident,
filter: libc::EVFILT_VNODE,
flags: libc::EV_ADD | libc::EV_CLEAR,
fflags: libc::NOTE_RENAME | libc::NOTE_DELETE | libc::NOTE_REVOKE,
data: 0,
udata: std::ptr::null_mut(),
};
let added = unsafe {
libc::kevent(
queue.as_raw_fd(),
&raw const change,
1,
std::ptr::null_mut(),
0,
std::ptr::null(),
)
};
if added < 0 {
return Err(fail(dir));
}
dirs.push(fd);
}
Ok(Self { queue, _dirs: dirs })
}
pub(super) fn moved(&self) -> bool {
use std::os::fd::AsRawFd as _;
let mut event: libc::kevent = unsafe { std::mem::zeroed() };
let now = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let n = unsafe {
libc::kevent(
self.queue.as_raw_fd(),
std::ptr::null(),
0,
&raw mut event,
1,
&raw const now,
)
};
n != 0
}
}
#[cfg(not(any(target_os = "linux", target_vendor = "apple")))]
pub(super) struct MoveWatch;
#[cfg(not(any(target_os = "linux", target_vendor = "apple")))]
impl MoveWatch {
const SEES_RENAMES: bool = false;
#[allow(clippy::unnecessary_wraps)]
pub(super) fn new(_cradle: &Path) -> Result<Self, GateError> {
Ok(Self)
}
pub(super) fn moved(&self) -> bool {
false
}
}
impl LockedFiles {
pub(super) fn read(tree: &Path, locked: &[String]) -> Result<Self, GateError> {
let mut held = BTreeMap::new();
if !locked.is_empty() {
walk(tree, tree, &mut |rel, path, meta| {
if !locked.iter().any(|c| under_locked(rel, c)) {
return Ok(());
}
let fail = |e: std::io::Error| GateError::Check(format!("reading {rel}: {e}"));
let stamp = Stamp::of(meta);
let entry = if meta.file_type().is_symlink() {
let target = std::fs::read_link(path).map_err(fail)?;
Held::Link(
hex::encode(Sha256::digest(target.to_string_lossy().as_bytes())),
stamp,
)
} else {
Held::File(
hex::encode(Sha256::digest(std::fs::read(path).map_err(fail)?)),
stamp,
)
};
held.insert(rel.to_owned(), entry);
Ok(())
})?;
}
let mut dirs = BTreeMap::new();
for rel in held.keys() {
for dir in parents(rel) {
dirs.entry(dir.clone()).or_insert_with(|| {
std::fs::symlink_metadata(tree.join(&dir))
.ok()
.filter(std::fs::Metadata::is_dir)
.map(|m| Stamp::of(&m))
});
}
}
let root = std::fs::symlink_metadata(tree)
.ok()
.filter(std::fs::Metadata::is_dir)
.map(|m| {
let s = Stamp::of(&m);
(s.dev, s.ino)
});
let chain = tree.parent().map(chain).unwrap_or_default();
Ok(Self {
held,
dirs,
root,
chain,
})
}
pub(super) fn changes(
&self,
tree: &Path,
now: &Self,
moved: bool,
) -> BTreeMap<String, LockedChange> {
let mut changed = BTreeMap::new();
for (path, was) in &self.held {
match now.held.get(path) {
None => {
changed.insert(path.clone(), LockedChange::Deleted);
}
Some(is) if std::mem::discriminant(is) != std::mem::discriminant(was) => {
changed.insert(path.clone(), LockedChange::TypeChanged);
}
Some(is) if is != was => {
changed.insert(path.clone(), LockedChange::Modified);
}
Some(_) => {}
}
}
for (dir, was) in &self.dirs {
let is = std::fs::symlink_metadata(tree.join(dir))
.ok()
.filter(std::fs::Metadata::is_dir)
.map(|m| Stamp::of(&m));
if is != *was {
for path in self
.held
.keys()
.filter(|p| p.starts_with(&format!("{dir}/")))
{
changed
.entry(path.clone())
.or_insert(LockedChange::Modified);
}
}
}
for path in now.held.keys() {
if !self.held.contains_key(path) {
changed.insert(path.clone(), LockedChange::Added);
}
}
let root_moved = self.root.is_some() && self.root != now.root;
let chain_moved = moved || self.chain != now.chain;
if (root_moved || chain_moved) && !self.held.is_empty() {
for path in self.held.keys() {
changed
.entry(path.clone())
.or_insert(LockedChange::Modified);
}
}
changed
}
#[cfg(unix)]
pub(super) fn lock(&self, tree: &Path) -> Result<(), GateError> {
use std::os::unix::fs::PermissionsExt as _;
let set = |path: &Path, mode: u32| {
std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))
.map_err(|e| GateError::Check(format!("{}: {e}", path.display())))
};
for (rel, held) in &self.held {
if matches!(held, Held::File(..)) {
set(&tree.join(rel), 0o444)?;
}
}
for dir in self.dirs.keys() {
set(&tree.join(dir), 0o555)?;
}
if let Some(cradle) = tree.parent() {
set(cradle, 0o555)?;
}
Ok(())
}
#[cfg(not(unix))]
pub(super) fn lock(&self, tree: &Path) -> Result<(), GateError> {
for (rel, held) in &self.held {
if matches!(held, Held::File(..)) {
let path = tree.join(rel);
let mut perms = std::fs::symlink_metadata(&path)
.map_err(|e| GateError::Check(format!("{}: {e}", path.display())))?
.permissions();
perms.set_readonly(true);
std::fs::set_permissions(&path, perms)
.map_err(|e| GateError::Check(format!("{}: {e}", path.display())))?;
}
}
Ok(())
}
}
fn parents(rel: &str) -> Vec<String> {
let mut out = Vec::new();
let mut parent = Path::new(rel).parent();
while let Some(dir) = parent.filter(|d| !d.as_os_str().is_empty()) {
out.push(dir.to_string_lossy().replace('\\', "/"));
parent = dir.parent();
}
out
}
fn walk(
root: &Path,
dir: &Path,
visit: &mut dyn FnMut(&str, &Path, &std::fs::Metadata) -> Result<(), GateError>,
) -> Result<(), GateError> {
let entries = std::fs::read_dir(dir)
.map_err(|e| GateError::Check(format!("reading {}: {e}", dir.display())))?;
for entry in entries {
let entry = entry.map_err(|e| GateError::Check(format!("reading the tree: {e}")))?;
let path = entry.path();
let meta = std::fs::symlink_metadata(&path)
.map_err(|e| GateError::Check(format!("{}: {e}", path.display())))?;
if meta.is_dir() {
walk(root, &path, visit)?;
continue;
}
let rel = path
.strip_prefix(root)
.map_err(|_| GateError::Check("a path outside the run tree".into()))?
.to_string_lossy()
.replace('\\', "/");
visit(&rel, &path, &meta)?;
}
Ok(())
}
pub(super) struct Unlock(pub(super) PathBuf);
impl Drop for Unlock {
fn drop(&mut self) {
fn open_up(dir: &Path) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let _ = std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700));
}
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
match std::fs::symlink_metadata(&path) {
Ok(m) if m.is_dir() => open_up(&path),
#[cfg(not(unix))]
Ok(m) if m.is_file() && m.permissions().readonly() => {
let mut perms = m.permissions();
#[expect(
clippy::permissions_set_readonly_false,
reason = "Windows: clears the read-only attribute, no mode bits"
)]
perms.set_readonly(false);
let _ = std::fs::set_permissions(&path, perms);
}
_ => {}
}
}
}
}
open_up(&self.0);
}
}