fdf 0.9.2

A fast, multi-threaded filesystem search tool with regex/glob support and extremely pretty colours!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#![allow(clippy::missing_inline_in_public_items)]
use crate::{
    SearchConfigError,
    config,
    filters::{FileTypeFilter, SizeFilter, TimeFilter},
    fs::DirEntry,
    //  util::IgnoreMatcher,
    walk::{DirEntryFilter, FilterType, finder::Finder},
};

use core::num::NonZeroU32;
use core::num::NonZeroUsize;
use dashmap::DashSet;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use std::{
    ffi::{OsStr, OsString},
    fs::metadata,
    io,
    os::unix::fs::MetadataExt as _,
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
};

/**
 A builder for creating a `Finder` instance with customisable options.

 This builder allows you to set various options such as hiding hidden files, case sensitivity,
 matching file extensions, setting maximum search depth,
 following symlinks, and applying a custom filter function.
*/
#[expect(
    clippy::struct_excessive_bools,
    reason = "Naturally a builder will contain many bools"
)]
pub struct FinderBuilder {
    pub(crate) root: OsString,
    pub(crate) pattern: Option<String>,
    pub(crate) and_patterns: Vec<String>,
    pub(crate) hide_hidden: bool,
    pub(crate) case_insensitive: bool,
    pub(crate) file_name_only: bool,
    pub(crate) extension_match: Option<Box<[u8]>>,
    pub(crate) max_depth: Option<NonZeroU32>,
    pub(crate) follow_symlinks: bool,
    pub(crate) filter: Option<DirEntryFilter>,
    pub(crate) size_filter: Option<SizeFilter>,
    pub(crate) time_filter: Option<TimeFilter>,
    pub(crate) file_type: Option<FileTypeFilter>,
    pub(crate) collect_errors: bool,
    pub(crate) use_glob: bool,
    pub(crate) canonicalise: bool,
    pub(crate) same_filesystem: bool,
    pub(crate) thread_count: NonZeroUsize,
    pub(crate) respect_gitignore: bool,
    pub(crate) ignore_patterns: Vec<String>,
    pub(crate) ignore_glob_patterns: Vec<String>,
    pub(crate) ignore_files: Vec<PathBuf>,
}

impl FinderBuilder {
    pub(crate) fn new<A: AsRef<OsStr>>(root: A) -> Self {
        const MIN_THREADS: NonZeroUsize = NonZeroUsize::MIN;
        let num_threads = std::thread::available_parallelism().unwrap_or(MIN_THREADS);
        Self {
            root: root.as_ref().to_owned(),
            pattern: None,
            and_patterns: Vec::new(),
            hide_hidden: true,
            case_insensitive: true,
            file_name_only: true,
            extension_match: None,
            max_depth: None,
            follow_symlinks: false,
            filter: None,
            size_filter: None,
            time_filter: None,
            file_type: None,
            collect_errors: false,
            use_glob: false,
            canonicalise: false,
            same_filesystem: false,
            thread_count: num_threads,
            respect_gitignore: true,
            ignore_patterns: Vec::new(),
            ignore_glob_patterns: Vec::new(),
            ignore_files: Vec::new(),
        }
    }

    /// Set the search pattern (regex or glob)
    #[must_use]
    pub fn pattern<P: AsRef<str>>(mut self, pattern: P) -> Self {
        self.pattern = Some(pattern.as_ref().into());
        self
    }

    /// Set additional required patterns. All of them must match.
    #[must_use]
    pub fn and_patterns(mut self, patterns: Vec<String>) -> Self {
        self.and_patterns = patterns;
        self
    }

    /// Set whether to hide hidden files, defaults to true
    #[must_use]
    pub const fn keep_hidden(mut self, hide_hidden: bool) -> Self {
        self.hide_hidden = hide_hidden;
        self
    }
    /// Set case insensitive matching,defaults to true
    #[must_use]
    pub const fn case_insensitive(mut self, case_insensitive: bool) -> Self {
        self.case_insensitive = case_insensitive;
        self
    }

    /// Set whether to use short paths in regex/glob matching, defaults to true
    /// This is over-ridden if the search term contains a '/'
    #[must_use]
    pub const fn file_name_only(mut self, short_path: bool) -> Self {
        self.file_name_only = short_path;
        self
    }

