1- use std:: collections :: HashMap ;
1+ use std:: borrow :: Cow ;
22use std:: fs;
33use std:: ops;
44use std:: path:: { Path , PathBuf } ;
@@ -76,7 +76,7 @@ impl<'a> DownloadCfg<'a> {
7676 & partial_file_path,
7777 Some ( & mut hasher) ,
7878 true ,
79- & self . tracker ,
79+ None ,
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 . tracker , self . process ) . await ?;
128+ download_file ( & hash_url, & hash_file, None , None , self . process ) . await ?;
129129
130130 utils:: read_file ( "hash" , & hash_file) . map ( |s| s[ 0 ..64 ] . to_owned ( ) )
131131 }
@@ -139,6 +139,7 @@ impl<'a> DownloadCfg<'a> {
139139 & self ,
140140 url_str : & str ,
141141 update_hash : Option < & Path > ,
142+ status : Option < & DownloadStatus > ,
142143 ext : & str ,
143144 ) -> Result < Option < ( temp:: File , String ) > > {
144145 let hash = self . download_hash ( url_str) . await ?;
@@ -166,7 +167,7 @@ impl<'a> DownloadCfg<'a> {
166167 let file = self . tmp_cx . new_file_with_ext ( "" , ext) ?;
167168
168169 let mut hasher = Sha256 :: new ( ) ;
169- download_file ( & url, & file, Some ( & mut hasher) , & self . tracker , self . process ) . await ?;
170+ download_file ( & url, & file, Some ( & mut hasher) , status , self . process ) . await ?;
170171 let actual_hash = format ! ( "{:x}" , hasher. finalize( ) ) ;
171172
172173 if hash != actual_hash {
@@ -192,8 +193,6 @@ impl<'a> DownloadCfg<'a> {
192193pub ( crate ) struct DownloadTracker {
193194 /// MultiProgress bar for the downloads.
194195 multi_progress_bars : MultiProgress ,
195- /// Mapping of URLs being downloaded to their corresponding download status.
196- file_progress_bars : Mutex < HashMap < String , DownloadStatus > > ,
197196}
198197
199198impl DownloadTracker {
@@ -207,70 +206,30 @@ impl DownloadTracker {
207206
208207 Self {
209208 multi_progress_bars,
210- file_progress_bars : Mutex :: new ( HashMap :: new ( ) ) ,
211209 }
212210 }
213211
214212 /// Creates a new ProgressBar for the given component.
215- pub ( crate ) fn create_progress_bar ( & self , component : String , url : String ) {
213+ pub ( crate ) fn status_for ( & self , component : impl Into < Cow < ' static , str > > ) -> DownloadStatus {
216214 let status = DownloadStatus :: new ( component) ;
217215 self . multi_progress_bars . add ( status. progress . clone ( ) ) ;
218- self . file_progress_bars . lock ( ) . unwrap ( ) . insert ( url, status) ;
219- }
220-
221- /// Sets the length for a new ProgressBar and gives it a style.
222- pub ( crate ) fn content_length_received ( & self , content_len : u64 , url : & str ) {
223- if let Some ( status) = self . file_progress_bars . lock ( ) . unwrap ( ) . get ( url) {
224- status. received_length ( content_len) ;
225- }
226- }
227-
228- /// Notifies self that data of size `len` has been received.
229- pub ( crate ) fn data_received ( & self , len : usize , url : & str ) {
230- let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
231- if let Some ( status) = map. get_mut ( url) {
232- status. received_data ( len) ;
233- } ;
234- }
235-
236- /// Notifies self that the download has finished.
237- pub ( crate ) fn download_finished ( & self , url : & str ) {
238- let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
239- if let Some ( status) = map. get ( url) {
240- status. finished ( )
241- } ;
242- }
243-
244- /// Notifies self that the download has failed.
245- pub ( crate ) fn download_failed ( & self , url : & str ) {
246- let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
247- if let Some ( status) = map. get ( url) {
248- status. failed ( ) ;
249- } ;
250- }
251-
252- /// Notifies self that the download is being retried.
253- pub ( crate ) fn retrying_download ( & self , url : & str ) {
254- let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
255- if let Some ( status) = map. get_mut ( url) {
256- status. retrying ( ) ;
257- } ;
216+ status
258217 }
259218}
260219
261- struct DownloadStatus {
220+ pub ( crate ) struct DownloadStatus {
262221 progress : ProgressBar ,
263222 /// The instant where the download is being retried.
264223 ///
265224 /// Allows us to delay the reappearance of the progress bar so that the user can see
266225 /// the message "retrying download" for at least a second. Without it, the progress
267226 /// bar would reappear immediately, not allowing the user to correctly see the message,
268227 /// before the progress bar starts again.
269- retry_time : Option < Instant > ,
228+ retry_time : Mutex < Option < Instant > > ,
270229}
271230
272231impl DownloadStatus {
273- fn new ( component : String ) -> Self {
232+ fn new ( component : impl Into < Cow < ' static , str > > ) -> Self {
274233 let progress = ProgressBar :: hidden ( ) ;
275234 progress. set_style (
276235 ProgressStyle :: with_template (
@@ -283,25 +242,23 @@ impl DownloadStatus {
283242
284243 Self {
285244 progress,
286- retry_time : None ,
245+ retry_time : Mutex :: new ( None ) ,
287246 }
288247 }
289248
290- fn received_length ( & self , len : u64 ) {
249+ pub ( crate ) fn received_length ( & self , len : u64 ) {
291250 self . progress . reset ( ) ;
292251 self . progress . set_length ( len) ;
293252 }
294253
295- fn received_data ( & mut self , len : usize ) {
254+ pub ( crate ) fn received_data ( & self , len : usize ) {
296255 self . progress . inc ( len as u64 ) ;
297- if !self
298- . retry_time
299- . is_some_and ( |instant| instant. elapsed ( ) > Duration :: from_secs ( 1 ) )
300- {
256+ let mut retry_time = self . retry_time . lock ( ) . unwrap ( ) ;
257+ if !retry_time. is_some_and ( |instant| instant. elapsed ( ) > Duration :: from_secs ( 1 ) ) {
301258 return ;
302259 }
303260
304- self . retry_time = None ;
261+ * retry_time = None ;
305262 self . progress . set_style (
306263 ProgressStyle :: with_template (
307264 "{msg:>12.bold} [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA: {eta})" ,
@@ -311,24 +268,24 @@ impl DownloadStatus {
311268 ) ;
312269 }
313270
314- fn finished ( & self ) {
271+ pub ( crate ) fn finished ( & self ) {
315272 self . progress . set_style (
316273 ProgressStyle :: with_template ( "{msg:>12.bold} downloaded {total_bytes} in {elapsed}" )
317274 . unwrap ( ) ,
318275 ) ;
319276 self . progress . finish ( ) ;
320277 }
321278
322- fn failed ( & self ) {
279+ pub ( crate ) fn failed ( & self ) {
323280 self . progress . set_style (
324281 ProgressStyle :: with_template ( "{msg:>12.bold} download failed after {elapsed}" )
325282 . unwrap ( ) ,
326283 ) ;
327284 self . progress . finish ( ) ;
328285 }
329286
330- fn retrying ( & mut self ) {
331- self . retry_time = Some ( Instant :: now ( ) ) ;
287+ pub ( crate ) fn retrying ( & self ) {
288+ * self . retry_time . lock ( ) . unwrap ( ) = Some ( Instant :: now ( ) ) ;
332289 self . progress
333290 . set_style ( ProgressStyle :: with_template ( "{msg:>12.bold} retrying download" ) . unwrap ( ) ) ;
334291 }
0 commit comments