Skip to content

Commit e225586

Browse files
committed
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
1 parent cf7663f commit e225586

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/utils/fs.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn copy_files_except_ext(
110110

111111
for entry in fs::read_dir(from)? {
112112
let entry = entry?;
113-
let metadata = entry.metadata()?;
113+
let metadata = entry.path().metadata()?;
114114

115115
// If the entry is a dir and the recursive option is enabled, call itself
116116
if metadata.is_dir() && recursive {
@@ -187,7 +187,17 @@ pub fn get_404_output_file(input_404: &Option<String>) -> String {
187187
#[cfg(test)]
188188
mod tests {
189189
use super::copy_files_except_ext;
190-
use std::fs;
190+
use std::{fs, io::Result, path::Path};
191+
192+
#[cfg(target_os = "windows")]
193+
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> Result<()> {
194+
std::os::window::fs::symlink_file(src, dst)
195+
}
196+
197+
#[cfg(not(target_os = "windows"))]
198+
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> Result<()> {
199+
std::os::unix::fs::symlink(src, dst)
200+
}
191201

192202
#[test]
193203
fn copy_files_except_ext_test() {
@@ -218,6 +228,12 @@ mod tests {
218228
if let Err(err) = fs::File::create(&tmp.path().join("sub_dir_exists/file.txt")) {
219229
panic!("Could not create sub_dir_exists/file.txt: {}", err);
220230
}
231+
if let Err(err) = symlink(
232+
&tmp.path().join("file.png"),
233+
&tmp.path().join("symlink.png"),
234+
) {
235+
panic!("Could not symlink file.png: {}", err);
236+
}
221237

222238
// Create output dir
223239
if let Err(err) = fs::create_dir(&tmp.path().join("output")) {
@@ -249,5 +265,8 @@ mod tests {
249265
if !(&tmp.path().join("output/sub_dir_exists/file.txt")).exists() {
250266
panic!("output/sub_dir/file.png should exist")
251267
}
268+
if !(&tmp.path().join("output/symlink.png")).exists() {
269+
panic!("output/symlink.png should exist")
270+
}
252271
}
253272
}

0 commit comments

Comments
 (0)