Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtcollins committed May 18, 2019
1 parent 9fdb88e commit 0bb14ce
Showing 1 changed file with 31 additions and 48 deletions.
79 changes: 31 additions & 48 deletions src/dist/component/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,45 +211,6 @@ impl<'a> TarPackage<'a> {
}

#[cfg(windows)]

// impl<'a> FileReaderWithProgress<'a> {
// pub fn new_file(path: &Path, notify_handler: &'a dyn Fn(Notification<'_>)) -> Result<Self> {
// let fh = match std::fs::File::open(path) {
// Ok(fh) => fh,
// Err(_) => Err(ErrorKind::ReadingFile {
// name: "downloaded",
// path: path.to_path_buf(),
// })?,
// };

// // Inform the tracker of the file size
// let flen = fh.metadata()?.len();
// (notify_handler)(Notification::DownloadContentLengthReceived(flen));

// let fh = BufReader::with_capacity(8 * 1024 * 1024, fh);

// Ok(FileReaderWithProgress {
// fh,
// notify_handler,
// nbytes: 0,
// flen: flen,
// })
// }
// }

// impl<'a> std::io::Read for FileReaderWithProgress<'a> {
// fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
// match self.fh.read(buf) {
// Ok(nbytes) => {
// self.nbytes += nbytes as u64;
// if nbytes != 0 {
// (self.notify_handler)(Notification::DownloadDataReceived(&buf[0..nbytes]));
// }
// if (nbytes == 0) || (self.flen == self.nbytes) {
// (self.notify_handler)(Notification::DownloadFinished);
// }
// Ok(nbytes)

mod unpacker {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -288,26 +249,48 @@ mod unpacker {

impl<'a> Drop for Unpacker<'a> {
fn drop(&mut self) {
let mut n_files = self.n_files.load(Ordering::Relaxed);
while n_files != 0 {
// Some explanation is in order. Even though the tar we are reading from (if
// any) will have had its FileWithProgress download tracking
// completed before we hit drop, that is not true if we are unwinding due to a
// failure, where the logical ownership of the progress bar is
// ambiguous, and as the tracker itself is abstracted out behind
// notifications etc we cannot just query for that. So: we assume no
// more reads of the underlying tar will take place: either the
// error unwinding will stop reads, or we completed; either way, we
// notify finished to the tracker to force a reset to zero; we set
// the units to files, show our progress, and set our units back
// afterwards. The largest archives today - rust docs - have ~20k
// items, and the download tracker's progress is confounded with
// actual handling of data today, we synthesis a data buffer and
// pretend to have bytes to deliver.
self.notify_handler.map(|handler| handler(Notification::DownloadFinished));
let prev_files = self.n_files.load(Ordering::Relaxed);
self.notify_handler.map(|handler| handler(Notification::DownloadContentLengthReceived(prev_files as u64)));
println!("pending close of {} files", prev_files);
let buf: Vec<u8> = vec![0; prev_files];
assert!(32767 > prev_files);
let mut current_files = prev_files;
while current_files != 0 {
use std::thread::sleep;
sleep(std::time::Duration::from_millis(100));
n_files = self.n_files.load(Ordering::Relaxed);

println!("pending close of {} files", n_files);
current_files = self.n_files.load(Ordering::Relaxed);
let step_count = prev_files - current_files;
self.notify_handler.map(|handler| handler(Notification::DownloadDataReceived(&buf[0..step_count])));
println!("closed {}, pending {}", step_count, current_files);
current_files = prev_files;
}
// (notify_handler)(Notification::DownloadContentLengthReceived(flen))
self.pool.join();
self.notify_handler.map(|handler| handler(Notification::DownloadFinished));
}
}
}

#[cfg(not(windows))]
mod unpacker {
use crate::utils::notifications::Notification;
pub struct Unpacker<'a> {}
impl<'a> Unpacker<'a> {
pub fn new(_notify_handler: Option<&'a dyn Fn(Notification<'_>)>) -> Unpacker {
pub struct Unpacker {}
impl Unpacker {
pub fn new<'a>(_notify_handler: Option<&'a dyn Fn(Notification<'_>)>) -> Unpacker {
Unpacker {}
}
pub fn handle(&mut self, _unpacked: tar::Unpacked) {}
Expand Down

0 comments on commit 0bb14ce

Please sign in to comment.