@@ -46,10 +46,17 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
46
46
}
47
47
48
48
// open file
49
+ std::string filepath_download (filepath);
50
+ filepath_download += " .download" ;
49
51
HFile file;
50
- ret = file.open (filepath, " wb" );
52
+ if (use_range) {
53
+ ret = file.open (filepath_download.c_str (), " ab" );
54
+ from = file.size ();
55
+ } else {
56
+ ret = file.open (filepath_download.c_str (), " wb" );
57
+ }
51
58
if (ret != 0 ) {
52
- fprintf (stderr, " Failed to open file %s\n " , filepath );
59
+ fprintf (stderr, " Failed to open file %s\n " , filepath_download. c_str () );
53
60
return ret;
54
61
}
55
62
printf (" Save file to %s ...\n " , filepath);
@@ -80,7 +87,7 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
80
87
fprintf (stderr, " request error: %d\n " , ret);
81
88
goto error;
82
89
}
83
- return 0 ;
90
+ goto success ;
84
91
}
85
92
86
93
// Range: bytes=from-to
@@ -103,10 +110,16 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
103
110
}
104
111
}
105
112
106
- return 0 ;
113
+ success:
114
+ file.close ();
115
+ ret = rename (filepath_download.c_str (), filepath);
116
+ if (ret != 0 ) {
117
+ fprintf (stderr, " mv %s => %s failed: %s:%d\n " , filepath_download.c_str (), filepath, strerror (ret), ret);
118
+ }
119
+ return ret;
107
120
error:
108
121
file.close ();
109
- // remove(filepath );
122
+ // remove(filepath_download.c_str() );
110
123
return ret;
111
124
}
112
125
0 commit comments