Skip to content

Commit 4ae10b0

Browse files
head: make process fail when writing to /dev/full
1 parent c19ab85 commit 4ae10b0

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/uu/head/src/head.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
99
use std::ffi::OsString;
10-
use std::io::{self, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write};
10+
use std::io::{self, ErrorKind, Read, Seek, SeekFrom, Write};
1111
use std::num::TryFromIntError;
1212
use thiserror::Error;
1313
use uucore::display::Quotable;
@@ -18,9 +18,6 @@ use uucore::{format_usage, help_about, help_usage, show};
1818

1919
const BUF_SIZE: usize = 65536;
2020

21-
/// The capacity in bytes for buffered writers.
22-
const BUFWRITER_CAPACITY: usize = 16_384; // 16 kilobytes
23-
2421
const ABOUT: &str = help_about!("head.md");
2522
const USAGE: &str = help_usage!("head.md");
2623

@@ -254,6 +251,7 @@ where
254251
let mut stdout = stdout.lock();
255252

256253
io::copy(&mut reader, &mut stdout)?;
254+
stdout.flush()?;
257255

258256
Ok(())
259257
}
@@ -263,11 +261,10 @@ fn read_n_lines(input: &mut impl std::io::BufRead, n: u64, separator: u8) -> std
263261
let mut reader = take_lines(input, n, separator);
264262

265263
// Write those bytes to `stdout`.
266-
let stdout = std::io::stdout();
267-
let stdout = stdout.lock();
268-
let mut writer = BufWriter::with_capacity(BUFWRITER_CAPACITY, stdout);
264+
let mut stdout = std::io::stdout();
269265

270-
io::copy(&mut reader, &mut writer)?;
266+
io::copy(&mut reader, &mut stdout)?;
267+
stdout.flush()?;
271268

272269
Ok(())
273270
}

tests/by-util/test_head.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ fn test_all_but_last_lines() {
476476
.stdout_is_fixture("lorem_ipsum_backwards_15_lines.expected");
477477
}
478478

479+
#[cfg(any(
480+
target_os = "linux",
481+
target_os = "android",
482+
target_os = "freebsd",
483+
target_os = "netbsd"
484+
))]
479485
#[test]
480486
fn test_write_to_dev_full() {
481487
use std::fs::OpenOptions;

0 commit comments

Comments
 (0)