use std::fs::{File, OpenOptions};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use fs4::fs_std::FileExt;
use super::call::{Call, CancelToken};
use super::fscheck;
use super::sidecar::{self, Sidecar, SidecarState, INTENT, VALVE};
use crate::error::{MnemoError, Result};
use crate::pager::LockedFile;
use crate::store::{is_file_at, is_lock_conflict};
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct SharedOptions {
pub deadline: Duration,
pub allow_network_fs: bool,
pub cancel: Option<CancelToken>,
}
impl Default for SharedOptions {
fn default() -> Self {
SharedOptions {
deadline: Duration::from_secs(10),
allow_network_fs: false,
cancel: None,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum LockKind {
Shared,
Exclusive,
}
#[derive(Debug)]
pub(crate) struct BrainLocks {
path: PathBuf,
resolved: PathBuf,
sidecar: Option<Arc<Sidecar>>,
notices: Vec<String>,
plain: bool,
valve_takes: u64,
}
const VALVE_AFTER: Duration = Duration::from_millis(500);
fn valve_off() -> bool {
#[cfg(feature = "dev-hooks")]
{
static OFF: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*OFF.get_or_init(|| std::env::var_os("MNEMO_DEV_NO_VALVE").is_some())
}
#[cfg(not(feature = "dev-hooks"))]
false
}
fn valve_after() -> Duration {
#[cfg(feature = "dev-hooks")]
{
static AFTER: std::sync::OnceLock<Duration> = std::sync::OnceLock::new();
*AFTER.get_or_init(|| {
std::env::var("MNEMO_DEV_VALVE_MS")
.ok()
.and_then(|v| v.parse().ok())
.map(Duration::from_millis)
.unwrap_or(VALVE_AFTER)
})
}
#[cfg(not(feature = "dev-hooks"))]
VALVE_AFTER
}
pub(crate) const WINDOWS_OLD_RENAME: bool = cfg!(mnemo_windows_old_rename);
impl BrainLocks {
pub(crate) fn new(path: &Path, opts: &SharedOptions) -> Result<Self> {
Self::with_sidecar(path, opts, true)
}
pub(crate) fn for_plain(path: &Path, opts: &SharedOptions) -> Result<Self> {
Self::with_sidecar(path, opts, false)
}
fn with_sidecar(path: &Path, opts: &SharedOptions, create: bool) -> Result<Self> {
if WINDOWS_OLD_RENAME {
return Err(MnemoError::Unsupported(
"shared mode on Windows needs a build with Rust 1.85 or later (this one was \
built with an older compiler, whose std::fs::rename can't replace an open \
file)"
.into(),
));
}
let path = if path.is_absolute() {
path.to_path_buf()
} else {
std::env::current_dir()?.join(path)
};
let resolved = std::fs::canonicalize(&path)?;
let mut notices = fscheck::check(&resolved, opts.allow_network_fs)?;
let present = std::fs::symlink_metadata(sidecar::sidecar_path(&resolved)).is_ok();
let sidecar = if !create && !present {
None
} else {
match sidecar::open(&resolved) {
SidecarState::Ready(s) => {
if create {
let _ = s.lock_file(VALVE);
}
Some(Arc::new(s))
}
SidecarState::Unavailable(why) => {
notices.push(format!(
"not using the sidecar directory: {why}; each call takes the main \
lock alone, without writer preference or a lock-info note"
));
None
}
}
};
Ok(BrainLocks {
path,
resolved,
sidecar,
notices,
plain: !create,
valve_takes: 0,
})
}
pub(crate) fn notices(&self) -> &[String] {
&self.notices
}
pub(crate) fn has_sidecar(&self) -> bool {
self.sidecar.is_some()
}
pub(crate) fn sidecar_path(&self) -> Option<&Path> {
self.sidecar.as_deref().map(Sidecar::path)
}
pub(crate) fn valve_takes(&self) -> u64 {
self.valve_takes
}
pub(crate) fn acquire(&mut self, kind: LockKind, call: &mut Call) -> Result<Held> {
let mut replaced_once = false;
let mut valve: Option<LockedFile> = None;
'call: loop {
call.check()?;
if kind == LockKind::Shared
&& !valve_off()
&& call.waited() < valve_after()
&& self.valve_held()
{
self.wait(kind, call, &mut valve)?;
continue;
}
let intent = match self.try_intent(kind, call)? {
Intent::Held(f) => Some(f),
Intent::NotUsed => None,
Intent::Busy => {
self.wait(kind, call, &mut valve)?;
continue;
}
};
loop {
#[cfg(any(test, feature = "failpoints"))]
crate::__failpoints::count_shared_main_try();
match self.try_main(kind)? {
Main::Got(file) => {
drop(valve);
drop(intent);
return Ok(Held {
file: Some(file),
sidecar: self.sidecar.clone(),
announced: false,
});
}
Main::Replaced => {
call.check()?;
if call.past_deadline() {
return Err(self.locked());
}
if replaced_once {
drop(intent);
self.wait(kind, call, &mut valve)?;
continue 'call;
}
replaced_once = true;
continue;
}
Main::Busy => {}
}
if intent.is_none() {
break;
}
self.wait(kind, call, &mut valve)?;
}
drop(intent);
self.wait(kind, call, &mut valve)?;
}
}
fn wait(
&mut self,
kind: LockKind,
call: &mut Call,
valve: &mut Option<LockedFile>,
) -> Result<()> {
self.find_sidecar();
if kind == LockKind::Exclusive
&& valve.is_none()
&& !valve_off()
&& call.waited() >= valve_after()
{
call.hurry();
*valve = self.take_valve(call);
}
if kind == LockKind::Shared && !valve_off() && call.waited() >= valve_after() {
call.hurry();
}
if call.back_off()? {
Ok(())
} else {
Err(self.locked())
}
}
fn find_sidecar(&mut self) {
if !self.plain || self.sidecar.is_some() {
return;
}
if std::fs::symlink_metadata(sidecar::sidecar_path(&self.resolved)).is_err() {
return;
}
if let SidecarState::Ready(s) = sidecar::open(&self.resolved) {
self.sidecar = Some(Arc::new(s));
}
}
fn take_valve(&mut self, call: &mut Call) -> Option<LockedFile> {
let side = self.sidecar.clone()?;
let f = match side.lock_file(VALVE) {
Ok(f) => f,
Err(_) if !call.sidecar_reopened => {
call.sidecar_reopened = true;
let SidecarState::Ready(s) = sidecar::open(&self.resolved) else {
return None;
};
let s = Arc::new(s);
self.sidecar = Some(s.clone());
match s.lock_file(VALVE) {
Ok(f) => f,
Err(e) => return self.no_valve(&s, e),
}
}
Err(e) => return self.no_valve(&side, e),
};
match FileExt::try_lock_exclusive(&f) {
Ok(()) => {
self.valve_takes += 1;
Some(LockedFile::new(f))
}
Err(_) => None,
}
}
fn no_valve(&mut self, side: &Sidecar, e: std::io::Error) -> Option<LockedFile> {
if e.kind() != std::io::ErrorKind::NotFound {
self.notice(format!(
"can't use {}: {e}; a write that waits goes on without the writer safety valve",
side.path().join(VALVE).display()
));
}
None
}
fn valve_held(&self) -> bool {
let Some(f) = self.sidecar.as_ref().and_then(|s| s.valve_for_reading()) else {
return false;
};
match FileExt::try_lock_shared(f) {
Ok(()) => {
let _ = FileExt::unlock(f);
false
}
Err(e) => is_lock_conflict(&e),
}
}
fn locked(&self) -> MnemoError {
MnemoError::Locked {
path: self.path.clone(),
holder: self.holder(),
}
}
fn try_intent(&mut self, kind: LockKind, call: &mut Call) -> Result<Intent> {
let Some(side) = &self.sidecar else {
return Ok(Intent::NotUsed);
};
let f = match side.lock_file(INTENT) {
Ok(f) => f,
Err(_) if !call.sidecar_reopened => {
call.sidecar_reopened = true;
match sidecar::open(&self.resolved) {
SidecarState::Ready(s) => {
let s = Arc::new(s);
self.sidecar = Some(s.clone());
match s.lock_file(INTENT) {
Ok(f) => f,
Err(e) => {
self.notice(format!(
"can't use {}: {e}; this call takes the main lock alone, \
without writer preference",
s.path().join(INTENT).display()
));
return Ok(Intent::NotUsed);
}
}
}
SidecarState::Unavailable(why) => {
self.notice(format!(
"not using the sidecar directory: {why}; calls take the main \
lock alone, without writer preference or a lock-info note"
));
return Ok(Intent::NotUsed);
}
}
}
Err(_) => return Ok(Intent::NotUsed),
};
match try_lock(&f, kind) {
Ok(()) => Ok(Intent::Held(LockedFile::new(f))),
Err(e) if is_lock_conflict(&e) => Ok(Intent::Busy),
Err(e) => Err(MnemoError::Io(e)),
}
}
pub(crate) fn notice(&mut self, text: String) {
if !self.notices.contains(&text) {
self.notices.push(text);
}
}
fn try_main(&self, kind: LockKind) -> Result<Main> {
let mut o = OpenOptions::new();
o.read(true);
if kind == LockKind::Exclusive {
o.write(true);
}
let f = match o.open(&self.path) {
Ok(f) => f,
Err(e) if kind == LockKind::Exclusive && is_permission_denied(&e) => {
return Err(MnemoError::Io(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
format!(
"{} isn't writable by this process ({e}), so a shared-mode write \
can't take its lock; reads still work",
self.path.display()
),
)))
}
Err(e) => return Err(MnemoError::Io(e)),
};
match try_lock(&f, kind) {
Ok(()) => {
let f = LockedFile::new(f);
#[cfg(any(test, feature = "failpoints"))]
if crate::__failpoints::take_shared_replaced() {
return Ok(Main::Replaced);
}
if is_file_at(&f, &self.path)? {
Ok(Main::Got(f))
} else {
Ok(Main::Replaced)
}
}
Err(e) if is_lock_conflict(&e) => Ok(Main::Busy),
Err(e) => Err(MnemoError::Io(e)),
}
}
fn holder(&self) -> Option<String> {
let line = self.sidecar.as_ref()?.read_lock_info()?;
let info = LockInfo::parse(&line)?;
sidecar::pid_alive(info.pid).then(|| info.describe())
}
}
enum Intent {
Held(LockedFile),
Busy,
NotUsed,
}
enum Main {
Got(LockedFile),
Busy,
Replaced,
}
pub(crate) fn is_permission_denied(e: &std::io::Error) -> bool {
e.kind() == std::io::ErrorKind::PermissionDenied
}
fn try_lock(f: &File, kind: LockKind) -> std::io::Result<()> {
match kind {
LockKind::Shared => FileExt::try_lock_shared(f),
LockKind::Exclusive => FileExt::try_lock_exclusive(f),
}
}
pub(crate) struct Held {
file: Option<LockedFile>,
sidecar: Option<Arc<Sidecar>>,
announced: bool,
}
impl Held {
pub(crate) fn file(&self) -> &File {
self.file.as_ref().expect("held until dropped")
}
pub(crate) fn take_file(&mut self) -> LockedFile {
self.file.take().expect("taken once")
}
pub(crate) fn announce(&mut self, op: &str) {
if let Some(side) = &self.sidecar {
let line = LockInfo::current(op).encode();
if side.write_lock_info(&line).is_ok() {
self.announced = true;
}
}
}
}
impl Drop for Held {
fn drop(&mut self) {
if self.announced {
if let Some(side) = &self.sidecar {
side.remove_lock_info();
}
}
drop(self.file.take());
}
}
struct LockInfo {
op: String,
pid: u32,
since: u64,
tool: String,
}
impl LockInfo {
fn current(op: &str) -> Self {
let tool = std::env::current_exe()
.ok()
.and_then(|p| p.file_stem().map(|s| s.to_string_lossy().into_owned()))
.unwrap_or_else(|| "mnemo".into());
LockInfo {
op: clean(op),
pid: std::process::id(),
since: SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_secs()),
tool: clean(&tool),
}
}
fn encode(&self) -> String {
format!("{}\t{}\t{}\t{}", self.op, self.pid, self.since, self.tool)
}
fn parse(line: &str) -> Option<Self> {
let mut f = line.split('\t');
Some(LockInfo {
op: clean(f.next()?),
pid: f.next()?.parse().ok()?,
since: f.next()?.parse().ok()?,
tool: clean(f.next()?),
})
}
fn describe(&self) -> String {
let s = self.since % 86_400;
format!(
"{} since {:02}:{:02}:{:02} UTC (pid {}, {})",
self.op,
s / 3600,
s % 3600 / 60,
s % 60,
self.pid,
self.tool
)
}
}
fn clean(s: &str) -> String {
s.chars().filter(|c| !c.is_control()).take(64).collect()
}