dd: add bytes_total to ReadStat

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.
This commit is contained in:
Jeffrey Finkelstein 2023-02-23 22:03:36 -05:00
parent 00e1bbea33
commit 12e4acaec3
2 changed files with 18 additions and 9 deletions

View file

@ -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(),
})
}
@ -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());
@ -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;
}
}
@ -359,6 +362,7 @@ impl<'a> Input<'a> {
reads_complete,
reads_partial,
records_truncated: 0,
bytes_total: bytes_total.try_into().unwrap(),
})
}
}

View file

@ -79,9 +79,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 {
@ -121,7 +121,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(),
@ -191,7 +191,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(),
@ -276,16 +276,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,
}
}
@ -315,6 +319,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,
}
}
}
@ -514,7 +519,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");
@ -530,7 +535,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;