Skip to content

Commit f9dbbd3

Browse files
committed
sort: report read failures with context
A read error was reported as the bare io::Error string, e.g. `sort: Input/output error (os error 5)` for `sort /proc/self/mem`. That names neither the operation that failed nor anything the user can act on, and the `(os error 5)` suffix is noise. Wrap it with a "read failed" context and drop the errno suffix using the existing strip_errno helper, matching the style of the other SortError variants: sort: read failed: Input/output error Fixes #13992
1 parent 1a36740 commit f9dbbd3

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/uu/sort/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sort-after-help = The key format is FIELD[.CHAR][OPTIONS][,FIELD[.CHAR]][OPTIONS
1414
sort-open-failed = open failed: {$path}: {$error}
1515
sort-parse-key-error = failed to parse key {$key}: {$msg}
1616
sort-cannot-read = cannot read: {$path}: {$error}
17+
sort-read-failed = read failed: {$error}
1718
sort-open-tmp-file-failed = failed to open temporary file: {$error}
1819
sort-compress-prog-execution-failed = could not run compress program '{$prog}': {$error}
1920
sort-compress-prog-terminated-abnormally = {$prog} terminated abnormally

src/uu/sort/locales/fr-FR.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sort-after-help = Le format de clé est CHAMP[.CAR][OPTIONS][,CHAMP[.CAR]][OPTIO
1414
sort-open-failed = échec d'ouverture : {$path} : {$error}
1515
sort-parse-key-error = échec d'analyse de la clé {$key} : {$msg}
1616
sort-cannot-read = impossible de lire : {$path} : {$error}
17+
sort-read-failed = échec de lecture : {$error}
1718
sort-open-tmp-file-failed = échec d'ouverture du fichier temporaire : {$error}
1819
sort-compress-prog-execution-failed = impossible d'exécuter le programme de compression '{$prog}' : {$error}
1920
sort-compress-prog-terminated-abnormally = {$prog} s'est terminé anormalement

src/uu/sort/src/chunks.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use std::{
1717

1818
use memchr::memchr_iter;
1919
use self_cell::self_cell;
20-
use uucore::error::{UResult, USimpleError};
20+
use uucore::error::{UResult, USimpleError, strip_errno};
21+
use uucore::translate;
2122

2223
use crate::{
2324
GeneralBigDecimalParseResult, GlobalSettings, Line, SortMode, numeric_str_cmp::NumInfo,
@@ -427,7 +428,12 @@ fn read_to_buffer<T: Read>(
427428
Err(e) if e.kind() == ErrorKind::Interrupted => {
428429
// retry
429430
}
430-
Err(e) => return Err(USimpleError::new(2, e.to_string())),
431+
Err(e) => {
432+
return Err(USimpleError::new(
433+
2,
434+
translate!("sort-read-failed", "error" => strip_errno(&e)),
435+
));
436+
}
431437
}
432438
}
433439
}

tests/by-util/test_sort.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,18 @@ fn test_merge_write_error_does_not_panic() {
11821182
}
11831183
}
11841184

1185+
// A read error must be reported with context and without the raw io::Error suffix.
1186+
// It used to print `sort: Input/output error (os error 5)`.
1187+
#[test]
1188+
#[cfg(target_os = "linux")]
1189+
fn test_read_error_message() {
1190+
// Reading /proc/self/mem from offset 0 fails with EIO.
1191+
new_ucmd!()
1192+
.arg("/proc/self/mem")
1193+
.fails_with_code(2)
1194+
.stderr_only("sort: read failed: Input/output error\n");
1195+
}
1196+
11851197
#[test]
11861198
fn test_merge_unique() {
11871199
new_ucmd!()

0 commit comments

Comments
 (0)