Skip to content

Commit 04fb6bb

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 3cf5ac0 commit 04fb6bb

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
@@ -316,6 +316,7 @@ Options:
316316
-t, --type <filetype> Filter by type: file (f), directory (d), symlink (l),
317317
executable (x), empty (e), socket (s), pipe (p)
318318
-e, --extension <ext> Filter by file extension
319+
--inum <num> Filter by inode number
319320
-S, --size <size> Limit results based on the size of files
320321
--changed-within <date|dur> Filter by file modification time (newer than)
321322
--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
@@ -243,6 +243,9 @@ Always colorize output.
243243
.BI "\-j, \-\-threads " num
244244
Set number of threads to use for searching & executing (default: number of available CPU cores).
245245
.TP
246+
.BI "\-\-inum " num
247+
Filter files by their inode number.
248+
.TP
246249
.BI "\-S, \-\-size " size
247250
Limit results based on the size of files using the format
248251
.I <+-><NUM><UNIT>

src/cli.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,20 @@ pub struct Opts {
365365
)]
366366
pub extensions: Option<Vec<String>>,
367367

368+
/// Filter files by their inode number.
369+
/// Format: [inum].
370+
///
371+
/// Examples:
372+
/// {n} --inum 4242
373+
#[cfg(unix)]
374+
#[arg(
375+
long,
376+
value_name = "inode-number",
377+
help = "Filter by inode number",
378+
long_help
379+
)]
380+
pub inum: Option<u64>,
381+
368382
/// Limit results based on the size of files using the format <+-><NUM><UNIT>.
369383
/// '+': file size must be greater than or equal to this
370384
/// '-': 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
@@ -45,6 +45,10 @@ pub struct Config {
4545
/// Whether elements of output should be separated by a null character
4646
pub null_separator: bool,
4747

48+
#[cfg(unix)]
49+
/// The inode number to search for.
50+
pub inode_number: Option<u64>,
51+
4852
/// The maximum search depth, or `None` if no maximum search depth should be set.
4953
///
5054
/// 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
@@ -205,6 +205,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
205205
.unwrap_or_else(|| std::path::MAIN_SEPARATOR.to_string());
206206
check_path_separator_length(path_separator.as_deref())?;
207207

208+
#[cfg(unix)]
209+
let inode_number = std::mem::take(&mut opts.inum);
208210
let size_limits = std::mem::take(&mut opts.size);
209211
let time_constraints = extract_time_constraints(&opts)?;
210212
#[cfg(unix)]
@@ -298,6 +300,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
298300
batch_size: opts.batch_size,
299301
exclude_patterns: opts.exclude.iter().map(|p| String::from("!") + p).collect(),
300302
ignore_files: std::mem::take(&mut opts.ignore_file),
303+
#[cfg(unix)]
304+
inode_number,
301305
size_constraints: size_limits,
302306
time_constraints,
303307
#[cfg(unix)]

src/walk.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::ffi::OsStr;
22
use std::io;
33
use std::mem;
4+
#[cfg(unix)]
5+
use std::os::unix::fs::MetadataExt;
46
use std::path::PathBuf;
57
use std::sync::atomic::{AtomicBool, Ordering};
68
use std::sync::{Arc, Mutex};
@@ -485,6 +487,18 @@ fn spawn_senders(
485487
}
486488
}
487489

490+
// Filter out unwanted inode numbers.
491+
#[cfg(unix)]
492+
if let Some(inode_number) = config.inode_number {
493+
if let Some(metadata) = entry.metadata() {
494+
if inode_number != metadata.ino() {
495+
return ignore::WalkState::Continue;
496+
}
497+
} else {
498+
return ignore::WalkState::Continue;
499+
}
500+
}
501+
488502
#[cfg(unix)]
489503
{
490504
if let Some(ref owner_constraint) = config.owner_constraint {

0 commit comments

Comments
 (0)