Browse Source

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.
index-subcmd
Jakub Turski Vincent Prouillet 4 years ago
parent
commit
8a737d71fb
6 changed files with 22 additions and 6 deletions
  1. +3
    -0
      components/config/src/config.rs
  2. +2
    -1
      components/site/src/lib.rs
  3. +8
    -4
      components/utils/src/fs.rs
  4. +6
    -0
      docs/content/documentation/getting-started/configuration.md
  5. +2
    -0
      docs/content/documentation/getting-started/directory-structure.md
  6. +1
    -1
      src/cmd/serve.rs

+ 3
- 0
components/config/src/config.rs 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,


+ 2
- 1
components/site/src/lib.rs 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(())


+ 8
- 4
components/utils/src/fs.rs 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(())


+ 6
- 0
docs/content/documentation/getting-started/configuration.md 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 = [


+ 2
- 0
docs/content/documentation/getting-started/directory-structure.md 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.


+ 1
- 1
src/cmd/serve.rs 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(),
);
}


Loading…
Cancel
Save