Skip to content

Commit 690405d

Browse files
authored
fix(readlink): emit GNU-style Invalid argument for non-symlinks (#9189)
* fix(readlink): emit GNU-style Invalid argument for non-symlinks * Replace format! with translate! and test skip on Windows
1 parent 700bb1c commit 690405d

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ readlink-help-zero = separate output with NUL rather than newline
1414
# Error messages
1515
readlink-error-missing-operand = missing operand
1616
readlink-error-ignoring-no-newline = ignoring --no-newline with multiple arguments
17+
readlink-error-invalid-argument = {$path}: Invalid argument

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ readlink-help-zero = séparer la sortie avec NUL plutôt qu'une nouvelle ligne
1414
# Messages d'erreur
1515
readlink-error-missing-operand = opérande manquant
1616
readlink-error-ignoring-no-newline = ignorer --no-newline avec plusieurs arguments
17+
readlink-error-invalid-argument = {$path} : argument non valide

src/uu/readlink/src/readlink.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use std::ffi::OsString;
1010
use std::fs;
1111
use std::io::{Write, stdout};
1212
use std::path::{Path, PathBuf};
13-
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
13+
use uucore::error::{FromIo, UResult, UUsageError};
1414
use uucore::fs::{MissingHandling, ResolveMode, canonicalize};
15+
use uucore::libc::EINVAL;
1516
use uucore::line_ending::LineEnding;
1617
use uucore::translate;
1718
use uucore::{format_usage, show_error};
@@ -88,15 +89,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8889
show(&path, line_ending).map_err_context(String::new)?;
8990
}
9091
Err(err) => {
91-
return if verbose {
92-
Err(USimpleError::new(
93-
1,
94-
err.map_err_context(move || p.to_string_lossy().to_string())
95-
.to_string(),
96-
))
92+
if silent && !verbose {
93+
return Err(1.into());
94+
}
95+
96+
let path = p.to_string_lossy().into_owned();
97+
let message = if err.raw_os_error() == Some(EINVAL) {
98+
translate!("readlink-error-invalid-argument", "path" => path.clone())
9799
} else {
98-
Err(1.into())
100+
err.map_err_context(|| path.clone()).to_string()
99101
};
102+
show_error!("{message}");
103+
return Err(1.into());
100104
}
101105
}
102106
}

tests/by-util/test_readlink.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ fn test_symlink_to_itself_verbose() {
102102
.stderr_contains("Too many levels of symbolic links");
103103
}
104104

105+
#[test]
106+
#[cfg(not(windows))]
107+
fn test_posixly_correct_regular_file() {
108+
let scene = TestScenario::new(util_name!());
109+
let at = &scene.fixtures;
110+
at.touch("regfile");
111+
scene
112+
.ucmd()
113+
.env("POSIXLY_CORRECT", "1")
114+
.arg("regfile")
115+
.fails_with_code(1)
116+
.stderr_contains("Invalid argument")
117+
.no_stdout();
118+
}
119+
105120
#[test]
106121
fn test_trailing_slash_regular_file() {
107122
let scene = TestScenario::new(util_name!());

0 commit comments

Comments
 (0)