Skip to content

Commit 00bc854

Browse files
committed
Add option to filter files by inode number
Add the option on Unix systems to filter files by their inode number. The Windows equivalent FileIndex is not yet stabilized, see rust-lang/rust#63010. This is especially useful to debug audit records, e.g.: Jan 30 17:48:55 laptop audit: PATH item=0 name="pulse" inode=7340042 dev=fe:03 mode=040700 ouid=1001 ogid=1001 rdev=00:00 obj=system_u:object_r:unlabeled_t:s0 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0 Closes: sharkdp#880
1 parent 47ff118 commit 00bc854

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Options:
320320
executable (x), empty (e), socket (s), pipe (p), char-device
321321
(c), block-device (b)
322322
-e, --extension <ext> Filter by file extension
323+
--inum <num> Filter by inode number
323324
-S, --size <size> Limit results based on the size of files
324325
--changed-within <date|dur> Filter by file modification time (newer than)
325326
--changed-before <date|dur> Filter by file modification time (older than)

doc/fd.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ Always colorize output.
271271
.BI "\-j, \-\-threads " num
272272
Set number of threads to use for searching & executing (default: number of available CPU cores).
273273
.TP
274+
.BI "\-\-inum " num
275+
Filter files by their inode number.
276+
.TP
274277
.BI "\-S, \-\-size " size
275278
Limit results based on the size of files using the format
276279
.I <+-><NUM><UNIT>

src/cli.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ pub struct Opts {
375375
)]
376376
pub extensions: Option<Vec<String>>,
377377

378+
/// Filter files by their inode number.
379+
/// Format: [inum].
380+
///
381+
/// Examples:
382+
/// {n} --inum 4242
383+
#[cfg(unix)]
384+
#[arg(
385+
long,
386+
value_name = "inode-number",
387+
help = "Filter by inode number",
388+
long_help
389+
)]
390+
pub inum: Option<u64>,
391+
378392
/// Limit results based on the size of files using the format <+-><NUM><UNIT>.
379393
/// '+': file size must be greater than or equal to this
380394
/// '-': file size must be less than or equal to this

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ pub struct Config {
4747
/// Whether elements of output should be separated by a null character
4848
pub null_separator: bool,
4949

50+
#[cfg(unix)]
51+
/// The inode number to search for.
52+
pub inode_number: Option<u64>,
53+
5054
/// The maximum search depth, or `None` if no maximum search depth should be set.
5155
///
5256
/// A depth of `1` includes all files under the current directory, a depth of `2` also includes

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
206206
.unwrap_or_else(|| std::path::MAIN_SEPARATOR.to_string());
207207
check_path_separator_length(path_separator.as_deref())?;
208208

209+
#[cfg(unix)]
210+
let inode_number = std::mem::take(&mut opts.inum);
209211
let size_limits = std::mem::take(&mut opts.size);
210212
let time_constraints = extract_time_constraints(&opts)?;
211213
#[cfg(unix)]
@@ -303,6 +305,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
303305
batch_size: opts.batch_size,
304306
exclude_patterns: opts.exclude.iter().map(|p| String::from("!") + p).collect(),
305307
ignore_files: std::mem::take(&mut opts.ignore_file),
308+
#[cfg(unix)]
309+
inode_number,
306310
size_constraints: size_limits,
307311
time_constraints,
308312
#[cfg(unix)]

src/walk.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::borrow::Cow;
22
use std::ffi::OsStr;
33
use std::io::{self, Write};
44
use std::mem;
5+
#[cfg(unix)]
6+
use std::os::unix::fs::MetadataExt;
57
use std::path::PathBuf;
68
use std::sync::atomic::{AtomicBool, Ordering};
79
use std::sync::{Arc, Mutex, MutexGuard};
@@ -550,6 +552,18 @@ impl WorkerState {
550552
}
551553
}
552554

555+
// Filter out unwanted inode numbers.
556+
#[cfg(unix)]
557+
if let Some(inode_number) = config.inode_number {
558+
if let Some(metadata) = entry.metadata() {
559+
if inode_number != metadata.ino() {
560+
return ignore::WalkState::Continue;
561+
}
562+
} else {
563+
return ignore::WalkState::Continue;
564+
}
565+
}
566+
553567
#[cfg(unix)]
554568
{
555569
if let Some(ref owner_constraint) = config.owner_constraint {

0 commit comments

Comments
 (0)