Skip to content

Commit c3eec6c

Browse files
authored
Merge pull request #221 from gportay/fix-CURLE_PARTIAL_FILE-on-downloading-index
Fix curle partial file on downloading index
2 parents 90757e4 + 161eebe commit c3eec6c

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

Diff for: src/casync-http.c

+29-24
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,22 @@ static int process_remote(CaRemote *rr, ProcessUntil until) {
168168
}
169169
}
170170

171-
static size_t write_index(const void *buffer, size_t size, size_t nmemb, void *userdata) {
172-
CaRemote *rr = userdata;
173-
size_t product;
171+
static int write_index(CaRemote *rr, ReallocBuffer *buffer) {
174172
int r;
175173

176-
product = size * nmemb;
174+
assert(rr);
177175

178176
r = process_remote(rr, PROCESS_UNTIL_CAN_PUT_INDEX);
179177
if (r < 0)
180-
return 0;
178+
return r;
181179

182-
r = ca_remote_put_index(rr, buffer, product);
180+
r = ca_remote_put_index(rr, realloc_buffer_data(buffer), realloc_buffer_size(buffer));
183181
if (r < 0) {
184182
log_error("Failed to put index: %m");
185-
return 0;
183+
return r;
186184
}
187185

188-
return product;
186+
return 0;
189187
}
190188

191189
static int write_index_eof(CaRemote *rr) {
@@ -240,24 +238,24 @@ static int write_archive_eof(CaRemote *rr) {
240238
return 0;
241239
}
242240

243-
static size_t write_chunk(const void *buffer, size_t size, size_t nmemb, void *userdata) {
244-
ReallocBuffer *chunk_buffer = userdata;
241+
static size_t write_buffer(const void *ptr, size_t size, size_t nmemb, void *userdata) {
242+
ReallocBuffer *buffer = userdata;
245243
size_t product, z;
246244

247245
product = size * nmemb;
248246

249-
z = realloc_buffer_size(chunk_buffer) + product;
250-
if (z < realloc_buffer_size(chunk_buffer)) {
247+
z = realloc_buffer_size(buffer) + product;
248+
if (z < realloc_buffer_size(buffer)) {
251249
log_error("Overflow");
252250
return 0;
253251
}
254252

255253
if (z > (CA_PROTOCOL_SIZE_MAX - offsetof(CaProtocolChunk, data))) {
256-
log_error("Chunk too large");
254+
log_error("Message too large");
257255
return 0;
258256
}
259257

260-
if (!realloc_buffer_append(chunk_buffer, buffer, product)) {
258+
if (!realloc_buffer_append(buffer, ptr, product)) {
261259
log_oom();
262260
return 0;
263261
}
@@ -292,13 +290,14 @@ static char *chunk_url(const char *store_url, const CaChunkID *id) {
292290
static int acquire_file(CaRemote *rr,
293291
CURL *curl,
294292
const char *url,
295-
size_t (*callback)(const void *p, size_t size, size_t nmemb, void *userdata)) {
296-
293+
size_t (*callback)(const void *p, size_t size, size_t nmemb, void *userdata),
294+
void *userdata) {
297295
long protocol_status;
298296

299297
assert(curl);
300298
assert(url);
301299
assert(callback);
300+
assert(userdata);
302301

303302
if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK) {
304303
log_error("Failed to set CURL URL to: %s", url);
@@ -310,7 +309,7 @@ static int acquire_file(CaRemote *rr,
310309
return -EIO;
311310
}
312311

313-
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, rr) != CURLE_OK) {
312+
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, userdata) != CURLE_OK) {
314313
log_error("Failed to set CURL private data.");
315314
return -EIO;
316315
}
@@ -375,7 +374,7 @@ static int run(int argc, char *argv[]) {
375374
size_t n_stores = 0, current_store = 0;
376375
CURL *curl = NULL;
377376
_cleanup_(ca_remote_unrefp) CaRemote *rr = NULL;
378-
_cleanup_(realloc_buffer_free) ReallocBuffer chunk_buffer = {};
377+
_cleanup_(realloc_buffer_free) ReallocBuffer buffer = {};
379378
_cleanup_free_ char *url_buffer = NULL;
380379
long protocol_status;
381380
int r;
@@ -478,7 +477,7 @@ static int run(int argc, char *argv[]) {
478477
}
479478

480479
if (archive_url) {
481-
r = acquire_file(rr, curl, archive_url, write_archive);
480+
r = acquire_file(rr, curl, archive_url, write_archive, rr);
482481
if (r < 0)
483482
goto finish;
484483
if (r == 0)
@@ -490,15 +489,21 @@ static int run(int argc, char *argv[]) {
490489
}
491490

492491
if (index_url) {
493-
r = acquire_file(rr, curl, index_url, write_index);
492+
r = acquire_file(rr, curl, index_url, write_buffer, &buffer);
494493
if (r < 0)
495494
goto finish;
496495
if (r == 0)
497496
goto flush;
498497

498+
r = write_index(rr, &buffer);
499+
if (r < 0)
500+
goto finish;
501+
499502
r = write_index_eof(rr);
500503
if (r < 0)
501504
goto finish;
505+
506+
realloc_buffer_empty(&buffer);
502507
}
503508

504509
for (;;) {
@@ -550,13 +555,13 @@ static int run(int argc, char *argv[]) {
550555
goto finish;
551556
}
552557

553-
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_chunk) != CURLE_OK) {
558+
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buffer) != CURLE_OK) {
554559
log_error("Failed to set CURL callback function.");
555560
r = -EIO;
556561
goto finish;
557562
}
558563

559-
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, &chunk_buffer) != CURLE_OK) {
564+
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer) != CURLE_OK) {
560565
log_error("Failed to set CURL private data.");
561566
r = -EIO;
562567
goto finish;
@@ -608,7 +613,7 @@ static int run(int argc, char *argv[]) {
608613
(arg_protocol == ARG_PROTOCOL_FTP && (protocol_status >= 200 && protocol_status <= 299))||
609614
(arg_protocol == ARG_PROTOCOL_SFTP && (protocol_status == 0))) {
610615

611-
r = ca_remote_put_chunk(rr, &id, CA_CHUNK_COMPRESSED, realloc_buffer_data(&chunk_buffer), realloc_buffer_size(&chunk_buffer));
616+
r = ca_remote_put_chunk(rr, &id, CA_CHUNK_COMPRESSED, realloc_buffer_data(&buffer), realloc_buffer_size(&buffer));
612617
if (r < 0) {
613618
log_error_errno(r, "Failed to write chunk: %m");
614619
goto finish;
@@ -625,7 +630,7 @@ static int run(int argc, char *argv[]) {
625630
}
626631
}
627632

628-
realloc_buffer_empty(&chunk_buffer);
633+
realloc_buffer_empty(&buffer);
629634

630635
r = process_remote(rr, PROCESS_UNTIL_WRITTEN);
631636
if (r == -EPIPE) {

0 commit comments

Comments
 (0)