Skip to content

Commit 15ab46a

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 f718456 commit 15ab46a

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
@@ -256,6 +256,9 @@ Always colorize output.
256256
.BI "\-j, \-\-threads " num
257257
Set number of threads to use for searching & executing (default: number of available CPU cores).
258258
.TP
259+
.BI "\-\-inum " num
260+
Filter files by their inode number.
261+
.TP
259262
.BI "\-S, \-\-size " size
260263
Limit results based on the size of files using the format
261264
.I <+-><NUM><UNIT>

src/cli.rs

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

370+
/// Filter files by their inode number.
371+
/// Format: [inum].
372+
///
373+
/// Examples:
374+
/// {n} --inum 4242
375+
#[cfg(unix)]
376+
#[arg(
377+
long,
378+
value_name = "inode-number",
379+
help = "Filter by inode number",
380+
long_help
381+
)]
382+
pub inum: Option<u64>,
383+
370384
/// Limit results based on the size of files using the format <+-><NUM><UNIT>.
371385
/// '+': file size must be greater than or equal to this
372386
/// '-': 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)]
@@ -299,6 +301,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
299301
batch_size: opts.batch_size,
300302
exclude_patterns: opts.exclude.iter().map(|p| String::from("!") + p).collect(),
301303
ignore_files: std::mem::take(&mut opts.ignore_file),
304+
#[cfg(unix)]
305+
inode_number,
302306
size_constraints: size_limits,
303307
time_constraints,
304308
#[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};
@@ -472,6 +474,18 @@ fn spawn_senders(
472474
}
473475
}
474476

477+
// Filter out unwanted inode numbers.
478+
#[cfg(unix)]
479+
if let Some(inode_number) = config.inode_number {
480+
if let Some(metadata) = entry.metadata() {
481+
if inode_number != metadata.ino() {
482+
return ignore::WalkState::Continue;
483+
}
484+
} else {
485+
return ignore::WalkState::Continue;
486+
}
487+
}
488+
475489
#[cfg(unix)]
476490
{
477491
if let Some(ref owner_constraint) = config.owner_constraint {

0 commit comments

Comments
 (0)