pathutil.rs
This file
Path
crates/ignore/src/pathutil.rs
171 lines · 4.7 KiB · Rust
Last changed by
ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
History of this fileTake it
The walk stopped after 200 commits: lines older than d869038 point at it.
ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 1–4why, one step back1use std::{ffi::OsStr, path::Path};23use crate::walk::DirEntry;4ignore: add routine for checking if a path is hidden or notAndrew Gallantbefore adoption6e527e9
lines 5–40why, one step back5/// Returns true if and only if this path is considered to be hidden.6///7/// # Platform behavior8///9/// ## Windows10///11/// This returns true if one of the following is true:12///13/// * The base name of the path starts with a `.`.14/// * The file attributes have the `HIDDEN` property set.15///16/// ## All other platforms17///18/// This only returns true if the base name of the path starts with a `.`.19pub(crate) fn is_hidden_path(dent: &Path) -> bool {20 #[cfg(not(windows))]21 fn imp(path: &Path) -> bool {22 is_hidden_path_only(path)23 }2425 #[cfg(windows)]26 fn imp(path: &Path) -> bool {27 use std::os::windows::fs::MetadataExt;28 use winapi_util::file;2930 if let Ok(md) = path.metadata() {31 if file::is_hidden(md.file_attributes() as u64) {32 return true;33 }34 }35 is_hidden_path_only(path)36 }3738 imp(dent)39}40ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
lines 41–42why, one step back41/// Returns true if and only if this directory entry is considered to be42/// hidden.ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 43why, one step back43///ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
line 44why, one step back44/// # Platform behaviorignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 45why, one step back45///ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
line 46why, one step back46/// ## Windowsignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 47why, one step back47///ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
line 48why, one step back48/// This returns true if one of the following is true:ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 49–51why, one step back49///50/// * The base name of the path starts with a `.`.51/// * The file attributes have the `HIDDEN` property set.ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
lines 52–56why, one step back52///53/// ## All other platforms54///55/// This only returns true if the base name of the path starts with a `.`.56pub(crate) fn is_hidden_entry(dent: &DirEntry) -> bool {ignore: add routine for checking if a path is hidden or notAndrew Gallantbefore adoption6e527e9
line 57why, one step back57 #[cfg(not(windows))]ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
line 58why, one step back58 fn imp(dent: &DirEntry) -> bool {ignore: add routine for checking if a path is hidden or notAndrew Gallantbefore adoption6e527e9
line 59why, one step back59 is_hidden_path_only(dent.path())ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 60why, one step back60 }ignore: refactor `is_hidden`Andrew Gallantbefore adoption626b895
lines 61–74why, one step back6162 #[cfg(windows)]63 fn imp(dent: &DirEntry) -> bool {64 use std::os::windows::fs::MetadataExt;65 use winapi_util::file;6667 // This looks like we're doing an extra stat call, but on Windows, the68 // directory traverser reuses the metadata retrieved from each directory69 // entry and stores it on the DirEntry itself. So this is "free."70 if let Ok(md) = dent.metadata() {71 if file::is_hidden(md.file_attributes() as u64) {72 return true;73 }74 }ignore: add routine for checking if a path is hidden or notAndrew Gallantbefore adoption6e527e9
lines 75–85why, one step back75 is_hidden_path_only(dent.path())76 }7778 imp(dent)79}8081/// Returns true if and only if this path is considered to be hidden from only82/// the path itself.83///84/// This has the same behavior on all platforms.85fn is_hidden_path_only(path: &Path) -> bool {ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 86–89why, one step back86 if let Some(name) = file_name(path) {87 name.as_encoded_bytes().starts_with(b".")88 } else {89 falseignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 90–99why, one step back90 }91}9293/// Strip `prefix` from the `path` and return the remainder.94///95/// If `path` doesn't have a prefix `prefix`, then return `None`.96pub(crate) fn strip_prefix<'a, P: AsRef<Path> + ?Sized>(97 prefix: &'a P,98 path: &'a Path,99) -> Option<&'a Path> {ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 100–102why, one step back100 #[cfg(unix)]101 fn imp<'a>(prefix: &'a Path, path: &'a Path) -> Option<&'a Path> {102 use std::os::unix::ffi::OsStrExt;ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 103why, one step back103ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 104–110why, one step back104 let prefix = prefix.as_os_str().as_bytes();105 let path = path.as_os_str().as_bytes();106 if prefix.len() > path.len() || prefix != &path[0..prefix.len()] {107 None108 } else {109 Some(&Path::new(OsStr::from_bytes(&path[prefix.len()..])))110 }ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 111–112why, one step back111 }112ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 113–116why, one step back113 #[cfg(not(unix))]114 fn imp<'a>(prefix: &'a Path, path: &'a Path) -> Option<&'a Path> {115 path.strip_prefix(prefix).ok()116 }ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 117why, one step back117ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
line 118why, one step back118 imp(prefix.as_ref(), path)ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 119–123why, one step back119}120121/// Returns true if this file path is just a file name. i.e., Its parent is122/// the empty string.123pub(crate) fn is_file_name<P: AsRef<Path>>(path: P) -> bool {ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 124–135why, one step back124 #[cfg(unix)]125 {126 memchr::memchr(b'/', path.as_ref().as_os_str().as_encoded_bytes())127 .is_none()128 }129 #[cfg(not(unix))]130 {131 path.as_ref()132 .parent()133 .map(|p| p.as_os_str().is_empty())134 .unwrap_or(false)135 }ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 136–139why, one step back136}137138/// The final component of the path, if it is a normal file.139///ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 140–141why, one step back140/// If the path terminates in `.`, `..`, or consists solely of a root of141/// prefix, this will return `None`.ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 142–144why, one step back142pub(crate) fn file_name<'a, P: AsRef<Path> + ?Sized>(143 path: &'a P,144) -> Option<&'a OsStr> {ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 145–147why, one step back145 #[cfg(unix)]146 fn imp(path: &Path) -> Option<&OsStr> {147 use std::os::unix::ffi::OsStrExt;ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 148why, one step back148ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 149–162why, one step back149 use memchr::memrchr;150151 let path = path.as_os_str().as_bytes();152 if path.is_empty() {153 return None;154 } else if path.len() == 1 && path[0] == b'.' {155 return None;156 } else if path.last() == Some(&b'.') {157 return None;158 } else if path.len() >= 2 && &path[path.len() - 2..] == &b".."[..] {159 return None;160 }161 let last_slash = memrchr(b'/', path).map(|i| i + 1).unwrap_or(0);162 Some(OsStr::from_bytes(&path[last_slash..]))ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
lines 163–164why, one step back163 }164ignore: more pathutil clean-upAndrew Gallantbefore adoptionc55818f
lines 165–170why, one step back165 #[cfg(not(unix))]166 fn imp(path: &Path) -> Option<&OsStr> {167 path.file_name()168 }169170 imp(path.as_ref())ignore: improve multithreading heuristicAlex Povelbefore adoptiond869038
line 171why, one step back171}