Skip to content
Myr

ignore: add routine for checking if a path is hidden or not

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This needs to do a bit more work on Windows unfortunately, but this will only be used in incremental checking.

authored by Andrew Gallant · committed by Andrew Gallant · parent 626b895 · Browse files · 1 file+59 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+59 added−14 removed

  • other:+59 added−14 removed

1 file+59 added−14 removed

modified:crates/ignore/src/pathutil.rs+59 added-14 removed
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 behavior
8 unmodified lines
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 {21 #[cfg(unix)]57 #[cfg(not(windows))]58 fn imp(dent: &DirEntry) -> bool {23 use std::os::unix::ffi::OsStrExt;2425 if let Some(name) = file_name(dent.path()) {26 name.as_bytes().get(0) == Some(&b'.')27 } else {28 false29 }59 is_hidden_path_only(dent.path())60 }6162 #[cfg(windows)]63 fn imp(dent: &DirEntry) -> bool {
7 unmodified lines
71 if file::is_hidden(md.file_attributes() as u64) {72 return true;73 }74 }45 if let Some(name) = file_name(dent.path()) {46 name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)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 #[cfg(unix)]87 fn imp(path: &Path) -> bool {88 use std::os::unix::ffi::OsStrExt;8990 if let Some(name) = file_name(path) {91 name.as_bytes().get(0) == Some(&b'.')92 } else {93 false94 }95 }9652 #[cfg(not(any(unix, windows)))]53 fn imp(dent: &DirEntry) -> bool {54 if let Some(name) = file_name(dent.path()) {97 #[cfg(not(unix))]98 fn imp(path: &Path) -> bool {99 if let Some(name) = file_name(path) {100 name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)101 } else {102 false103 }104 }10561 imp(dent)106 imp(path)107}108109/// Strip `prefix` from the `path` and return the remainder.110///
82 unmodified lines