use crate::iso_time::{format_iso_milliseconds, now_iso};
use crate::subvolume_error::MigrateError;
use serde_json::{Map, Value};
use std::collections::BTreeMap;
use std::io::Write;
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
const SCHEMA: u64 = 1;
const PRIVATE_FILE_MODE: u32 = 0o600;
const TEMPORARY_SUFFIX: &str = ".tmp";
fn is_leftover_name(candidate: &[u8], journal_name: &[u8]) -> bool {
let Some(after_name) = candidate.strip_prefix(journal_name) else { return false };
let Some(rest) = after_name.strip_prefix(TEMPORARY_SUFFIX.as_bytes()) else { return false };
if rest.is_empty() {
return true;
}
let Some(numbers) = rest.strip_prefix(b".") else { return false };
let mut parts = numbers.split(|byte: &u8| { return *byte == b'.'; });
let numeric = |part: Option<&[u8]>| -> bool { return part.is_some_and(|digits: &[u8]| { return !digits.is_empty() && digits.iter().all(u8::is_ascii_digit); }); };
let process: Option<&[u8]> = parts.next();
let clock: Option<&[u8]> = parts.next();
return numeric(process) && numeric(clock) && parts.next().is_none();
}
fn write_exclusively(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
let mut file: std::fs::File = std::fs::OpenOptions::new().write(true).create_new(true).mode(PRIVATE_FILE_MODE).open(path)?;
let written: std::io::Result<()> = file.write_all(bytes).and_then(|()| { return file.sync_all(); });
if let Err(error) = written {
let _removed: std::io::Result<()> = std::fs::remove_file(path);
return Err(error);
}
return Ok(());
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Journal {
path: PathBuf,
started: String,
states: BTreeMap<String, String>,
times: BTreeMap<String, i64>,
persisted: bool,
}
impl Journal {
pub fn load(path: &Path) -> Result<Journal, MigrateError> {
let text: String = match std::fs::read_to_string(path) {
Ok(found) => found,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(Journal { path: path.to_path_buf(), started: now_iso(), states: BTreeMap::new(), times: BTreeMap::new(), persisted: false });
}
Err(error) => return Err(MigrateError::io("read the journal", path, &error)),
};
let broken = |why: &str| { return MigrateError::Journal(format!("the journal {} {why}; read it, and delete it only if no migration is in progress", path.display())); };
let value: Value = serde_json::from_str(text.as_str()).map_err(|error: serde_json::Error| { return broken(format!("is not valid JSON ({error})").as_str()); })?;
if value.get("schema").and_then(Value::as_u64) != Some(SCHEMA) {
return Err(broken("has an unknown schema version"));
}
let started: String = value.get("started").and_then(Value::as_str).ok_or_else(|| { return broken("has no start time"); })?.to_string();
let steps: &Map<String, Value> = value.get("steps").and_then(Value::as_object).ok_or_else(|| { return broken("has no steps"); })?;
let mut states: BTreeMap<String, String> = BTreeMap::new();
let mut times: BTreeMap<String, i64> = BTreeMap::new();
for (name, record) in steps {
let state: &str = record.get("state").and_then(Value::as_str).ok_or_else(|| { return broken("has a step without a state"); })?;
states.insert(name.clone(), state.to_string());
if let Some(milliseconds) = record.get("epoch_ms").and_then(Value::as_i64) {
times.insert(name.clone(), milliseconds);
}
}
return Ok(Journal { path: path.to_path_buf(), started, states, times, persisted: true });
}
pub fn started(&self) -> &str {
return self.started.as_str();
}
pub fn path(&self) -> &Path {
return self.path.as_path();
}
pub fn is_persisted(&self) -> bool {
return self.persisted;
}
pub fn seconds_of(&self, step: &str) -> Option<i64> {
return self.times.get(step).map(|milliseconds: &i64| { return milliseconds.div_euclid(1000); });
}
pub fn state(&self, step: &str) -> Option<&str> {
return self.states.get(step).map(String::as_str);
}
pub fn is_done(&self, step: &str) -> bool {
return self.state(step) == Some("done");
}
pub fn has_begun(&self, step: &str) -> bool {
return matches!(self.state(step), Some("started" | "done"));
}
pub fn mark(&mut self, step: &str, state: &str) -> Result<(), MigrateError> {
self.states.insert(step.to_string(), state.to_string());
let now: i64 = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |elapsed: std::time::Duration| { return i64::try_from(elapsed.as_millis()).unwrap_or(i64::MAX); });
self.times.insert(step.to_string(), now);
let mut steps: Map<String, Value> = Map::new();
for (name, recorded) in &self.states {
let milliseconds: i64 = self.times.get(name).copied().unwrap_or(now);
let mut record: Map<String, Value> = Map::new();
record.insert(String::from("state"), Value::String(recorded.clone()));
record.insert(String::from("at"), Value::String(format_iso_milliseconds(milliseconds)));
record.insert(String::from("epoch_ms"), Value::from(milliseconds));
steps.insert(name.clone(), Value::Object(record));
}
let mut document: Map<String, Value> = Map::new();
document.insert(String::from("schema"), Value::from(SCHEMA));
document.insert(String::from("started"), Value::String(self.started.clone()));
document.insert(String::from("steps"), Value::Object(steps));
let text: String = serde_json::to_string_pretty(&Value::Object(document)).unwrap_or_default();
let sibling_path: PathBuf = self.temporary_path();
let write = || -> std::io::Result<()> {
write_exclusively(sibling_path.as_path(), text.as_bytes())?;
if let Err(error) = std::fs::rename(&sibling_path, &self.path) {
let _removed: std::io::Result<()> = std::fs::remove_file(&sibling_path);
return Err(error);
}
return match self.path.parent() {
Some(directory) => std::fs::File::open(directory).and_then(|held: std::fs::File| { return held.sync_all(); }),
None => Ok(()),
};
};
write().map_err(|error: std::io::Error| { return MigrateError::io("write the journal", self.path.as_path(), &error); })?;
self.persisted = true;
self.remove_leftovers();
return Ok(());
}
fn temporary_path(&self) -> PathBuf {
let nanos: u128 = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |elapsed: std::time::Duration| { return elapsed.as_nanos(); });
let mut name: std::ffi::OsString = self.path.as_os_str().to_os_string();
name.push(format!("{TEMPORARY_SUFFIX}.{}.{nanos}", std::process::id()));
return PathBuf::from(name);
}
fn remove_leftovers(&self) {
let (Some(directory), Some(journal_name)) = (self.path.parent(), self.path.file_name()) else { return };
let Ok(listing) = std::fs::read_dir(directory) else { return };
for item in listing.flatten() {
let name: std::ffi::OsString = item.file_name();
if !is_leftover_name(name.as_encoded_bytes(), journal_name.as_encoded_bytes()) {
continue;
}
let is_plain_file: bool = item.file_type().is_ok_and(|kind: std::fs::FileType| { return kind.is_file(); });
if is_plain_file {
let _removed: std::io::Result<()> = std::fs::remove_file(item.path());
}
}
}
pub fn delete(&mut self) -> Result<(), MigrateError> {
match std::fs::remove_file(&self.path) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(MigrateError::io("delete the journal", self.path.as_path(), &error)),
}
self.states.clear();
self.times.clear();
self.persisted = false;
return Ok(());
}
}
#[cfg(test)]
#[path = "subvolume_journal_tests.rs"]
mod tests;