Skip to content

Commit 18ddc48

Browse files
emirmrkaljevicsylvestre
authored andcommitted
readlink: report an empty operand as missing with -f/-e/-m
canonicalize() resolves an empty path to the current directory, so readlink -f '' printed the cwd and exited 0. Treat it as a missing file like GNU: nothing is printed for it, -v reports "'': No such file or directory", and the exit status is 1.
1 parent b96867b commit 18ddc48

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/uu/readlink/src/readlink.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::path::{Path, PathBuf};
1414
use uucore::display::Quotable;
1515
use uucore::error::{FromIo, UResult};
1616
use uucore::fs::{MissingHandling, ResolveMode, canonicalize};
17-
use uucore::libc::EINVAL;
17+
use uucore::libc::{EINVAL, ENOENT};
1818
use uucore::line_ending::LineEnding;
1919
use uucore::translate;
2020
use uucore::{format_usage, show_error};
@@ -82,6 +82,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8282
for p in &files {
8383
let path_result = if res_mode == ResolveMode::None {
8484
fs::read_link(p)
85+
} else if p.as_os_str().is_empty() {
86+
// An empty name never refers to a file, so there is nothing to
87+
// canonicalize; resolving it would yield the current directory.
88+
Err(std::io::Error::from_raw_os_error(ENOENT))
8589
} else {
8690
canonicalize(p, can_mode, res_mode)
8791
};

‎tests/by-util/test_readlink.rs‎

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

66
// spell-checker:ignore regfile parentdir
77

8+
use rstest::rstest;
89
use uutests::util::{TestScenario, get_root_path};
910
use uutests::{at_and_ucmd, new_ucmd, path_concat, util_name};
1011

@@ -80,6 +81,37 @@ fn test_canonicalize_existing_keeps_going_after_a_missing_operand() {
8081
assert_eq!(actual, expect);
8182
}
8283

84+
#[uutests::wasi_ignore(GuestRootPathResolution)]
85+
#[rstest]
86+
#[case::canonicalize("-f")]
87+
#[case::canonicalize_existing("-e")]
88+
#[case::canonicalize_missing("-m")]
89+
fn test_canonicalize_empty_operand(#[case] mode: &str) {
90+
let scene = TestScenario::new(util_name!());
91+
let at = &scene.fixtures;
92+
at.mkdir("dir");
93+
let dir = path_concat!(at.root_dir_resolved(), "dir") + "\n";
94+
95+
// An empty operand is a missing file, not the current directory, and
96+
// does not stop the operands after it.
97+
scene
98+
.ucmd()
99+
.args(&[mode, "", "dir"])
100+
.fails_with_code(1)
101+
.stdout_is(&dir)
102+
.no_stderr();
103+
104+
let result = scene
105+
.ucmd()
106+
.args(&["-v", mode, "", "dir"])
107+
.fails_with_code(1);
108+
result.stdout_is(&dir);
109+
#[cfg(not(windows))]
110+
result.stderr_is("readlink: '': No such file or directory\n");
111+
#[cfg(windows)]
112+
result.stderr_contains("readlink: '': ");
113+
}
114+
83115
#[uutests::wasi_ignore(GuestRootPathResolution)]
84116
#[test]
85117
fn test_canonicalize() {

0 commit comments

Comments
 (0)