Skip to content

Commit

Permalink
builder: address comments ...
Browse files Browse the repository at this point in the history
* attempt to reduce `stat` calls
  • Loading branch information
liushuyu committed Apr 22, 2021
1 parent 1d5d533 commit a1d95aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/builder.rs
Expand Up @@ -584,15 +584,17 @@ fn append_dir_all(
mode: HeaderMode,
follow: bool,
) -> io::Result<()> {
let mut stack = vec![(src_path.to_path_buf(), true, false)];
while let Some((src, is_dir, is_symlink)) = stack.pop() {
let mut stack = vec![(src_path.to_path_buf(), true, fs::metadata(src_path)?)];
while let Some((src, is_dir, metadata)) = stack.pop() {
let dest = path.join(src.strip_prefix(&src_path).unwrap());
let file_type = metadata.file_type();
let is_symlink = file_type.is_symlink();
// In case of a symlink pointing to a directory, is_dir is false, but src.is_dir() will return true
if is_dir || (is_symlink && follow && src.is_dir()) {
for entry in fs::read_dir(&src)? {
let entry = entry?;
let file_type = entry.file_type()?;
stack.push((entry.path(), file_type.is_dir(), file_type.is_symlink()));
let metadata = entry.metadata()?;
stack.push((entry.path(), metadata.is_dir(), metadata));
}
if dest != Path::new("") {
append_dir(dst, &dest, &src, mode)?;
Expand All @@ -604,9 +606,8 @@ fn append_dir_all(
} else {
#[cfg(unix)]
{
let stat = fs::metadata(&src)?;
if !stat.is_file() {
append_special(dst, &dest, &stat, mode)?;
if !file_type.is_file() {
append_special(dst, &dest, &metadata, mode)?;
continue;
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/all.rs
Expand Up @@ -1176,7 +1176,11 @@ fn tar_directory_containing_special_files() {
// unfortunately, block device file cannot be created by non-root users
// as a substitute, just test the file that exists on most Unix systems
t!(env::set_current_dir("/dev/"));
t!(ar.append_path("loop0"));
if cfg!(linux) {
t!(ar.append_path("loop0"));
} else if cfg!(macos) {
t!(ar.append_path("disk0"));
}
// CI systems seem to have issues with creating a chr device
t!(ar.append_path("null"));
t!(ar.finish());
Expand Down

0 comments on commit a1d95aa

Please sign in to comment.