Skip to content

Commit 769cc0a

Browse files
authored
Merge pull request #1026 from andrewdavidmackenzie/master
Avoid a possible recursive copy when destination (build_dir) is underneath source directory
2 parents 6eb597a + c2686a8 commit 769cc0a

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

book-example/src/cli/build.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ not specified it will default to the value of the `build.build-dir` key in
3535

3636
-------------------
3737

38-
***Note:*** *Make sure to run the build command in the root directory and not in
39-
the source directory*
38+
***Note:*** *The build command copies all files (excluding files with `.md` extension) from the source directory
39+
into the build directory.*

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ impl Renderer for HtmlHandlebars {
291291
let src_dir = ctx.root.join(&ctx.config.book.src);
292292
let destination = &ctx.destination;
293293
let book = &ctx.book;
294+
let build_dir = ctx.root.join(&ctx.config.build.build_dir);
294295

295296
if destination.exists() {
296297
utils::fs::remove_dir_content(destination)
@@ -377,8 +378,8 @@ impl Renderer for HtmlHandlebars {
377378
}
378379
}
379380

380-
// Copy all remaining files
381-
utils::fs::copy_files_except_ext(&src_dir, &destination, true, &["md"])?;
381+
// Copy all remaining files, avoid a recursive copy from/to the book build dir
382+
utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?;
382383

383384
Ok(())
384385
}

src/utils/fs.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ pub fn copy_files_except_ext(
9595
from: &Path,
9696
to: &Path,
9797
recursive: bool,
98+
avoid_dir: Option<&PathBuf>,
9899
ext_blacklist: &[&str],
99100
) -> Result<()> {
100101
debug!(
101-
"Copying all files from {} to {} (blacklist: {:?})",
102+
"Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}",
102103
from.display(),
103104
to.display(),
104-
ext_blacklist
105+
ext_blacklist,
106+
avoid_dir
105107
);
106108

107109
// Check that from and to are different
@@ -119,6 +121,12 @@ pub fn copy_files_except_ext(
119121
continue;
120122
}
121123

124+
if let Some(avoid) = avoid_dir {
125+
if entry.path() == *avoid {
126+
continue;
127+
}
128+
}
129+
122130
// check if output dir already exists
123131
if !to.join(entry.file_name()).exists() {
124132
fs::create_dir(&to.join(entry.file_name()))?;
@@ -128,6 +136,7 @@ pub fn copy_files_except_ext(
128136
&from.join(entry.file_name()),
129137
&to.join(entry.file_name()),
130138
true,
139+
avoid_dir,
131140
ext_blacklist,
132141
)?;
133142
} else if metadata.is_file() {
@@ -215,7 +224,7 @@ mod tests {
215224
}
216225

217226
if let Err(e) =
218-
copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, &["md"])
227+
copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, None, &["md"])
219228
{
220229
panic!("Error while executing the function:\n{:?}", e);
221230
}

0 commit comments

Comments
 (0)