minifind 0.7.4

minimal find reimplementation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use crate::args;
use ignore::DirEntry;
use std::fs;

#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;

// The 8 flags map 1:1 to find(1) -type selectors; a bitflags newtype would
// obscure that direct mapping for no real gain on a Copy plain-data struct.
#[expect(clippy::struct_excessive_bools)]
#[derive(Default, Copy, Clone)]
pub struct FileType {
    pub empty: bool,
    pub block_device: bool,
    pub char_device: bool,
    pub directory: bool,
    pub pipe: bool,
    pub file: bool,
    pub symlink: bool,
    pub socket: bool,
}

impl FileType {
    /// Creates a new instance of `FileType` based on the provided Vec of `args::FileType`.
    ///
    /// # Arguments
    ///
    /// * `clap_filetype` - A reference to a Vec of `args::FileType` enums.
    ///
    /// # Returns
    ///
    /// * `Self` - A new instance of `FileType` with flags set based on the input Vec.
    pub fn new(clap_filetype: &[args::FileType]) -> Self {
        let mut filetype = Self::default();

        for v in clap_filetype {
            match v {
                args::FileType::Empty => filetype.empty = true,
                args::FileType::BlockDevice => filetype.block_device = true,
                args::FileType::CharDevice => filetype.char_device = true,
                args::FileType::Directory => filetype.directory = true,
                args::FileType::Pipe => filetype.pipe = true,
                args::FileType::File => filetype.file = true,
                args::FileType::Symlink => filetype.symlink = true,
                args::FileType::Socket => filetype.socket = true,
            }
        }

        // helpful default of searching for both empty files and directories
        if filetype.empty && !filetype.directory && !filetype.file {
            filetype.directory = true;
            filetype.file = true;
        }

        filetype
    }
    /// Determines whether to ignore a file type based on the flags set in the `FileType` instance.
    ///
    /// # Arguments
    ///
    /// * `dir_entry` - A reference to the `DirEntry` representing the file type to check.
    ///
    /// # Returns
    ///
    /// * `bool` - `true` if the file type should be ignored, `false` otherwise.
    #[inline]
    pub fn ignore_filetype(self, dir_entry: &DirEntry) -> bool {
        if let Some(entry_type) = dir_entry.file_type() {
            // works everywhere
            (!self.file && entry_type.is_file())
                || (!self.directory && entry_type.is_dir())
                || (!self.symlink && entry_type.is_symlink())
                // requires Unix-only std::os::unix::fs::FileTypeExt trait
                || (!self.block_device && Self::is_block_device(entry_type))
                || (!self.char_device && Self::is_char_device(entry_type))
                || (!self.pipe && Self::is_pipe(entry_type))
                || (!self.socket && Self::is_socket(entry_type))
                // exclusive search; requires additional lookups
                || (self.empty && !Self::is_empty(dir_entry, entry_type))
        } else {
            true
        }
    }

    /// Checks if the given file type represents a block device.
    #[cfg(unix)]
    #[inline]
    pub fn is_block_device(entry_type: fs::FileType) -> bool {
        entry_type.is_block_device()
    }

    #[cfg(not(unix))]
    #[inline]
    pub fn is_block_device(_: fs::FileType) -> bool {
        false
    }

    /// Checks if the given file type represents a character device.
    #[cfg(unix)]
    #[inline]
    pub fn is_char_device(entry_type: fs::FileType) -> bool {
        entry_type.is_char_device()
    }

    #[cfg(not(unix))]
    #[inline]
    pub fn is_char_device(_: fs::FileType) -> bool {
        false
    }

    /// Checks if the given file type represents a named FIFO
    #[cfg(unix)]
    #[inline]
    pub fn is_pipe(entry_type: fs::FileType) -> bool {
        entry_type.is_fifo()
    }

    #[cfg(not(unix))]
    #[inline]
    pub fn is_pipe(_: fs::FileType) -> bool {
        false
    }

    /// Checks if the given file type represents a socket
    #[cfg(unix)]
    #[inline]
    pub fn is_socket(entry_type: fs::FileType) -> bool {
        entry_type.is_socket()
    }

    #[cfg(not(unix))]
    #[inline]
    pub fn is_socket(_: fs::FileType) -> bool {
        false
    }

