mod create; mod fsck; pub mod healthcheck; mod restore; mod retention; pub mod scheduler; mod verify; use std::fs::{DirBuilder, File}; use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt}; use std::path::{Path, PathBuf}; use forge_core::token::sha256_reader; use forge_core::{Error, Result, Store}; use forge_git::GitEnv; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; pub use create::create_backup; pub use fsck::{FsckSummary, run_fsck}; pub use restore::restore_backup; pub use scheduler::Jobs; pub use verify::verify_backup; pub const FORMAT_VERSION: u32 = 1; pub const MANIFEST_FILE: &str = "manifest.json"; pub const DB_FILE: &str = "db/forge.db"; pub const KEY_FILE: &str = "keys/forge.key"; const BACKUP_DIR_PREFIX: &str = "forge-backup-"; const PARTIAL_PREFIX: &str = ".partial-"; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Scope { All, ReposOnly, DbOnly, } #[derive(Clone)] pub struct BackupSource { pub store: Store, pub git: GitEnv, pub base_url: String, } #[derive(Debug, Clone)] pub struct BackupOptions { pub out: PathBuf, pub include_key: bool, pub scope: Scope, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct BackupSummary { pub path: PathBuf, pub scope: Scope, pub files: usize, pub bytes: u64, pub repos: usize, pub bundles: usize, pub empty_repos: usize, pub schema_version: Option, pub key_included: bool, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct VerifyReport { pub path: PathBuf, pub scope: Scope, pub files: usize, pub bytes: u64, pub repos: usize, pub bundles_verified: usize, pub empty_repos: usize, pub schema_version: Option, pub problems: Vec, } impl VerifyReport { pub fn is_ok(&self) -> bool { self.problems.is_empty() } } #[derive(Debug, Serialize, Deserialize)] pub struct Manifest { pub format: u32, pub forge_version: String, pub created_at: String, pub base_url: String, pub scope: Scope, pub db_snapshot: Option, pub key_included: bool, pub files: Vec, pub repos: Vec, } #[derive(Debug, Serialize, Deserialize)] pub struct DbSnapshot { pub method: String, pub schema_version: u32, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct FileEntry { pub path: String, pub bytes: u64, pub sha256: String, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct RepoEntry { pub id: String, pub owner: String, pub name: String, pub refs_sha256: String, pub bundle: Option, } fn io_err(e: std::io::Error) -> Error { Error::Io(e) } fn create_private_dir(path: &Path) -> Result<()> { DirBuilder::new().mode(0o700).create(path).map_err(io_err) } fn ensure_private_dir_all(path: &Path) -> Result<()> { DirBuilder::new() .recursive(true) .mode(0o700) .create(path) .map_err(io_err) } fn write_private_file(path: &Path, bytes: &[u8]) -> Result<()> { use std::io::Write; let mut f = std::fs::OpenOptions::new() .write(true) .create_new(true) .mode(0o600) .open(path) .map_err(io_err)?; f.write_all(bytes).map_err(io_err)?; f.sync_all().map_err(io_err) } fn set_private(path: &Path) -> Result<()> { std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).map_err(io_err) } fn fsync_dir(path: &Path) -> Result<()> { File::open(path).map_err(io_err)?.sync_all().map_err(io_err) } fn hash_file(path: &Path) -> Result<(u64, String)> { sha256_reader(File::open(path).map_err(io_err)?).map_err(io_err) } fn name_stamp(t: time::OffsetDateTime) -> String { stamp(t, "", "") } fn manifest_stamp(t: time::OffsetDateTime) -> String { stamp(t, "-", ":") } fn stamp(t: time::OffsetDateTime, date_sep: &str, time_sep: &str) -> String { format!( "{:04}{d}{:02}{d}{:02}T{:02}{s}{:02}{s}{:02}Z", t.year(), u8::from(t.month()), t.day(), t.hour(), t.minute(), t.second(), d = date_sep, s = time_sep, ) } fn unique_suffix() -> String { let nanos = time::OffsetDateTime::now_utc().unix_timestamp_nanos(); format!("{}-{nanos}", std::process::id()) } fn empty_sha256() -> String { hex::encode(Sha256::digest(b"")) } fn is_known_path(rel: &str) -> bool { if rel == DB_FILE || rel == KEY_FILE { return true; } rel.strip_prefix("repos/") .and_then(|name| name.strip_suffix(".bundle")) .map(|id| id.strip_suffix(".wiki").unwrap_or(id)) .is_some_and(|id| !id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric())) } fn sorted(mut files: Vec) -> Vec { files.sort_by(|a, b| a.path.cmp(&b.path)); files } #[cfg(test)] mod tests;