1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

cat: add error handling in write_fast function (#8091)

This commit is contained in:
Yuankun Zhang 2025-06-13 17:02:03 +08:00 committed by GitHub
parent e5bde07591
commit e1f40ff601
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 4 deletions

View file

@ -496,12 +496,17 @@ fn write_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
// If we're not on Linux or Android, or the splice() call failed,
// fall back on slower writing.
let mut buf = [0; 1024 * 64];
while let Ok(n) = handle.reader.read(&mut buf) {
loop {
match handle.reader.read(&mut buf) {
Ok(n) => {
if n == 0 {
break;
}
stdout_lock.write_all(&buf[..n])?;
}
Err(e) => return Err(e.into()),
}
}
// If the splice() call failed and there has been some data written to
// stdout via while loop above AND there will be second splice() call

View file

@ -713,6 +713,26 @@ fn test_u_ignored() {
}
}
#[test]
#[cfg(unix)]
fn test_write_fast_read_error() {
use std::os::unix::fs::PermissionsExt;
let (at, mut ucmd) = at_and_ucmd!();
// Create a file with content
at.write("foo", "content");
// Remove read permissions to cause a read error
let file_path = at.plus_as_string("foo");
let mut perms = std::fs::metadata(&file_path).unwrap().permissions();
perms.set_mode(0o000); // No permissions
std::fs::set_permissions(&file_path, perms).unwrap();
// Test that cat fails with permission denied
ucmd.arg("foo").fails().stderr_contains("Permission denied");
}
#[test]
#[cfg(target_os = "linux")]
fn test_appending_same_input_output() {