Skip to content

Commit

Permalink
Add skip_path feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorunic committed Sep 26, 2022
1 parent c80075a commit 4b68d64
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "findlargedir"
version = "0.1.3"
version = "0.1.4"
authors = ["Dinko Korunic <[email protected]>"]
categories = ["command-line-utilities"]
description = "find all blackhole directories with a huge amount of filesystem entries in a flat structure"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ OPTIONS:
-o, --one-filesystem <ONE_FILESYSTEM>
[default: true] [possible values: true, false]

-s, --skip-path <SKIP_PATH>


-t, --calibration-path <CALIBRATION_PATH>


Expand Down
18 changes: 15 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
use clap::Parser;
use clap::ValueHint;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[derive(Parser, Default, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
// TODO: Perform accurate directory entry counting
#[clap(short = 'a', long, action = clap::ArgAction::Set, default_value_t = false)]
pub accurate: bool,

// Do not cross mount points
#[clap(short = 'o', long, action = clap::ArgAction::Set, default_value_t = true)]
pub one_filesystem: bool,

// Calibration entry count
#[clap(short = 'c', long, value_parser, default_value_t = crate::calibrate::DEFAULT_TEST_COUNT)]
pub calibration_count: u64,

#[clap(short = 't', long, value_parser)]
// Calibration path
#[clap(short = 't', long, value_parser, value_hint = ValueHint::AnyPath)]
pub calibration_path: Option<PathBuf>,

// Alert threshold count: just print the estimate
#[clap(short = 'A', long, value_parser, default_value_t = crate::walk::ALERT_COUNT)]
pub alert_threshold: u64,

// Blacklist threshold count: print the estimate and stop further deeper scan
#[clap(short = 'B', long, value_parser, default_value_t = crate::walk::BLACKLIST_COUNT)]
pub blacklist_threshold: u64,

#[clap(required = true, value_parser)]
// Directories to never scan
#[clap(short = 's', long, value_parser, value_hint = ValueHint::AnyPath)]
pub skip_path: Vec<PathBuf>,

// Paths to perform deep scan on
#[clap(required = true, value_parser, value_hint = ValueHint::AnyPath)]
pub path: Vec<PathBuf>,
}
15 changes: 15 additions & 0 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fs_err as fs;
use human_bytes::human_bytes;
use human_format::Formatter;
use jwalk::{DirEntry, Parallelism, WalkDir};
use std::collections::HashSet;
use std::fs::Metadata;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub fn parallel_search(
args.alert_threshold,
args.blacklist_threshold,
);
let skip_path = args.skip_path.iter().cloned().collect::<HashSet<_>>();

for _ in WalkDir::new(path)
.skip_hidden(false)
Expand All @@ -53,6 +55,7 @@ pub fn parallel_search(
&path_metadata,
size_inode_ratio,
dir_entry_result,
&skip_path,
one_filesystem,
alert_threshold,
blacklist_threshold,
Expand All @@ -70,13 +73,25 @@ fn process_dir_entry<E>(
path_metadata: &Metadata,
size_inode_ratio: u64,
dir_entry_result: &mut Result<DirEntry<((), ())>, E>,
skip_path: &HashSet<PathBuf>,
one_filesystem: bool,
alert_threshold: u64,
blacklist_threshold: u64,
) {
if let Ok(dir_entry) = dir_entry_result {
if dir_entry.file_type.is_dir() {
if let Some(full_path) = dir_entry.read_children_path.as_ref() {
// Ignore skip paths, typically being virtual filesystems (/proc, /dev, /sys, /run)
if !skip_path.is_empty() && skip_path.contains(&full_path.to_path_buf()) {
println!(
"Skipping further scan at {} as requested",
full_path.display()
);

dir_entry.read_children_path = None;
return;
}

// Retrieve Unix metadata for a given directory
if let Ok(dir_entry_metadata) = fs::metadata(full_path) {
// If `one_filesystem` flag has been set and if directory is not residing
Expand Down

0 comments on commit 4b68d64

Please sign in to comment.