use std::os::fd::{BorrowedFd, OwnedFd};
use rustix::fs::{FileType, FlockOperation, Mode, OFlags};
use rustix::io::Errno;
use super::dir::{BENEATH, io_error, require};
use super::{FsError, Reason, RepoDir, RepoLayout, RootFile};
use crate::lock::LockKind;
const LOCK_OPEN: OFlags = OFlags::RDWR
.union(OFlags::NOFOLLOW)
.union(OFlags::NONBLOCK)
.union(OFlags::NOCTTY)
.union(OFlags::CLOEXEC);
impl RepoDir {
pub(crate) fn open_lock(&self, name: &str) -> Result<OwnedFd, FsError> {
let (plain, shared) =
self.lock_modes("open_lock needs the root handle or the tmp handle")?;
let mut again = true;
loop {
#[cfg(test)]
super::dir::run_hook(super::HookPoint::LockCreate, name.as_bytes());
match self.create_new(name, plain, shared, false) {
Ok(file) => return Ok(OwnedFd::from(file)),
Err(FsError::Exists) => {}
Err(e) => return Err(e),
}
#[cfg(test)]
super::dir::run_hook(super::HookPoint::LockOpen, name.as_bytes());
match self.open_existing_lock(name) {
Err(FsError::Absent { .. }) if again => again = false,
other => return other,
}
}
}
pub(crate) fn open_existing_lock(&self, name: &str) -> Result<OwnedFd, FsError> {
self.lock_modes("open_existing_lock needs the root handle or the tmp handle")?;
self.check_path(name)?;
match rustix::fs::openat2(self, name, LOCK_OPEN, Mode::empty(), BENEATH) {
Ok(fd) => self
.finish(fd, name)
.map(|opened| OwnedFd::from(opened.file)),
Err(Errno::ISDIR) => Err(FsError::Invalid {
path: self.err_path(name),
reason: Reason::NotRegular(FileType::Directory),
}),
Err(e) => Err(self.open_error(e, name)),
}
}
fn lock_modes(&self, what: &'static str) -> Result<(u32, u32), FsError> {
let modes = match self.kind {
RepoLayout::Root => Some((
RootFile::Lock.file_mode(),
RootFile::Lock.shared_file_mode(),
)),
RepoLayout::Tmp => {
let rules = self.kind.rules();
rules.file_mode.zip(rules.shared_file_mode)
}
_ => None,
};
require(modes, what)
}
}
pub(crate) fn try_lock(fd: BorrowedFd<'_>, kind: LockKind) -> Result<bool, FsError> {
let operation = match kind {
LockKind::Shared => FlockOperation::NonBlockingLockShared,
LockKind::Exclusive => FlockOperation::NonBlockingLockExclusive,
};
match rustix::fs::fcntl_lock(fd, operation) {
Ok(()) => Ok(true),
Err(Errno::AGAIN | Errno::ACCESS) => Ok(false),
Err(e) => Err(io_error(e)),
}
}
pub(crate) fn unlock(fd: BorrowedFd<'_>) -> Result<(), FsError> {
rustix::fs::fcntl_lock(fd, FlockOperation::Unlock).map_err(io_error)
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use std::fs::File;
use std::io::Read;
use std::os::fd::AsFd;
use std::os::unix::fs::{PermissionsExt, symlink};
use std::os::unix::net::UnixListener;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::rc::Rc;
use ostrya_core::RepoMode;
use super::super::{
HookPoint, TestDir, id_of, invalid, io_err, mkfifo, mode_of, reference_file_mode, root,
set_hook, stat_fd, without_block,
};
use super::*;
const CHILD_DIR_ENV: &str = "OSTRYA_TEST_REPO_FS_LOCK_DIR";
const CHILD_EXPECT_ENV: &str = "OSTRYA_TEST_REPO_FS_LOCK_EXPECT";
fn not_regular<T: std::fmt::Debug>(result: Result<T, FsError>, path: &str) -> FileType {
match invalid(result) {
(p, Reason::NotRegular(file_type)) if p == path => file_type,
other => panic!("{other:?}"),
}
}
fn content(fd: OwnedFd) -> Vec<u8> {
let mut buf = Vec::new();
File::from(fd).read_to_end(&mut buf).unwrap();
buf
}
#[test]
fn open_lock_creates_the_file_with_the_lock_mode() {
for mode in [None, Some(RepoMode::BareUserShared)] {
let dir = TestDir::new("lock-create");
let mut repo = root(&dir);
if let Some(mode) = mode {
repo = repo.with_repo_mode(mode);
}
let tmp = repo.tmp_dir(false).unwrap();
let tmp_path = dir.path().join("tmp");
let cases = [
(&repo, ".lock", dir.path().to_owned(), 0o660),
(&repo, ".update.lock", dir.path().to_owned(), 0o660),
(&tmp, "staging-x-lock", tmp_path, 0o600),
];
for (handle, name, parent, plain) in cases {
let fd = handle.open_lock(name).unwrap();
let expected = match mode {
Some(_) => 0o660,
None => reference_file_mode(&parent, plain),
};
let path = parent.join(name);
assert_eq!(mode_of(&path), expected, "{mode:?} {name}");
let stat = stat_fd(fd.as_fd()).unwrap();
assert_eq!((stat.dev, stat.ino), id_of(&path), "{mode:?} {name}");
assert!(try_lock(fd.as_fd(), LockKind::Exclusive).unwrap());
}
}
}
#[test]
fn open_lock_opens_an_existing_regular_file_and_keeps_its_mode() {
let dir = TestDir::new("lock-existing");
let repo = root(&dir).with_repo_mode(RepoMode::BareUserShared);
let path = dir.path().join(".lock");
std::fs::write(&path, b"content").unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
let fd = repo.open_lock(".lock").unwrap();
let stat = stat_fd(fd.as_fd()).unwrap();
assert_eq!((stat.dev, stat.ino), id_of(&path));
assert_eq!(mode_of(&path), 0o600);
assert_eq!(content(fd), b"content");
let fd = repo.open_existing_lock(".lock").unwrap();
assert_eq!(content(fd), b"content");
}
#[test]
fn open_lock_fails_for_a_symlink_a_fifo_a_socket_and_a_directory() {
let dir = TestDir::new("lock-special");
let other = TestDir::new("lock-special-other");
let at = |name: &str| dir.path().join(name);
let target = other.path().join("target");
std::fs::write(&target, b"target").unwrap();
symlink(&target, at("link.lock")).unwrap();
symlink(other.path().join("missing"), at("dangling.lock")).unwrap();
let fifo = at("fifo.lock");
mkfifo(&fifo);
let _listener = UnixListener::bind(at("sock.lock")).unwrap();
std::fs::create_dir(at("dir.lock")).unwrap();
let repo = root(&dir);
for name in ["link.lock", "dangling.lock"] {
for result in [repo.open_lock(name), repo.open_existing_lock(name)] {
let (path, reason) = invalid(result);
assert_eq!(path, name);
assert!(matches!(reason, Reason::Symlink), "{name}: {reason:?}");
}
}
assert_eq!(std::fs::read(&target).unwrap(), b"target");
assert!(!other.path().join("missing").exists());
let handle = repo.clone();
let results = without_block(&fifo, move || {
[
handle.open_lock("fifo.lock").map(drop),
handle.open_existing_lock("fifo.lock").map(drop),
]
});
for result in results {
assert_eq!(not_regular(result, "fifo.lock"), FileType::Fifo);
}
for (name, expected) in [
("sock.lock", FileType::Socket),
("dir.lock", FileType::Directory),
] {
for result in [repo.open_lock(name), repo.open_existing_lock(name)] {
assert_eq!(not_regular(result, name), expected, "{name}");
}
}
}
#[test]
fn open_lock_creates_the_file_again_when_it_goes_after_the_create() {
let dir = TestDir::new("lock-gone");
let path = dir.path().join(".lock");
let moved = dir.path().join("moved");
std::fs::write(&path, b"").unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
let calls = Rc::new(Cell::new(0));
let hook_calls = Rc::clone(&calls);
let (hook_path, hook_moved) = (path.clone(), moved.clone());
let _hook = set_hook(HookPoint::LockOpen, move |name| {
assert_eq!(name, b".lock");
std::fs::rename(&hook_path, &hook_moved).unwrap();
hook_calls.set(hook_calls.get() + 1);
});
let fd = root(&dir).open_lock(".lock").unwrap();
assert_eq!(calls.get(), 1);
let stat = stat_fd(fd.as_fd()).unwrap();
assert_eq!((stat.dev, stat.ino), id_of(&path));
assert_ne!(id_of(&path), id_of(&moved));
assert_eq!(mode_of(&path), reference_file_mode(dir.path(), 0o660));
}
#[test]
fn open_lock_gives_absent_when_the_file_goes_after_each_create() {
let dir = TestDir::new("lock-gone-twice");
let path = dir.path().join(".lock");
let moved = dir.path().join("moved");
let calls = Rc::new(Cell::new(0));
let hook_calls = Rc::clone(&calls);
let create_path = path.clone();
let (hook_path, hook_moved) = (path.clone(), moved.clone());
let _create = set_hook(HookPoint::LockCreate, move |_| {
std::fs::write(&create_path, b"").unwrap();
});
let _open = set_hook(HookPoint::LockOpen, move |_| {
std::fs::rename(&hook_path, &hook_moved).unwrap();
hook_calls.set(hook_calls.get() + 1);
});
match root(&dir).open_lock(".lock") {
Err(FsError::Absent { path: p }) => assert_eq!(p, ".lock"),
other => panic!("{other:?}"),
}
assert_eq!(calls.get(), 2);
assert!(!path.exists());
assert!(moved.exists());
}
#[test]
fn open_existing_lock_gives_absent_for_a_missing_file() {
let dir = TestDir::new("lock-absent");
let repo = root(&dir);
let tmp = repo.tmp_dir(false).unwrap();
for (handle, name, path) in [
(&repo, ".lock", ".lock"),
(&tmp, "staging-x-lock", "tmp/staging-x-lock"),
] {
match handle.open_existing_lock(name) {
Err(FsError::Absent { path: p }) => assert_eq!(p, path),
other => panic!("{other:?}"),
}
}
assert!(!dir.path().join(".lock").exists());
assert!(!dir.path().join("tmp/staging-x-lock").exists());
}
#[test]
fn try_lock_takes_and_unlock_releases_a_record_lock() {
let dir = TestDir::new("lock-try");
let fd = root(&dir).open_lock(".lock").unwrap();
assert!(try_lock(fd.as_fd(), LockKind::Shared).unwrap());
assert!(try_lock(fd.as_fd(), LockKind::Exclusive).unwrap());
unlock(fd.as_fd()).unwrap();
let read_only = rustix::fs::openat(
dir.fd(),
".lock",
OFlags::RDONLY | OFlags::CLOEXEC,
Mode::empty(),
)
.unwrap();
let err = io_err(try_lock(read_only.as_fd(), LockKind::Exclusive));
assert_eq!(err.raw_os_error(), Some(Errno::BADF.raw_os_error()));
}
fn run_child(dir: &Path, expect: &str) {
let output = Command::new(std::env::current_exe().unwrap())
.args([
"repo_fs::lock::tests::lock_child_subprocess",
"--exact",
"--ignored",
"--nocapture",
])
.env(CHILD_DIR_ENV, dir)
.env(CHILD_EXPECT_ENV, expect)
.stdin(Stdio::null())
.stderr(Stdio::inherit())
.output()
.expect("re-execute this test binary");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
output.status.success() && stdout.contains("1 passed"),
"the child reported {}:\n{stdout}",
output.status
);
assert!(dir.join(format!(".child-{expect}")).exists(), "{expect}");
}
#[test]
fn try_lock_gives_false_while_another_process_holds_the_lock() {
let dir = TestDir::new("lock-process");
let repo = root(&dir);
let fd = repo.open_lock(".lock").unwrap();
assert!(try_lock(fd.as_fd(), LockKind::Exclusive).unwrap());
assert_eq!(
repo.lstat(".lock").unwrap().file_type,
FileType::RegularFile
);
assert!(repo.exists(".lock").unwrap());
run_child(dir.path(), "held");
unlock(fd.as_fd()).unwrap();
run_child(dir.path(), "free");
}
#[test]
#[ignore = "helper process for try_lock_gives_false_while_another_process_holds_the_lock"]
fn lock_child_subprocess() {
let Some(path) = std::env::var_os(CHILD_DIR_ENV).map(PathBuf::from) else {
return;
};
let expect = std::env::var(CHILD_EXPECT_ENV).unwrap();
let free = match expect.as_str() {
"held" => false,
"free" => true,
other => panic!("{other}"),
};
let repo = RepoDir::open_root(rustix::fs::CWD, &path).unwrap();
let fd = repo.open_lock(".lock").unwrap();
assert_eq!(try_lock(fd.as_fd(), LockKind::Shared).unwrap(), free);
assert_eq!(try_lock(fd.as_fd(), LockKind::Exclusive).unwrap(), free);
std::fs::write(path.join(format!(".child-{expect}")), b"1").unwrap();
}
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "open_lock needs the root handle or the tmp handle")]
fn open_lock_on_another_kind_fails_a_debug_assertion() {
let dir = TestDir::new("misuse-lock");
let refs = root(&dir).ensure_layout(RepoLayout::Refs, false).unwrap();
let _ = refs.open_lock("x.lock");
}
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "open_existing_lock needs the root handle or the tmp handle")]
fn open_existing_lock_on_another_kind_fails_a_debug_assertion() {
let dir = TestDir::new("misuse-existing-lock");
let refs = root(&dir).ensure_layout(RepoLayout::Refs, false).unwrap();
let _ = refs.open_existing_lock("x.lock");
}
}