Add `--type empty`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add a new `empty`/`e` type to search for empty files and/or directories. To search for both empty files and directories, use one of the following: fd --type empty fd -te fd --type empty --type file --type directory To search for empty files, use fd --type empty --type file fd -te -tf To search for empty directories, use fd --type empty --type directory fd -te -td closes #273
authored by sharkdp · committed by sharkdp · parent 50a2bab · Browse files · 7 files·+68 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+68 added−3 removed
- tests:+27 added−0 removed
- other:+41 added−3 removed
Provenance
- Commit
aa70c5a- Tree
bbfc0fb- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
50a2bab
7 files changed+68 added−3 removed
modified:doc/fd.1+2 added
84 unmodified lines85 .IP "l, symlink"86 symbolic links87 .IP "x, executable"88 executable (files)89+.IP "e, empty"90+empty files or directories91 .RE92 93 .RS94 This option can be used repeatedly to allow for multiple file types.65 unmodified linesmodified:src/fshelper/mod.rs+21 added-1 removed
11 unmodified lines12 #[cfg(any(unix, target_os = "redox"))]13 use std::os::unix::fs::PermissionsExt;14 use std::path::{Path, PathBuf};15 16+use ignore::DirEntry;17+18 pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {19 if path.is_absolute() {20 Ok(path.to_path_buf())21 } else {15 unmodified lines37 38 Ok(path_buf)39 }40 39-// Path::is_dir() is not guarandteed to be intuitively correct for "." and ".."41+// Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."42 // See: https://github.com/rust-lang/rust/issues/4530243 pub fn is_dir(path: &Path) -> bool {44 if path.file_name().is_some() {45 path.is_dir()10 unmodified lines56 #[cfg(windows)]57 pub fn is_executable(_: &fs::Metadata) -> bool {58 false59 }60+61+pub fn is_empty(entry: &DirEntry) -> bool {62+ if let Some(file_type) = entry.file_type() {63+ if file_type.is_dir() {64+ if let Ok(mut entries) = fs::read_dir(entry.path()) {65+ entries.next().is_none()66+ } else {67+ false68+ }69+ } else if file_type.is_file() {70+ entry.metadata().map(|m| m.len() == 0).unwrap_or(false)71+ } else {72+ false73+ }74+ } else {75+ false76+ }77+}modified:src/app.rs+5 added-2 removed
85 unmodified lines86 "l",87 "symlink",88 "x",89 "executable",90+ "e",91+ "empty",92 ]).hide_possible_values(true),93 ).arg(94 arg("extension")95 .long("extension")101 unmodified lines197 , "Set maximum search depth (default: none)"198 , "Limit the directory traversal to a given depth. By default, there is no limit \199 on the search depth.");200 doc!(h, "file-type"199- , "Filter by type: file (f), directory (d), symlink (l),\nexecutable (x)"201+ , "Filter by type: file (f), directory (d), symlink (l),\nexecutable (x), empty (e)"202 , "Filter the search by type (multiple allowable filetypes can be specified):\n \203 'f' or 'file': regular files\n \204 'd' or 'directory': directories\n \205 'l' or 'symlink': symbolic links\n \204- 'x' or 'executable': executables");206+ 'x' or 'executable': executables\n \207+ 'e' or 'empty': empty files or directories");208 doc!(h, "extension"209 , "Filter by file extension"210 , "(Additionally) filter search results by their file extension. Multiple allowable file \211 extensions can be specified.");61 unmodified linesmodified:src/internal.rs+2 added
26 unmodified lines27 pub files: bool,28 pub directories: bool,29 pub symlinks: bool,30 pub executables_only: bool,31+ pub empty_only: bool,32 }33 34 impl Default for FileTypes {35 fn default() -> FileTypes {1 unmodified line37 files: false,38 directories: false,39 symlinks: false,40 executables_only: false,41+ empty_only: false,42 }43 }44 }45 436 unmodified linesmodified:src/main.rs+10 added
180 unmodified lines181 "x" | "executable" => {182 file_types.executables_only = true;183 file_types.files = true;184 }185+ "e" | "empty" => {186+ file_types.empty_only = true;187+ }188 _ => unreachable!(),189 }190 }191+192+ // If only 'empty' was specified, search for both files and directories:193+ if file_types.empty_only && !(file_types.files || file_types.directories) {194+ file_types.files = true;195+ file_types.directories = true;196+ }197+198 file_types199 }),200 extensions: matches.values_of("extension").map(|exts| {201 let patterns = exts37 unmodified linesmodified:src/walk.rs+1 added
221 unmodified lines222 || (file_types.executables_only && !entry223 .metadata()224 .map(|m| fshelper::is_executable(&m))225 .unwrap_or(false))226+ || (file_types.empty_only && !fshelper::is_empty(&entry))227 {228 return ignore::WalkState::Continue;229 } else if !(entry_type.is_file()230 || entry_type.is_dir()74 unmodified linesmodified:tests/tests.rs+27 added
642 unmodified lines643 one/two/three/directory_foo",644 );645 }646 647+/// Test `--type empty`648+#[test]649+fn test_type_empty() {650+ let te = TestEnv::new(&["dir_empty", "dir_nonempty"], &[]);651+652+ create_file_with_size(te.test_root().join("0_bytes.foo"), 0);653+ create_file_with_size(te.test_root().join("5_bytes.foo"), 5);654+655+ create_file_with_size(te.test_root().join("dir_nonempty").join("2_bytes.foo"), 2);656+657+ te.assert_output(658+ &["--type", "empty"],659+ "0_bytes.foo660+ dir_empty",661+ );662+663+ te.assert_output(664+ &["--type", "empty", "--type", "file", "--type", "directory"],665+ "0_bytes.foo666+ dir_empty",667+ );668+669+ te.assert_output(&["--type", "empty", "--type", "file"], "0_bytes.foo");670+671+ te.assert_output(&["--type", "empty", "--type", "directory"], "dir_empty");672+}673+674 /// File extension (--extension)675 #[test]676 fn test_extension() {677 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);434 unmodified lines