Skip to content

Commit

Permalink
Update deps. Add path argument normalization and directory check.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorunic committed Nov 14, 2024
1 parent 7a3f7ca commit 7d0eb75
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "findlargedir"
version = "0.8.0"
version = "0.9.0"
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 All @@ -15,17 +15,18 @@ rayon = "1.10.0"
tempfile = "3.14.0"
anyhow = "1.0.93"
human_format = "1.1.0"
clap = { version = "4.5.20", features = ["derive", "unicode", "wrap_help"] }
clap = { version = "4.5.21", features = ["derive", "unicode", "wrap_help"] }
rm_rf = "0.6.2"
ansi_term = "0.12.1"
fs-err = "3.0.0"
indicatif = { version = "0.17.8", features = ["rayon"] }
indicatif = { version = "0.17.9", features = ["rayon"] }
cfg-if = "1.0"
fdlimit = "0.3.0"
ahash = "0.8.11"
anstyle = "1.0.10"
signal-hook = "0.3.17"
ignore = "0.4.23"
normpath = "1.3.0"

[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
tikv-jemallocator = "0.6.0"
Expand Down
43 changes: 40 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::thread;

use anstyle::AnsiColor;
use anyhow::{anyhow, Error};
use clap::builder::{styling::Styles, ValueParser};
use clap::Parser;
use clap::ValueHint;
use normpath::PathExt;

const STYLES: Styles = Styles::styled()
.header(AnsiColor::Yellow.on_default())
Expand Down Expand Up @@ -41,7 +42,8 @@ pub struct Args {
pub blacklist_threshold: u64,

/// Number of threads to use when calibrating and scanning
#[clap(short = 'x', long, value_parser = ValueParser::new(parse_threads), default_value_t = thread::available_parallelism().map(| n | n.get()).unwrap_or(2))]
#[clap(short = 'x', long, value_parser = ValueParser::new(parse_threads), default_value_t = thread::available_parallelism().map(| n | n.get()).unwrap_or(2)
)]
pub threads: usize,

/// Seconds between status updates, set to 0 to disable
Expand All @@ -61,7 +63,8 @@ pub struct Args {
pub skip_path: Vec<PathBuf>,

/// Paths to check for large directories
#[clap(required = true, value_parser, value_hint = ValueHint::AnyPath)]
#[clap(required = true, value_parser = ValueParser::new(parse_paths), value_hint = ValueHint::AnyPath
)]
pub path: Vec<PathBuf>,
}

Expand All @@ -77,3 +80,37 @@ fn parse_threads(x: &str) -> Result<usize, Error> {
Err(e) => Err(Error::from(e)),
}
}

/// Parses a string into a `PathBuf`, checking if the path is a directory and exists.
///
/// # Arguments
///
/// * `x` - A string slice to be parsed into a `PathBuf`.
///
/// # Returns
///
/// * `Result<PathBuf, Error>` - An `Ok` variant containing a normalized `PathBuf` if the path is an existing directory,
/// or an `Err` variant with an error message if the path does not exist or is not a directory.
fn parse_paths(x: &str) -> Result<PathBuf, Error> {
let p = Path::new(x);

if directory_exists(p) {
Ok(p.normalize()?.into_path_buf())
} else {
Err(anyhow!("'{}' is not an existing directory", x))
}
}

/// Checks if the given path is a directory and exists.
///
/// # Arguments
///
/// * `x` - A reference to the path to check.
///
/// # Returns
///
/// * `bool` - `true` if the path is an existing directory, `false` otherwise.
#[inline]
fn directory_exists(x: &Path) -> bool {
x.is_dir() && x.normalize().is_ok()
}

0 comments on commit 7d0eb75

Please sign in to comment.