Skip to content

Commit 7c12af2

Browse files
emirmrkaljevicsylvestre
authored andcommitted
dircolors: report a directory operand as a read error with exit 1
1 parent 5a24dd0 commit 7c12af2

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

‎src/uu/dircolors/locales/en-US.ftl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dircolors-error-extra-operand-print-database = extra operand { $operand }
1818
file operands cannot be combined with --print-database (-p)
1919
dircolors-error-no-shell-environment = no SHELL environment variable, and no shell type option given
2020
dircolors-error-extra-operand = extra operand { $operand }
21-
dircolors-error-expected-file-got-directory = expected file, got directory { $path }
21+
dircolors-error-read = { $path }: read error: { $error }
22+
dircolors-error-read-directory = { $path }: read error: Is a directory
2223
dircolors-error-invalid-line-missing-token = { $file }:{ $line }: invalid line; missing second token
2324
dircolors-error-unrecognized-keyword = unrecognized keyword { $keyword }

‎src/uu/dircolors/locales/fr-FR.ftl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dircolors-error-extra-operand-print-database = opérande supplémentaire { $oper
1818
les opérandes de fichier ne peuvent pas être combinées avec --print-database (-p)
1919
dircolors-error-no-shell-environment = aucune variable d'environnement SHELL, et aucune option de type de shell donnée
2020
dircolors-error-extra-operand = opérande supplémentaire { $operand }
21-
dircolors-error-expected-file-got-directory = fichier attendu, répertoire obtenu { $path }
21+
dircolors-error-read = { $path } : erreur de lecture : { $error }
22+
dircolors-error-read-directory = { $path } : erreur de lecture : est un répertoire
2223
dircolors-error-invalid-line-missing-token = { $file }:{ $line } : ligne invalide ; jeton manquant
2324
dircolors-error-unrecognized-keyword = mot-clé non reconnu { $keyword }

‎src/uu/dircolors/src/dircolors.rs‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use std::env;
1010
use std::ffi::OsString;
1111
use std::fmt::Write as _;
1212
use std::fs::File;
13-
use std::io::{BufRead, BufReader, Write, stdout};
13+
use std::io::{BufRead, BufReader, ErrorKind, Write, stdout};
1414
use std::path::Path;
1515

1616
use clap::{Arg, ArgAction, Command};
1717
use uucore::colors::{FILE_ATTRIBUTE_CODES, FILE_COLORS, FILE_TYPES, TERMS};
1818
use uucore::display::Quotable;
19-
use uucore::error::{UResult, USimpleError, UUsageError};
19+
use uucore::error::{UResult, USimpleError, UUsageError, strip_errno};
2020
use uucore::translate;
2121

2222
use uucore::{format_usage, parser::parse_glob};
@@ -202,20 +202,32 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
202202
parse(fin.lines().map_while(Result::ok), out_format, "-")
203203
} else {
204204
let path = Path::new(&file_arg);
205-
if path.is_dir() {
206-
return Err(USimpleError::new(
207-
2,
208-
translate!("dircolors-error-expected-file-got-directory", "path" => path.quote()),
209-
));
205+
let read_error = |e: &std::io::Error| {
206+
USimpleError::new(
207+
1,
208+
translate!("dircolors-error-read", "path" => path.maybe_quote(), "error" => strip_errno(e)),
209+
)
210+
};
211+
// Windows detects a directory as error of open instead of read
212+
let file = File::open(path).map_err(|e| {
213+
if cfg!(windows) && path.is_dir() {
214+
USimpleError::new(
215+
1,
216+
translate!("dircolors-error-read-directory", "path" => path.maybe_quote()),
217+
)
218+
} else {
219+
USimpleError::new(1, format!("{}: {e}", path.maybe_quote()))
220+
}
221+
})?;
222+
let mut lines = Vec::new();
223+
for line in BufReader::new(file).lines() {
224+
match line {
225+
Ok(line) => lines.push(line),
226+
Err(e) if e.kind() == ErrorKind::InvalidData => break,
227+
Err(e) => return Err(read_error(&e)),
228+
}
210229
}
211-
let file = File::open(path)
212-
.map_err(|e| USimpleError::new(1, format!("{}: {e}", path.maybe_quote())))?;
213-
let fin = BufReader::new(file);
214-
parse(
215-
fin.lines().map_while(Result::ok),
216-
out_format,
217-
&path.to_string_lossy(),
218-
)
230+
parse(lines.into_iter(), out_format, &path.to_string_lossy())
219231
};
220232

221233
let string = result.map_err(|s| USimpleError::new(1, s))?;

‎tests/by-util/test_dircolors.rs‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// spell-checker:ignore overridable colorterm
77

8-
#[cfg(target_os = "linux")]
98
use uutests::at_and_ucmd;
109
use uutests::new_ucmd;
1110

@@ -216,14 +215,26 @@ fn test_helper(file_name: &str, term: &str) {
216215
.stdout_is_fixture(format!("{file_name}.sh.expected"));
217216
}
218217

218+
#[uutests::wasi_ignore(ErrnoMessageMismatch)]
219219
#[test]
220220
fn test_dircolors_for_dir_as_file() {
221-
let result = new_ucmd!().args(&["-c", "/"]).fails();
222-
result.no_stdout();
223-
assert_eq!(
224-
result.stderr_str().trim(),
225-
"dircolors: expected file, got directory '/'",
226-
);
221+
let (at, mut ucmd) = at_and_ucmd!();
222+
at.mkdir("dir");
223+
ucmd.args(&["-c", "dir"])
224+
.fails_with_code(1)
225+
.stderr_only("dircolors: dir: read error: Is a directory\n");
226+
}
227+
228+
#[uutests::wasi_ignore(HostPathsNotVisible)]
229+
#[test]
230+
#[cfg(target_os = "linux")]
231+
fn test_dircolors_read_error() {
232+
// The errno text comes from the libc (musl says "I/O error").
233+
new_ucmd!()
234+
.args(&["-b", "/proc/self/mem"])
235+
.fails_with_code(1)
236+
.no_stdout()
237+
.stderr_contains("dircolors: /proc/self/mem: read error: ");
227238
}
228239

229240
#[test]

0 commit comments

Comments
 (0)