| 246 | } |
| 247 | |
| 248 | fn collect_snapshot(base: &Path, path: &Path, snapshot: &mut BTreeMap<String, Vec<u8>>) { |
| 249 | let metadata = fs::symlink_metadata(path) |
| 250 | .unwrap_or_else(|error| panic!("snapshot metadata for {}: {error}", path.display())); |
| 251 | let relative = path |
| 252 | .strip_prefix(base) |
| 253 | .expect("snapshot relative path") |
| 254 | .to_string_lossy() |
| 255 | .into_owned(); |
| 256 | if metadata.file_type().is_symlink() { |
| 257 | snapshot.insert( |
| 258 | format!("link:{relative}"), |
| 259 | fs::read_link(path) |
| 260 | .expect("read snapshot symlink") |
| 261 | .to_string_lossy() |
| 262 | .as_bytes() |
| 263 | .to_vec(), |
| 264 | ); |
| 265 | } else if metadata.is_file() { |
| 266 | // redb 4.2 writes a graceful-shutdown marker into atomic.redb on |
| 267 | // every clean close, so the file's bytes differ between opens even |
| 268 | // when no state changed. That internal marker is not a mutation of |
| 269 | // repository state; record its size so growth is still caught and |
| 270 | // rely on the change store, shim journal, and op-log snapshots |
| 271 | // for semantic tamper evidence. |
| 272 | if relative.ends_with("atomic.redb") { |
| 273 | snapshot.insert( |
| 274 | format!("file-size:{relative}"), |
| 275 | metadata.len().to_le_bytes().to_vec(), |
| 276 | ); |
| 277 | } else { |
| 278 | snapshot.insert( |
| 279 | format!("file:{relative}"), |
| 280 | fs::read(path).expect("snapshot file"), |
| 281 | ); |
| 282 | } |
| 283 | } else if metadata.is_dir() { |
| 284 | snapshot.insert(format!("dir:{relative}"), Vec::new()); |
| 285 | let mut children: Vec<PathBuf> = fs::read_dir(path) |
| 286 | .expect("snapshot directory") |
| 287 | .map(|entry| entry.expect("snapshot entry").path()) |
| 288 | .collect(); |
| 289 | children.sort(); |
| 290 | for child in children { |
| 291 | collect_snapshot(base, &child, snapshot); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | fn snapshot_worktree(root: &Path) -> BTreeMap<String, Vec<u8>> { |
| 297 | let mut snapshot = BTreeMap::new(); |