mod common;
use std::os::unix::net::UnixListener;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::{Arc, Barrier};
use std::thread;
use std::time::Duration;
use common::TmpDir;
use ostrya::{
Checksum, CreateOptions, DirMeta, Error, LockKind, ObjectType, Repo, RepoMode, Xattrs,
loose_path,
};
use ostrya_rt::block_on;
fn staging_dirs(repo: &Path) -> Vec<String> {
let tmp = repo.join("tmp");
let mut dirs = Vec::new();
if let Ok(entries) = std::fs::read_dir(&tmp) {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().into_owned();
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
if is_dir && name.starts_with("staging-") && !name.ends_with("-lock") {
dirs.push(name);
}
}
}
dirs
}
fn new_repo(tag: &str) -> (TmpDir, PathBuf) {
let dir = TmpDir::new(tag);
let repo_path = dir.path().join("repo");
block_on(Repo::create(&repo_path, CreateOptions::new(RepoMode::Bare))).expect("create repo");
(dir, repo_path)
}
#[test]
fn transaction_allocates_and_removes_a_staging_dir() {
let (_dir, repo_path) = new_repo("txn-life");
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
let dirs = staging_dirs(&repo_path);
assert_eq!(dirs.len(), 1, "one staging dir during the transaction");
let lock = repo_path.join("tmp").join(format!("{}-lock", dirs[0]));
assert!(
lock.exists(),
"staging lock sibling exists during the transaction"
);
txn.commit().await.unwrap();
assert!(
staging_dirs(&repo_path).is_empty(),
"staging dir removed after commit"
);
assert!(!lock.exists(), "staging lock removed after commit");
});
}
#[test]
fn aborting_a_transaction_removes_its_staging_dir() {
let (_dir, repo_path) = new_repo("txn-abort");
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
assert_eq!(staging_dirs(&repo_path).len(), 1);
txn.abort().await.unwrap();
assert!(
staging_dirs(&repo_path).is_empty(),
"abort reaps the staging dir"
);
});
}
#[test]
fn dropping_a_transaction_reaps_its_staging_dir() {
let (_dir, repo_path) = new_repo("txn-drop");
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
{
let _txn = repo.transaction().await.unwrap();
assert_eq!(
staging_dirs(&repo_path).len(),
1,
"staging dir present while held"
);
} assert!(
staging_dirs(&repo_path).is_empty(),
"dropping the transaction reaps the staging dir"
);
});
}
#[test]
fn concurrent_shared_transactions_get_distinct_staging_dirs() {
let (_dir, repo_path) = new_repo("txn-concurrent");
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let t1 = repo.transaction().await.unwrap();
let t2 = repo.transaction().await.unwrap();
let mut dirs = staging_dirs(&repo_path);
dirs.sort();
assert_eq!(dirs.len(), 2, "two live transactions, two staging dirs");
assert_ne!(dirs[0], dirs[1], "each transaction has its own staging dir");
t1.commit().await.unwrap();
assert_eq!(
staging_dirs(&repo_path).len(),
1,
"one staging dir after the first commit"
);
t2.commit().await.unwrap();
assert!(
staging_dirs(&repo_path).is_empty(),
"no staging dir after both commit"
);
});
}
#[test]
fn concurrent_transactions_keep_their_staging_dirs() {
let (_dir, repo_path) = new_repo("txn-stress");
let config = repo_path.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("tmp-expiry-secs=0\n");
std::fs::write(&config, text).unwrap();
const WORKERS: usize = 8;
const ROUNDS: usize = 20;
let repo = block_on(Repo::open(&repo_path)).unwrap();
for round in 0..ROUNDS {
let start = Arc::new(Barrier::new(WORKERS));
let created = Arc::new(Barrier::new(WORKERS + 1));
let release = Arc::new(Barrier::new(WORKERS + 1));
let mut handles = Vec::with_capacity(WORKERS);
for _ in 0..WORKERS {
let repo = repo.clone();
let start = Arc::clone(&start);
let created = Arc::clone(&created);
let release = Arc::clone(&release);
handles.push(thread::spawn(move || {
block_on(async {
start.wait();
let txn = repo.transaction().await.unwrap();
created.wait();
release.wait();
txn.commit().await.unwrap();
});
}));
}
created.wait();
let dirs = staging_dirs(&repo_path);
release.wait();
for handle in handles {
handle.join().unwrap();
}
assert_eq!(
dirs.len(),
WORKERS,
"round {round}: each live transaction keeps its own staging dir, got {dirs:?}"
);
}
}
#[test]
fn a_transaction_reaps_a_stale_staging_dir() {
let (dir, repo_path) = new_repo("txn-reap");
let tmp = repo_path.join("tmp");
let stale = format!("staging-{}-STALE0", boot_id());
let stale_dir = tmp.join(&stale);
std::fs::create_dir_all(stale_dir.join("aa")).unwrap();
std::fs::write(stale_dir.join("aa").join("leftover"), b"x").unwrap();
let stale_lock = tmp.join(format!("{stale}-lock"));
std::fs::write(&stale_lock, b"").unwrap();
let outside = dir.path().join("outside");
std::fs::create_dir(&outside).unwrap();
std::fs::write(outside.join("keep"), b"k").unwrap();
let outside_file = dir.path().join("outside-file");
std::fs::write(&outside_file, b"f").unwrap();
std::os::unix::fs::symlink(&outside, stale_dir.join("aa").join("to-dir")).unwrap();
std::os::unix::fs::symlink(&outside_file, stale_dir.join("to-file")).unwrap();
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
assert!(
!stale_dir.exists(),
"the stale staging dir is reaped at transaction start"
);
assert!(!stale_lock.exists(), "the stale staging lock is reaped too");
txn.commit().await.unwrap();
});
assert_eq!(std::fs::read(outside.join("keep")).unwrap(), b"k");
assert_eq!(std::fs::read(&outside_file).unwrap(), b"f");
}
fn boot_id() -> String {
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")
.unwrap()
.trim()
.to_owned()
}
fn mkfifo(path: &Path) {
rustix::fs::mknodat(
rustix::fs::CWD,
path,
rustix::fs::FileType::Fifo,
rustix::fs::Mode::from_raw_mode(0o600),
0,
)
.unwrap();
}
fn without_block<T: Send + 'static>(fifo: &Path, f: impl FnOnce() -> T + Send + 'static) -> T {
let (tx, rx) = std::sync::mpsc::channel();
let worker = thread::spawn(move || {
let _ = tx.send(f());
});
match rx.recv_timeout(Duration::from_secs(10)) {
Ok(value) => {
worker.join().unwrap();
value
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => match worker.join() {
Err(panic) => std::panic::resume_unwind(panic),
Ok(()) => unreachable!("the worker sends a value before it ends"),
},
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
let _ = rustix::fs::open(
fifo,
rustix::fs::OFlags::WRONLY
| rustix::fs::OFlags::NONBLOCK
| rustix::fs::OFlags::CLOEXEC,
rustix::fs::Mode::empty(),
);
panic!("an open of the FIFO {} blocked", fifo.display());
}
}
}
const NOT_REGULAR: [&str; 4] = ["symlink", "fifo", "socket", "directory"];
fn make_entry(kind: &str, at: &Path, target: &Path) -> Option<UnixListener> {
match kind {
"symlink" => std::os::unix::fs::symlink(target, at).unwrap(),
"fifo" => mkfifo(at),
"socket" => {
let bound = target.with_file_name("socket");
let listener = UnixListener::bind(&bound).unwrap();
std::fs::rename(&bound, at).unwrap();
return Some(listener);
}
"directory" => std::fs::create_dir(at).unwrap(),
other => unreachable!("no entry kind {other}"),
}
None
}
#[test]
fn a_repository_lock_that_is_not_a_regular_file_fails_the_begin() {
for kind in NOT_REGULAR {
let (dir, repo_path) = new_repo("txn-lock-not-regular");
let target = dir.path().join("outside");
std::fs::write(&target, b"outside bytes").unwrap();
let lock = repo_path.join(".lock");
let _listener = make_entry(kind, &lock, &target);
let path = repo_path.clone();
let err = without_block(&lock, move || {
block_on(async {
let repo = Repo::open(&path).await.unwrap();
repo.transaction().await.map(drop).unwrap_err()
})
});
let expected = if kind == "symlink" {
".lock passes through a symlink; ostrya refuses a symlink in the repository"
} else {
".lock is not a regular file"
};
assert!(
matches!(&err, Error::InvalidFormat(m) if m == expected),
".lock as a {kind}: {err:?}"
);
assert!(
staging_dirs(&repo_path).is_empty(),
".lock as a {kind}: the begin made no staging dir"
);
let left: Vec<String> = listing(&repo_path.join("tmp"))
.into_iter()
.filter(|n| n.starts_with("staging-"))
.collect();
assert!(left.is_empty(), ".lock as a {kind}: {left:?}");
assert_eq!(std::fs::read(&target).unwrap(), b"outside bytes");
let config = repo_path.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("locking=false\n");
std::fs::write(&config, text).unwrap();
let path = repo_path.clone();
without_block(&lock, move || {
block_on(async {
let repo = Repo::open(&path).await.unwrap();
repo.transaction().await.unwrap().commit().await.unwrap();
})
});
}
}
#[test]
fn a_stale_staging_dir_whose_lock_is_not_a_regular_file_stays() {
for kind in NOT_REGULAR {
let (dir, repo_path) = new_repo("txn-reap-not-regular");
let tmp = repo_path.join("tmp");
let target = dir.path().join("outside");
std::fs::write(&target, b"outside bytes").unwrap();
let stale = format!("staging-{}-STALE1", boot_id());
let stale_dir = tmp.join(&stale);
std::fs::create_dir(&stale_dir).unwrap();
std::fs::write(stale_dir.join("leftover"), b"x").unwrap();
stamp(&stale_dir, OLD);
let stale_lock = tmp.join(format!("{stale}-lock"));
let _listener = make_entry(kind, &stale_lock, &target);
let path = repo_path.clone();
without_block(&stale_lock, move || {
block_on(async {
let repo = Repo::open(&path).await.unwrap();
repo.transaction().await.unwrap().commit().await.unwrap();
})
});
assert!(
stale_dir.join("leftover").exists(),
"a {kind} lock keeps the stale staging dir"
);
assert!(
std::fs::symlink_metadata(&stale_lock).is_ok(),
"the {kind} lock stays"
);
assert_eq!(std::fs::read(&target).unwrap(), b"outside bytes");
}
}
fn stamp(path: &Path, epoch: i64) {
let status = Command::new("touch")
.arg("-h")
.arg("-d")
.arg(format!("@{epoch}"))
.arg(path)
.status()
.unwrap();
assert!(status.success(), "touch could not stamp {}", path.display());
}
fn listing(dir: &Path) -> Vec<String> {
let mut names: Vec<String> = std::fs::read_dir(dir)
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
}
const OLD: i64 = 1_577_836_800;
#[test]
fn a_transaction_reaps_aged_tmp_entries() {
let (dir, repo_path) = new_repo("txn-reap-aged");
let tmp = repo_path.join("tmp");
let outside = dir.path().join("outside");
std::fs::create_dir(&outside).unwrap();
std::fs::write(outside.join("keep"), b"k").unwrap();
stamp(&outside, OLD);
std::fs::write(tmp.join("oldfile"), b"o").unwrap();
stamp(&tmp.join("oldfile"), OLD);
std::fs::write(tmp.join(".tmp-old"), b"o").unwrap();
stamp(&tmp.join(".tmp-old"), OLD);
for name in [".ostrya-ref-1-1-aBc123", ".ostrya-tmp-1-aBc123"] {
std::fs::write(tmp.join(name), b"o").unwrap();
stamp(&tmp.join(name), OLD);
}
std::fs::write(tmp.join("staging-not-a-dir"), b"o").unwrap();
stamp(&tmp.join("staging-not-a-dir"), OLD);
let fifo = Command::new("mkfifo")
.arg(tmp.join("oldfifo"))
.status()
.unwrap();
assert!(fifo.success(), "mkfifo failed");
stamp(&tmp.join("oldfifo"), OLD);
std::fs::create_dir_all(tmp.join("olddir/sub")).unwrap();
std::fs::write(tmp.join("olddir/sub/fresh"), b"n").unwrap();
stamp(&tmp.join("olddir"), OLD);
std::os::unix::fs::symlink(&outside, tmp.join("oldlink")).unwrap();
stamp(&tmp.join("oldlink"), OLD);
std::fs::write(tmp.join("newfile"), b"n").unwrap();
std::fs::write(tmp.join(".ostrya-ref-2-2-aBc123"), b"n").unwrap();
std::fs::write(tmp.join(".ostrya-tmp-2-aBc123"), b"n").unwrap();
std::fs::create_dir(tmp.join("newdir")).unwrap();
std::fs::write(tmp.join("newdir/oldkid"), b"o").unwrap();
stamp(&tmp.join("newdir/oldkid"), OLD);
std::fs::write(tmp.join("cache/oldentry"), b"o").unwrap();
stamp(&tmp.join("cache/oldentry"), OLD);
stamp(&tmp.join("cache"), OLD);
std::fs::write(tmp.join("staging-orphan-lock"), b"").unwrap();
stamp(&tmp.join("staging-orphan-lock"), OLD);
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
let own: Vec<String> = staging_dirs(&repo_path)
.into_iter()
.flat_map(|n| [format!("{n}-lock"), n])
.collect();
let mut left = listing(&tmp);
left.retain(|n| !own.contains(n));
assert_eq!(
left,
[
".ostrya-ref-2-2-aBc123",
".ostrya-tmp-2-aBc123",
"cache",
"newdir",
"newfile",
"staging-orphan-lock"
],
"the reap at transaction start removes the aged entries only"
);
txn.commit().await.unwrap();
});
assert!(
tmp.join("cache/oldentry").exists(),
"tmp/cache is not touched"
);
assert!(
tmp.join("newdir/oldkid").exists(),
"a fresh directory is kept whole"
);
assert!(outside.join("keep").exists(), "the symlink target stays");
}
#[test]
fn an_aged_temp_symlink_goes_as_a_link_and_a_name_that_is_not_utf8_stays() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
let (dir, repo_path) = new_repo("txn-reap-names");
let tmp = repo_path.join("tmp");
let outside = dir.path().join("outside");
std::fs::write(&outside, b"outside bytes").unwrap();
let link = tmp.join(".ostrya-tmp-1-aBc123");
std::os::unix::fs::symlink(&outside, &link).unwrap();
stamp(&link, OLD);
let raw = tmp.join(OsStr::from_bytes(b"\xffold"));
std::fs::write(&raw, b"o").unwrap();
stamp(&raw, OLD);
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
repo.transaction().await.unwrap().commit().await.unwrap();
});
assert!(
std::fs::symlink_metadata(&link).is_err(),
"the aged temp symlink goes"
);
assert_eq!(std::fs::read(&outside).unwrap(), b"outside bytes");
assert!(raw.exists(), "a name that is not UTF-8 stays");
}
#[test]
fn the_tmp_age_test_is_strict_on_whole_seconds() {
let (_dir, repo_path) = new_repo("txn-reap-age");
let config = repo_path.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("tmp-expiry-secs=5\n");
std::fs::write(&config, text).unwrap();
let tmp = repo_path.join("tmp");
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
for age in [3, 8] {
let path = tmp.join(format!("age{age}"));
std::fs::write(&path, b"a").unwrap();
stamp(&path, now - age);
}
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
assert!(
tmp.join("age3").exists(),
"an entry inside the window stays"
);
assert!(!tmp.join("age8").exists(), "an entry past the window goes");
txn.commit().await.unwrap();
});
}
#[test]
fn an_aged_lock_of_a_live_transaction_is_kept() {
let (_dir, repo_path) = new_repo("txn-reap-live-lock");
let tmp = repo_path.join("tmp");
block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let live = repo.transaction().await.unwrap();
let name = staging_dirs(&repo_path).pop().unwrap();
let lock = tmp.join(format!("{name}-lock"));
stamp(&tmp.join(&name), OLD);
stamp(&lock, OLD);
let second = repo.transaction().await.unwrap();
assert!(
lock.exists(),
"the lock of a live transaction is kept at any age"
);
assert!(tmp.join(&name).exists(), "the live staging dir is kept");
second.commit().await.unwrap();
live.commit().await.unwrap();
});
}
#[test]
fn cross_process_lock_contention() {
let (_dir, repo_path) = new_repo("txn-xproc");
let config = repo_path.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("lock-timeout-secs=1\n");
std::fs::write(&config, text).unwrap();
let held_marker = repo_path.join(".held");
let _ = std::fs::remove_file(&held_marker);
let mut holder = Command::new(std::env::current_exe().unwrap())
.args([
"lock_holder_subprocess",
"--exact",
"--ignored",
"--nocapture",
])
.env("OSTRYA_HOLD_REPO", &repo_path)
.env("OSTRYA_HOLD_MS", "3000")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("spawn lock holder");
let mut ready = false;
for _ in 0..200 {
if held_marker.exists() {
ready = true;
break;
}
std::thread::sleep(Duration::from_millis(50));
}
assert!(ready, "the holder never acquired the lock");
let contended = block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
repo.transaction().await
});
assert!(contended.is_err(), "acquiring while contended should fail");
assert!(
matches!(contended.err(), Some(Error::LockTimeout { .. })),
"the contended acquire should report a lock timeout"
);
holder.wait().expect("holder exits");
let after = block_on(async {
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await?;
txn.commit().await
});
assert!(
after.is_ok(),
"acquire after release should succeed: {after:?}"
);
}
#[test]
#[ignore = "helper process for cross_process_lock_contention"]
fn lock_holder_subprocess() {
let Ok(repo_path) = std::env::var("OSTRYA_HOLD_REPO") else {
return;
};
let hold_ms: u64 = std::env::var("OSTRYA_HOLD_MS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(3000);
block_on(async {
let repo = Repo::open(Path::new(&repo_path)).await.expect("open repo");
let txn = repo
.transaction_with_lock(LockKind::Exclusive)
.await
.expect("acquire exclusive lock");
std::fs::write(Path::new(&repo_path).join(".held"), b"1").expect("write readiness marker");
ostrya_rt::Timer::after(Duration::from_millis(hold_ms)).await;
txn.commit().await.expect("release lock");
});
}
#[test]
fn write_dirmeta_and_loose_path_are_public() {
let dir = TmpDir::new("public-dirmeta");
let meta = DirMeta {
uid: 1000,
gid: 1000,
mode: 0o042_771,
xattrs: Xattrs::new([(b"user.mark\0".to_vec(), b"value".to_vec())]).unwrap(),
};
let canonical = DirMeta {
uid: 0,
gid: 0,
mode: 0o040_751,
xattrs: Xattrs::empty(),
};
let stored = |mode: RepoMode, meta: &DirMeta| -> Checksum {
let repo_path = dir.path().join(format!("repo-{}", mode.as_mode_str()));
let meta = meta.clone();
block_on(async move {
Repo::create(&repo_path, CreateOptions::new(mode))
.await
.unwrap();
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
let checksum = txn.write_dirmeta(&meta).await.unwrap();
txn.commit().await.unwrap();
let path =
repo_path
.join("objects")
.join(loose_path(&checksum, ObjectType::DirMeta, mode));
assert!(path.exists(), "{} names the written object", path.display());
assert_eq!(
std::fs::read(&path).unwrap(),
meta.serialize().unwrap(),
"the object holds the bytes the mode records"
);
checksum
})
};
let bare = stored(RepoMode::Bare, &meta);
let bare_user_only = stored(RepoMode::BareUserOnly, &canonical);
assert_ne!(
bare, bare_user_only,
"the two modes record the directory under different identities"
);
let repo_path = dir.path().join("repo-canonical");
let recorded = block_on(async {
Repo::create(&repo_path, CreateOptions::new(RepoMode::BareUserOnly))
.await
.unwrap();
let repo = Repo::open(&repo_path).await.unwrap();
let txn = repo.transaction().await.unwrap();
let checksum = txn.write_dirmeta(&meta).await.unwrap();
txn.commit().await.unwrap();
checksum
});
assert_eq!(recorded, bare_user_only);
}