From d29f9f2e300fe768b635faec30b1758adb55f2bc Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 28 Mar 2023 22:46:59 +0200 Subject: [PATCH 1/2] dircolors: trigger an error when used on / --- src/uu/dircolors/src/dircolors.rs | 19 +++++++++++++++---- tests/by-util/test_dircolors.rs | 10 ++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index abab966af..e960fdb13 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -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; @@ -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(); @@ -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), + &out_format, + &path.to_string_lossy(), + ); } Err(e) => { return Err(USimpleError::new( 1, - format!("{}: {}", files[0].maybe_quote(), e), + format!("{}: {}", path.maybe_quote(), e), )); } } diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index 629a10912..d2ec32f33 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -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 '/'", + ); +} From 889df29cba951df33ce7cb589aacfe11052502cb Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 29 Mar 2023 21:15:24 +0200 Subject: [PATCH 2/2] dircolors : replace filter_map by map_while --- src/uu/dircolors/src/dircolors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index e960fdb13..6b2bd9a98 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -149,7 +149,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Ok(f) => { let fin = BufReader::new(f); result = parse( - fin.lines().filter_map(Result::ok), + fin.lines().map_while(Result::ok), &out_format, &path.to_string_lossy(), );