Skip to content

Commit c56ac4f

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 c57e922 commit c56ac4f

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
@@ -297,6 +297,9 @@ Always output hyperlink escapes, regardless of color settings.
297297
.BI "\-j, \-\-threads " num
298298
Set number of threads to use for searching & executing (default: number of available CPU cores).
299299
.TP
300+
.BI "\-\-inum " num
301+
Filter files by their inode number.
302+
.TP
300303
.BI "\-S, \-\-size " size
301304
Limit results based on the size of files using the format
302305
.I <+-><NUM><UNIT>

src/cli.rs

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

377+
/// Filter files by their inode number.
378+
/// Format: [inum].
379+
///
380+
/// Examples:
381+
/// {n} --inum 4242
382+
#[cfg(unix)]
383+
#[arg(
384+
long,
385+
value_name = "inode-number",
386+
help = "Filter by inode number",
387+
long_help
388+
)]
389+
pub inum: Option<u64>,
390+
377391
/// Limit results based on the size of files using the format <+-><NUM><UNIT>.
378392
/// '+': file size must be greater than or equal to this
379393
/// '-': 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
@@ -48,6 +48,10 @@ pub struct Config {
4848
/// Whether elements of output should be separated by a null character
4949
pub null_separator: bool,
5050

51+
#[cfg(unix)]
52+
/// The inode number to search for.
53+
pub inode_number: Option<u64>,
54+
5155
/// The maximum search depth, or `None` if no maximum search depth should be set.
5256
///
5357
/// 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
@@ -208,6 +208,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
208208
.unwrap_or_else(|| std::path::MAIN_SEPARATOR.to_string());
209209
check_path_separator_length(path_separator.as_deref())?;
210210

211+
#[cfg(unix)]
212+
let inode_number = std::mem::take(&mut opts.inum);
211213
let size_limits = std::mem::take(&mut opts.size);
212214
let time_constraints = extract_time_constraints(&opts)?;
213215
#[cfg(unix)]
@@ -315,6 +317,8 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
315317
batch_size: opts.batch_size,
316318
exclude_patterns: opts.exclude.iter().map(|p| String::from("!") + p).collect(),
317319
ignore_files: std::mem::take(&mut opts.ignore_file),
320+
#[cfg(unix)]
321+
inode_number,
318322
size_constraints: size_limits,
319323
time_constraints,
320324
#[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};
@@ -551,6 +553,18 @@ impl WorkerState {
551553
}
552554
}
553555

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

0 commit comments

Comments
 (0)