From e225586953ae244736b3dbd0e12b2a9ffa927abb Mon Sep 17 00:00:00 2001 From: ifeanyi Date: Wed, 9 Sep 2020 17:42:14 +0200 Subject: [PATCH 1/2] Resolve full file paths when copying files Currently, the `copy_files` function doesn't support symlink files due to its use of `DirEntry::metadata` - To avoid this issue, this patch resolves the file path first before checking for metadata. Fixes #1157 --- src/utils/fs.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 7a7aa63a..16247435 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -110,7 +110,7 @@ pub fn copy_files_except_ext( for entry in fs::read_dir(from)? { let entry = entry?; - let metadata = entry.metadata()?; + let metadata = entry.path().metadata()?; // If the entry is a dir and the recursive option is enabled, call itself if metadata.is_dir() && recursive { @@ -187,7 +187,17 @@ pub fn get_404_output_file(input_404: &Option) -> String { #[cfg(test)] mod tests { use super::copy_files_except_ext; - use std::fs; + use std::{fs, io::Result, path::Path}; + + #[cfg(target_os = "windows")] + fn symlink, Q: AsRef>(src: P, dst: Q) -> Result<()> { + std::os::window::fs::symlink_file(src, dst) + } + + #[cfg(not(target_os = "windows"))] + fn symlink, Q: AsRef>(src: P, dst: Q) -> Result<()> { + std::os::unix::fs::symlink(src, dst) + } #[test] fn copy_files_except_ext_test() { @@ -218,6 +228,12 @@ mod tests { if let Err(err) = fs::File::create(&tmp.path().join("sub_dir_exists/file.txt")) { panic!("Could not create sub_dir_exists/file.txt: {}", err); } + if let Err(err) = symlink( + &tmp.path().join("file.png"), + &tmp.path().join("symlink.png"), + ) { + panic!("Could not symlink file.png: {}", err); + } // Create output dir if let Err(err) = fs::create_dir(&tmp.path().join("output")) { @@ -249,5 +265,8 @@ mod tests { if !(&tmp.path().join("output/sub_dir_exists/file.txt")).exists() { panic!("output/sub_dir/file.png should exist") } + if !(&tmp.path().join("output/symlink.png")).exists() { + panic!("output/symlink.png should exist") + } } } From b349e8abc91263d91c93c5e8804a18b38d4e3295 Mon Sep 17 00:00:00 2001 From: ifeanyi Date: Wed, 9 Sep 2020 17:57:45 +0200 Subject: [PATCH 2/2] Fix windows typo --- src/utils/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 16247435..dd1ec218 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -191,7 +191,7 @@ mod tests { #[cfg(target_os = "windows")] fn symlink, Q: AsRef>(src: P, dst: Q) -> Result<()> { - std::os::window::fs::symlink_file(src, dst) + std::os::windows::fs::symlink_file(src, dst) } #[cfg(not(target_os = "windows"))]