walk: catch ignore NotFound error in the case of a broken symlink
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Milligan · committed by David Peter · parent 06434f2 · Browse files · 1 file·+39 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
1 file+39 added−3 removed
- other:+39 added−3 removed
Provenance
- Commit
9d73402- Tree
c77a459- Author
Tom Milligan <tom@reinfer.io>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
06434f2
1 file changed+39 added−3 removed
modified:src/walk.rs+39 added-3 removed
11 unmodified lines12 use crate::internal::{opts::FdOptions, osstr_to_bytes, MAX_BUFFER_LENGTH};13 use crate::output;14 15 use std::borrow::Cow;16+use std::convert::TryFrom;17 use std::error::Error;18 use std::ffi::OsStr;19+use std::fs;20 use std::io;21 use std::path::PathBuf;22 use std::process;23 use std::sync::atomic::{AtomicBool, Ordering};21 unmodified lines45 Entry(PathBuf),46 Error(ignore::Error),47 }48 49+/// Converts a WorkerResult to a PathBuf. This is required as in some cases,50+/// the underlying `ignore` libary will return an error when we actually51+/// still want to show the path.52+///53+/// Currently, the cases supported are:54+/// - a broken symlink should still be found and the path returned55+impl TryFrom<WorkerResult> for PathBuf {56+ type Error = ignore::Error;57+58+ fn try_from(worker_result: WorkerResult) -> Result<PathBuf, Self::Error> {59+ match worker_result {60+ WorkerResult::Entry(value) => return Ok(value),61+ WorkerResult::Error(err) => {62+ if let ignore::Error::WithPath {63+ path,64+ err: path_err,65+ } = &err66+ {67+ if let ignore::Error::Io(io_err) = &**path_err {68+ if io_err.kind() == io::ErrorKind::NotFound {69+ if let Ok(metadata) = fs::symlink_metadata(&path) {70+ if metadata.file_type().is_symlink() {71+ return Ok(path.to_path_buf());72+ }73+ }74+ }75+ }76+ }77+ return Err(err);78+ }79+ }80+ }81+}82+83 /// Recursively scan the given search path for files / pathnames matching the pattern.84 ///85 /// If the `--exec` argument was supplied, this will create a thread pool for executing86 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each149 unmodified lines236 let stdout = io::stdout();237 let mut stdout = stdout.lock();238 239 for worker_result in rx {204- match worker_result {205- WorkerResult::Entry(value) => {240+ match PathBuf::try_from(worker_result) {241+ Ok(value) => {242 match mode {243 ReceiverMode::Buffering => {244 buffer.push(value);245 20 unmodified lines266 output::print_entry(&mut stdout, &value, &config, &wants_to_quit);267 }268 }269 }234- WorkerResult::Error(err) => {270+ Err(err) => {271 if show_filesystem_errors {272 print_error!("{}", err);273 }274 }150 unmodified lines