Skip to content

Commit

Permalink
Merge #44
Browse files Browse the repository at this point in the history
44: Implement progress bar r=obbardc a=Razaloc

Display a progress bar for the copy process

Closes: #33 

Signed-off-by: Rafael Garcia Ruiz <[email protected]>

Co-authored-by: Rafael Garcia Ruiz <[email protected]>
  • Loading branch information
bors[bot] and Razaloc authored Oct 31, 2022
2 parents a5e2b86 + 4aa1ef0 commit 441119d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions bmap-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ anyhow = "1.0.66"
nix = "0.25.0"
flate2 = "1.0.24"
clap = { version = "4.0.18", features = ["derive"] }
indicatif = "0.17.1"
13 changes: 11 additions & 2 deletions bmap-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use anyhow::{anyhow, bail, Context, Result};
use bmap::{Bmap, Discarder, SeekForward};
use clap::Parser;
use flate2::read::GzDecoder;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use nix::unistd::ftruncate;
use std::ffi::OsStr;
use std::fmt::Write;
use std::fs::File;
use std::io::Read;
use std::os::unix::io::AsRawFd;
Expand Down Expand Up @@ -100,7 +102,7 @@ fn copy(c: Copy) -> Result<()> {
b.read_to_string(&mut xml)?;

let bmap = Bmap::from_xml(&xml)?;
let mut output = std::fs::OpenOptions::new()
let output = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(c.dest)?;
Expand All @@ -111,7 +113,14 @@ fn copy(c: Copy) -> Result<()> {
}

let mut input = setup_input(&c.image)?;
bmap::copy(&mut input, &mut output, &bmap)?;
let pb = ProgressBar::new(bmap.total_mapped_size());
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
bmap::copy(&mut input, &mut pb.wrap_write(&output), &bmap)?;
pb.finish_and_clear();

println!("Done: Syncing...");
output.sync_all().expect("Sync failure");

Expand Down
3 changes: 3 additions & 0 deletions bmap/src/bmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl Bmap {
pub fn block_map(&self) -> impl ExactSizeIterator + Iterator<Item = &BlockRange> {
self.blockmap.iter()
}
pub fn total_mapped_size(&self) -> u64 {
self.block_size * self.mapped_blocks
}
}

#[derive(Clone, Debug, Error)]
Expand Down

0 comments on commit 441119d

Please sign in to comment.