Skip to content

Commit

Permalink
fix: Prevent recursive copy
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Feb 17, 2024
1 parent 16f05f7 commit 7c51fa6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ keywords = ["copy", "recursive", "filesystem", "file"]
default = ["jwalk"]

[dependencies]
walkdir = "2.3"
walkdir = "2.4"
log = "0.4"
# rayon = "1.4.0"
jwalk = { version = "0.8", optional = true }

[dev-dependencies]
unzip = "0.1"
reqwest = { version = "0.11", features = ["blocking"] }
env_logger = "0.10"
env_logger = "0.11"
criterion = "0.5"

[[bench]]
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ impl CopyBuilder {
abs_dest.display()
);

'files: for entry in WalkDir::new(&abs_source).into_iter().filter_map(|e| e.ok()) {
'files: for entry in WalkDir::new(&abs_source)
.into_iter()
.filter_entry(|e| e.path() != abs_dest)
.filter_map(|e| e.ok())
{
let rel_dest = entry.path().strip_prefix(&abs_source).map_err(|e| {
Error::new(ErrorKind::Other, format!("Could not strip prefix: {:?}", e))
})?;
Expand Down
20 changes: 20 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ fn copy_basic() {
std::fs::remove_dir_all("dest").unwrap();
}


#[test]
fn copy_subdir() {
std::env::set_var("RUST_LOG", "debug");
let _ = env_logger::try_init();
create_dir_all("source/subdir").unwrap();
create_dir_all("source/this_should_copy").unwrap();
File::create("source/this_should_copy/file.doc").unwrap();
File::create("source/a.jpg").unwrap();
File::create("source/b.jpg").unwrap();
File::create("source/d.txt").unwrap();

CopyBuilder::new("source", "source/subdir")
.run()
.unwrap();

// std::fs::remove_dir_all("source").unwrap();
}


#[test]
fn copy_exclude() {
std::env::set_var("RUST_LOG", "DEBUG");
Expand Down

0 comments on commit 7c51fa6

Please sign in to comment.