    /// Checks if a directory entry is empty based on the given file type.
    ///
    /// If the file type is a directory, it checks if the directory is empty.
    /// If the file type is not a directory, it checks if the file has a size of 0.
    ///
    /// # Arguments
    /// * `dir_entry` - A reference to the directory entry to check.
    /// * `entry_type` - The file type of the directory entry.
    ///
    /// # Returns
    /// A boolean value indicating whether the directory entry is empty.
    #[inline]
    pub fn is_empty(dir_entry: &DirEntry, entry_type: fs::FileType) -> bool {
        if entry_type.is_dir() {
            dir_entry.path().read_dir().is_ok_and(|mut r| r.next().is_none())
        } else {
            dir_entry.metadata().is_ok_and(|m| m.len() == 0)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::args;
    use ignore::WalkBuilder;
    use std::fs;
    use tempfile::TempDir;

    // --- FileType::new() ---

    #[test]
    fn test_new_empty_slice() {
        let ft = FileType::new(&[]);
        assert!(!ft.file);
        assert!(!ft.directory);
        assert!(!ft.symlink);
        assert!(!ft.block_device);
        assert!(!ft.char_device);
        assert!(!ft.pipe);
        assert!(!ft.socket);
        assert!(!ft.empty);
    }

    #[test]
    fn test_new_file_only() {
        let ft = FileType::new(&[args::FileType::File]);
        assert!(ft.file);
        assert!(!ft.directory);
        assert!(!ft.symlink);
        assert!(!ft.empty);
    }

    #[test]
    fn test_new_directory_only() {
        let ft = FileType::new(&[args::FileType::Directory]);
        assert!(!ft.file);
        assert!(ft.directory);
        assert!(!ft.symlink);
    }

    #[test]
    fn test_new_symlink_only() {
        let ft = FileType::new(&[args::FileType::Symlink]);
        assert!(!ft.file);
        assert!(!ft.directory);
        assert!(ft.symlink);
    }

    #[test]
    fn test_new_empty_alone_auto_expands() {
        // Empty alone must auto-set both file and directory
        let ft = FileType::new(&[args::FileType::Empty]);
        assert!(ft.empty);
        assert!(ft.file);
        assert!(ft.directory);
    }

    #[test]
    fn test_new_empty_with_directory_no_file_expansion() {
        let ft =
            FileType::new(&[args::FileType::Empty, args::FileType::Directory]);
        assert!(ft.empty);
        assert!(ft.directory);
        assert!(!ft.file);
    }

    #[test]
    fn test_new_empty_with_file_no_dir_expansion() {
        let ft = FileType::new(&[args::FileType::Empty, args::FileType::File]);
        assert!(ft.empty);
        assert!(ft.file);
        assert!(!ft.directory);
    }

    #[test]
    fn test_new_empty_with_both_no_expansion_needed() {
        let ft = FileType::new(&[
            args::FileType::Empty,
            args::FileType::File,
            args::FileType::Directory,
        ]);
        assert!(ft.empty);
        assert!(ft.file);
        assert!(ft.directory);
    }

    #[test]
    fn test_new_all_types() {
        let ft = FileType::new(&[
            args::FileType::File,
            args::FileType::Directory,
            args::FileType::Symlink,
            args::FileType::BlockDevice,
            args::FileType::CharDevice,
            args::FileType::Pipe,
            args::FileType::Socket,
        ]);
        assert!(ft.file);
        assert!(ft.directory);
        assert!(ft.symlink);
        assert!(ft.block_device);
        assert!(ft.char_device);
        assert!(ft.pipe);
        assert!(ft.socket);
    }

    // --- ignore_filetype() ---

    /// Build a temp directory with a known fixture layout:
    /// - file.txt  (non-empty regular file)
    /// - empty.txt (empty regular file)
    /// - subdir/   (empty subdirectory)
    fn setup_fixture() -> TempDir {
        let tmp = TempDir::new().expect("failed to create tempdir");
        fs::write(tmp.path().join("file.txt"), b"content")
            .expect("write file.txt");
        fs::write(tmp.path().join("empty.txt"), b"").expect("write empty.txt");
        fs::create_dir(tmp.path().join("subdir")).expect("create subdir");
        tmp
    }

    fn walk_all(root: &std::path::Path) -> Vec<ignore::DirEntry> {
        WalkBuilder::new(root)
            .hidden(false)
            .standard_filters(false)
            .build()
            .filter_map(|e| e.ok())
            .collect()
    }

    fn find_entry<'a>(
        entries: &'a [ignore::DirEntry],
        name: &str,
        root: &std::path::Path,
    ) -> &'a ignore::DirEntry {
        let target = root.join(name);
        entries
            .iter()
            .find(|e| e.path() == target)
            .unwrap_or_else(|| panic!("{name} not found in walk"))
    }

