Skip to content

Commit

Permalink
dd: add bytes_total to ReadStat
Browse files Browse the repository at this point in the history
Add the `bytes_total` field to the `ReadStat` struct. This lets the
main loop of `dd` keep track of the total number of bytes read.
  • Loading branch information
jfinkels committed Mar 19, 2023
1 parent f056a74 commit 6de9c47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,13 @@ impl<'a> Input<'a> {
_ => break,
}
}

buf.truncate(bytes_total);
Ok(ReadStat {
reads_complete,
reads_partial,
// Records are not truncated when filling.
records_truncated: 0,
bytes_total: bytes_total.try_into().unwrap(),
})
}

Expand All @@ -334,6 +334,7 @@ impl<'a> Input<'a> {
let mut reads_complete = 0;
let mut reads_partial = 0;
let mut base_idx = 0;
let mut bytes_total = 0;

while base_idx < buf.len() {
let next_blk = cmp::min(base_idx + self.settings.ibs, buf.len());
Expand All @@ -342,11 +343,13 @@ impl<'a> Input<'a> {
match self.read(&mut buf[base_idx..next_blk])? {
0 => break,
rlen if rlen < target_len => {
bytes_total += rlen;
reads_partial += 1;
let padding = vec![pad; target_len - rlen];
buf.splice(base_idx + rlen..next_blk, padding.into_iter());
}
_ => {
rlen => {
bytes_total += rlen;
reads_complete += 1;
}
}
Expand All @@ -359,6 +362,7 @@ impl<'a> Input<'a> {
reads_complete,
reads_partial,
records_truncated: 0,
bytes_total: bytes_total.try_into().unwrap(),
})
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/uu/dd/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl ProgUpdate {
/// ```rust,ignore
/// use std::io::Cursor;
/// use std::time::Duration;
/// use crate::progress::{ProgUpdate, ReadState, WriteStat};
/// use crate::progress::{ProgUpdate, ReadStat, WriteStat};
///
/// let read_stat = ReadStat::new(1, 2, 3);
/// let read_stat = ReadStat::new(1, 2, 3, 999);
/// let write_stat = WriteStat::new(4, 5, 6);
/// let duration = Duration::new(789, 0);
/// let prog_update = ProgUpdate {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl ProgUpdate {
/// ```rust,ignore
/// use std::io::Cursor;
/// use std::time::Duration;
/// use crate::progress::{ProgUpdate, ReadState, WriteStat};
/// use crate::progress::ProgUpdate;
///
/// let prog_update = ProgUpdate {
/// read_stat: Default::default(),
Expand Down Expand Up @@ -184,7 +184,7 @@ impl ProgUpdate {
/// ```rust,ignore
/// use std::io::Cursor;
/// use std::time::Duration;
/// use crate::progress::{ProgUpdate, ReadState, WriteStat};
/// use crate::progress::ProgUpdate;
///
/// let prog_update = ProgUpdate {
/// read_stat: Default::default(),
Expand Down Expand Up @@ -269,16 +269,20 @@ pub(crate) struct ReadStat {
///
/// A truncated record can only occur in `conv=block` mode.
pub(crate) records_truncated: u32,

/// The total number of bytes read.
pub(crate) bytes_total: u64,
}

impl ReadStat {
/// Create a new instance.
#[allow(dead_code)]
fn new(complete: u64, partial: u64, truncated: u32) -> Self {
fn new(complete: u64, partial: u64, truncated: u32, bytes_total: u64) -> Self {
Self {
reads_complete: complete,
reads_partial: partial,
records_truncated: truncated,
bytes_total,
}
}

Expand Down Expand Up @@ -308,6 +312,7 @@ impl std::ops::AddAssign for ReadStat {
reads_complete: self.reads_complete + other.reads_complete,
reads_partial: self.reads_partial + other.reads_partial,
records_truncated: self.records_truncated + other.records_truncated,
bytes_total: self.bytes_total + other.bytes_total,
}
}
}
Expand Down Expand Up @@ -498,7 +503,7 @@ mod tests {

#[test]
fn test_read_stat_report() {
let read_stat = ReadStat::new(1, 2, 3);
let read_stat = ReadStat::new(1, 2, 3, 4);
let mut cursor = Cursor::new(vec![]);
read_stat.report(&mut cursor).unwrap();
assert_eq!(cursor.get_ref(), b"1+2 records in\n");
Expand All @@ -514,7 +519,7 @@ mod tests {

#[test]
fn test_prog_update_write_io_lines() {
let read_stat = ReadStat::new(1, 2, 3);
let read_stat = ReadStat::new(1, 2, 3, 4);
let write_stat = WriteStat::new(4, 5, 6);
let duration = Duration::new(789, 0);
let complete = false;
Expand Down

0 comments on commit 6de9c47

Please sign in to comment.