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

dircolors: trigger an error when used on / #4672

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 15 additions & 4 deletions src/uu/dircolors/src/dircolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::borrow::Borrow;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

use clap::{crate_version, Arg, ArgAction, Command};
use uucore::display::Quotable;
Expand Down Expand Up @@ -42,7 +43,6 @@ pub enum OutputFmt {
}

pub fn guess_syntax() -> OutputFmt {
use std::path::Path;
match env::var("SHELL") {
Ok(ref s) if !s.is_empty() => {
let shell_path: &Path = s.as_ref();
Expand Down Expand Up @@ -138,15 +138,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let fin = BufReader::new(std::io::stdin());
result = parse(fin.lines().filter_map(Result::ok), &out_format, files[0]);
} else {
match File::open(files[0]) {
let path = Path::new(files[0]);
if path.is_dir() {
return Err(USimpleError::new(
2,
format!("expected file, got directory {}", path.quote()),
));
}
match File::open(path) {
Ok(f) => {
let fin = BufReader::new(f);
result = parse(fin.lines().filter_map(Result::ok), &out_format, files[0]);
result = parse(
fin.lines().filter_map(Result::ok),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an instance of the clippy lint we proposed :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify a bit, this should be map_while, even though we don't get the infinite loop anymore on a directory, it would still be safer to stop on an error.

&out_format,
&path.to_string_lossy(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could open an issue for date to handle non utf8 paths.

);
}
Err(e) => {
return Err(USimpleError::new(
1,
format!("{}: {}", files[0].maybe_quote(), e),
format!("{}: {}", path.maybe_quote(), e),
));
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_dircolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,13 @@ fn test_helper(file_name: &str, term: &str) {
.run()
.stdout_is_fixture(format!("{file_name}.sh.expected"));
}

#[test]
fn test_dircolors_for_dir_as_file() {
let result = new_ucmd!().args(&["-c", "/"]).fails();
result.no_stdout();
assert_eq!(
result.stderr_str().trim(),
"dircolors: expected file, got directory '/'",
);
}