Fix directory-existence check on Windows
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This fixes a bug on Windows where `fd` could not be used on ram disks and encrypted folders. closes #752
authored by David Peter · committed by David Peter · parent 6e44828 · Browse files · 4 files·+23 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
4 files+23 added−8 removed
- other:+23 added−8 removed
Provenance
- Commit
2d398dc- Tree
392031b- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
6e44828
4 files changed+23 added−8 removed
modified:src/filesystem.rs+6 added-4 removed
5 unmodified lines6 #[cfg(any(unix, target_os = "redox"))]7 use std::os::unix::fs::{FileTypeExt, PermissionsExt};8 use std::path::{Path, PathBuf};9 10+use normpath::PathExt;11+12 use crate::walk;13 14 pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {15 if path.is_absolute() {18 unmodified lines34 35 Ok(path_buf)36 }37 36-// Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."37-// See: https://github.com/rust-lang/rust/issues/4530238-pub fn is_dir(path: &Path) -> bool {39- path.is_dir() && (path.file_name().is_some() || path.canonicalize().is_ok())38+pub fn is_existing_directory(path: &Path) -> bool {39+ // Note: we do not use `.exists()` here, as `.` always exists, even if40+ // the CWD has been deleted.41+ path.is_dir() && (path.file_name().is_some() || path.normalize().is_ok())42 }43 44 #[cfg(any(unix, target_os = "redox"))]45 pub fn is_executable(md: &fs::Metadata) -> bool {103 unmodified linesmodified:src/main.rs+4 added-4 removed
53 unmodified lines54 55 // Set the current working directory of the process56 if let Some(base_directory) = matches.value_of_os("base-directory") {57 let base_directory = Path::new(base_directory);58- if !filesystem::is_dir(base_directory) {58+ if !filesystem::is_existing_directory(base_directory) {59 return Err(anyhow!(60 "The '--base-directory' path '{}' is not a directory.",61 base_directory.to_string_lossy()62 ));6 unmodified lines69 })?;70 }71 72 let current_directory = Path::new(".");73- if !filesystem::is_dir(current_directory) {73+ if !filesystem::is_existing_directory(current_directory) {74 return Err(anyhow!(75 "Could not retrieve current directory (has it been deleted?)."76 ));77 }16 unmodified lines94 let mut search_paths = if let Some(paths) = passed_arguments {95 let mut directories = vec![];96 for path in paths {97 let path_buffer = PathBuf::from(path);98- if filesystem::is_dir(&path_buffer) {98+ if filesystem::is_existing_directory(&path_buffer) {99 directories.push(path_buffer);100 } else {101 print_error(format!(102 "Search path '{}' is not a directory.",26 unmodified lines129 130 // Detect if the user accidentally supplied a path instead of a search pattern131 if !matches.is_present("full-path")132 && pattern.contains(std::path::MAIN_SEPARATOR)133- && filesystem::is_dir(Path::new(pattern))133+ && Path::new(pattern).is_dir()134 {135 return Err(anyhow!(136 "The search pattern '{pattern}' contains a path-separation character ('{sep}') \137 and will not lead to any search results.\n\n\353 unmodified linesmodified:Cargo.lock+12 added
1 # This file is automatically @generated by Cargo.2 # It is not intended for manual editing.3+version = 34+5 [[package]]6 name = "aho-corasick"7 version = "0.7.18"8 source = "registry+https://github.com/rust-lang/crates.io-index"158 unmodified lines167 "jemallocator",168 "lazy_static",169 "libc",170 "lscolors",171+ "normpath",172 "num_cpus",173 "regex",174 "regex-syntax",175 "tempdir",157 unmodified lines333 "libc",334 ]335 336 [[package]]337+name = "normpath"338+version = "0.3.0"339+source = "registry+https://github.com/rust-lang/crates.io-index"340+checksum = "27e6e8f70e9fbbe3752d330d769e3424f24b9458ce266df93a3b456902fd696a"341+dependencies = [342+ "winapi",343+]344+345+[[package]]346 name = "num_cpus"347 version = "1.13.0"348 source = "registry+https://github.com/rust-lang/crates.io-index"349 checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"222 unmodified linesmodified:Cargo.toml+1 added
45 unmodified lines46474849505152535421 unmodified lines45 unmodified lineslscolors = "0.7"globset = "0.4"anyhow = "1.0"dirs-next = "2.0"normpath = "0.3"
[dependencies.clap]version = "2.31.2"features = ["suggestions", "color", "wrap_help"]21 unmodified lines