Merge pull request #2143 from tmccombs/broken-symlink-refactor
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by GitHub · parents 3460b1e, 3b3baea · Browse files · 1 file·+50 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- refactor: Avoid depending on ignore implementation detail
Diff stats
1 file+50 added−32 removed
- other:+50 added−32 removed
Provenance
- Commit
9fe5cb7- Tree
33667e9- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
3460b1e- Parent 2
3b3baea
1 file changed+50 added−32 removed
modified:src/walk.rs+50 added-32 removed
1 use std::borrow::Cow;2 use std::ffi::OsStr;3 use std::io::{self, Write};4 use std::mem;5-use std::path::{Path, PathBuf};5+use std::path::PathBuf;6 use std::sync::atomic::{AtomicBool, Ordering};7 use std::sync::{Arc, Mutex, MutexGuard};8 use std::thread;9 use std::time::{Duration, Instant};472 unmodified lines482 }483 }484 let entry = match entry {485 Ok(e) => DirEntry::normal(e),486- Err(err) => {487- // The depth has to be read off the error before it is488- // taken apart, since it is recorded on an inner variant.489- let depth = err.depth();490- match err {491- ignore::Error::WithPath {492- path,493- err: inner_err,494- } if is_broken_symlink(&path, &inner_err) => {495- DirEntry::broken_symlink(path, depth)496- }497- err => {498- return match tx.send(WorkerResult::Error(err)) {499- Ok(_) => WalkState::Continue,500- Err(_) => WalkState::Quit,501- };486+ Err(err) => match broken_symlink_from_err(err) {487+ Ok(entry) => entry,488+ Err(err) => {489+ return match tx.send(WorkerResult::Error(err)) {490+ Ok(_) => WalkState::Continue,491+ Err(_) => WalkState::Quit,492+ };502- }493 }504- }494+ },495 };496 497 if let Some(min_depth) = config.min_depth498 && entry.depth().is_none_or(|d| d < min_depth)142 unmodified lines641 }642 }643 }644 655-/// Whether a walk error is really a broken symlink rather than a failure worth656-/// reporting.657-///658-/// A symlink whose target is missing is surfaced by the walker as a NotFound659-/// error against the link's own path, so it never arrives as an entry. fd still660-/// wants to match and print it (see issue #1017), which means recovering it here.661-fn is_broken_symlink(path: &Path, err: &ignore::Error) -> bool {662- err.io_error()663- .is_some_and(|io_error| io_error.kind() == io::ErrorKind::NotFound)664- && path665- .symlink_metadata()666- .ok()667- .is_some_and(|m| m.file_type().is_symlink())645+/// If `err` is for a broken symlink, create a broken symlink DirEntry,646+/// otherwise, return the original error.647+fn broken_symlink_from_err(mut err: ignore::Error) -> Result<DirEntry, ignore::Error> {648+ use ignore::Error::*;649+ let mut depth = None;650+ let mut path = None;651+ let mut current = &mut err;652+ // Loop through the items in the chain, and extract the path and depth as we find them.653+ // Then once we get to the (terminal) io error, check that it is for a broken symlink.654+ loop {655+ match current {656+ WithPath { err, path: p } => {657+ path = Some(p);658+ current = err;659+ }660+ WithDepth { err, depth: d } => {661+ depth = Some(*d);662+ current = err;663+ }664+ Io(e) if e.kind() == io::ErrorKind::NotFound => {665+ if let Some(path) = path666+ && path667+ .symlink_metadata()668+ .ok()669+ .is_some_and(|m| m.file_type().is_symlink())670+ {671+ // Since we no longer need the original error, we can take the PathBuf of the672+ // path out of it.673+ return Ok(DirEntry::broken_symlink(mem::take(path), depth));674+ } else {675+ // This is pretty unlikely.676+ return Err(err);677+ }678+ }679+ // We don't care about Loop because that won't happen680+ // for broken symlinks, and there are two paths involved681+ // and we don't care about WithLineNumber because that only applies682+ // to errors while parsing ignore files.683+ _ => return Err(err),684+ }685+ }686 }687 688 fn search_str_for_entry<'a>(689 entry_path: &'a std::path::Path,87 unmodified lines