Skip to content

Commit 1a29cdc

Browse files
committed
casync-http: Use an automatic pointer for the chunks curl handle
This removes the need for the 'finish' label, hence a bunch of goto go away. Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent db25594 commit 1a29cdc

File tree

1 file changed

+39
-71
lines changed

1 file changed

+39
-71
lines changed

src/casync-http.c

Lines changed: 39 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ static int acquire_file(CaRemote *rr, CURL *handle) {
485485
static int run(int argc, char *argv[]) {
486486
const char *base_url, *archive_url, *index_url, *wstore_url;
487487
size_t n_stores = 0, current_store = 0;
488-
CURL *curl = NULL;
488+
_cleanup_(curl_easy_cleanupp) CURL *curl = NULL;
489489
_cleanup_(ca_remote_unrefp) CaRemote *rr = NULL;
490490
_cleanup_(realloc_buffer_free) ReallocBuffer chunk_buffer = {};
491491
_cleanup_free_ char *url_buffer = NULL;
@@ -517,68 +517,62 @@ static int run(int argc, char *argv[]) {
517517
}
518518

519519
rr = ca_remote_new();
520-
if (!rr) {
521-
r = log_oom();
522-
goto finish;
523-
}
520+
if (!rr)
521+
return log_oom();
524522

525523
r = ca_remote_set_local_feature_flags(rr,
526524
(n_stores > 0 ? CA_PROTOCOL_READABLE_STORE : 0) |
527525
(index_url ? CA_PROTOCOL_READABLE_INDEX : 0) |
528526
(archive_url ? CA_PROTOCOL_READABLE_ARCHIVE : 0));
529-
if (r < 0) {
530-
log_error("Failed to set feature flags: %m");
531-
goto finish;
532-
}
527+
if (r < 0)
528+
return log_error_errno(r, "Failed to set feature flags: %m");
533529

534530
r = ca_remote_set_io_fds(rr, STDIN_FILENO, STDOUT_FILENO);
535-
if (r < 0) {
536-
log_error("Failed to set I/O file descriptors: %m");
537-
goto finish;
538-
}
531+
if (r < 0)
532+
return log_error_errno(r, "Failed to set I/O file descriptors: %m");
539533

540534
if (archive_url) {
541535
_cleanup_(curl_easy_cleanupp) CURL *handle = NULL;
542536

543537
r = make_curl_easy_handle(&handle, write_archive, rr, NULL);
544538
if (r < 0)
545-
goto finish;
539+
return r;
546540

547541
r = configure_curl_easy_handle(handle, archive_url);
548542
if (r < 0)
549-
goto finish;
543+
return r;
550544

551545
r = acquire_file(rr, handle);
552546
if (r < 0)
553-
goto finish;
547+
return r;
554548
if (r == 0)
555549
goto flush;
556550

557551
r = write_archive_eof(rr);
558552
if (r < 0)
559-
goto finish;
553+
return r;
560554
}
561555

562556
if (index_url) {
563557
_cleanup_(curl_easy_cleanupp) CURL *handle = NULL;
564558

565559
r = make_curl_easy_handle(&handle, write_index, rr, NULL);
566560
if (r < 0)
567-
goto finish;
561+
return r;
568562

569563
r = configure_curl_easy_handle(handle, index_url);
570564
if (r < 0)
571-
goto finish;
565+
return r;
572566

573567
r = acquire_file(rr, handle);
574568
if (r < 0)
575-
goto finish;
569+
return r;
576570
if (r == 0)
577571
goto flush;
578572

579573
r = write_index_eof(rr);
580574
if (r < 0)
581-
goto finish;
575+
return r;
582576
}
583577

584578
for (;;) {
@@ -587,8 +581,7 @@ static int run(int argc, char *argv[]) {
587581

588582
if (quit) {
589583
log_info("Got exit signal, quitting.");
590-
r = 0;
591-
goto finish;
584+
return 0;
592585
}
593586

594587
if (n_stores == 0) /* No stores? Then we did all we could do */
@@ -597,24 +590,20 @@ static int run(int argc, char *argv[]) {
597590
if (!curl) {
598591
r = make_curl_easy_handle(&curl, write_chunk, &chunk_buffer, NULL);
599592
if (r < 0)
600-
goto finish;
593+
return r;
601594
}
602595

603596
r = process_remote(rr, PROCESS_UNTIL_HAVE_REQUEST);
604-
if (r == -EPIPE) {
605-
r = 0;
606-
goto finish;
607-
}
597+
if (r == -EPIPE)
598+
return 0;
608599
if (r < 0)
609-
goto finish;
600+
return r;
610601

611602
r = ca_remote_next_request(rr, &id);
612603
if (r == -ENODATA)
613604
continue;
614-
if (r < 0) {
615-
log_error_errno(r, "Failed to determine next chunk to get: %m");
616-
goto finish;
617-
}
605+
if (r < 0)
606+
return log_error_errno(r, "Failed to determine next chunk to get: %m");
618607

619608
current_store = current_store % n_stores;
620609
if (wstore_url)
@@ -625,75 +614,54 @@ static int run(int argc, char *argv[]) {
625614

626615
free(url_buffer);
627616
url_buffer = chunk_url(store_url, &id);
628-
if (!url_buffer) {
629-
r = log_oom();
630-
goto finish;
631-
}
617+
if (!url_buffer)
618+
return log_oom();
632619

633620
r = configure_curl_easy_handle(curl, url_buffer);
634621
if (r < 0)
635-
goto finish;
622+
return r;
636623

637624
log_debug("Acquiring %s...", url_buffer);
638625

639-
if (robust_curl_easy_perform(curl) != CURLE_OK) {
640-
log_error("Failed to acquire %s", url_buffer);
641-
r = -EIO;
642-
goto finish;
643-
}
626+
if (robust_curl_easy_perform(curl) != CURLE_OK)
627+
return log_error_errno(-EIO, "Failed to acquire %s", url_buffer);
644628

645-
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &protocol_status) != CURLE_OK) {
646-
log_error("Failed to query response code");
647-
r = -EIO;
648-
goto finish;
649-
}
629+
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &protocol_status) != CURLE_OK)
630+
return log_error_errno(-EIO, "Failed to query response code");
650631

651632
r = process_remote(rr, PROCESS_UNTIL_CAN_PUT_CHUNK);
652-
if (r == -EPIPE) {
653-
r = 0;
654-
goto finish;
655-
}
633+
if (r == -EPIPE)
634+
return 0;
656635
if (r < 0)
657-
goto finish;
636+
return r;
658637

659638
if (protocol_status_ok(arg_protocol, protocol_status)) {
660639
r = ca_remote_put_chunk(rr, &id, CA_CHUNK_COMPRESSED, realloc_buffer_data(&chunk_buffer), realloc_buffer_size(&chunk_buffer));
661-
if (r < 0) {
662-
log_error_errno(r, "Failed to write chunk: %m");
663-
goto finish;
664-
}
665-
640+
if (r < 0)
641+
return log_error_errno(r, "Failed to write chunk: %m");
666642
} else {
667643
if (arg_verbose)
668644
log_error("%s server failure %ld while requesting %s",
669645
protocol_str(arg_protocol), protocol_status,
670646
url_buffer);
671647

672648
r = ca_remote_put_missing(rr, &id);
673-
if (r < 0) {
674-
log_error_errno(r, "Failed to write missing message: %m");
675-
goto finish;
676-
}
649+
if (r < 0)
650+
return log_error_errno(r, "Failed to write missing message: %m");
677651
}
678652

679653
realloc_buffer_empty(&chunk_buffer);
680654

681655
r = process_remote(rr, PROCESS_UNTIL_WRITTEN);
682-
if (r == -EPIPE) {
683-
r = 0;
684-
goto finish;
685-
}
656+
if (r == -EPIPE)
657+
return 0;
686658
if (r < 0)
687-
goto finish;
659+
return r;
688660
}
689661

690662
flush:
691663
r = process_remote(rr, PROCESS_UNTIL_FINISHED);
692664

693-
finish:
694-
if (curl)
695-
curl_easy_cleanup(curl);
696-
697665
return r;
698666
}
699667

0 commit comments

Comments
 (0)