Skip to content

Commit 7d0eb75

Browse files
committed
Update deps. Add path argument normalization and directory check.
1 parent 7a3f7ca commit 7d0eb75

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "findlargedir"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = ["Dinko Korunic <[email protected]>"]
55
categories = ["command-line-utilities"]
66
description = "find all blackhole directories with a huge amount of filesystem entries in a flat structure"
@@ -15,17 +15,18 @@ rayon = "1.10.0"
1515
tempfile = "3.14.0"
1616
anyhow = "1.0.93"
1717
human_format = "1.1.0"
18-
clap = { version = "4.5.20", features = ["derive", "unicode", "wrap_help"] }
18+
clap = { version = "4.5.21", features = ["derive", "unicode", "wrap_help"] }
1919
rm_rf = "0.6.2"
2020
ansi_term = "0.12.1"
2121
fs-err = "3.0.0"
22-
indicatif = { version = "0.17.8", features = ["rayon"] }
22+
indicatif = { version = "0.17.9", features = ["rayon"] }
2323
cfg-if = "1.0"
2424
fdlimit = "0.3.0"
2525
ahash = "0.8.11"
2626
anstyle = "1.0.10"
2727
signal-hook = "0.3.17"
2828
ignore = "0.4.23"
29+
normpath = "1.3.0"
2930

3031
[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
3132
tikv-jemallocator = "0.6.0"

src/args.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::path::PathBuf;
1+
use std::path::{Path, PathBuf};
22
use std::thread;
33

44
use anstyle::AnsiColor;
55
use anyhow::{anyhow, Error};
66
use clap::builder::{styling::Styles, ValueParser};
77
use clap::Parser;
88
use clap::ValueHint;
9+
use normpath::PathExt;
910

1011
const STYLES: Styles = Styles::styled()
1112
.header(AnsiColor::Yellow.on_default())
@@ -41,7 +42,8 @@ pub struct Args {
4142
pub blacklist_threshold: u64,
4243

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

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

6365
/// Paths to check for large directories
64-
#[clap(required = true, value_parser, value_hint = ValueHint::AnyPath)]
66+
#[clap(required = true, value_parser = ValueParser::new(parse_paths), value_hint = ValueHint::AnyPath
67+
)]
6568
pub path: Vec<PathBuf>,
6669
}
6770

@@ -77,3 +80,37 @@ fn parse_threads(x: &str) -> Result<usize, Error> {
7780
Err(e) => Err(Error::from(e)),
7881
}
7982
}
83+
84+
/// Parses a string into a `PathBuf`, checking if the path is a directory and exists.
85+
///
86+
/// # Arguments
87+
///
88+
/// * `x` - A string slice to be parsed into a `PathBuf`.
89+
///
90+
/// # Returns
91+
///
92+
/// * `Result<PathBuf, Error>` - An `Ok` variant containing a normalized `PathBuf` if the path is an existing directory,
93+
/// or an `Err` variant with an error message if the path does not exist or is not a directory.
94+
fn parse_paths(x: &str) -> Result<PathBuf, Error> {
95+
let p = Path::new(x);
96+
97+
if directory_exists(p) {
98+
Ok(p.normalize()?.into_path_buf())
99+
} else {
100+
Err(anyhow!("'{}' is not an existing directory", x))
101+
}
102+
}
103+
104+
/// Checks if the given path is a directory and exists.
105+
///
106+
/// # Arguments
107+
///
108+
/// * `x` - A reference to the path to check.
109+
///
110+
/// # Returns
111+
///
112+
/// * `bool` - `true` if the path is an existing directory, `false` otherwise.
113+
#[inline]
114+
fn directory_exists(x: &Path) -> bool {
115+
x.is_dir() && x.normalize().is_ok()
116+
}

0 commit comments

Comments
 (0)