@@ -23,7 +23,7 @@ const UPDATE_HASH_LEN: usize = 20;
2323pub struct DownloadCfg < ' a > {
2424 pub tmp_cx : & ' a temp:: Context ,
2525 pub download_dir : & ' a PathBuf ,
26- pub ( crate ) notifier : Notifier ,
26+ pub ( crate ) tracker : DownloadTracker ,
2727 pub process : & ' a Process ,
2828}
2929
@@ -33,7 +33,7 @@ impl<'a> DownloadCfg<'a> {
3333 DownloadCfg {
3434 tmp_cx : & cfg. tmp_cx ,
3535 download_dir : & cfg. download_dir ,
36- notifier : Notifier :: new ( cfg. quiet , cfg. process ) ,
36+ tracker : DownloadTracker :: new ( cfg. quiet , cfg. process ) ,
3737 process : cfg. process ,
3838 }
3939 }
@@ -47,7 +47,7 @@ impl<'a> DownloadCfg<'a> {
4747 let target_file = self . download_dir . join ( Path :: new ( hash) ) ;
4848
4949 if target_file. exists ( ) {
50- let cached_result = file_hash ( & target_file, & self . notifier ) ?;
50+ let cached_result = file_hash ( & target_file, & self . tracker ) ?;
5151 if hash == cached_result {
5252 debug ! ( "reusing previously downloaded file" ) ;
5353 debug ! ( url = url. as_ref( ) , "checksum passed" ) ;
@@ -76,7 +76,7 @@ impl<'a> DownloadCfg<'a> {
7676 & partial_file_path,
7777 Some ( & mut hasher) ,
7878 true ,
79- & self . notifier ,
79+ & self . tracker ,
8080 self . process ,
8181 )
8282 . await
@@ -125,7 +125,7 @@ impl<'a> DownloadCfg<'a> {
125125 let hash_url = utils:: parse_url ( & ( url. to_owned ( ) + ".sha256" ) ) ?;
126126 let hash_file = self . tmp_cx . new_file ( ) ?;
127127
128- download_file ( & hash_url, & hash_file, None , & self . notifier , self . process ) . await ?;
128+ download_file ( & hash_url, & hash_file, None , & self . tracker , self . process ) . await ?;
129129
130130 utils:: read_file ( "hash" , & hash_file) . map ( |s| s[ 0 ..64 ] . to_owned ( ) )
131131 }
@@ -166,7 +166,7 @@ impl<'a> DownloadCfg<'a> {
166166 let file = self . tmp_cx . new_file_with_ext ( "" , ext) ?;
167167
168168 let mut hasher = Sha256 :: new ( ) ;
169- download_file ( & url, & file, Some ( & mut hasher) , & self . notifier , self . process ) . await ?;
169+ download_file ( & url, & file, Some ( & mut hasher) , & self . tracker , self . process ) . await ?;
170170 let actual_hash = format ! ( "{:x}" , hasher. finalize( ) ) ;
171171
172172 if hash != actual_hash {
@@ -185,22 +185,6 @@ impl<'a> DownloadCfg<'a> {
185185 }
186186}
187187
188- pub ( crate ) struct Notifier {
189- tracker : Mutex < DownloadTracker > ,
190- }
191-
192- impl Notifier {
193- pub ( crate ) fn new ( quiet : bool , process : & Process ) -> Self {
194- Self {
195- tracker : Mutex :: new ( DownloadTracker :: new ( !quiet, process) ) ,
196- }
197- }
198-
199- pub ( crate ) fn handle ( & self , n : Notification < ' _ > ) {
200- self . tracker . lock ( ) . unwrap ( ) . handle_notification ( & n) ;
201- }
202- }
203-
204188/// Tracks download progress and displays information about it to a terminal.
205189///
206190/// *not* safe for tracking concurrent downloads yet - it is basically undefined
@@ -214,7 +198,7 @@ pub(crate) struct DownloadTracker {
214198 /// the message "retrying download" for at least a second.
215199 /// Without it, the progress bar would reappear immediately, not allowing the user to
216200 /// correctly see the message, before the progress bar starts again.
217- file_progress_bars : HashMap < String , ( ProgressBar , Option < Instant > ) > ,
201+ file_progress_bars : Mutex < HashMap < String , ( ProgressBar , Option < Instant > ) > > ,
218202}
219203
220204impl DownloadTracker {
@@ -228,12 +212,12 @@ impl DownloadTracker {
228212
229213 Self {
230214 multi_progress_bars,
231- file_progress_bars : HashMap :: new ( ) ,
215+ file_progress_bars : Mutex :: new ( HashMap :: new ( ) ) ,
232216 }
233217 }
234218
235- pub ( crate ) fn handle_notification ( & mut self , n : & Notification < ' _ > ) {
236- match * n {
219+ pub ( crate ) fn handle ( & self , n : Notification < ' _ > ) {
220+ match n {
237221 Notification :: DownloadContentLengthReceived ( content_len, url) => {
238222 if let Some ( url) = url {
239223 self . content_length_received ( content_len, url) ;
@@ -263,7 +247,7 @@ impl DownloadTracker {
263247 }
264248
265249 /// Creates a new ProgressBar for the given component.
266- pub ( crate ) fn create_progress_bar ( & mut self , component : String , url : String ) {
250+ pub ( crate ) fn create_progress_bar ( & self , component : String , url : String ) {
267251 let pb = ProgressBar :: hidden ( ) ;
268252 pb. set_style (
269253 ProgressStyle :: with_template (
@@ -274,20 +258,24 @@ impl DownloadTracker {
274258 ) ;
275259 pb. set_message ( component) ;
276260 self . multi_progress_bars . add ( pb. clone ( ) ) ;
277- self . file_progress_bars . insert ( url, ( pb, None ) ) ;
261+ self . file_progress_bars
262+ . lock ( )
263+ . unwrap ( )
264+ . insert ( url, ( pb, None ) ) ;
278265 }
279266
280267 /// Sets the length for a new ProgressBar and gives it a style.
281- pub ( crate ) fn content_length_received ( & mut self , content_len : u64 , url : & str ) {
282- if let Some ( ( pb, _) ) = self . file_progress_bars . get ( url) {
268+ pub ( crate ) fn content_length_received ( & self , content_len : u64 , url : & str ) {
269+ if let Some ( ( pb, _) ) = self . file_progress_bars . lock ( ) . unwrap ( ) . get ( url) {
283270 pb. reset ( ) ;
284271 pb. set_length ( content_len) ;
285272 }
286273 }
287274
288275 /// Notifies self that data of size `len` has been received.
289- pub ( crate ) fn data_received ( & mut self , len : usize , url : & str ) {
290- let Some ( ( pb, retry_time) ) = self . file_progress_bars . get_mut ( url) else {
276+ pub ( crate ) fn data_received ( & self , len : usize , url : & str ) {
277+ let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
278+ let Some ( ( pb, retry_time) ) = map. get_mut ( url) else {
291279 return ;
292280 } ;
293281 pb. inc ( len as u64 ) ;
@@ -305,8 +293,9 @@ impl DownloadTracker {
305293 }
306294
307295 /// Notifies self that the download has finished.
308- pub ( crate ) fn download_finished ( & mut self , url : & str ) {
309- let Some ( ( pb, _) ) = self . file_progress_bars . get ( url) else {
296+ pub ( crate ) fn download_finished ( & self , url : & str ) {
297+ let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
298+ let Some ( ( pb, _) ) = map. get ( url) else {
310299 return ;
311300 } ;
312301 pb. set_style (
@@ -317,8 +306,9 @@ impl DownloadTracker {
317306 }
318307
319308 /// Notifies self that the download has failed.
320- pub ( crate ) fn download_failed ( & mut self , url : & str ) {
321- let Some ( ( pb, _) ) = self . file_progress_bars . get ( url) else {
309+ pub ( crate ) fn download_failed ( & self , url : & str ) {
310+ let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
311+ let Some ( ( pb, _) ) = map. get ( url) else {
322312 return ;
323313 } ;
324314 pb. set_style (
@@ -329,8 +319,9 @@ impl DownloadTracker {
329319 }
330320
331321 /// Notifies self that the download is being retried.
332- pub ( crate ) fn retrying_download ( & mut self , url : & str ) {
333- let Some ( ( pb, retry_time) ) = self . file_progress_bars . get_mut ( url) else {
322+ pub ( crate ) fn retrying_download ( & self , url : & str ) {
323+ let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
324+ let Some ( ( pb, retry_time) ) = map. get_mut ( url) else {
334325 return ;
335326 } ;
336327 * retry_time = Some ( Instant :: now ( ) ) ;
@@ -354,9 +345,9 @@ pub(crate) enum Notification<'a> {
354345 DownloadFailed ( & ' a str ) ,
355346}
356347
357- fn file_hash ( path : & Path , notifier : & Notifier ) -> Result < String > {
348+ fn file_hash ( path : & Path , tracker : & DownloadTracker ) -> Result < String > {
358349 let mut hasher = Sha256 :: new ( ) ;
359- let mut downloaded = utils:: FileReaderWithProgress :: new_file ( path, notifier ) ?;
350+ let mut downloaded = utils:: FileReaderWithProgress :: new_file ( path, tracker ) ?;
360351 use std:: io:: Read ;
361352 let mut buf = vec ! [ 0 ; 32768 ] ;
362353 while let Ok ( n) = downloaded. read ( & mut buf) {
0 commit comments