Skip to content

Commit

Permalink
Small bug fix
Browse files Browse the repository at this point in the history
If the traversed file is not a directory (symlinked or otherwise),
_only_ handle it if it's a regular file
  • Loading branch information
Xophmeister committed Jan 27, 2025
1 parent b3e40cd commit da67f0a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions topiary-cli/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use std::{
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

// TODO See FileMeta::new, below
// #[cfg(windows)]
// use std::os::windows::fs::MetadataExt;

enum FileType {
/// Regular file
File,
Expand All @@ -21,8 +25,12 @@ enum FileType {

#[allow(dead_code)]
enum Hardlink {
Links(u64),
// We know there's at least one link, but cannot say more than that
AtLeastOne,

// We know exactly how many links there are
// NOTE While technically possible, I would worry about a file that had 2^64 links
Exactly(u64),
}

struct FileMeta {
Expand Down Expand Up @@ -51,7 +59,7 @@ impl FileMeta {
};

#[cfg(unix)]
let hardlink = Hardlink::Links(meta.nlink());
let hardlink = Hardlink::Exactly(meta.nlink());

// TODO Windows has fs::MetadataExt::number_of_links, but this is experimental as of
// writing (see https://github.com/rust-lang/rust/issues/63010)
Expand All @@ -77,12 +85,16 @@ impl FileMeta {
matches!(self.filetype, FileType::Directory)
}

fn is_file(&self) -> bool {
matches!(self.filetype, FileType::File)
}

fn is_symlink(&self) -> bool {
self.symlink
}

fn has_multiple_links(&self) -> bool {
matches!(self.hardlink, Hardlink::Links(n) if n > 1)
matches!(self.hardlink, Hardlink::Exactly(n) if n > 1)
}
}

Expand Down Expand Up @@ -118,7 +130,7 @@ pub fn traverse(files: &mut Vec<PathBuf>, follow_symlinks: bool) -> CLIResult<()
let mut subfiles = file.read_dir()?.flatten().map(|f| f.path()).collect();
traverse(&mut subfiles, follow_symlinks)?;
expanded.append(&mut subfiles);
} else {
} else if meta.is_file() {
if meta.is_symlink() && !follow_symlinks {
log::debug!(
"{} is a symlink; use --follow-symlinks to follow",
Expand Down

0 comments on commit da67f0a

Please sign in to comment.