New implementation of broken-symlink handling
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent bfc8c42 · Browse files · 2 files·+56 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+56 added−8 removed
- other:+56 added−8 removed
Provenance
- Commit
bbf0f1c- Tree
44f000f- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
bfc8c42
2 files changed+56 added−8 removed
modified:src/fshelper/mod.rs+2 added-2 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;16+use crate::walk;17 18 pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {19 if path.is_absolute() {20 return Ok(path.to_path_buf());33 unmodified lines54 pub fn is_executable(_: &fs::Metadata) -> bool {55 false56 }57 58-pub fn is_empty(entry: &DirEntry) -> bool {58+pub fn is_empty(entry: &walk::DirEntry) -> bool {59 if let Some(file_type) = entry.file_type() {60 if file_type.is_dir() {61 if let Ok(mut entries) = fs::read_dir(entry.path()) {62 entries.next().is_none()12 unmodified linesmodified:src/walk.rs+54 added-6 removed
13 unmodified lines14 15 use std::borrow::Cow;16 use std::error::Error;17 use std::ffi::OsStr;18+use std::fs::{FileType, Metadata};19 use std::io;19-use std::path::PathBuf;20+use std::path::{Path, PathBuf};21 use std::process;22 use std::sync::atomic::{AtomicBool, Ordering};23 use std::sync::mpsc::{channel, Receiver, Sender};24 use std::sync::{Arc, Mutex};228 unmodified lines253 }254 })255 }256 257+pub enum DirEntry {258+ Normal(ignore::DirEntry),259+ BrokenSymlink(PathBuf),260+}261+262+impl DirEntry {263+ pub fn path(&self) -> &Path {264+ match self {265+ DirEntry::Normal(e) => e.path(),266+ DirEntry::BrokenSymlink(pathbuf) => pathbuf.as_path(),267+ }268+ }269+270+ pub fn file_type(&self) -> Option<FileType> {271+ match self {272+ DirEntry::Normal(e) => e.file_type(),273+ DirEntry::BrokenSymlink(pathbuf) => {274+ pathbuf.symlink_metadata().map(|m| m.file_type()).ok()275+ }276+ }277+ }278+279+ pub fn metadata(&self) -> Option<Metadata> {280+ match self {281+ DirEntry::Normal(e) => e.metadata().ok(),282+ DirEntry::BrokenSymlink(_) => None,283+ }284+ }285+}286+287 fn spawn_senders(288 config: &Arc<FdOptions>,289 wants_to_quit: &Arc<AtomicBool>,290 pattern: Arc<Regex>,11 unmodified lines302 return ignore::WalkState::Quit;303 }304 305 let entry = match entry_o {275- Ok(e) => e,306+ Ok(e) if e.depth() == 0 => {307+ // Skip the root directory entry.308+ return ignore::WalkState::Continue;309+ }310+ Ok(e) => DirEntry::Normal(e),311+ Err(ignore::Error::WithPath {312+ path,313+ err: inner_err,314+ }) => match inner_err.as_ref() {315+ ignore::Error::Io(io_error) if io_error.kind() == io::ErrorKind::NotFound => {316+ DirEntry::BrokenSymlink(path.to_owned())317+ }318+ _ => {319+ tx_thread320+ .send(WorkerResult::Error(ignore::Error::WithPath {321+ path,322+ err: inner_err,323+ }))324+ .unwrap();325+ return ignore::WalkState::Continue;326+ }327+ },328 Err(err) => {329 tx_thread.send(WorkerResult::Error(err)).unwrap();330 return ignore::WalkState::Continue;331 }332 };333 282- if entry.depth() == 0 {283- return ignore::WalkState::Continue;284- }285-334 // Check the name first, since it doesn't require metadata335 let entry_path = entry.path();336 337 let search_str: Cow<OsStr> = if config.search_full_path {99 unmodified lines