8000 cat: return the same error message as GNU with loop symlink by sylvestre · Pull Request #5466 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cat: return the same error message as GNU with loop symlink #5466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

< 8000 strong class="js-conversation-menu-button">Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) nonprint nonblank nonprinting
// spell-checker:ignore (ToDO) nonprint nonblank nonprinting ELOOP
use clap::{crate_version, Arg, ArgAction, Command};
use std::fs::{metadata, File};
use std::io::{self, IsTerminal, Read, Write};
Expand Down Expand Up @@ -50,6 +50,8 @@ enum CatError {
IsDirectory,
#[error("input file is output file")]
OutputIsInput,
#[error("Too many levels of symbolic links")]
TooManySymlinks,
}

type CatResult<T> = Result<T, CatError>;
Expand Down Expand Up @@ -401,7 +403,23 @@ fn get_input_type(path: &str) -> CatResult<InputType> {
return Ok(InputType::StdIn);
}

let ft = metadata(path)?.file_type();
let ft = match metadata(path) {
Ok(md) => md.file_type(),
Err(e) => {
if let Some(raw_error) = e.raw_os_error() {
// On Unix-like systems, the error code for "Too many levels of symbolic links" is 40 (ELOOP).
// we want to provide a proper error message in this case.
#[cfg(not(target_os = "macos"))]
let too_many_symlink_code = 40;
#[cfg(target_os = "macos")]
let too_many_symlink_code = 62;
if raw_error == too_many_symlink_code {
return Err(CatError::TooManySymlinks);
}
}
return Err(CatError::Io(e));
}
};
match ft {
#[cfg(unix)]
ft if ft.is_block_device() => Ok(InputType::BlockDevice),
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,15 @@ fn test_write_to_self() {
"first_file_content.second_file_content."
);
}

#[test]
#[cfg(unix)]
fn test_error_loop() {
let (at, mut ucmd) = at_and_ucmd!();
at.symlink_file("2", "1");
at.symlink_file("3", "2");
at.symlink_file("1", "3");
ucmd.arg("1")
.fails()
.stderr_is("cat: 1: Too many levels of symbolic links\n");
}
0