#![allow(dead_code)]
use std::collections::BTreeMap;
use std::fs;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
pub fn scratch() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("fstx-test-")
.tempdir_in(env!("CARGO_TARGET_TMPDIR"))
.unwrap()
}
pub fn snapshot(root: &Path) -> BTreeMap<String, (bool, Vec<u8>, u64)> {
let mut out = BTreeMap::new();
walk(root, root, &mut out);
out
}
fn walk(root: &Path, dir: &Path, out: &mut BTreeMap<String, (bool, Vec<u8>, u64)>) {
for e in fs::read_dir(dir).unwrap() {
let e = e.unwrap();
let p = e.path();
let rel = p.strip_prefix(root).unwrap().to_string_lossy().into_owned();
if rel == ".fstx" {
continue;
}
let m = fs::symlink_metadata(&p).unwrap();
if m.is_dir() {
out.insert(rel, (true, Vec::new(), m.ino()));
walk(root, &p, out);
} else {
out.insert(rel, (false, fs::read(&p).unwrap_or_default(), m.ino()));
}
}
}
pub fn leftovers(root: &Path) -> Vec<String> {
match fs::read_dir(root.join(".fstx")) {
Ok(rd) => rd
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.filter(|n| n != "lock")
.collect(),
Err(_) => Vec::new(),
}
}