11use std:: collections:: HashMap ;
2- use std:: path:: Path ;
32use std:: path:: PathBuf ;
43use std:: str:: FromStr ;
54use std:: time:: Duration ;
65
76use anyhow:: bail;
87use anyhow:: ensure;
98use anyhow:: Context ;
10- use bytes:: Bytes ;
119use futures:: StreamExt ;
1210use reqwest:: StatusCode ;
1311use tokio:: fs:: File ;
1412use tokio:: io:: AsyncWriteExt ;
15- use tracing:: error;
1613use tracing:: info;
1714use tracing:: warn;
1815
@@ -39,7 +36,6 @@ const DOWNLOAD_BACKOFF_MIN_MILLIS: u64 = 100;
3936const DOWNLOAD_BACKOFF_MAX_MILLIS : u64 = 10_000 ;
4037
4138/// Ratio to convert byte to megabytes.
42- const BYTES_TO_MEGABYTES_USIZE : usize = 1024 * 1024 ;
4339const BYTES_TO_MEGABYTES_U64 : u64 = 1024 * 1024 ;
4440
4541/// Download and verify `file_name`.
@@ -55,7 +51,7 @@ pub async fn download_and_checksum(
5551 param_dir : & str ,
5652 file_name : & str ,
5753 checksums : & HashMap < String , blake3:: Hash > ,
58- ) -> anyhow:: Result < Bytes > {
54+ ) -> anyhow:: Result < PathBuf > {
5955 let mut filepath = PathBuf :: from ( param_dir) ;
6056 filepath. push ( file_name) ;
6157
@@ -84,16 +80,14 @@ pub async fn download_and_checksum(
8480 . with_context ( || format ! ( "Failed to create file. filepath: {}" , filepath. display( ) ) ) ?;
8581
8682 let mut hasher = blake3:: Hasher :: new ( ) ;
87- let mut buf = std:: fs:: read ( & filepath)
88- . with_context ( || format ! ( "Reading file failed. filepath: {}" , filepath. display( ) ) ) ?;
89- hasher. update_rayon ( & buf) ;
83+ hasher. update_mmap_rayon ( & filepath) ?;
9084
9185 if * expected_checksum == hasher. finalize ( ) {
9286 info ! (
9387 "Found file matching checksum, skipping download. filepath: {}" ,
9488 filepath. display( )
9589 ) ;
96- return Ok ( Bytes :: from ( buf ) ) ;
90+ return Ok ( filepath ) ;
9791 }
9892
9993 let fileurl = format ! ( "{base_url}/{file_name}" ) ;
@@ -111,10 +105,7 @@ pub async fn download_and_checksum(
111105
112106 for ( retry, duration) in backoff. iter ( ) . enumerate ( ) {
113107 let result = resume_download (
114- & mut buf,
115- base_url,
116108 & mut file,
117- & filepath,
118109 & fileurl,
119110 retry,
120111 & client,
@@ -125,12 +116,8 @@ pub async fn download_and_checksum(
125116
126117 match result {
127118 Ok ( ( ) ) => {
128- info ! (
129- "Params loaded. filepath: {} size: {}MiB" ,
130- filepath. display( ) ,
131- buf. len( ) / BYTES_TO_MEGABYTES_USIZE ,
132- ) ;
133- return Ok ( Bytes :: from ( buf) ) ;
119+ info ! ( "Downloaded file. filepath: {}" , filepath. display( ) ) ;
120+ return Ok ( filepath) ;
134121 } ,
135122 Err ( err) => {
136123 if let Some ( duration) = duration {
@@ -154,10 +141,7 @@ pub async fn download_and_checksum(
154141
155142#[ allow( clippy:: too_many_arguments) ]
156143async fn resume_download (
157- buf : & mut Vec < u8 > ,
158- base_url : & str ,
159144 file : & mut File ,
160- filepath : & Path ,
161145 fileurl : & str ,
162146 retry : usize ,
163147 client : & reqwest:: Client ,
@@ -182,66 +166,44 @@ async fn resume_download(
182166 Ok ( parsed)
183167 } ) ?;
184168
169+ if response. status ( ) == StatusCode :: RANGE_NOT_SATISFIABLE {
170+ hasher. reset ( ) ;
171+ file. set_len ( 0 ) . await ?;
172+ bail ! ( "Local file is bigger than remote, reset file and restart download" ) ;
173+ }
174+
175+ ensure ! (
176+ response. status( ) . is_success( ) ,
177+ "Requesting params failed. status: {}" ,
178+ response. status( ) ,
179+ ) ;
180+
185181 info ! (
186- "Downloading params. base_url: {} filepath: {} present: {}MiB download: {}MiB retry: {}" ,
187- base_url,
188- filepath. display( ) ,
182+ "Downloading params. fileurl: {} present: {}MiB download: {}MiB retry: {}" ,
183+ fileurl,
189184 metadata. len( ) / BYTES_TO_MEGABYTES_U64 ,
190185 length / BYTES_TO_MEGABYTES_U64 ,
191186 retry,
192187 ) ;
193188
194- if response. status ( ) == StatusCode :: RANGE_NOT_SATISFIABLE {
195- warn ! (
196- "Local file is bigger than remote, resetting length and checking checksum. filepath: {}" ,
197- filepath. display( ) ,
198- ) ;
199-
200- hasher. reset ( ) ;
201- buf. resize (
202- length. try_into ( ) . expect ( "File size should fit in a usize" ) ,
203- 0 ,
204- ) ;
205- file. set_len ( length) . await ?;
206- hasher. update_rayon ( buf) ;
207- } else {
208- ensure ! (
209- response. status( ) . is_success( ) ,
210- "Requesting params failed. status: {} filepath: {}" ,
211- response. status( ) ,
212- filepath. display( ) ,
213- ) ;
214-
215- let mut stream = response. bytes_stream ( ) ;
216- while let Some ( data) = stream. next ( ) . await {
217- let data = data?;
218- file. write_all ( & data) . await ?;
219- hasher. update_rayon ( & data) ;
220- buf. extend ( data) ;
221- }
189+ let mut stream = response. bytes_stream ( ) ;
190+ while let Some ( data) = stream. next ( ) . await {
191+ let data = data?;
192+ file. write_all ( & data) . await ?;
193+ hasher. update_rayon ( & data) ;
222194 }
223195
224196 let found_checksum = hasher. finalize ( ) ;
225197 if found_checksum != * expected_checksum {
226- error ! (
227- "Checksum failed, restarting download. checksum: {} expected: {} filepath: {}" ,
228- found_checksum. to_hex( ) ,
229- expected_checksum. to_hex( ) ,
230- filepath. display( ) ,
231- ) ;
232-
233198 hasher. reset ( ) ;
234- buf. clear ( ) ;
235199 file. set_len ( 0 ) . await ?;
236200
237201 bail ! (
238- "Checksum failed, restarting download. checksum: {} expected: {} filepath: {} " ,
202+ "Checksum failed, restarting download. checksum: {} expected: {}" ,
239203 found_checksum. to_hex( ) ,
240204 expected_checksum. to_hex( ) ,
241- filepath. display( ) ,
242205 ) ;
243206 } else {
244- info ! ( "Downloaded file. filepath: {}" , filepath. display( ) ) ;
245207 Ok ( ( ) )
246208 }
247209}
0 commit comments