use std::fs::{self, File, OpenOptions};
use std::io::{self, ErrorKind, Write};
use std::path::{Path, PathBuf};
use crate::model::Ledger;
use crate::untrusted;
fn create_private_dir(dir: &Path) -> io::Result<()> {
if dir.exists() {
return Ok(());
}
#[cfg(unix)]
{
use std::os::unix::fs::DirBuilderExt;
fs::DirBuilder::new()
.recursive(true)
.mode(0o700)
.create(dir)
}
#[cfg(not(unix))]
{
fs::create_dir_all(dir)
}
}
fn private_file() -> OpenOptions {
let mut o = OpenOptions::new();
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
o.mode(0o600);
}
o
}
pub fn append_private(path: &Path, line: &str) -> io::Result<()> {
create_private_dir(parent_dir(path))?;
let mut f = private_file().append(true).create(true).open(path)?;
f.write_all(format!("{line}\n").as_bytes())
}
#[cfg(unix)]
fn make_private(path: &Path) {
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(path, fs::Permissions::from_mode(0o600));
}
#[cfg(not(unix))]
fn make_private(_path: &Path) {}
fn invalid_data(e: impl std::fmt::Display) -> io::Error {
io::Error::new(ErrorKind::InvalidData, e.to_string())
}
#[derive(Debug)]
pub struct LedgerLock {
_file: File,
}
fn sidecar(path: &Path, suffix: &str) -> PathBuf {
let mut s = path.as_os_str().to_owned();
s.push(suffix);
PathBuf::from(s)
}
fn parent_dir(path: &Path) -> &Path {
path.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or(Path::new("."))
}
pub fn lock(path: &Path) -> io::Result<LedgerLock> {
create_private_dir(parent_dir(path))?;
let file = private_file()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(sidecar(path, ".lock"))?;
file.lock()?;
Ok(LedgerLock { _file: file })
}
pub fn lock_within(path: &Path, wait: std::time::Duration) -> io::Result<LedgerLock> {
create_private_dir(parent_dir(path))?;
let file = private_file()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(sidecar(path, ".lock"))?;
let start = std::time::Instant::now();
loop {
match file.try_lock() {
Ok(()) => return Ok(LedgerLock { _file: file }),
Err(std::fs::TryLockError::WouldBlock) => {
if start.elapsed() >= wait {
return Err(io::Error::new(
ErrorKind::TimedOut,
"the ledger is busy (another ana process holds its lock)",
));
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
Err(std::fs::TryLockError::Error(e)) => return Err(e),
}
}
}
pub fn backup_path(path: &Path) -> PathBuf {
sidecar(path, ".bak")
}
pub fn lock_path(path: &Path) -> PathBuf {
sidecar(path, ".lock")
}
pub fn load(path: &Path) -> io::Result<Ledger> {
let text = match fs::read_to_string(path) {
Ok(text) => text,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Ledger::default()),
Err(e) => return Err(e),
};
if text.trim().is_empty() {
if backup_path(path).exists() {
return Err(io::Error::new(
ErrorKind::InvalidData,
"the ledger file is empty, but a backup of an earlier ledger sits beside it; refusing to start a new ledger over it",
));
}
return Ok(Ledger::default());
}
let ledger: Ledger = serde_json::from_str(&text).map_err(invalid_data)?;
validate(&ledger).map_err(|m| io::Error::new(ErrorKind::InvalidData, m))?;
Ok(ledger)
}
fn valid_id(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 64
&& id
.chars()
.all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))
}
fn validate(ledger: &Ledger) -> Result<(), String> {
for c in &ledger.claims {
if !valid_id(&c.id) {
return Err(format!(
"a claim has an id that is not a plain identifier ({}); ids are letters, digits, '.', '_' and '-', at most 64 characters",
untrusted::tag(&c.id)
));
}
for (i, f) in c.forecasts.iter().enumerate() {
let n = i + 1;
if let Some(p) = f.prob {
if !(0.0..=1.0).contains(&p) {
return Err(format!(
"claim [{}] forecast {n}: probability {p} is outside 0..1",
c.id
));
}
}
if let Some(iv) = f.interval {
if iv.low > iv.high {
return Err(format!(
"claim [{}] forecast {n}: interval {}..{} has its low end above its high end",
c.id, iv.low, iv.high
));
}
if iv.level <= 0.0 || iv.level >= 1.0 {
return Err(format!(
"claim [{}] forecast {n}: level {} is not strictly between 0 and 1",
c.id, iv.level
));
}
}
}
}
Ok(())
}
pub fn save(path: &Path, ledger: &Ledger) -> io::Result<()> {
let dir = parent_dir(path).to_path_buf();
create_private_dir(&dir)?;
let json = serde_json::to_vec_pretty(ledger).map_err(invalid_data)?;
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("ledger");
let tmp = dir.join(format!(".{name}.{}.{nanos}.tmp", std::process::id()));
{
let mut f = private_file().write(true).create_new(true).open(&tmp)?;
if let Err(e) = f.write_all(&json).and_then(|()| f.sync_all()) {
drop(f);
let _ = fs::remove_file(&tmp);
return Err(e);
}
}
if path.exists() {
let bak = backup_path(path);
if fs::copy(path, &bak).is_ok() {
make_private(&bak);
}
}
if let Err(e) = fs::rename(&tmp, path) {
let _ = fs::remove_file(&tmp);
return Err(e);
}
#[cfg(unix)]
File::open(&dir)?.sync_all()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{Claim, ClaimKind, Forecast, Outcome, Resolution};
use chrono::{TimeZone, Utc};
#[cfg(unix)]
#[test]
fn a_ledger_is_written_private_and_an_existing_directory_is_left_alone() {
use std::os::unix::fs::PermissionsExt;
let mode = |p: &Path| fs::metadata(p).unwrap().permissions().mode() & 0o777;
let root = std::env::temp_dir().join(format!("ana_private_{}", std::process::id()));
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(&root).unwrap();
fs::set_permissions(&root, fs::Permissions::from_mode(0o755)).unwrap();
let fresh = root.join("made-by-ana");
let path = fresh.join("l.json");
save(&path, &Ledger::default()).unwrap();
save(&path, &Ledger::default()).unwrap(); assert_eq!(mode(&fresh), 0o700);
assert_eq!(mode(&path), 0o600);
assert_eq!(mode(&backup_path(&path)), 0o600);
drop(lock(&path).unwrap()); assert_eq!(mode(&lock_path(&path)), 0o600);
let existing = root.join("l2.json");
save(&existing, &Ledger::default()).unwrap();
assert_eq!(
mode(&root),
0o755,
"an existing directory must be left as it was"
);
assert_eq!(mode(&existing), 0o600);
fs::set_permissions(&existing, fs::Permissions::from_mode(0o664)).unwrap();
save(&existing, &Ledger::default()).unwrap();
assert_eq!(mode(&existing), 0o600);
assert_eq!(
mode(&backup_path(&existing)),
0o600,
"the copy of the old file too"
);
let _ = fs::remove_dir_all(&root);
}
#[test]
fn a_bounded_lock_gives_up_instead_of_waiting_forever() {
let dir = std::env::temp_dir().join(format!("ana_lockwithin_{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
let path = dir.join("l.json");
let held = lock(&path).unwrap();
let t = std::time::Instant::now();
let err = lock_within(&path, std::time::Duration::from_millis(150))
.expect_err("the lock is held, so this must time out");
assert_eq!(err.kind(), ErrorKind::TimedOut);
assert!(
t.elapsed() < std::time::Duration::from_secs(2),
"{:?}",
t.elapsed()
);
drop(held);
assert!(lock_within(&path, std::time::Duration::from_millis(150)).is_ok());
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn missing_file_is_empty_ledger() {
let p = std::env::temp_dir().join("anamnesis_does_not_exist_xyz.json");
let _ = fs::remove_file(&p);
let l = load(&p).unwrap();
assert!(l.claims.is_empty());
}
#[test]
fn round_trip_preserves_everything() {
let dir = std::env::temp_dir().join(format!("anamnesis_test_{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let path = dir.join("ledger.json");
let now = Utc.with_ymd_and_hms(2025, 4, 2, 9, 30, 0).unwrap();
let ledger = Ledger {
claims: vec![Claim {
id: "abc123".into(),
statement: "It will rain tomorrow".into(),
created_at: now,
horizon_days: None,
resolve_by: Some(chrono::NaiveDate::from_ymd_opt(2025, 4, 3).unwrap()),
tags: vec!["weather".into()],
kind: ClaimKind::Binary,
stake: 1.0,
forecasts: vec![
Forecast {
at: now,
prob: Some(0.4),
interval: None,
because: Some("dry front".into()),
},
Forecast {
at: now,
prob: Some(0.7),
interval: None,
because: Some("front stalled".into()),
},
],
resolution: Some(Resolution {
at: now,
outcome: Some(Outcome::True),
value: None,
note: Some("the front stalled, as feared".into()),
resolved_by: None,
}),
void: None,
check: None,
extra: Default::default(),
amendments: Vec::new(),
}],
..Default::default()
};
save(&path, &ledger).unwrap();
let back = load(&path).unwrap();
assert_eq!(back.claims.len(), 1);
let c = &back.claims[0];
assert_eq!(c.id, "abc123");
assert_eq!(c.forecasts.len(), 2);
assert_eq!(c.current_prob(), Some(0.7));
assert_eq!(c.outcome(), Some(Outcome::True));
assert_eq!(c.tags, vec!["weather".to_string()]);
let _ = fs::remove_dir_all(&dir);
}
}