Skip to content

Commit

Permalink
Drop dependency on time (#100)
Browse files Browse the repository at this point in the history
pbr currently depends on version 0.1 of time which is was found to contain a security vulnerability
(RUSTSEC-2020-0071 [1]). Updating to the current 0.3 branch would be a possiblity, however that
doesn't gain us anything over just relying on std. SteadyTime no longer exists and was never
actually steady in the first place [2]. For a while there was a fallback that turned SteadyTime
into an alias for time::Instant [3] which in turn is a wrapper around std::time::Instant adding
support for negative durations.

pbr doesn't deal with negative durations so swap time::Duration for std::time::Duration and
time::SteadyTime for std::time::Instant.

[1] https://rustsec.org/advisories/RUSTSEC-2020-0071
[2] time-rs/time#95
[3] time-rs/time@76e3575
  • Loading branch information
LingMan authored Jun 20, 2022
1 parent 6dd0bad commit f81de47
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ license = "MIT"

[dependencies]
libc = "0.2"
time = "0.1"
crossbeam-channel = "0.5"

[target.'cfg(target_os = "windows")'.dependencies.winapi]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ macro_rules! printfl {
}

extern crate crossbeam_channel;
extern crate time;
mod multi;
mod pb;
mod tty;
Expand Down
40 changes: 15 additions & 25 deletions src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::tty::{terminal_size, Width};
use std::io::Stdout;
use std::io::{self, Write};
use std::iter::repeat;
use std::time::Duration;
use time::{self, SteadyTime};
use std::time::{Duration, Instant};

macro_rules! kb_fmt {
($n: ident) => {{
Expand Down Expand Up @@ -37,7 +36,7 @@ pub enum Units {
}

pub struct ProgressBar<T: Write> {
start_time: SteadyTime,
start_time: Instant,
units: Units,
pub total: u64,
current: u64,
Expand All @@ -50,8 +49,8 @@ pub struct ProgressBar<T: Write> {
tick_state: usize,
width: Option<usize>,
message: String,
last_refresh_time: SteadyTime,
max_refresh_rate: Option<time::Duration>,
last_refresh_time: Instant,
max_refresh_rate: Option<Duration>,
pub is_finish: bool,
pub is_multibar: bool,
pub show_bar: bool,
Expand Down Expand Up @@ -112,7 +111,7 @@ impl<T: Write> ProgressBar<T> {
let mut pb = ProgressBar {
total: total,
current: 0,
start_time: SteadyTime::now(),
start_time: Instant::now(),
units: Units::Default,
is_finish: false,
is_multibar: false,
Expand All @@ -132,7 +131,7 @@ impl<T: Write> ProgressBar<T> {
tick_state: 0,
width: None,
message: String::new(),
last_refresh_time: SteadyTime::now(),
last_refresh_time: Instant::now(),
max_refresh_rate: None,
handle: handle,
};
Expand Down Expand Up @@ -242,7 +241,7 @@ impl<T: Write> ProgressBar<T> {
/// pb.set_max_refresh_rate(Some(Duration::from_millis(100)));
/// ```
pub fn set_max_refresh_rate(&mut self, w: Option<Duration>) {
self.max_refresh_rate = w.map(time::Duration::from_std).map(Result::unwrap);
self.max_refresh_rate = w;
if let Some(dur) = self.max_refresh_rate {
self.last_refresh_time = self.last_refresh_time - dur;
}
Expand Down Expand Up @@ -312,18 +311,21 @@ impl<T: Write> ProgressBar<T> {

/// Resets the start time to now
pub fn reset_start_time(&mut self) {
self.start_time = SteadyTime::now();
self.start_time = Instant::now();
}

fn draw(&mut self) {
let now = SteadyTime::now();
let now = Instant::now();
if let Some(mrr) = self.max_refresh_rate {
if now - self.last_refresh_time < mrr && self.current < self.total {
return;
}
}

let time_elapsed = time_to_std(now - self.start_time);
let mut time_elapsed = now - self.start_time;
if time_elapsed.is_zero() {
time_elapsed = Duration::from_nanos(1);
}
let speed = self.current as f64 / fract_dur(time_elapsed);
let width = self.width();

Expand Down Expand Up @@ -406,7 +408,7 @@ impl<T: Write> ProgressBar<T> {
// print
printfl!(self.handle, "\r{}", out);

self.last_refresh_time = SteadyTime::now();
self.last_refresh_time = Instant::now();
}

// finish_draw ensure that the progress bar is reached to its end, and do the
Expand All @@ -415,7 +417,7 @@ impl<T: Write> ProgressBar<T> {
let mut redraw = false;

if let Some(mrr) = self.max_refresh_rate {
if SteadyTime::now() - self.last_refresh_time < mrr {
if Instant::now() - self.last_refresh_time < mrr {
self.max_refresh_rate = None;
redraw = true;
}
Expand Down Expand Up @@ -489,18 +491,6 @@ impl<T: Write> Write for ProgressBar<T> {
}
}

fn time_to_std(d: time::Duration) -> Duration {
if d > time::Duration::zero() {
let secs = d.num_seconds();
let nsecs = (d - time::Duration::seconds(secs))
.num_nanoseconds()
.unwrap();
Duration::new(secs as u64, nsecs as u32)
} else {
Duration::new(0, 1)
}
}

fn fract_dur(d: Duration) -> f64 {
d.as_secs() as f64 + d.subsec_nanos() as f64 / NANOS_PER_SEC as f64
}
Expand Down

0 comments on commit f81de47

Please sign in to comment.