11use std:: {
22 fs:: { self , File , OpenOptions , Permissions } ,
3- io:: { Read as _, Write as _} ,
3+ io:: { Read as _, Seek as _ , SeekFrom , Write as _} ,
44 os:: unix:: fs:: PermissionsExt as _,
55 path:: { Path , PathBuf } ,
66 sync:: Arc ,
@@ -254,8 +254,13 @@ impl Download {
254254 if output_path. is_file ( ) {
255255 match self . overwrite {
256256 OverwriteMode :: Skip => {
257- debug ! ( path = %output_path. display( ) , "file exists, skipping download" ) ;
258- return Ok ( output_path) ;
257+ // Only skip if there's no resume info (complete download)
258+ // If resume info exists, it's a partial download that should continue
259+ if resume_info. is_none ( ) {
260+ debug ! ( path = %output_path. display( ) , "file exists, skipping download" ) ;
261+ return Ok ( output_path) ;
262+ }
263+ debug ! ( path = %output_path. display( ) , "file exists but is partial, resuming download" ) ;
259264 }
260265 OverwriteMode :: Force => {
261266 debug ! ( path = %output_path. display( ) , "file exists, forcing overwrite" ) ;
@@ -273,7 +278,7 @@ impl Download {
273278 }
274279 }
275280 }
276- } ;
281+ }
277282
278283 if let Some ( parent) = output_path. parent ( ) {
279284 trace ! ( path = %parent. display( ) , "creating parent directories" ) ;
@@ -395,8 +400,14 @@ impl Download {
395400 }
396401
397402 let mut file = if is_resuming {
398- trace ! ( path = %path. display( ) , "opening file for append (resume)" ) ;
399- OpenOptions :: new ( ) . append ( true ) . open ( path) ?
403+ let resume_pos = resume_from. unwrap ( ) ;
404+ trace ! ( path = %path. display( ) , resume_pos = resume_pos, "opening file for resume" ) ;
405+ // Truncate file to resume position to avoid duplicated bytes
406+ // (file may have more bytes than last checkpoint due to writes between checkpoints)
407+ let mut file = OpenOptions :: new ( ) . write ( true ) . open ( path) ?;
408+ file. set_len ( resume_pos) ?;
409+ file. seek ( SeekFrom :: End ( 0 ) ) ?;
410+ file
400411 } else {
401412 trace ! ( path = %path. display( ) , "creating new file" ) ;
402413 File :: create ( path) ?
0 commit comments