Add an option to hard link files from static/ instead of copying. (#723)

* Add hard_link_static config option.

* Copy or hardlink file depending on an argument.

Modify the call sites for `copy_file` to account for the extra argument.

* Plug the config setting through to copy_file.

Don't apply the config option to theme's static directory.

* Update documentation.

* Backticks make no sense in this comment.

* Addressing PR comments.

* Be consistent with argument naming.
This commit is contained in:
Jakub Turski 2019-07-10 22:37:19 +01:00 committed by Vincent Prouillet
parent 6992e8a1c1
commit 8a737d71fb
6 changed files with 22 additions and 6 deletions

View file

@ -106,6 +106,8 @@ pub struct Config {
pub generate_rss: bool,
/// The number of articles to include in the RSS feed. Defaults to including all items.
pub rss_limit: Option<usize>,
/// If set, files from static/ will be hardlinked instead of copied to the output dir.
pub hard_link_static: bool,
pub taxonomies: Vec<Taxonomy>,
@ -280,6 +282,7 @@ impl Default for Config {
languages: Vec::new(),
generate_rss: false,
rss_limit: None,
hard_link_static: false,
taxonomies: Vec::new(),
compile_sass: false,
check_external_links: false,

View file

@ -597,11 +597,12 @@ impl Site {
copy_directory(
&self.base_path.join("themes").join(theme).join("static"),
&self.output_path,
false
)?;
}
// We're fine with missing static folders
if self.static_path.exists() {
copy_directory(&self.static_path, &self.output_path)?;
copy_directory(&self.static_path, &self.output_path, self.config.hard_link_static)?;
}
Ok(())

View file

@ -95,7 +95,7 @@ pub fn find_related_assets(path: &Path) -> Vec<PathBuf> {
/// Copy a file but takes into account where to start the copy as
/// there might be folders we need to create on the way
pub fn copy_file(src: &Path, dest: &PathBuf, base_path: &PathBuf) -> Result<()> {
pub fn copy_file(src: &Path, dest: &PathBuf, base_path: &PathBuf, hard_link: bool) -> Result<()> {
let relative_path = src.strip_prefix(base_path).unwrap();
let target_path = dest.join(relative_path);
@ -103,11 +103,15 @@ pub fn copy_file(src: &Path, dest: &PathBuf, base_path: &PathBuf) -> Result<()>
create_dir_all(parent_directory)?;
}
copy(src, target_path)?;
if hard_link {
std::fs::hard_link(src, target_path)?
} else {
copy(src, target_path)?;
}
Ok(())
}
pub fn copy_directory(src: &PathBuf, dest: &PathBuf) -> Result<()> {
pub fn copy_directory(src: &PathBuf, dest: &PathBuf, hard_link: bool) -> Result<()> {
for entry in WalkDir::new(src).into_iter().filter_map(std::result::Result::ok) {
let relative_path = entry.path().strip_prefix(src).unwrap();
let target_path = dest.join(relative_path);
@ -117,7 +121,7 @@ pub fn copy_directory(src: &PathBuf, dest: &PathBuf) -> Result<()> {
create_directory(&target_path)?;
}
} else {
copy_file(entry.path(), dest, src)?;
copy_file(entry.path(), dest, src, hard_link)?;
}
}
Ok(())

View file

@ -41,6 +41,12 @@ generate_rss = false
# not set (the default).
# rss_limit = 20
# Whether to copy or hardlink files in static/ directory. Useful for sites
# whose static files are large. Note that for this to work, both static/ and
# output directory need to be on the same filesystem. Also, theme's static/
# files are always copies, regardles of this setting. False by default.
# hard_link_static = false
# The taxonomies to be rendered for that site and their configuration
# Example:
# taxonomies = [

View file

@ -38,6 +38,8 @@ The directory structure of the `sass` folder will be preserved when copying over
## `static`
Contains any kind of files. All the files/folders in the `static` folder will be copied as-is in the output directory.
If your static files are large you can configure Zola to [hard link](https://en.wikipedia.org/wiki/Hard_link) them
instead of copying by setting `hard_link_static = true` in the config file.
## `templates`
Contains all the [Tera](https://tera.netlify.com) templates that will be used to render this site.

View file

@ -325,7 +325,7 @@ pub fn serve(
} else {
rebuild_done_handling(
&broadcaster,
copy_file(&path, &site.output_path, &site.static_path),
copy_file(&path, &site.output_path, &site.static_path, site.config.hard_link_static),
&partial_path.to_string_lossy(),
);
}