Skip to content
Myr

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

7 files+68 added−3 removed

modified:doc/fd.1+2 added
84 unmodified lines
85.IP "l, symlink"86symbolic links87.IP "x, executable"88executable (files)89.IP "e, empty"90empty files or directories91.RE9293.RS94This option can be used repeatedly to allow for multiple file types.
65 unmodified lines
modified:src/fshelper/mod.rs+21 added-1 removed
11 unmodified lines
12#[cfg(any(unix, target_os = "redox"))]13use std::os::unix::fs::PermissionsExt;14use std::path::{Path, PathBuf};1516use ignore::DirEntry;1718pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {19 if path.is_absolute() {20 Ok(path.to_path_buf())21 } else {
15 unmodified lines
3738 Ok(path_buf)39}4039// 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/4530243pub fn is_dir(path: &Path) -> bool {44 if path.file_name().is_some() {45 path.is_dir()
10 unmodified lines
56#[cfg(windows)]57pub fn is_executable(_: &fs::Metadata) -> bool {58 false59}6061pub 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 lines
86 "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 lines
197 , "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 lines
modified:src/internal.rs+2 added
26 unmodified lines
27 pub files: bool,28 pub directories: bool,29 pub symlinks: bool,30 pub executables_only: bool,31 pub empty_only: bool,32}3334impl Default for FileTypes {35 fn default() -> FileTypes {
1 unmodified line
37 files: false,38 directories: false,39 symlinks: false,40 executables_only: false,41 empty_only: false,42 }43 }44}45
436 unmodified lines
modified:src/main.rs+10 added
180 unmodified lines
181 "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 }191192 // 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 }197198 file_types199 }),200 extensions: matches.values_of("extension").map(|exts| {201 let patterns = exts
37 unmodified lines
modified:src/walk.rs+1 added
221 unmodified lines
222 || (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 lines
modified:tests/tests.rs+27 added
642 unmodified lines
643 one/two/three/directory_foo",644 );645}646647/// Test `--type empty`648#[test]649fn test_type_empty() {650 let te = TestEnv::new(&["dir_empty", "dir_nonempty"], &[]);651652 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);654655 create_file_with_size(te.test_root().join("dir_nonempty").join("2_bytes.foo"), 2);656657 te.assert_output(658 &["--type", "empty"],659 "0_bytes.foo660 dir_empty",661 );662663 te.assert_output(664 &["--type", "empty", "--type", "file", "--type", "directory"],665 "0_bytes.foo666 dir_empty",667 );668669 te.assert_output(&["--type", "empty", "--type", "file"], "0_bytes.foo");670671 te.assert_output(&["--type", "empty", "--type", "directory"], "dir_empty");672}673674/// File extension (--extension)675#[test]676fn test_extension() {677 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
434 unmodified lines