Skip to content
Myr

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

4 files+96 added−96 removed

deleted:src/fshelper/mod.rs-84 removed
1use std::borrow::Cow;2use std::env::current_dir;3use std::ffi::OsStr;4use std::fs;5use std::io;6#[cfg(any(unix, target_os = "redox"))]7use std::os::unix::fs::PermissionsExt;8use std::path::{Path, PathBuf};910use crate::walk;1112pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {13 if path.is_absolute() {14 return Ok(path.to_path_buf());15 }1617 let path = path.strip_prefix(".").unwrap_or(path);18 current_dir().map(|path_buf| path_buf.join(path))19}2021pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {22 let path_buf = path_absolute_form(path)?;2324 #[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();3233 Ok(path_buf)34}3536// Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."37// See: https://github.com/rust-lang/rust/issues/4530238pub fn is_dir(path: &Path) -> bool {39 path.is_dir() && (path.file_name().is_some() || path.canonicalize().is_ok())40}4142#[cfg(any(unix, target_os = "redox"))]43pub fn is_executable(md: &fs::Metadata) -> bool {44 md.permissions().mode() & 0o111 != 045}4647#[cfg(windows)]48pub fn is_executable(_: &fs::Metadata) -> bool {49 false50}5152pub 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}6970#[cfg(any(unix, target_os = "redox"))]71pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {72 use std::os::unix::ffi::OsStrExt;73 Cow::Borrowed(input.as_bytes())74}7576#[cfg(windows)]77pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {78 let string = input.to_string_lossy();7980 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
1use std::borrow::Cow;2use std::env::current_dir;3use std::ffi::OsStr;4use std::fs;5use std::io;6#[cfg(any(unix, target_os = "redox"))]7use std::os::unix::fs::PermissionsExt;8use std::path::{Path, PathBuf};910use crate::walk;1112pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {13 if path.is_absolute() {14 return Ok(path.to_path_buf());15 }1617 let path = path.strip_prefix(".").unwrap_or(path);18 current_dir().map(|path_buf| path_buf.join(path))19}2021pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {22 let path_buf = path_absolute_form(path)?;2324 #[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();3233 Ok(path_buf)34}3536// Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."37// See: https://github.com/rust-lang/rust/issues/4530238pub fn is_dir(path: &Path) -> bool {39 path.is_dir() && (path.file_name().is_some() || path.canonicalize().is_ok())40}4142#[cfg(any(unix, target_os = "redox"))]43pub fn is_executable(md: &fs::Metadata) -> bool {44 md.permissions().mode() & 0o111 != 045}4647#[cfg(windows)]48pub fn is_executable(_: &fs::Metadata) -> bool {49 false50}5152pub 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}6970#[cfg(any(unix, target_os = "redox"))]71pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {72 use std::os::unix::ffi::OsStrExt;73 Cow::Borrowed(input.as_bytes())74}7576#[cfg(windows)]77pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {78 let string = input.to_string_lossy();7980 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 lines
34mod app;5mod exec;6mod exit_codes;7mod filesystem;8mod filetypes;9mod filter;9mod fshelper;10mod options;11mod output;12mod regex_helper;13mod walk;
25 unmodified lines
3940 // 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 lines
59 let pattern = matches.value_of("pattern").unwrap_or("");6061 // 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 }6667 // Get one or more root directories to search.
3 unmodified lines
71 {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 lines
89 .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 }9899 // 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 lines
modified:src/walk.rs+6 added-6 removed
1use crate::exec;2use crate::exit_codes::{merge_exitcodes, ExitCode};3use crate::fshelper;3use crate::filesystem;4use crate::options::Options;5use crate::output;67use std::borrow::Cow;
335 unmodified lines
343 // Check the name first, since it doesn't require metadata344 let entry_path = entry.path();345346 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 lines
360 ),361 }362 };363364 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 }367368 // 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 lines
384 || (!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