mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
ls: On Windows don't display files hidden by NTFS (#1662)
This little check, allows us to hide the files that shouldn't be shown on the listing on Windows operating systems. Just like the "dot" in UNIX based operating systems Windows uses its own file attributes to determine if a file is hidden or not. The lack of support for this option is normally an annoyance for many users, this commit adds full support for this feature
This commit is contained in:
parent
469abf2427
commit
8b12686888
2 changed files with 39 additions and 1 deletions
|
@ -255,6 +255,18 @@ fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn is_hidden(file_path: &DirEntry) -> std::io::Result<bool> {
|
||||
let metadata = fs::metadata(file_path.path())?;
|
||||
let attr = metadata.file_attributes();
|
||||
Ok(((attr & 0x2) > 0) || file_path.file_name().to_string_lossy().starts_with('.'))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn is_hidden(file_path: &DirEntry) -> std::io::Result<bool> {
|
||||
Ok(file_path.file_name().to_string_lossy().starts_with('.'))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) {
|
||||
let mut reverse = options.opt_present("r");
|
||||
|
@ -286,7 +298,7 @@ fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) {
|
|||
fn should_display(entry: &DirEntry, options: &getopts::Matches) -> bool {
|
||||
let ffi_name = entry.file_name();
|
||||
let name = ffi_name.to_string_lossy();
|
||||
if !options.opt_present("a") && !options.opt_present("A") && name.starts_with('.') {
|
||||
if !options.opt_present("a") && !options.opt_present("A") && is_hidden(entry).unwrap() {
|
||||
return false;
|
||||
}
|
||||
if options.opt_present("B") && name.ends_with('~') {
|
||||
|
|
|
@ -310,3 +310,29 @@ fn test_ls_human() {
|
|||
assert!(result.success);
|
||||
assert!(result.stdout.contains("1.02M"));
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn test_ls_hidden_windows() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
let file = "hiddenWindowsFileNoDot";
|
||||
at.touch(file);
|
||||
// hide the file
|
||||
scene
|
||||
.cmd("attrib")
|
||||
.arg("+h")
|
||||
.arg("+S")
|
||||
.arg("+r")
|
||||
.arg(file)
|
||||
.run();
|
||||
let result = scene.ucmd().run();
|
||||
println!("stderr = {:?}", result.stderr);
|
||||
println!("stdout = {:?}", result.stdout);
|
||||
assert!(result.success);
|
||||
let result = scene.ucmd().arg("-a").run();
|
||||
println!("stderr = {:?}", result.stderr);
|
||||
println!("stdout = {:?}", result.stdout);
|
||||
assert!(result.success);
|
||||
assert!(result.stdout.contains(file));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue