Skip to content

Commit 6eb597a

Browse files
authored
Revert "Assure copy_files_except_ext(..) won't copy directories into itself (#1135)" (#1181)
This reverts commit d7a2b29.
1 parent 5956820 commit 6eb597a

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

src/utils/fs.rs

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ pub fn copy_files_except_ext(
109109
return Ok(());
110110
}
111111

112-
// Is the destination inside of the source?
113-
if to.canonicalize()?.starts_with(from.canonicalize()?) {
114-
return Err(Error::from(format!(
115-
"Destination directory cannot be contained in source directory: '{}' is in '{}'",
116-
to.display(),
117-
from.display()
118-
)));
119-
}
120-
121112
for entry in fs::read_dir(from)? {
122113
let entry = entry?;
123114
let metadata = entry.metadata()?;
@@ -185,82 +176,64 @@ mod tests {
185176
use super::copy_files_except_ext;
186177
use std::fs;
187178

188-
#[test]
189-
fn it_fails_when_destination_is_in_source() {
190-
let tmp = match tempfile::TempDir::new() {
191-
Ok(t) => t,
192-
Err(e) => panic!("Could not create a temp dir: {}", e),
193-
};
194-
let dst = tmp.path().join("destination");
195-
fs::create_dir(&dst).unwrap();
196-
assert!(
197-
format!("{:?}", copy_files_except_ext(tmp.path(), &dst, true, &[]))
198-
.contains("Destination directory cannot be contained in source directory: ")
199-
);
200-
}
201-
202179
#[test]
203180
fn copy_files_except_ext_test() {
204-
let src = match tempfile::TempDir::new() {
181+
let tmp = match tempfile::TempDir::new() {
205182
Ok(t) => t,
206183
Err(e) => panic!("Could not create a temp dir: {}", e),
207184
};
208185

209186
// Create a couple of files
210-
if let Err(err) = fs::File::create(&src.path().join("file.txt")) {
187+
if let Err(err) = fs::File::create(&tmp.path().join("file.txt")) {
211188
panic!("Could not create file.txt: {}", err);
212189
}
213-
if let Err(err) = fs::File::create(&src.path().join("file.md")) {
190+
if let Err(err) = fs::File::create(&tmp.path().join("file.md")) {
214191
panic!("Could not create file.md: {}", err);
215192
}
216-
if let Err(err) = fs::File::create(&src.path().join("file.png")) {
193+
if let Err(err) = fs::File::create(&tmp.path().join("file.png")) {
217194
panic!("Could not create file.png: {}", err);
218195
}
219-
if let Err(err) = fs::create_dir(&src.path().join("sub_dir")) {
196+
if let Err(err) = fs::create_dir(&tmp.path().join("sub_dir")) {
220197
panic!("Could not create sub_dir: {}", err);
221198
}
222-
if let Err(err) = fs::File::create(&src.path().join("sub_dir/file.png")) {
199+
if let Err(err) = fs::File::create(&tmp.path().join("sub_dir/file.png")) {
223200
panic!("Could not create sub_dir/file.png: {}", err);
224201
}
225-
if let Err(err) = fs::create_dir(&src.path().join("sub_dir_exists")) {
202+
if let Err(err) = fs::create_dir(&tmp.path().join("sub_dir_exists")) {
226203
panic!("Could not create sub_dir_exists: {}", err);
227204
}
228-
if let Err(err) = fs::File::create(&src.path().join("sub_dir_exists/file.txt")) {
205+
if let Err(err) = fs::File::create(&tmp.path().join("sub_dir_exists/file.txt")) {
229206
panic!("Could not create sub_dir_exists/file.txt: {}", err);
230207
}
231208

232209
// Create output dir
233-
let dst = match tempfile::TempDir::new() {
234-
Ok(t) => t,
235-
Err(e) => panic!("Could not create a temp dir: {}", e),
236-
};
237-
if let Err(err) = fs::create_dir(&dst.path().join("output")) {
210+
if let Err(err) = fs::create_dir(&tmp.path().join("output")) {
238211
panic!("Could not create output: {}", err);
239212
}
240-
if let Err(err) = fs::create_dir(&dst.path().join("output/sub_dir_exists")) {
213+
if let Err(err) = fs::create_dir(&tmp.path().join("output/sub_dir_exists")) {
241214
panic!("Could not create output/sub_dir_exists: {}", err);
242215
}
243216

244217
if let Err(e) =
245-
copy_files_except_ext(&src.path(), &dst.path().join("output"), true, &["md"])
218+
copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, &["md"])
246219
{
247220
panic!("Error while executing the function:\n{:?}", e);
248221
}
249222

250223
// Check if the correct files where created
251-
if !(&dst.path().join("output/file.txt")).exists() {
224+
if !(&tmp.path().join("output/file.txt")).exists() {
252225
panic!("output/file.txt should exist")
253226
}
254-
if (&dst.path().join("output/file.md")).exists() {
227+
if (&tmp.path().join("output/file.md")).exists() {
255228
panic!("output/file.md should not exist")
256229
}
257-
if !(&dst.path().join("output/file.png")).exists() {
230+
if !(&tmp.path().join("output/file.png")).exists() {
258231
panic!("output/file.png should exist")
259232
}
260-
if !(&dst.path().join("output/sub_dir/file.png")).exists() {
233+
if !(&tmp.path().join("output/sub_dir/file.png")).exists() {
261234
panic!("output/sub_dir/file.png should exist")
262235
}
263-
if !(&dst.path().join("output/sub_dir_exists/file.txt")).exists() {
236+
if !(&tmp.path().join("output/sub_dir_exists/file.txt")).exists() {
264237
panic!("output/sub_dir/file.png should exist")
265238
}
266239
}

0 commit comments

Comments
 (0)