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
use crate::args;
use ignore::DirEntry;
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
#[allow(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: &Vec<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(ref 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)
}
}
}