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

feat: Windows safe default permissions (fixes ACL errors/performance) #911

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ size: default

# == Permission ==
# Specify the format of the permission column
# Possible value: rwx, octal
permission: rwx
# Possible value: rwx, octal, attributes (windows only), disabled
# permission: rwx

# == Sorting ==
sorting:
Expand Down
2 changes: 1 addition & 1 deletion doc/lsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich
: Do not display files/directories with names matching the glob pattern(s). More than one can be specified by repeating the argument [default: ]

`--permission <permission>...`
: How to display permissions [default: rwx] [possible values: rwx, octal]
: How to display permissions [default: rwx for linux, attributes for windows] [possible values: rwx, octal, attributes, disable]

`--size <size>...`
: How to display size [default: default] [possible values: default, short, bytes]
Expand Down
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub struct Cli {
#[arg(short, long, conflicts_with_all = ["depth", "recursive"])]
pub directory_only: bool,

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

/// How to display size [default: default]
Expand Down
17 changes: 17 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub enum Elem {
Acl,
Context,

/// Attributes
Archive,
AttributeRead,
Hidden,
System,

/// Last Time Modified
DayOld,
HourOld,
Expand Down Expand Up @@ -112,6 +118,11 @@ impl Elem {
Elem::Acl => theme.permission.acl,
Elem::Context => theme.permission.context,

Elem::Archive => theme.attributes.archive,
Elem::AttributeRead => theme.attributes.read,
Elem::Hidden => theme.attributes.hidden,
Elem::System => theme.attributes.system,

Elem::DayOld => theme.date.day_old,
Elem::HourOld => theme.date.hour_old,
Elem::Older => theme.date.older,
Expand Down Expand Up @@ -399,6 +410,12 @@ mod elem {
acl: Color::DarkCyan,
context: Color::Cyan,
},
attributes: color::Attributes {
read: Color::Green,
archive: Color::Yellow,
hidden: Color::Red,
system: Color::Magenta,
},
file_type: color::FileType {
file: color::File {
exec_uid: Color::AnsiValue(40), // Green3
Expand Down
7 changes: 3 additions & 4 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ size: default

# == Permission ==
# Specify the format of the permission column.
# Possible value: rwx, octal
permission: rwx
# Possible value: rwx, octal, attributes, disable
# permission: rwx

# == Sorting ==
sorting:
Expand Down Expand Up @@ -363,7 +363,6 @@ mod tests {
use crate::flags::color::{ColorOption, ThemeOption};
use crate::flags::icons::{IconOption, IconTheme};
use crate::flags::layout::Layout;
use crate::flags::permission::PermissionFlag;
use crate::flags::size::SizeFlag;
use crate::flags::sorting::{DirGrouping, SortColumn};
use crate::flags::HyperlinkOption;
Expand Down Expand Up @@ -402,7 +401,7 @@ mod tests {
depth: None,
}),
size: Some(SizeFlag::Default),
permission: Some(PermissionFlag::Rwx),
permission: None,
sorting: Some(config_file::Sorting {
column: Some(SortColumn::Name),
reverse: Some(false),
Expand Down
24 changes: 10 additions & 14 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::color::Colors;
use crate::display;
use crate::flags::{
ColorOption, Display, Flags, HyperlinkOption, Layout, Literal, PermissionFlag, SortOrder,
ThemeOption,
ColorOption, Display, Flags, HyperlinkOption, Layout, Literal, SortOrder, ThemeOption,
};
use crate::git::GitCache;
use crate::icon::Icons;
Expand Down Expand Up @@ -106,18 +105,15 @@ impl Core {
};

for path in paths {
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 mut meta =
match Meta::from_path(&path, self.flags.dereference.0, self.flags.permission) {
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
30 changes: 16 additions & 14 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,10 @@ fn get_output(
Block::Permission => {
block_vec.extend([
meta.file_type.render(colors),
match meta.permissions {
Some(permissions) => permissions.render(colors, flags),
match &meta.permissions_or_attributes {
Some(permissions_or_attributes) => {
permissions_or_attributes.render(colors, flags)
}
None => colorize_missing("?????????"),
},
match &meta.access_control {
Expand Down Expand Up @@ -488,7 +490,7 @@ mod tests {
use crate::app::Cli;
use crate::color;
use crate::color::Colors;
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme};
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme, PermissionFlag};
use crate::icon::Icons;
use crate::meta::{FileType, Name};
use crate::Config;
Expand Down Expand Up @@ -683,7 +685,7 @@ mod tests {
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
dir.child("one.d/.hidden").touch().unwrap();
let mut metas = Meta::from_path(Path::new(dir.path()), false, false)
let mut metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -716,7 +718,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, false)
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -757,7 +759,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, false)
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -797,7 +799,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, false)
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -828,7 +830,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
dir.child("test").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, false)
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -862,7 +864,7 @@ mod tests {

let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, false)
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -892,11 +894,11 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false, false).unwrap();
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false, false).unwrap();
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();

assert_eq!(
display_folder_path(&dir),
Expand Down Expand Up @@ -942,15 +944,15 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false, false).unwrap();
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false, false).unwrap();
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();

let link_path = tmp_dir.path().join("link");
std::os::unix::fs::symlink("dir", &link_path).unwrap();
let link = Meta::from_path(&link_path, false, false).unwrap();
let link = Meta::from_path(&link_path, false, PermissionFlag::Rwx).unwrap();

let grid_flags = Flags {
layout: Layout::Grid,
Expand Down
23 changes: 21 additions & 2 deletions src/flags/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ use serde::Deserialize;
#[serde(rename_all = "kebab-case")]
pub enum PermissionFlag {
/// The variant to show file permissions in rwx format
#[default]
#[cfg_attr(not(target_os = "windows"), default)]
Rwx,
/// The variant to show file permissions in octal format
Octal,
/// (windows only): Attributes from powershell's `Get-ChildItem`
#[cfg_attr(target_os = "windows", default)]
Attributes,
/// Disable the display of owner and permissions, may be used to speed up in Windows
Disable,
}
Expand All @@ -26,6 +29,7 @@ impl PermissionFlag {
match value {
"rwx" => Self::Rwx,
"octal" => Self::Octal,
"attributes" => Self::Attributes,
"disable" => Self::Disable,
// Invalid value should be handled by `clap` when building an `Cli`
other => unreachable!("Invalid value '{other}' for 'permission'"),
Expand Down Expand Up @@ -75,7 +79,12 @@ mod test {

#[test]
fn test_default() {
assert_eq!(PermissionFlag::Rwx, PermissionFlag::default());
let expected = if cfg!(target_os = "windows") {
PermissionFlag::Attributes
} else {
PermissionFlag::Rwx
};
assert_eq!(expected, PermissionFlag::default());
}

#[test]
Expand All @@ -99,6 +108,16 @@ mod test {
assert_eq!(Some(PermissionFlag::Octal), PermissionFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_attributes() {
let argv = ["lsd", "--permission", "attributes"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(
Some(PermissionFlag::Attributes),
PermissionFlag::from_cli(&cli)
);
}

#[test]
fn test_from_cli_permissions_disable() {
let argv = ["lsd", "--permission", "disable"];
Expand Down
Loading
Loading