|
| 1 | +//! PFS-MS-aware compaction: rebuild a multi-session file into a fresh, |
| 2 | +//! single-session snapshot of its current state (spec Section 15). |
| 3 | +//! |
| 4 | +//! Generic PCF compaction (PCF Section 11.5, [`pcf::Container::compacted_image`]) |
| 5 | +//! MUST NOT be used on a PFS-MS file: it repacks entries into shared blocks and |
| 6 | +//! rewrites every `table_hash`, which destroys the one-`PFS_SESSION`-per-HEAD |
| 7 | +//! invariant and the inter-session hash commitments (`member_blocks_digest`, |
| 8 | +//! `prev_session_hash`). The result no longer scans or verifies as PFS-MS. |
| 9 | +//! |
| 10 | +//! Compaction here is therefore profile-aware. It resolves the live tree at the |
| 11 | +//! head and re-emits it as **one** session (`session_seq = 1`, |
| 12 | +//! `prev_session_hash = 0`). This is a full rewrite that *discards history*: |
| 13 | +//! |
| 14 | +//! * deleted nodes are gone — only live nodes are re-emitted; |
| 15 | +//! * every file is stored as fresh `Direct` (or `Empty`) content, collapsing |
| 16 | +//! any delta chain to the newest full version; |
| 17 | +//! * superseded versions and abandoned tails are reclaimed. |
| 18 | +//! |
| 19 | +//! The output is a fully valid, verifiable PFS-MS file. |
| 20 | +
|
| 21 | +use std::fs::{self, OpenOptions}; |
| 22 | +use std::io::{Read, Seek, Write}; |
| 23 | +use std::path::{Path, PathBuf}; |
| 24 | + |
| 25 | +use pcf::HashAlgo; |
| 26 | + |
| 27 | +use crate::error::{Error, Result}; |
| 28 | +use crate::fs::FsReader; |
| 29 | +use crate::tree::Tree; |
| 30 | +use crate::writer::{Change, FsWriter}; |
| 31 | +use crate::ROOT_NODE_ID; |
| 32 | + |
| 33 | +/// Rebuild the PFS-MS file in `src` into a fresh, single-session image written |
| 34 | +/// to `dst`, returning the destination handle. |
| 35 | +/// |
| 36 | +/// The resolved current tree of `src` becomes session 1 (`session_seq = 1`, |
| 37 | +/// `prev_session_hash = 0`); history is discarded (Section 15). The source is |
| 38 | +/// verified before any output is produced, so a corrupt input is rejected |
| 39 | +/// rather than propagated. `dst` must be a fresh, writable, empty handle. |
| 40 | +/// |
| 41 | +/// The whole source tree (every live file's content) is materialised in memory |
| 42 | +/// before `dst` is touched, so `src` and `dst` may be distinct handles to the |
| 43 | +/// same logical data without interfering. |
| 44 | +pub fn compact<R, W>(src: R, mut dst: W) -> Result<W> |
| 45 | +where |
| 46 | + R: Read + Write + Seek, |
| 47 | + W: Read + Write + Seek, |
| 48 | +{ |
| 49 | + let mut r = FsReader::open(src)?; |
| 50 | + // Refuse to compact a corrupt source (mirrors pcf-compact's verify-before). |
| 51 | + r.verify()?; |
| 52 | + |
| 53 | + let algo = source_hash_algo(&mut r)?; |
| 54 | + let tree = r.tree()?; |
| 55 | + let changes = collect_changes(&mut r, &tree)?; |
| 56 | + |
| 57 | + let mut w = FsWriter::create(&mut dst, algo)?; |
| 58 | + w.set_writer_id(b"pfs-compact"); |
| 59 | + w.set_compression(true); |
| 60 | + // An empty source tree yields no changes; `commit_changes` then commits |
| 61 | + // nothing and `dst` stays at the valid empty-table state from `create`. |
| 62 | + w.commit_changes(&changes)?; |
| 63 | + drop(w); |
| 64 | + |
| 65 | + Ok(dst) |
| 66 | +} |
| 67 | + |
| 68 | +/// Compact the PFS-MS file at `src` into `dst` on the host filesystem. |
| 69 | +/// |
| 70 | +/// When `dst == src` the file is compacted in place. Output is written to a |
| 71 | +/// sibling temp file, fsynced, and atomically renamed into place, so a crash |
| 72 | +/// leaves either the original or the fully written replacement. |
| 73 | +pub fn compact_archive(src: &Path, dst: &Path) -> Result<()> { |
| 74 | + // Build the compacted image in memory from the source. |
| 75 | + let image = { |
| 76 | + let in_file = OpenOptions::new() |
| 77 | + .read(true) |
| 78 | + .write(true) |
| 79 | + .open(src) |
| 80 | + .map_err(Error::Io)?; |
| 81 | + let out = compact(in_file, std::io::Cursor::new(Vec::new()))?; |
| 82 | + out.into_inner() |
| 83 | + }; |
| 84 | + |
| 85 | + // Write to a sibling temp file, fsync, then atomically rename into place. |
| 86 | + let dir = dst.parent().filter(|p| !p.as_os_str().is_empty()); |
| 87 | + let tmp: PathBuf = { |
| 88 | + let name = dst |
| 89 | + .file_name() |
| 90 | + .map(|n| n.to_string_lossy().into_owned()) |
| 91 | + .unwrap_or_else(|| "pfs".into()); |
| 92 | + let pid = std::process::id(); |
| 93 | + let tmp_name = format!(".{name}.pfs-compact.tmp.{pid}"); |
| 94 | + match dir { |
| 95 | + Some(d) => d.join(tmp_name), |
| 96 | + None => PathBuf::from(tmp_name), |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + let mut f = OpenOptions::new() |
| 101 | + .read(true) |
| 102 | + .write(true) |
| 103 | + .create(true) |
| 104 | + .truncate(true) |
| 105 | + .open(&tmp) |
| 106 | + .map_err(Error::Io)?; |
| 107 | + f.write_all(&image).map_err(Error::Io)?; |
| 108 | + f.sync_all().map_err(Error::Io)?; |
| 109 | + drop(f); |
| 110 | + |
| 111 | + fs::rename(&tmp, dst).map_err(|e| { |
| 112 | + let _ = fs::remove_file(&tmp); |
| 113 | + Error::Io(e) |
| 114 | + })?; |
| 115 | + Ok(()) |
| 116 | +} |
| 117 | + |
| 118 | +/// The table-hash algorithm of the source's head session (`Sha256` if empty). |
| 119 | +fn source_hash_algo<S: Read + Write + Seek>(r: &mut FsReader<S>) -> Result<HashAlgo> { |
| 120 | + let scan = r.scan()?; |
| 121 | + Ok(scan |
| 122 | + .sessions |
| 123 | + .first() |
| 124 | + .map(|s| s.block_hashes[0].2) |
| 125 | + .unwrap_or(HashAlgo::Sha256)) |
| 126 | +} |
| 127 | + |
| 128 | +/// Build the change set re-creating the whole live tree in one session. |
| 129 | +fn collect_changes<S: Read + Write + Seek>( |
| 130 | + r: &mut FsReader<S>, |
| 131 | + tree: &Tree, |
| 132 | +) -> Result<Vec<Change>> { |
| 133 | + let mut out = Vec::new(); |
| 134 | + walk(r, tree, ROOT_NODE_ID, "", &mut out)?; |
| 135 | + Ok(out) |
| 136 | +} |
| 137 | + |
| 138 | +fn walk<S: Read + Write + Seek>( |
| 139 | + r: &mut FsReader<S>, |
| 140 | + tree: &Tree, |
| 141 | + node: [u8; 16], |
| 142 | + prefix: &str, |
| 143 | + out: &mut Vec<Change>, |
| 144 | +) -> Result<()> { |
| 145 | + let kids = match tree.children.get(&node) { |
| 146 | + Some(k) => k.clone(), |
| 147 | + None => return Ok(()), |
| 148 | + }; |
| 149 | + for cid in kids { |
| 150 | + let rec = tree.nodes.get(&cid).ok_or(Error::NotFound)?; |
| 151 | + let name = rec.name_str(); |
| 152 | + let rel = if prefix.is_empty() { |
| 153 | + name |
| 154 | + } else { |
| 155 | + format!("{prefix}/{name}") |
| 156 | + }; |
| 157 | + if rec.is_dir() { |
| 158 | + // Emit every directory (preserving empty ones), then recurse. |
| 159 | + out.push(Change::Mkdir { |
| 160 | + path: rel.clone(), |
| 161 | + mode: rec.mode, |
| 162 | + mtime_unix_ms: rec.mtime_unix_ms, |
| 163 | + }); |
| 164 | + walk(r, tree, cid, &rel, out)?; |
| 165 | + } else { |
| 166 | + // Reconstruct the full current content; re-emitted as Direct/Empty. |
| 167 | + let (mode, mtime) = (rec.mode, rec.mtime_unix_ms); |
| 168 | + let content = r.read_path(&rel)?; |
| 169 | + out.push(Change::PutFile { |
| 170 | + path: rel, |
| 171 | + content, |
| 172 | + mode, |
| 173 | + mtime_unix_ms: mtime, |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
| 177 | + Ok(()) |
| 178 | +} |
0 commit comments