Rename fshelper to filesystem
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 e0cba4b · Browse files · 4 files·+96 added−96 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+96 added−96 removed
- other:+96 added−96 removed
Provenance
- Commit
b415d72- Tree
12f14e0- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e0cba4b
4 files changed+96 added−96 removed
deleted:src/fshelper/mod.rs-84 removed
1-use std::borrow::Cow;2-use std::env::current_dir;3-use std::ffi::OsStr;4-use std::fs;5-use std::io;6-#[cfg(any(unix, target_os = "redox"))]7-use std::os::unix::fs::PermissionsExt;8-use std::path::{Path, PathBuf};9-10-use crate::walk;11-12-pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {13- if path.is_absolute() {14- return Ok(path.to_path_buf());15- }16-17- let path = path.strip_prefix(".").unwrap_or(path);18- current_dir().map(|path_buf| path_buf.join(path))19-}20-21-pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {22- let path_buf = path_absolute_form(path)?;23-24- #[cfg(windows)]25- let path_buf = Path::new(26- path_buf27- .as_path()28- .to_string_lossy()29- .trim_start_matches(r"\\?\"),30- )31- .to_path_buf();32-33- Ok(path_buf)34-}35-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())40-}41-42-#[cfg(any(unix, target_os = "redox"))]43-pub fn is_executable(md: &fs::Metadata) -> bool {44- md.permissions().mode() & 0o111 != 045-}46-47-#[cfg(windows)]48-pub fn is_executable(_: &fs::Metadata) -> bool {49- false50-}51-52-pub fn is_empty(entry: &walk::DirEntry) -> bool {53- if let Some(file_type) = entry.file_type() {54- if file_type.is_dir() {55- if let Ok(mut entries) = fs::read_dir(entry.path()) {56- entries.next().is_none()57- } else {58- false59- }60- } else if file_type.is_file() {61- entry.metadata().map(|m| m.len() == 0).unwrap_or(false)62- } else {63- false64- }65- } else {66- false67- }68-}69-70-#[cfg(any(unix, target_os = "redox"))]71-pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {72- use std::os::unix::ffi::OsStrExt;73- Cow::Borrowed(input.as_bytes())74-}75-76-#[cfg(windows)]77-pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {78- let string = input.to_string_lossy();79-80- match string {81- Cow::Owned(string) => Cow::Owned(string.into_bytes()),82- Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),83- }84-}added:src/filesystem.rs+84 added
1+use std::borrow::Cow;2+use std::env::current_dir;3+use std::ffi::OsStr;4+use std::fs;5+use std::io;6+#[cfg(any(unix, target_os = "redox"))]7+use std::os::unix::fs::PermissionsExt;8+use std::path::{Path, PathBuf};9+10+use crate::walk;11+12+pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {13+ if path.is_absolute() {14+ return Ok(path.to_path_buf());15+ }16+17+ let path = path.strip_prefix(".").unwrap_or(path);18+ current_dir().map(|path_buf| path_buf.join(path))19+}20+21+pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {22+ let path_buf = path_absolute_form(path)?;23+24+ #[cfg(windows)]25+ let path_buf = Path::new(26+ path_buf27+ .as_path()28+ .to_string_lossy()29+ .trim_start_matches(r"\\?\"),30+ )31+ .to_path_buf();32+33+ Ok(path_buf)34+}35+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())40+}41+42+#[cfg(any(unix, target_os = "redox"))]43+pub fn is_executable(md: &fs::Metadata) -> bool {44+ md.permissions().mode() & 0o111 != 045+}46+47+#[cfg(windows)]48+pub fn is_executable(_: &fs::Metadata) -> bool {49+ false50+}51+52+pub fn is_empty(entry: &walk::DirEntry) -> bool {53+ if let Some(file_type) = entry.file_type() {54+ if file_type.is_dir() {55+ if let Ok(mut entries) = fs::read_dir(entry.path()) {56+ entries.next().is_none()57+ } else {58+ false59+ }60+ } else if file_type.is_file() {61+ entry.metadata().map(|m| m.len() == 0).unwrap_or(false)62+ } else {63+ false64+ }65+ } else {66+ false67+ }68+}69+70+#[cfg(any(unix, target_os = "redox"))]71+pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {72+ use std::os::unix::ffi::OsStrExt;73+ Cow::Borrowed(input.as_bytes())74+}75+76+#[cfg(windows)]77+pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {78+ let string = input.to_string_lossy();79+80+ match string {81+ Cow::Owned(string) => Cow::Owned(string.into_bytes()),82+ Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),83+ }84+}modified:src/main.rs+6 added-6 removed
2 unmodified lines3 4 mod app;5 mod exec;6 mod exit_codes;7+mod filesystem;8 mod filetypes;9 mod filter;9-mod fshelper;10 mod options;11 mod output;12 mod regex_helper;13 mod walk;25 unmodified lines39 40 // Set the current working directory of the process41 if let Some(base_directory) = matches.value_of("base-directory") {42 let basedir = Path::new(base_directory);43- if !fshelper::is_dir(basedir) {43+ if !filesystem::is_dir(basedir) {44 print_error_and_exit!(45 "The '--base-directory' path ('{}') is not a directory.",46 basedir.to_string_lossy()47 );11 unmodified lines59 let pattern = matches.value_of("pattern").unwrap_or("");60 61 // Get the current working directory62 let current_dir = Path::new(".");63- if !fshelper::is_dir(current_dir) {63+ if !filesystem::is_dir(current_dir) {64 print_error_and_exit!("Could not get current directory.");65 }66 67 // Get one or more root directories to search.3 unmodified lines71 {72 Some(paths) => paths73 .map(|path| {74 let path_buffer = PathBuf::from(path);75- if !fshelper::is_dir(&path_buffer) {75+ if !filesystem::is_dir(&path_buffer) {76 print_error_and_exit!(77 "'{}' is not a directory.",78 path_buffer.to_string_lossy()79 );9 unmodified lines89 .iter()90 .map(|path_buffer| {91 path_buffer92 .canonicalize()93- .and_then(|pb| fshelper::absolute_path(pb.as_path()))93+ .and_then(|pb| filesystem::absolute_path(pb.as_path()))94 .unwrap()95 })96 .collect();97 }98 99 // Detect if the user accidentally supplied a path instead of a search pattern100 if !matches.is_present("full-path")101 && pattern.contains(std::path::MAIN_SEPARATOR)102- && fshelper::is_dir(Path::new(pattern))102+ && filesystem::is_dir(Path::new(pattern))103 {104 print_error_and_exit!(105 "The search pattern '{pattern}' contains a path-separation character ('{sep}') \106 and will not lead to any search results.\n\n\212 unmodified linesmodified:src/walk.rs+6 added-6 removed
1 use crate::exec;2 use crate::exit_codes::{merge_exitcodes, ExitCode};3-use crate::fshelper;3+use crate::filesystem;4 use crate::options::Options;5 use crate::output;6 7 use std::borrow::Cow;335 unmodified lines343 // Check the name first, since it doesn't require metadata344 let entry_path = entry.path();345 346 let search_str: Cow<OsStr> = if config.search_full_path {347- match fshelper::path_absolute_form(entry_path) {347+ match filesystem::path_absolute_form(entry_path) {348 Ok(path_abs_buf) => Cow::Owned(path_abs_buf.as_os_str().to_os_string()),349 Err(_) => {350 print_error_and_exit!("Unable to retrieve absolute path.");351 }8 unmodified lines360 ),361 }362 };363 364- if !pattern.is_match(&fshelper::osstr_to_bytes(search_str.as_ref())) {364+ if !pattern.is_match(&filesystem::osstr_to_bytes(search_str.as_ref())) {365 return ignore::WalkState::Continue;366 }367 368 // Filter out unwanted extensions.369 if let Some(ref exts_regex) = config.extensions {370 if let Some(path_str) = entry_path.file_name() {371- if !exts_regex.is_match(&fshelper::osstr_to_bytes(path_str)) {371+ if !exts_regex.is_match(&filesystem::osstr_to_bytes(path_str)) {372 return ignore::WalkState::Continue;373 }374 } else {375 return ignore::WalkState::Continue;8 unmodified lines384 || (!file_types.symlinks && entry_type.is_symlink())385 || (file_types.executables_only386 && !entry387 .metadata()388- .map(|m| fshelper::is_executable(&m))388+ .map(|m| filesystem::is_executable(&m))389 .unwrap_or(false))390- || (file_types.empty_only && !fshelper::is_empty(&entry))390+ || (file_types.empty_only && !filesystem::is_empty(&entry))391 || !(entry_type.is_file() || entry_type.is_dir() || entry_type.is_symlink())392 {393 return ignore::WalkState::Continue;394 }51 unmodified lines