feat: add a iter_dir function to toplevel
This commit is contained in:
parent
60f3159ad0
commit
c8b1e0b333
25
src/main.rs
25
src/main.rs
@ -134,20 +134,33 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn copy_recursive(src: &PathBuf, dest: &PathBuf) -> anyhow::Result<()> {
|
fn copy_recursive(src: &PathBuf, dest: &PathBuf) -> anyhow::Result<()> {
|
||||||
|
println!("Copying {} to {}", src.display(), dest.display());
|
||||||
std::fs::create_dir_all(&dest)?;
|
std::fs::create_dir_all(&dest)?;
|
||||||
for item in std::fs::read_dir(src)? {
|
iter_dir(src, &|entry: &DirEntry| -> anyhow::Result<()> {
|
||||||
let entry = item?;
|
|
||||||
let file_type = entry.file_type()?;
|
|
||||||
let dest = &dest.join(entry.file_name());
|
let dest = &dest.join(entry.file_name());
|
||||||
if file_type.is_dir() {
|
let filetype = entry.file_type()?;
|
||||||
copy_recursive(&entry.path(), &dest)?;
|
if filetype.is_symlink() {
|
||||||
} else if file_type.is_symlink() {
|
|
||||||
let _ = std::fs::remove_file(&dest);
|
let _ = std::fs::remove_file(&dest);
|
||||||
std::fs::copy(std::fs::read_link(entry.path())?, &dest)?;
|
std::fs::copy(std::fs::read_link(entry.path())?, &dest)?;
|
||||||
} else {
|
} else {
|
||||||
let _ = std::fs::remove_file(&dest);
|
let _ = std::fs::remove_file(&dest);
|
||||||
std::fs::copy(&entry.path(), &dest)?;
|
std::fs::copy(&entry.path(), &dest)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iter_dir<F>(path: &PathBuf, phandler: &F) -> anyhow::Result<()>
|
||||||
|
where
|
||||||
|
F: Fn(&DirEntry) -> anyhow::Result<()>,
|
||||||
|
{
|
||||||
|
for item in std::fs::read_dir(path)? {
|
||||||
|
let entry = item?;
|
||||||
|
if entry.file_type()?.is_dir() {
|
||||||
|
iter_dir(&entry.path(), phandler)?;
|
||||||
|
} else {
|
||||||
|
phandler(&entry)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user