From bd14d0910aa24121222601da7701414f415e74ef Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Sat, 14 Sep 2019 21:14:13 +0100 Subject: [PATCH] Avoid a possible recursive copy when destination (build_dir) is underneath source directory. Update docs to match. --- book-example/src/cli/build.md | 5 +++-- src/renderer/html_handlebars/hbs_renderer.rs | 5 +++-- src/utils/fs.rs | 17 +++++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/book-example/src/cli/build.md b/book-example/src/cli/build.md index 5d218b66..7572cd40 100644 --- a/book-example/src/cli/build.md +++ b/book-example/src/cli/build.md @@ -35,5 +35,6 @@ not specified it will default to the value of the `build.build-dir` key in ------------------- -***Note:*** *Make sure to run the build command in the root directory and not in -the source directory* +***Note:*** *The build command copies all files (excluding files with `.md` extension) from the source directory +into the build directory. If the build directory is under the source directory it is not copied as this would +result in a recursive copy until your disk is full.* diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index bf27ec34..05afbdec 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -291,6 +291,7 @@ impl Renderer for HtmlHandlebars { let src_dir = ctx.root.join(&ctx.config.book.src); let destination = &ctx.destination; let book = &ctx.book; + let build_dir = ctx.root.join(&ctx.config.build.build_dir); if destination.exists() { utils::fs::remove_dir_content(destination) @@ -377,8 +378,8 @@ impl Renderer for HtmlHandlebars { } } - // Copy all remaining files - utils::fs::copy_files_except_ext(&src_dir, &destination, true, &["md"])?; + // Copy all remaining files, avoid a recursive copy from/to the book build dir + utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?; Ok(()) } diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 99456c0f..94edeb98 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -95,13 +95,15 @@ pub fn copy_files_except_ext( from: &Path, to: &Path, recursive: bool, + avoid_dir: Option<&PathBuf>, ext_blacklist: &[&str], ) -> Result<()> { debug!( - "Copying all files from {} to {} (blacklist: {:?})", + "Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}", from.display(), to.display(), - ext_blacklist + ext_blacklist, + avoid_dir ); // Check that from and to are different @@ -113,12 +115,18 @@ pub fn copy_files_except_ext( let entry = entry?; let metadata = entry.metadata()?; - // If the entry is a dir and the recursive option is enabled, call itself + // If the entry is a dir and the recursive option is enabled , call itself if metadata.is_dir() && recursive { if entry.path() == to.to_path_buf() { continue; } + if let Some(avoid) = avoid_dir { + if entry.path() == *avoid { + continue; + } + } + // check if output dir already exists if !to.join(entry.file_name()).exists() { fs::create_dir(&to.join(entry.file_name()))?; @@ -128,6 +136,7 @@ pub fn copy_files_except_ext( &from.join(entry.file_name()), &to.join(entry.file_name()), true, + avoid_dir, ext_blacklist, )?; } else if metadata.is_file() { @@ -215,7 +224,7 @@ mod tests { } if let Err(e) = - copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, &["md"]) + copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, None, &["md"]) { panic!("Error while executing the function:\n{:?}", e); }