    /// Set extension to match, defaults to no extension.
    /// `-e rs` and `-e .rs` both match `*.rs`, and `-e '.'` matches files
    /// whose name ends with a dot (empty extension after the dot).
    #[must_use]
    pub fn extension<C: AsRef<str>>(mut self, extension: C) -> Self {
        let input = extension.as_ref().as_bytes();
        // Strip one leading dot so that `-e rs` and `-e .rs` are equivalent,
        // and `-e '.'` stores Some(b"") which matches filenames ending in '.'.
        let ext = input.strip_prefix(b".").unwrap_or(input);

        if input.is_empty() {
            self.extension_match = None;
        } else {
            self.extension_match = Some(ext.into());
        }

        self
    }

    /// Set maximum search depth
    #[must_use]
    pub const fn max_depth(mut self, max_depth: Option<u32>) -> Self {
        match max_depth {
            None => self,
            Some(num) => {
                self.max_depth = NonZeroU32::new(num);
                self
            }
        }
    }

    /// Sets size-based filtering criteria.
    #[must_use]
    pub const fn filter_by_size(mut self, size_of: Option<SizeFilter>) -> Self {
        self.size_filter = size_of;
        self
    }

    /// Sets time-based filtering criteria for file modification times.
    #[must_use]
    pub const fn filter_by_time(mut self, time_of: Option<TimeFilter>) -> Self {
        self.time_filter = time_of;
        self
    }

    /// Sets whether to follow symlinks (default: false).
    ///
    /// This will not recurse infinitely but can provide more results than expected
    #[must_use]
    pub const fn follow_symlinks(mut self, follow_symlinks: bool) -> Self {
        self.follow_symlinks = follow_symlinks;
        self
    }

    /// Set a custom filter
    #[must_use]
    pub const fn filter(mut self, filter: Option<fn(&DirEntry) -> bool>) -> Self {
        self.filter = filter;
        self
    }

    /// Sets file type filtering.
    #[must_use]
    pub const fn type_filter(mut self, filter: Option<FileTypeFilter>) -> Self {
        self.file_type = filter;
        self
    }

    /// Sets a glob pattern for regex matching, not a regex.
    #[must_use]
    pub const fn use_glob(mut self, use_glob: bool) -> Self {
        self.use_glob = use_glob;
        self
    }

    /// Set whether to collect errors during traversal, defaults to false
    #[must_use]
    pub const fn collect_errors(mut self, yesorno: bool) -> Self {
        self.collect_errors = yesorno;
        self
    }

    /// Set whether to canonicalise (resolve absolute path) the root directory, defaults to false
    #[must_use]
    pub const fn canonicalise_root(mut self, canonicalise: bool) -> Self {
        self.canonicalise = canonicalise;
        self
    }

    /// Set whether to escape any regexs in the string, defaults to false
    #[must_use]
    pub fn fixed_string(mut self, fixed_string: bool) -> Self {
        if fixed_string {
            self.pattern = self.pattern.as_ref().map(|patt| regex::escape(patt));
            self.and_patterns = self
                .and_patterns
                .into_iter()
                .map(|patt| regex::escape(&patt))
                .collect();
        }
        self
    }

    /// Set how many threads to use, defaults to max
    #[must_use]
    pub const fn thread_count(mut self, threads: Option<NonZeroUsize>) -> Self {
        match threads {
            Some(num) => self.thread_count = num,
            None => return self,
        }

        self
    }

    /// Set whether to follow the same filesystem as root
    #[must_use]
    pub const fn same_filesystem(mut self, yesorno: bool) -> Self {
        self.same_filesystem = yesorno;
        self
    }

    /// Set whether to respect `.gitignore` rules, defaults to true
    #[must_use]
    pub const fn respect_gitignore(mut self, yesorno: bool) -> Self {
        self.respect_gitignore = yesorno;
        self
    }

    /// Set regex patterns that should be ignored during traversal.
    #[must_use]
    pub fn ignore_patterns(mut self, patterns: Vec<String>) -> Self {
        self.ignore_patterns = patterns;
        self
    }

    /// Set glob patterns that should be ignored during traversal.
    #[must_use]
    pub fn ignore_glob_patterns(mut self, patterns: Vec<String>) -> Self {
        self.ignore_glob_patterns = patterns;
        self
    }

