Files
tessera/crates/tessera-providers/src/filesystem.rs
Tessera Agent a5ffb2c1b4 Initial Tessera backup vault (M0–M2)
Scaffold Rust workspace, React canvas, vault engine, hub API,
providers, agent wormhole path, and Docker compose per SPEC.
2026-11-09 09:59:42 +00:00

84 lines
2.9 KiB
Rust

use super::{BackupProvider, CaptureResult};
use anyhow::{Context, Result};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::json;
use std::path::PathBuf;
use tessera_core::config::ProviderKind;
use walkdir::WalkDir;
#[derive(Debug, Clone, Deserialize)]
pub struct FilesystemConfig {
pub paths: Vec<String>,
#[serde(default)]
pub max_file_bytes: Option<u64>,
}
pub struct FilesystemProvider {
config: FilesystemConfig,
}
impl FilesystemProvider {
pub fn from_config(config: &serde_json::Value) -> Result<Self> {
let config: FilesystemConfig = serde_json::from_value(config.clone())?;
Ok(Self { config })
}
}
#[async_trait]
impl BackupProvider for FilesystemProvider {
fn kind(&self) -> ProviderKind {
ProviderKind::Filesystem
}
async fn capture(&self) -> Result<CaptureResult> {
let mut archive = Vec::new();
let mut paths_meta = Vec::new();
for path_str in &self.config.paths {
let root = PathBuf::from(path_str);
if !root.exists() {
anyhow::bail!("path not found: {path_str}");
}
if root.is_file() {
let data = tokio::fs::read(&root).await.context("read file")?;
if let Some(max) = self.config.max_file_bytes {
if data.len() as u64 > max {
anyhow::bail!("file exceeds max_file_bytes");
}
}
archive.extend_from_slice(path_str.as_bytes());
archive.push(0);
archive.extend(&(data.len() as u64).to_le_bytes());
archive.extend(&data);
paths_meta.push(path_str.clone());
} else {
for entry in WalkDir::new(&root).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file() {
let p = entry.path();
let rel = p.strip_prefix(&root).unwrap_or(p);
let data = tokio::fs::read(p).await.context("read file")?;
if let Some(max) = self.config.max_file_bytes {
if data.len() as u64 > max {
continue;
}
}
let key = format!("{}/{}", path_str, rel.display());
archive.extend_from_slice(key.as_bytes());
archive.push(0);
archive.extend(&(data.len() as u64).to_le_bytes());
archive.extend(&data);
paths_meta.push(key);
}
}
}
}
Ok(CaptureResult {
payload: archive,
labels: vec!["filesystem".into()],
restore_hints: json!({ "paths": paths_meta, "format": "tessera-fs-v1" }),
})
}
}