From d6114749f71cf20f213fe6982470705cc660a5dd Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Thu, 25 Jun 2026 00:52:39 +0900 Subject: [PATCH] split: remove buffering --- src/uu/split/src/platform/mod.rs | 6 ++---- src/uu/split/src/platform/unix.rs | 10 +++++----- src/uu/split/src/platform/windows.rs | 6 +++--- src/uu/split/src/split.rs | 30 ++++++++++------------------ 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/uu/split/src/platform/mod.rs b/src/uu/split/src/platform/mod.rs index c347b39b2eb..ba963bddf5e 100644 --- a/src/uu/split/src/platform/mod.rs +++ b/src/uu/split/src/platform/mod.rs @@ -31,7 +31,7 @@ pub fn instantiate_current_writer( input: &std::ffi::OsStr, filename: &std::ffi::OsStr, is_new: bool, -) -> std::io::Result>> { +) -> std::io::Result> { // Refuse to truncate/overwrite the input. WASI cannot do the fd-based check // unix/windows use, so this is a best-effort path comparison. if paths_refer_to_same_file(input, filename) { @@ -50,9 +50,7 @@ pub fn instantiate_current_writer( .append(true) .open(std::path::Path::new(filename))? }; - Ok(std::io::BufWriter::new( - Box::new(file) as Box - )) + Ok(Box::new(file) as Box) } #[cfg(unix)] diff --git a/src/uu/split/src/platform/unix.rs b/src/uu/split/src/platform/unix.rs index b3f13e1ba15..b847cf95801 100644 --- a/src/uu/split/src/platform/unix.rs +++ b/src/uu/split/src/platform/unix.rs @@ -4,7 +4,7 @@ // file that was distributed with this source code. use std::env; use std::ffi::{OsStr, OsString}; -use std::io::{BufWriter, Error, Result}; +use std::io::{Error, Result}; use std::io::{ErrorKind, Write}; use std::path::Path; use std::process::{Child, Command, Stdio}; @@ -131,7 +131,7 @@ pub fn instantiate_current_writer( input: &OsStr, filename: &OsStr, is_new: bool, -) -> Result>> { +) -> Result> { match filter { None => { let file = if is_new { @@ -156,12 +156,12 @@ pub fn instantiate_current_writer( file }; - Ok(BufWriter::new(Box::new(file) as Box)) + Ok(Box::new(file) as Box) } - Some(filter_command) => Ok(BufWriter::new(Box::new( + Some(filter_command) => Ok(Box::new( // spawn a shell command and write to it FilterWriter::new(filter_command, filename)?, - ) as Box)), + ) as Box), } } diff --git a/src/uu/split/src/platform/windows.rs b/src/uu/split/src/platform/windows.rs index 14ab602ab38..4feb80c65c6 100644 --- a/src/uu/split/src/platform/windows.rs +++ b/src/uu/split/src/platform/windows.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. use std::ffi::OsStr; -use std::io::{BufWriter, Error, Result}; +use std::io::{Error, Result}; use std::io::{ErrorKind, Write}; use std::path::Path; use uucore::display::Quotable; @@ -19,7 +19,7 @@ pub fn instantiate_current_writer( input: &OsStr, filename: &OsStr, is_new: bool, -) -> Result>> { +) -> Result> { let file = if is_new { create_or_truncate_output_file(input, filename)? } else { @@ -41,7 +41,7 @@ pub fn instantiate_current_writer( file }; - Ok(BufWriter::new(Box::new(file) as Box)) + Ok(Box::new(file) as Box) } fn create_or_truncate_output_file(input: &OsStr, filename: &OsStr) -> Result { diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 74136345563..a37a29cf2b5 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -17,7 +17,7 @@ use std::env; use std::ffi::{OsStr, OsString}; use std::fs::{File, metadata}; use std::io; -use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write, stdin}; +use std::io::{BufRead, BufReader, ErrorKind, Read, Seek, SeekFrom, Write, stdin}; use std::path::Path; use thiserror::Error; use uucore::display::Quotable; @@ -543,7 +543,7 @@ impl Settings { &self, filename: &OsStr, is_new: bool, - ) -> io::Result>> { + ) -> io::Result> { if platform::paths_refer_to_same_file(&self.input, filename) { return Err(io::Error::other( translate!("split-error-would-overwrite-input", "file" => filename.quote()), @@ -710,7 +710,7 @@ struct ByteChunkWriter<'a> { /// Once the number of bytes written to this writer exceeds /// `chunk_size`, a new writer is initialized and assigned to this /// field. - inner: BufWriter>, + inner: Box, /// Iterator that yields filenames for each chunk. filename_iterator: FilenameIterator<'a>, @@ -831,7 +831,7 @@ struct LineChunkWriter<'a> { /// Once the number of lines written to this writer exceeds /// `chunk_size`, a new writer is initialized and assigned to this /// field. - inner: BufWriter>, + inner: Box, /// Iterator that yields filenames for each chunk. filename_iterator: FilenameIterator<'a>, @@ -854,7 +854,7 @@ impl<'a> LineChunkWriter<'a> { fn start_new_chunk( settings: &Settings, filename_iterator: &mut FilenameIterator, - ) -> io::Result>> { + ) -> io::Result> { let filename = filename_iterator.next().ok_or_else(|| { io::Error::other(translate!("split-error-output-file-suffixes-exhausted")) })?; @@ -919,7 +919,7 @@ impl Write for LineChunkWriter<'_> { /// Output file parameters struct OutFile { filename: OsString, - maybe_writer: Option>>, + maybe_writer: Option>, is_new: bool, } @@ -932,7 +932,7 @@ trait ManageOutFiles { &mut self, idx: usize, settings: &Settings, - ) -> UResult<&mut BufWriter>>; + ) -> UResult<&mut Box>; /// Initialize a new set of output files /// Each [`OutFile`] is generated with filename, while the writer for it could be /// optional, to be instantiated later by the calling function as needed. @@ -951,11 +951,7 @@ trait ManageOutFiles { /// are flagged as `is_new=false`, so they can be re-opened for appending /// instead of created anew if we need to keep writing into them later, /// i.e. in case of round robin distribution as in [`n_chunks_by_line_round_robin`] - fn get_writer( - &mut self, - idx: usize, - settings: &Settings, - ) -> UResult<&mut BufWriter>>; + fn get_writer(&mut self, idx: usize, settings: &Settings) -> UResult<&mut Box>; } impl ManageOutFiles for OutFiles { @@ -1001,7 +997,7 @@ impl ManageOutFiles for OutFiles { &mut self, idx: usize, settings: &Settings, - ) -> UResult<&mut BufWriter>> { + ) -> UResult<&mut Box> { let mut count = 0; // Use-case for doing multiple tries of closing fds: // E.g. split running in parallel to other processes (e.g. another split) doing similar stuff, @@ -1048,11 +1044,7 @@ impl ManageOutFiles for OutFiles { } } - fn get_writer( - &mut self, - idx: usize, - settings: &Settings, - ) -> UResult<&mut BufWriter>> { + fn get_writer(&mut self, idx: usize, settings: &Settings) -> UResult<&mut Box> { if self[idx].maybe_writer.is_some() { Ok(self[idx].maybe_writer.as_mut().unwrap()) } else { @@ -1472,7 +1464,7 @@ where // to be overwritten for sure at the beginning of the loop below // because we start with `remaining == 0`, indicating that a new // chunk should start. - let mut writer: BufWriter> = BufWriter::new(Box::new(io::Cursor::new(vec![]))); + let mut writer: Box = Box::new(io::Cursor::new(vec![])); let mut remaining = 0; for line in lines_with_sep(reader, settings.separator) {