Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/tar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ regex = { workspace = true }
tar = { workspace = true }
chrono = { workspace = true }
thiserror = { workspace = true }
libc = { workspace = true }
zstd = { workspace = true }
flate2 = "1"

Expand Down
10 changes: 10 additions & 0 deletions src/uu/tar/src/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ fn expand_posix_keystring(args: Vec<std::ffi::OsString>) -> Vec<std::ffi::OsStri

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Restore the default SIGPIPE disposition so that a closed stdout pipe
// (e.g. `tar tf archive.tar | head -1`) terminates the process with
// SIGPIPE, exit status 141, silently — matching GNU tar. The Rust
// runtime ignores SIGPIPE by default, which would instead surface the
// error as a noisy `TarError::Io` with exit code 2.
#[cfg(unix)]
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}

// Collect args - the test framework may add util_name as args[1], so skip it if present
let args_vec: Vec<_> = args.collect();
let util_name = uucore::util_name();
Expand Down
40 changes: 40 additions & 0 deletions tests/by-util/test_tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,3 +1326,43 @@ fn test_extract_invalid_gzip_archive_fails() {

ucmd.args(&["-xf", "invalid.tar.gz"]).fails().code_is(2);
}

#[cfg(unix)]
#[test]
fn test_list_broken_pipe_exits_via_sigpipe() {
// `tar tf archive.tar | head -1` must terminate via SIGPIPE (exit
// status 141) silently, like GNU tar — not report an I/O error with
// exit code 2 (uutils/tar#263). The archive lists more than a pipe's
// worth of entries so the write is guaranteed to fail once the
// reader is gone.
let (at, _ucmd) = at_and_ucmd!();

let mut tar_bytes = Vec::new();
{
let mut builder = TarRsBuilder::new(&mut tar_bytes);
let content = b"list test content";
for i in 0..900 {
let mut header = TarRsHeader::new_gnu();
header.set_path(format!("f{i}_{}", "x".repeat(80))).unwrap();
header.set_size(content.len() as u64);
header.set_mode(0o644);
header.set_cksum();
builder.append(&header, &content[..]).unwrap();
}
builder.finish().unwrap();
}
at.write_bytes("archive.tar", &tar_bytes);

use std::os::unix::process::ExitStatusExt;
use std::process::{Command, Stdio};
let mut child = Command::new(env!("CARGO_BIN_EXE_tarapp"))
.args(["-tf", &at.plus_as_string("archive.tar")])
.stdout(Stdio::piped())
.spawn()
.unwrap();
// Close the read end of the pipe: the next stdout write raises EPIPE.
drop(child.stdout.take());
let status = child.wait().unwrap();
assert_eq!(status.code(), None, "expected a signal death, not an exit");
assert_eq!(status.signal(), Some(13), "expected death by SIGPIPE (141)");
}
Loading