Skip to content

Commit

Permalink
✨ add disable option for permission
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Aug 28, 2023
1 parent 9b8ed0e commit c082c03
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct Cli {
pub directory_only: bool,

/// How to display permissions [default: rwx]
#[arg(long, value_name = "MODE", value_parser = ["rwx", "octal"])]
#[arg(long, value_name = "MODE", value_parser = ["rwx", "octal", "disable"])]
pub permission: Option<String>,

/// How to display size [default: default]
Expand Down
21 changes: 12 additions & 9 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::color::Colors;
use crate::display;
use crate::flags::{ColorOption, Display, Flags, HyperlinkOption, Layout, SortOrder, ThemeOption};
use crate::flags::{ColorOption, Display, Flags, HyperlinkOption, Layout, SortOrder, PermissionFlag, ThemeOption};
use crate::git::GitCache;
use crate::icon::Icons;

Expand Down Expand Up @@ -103,14 +103,17 @@ 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);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};
let mut meta =
match Meta::from_path(&path,
self.flags.dereference.0,
self.flags.permission == PermissionFlag::Disable) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};

let cache = if self.flags.blocks.0.contains(&Block::GitStatus) {
Some(GitCache::new(&path))
Expand Down
3 changes: 3 additions & 0 deletions src/flags/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ pub enum PermissionFlag {
Rwx,
/// The variant to show file permissions in octal format
Octal,
/// Disable the display of owner and permissions, may be used to speed up in Windows
Disable,
}

impl PermissionFlag {
fn from_arg_str(value: &str) -> Self {
match value {
"rwx" => Self::Rwx,
"octal" => Self::Octal,
"disable" => Self::Disable,
// Invalid value should be handled by `clap` when building an `Cli`
other => unreachable!("Invalid value '{other}' for 'permission'"),
}
Expand Down
50 changes: 38 additions & 12 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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::{Display, Flags, Layout, PermissionFlag};
use crate::{print_error, ExitCode};

use crate::git::GitCache;
Expand Down Expand Up @@ -95,8 +95,11 @@ impl Meta {
let mut current_meta = self.clone();
current_meta.name.name = ".".to_owned();

let mut parent_meta =
Self::from_path(&self.path.join(Component::ParentDir), flags.dereference.0)?;
let mut parent_meta = Self::from_path(
&self.path.join(Component::ParentDir),
flags.dereference.0,
flags.permission == PermissionFlag::Disable,
)?;
parent_meta.name.name = "..".to_owned();

current_meta.git_status = cache.and_then(|cache| cache.get(&current_meta.path, true));
Expand Down Expand Up @@ -139,7 +142,11 @@ 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.permission == PermissionFlag::Disable,
) {
Ok(res) => res,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
Expand Down Expand Up @@ -244,7 +251,7 @@ impl Meta {
}
}

pub fn from_path(path: &Path, dereference: bool) -> io::Result<Self> {
pub fn from_path(path: &Path, dereference: bool, disable_permission: bool) -> io::Result<Self> {
let mut metadata = path.symlink_metadata()?;
let mut symlink_meta = None;
let mut broken_link = false;
Expand All @@ -269,15 +276,34 @@ impl Meta {
}

#[cfg(unix)]
let owner = Owner::from(&metadata);
#[cfg(unix)]
let permissions = Permissions::from(&metadata);
let (owner, permissions) = if disable_permission {
(None, None)
} else {
(
Some(Owner::from(&metadata)),
Some(Permissions::from(&metadata)),
)
};

#[cfg(windows)]
let (owner, permissions) = windows_utils::get_file_data(path)?;
let (owner, permissions) = if disable_permission {
(None, None)
} else {
match windows_utils::get_file_data(path) {
Ok((owner, permissions)) => (Some(owner), Some(permissions)),
Err(e) => {
eprintln!("lsd: {}: {}\n", path.to_str().unwrap_or(""), e);
(None, None)
}
}
};

#[cfg(not(windows))]
let file_type = FileType::new(&metadata, symlink_meta.as_ref(), &permissions);
let file_type = FileType::new(
&metadata,
symlink_meta.as_ref(),
&permissions.unwrap_or_default(),
);

#[cfg(windows)]
let file_type = FileType::new(&metadata, symlink_meta.as_ref(), path);
Expand Down Expand Up @@ -305,8 +331,8 @@ impl Meta {
size,
date,
indicator: Indicator::from(file_type),
owner,
permissions,
owner: owner.unwrap_or_default(),
permissions: permissions.unwrap_or_default(),
name,
file_type,
content: None,
Expand Down
9 changes: 9 additions & 0 deletions src/meta/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ impl Owner {
}
}

impl Default for Owner {
fn default() -> Owner {
Owner {
user: String::from("-"),
group: String::from("-"),
}
}
}

#[cfg(unix)]
impl From<&Metadata> for Owner {
fn from(meta: &Metadata) -> Self {
Expand Down
20 changes: 20 additions & 0 deletions src/meta/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ impl From<&Metadata> for Permissions {
}
}

impl Default for Permissions {
fn default() -> Permissions {
Permissions {
user_read: false,
user_write: false,
user_execute: false,
group_read: false,
group_write: false,
group_execute: false,
other_read: false,
other_write: false,
other_execute: false,
sticky: false,
setgid: false,
setuid: false,
}
}
}

impl Permissions {
fn bits_to_octal(r: bool, w: bool, x: bool) -> u8 {
(r as u8) * 4 + (w as u8) * 2 + (x as u8)
Expand Down Expand Up @@ -122,6 +141,7 @@ impl Permissions {

colors.colorize(octals, &Elem::Octal).to_string()
}
PermissionFlag::Disable => colors.colorize('-', &Elem::NoAccess).to_string(),
};

ColoredString::new(Colors::default_style(), res)
Expand Down

0 comments on commit c082c03

Please sign in to comment.