Skip to content

Commit e6bfd49

Browse files
committed
casync-http: Move chunks download in a separate function for clarity
The goal is to make the run() function more readable, and only outline the major steps, while the bulk of the work is left to other functions. Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent 77652a8 commit e6bfd49

File tree

1 file changed

+86
-76
lines changed

1 file changed

+86
-76
lines changed

src/casync-http.c

Lines changed: 86 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -481,14 +481,92 @@ static int acquire_file(CaRemote *rr, CURL *handle) {
481481
return 1;
482482
}
483483

484+
static int acquire_chunks(CaRemote *rr, const char *store_url) {
485+
_cleanup_free_ char *url_buffer = NULL;
486+
_cleanup_(curl_easy_cleanupp) CURL *curl = NULL;
487+
_cleanup_(realloc_buffer_free) ReallocBuffer chunk_buffer = {};
488+
int r;
489+
490+
r = make_curl_easy_handle(&curl, write_chunk, &chunk_buffer, NULL);
491+
if (r < 0)
492+
return r;
493+
494+
for (;;) {
495+
CURLcode c;
496+
CaChunkID id;
497+
long protocol_status;
498+
499+
if (quit) {
500+
log_info("Got exit signal, quitting.");
501+
return 0;
502+
}
503+
504+
r = process_remote(rr, PROCESS_UNTIL_HAVE_REQUEST);
505+
if (r == -EPIPE)
506+
return 0;
507+
if (r < 0)
508+
return r;
509+
510+
r = ca_remote_next_request(rr, &id);
511+
if (r == -ENODATA)
512+
continue;
513+
if (r < 0)
514+
return log_error_errno(r, "Failed to determine next chunk to get: %m");
515+
516+
free(url_buffer);
517+
url_buffer = chunk_url(store_url, &id);
518+
if (!url_buffer)
519+
return log_oom();
520+
521+
r = configure_curl_easy_handle(curl, url_buffer);
522+
if (r < 0)
523+
return r;
524+
525+
log_debug("Acquiring %s...", url_buffer);
526+
527+
c = robust_curl_easy_perform(curl);
528+
if (c != CURLE_OK)
529+
return log_error_curle(c, "Failed to acquire %s", url_buffer);
530+
531+
c = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &protocol_status);
532+
if (c != CURLE_OK)
533+
return log_error_curle(c, "Failed to query response code");
534+
535+
r = process_remote(rr, PROCESS_UNTIL_CAN_PUT_CHUNK);
536+
if (r == -EPIPE)
537+
return 0;
538+
if (r < 0)
539+
return r;
540+
541+
if (protocol_status_ok(arg_protocol, protocol_status)) {
542+
r = ca_remote_put_chunk(rr, &id, CA_CHUNK_COMPRESSED, realloc_buffer_data(&chunk_buffer), realloc_buffer_size(&chunk_buffer));
543+
if (r < 0)
544+
return log_error_errno(r, "Failed to write chunk: %m");
545+
} else {
546+
if (arg_verbose)
547+
log_error("%s server failure %ld while requesting %s",
548+
protocol_str(arg_protocol), protocol_status,
549+
url_buffer);
550+
551+
r = ca_remote_put_missing(rr, &id);
552+
if (r < 0)
553+
return log_error_errno(r, "Failed to write missing message: %m");
554+
}
555+
556+
realloc_buffer_empty(&chunk_buffer);
557+
558+
r = process_remote(rr, PROCESS_UNTIL_WRITTEN);
559+
if (r == -EPIPE)
560+
return 0;
561+
if (r < 0)
562+
return r;
563+
}
564+
}
565+
484566
static int run(int argc, char *argv[]) {
485567
const char *base_url, *archive_url, *index_url, *wstore_url;
486568
size_t n_stores = 0, current_store = 0;
487-
_cleanup_(curl_easy_cleanupp) CURL *curl = NULL;
488569
_cleanup_(ca_remote_unrefp) CaRemote *rr = NULL;
489-
_cleanup_(realloc_buffer_free) ReallocBuffer chunk_buffer = {};
490-
_cleanup_free_ char *url_buffer = NULL;
491-
long protocol_status;
492570
int r;
493571

494572
if (argc < _CA_REMOTE_ARG_MAX) {
@@ -574,86 +652,18 @@ static int run(int argc, char *argv[]) {
574652
return r;
575653
}
576654

577-
for (;;) {
655+
if (n_stores > 0) {
578656
const char *store_url;
579-
CaChunkID id;
580-
581-
if (quit) {
582-
log_info("Got exit signal, quitting.");
583-
return 0;
584-
}
585-
586-
if (n_stores == 0) /* No stores? Then we did all we could do */
587-
break;
588-
589-
if (!curl) {
590-
r = make_curl_easy_handle(&curl, write_chunk, &chunk_buffer, NULL);
591-
if (r < 0)
592-
return r;
593-
}
594-
595-
r = process_remote(rr, PROCESS_UNTIL_HAVE_REQUEST);
596-
if (r == -EPIPE)
597-
return 0;
598-
if (r < 0)
599-
return r;
600-
601-
r = ca_remote_next_request(rr, &id);
602-
if (r == -ENODATA)
603-
continue;
604-
if (r < 0)
605-
return log_error_errno(r, "Failed to determine next chunk to get: %m");
606657

607658
current_store = current_store % n_stores;
608659
if (wstore_url)
609-
store_url = current_store == 0 ? wstore_url : argv[current_store + _CA_REMOTE_ARG_MAX - 1];
660+
store_url = current_store == 0 ? wstore_url :
661+
argv[current_store + _CA_REMOTE_ARG_MAX - 1];
610662
else
611663
store_url = argv[current_store + _CA_REMOTE_ARG_MAX];
612664
/* current_store++; */
613665

614-
free(url_buffer);
615-
url_buffer = chunk_url(store_url, &id);
616-
if (!url_buffer)
617-
return log_oom();
618-
619-
r = configure_curl_easy_handle(curl, url_buffer);
620-
if (r < 0)
621-
return r;
622-
623-
log_debug("Acquiring %s...", url_buffer);
624-
625-
if (robust_curl_easy_perform(curl) != CURLE_OK)
626-
return log_error_errno(-EIO, "Failed to acquire %s", url_buffer);
627-
628-
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &protocol_status) != CURLE_OK)
629-
return log_error_errno(-EIO, "Failed to query response code");
630-
631-
r = process_remote(rr, PROCESS_UNTIL_CAN_PUT_CHUNK);
632-
if (r == -EPIPE)
633-
return 0;
634-
if (r < 0)
635-
return r;
636-
637-
if (protocol_status_ok(arg_protocol, protocol_status)) {
638-
r = ca_remote_put_chunk(rr, &id, CA_CHUNK_COMPRESSED, realloc_buffer_data(&chunk_buffer), realloc_buffer_size(&chunk_buffer));
639-
if (r < 0)
640-
return log_error_errno(r, "Failed to write chunk: %m");
641-
} else {
642-
if (arg_verbose)
643-
log_error("%s server failure %ld while requesting %s",
644-
protocol_str(arg_protocol), protocol_status,
645-
url_buffer);
646-
647-
r = ca_remote_put_missing(rr, &id);
648-
if (r < 0)
649-
return log_error_errno(r, "Failed to write missing message: %m");
650-
}
651-
652-
realloc_buffer_empty(&chunk_buffer);
653-
654-
r = process_remote(rr, PROCESS_UNTIL_WRITTEN);
655-
if (r == -EPIPE)
656-
return 0;
666+
r = acquire_chunks(rr, store_url);
657667
if (r < 0)
658668
return r;
659669
}

0 commit comments

Comments
 (0)