use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use super::CommitId;
use crate::backend::BackendError;
use crate::backend::MemBackend;
use crate::filesystem::changelog::{
self, ChangeEntry, MutationKind, changelog_path, parse_rfc3339_utc,
};
use crate::provenance::{Provenance, ProvenanceKind};
use crate::vcs::{Actor, CommitContext, parse_client_id};
enum PendingState {
Upsert(Vec<u8>),
Delete,
}
struct Pending {
ops: HashMap<String, PendingState>,
}
impl Pending {
fn new() -> Self {
Self {
ops: HashMap::new(),
}
}
fn clear(&mut self) {
self.ops.clear();
}
}
pub struct FilesystemBackend {
root: PathBuf,
pending: Mutex<Pending>,
}
impl FilesystemBackend {
pub fn new(root: PathBuf) -> Self {
Self {
root,
pending: Mutex::new(Pending::new()),
}
}
fn read_source(
&self,
pending: &Pending,
rel_key: &str,
) -> Result<Option<Vec<u8>>, BackendError> {
if let Some(PendingState::Upsert(bytes)) = pending.ops.get(rel_key) {
return Ok(Some(bytes.clone()));
}
if let Some(PendingState::Delete) = pending.ops.get(rel_key) {
return Ok(None);
}
let full = self.root.join(rel_key);
match std::fs::read(&full) {
Ok(bytes) => Ok(Some(bytes)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(BackendError::Io(e)),
}
}
}
pub(crate) fn normalise_rel_path(rel_path: &Path) -> Result<String, BackendError> {
if rel_path.as_os_str().is_empty() {
return Err(BackendError::Path("mem-relative path is empty".to_string()));
}
let mut parts: Vec<String> = Vec::new();
for component in rel_path.components() {
use std::path::Component;
match component {
Component::Normal(s) => match s.to_str() {
Some(p) if !p.is_empty() => parts.push(p.to_string()),
_ => {
return Err(BackendError::Path(format!(
"non-utf-8 or empty path component in {}",
rel_path.display()
)));
}
},
Component::CurDir => continue,
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
return Err(BackendError::Path(format!(
"path traversal or absolute component in {}",
rel_path.display()
)));
}
}
}
if parts.is_empty() {
return Err(BackendError::Path(
"mem-relative path is empty after normalisation".to_string(),
));
}
Ok(parts.join("/"))
}
impl crate::backend::MemBackend for FilesystemBackend {
fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
let mut out = Vec::new();
if !self.root.exists() {
return Ok(out);
}
walk_for_md(&self.root, &self.root, &mut out)?;
Ok(out)
}
fn current_head(&self) -> Result<Option<String>, BackendError> {
let log_path = crate::filesystem::changelog::changelog_path(&self.root);
let raw = match std::fs::read_to_string(&log_path) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => {
return Err(BackendError::Other(format!(
"reading folder changelog {}: {e}",
log_path.display()
)));
}
};
let last_ts = raw
.lines()
.rev()
.filter_map(|line| {
let trimmed = line.trim();
if trimmed.is_empty() {
return None;
}
serde_json::from_str::<serde_json::Value>(trimmed)
.ok()?
.get("ts")?
.as_str()
.map(str::to_string)
})
.next();
Ok(last_ts)
}
fn read_entity(&self, rel_path: &Path) -> Result<Option<Vec<u8>>, BackendError> {
let key = normalise_rel_path(rel_path)?;
let pending = self.pending.lock().map_err(|_| {
BackendError::Other("filesystem writer pending state poisoned".to_string())
})?;
if let Some(state) = pending.ops.get(&key) {
return Ok(match state {
PendingState::Upsert(bytes) => Some(bytes.clone()),
PendingState::Delete => None,
});
}
let full = self.root.join(&key);
match std::fs::read(&full) {
Ok(bytes) => Ok(Some(bytes)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(BackendError::Io(e)),
}
}
fn storage_present(&self) -> Result<bool, BackendError> {
Ok(self.root.is_dir())
}
fn entity_exists(&self, rel_path: &Path) -> Result<bool, BackendError> {
let key = normalise_rel_path(rel_path)?;
let pending = self.pending.lock().map_err(|_| {
BackendError::Other("filesystem writer pending state poisoned".to_string())
})?;
if let Some(state) = pending.ops.get(&key) {
return Ok(matches!(state, PendingState::Upsert(_)));
}
drop(pending);
match std::fs::symlink_metadata(self.root.join(&key)) {
Ok(md) => Ok(md.is_file()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(BackendError::Io(e)),
}
}
fn write_entity(&self, rel_path: &Path, content: &[u8]) -> Result<(), BackendError> {
let key = normalise_rel_path(rel_path)?;
let mut pending = self.pending.lock().map_err(|_| {
BackendError::Path("filesystem writer pending state poisoned".to_string())
})?;
pending
.ops
.insert(key, PendingState::Upsert(content.to_vec()));
Ok(())
}
fn delete_entity(&self, rel_path: &Path) -> Result<(), BackendError> {
let key = normalise_rel_path(rel_path)?;
let mut pending = self.pending.lock().map_err(|_| {
BackendError::Path("filesystem writer pending state poisoned".to_string())
})?;
pending.ops.insert(key, PendingState::Delete);
Ok(())
}
fn move_entity(&self, from: &Path, to: &Path) -> Result<(), BackendError> {
let from_key = normalise_rel_path(from)?;
let to_key = normalise_rel_path(to)?;
let mut pending = self.pending.lock().map_err(|_| {
BackendError::Path("filesystem writer pending state poisoned".to_string())
})?;
let bytes = match pending.ops.remove(&from_key) {
Some(PendingState::Upsert(b)) => b,
Some(PendingState::Delete) => {
pending.ops.insert(from_key, PendingState::Delete);
return Err(BackendError::Path(format!(
"move source {} is already pending deletion",
from.display()
)));
}
None => match self.read_source(&pending, &from_key)? {
Some(b) => b,
None => {
return Err(BackendError::Path(format!(
"move source {} does not exist",
from.display()
)));
}
},
};
if matches!(pending.ops.get(&to_key), Some(PendingState::Upsert(_))) {
return Err(BackendError::Path(format!(
"move target {} already has a pending write",
to.display()
)));
}
pending.ops.insert(from_key, PendingState::Delete);
pending.ops.insert(to_key, PendingState::Upsert(bytes));
Ok(())
}
fn discard_pending(&self) -> Result<(), BackendError> {
let mut pending = self.pending.lock().map_err(|_| {
BackendError::Other("filesystem writer pending state poisoned".to_string())
})?;
pending.clear();
Ok(())
}
fn commit(&self, _message: &str, _ctx: &CommitContext<'_>) -> Result<CommitId, BackendError> {
let mut pending = self.pending.lock().map_err(|_| {
BackendError::Path("filesystem writer pending state poisoned".to_string())
})?;
for (key, state) in pending.ops.iter() {
let target = self.root.join(key);
match state {
PendingState::Upsert(bytes) => atomic_write(&target, bytes)?,
PendingState::Delete => match std::fs::remove_file(&target) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(BackendError::Io(e)),
},
}
}
pending.clear();
Ok(make_commit_id())
}
fn read_mem_config(&self) -> Result<Option<Vec<u8>>, BackendError> {
let config_path = self.root.join(crate::mem::MEM_META_DIR).join("config.json");
match std::fs::read(&config_path) {
Ok(bytes) => Ok(Some(bytes)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(BackendError::Io(e)),
}
}
fn write_mem_config(
&self,
bytes: &[u8],
_ctx: &crate::vcs::CommitContext<'_>,
) -> Result<(), BackendError> {
let memstead_dir = self.root.join(crate::mem::MEM_META_DIR);
std::fs::create_dir_all(&memstead_dir).map_err(BackendError::Io)?;
let config_path = memstead_dir.join("config.json");
std::fs::write(&config_path, bytes).map_err(BackendError::Io)
}
fn write_mem_config_cas(
&self,
expected: Option<&[u8]>,
bytes: &[u8],
_ctx: &crate::vcs::CommitContext<'_>,
) -> Result<bool, BackendError> {
let memstead_dir = self.root.join(crate::mem::MEM_META_DIR);
std::fs::create_dir_all(&memstead_dir).map_err(BackendError::Io)?;
let config_path = memstead_dir.join("config.json");
let lock_path = memstead_dir.join("config.json.lock");
let mut held = None;
for attempt in 0..50 {
match std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&lock_path)
{
Ok(f) => {
held = Some(f);
break;
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
if attempt == 49 {
let _ = std::fs::remove_file(&lock_path);
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(e) => return Err(BackendError::Io(e)),
}
}
let _lock = match held {
Some(f) => f,
None => std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&lock_path)
.map_err(BackendError::Io)?,
};
let release = || {
let _ = std::fs::remove_file(&lock_path);
};
let current = match std::fs::read(&config_path) {
Ok(b) => Some(b),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(e) => {
release();
return Err(BackendError::Io(e));
}
};
if let Some(expected) = expected
&& current.as_deref() != Some(expected)
{
release();
return Ok(false);
}
let result = std::fs::write(&config_path, bytes).map_err(BackendError::Io);
release();
result.map(|_| true)
}
fn read_anchors_sidecar(&self) -> Result<Option<Vec<u8>>, BackendError> {
self.read_entity(Path::new(crate::anchor::ANCHOR_SIDECAR_PATH))
}
fn write_anchors_sidecar(&self, bytes: &[u8]) -> Result<(), BackendError> {
<Self as MemBackend>::write_entity(
self,
Path::new(crate::anchor::ANCHOR_SIDECAR_PATH),
bytes,
)
}
fn append_provenance(&self, record: &Provenance) -> Result<(), BackendError> {
let kind: MutationKind = record.kind.into();
let entry = ChangeEntry {
kind,
entity: record.entity.as_deref(),
actor: record.actor,
client: record.client.as_ref(),
note: record.note.as_deref(),
logical_operation_id: record.logical_operation_id.as_deref(),
role: record.role,
identity: record.identity.as_deref(),
};
changelog::append_change_monotonic(&self.root, &entry, record.timestamp)
.map_err(|e| BackendError::Other(format!("changelog append: {e}")))
}
fn read_provenance(&self, cursor: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
let log_path = changelog_path(&self.root);
let raw = match std::fs::read_to_string(&log_path) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(BackendError::Io(e)),
};
let mut out = Vec::new();
for line in raw.lines() {
let trimmed = line.trim();
if trimmed.is_empty() {
continue;
}
let value: serde_json::Value = match serde_json::from_str(trimmed) {
Ok(v) => v,
Err(_) => continue,
};
let ts_str = value.get("ts").and_then(|v| v.as_str()).unwrap_or("");
if let Some(c) = cursor
&& ts_str <= c
{
continue;
}
let timestamp = parse_rfc3339_utc(ts_str).unwrap_or(std::time::UNIX_EPOCH);
let kind = value
.get("kind")
.and_then(|v| v.as_str())
.and_then(ProvenanceKind::parse)
.unwrap_or(ProvenanceKind::Update);
let entity = value
.get("entity")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let actor = value
.get("actor")
.and_then(|v| v.as_str())
.and_then(Actor::from_trailer)
.unwrap_or(Actor::Unknown);
let client = value
.get("client")
.and_then(|v| v.as_str())
.and_then(parse_client_id);
let note = value
.get("note")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let logical_operation_id = value
.get("logical_op")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let mut record = Provenance::new(timestamp, kind, entity, actor, client, note);
if let Some(id) = logical_operation_id {
record = record.with_logical_operation_id(id);
}
if let Some(role) = value
.get("role")
.and_then(|v| v.as_str())
.and_then(crate::vcs::Role::from_wire)
{
record = record.with_role(role);
}
record = record.with_identity(
value
.get("identity")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
);
out.push(record);
}
Ok(out)
}
}
fn walk_for_md(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<(), BackendError> {
let entries = match std::fs::read_dir(dir) {
Ok(e) => e,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(BackendError::Io(e)),
};
for entry in entries {
let entry = entry.map_err(BackendError::Io)?;
let path = entry.path();
let file_type = entry.file_type().map_err(BackendError::Io)?;
if file_type.is_dir() {
let name = entry.file_name();
if name == crate::mem::MEM_META_DIR {
continue;
}
walk_for_md(root, &path, out)?;
} else if file_type.is_file()
&& path.extension().and_then(|s| s.to_str()) == Some("md")
&& entry.file_name() != "README.md"
&& let Ok(rel) = path.strip_prefix(root)
{
out.push(rel.to_path_buf());
}
}
Ok(())
}
fn atomic_write(target: &Path, bytes: &[u8]) -> Result<(), BackendError> {
if let Some(parent) = target.parent()
&& !parent.as_os_str().is_empty()
{
std::fs::create_dir_all(parent).map_err(BackendError::Io)?;
}
let tmp = make_tmp_path(target);
std::fs::write(&tmp, bytes).map_err(BackendError::Io)?;
if let Err(e) = std::fs::rename(&tmp, target) {
let _ = std::fs::remove_file(&tmp);
return Err(BackendError::Io(e));
}
Ok(())
}
fn make_tmp_path(target: &Path) -> PathBuf {
let name = target
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| "_".to_string());
let suffix = unique_suffix();
let tmp_name = format!(".{name}.tmp.{suffix}");
target.with_file_name(tmp_name)
}
static TMP_COUNTER: AtomicU64 = AtomicU64::new(0);
static COMMIT_COUNTER: AtomicU64 = AtomicU64::new(0);
fn unique_suffix() -> String {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let counter = TMP_COUNTER.fetch_add(1, Ordering::Relaxed);
format!("{nanos:x}-{counter:x}")
}
pub(crate) fn make_commit_id() -> CommitId {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let counter = COMMIT_COUNTER.fetch_add(1, Ordering::Relaxed);
format!("{nanos:032x}{counter:016x}")
}
#[cfg(test)]
mod folder_drift_tests;
#[cfg(test)]
mod tests;