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
1use std::{ffi::OsStr, path::Path};23use crate::walk::DirEntry;45/// 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}4041/// Returns true if and only if this directory entry is considered to be42/// hidden.43///44/// # Platform behavior45///46/// ## Windows47///48/// This returns true if one of the following is true:49///50/// * The base name of the path starts with a `.`.51/// * The file attributes have the `HIDDEN` property set.52///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 {57 #[cfg(not(windows))]58 fn imp(dent: &DirEntry) -> bool {59 is_hidden_path_only(dent.path())60 }6162 #[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 }75 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 {86 if let Some(name) = file_name(path) {87 name.as_encoded_bytes().starts_with(b".")88 } else {89 false90 }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> {100 #[cfg(unix)]101 fn imp<'a>(prefix: &'a Path, path: &'a Path) -> Option<&'a Path> {102 use std::os::unix::ffi::OsStrExt;103104 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 }111 }112113 #[cfg(not(unix))]114 fn imp<'a>(prefix: &'a Path, path: &'a Path) -> Option<&'a Path> {115 path.strip_prefix(prefix).ok()116 }117118 imp(prefix.as_ref(), path)119}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 {124 #[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 }136}137138/// The final component of the path, if it is a normal file.139///140/// If the path terminates in `.`, `..`, or consists solely of a root of141/// prefix, this will return `None`.142pub(crate) fn file_name<'a, P: AsRef<Path> + ?Sized>(143 path: &'a P,144) -> Option<&'a OsStr> {145 #[cfg(unix)]146 fn imp(path: &Path) -> Option<&OsStr> {147 use std::os::unix::ffi::OsStrExt;148149 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..]))163 }164165 #[cfg(not(unix))]166 fn imp(path: &Path) -> Option<&OsStr> {167 path.file_name()168 }169170 imp(path.as_ref())171}