Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup windows by only fetching the necessary blocks #475

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ impl Core {
};

for path in paths {
let mut meta = match Meta::from_path(&path, self.flags.dereference.0) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
continue;
}
};
let mut meta =
match Meta::from_path(&path, self.flags.dereference.0, &self.flags.blocks) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
continue;
}
};

let recurse =
self.flags.layout == Layout::Tree || self.flags.display != Display::DirectoryOnly;
Expand Down
2 changes: 1 addition & 1 deletion src/flags/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Blocks {
///
/// It contains the [Block]s [Permission](Block::Permission), [User](Block::User),
/// [Group](Block::Group), [Size](Block::Size), [Date](Block::Date) and [Name](Block::Name).
fn long() -> Self {
pub fn long() -> Self {
Self(vec![
Block::Permission,
Block::User,
Expand Down
17 changes: 9 additions & 8 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ impl Icons {
#[cfg(test)]
mod test {
use super::{Icons, Theme};
use crate::flags::Blocks;
use crate::meta::Meta;
use std::fs::File;
use tempfile::tempdir;
Expand All @@ -355,7 +356,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::NoIcon, " ".to_string());
let icon = icon.get(&meta.name);
Expand All @@ -368,7 +369,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -381,7 +382,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Unicode, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -393,7 +394,7 @@ mod test {
fn get_directory_icon() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(&file_path.to_path_buf(), false).unwrap();
let meta = Meta::from_path(&file_path.to_path_buf(), false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -405,7 +406,7 @@ mod test {
fn get_directory_icon_unicode() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(&file_path.to_path_buf(), false).unwrap();
let meta = Meta::from_path(&file_path.to_path_buf(), false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Unicode, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -417,7 +418,7 @@ mod test {
fn get_directory_icon_with_ext() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(&file_path.to_path_buf(), false).unwrap();
let meta = Meta::from_path(&file_path.to_path_buf(), false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -432,7 +433,7 @@ mod test {
for (file_name, file_icon) in &Icons::get_default_icons_by_name() {
let file_path = tmp_dir.path().join(file_name);
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -448,7 +449,7 @@ mod test {
for (ext, file_icon) in &Icons::get_default_icons_by_extension() {
let file_path = tmp_dir.path().join(format!("file.{}", ext));
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, &Blocks::long()).unwrap();

let icon = Icons::new(Theme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand Down
3 changes: 2 additions & 1 deletion src/meta/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl FileType {
mod test {
use super::FileType;
use crate::color::{Colors, Theme};
use crate::flags::Blocks;
use crate::meta::Meta;
#[cfg(unix)]
use crate::meta::Permissions;
Expand Down Expand Up @@ -141,7 +142,7 @@ mod test {
#[test]
fn test_dir_type() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let meta = Meta::from_path(&tmp_dir.path().to_path_buf(), false)
let meta = Meta::from_path(&tmp_dir.path().to_path_buf(), false, &Blocks::long())
.expect("failed to get tempdir path");
let metadata = tmp_dir.path().metadata().expect("failed to get metas");

Expand Down
24 changes: 18 additions & 6 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use self::size::Size;
pub use self::symlink::SymLink;
pub use crate::icon::Icons;

use crate::flags::{Display, Flags, Layout};
use crate::flags::{Block, Blocks, Display, Flags, Layout};
use crate::print_error;

use std::fs::read_link;
Expand Down Expand Up @@ -84,8 +84,11 @@ impl Meta {
current_meta = self.clone();
current_meta.name.name = ".".to_owned();

let parent_meta =
Self::from_path(&self.path.join(Component::ParentDir), flags.dereference.0)?;
let parent_meta = Self::from_path(
&self.path.join(Component::ParentDir),
flags.dereference.0,
&flags.blocks,
)?;

content.push(current_meta);
content.push(parent_meta);
Expand All @@ -109,7 +112,7 @@ impl Meta {
}
}

let mut entry_meta = match Self::from_path(&path, flags.dereference.0) {
let mut entry_meta = match Self::from_path(&path, flags.dereference.0, &flags.blocks) {
Ok(res) => res,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
Expand Down Expand Up @@ -200,21 +203,30 @@ impl Meta {
}
}

pub fn from_path(path: &Path, dereference: bool) -> Result<Self, std::io::Error> {
pub fn from_path(
path: &Path,
dereference: bool,
blocks: &Blocks,
) -> Result<Self, std::io::Error> {
// If the file is a link then retrieve link metadata instead with target metadata (if present).
let (metadata, symlink_meta) = if read_link(path).is_ok() && !dereference {
(path.symlink_metadata()?, path.metadata().ok())
} else {
(path.metadata()?, None)
};

let get_owner = blocks.0.contains(&Block::User);
let get_group = blocks.0.contains(&Block::Group);
let get_permissions = blocks.0.contains(&Block::Permission);

#[cfg(unix)]
let owner = Owner::from(&metadata);
#[cfg(unix)]
let permissions = Permissions::from(&metadata);

#[cfg(windows)]
let (owner, permissions) = windows_utils::get_file_data(&path)?;
let (owner, permissions) =
windows_utils::get_file_data(&path, get_owner, get_group, get_permissions)?;

let file_type = FileType::new(&metadata, symlink_meta.as_ref(), &permissions);
let name = Name::new(&path, file_type);
Expand Down
5 changes: 3 additions & 2 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ mod test {
use super::DisplayOption;
use super::Name;
use crate::color::{self, Colors};
use crate::flags::Blocks;
use crate::icon::{self, Icons};
use crate::meta::FileType;
use crate::meta::Meta;
Expand Down Expand Up @@ -210,7 +211,7 @@ mod test {
// Chreate the directory
let dir_path = tmp_dir.path().join("directory");
fs::create_dir(&dir_path).expect("failed to create the dir");
let meta = Meta::from_path(&dir_path, false).unwrap();
let meta = Meta::from_path(&dir_path, false, &Blocks::long()).unwrap();

let colors = Colors::new(color::Theme::NoLscolors);

Expand Down Expand Up @@ -310,7 +311,7 @@ mod test {
// Create the file;
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, &Blocks::long()).unwrap();

let colors = Colors::new(color::Theme::NoColor);

Expand Down
2 changes: 1 addition & 1 deletion src/meta/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::color::{ColoredString, Colors, Elem};
use ansi_term::ANSIStrings;
use std::fs::Metadata;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Default)]
pub struct Permissions {
pub user_read: bool,
pub user_write: bool,
Expand Down
Loading