Skip to main content

coven_foundation/files/
tree_copy.rs

1//! Tests' copies of an installation's directory tree, as a backup or a
2//! crash leaves it.
3
4use std::path::Path;
5
6/// Copy the tree at `from` into `to`, making `to` and every folder below
7/// it, as restoring a backup of an installation does.
8pub fn copy_tree(from: &Path, to: &Path) {
9    copy_tree_keeping(from, to, &|_| true);
10}
11
12/// Copy the tree at `from` into `to`, as `copy_tree` does, with only the
13/// files whose name `keep` accepts.
14pub fn copy_tree_keeping(from: &Path, to: &Path, keep: &dyn Fn(&str) -> bool) {
15    std::fs::create_dir_all(to).unwrap();
16    for entry in std::fs::read_dir(from).unwrap() {
17        let entry = entry.unwrap();
18        let name = entry.file_name().into_string().expect("a test file's name is Unicode");
19        let target = to.join(&name);
20        if entry.file_type().unwrap().is_dir() {
21            copy_tree_keeping(&entry.path(), &target, keep);
22        } else if keep(&name) {
23            std::fs::copy(entry.path(), target).unwrap();
24        }
25    }
26}