2023-06-08 08:05:00 +02:00
|
|
|
use std::cell::OnceCell;
|
2022-05-18 16:18:54 +02:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::fs::{FileType, Metadata};
|
|
|
|
use std::path::{Path, PathBuf};
|
2021-11-09 11:01:09 +01:00
|
|
|
|
2022-10-31 16:52:23 +01:00
|
|
|
use lscolors::{Colorable, LsColors, Style};
|
|
|
|
|
2022-09-27 22:09:21 +02:00
|
|
|
use crate::config::Config;
|
|
|
|
use crate::filesystem::strip_current_dir;
|
|
|
|
|
2023-11-05 20:55:37 +01:00
|
|
|
#[derive(Debug)]
|
2021-11-09 11:01:09 +01:00
|
|
|
enum DirEntryInner {
|
|
|
|
Normal(ignore::DirEntry),
|
|
|
|
BrokenSymlink(PathBuf),
|
|
|
|
}
|
|
|
|
|
2023-11-05 20:55:37 +01:00
|
|
|
#[derive(Debug)]
|
2021-11-09 11:01:09 +01:00
|
|
|
pub struct DirEntry {
|
|
|
|
inner: DirEntryInner,
|
|
|
|
metadata: OnceCell<Option<Metadata>>,
|
2022-10-31 16:52:23 +01:00
|
|
|
style: OnceCell<Option<Style>>,
|
2021-11-09 11:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
2022-03-16 17:38:16 +01:00
|
|
|
#[inline]
|
2021-11-09 11:01:09 +01:00
|
|
|
pub fn normal(e: ignore::DirEntry) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: DirEntryInner::Normal(e),
|
|
|
|
metadata: OnceCell::new(),
|
2022-10-31 16:52:23 +01:00
|
|
|
style: OnceCell::new(),
|
2021-11-09 11:01:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn broken_symlink(path: PathBuf) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: DirEntryInner::BrokenSymlink(path),
|
|
|
|
metadata: OnceCell::new(),
|
2022-10-31 16:52:23 +01:00
|
|
|
style: OnceCell::new(),
|
2021-11-09 11:01:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path(&self) -> &Path {
|
|
|
|
match &self.inner {
|
|
|
|
DirEntryInner::Normal(e) => e.path(),
|
|
|
|
DirEntryInner::BrokenSymlink(pathbuf) => pathbuf.as_path(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 08:52:39 +01:00
|
|
|
pub fn into_path(self) -> PathBuf {
|
|
|
|
match self.inner {
|
|
|
|
DirEntryInner::Normal(e) => e.into_path(),
|
|
|
|
DirEntryInner::BrokenSymlink(p) => p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 22:09:21 +02:00
|
|
|
/// Returns the path as it should be presented to the user.
|
|
|
|
pub fn stripped_path(&self, config: &Config) -> &Path {
|
|
|
|
if config.strip_cwd_prefix {
|
|
|
|
strip_current_dir(self.path())
|
|
|
|
} else {
|
|
|
|
self.path()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path as it should be presented to the user.
|
|
|
|
pub fn into_stripped_path(self, config: &Config) -> PathBuf {
|
|
|
|
if config.strip_cwd_prefix {
|
|
|
|
self.stripped_path(config).to_path_buf()
|
|
|
|
} else {
|
|
|
|
self.into_path()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:01:09 +01:00
|
|
|
pub fn file_type(&self) -> Option<FileType> {
|
|
|
|
match &self.inner {
|
|
|
|
DirEntryInner::Normal(e) => e.file_type(),
|
|
|
|
DirEntryInner::BrokenSymlink(_) => self.metadata().map(|m| m.file_type()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metadata(&self) -> Option<&Metadata> {
|
|
|
|
self.metadata
|
|
|
|
.get_or_init(|| match &self.inner {
|
|
|
|
DirEntryInner::Normal(e) => e.metadata().ok(),
|
|
|
|
DirEntryInner::BrokenSymlink(path) => path.symlink_metadata().ok(),
|
|
|
|
})
|
|
|
|
.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn depth(&self) -> Option<usize> {
|
|
|
|
match &self.inner {
|
|
|
|
DirEntryInner::Normal(e) => Some(e.depth()),
|
|
|
|
DirEntryInner::BrokenSymlink(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 16:52:23 +01:00
|
|
|
|
|
|
|
pub fn style(&self, ls_colors: &LsColors) -> Option<&Style> {
|
|
|
|
self.style
|
|
|
|
.get_or_init(|| ls_colors.style_for(self).cloned())
|
|
|
|
.as_ref()
|
|
|
|
}
|
2021-11-09 11:01:09 +01:00
|
|
|
}
|
2022-03-16 17:38:16 +01:00
|
|
|
|
|
|
|
impl PartialEq for DirEntry {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.path() == other.path()
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 16:18:54 +02:00
|
|
|
|
2022-03-16 17:38:16 +01:00
|
|
|
impl Eq for DirEntry {}
|
|
|
|
|
|
|
|
impl PartialOrd for DirEntry {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
self.path().partial_cmp(other.path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for DirEntry {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
self.path().cmp(other.path())
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 16:18:54 +02:00
|
|
|
|
|
|
|
impl Colorable for DirEntry {
|
|
|
|
fn path(&self) -> PathBuf {
|
|
|
|
self.path().to_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_name(&self) -> OsString {
|
|
|
|
let name = match &self.inner {
|
|
|
|
DirEntryInner::Normal(e) => e.file_name(),
|
2022-10-25 17:16:31 +02:00
|
|
|
DirEntryInner::BrokenSymlink(path) => {
|
|
|
|
// Path::file_name() only works if the last component is Normal,
|
|
|
|
// but we want it for all component types, so we open code it.
|
|
|
|
// Copied from LsColors::style_for_path_with_metadata().
|
|
|
|
path.components()
|
|
|
|
.last()
|
|
|
|
.map(|c| c.as_os_str())
|
|
|
|
.unwrap_or_else(|| path.as_os_str())
|
|
|
|
}
|
2022-05-18 16:18:54 +02:00
|
|
|
};
|
|
|
|
name.to_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_type(&self) -> Option<FileType> {
|
|
|
|
self.file_type()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn metadata(&self) -> Option<Metadata> {
|
|
|
|
self.metadata().cloned()
|
|
|
|
}
|
|
|
|
}
|