    /// Set custom ignore files in `.gitignore` format.
    #[must_use]
    pub fn ignore_files(mut self, files: Vec<OsString>) -> Self {
        self.ignore_files = files.into_iter().map(PathBuf::from).collect();
        self
    }

    /**
    Builds a [`Finder`] instance with the configured options.

    This method performs validation of all configuration parameters and
    initialises the necessary components for file system traversal.


     # Returns
     Returns `Ok(Finder)` on successful configuration, or
     `Err(SearchConfigError)` if any validation fails.

    # Errors
    Returns an error if:
    - The root path is not a directory or cannot be accessed
    - The root path cannot be canonicalised (when enabled)
    - The search pattern cannot be compiled to a valid regular expression
    - File system metadata cannot be retrieved (for same-filesystem tracking)
    */
    pub fn build(self) -> core::result::Result<Finder, SearchConfigError> {
        // Resolve and validate the root directory
        let resolved_root = self.resolve_directory()?;
        let custom_ignore_matchers = self.compile_ignore_files()?;

        let starting_filesystem = if self.same_filesystem {
            // Get the filesystem ID of the root directory directly
            let metadata = metadata(resolved_root.as_ref())?;
            Some(metadata.dev()) // dev() returns the filesystem ID on Unix
        } else {
            None
        };

        let search_config = config::SearchConfig::new(
            self.pattern.as_ref(),
            self.hide_hidden,
            self.case_insensitive,
            self.file_name_only,
            self.extension_match,
            self.max_depth,
            self.follow_symlinks,
            self.size_filter,
            self.file_type,
            self.time_filter,
            self.use_glob,
            self.and_patterns,
            self.respect_gitignore,
            self.ignore_patterns,
            self.ignore_glob_patterns,
        )?;

        let lambda: FilterType = |rconfig, rdir, rfilter| {
            {
                // arrange the filters by order of costliness
                rconfig.matches_extension(&rdir.file_name())
                    && rconfig.matches_path(rdir, !rconfig.file_name_only)
                    && rconfig.matches_type(rdir)
                    && rconfig.matches_size(rdir)
                    && rconfig.matches_time(rdir)
                    && rfilter.is_none_or(|func| func(rdir)) // put the custom filter last because it's almost always unlikely
            }
        };

        let inode_cache: Option<DashSet<(u64, u64)>> = self.follow_symlinks.then(DashSet::new);

        let errors = self
            .collect_errors
            .then(|| Arc::new(Mutex::new(Vec::new())));

        Ok(Finder {
            root: resolved_root,
            search_config,
            custom_filter: self.filter,
            file_filter: lambda,
            starting_filesystem,
            inode_cache,
            errors,
            thread_count: self.thread_count,
            custom_ignore_matchers,
        })
    }

    fn compile_ignore_files(&self) -> core::result::Result<Vec<Arc<Gitignore>>, SearchConfigError> {
        self.ignore_files
            .iter()
            .map(|path| {
                let base = path.parent().unwrap_or_else(|| Path::new("."));
                let mut builder = GitignoreBuilder::new(base);

                if let Some(error) = builder.add(path) {
                    return Err(SearchConfigError::IOError(io::Error::other(
                        error.to_string(),
                    )));
                }

                builder.build().map(Arc::new).map_err(|error| {
                    SearchConfigError::IOError(io::Error::other(error.to_string()))
                })
            })
            .collect()
    }

    /**
     Resolves and validates the root directory path.

      This function handles:
      - Default to current directory (".") if root is empty
      - Validates that the path is a directory
      - Optionally canonicalises the path if canonicalise flag is set
    */
    fn resolve_directory(&self) -> core::result::Result<Box<OsStr>, SearchConfigError> {
        let dir_to_use = if self.root.is_empty() {
            // Get current directory and canonicalise it for consistency
            std::env::current_dir().map(PathBuf::into_os_string)?
        } else {
            self.root.clone()
        };

        let path_check = Path::new(&dir_to_use);

        // Validate that the path exists and is a directory
        if !path_check.is_dir() {
            return Err(SearchConfigError::NotADirectory);
        }

        // Apply canonicalisation if requested
        match (self.canonicalise, path_check.canonicalize()) {
            (true, Ok(good_path)) => Ok(good_path.as_os_str().into()),
            _ => Ok(dir_to_use.into_boxed_os_str()),
        }
    }
}