Skip to content

Commit

Permalink
Wrap walkdir::DirEntry in a new type (#436)
Browse files Browse the repository at this point in the history
* Add -follow support.

* tests/find: Serialize find_time()

find_time() relies on the working directory, but e.g.
delete_on_dot_dir() will temporarily change directories, causing
find_time() to fail when run in parallel.

* find: Don't use uutils::error::set_exit_code()

The global exit code can polute the results of other tests.

Link: uutils/coreutils#5777

* find: New WalkEntry wrapper

The new type wraps DirEntry when possible, but also lets us pass a valid
entry to matchers when walkdir returns a broken symlink error.  It also
implements a Metadata cache (part of #430).

* find: Implement -H, -L, -P flags

* find: Fix -follow -samefile

* find: Fix -follow -newer

* find: Implement -xtype

* find: Fix -delete error handling

---------

Co-authored-by: hanbings <hanbings@hanbings.io>
  • Loading branch information
tavianator and hanbings authored Aug 14, 2024
1 parent 465856c commit a7a73c3
Show file tree
Hide file tree
Showing 29 changed files with 948 additions and 356 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/find/matchers/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
// https://opensource.org/licenses/MIT.

use faccess::PathExt;
use walkdir::DirEntry;

use super::{Matcher, MatcherIO};
use super::{Matcher, MatcherIO, WalkEntry};

/// Matcher for -{read,writ,execut}able.
pub enum AccessMatcher {
Expand All @@ -17,7 +16,7 @@ pub enum AccessMatcher {
}

impl Matcher for AccessMatcher {
fn matches(&self, file_info: &DirEntry, _: &mut MatcherIO) -> bool {
fn matches(&self, file_info: &WalkEntry, _: &mut MatcherIO) -> bool {
let path = file_info.path();

match self {
Expand Down
20 changes: 9 additions & 11 deletions src/find/matchers/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
* file that was distributed with this source code.
*/

use std::fs::{self, FileType};
use std::fs;
use std::io::{self, stderr, Write};
use std::path::Path;

use walkdir::DirEntry;

use super::{Matcher, MatcherIO};
use super::{Matcher, MatcherIO, WalkEntry};

pub struct DeleteMatcher;

Expand All @@ -22,17 +19,17 @@ impl DeleteMatcher {
DeleteMatcher
}

fn delete(&self, file_path: &Path, file_type: FileType) -> io::Result<()> {
if file_type.is_dir() {
fs::remove_dir(file_path)
fn delete(&self, entry: &WalkEntry) -> io::Result<()> {
if entry.file_type().is_dir() && !entry.path_is_symlink() {
fs::remove_dir(entry.path())
} else {
fs::remove_file(file_path)
fs::remove_file(entry.path())
}
}
}

impl Matcher for DeleteMatcher {
fn matches(&self, file_info: &DirEntry, _: &mut MatcherIO) -> bool {
fn matches(&self, file_info: &WalkEntry, matcher_io: &mut MatcherIO) -> bool {
let path = file_info.path();
let path_str = path.to_string_lossy();

Expand All @@ -43,9 +40,10 @@ impl Matcher for DeleteMatcher {
return true;
}

match self.delete(path, file_info.file_type()) {
match self.delete(file_info) {
Ok(()) => true,
Err(e) => {
matcher_io.set_exit_code(1);
writeln!(&mut stderr(), "Failed to delete {path_str}: {e}").unwrap();
false
}
Expand Down
4 changes: 2 additions & 2 deletions src/find/matchers/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
io::{stderr, Write},
};

use super::Matcher;
use super::{Matcher, MatcherIO, WalkEntry};

pub struct EmptyMatcher;

Expand All @@ -20,7 +20,7 @@ impl EmptyMatcher {
}

impl Matcher for EmptyMatcher {
fn matches(&self, file_info: &walkdir::DirEntry, _: &mut super::MatcherIO) -> bool {
fn matches(&self, file_info: &WalkEntry, _: &mut MatcherIO) -> bool {
if file_info.file_type().is_file() {
match file_info.metadata() {
Ok(meta) => meta.len() == 0,
Expand Down
Loading

0 comments on commit a7a73c3

Please sign in to comment.