lsd/src/meta/mod.rs

123 lines
3.1 KiB
Rust
Raw Normal View History

2018-12-01 16:56:07 +00:00
mod date;
mod filetype;
2018-12-10 17:26:34 +00:00
mod indicator;
2018-12-01 17:59:53 +00:00
mod name;
2018-12-01 16:15:55 +00:00
mod owner;
mod permissions;
mod size;
mod symlink;
2018-12-01 16:56:07 +00:00
pub use self::date::Date;
pub use self::filetype::FileType;
2018-12-10 17:26:34 +00:00
pub use self::indicator::Indicator;
2018-12-01 17:59:53 +00:00
pub use self::name::Name;
2018-12-01 16:15:55 +00:00
pub use self::owner::Owner;
pub use self::permissions::Permissions;
pub use self::size::Size;
pub use self::symlink::SymLink;
2019-02-09 10:41:42 +00:00
pub use crate::icon::Icons;
use std::fs::read_link;
2019-01-20 10:22:14 +00:00
use std::io::{Error, ErrorKind};
2018-12-04 12:29:54 +00:00
use std::path::PathBuf;
2018-11-24 14:43:04 +00:00
#[derive(Debug)]
2018-11-24 14:53:46 +00:00
pub struct Meta {
2018-12-01 17:59:53 +00:00
pub name: Name,
2018-12-04 12:29:54 +00:00
pub path: PathBuf,
pub permissions: Permissions,
2018-12-01 16:56:07 +00:00
pub date: Date,
2018-12-01 16:15:55 +00:00
pub owner: Owner,
pub file_type: FileType,
2018-11-30 13:53:54 +00:00
pub size: Size,
pub symlink: SymLink,
2018-12-10 17:26:34 +00:00
pub indicator: Indicator,
2019-01-20 10:22:14 +00:00
pub content: Option<Vec<Meta>>,
2018-11-24 14:43:04 +00:00
}
impl Meta {
2019-01-20 10:22:14 +00:00
pub fn from_path_recursive(
path: &PathBuf,
depth: usize,
list_hidden_files: bool,
) -> Result<Self, std::io::Error> {
let mut meta = Self::from_path(path)?;
if depth == 0 {
return Ok(meta);
}
match meta.file_type {
FileType::Directory { .. } => (),
_ => return Ok(meta),
}
if let Err(err) = meta.path.read_dir() {
println!("cannot access '{}': {}", path.display(), err);
return Ok(meta);
}
let mut content = Vec::new();
for entry in meta.path.read_dir()? {
let path = entry?.path();
if !list_hidden_files
&& path
.file_name()
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, "invalid file name"))?
.to_string_lossy()
.starts_with('.')
{
continue;
}
let entry_meta = match Self::from_path_recursive(
&path.to_path_buf(),
depth - 1,
list_hidden_files,
) {
Ok(res) => res,
Err(err) => {
println!("cannot access '{}': {}", path.display(), err);
continue;
}
};
content.push(entry_meta);
}
if !content.is_empty() {
meta.content = Some(content);
}
Ok(meta)
}
2019-01-16 12:36:57 +00:00
pub fn from_path(path: &PathBuf) -> Result<Self, std::io::Error> {
let metadata = if read_link(path).is_ok() {
// If the file is a link, retrieve the metadata without following
// the link.
2019-01-16 12:36:57 +00:00
path.symlink_metadata()?
2018-12-03 12:27:06 +00:00
} else {
2019-01-16 12:36:57 +00:00
path.metadata()?
};
2018-11-24 14:43:04 +00:00
2018-12-10 17:26:34 +00:00
let permissions = Permissions::from(&metadata);
let file_type = FileType::new(&metadata, &permissions);
2018-12-08 18:52:56 +00:00
let name = Name::new(&path, file_type);
2018-12-04 20:03:39 +00:00
2019-01-16 12:36:57 +00:00
Ok(Self {
2018-12-04 12:29:54 +00:00
path: path.to_path_buf(),
symlink: SymLink::from(path.as_path()),
size: Size::from(&metadata),
2018-12-01 16:56:07 +00:00
date: Date::from(&metadata),
2018-12-10 17:26:34 +00:00
indicator: Indicator::from(file_type),
2018-12-03 12:27:06 +00:00
owner: Owner::from(&metadata),
2018-12-10 17:26:34 +00:00
permissions,
2018-12-08 18:52:56 +00:00
name,
file_type,
2019-01-20 10:22:14 +00:00
content: None,
})
2018-11-24 14:43:04 +00:00
}
}