Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid a possible recursive copy when destination (build_dir) is underneath source directory #1026

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions book-example/src/cli/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ 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.*
5 changes: 3 additions & 2 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(())
}
Expand Down
15 changes: 12 additions & 3 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ pub fn copy_files_except_ext(
from: &Path,
to: &Path,
recursive: bool,
avoid_dir: Option<&PathBuf>,
andrewdavidmackenzie marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -119,6 +121,12 @@ pub fn copy_files_except_ext(
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()))?;
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down