Skip to content

Commit

Permalink
consider "fullblock" cmd line arg also for block writes
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Mar 9, 2024
1 parent 511005b commit 6eca368
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,30 @@ impl<'a> Output<'a> {
}
}

/// writes a block of data. optionally retries when first try didn't complete
///
/// this is needed by gnu-test: tests/dd/stats.s
/// the write can be interrupted by a system signal.
/// e.g. SIGUSR1 which is send to report status
/// without retry, the data might not be fully written to destination.
fn write_block(&mut self, chunk: &[u8]) -> io::Result<usize> {
let full_len = chunk.len();
let mut base_idx = 0;
loop {
match self.dst.write(&chunk[base_idx..]) {
Ok(wlen) => {
base_idx += wlen;
// take iflags.fullblock as oflags shall not have this option
if (base_idx >= full_len) || !self.settings.iflags.fullblock {
return Ok(base_idx);
}
}
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
}
}
}

/// Write the given bytes one block at a time.
///
/// This may write partial blocks (for example, if the underlying
Expand All @@ -788,7 +812,7 @@ impl<'a> Output<'a> {
let mut bytes_total = 0;

for chunk in buf.chunks(self.settings.obs) {
let wlen = self.dst.write(chunk)?;
let wlen = self.write_block(chunk)?;
if wlen < self.settings.obs {
writes_partial += 1;
} else {
Expand Down

0 comments on commit 6eca368

Please sign in to comment.