    #[test]
    fn test_ignore_filetype_file_accepted() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::File]);
        let e = find_entry(&entries, "file.txt", tmp.path());
        assert!(!ft.ignore_filetype(e));
    }

    #[test]
    fn test_ignore_filetype_file_rejected_when_dir_only() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::Directory]);
        let e = find_entry(&entries, "file.txt", tmp.path());
        assert!(ft.ignore_filetype(e));
    }

    #[test]
    fn test_ignore_filetype_dir_accepted() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::Directory]);
        let e = find_entry(&entries, "subdir", tmp.path());
        assert!(!ft.ignore_filetype(e));
    }

    #[test]
    fn test_ignore_filetype_dir_rejected_when_file_only() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::File]);
        let e = find_entry(&entries, "subdir", tmp.path());
        assert!(ft.ignore_filetype(e));
    }

    #[test]
    fn test_ignore_filetype_empty_file_accepted() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        // Empty alone auto-expands to file+dir; empty.txt qualifies
        let ft = FileType::new(&[args::FileType::Empty]);
        let e = find_entry(&entries, "empty.txt", tmp.path());
        assert!(!ft.ignore_filetype(e));
    }

    #[test]
    fn test_ignore_filetype_nonempty_file_rejected_with_empty() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::Empty]);
        let e = find_entry(&entries, "file.txt", tmp.path());
        // file.txt has content → not empty → rejected
        assert!(ft.ignore_filetype(e));
    }

    #[test]
    fn test_ignore_filetype_empty_dir_accepted() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::Empty]);
        let e = find_entry(&entries, "subdir", tmp.path());
        // subdir has no children → empty → accepted
        assert!(!ft.ignore_filetype(e));
    }

    #[cfg(unix)]
    #[test]
    fn test_ignore_filetype_symlink_accepted() {
        let tmp = setup_fixture();
        let link = tmp.path().join("link.txt");
        std::os::unix::fs::symlink(tmp.path().join("file.txt"), &link)
            .expect("create symlink");
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::Symlink]);
        let e = find_entry(&entries, "link.txt", tmp.path());
        assert!(!ft.ignore_filetype(e));
    }

    #[cfg(unix)]
    #[test]
    fn test_ignore_filetype_symlink_rejected_when_file_only() {
        let tmp = setup_fixture();
        let link = tmp.path().join("link.txt");
        std::os::unix::fs::symlink(tmp.path().join("file.txt"), &link)
            .expect("create symlink");
        let entries = walk_all(tmp.path());
        let ft = FileType::new(&[args::FileType::File]);
        let e = find_entry(&entries, "link.txt", tmp.path());
        assert!(ft.ignore_filetype(e));
    }

    // --- FileType::is_empty() ---

    // F6 — is_empty: zero-byte file must return true
    #[test]
    fn test_is_empty_zero_byte_file_returns_true() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let e = find_entry(&entries, "empty.txt", tmp.path());
        let ft = e.file_type().unwrap();
        assert!(
            FileType::is_empty(e, ft),
            "zero-byte file must be reported as empty"
        );
    }

    // F6 — is_empty: non-zero file must return false
    #[test]
    fn test_is_empty_nonempty_file_returns_false() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        let e = find_entry(&entries, "file.txt", tmp.path());
        let ft = e.file_type().unwrap();
        assert!(
            !FileType::is_empty(e, ft),
            "file with content must not be reported as empty"
        );
    }

    // F7 — is_empty: empty directory must return true
    #[test]
    fn test_is_empty_empty_dir_returns_true() {
        let tmp = setup_fixture();
        let entries = walk_all(tmp.path());
        // setup_fixture creates subdir/ with no children
        let e = find_entry(&entries, "subdir", tmp.path());
        let ft = e.file_type().unwrap();
        assert!(
            FileType::is_empty(e, ft),
            "empty directory must be reported as empty"
        );
    }

    // F7 — is_empty: directory with a child must return false
    #[test]
    fn test_is_empty_nonempty_dir_returns_false() {
        let tmp = TempDir::new().expect("tempdir");
        let sub = tmp.path().join("nonempty");
        fs::create_dir(&sub).expect("create subdir");
        fs::write(sub.join("child.txt"), b"x").expect("write child");
        let entries = walk_all(tmp.path());
        let e = find_entry(&entries, "nonempty", tmp.path());
        let ft = e.file_type().unwrap();
        assert!(
            !FileType::is_empty(e, ft),
            "directory with a child must not be reported as empty"
        );
    }

    // F8 — is_empty: correct branch selected for file AND directory
    #[test]
    fn test_is_empty_dispatches_correct_branch_for_both_types() {
        let tmp = TempDir::new().expect("tempdir");
        fs::write(tmp.path().join("zero.txt"), b"").expect("write zero.txt");
        fs::create_dir(tmp.path().join("emptydir")).expect("create emptydir");
        let entries = walk_all(tmp.path());

        let file_e = find_entry(&entries, "zero.txt", tmp.path());
        let file_ft = file_e.file_type().unwrap();
        assert!(
            FileType::is_empty(file_e, file_ft),
            "zero-byte file must be empty (tests file branch)"
        );

        let dir_e = find_entry(&entries, "emptydir", tmp.path());
        let dir_ft = dir_e.file_type().unwrap();
        assert!(
            FileType::is_empty(dir_e, dir_ft),
            "empty directory must be empty (tests directory branch)"
        );
    }
}