1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use opts::{self, number::units};
use results::elapsed;
use std::{
    fmt::Display,
    io::{Error, Result, StderrLock, Write},
    time::SystemTime,
};
pub struct ErrWriter<W: std::io::Write> {
    /// the internal writer (almost always stdout)
    w: W,
    /// whether to make continual progress reports
    report: bool,
    /// Whether to continue on an io error
    continue_on_err: bool,

    /// when the previous report happened (in bytes, lines, or blcoks)
    prev_report: usize,
}

impl<W: Write> ErrWriter<W> {
    pub fn new(w: W, report: bool, continue_on_err: bool) -> Self {
        ErrWriter {
            w,
            report,
            continue_on_err,
            prev_report: 0,
        }
    }

    pub fn from_opts(w: W, o: &opts::Opts) -> Self {
        let report = o.status == opts::StatusLevel::Progress;
        let continue_on_err = o.cflag(opts::flags::Conv::NOERROR);
        ErrWriter {
            w,
            report,
            continue_on_err,
            prev_report: 0,
        }
    }

    pub fn basic(w: W) -> Self {
        ErrWriter {
            w,
            report: false,
            continue_on_err: false,
            prev_report: 0,
        }
    }
}
impl<W: Write> ErrWriter<W> {
    const BLOCK_THRESHOLD_FOR_REPORT: usize = 25;
    const LINE_THRESHOLD_FOR_REPORT: usize = 20;
    const STANDARD_BYTE_THRESHOLD: usize = 5 * units::MB;

    pub fn handle<T>(&mut self, res: Result<T>) -> Result<Option<T>> {
        match (res, self.continue_on_err) {
            (Err(err), true) => {
                writeln!(self.w, "io error: {}", err);
                Ok(None)
            },
            (Err(err), false) => Err(err),
            (Ok(t), _) => Ok(Some(t)),
        }
    }

    /// report status during standard operation
    pub fn report_status_standard(&mut self, bytes: usize, start: &SystemTime) -> std::io::Result<()> {
        if self.report && bytes.saturating_sub(self.prev_report) > Self::STANDARD_BYTE_THRESHOLD {
            self.prev_report = bytes;
            writeln!(
                self.w,
                "dd in progress: elapsed: {}: wrote {} bytes",
                elapsed(start),
                bytes
            )?;
        }
        Ok(())
    }

    pub fn report_status_block(&mut self, lines: usize, start: &SystemTime) {
        if self.report && lines % Self::LINE_THRESHOLD_FOR_REPORT == 0 {
            writeln!(
                self.w,
                "dd in progress: unblock: elapsed: {}, wrote {} lines",
                elapsed(&start),
                lines
            );
        }
    }

    pub fn report_status_unblock(&mut self, blocks: usize, start: &SystemTime) {
        if self.report && blocks % Self::BLOCK_THRESHOLD_FOR_REPORT == 0 {
            writeln!(
                self.w,
                "dd in progress: unblock: elapsed: {}, wrote {} fixed-length blocks",
                elapsed(&start),
                blocks
            );
        }
    }
}