From: Michael Tremer Date: Sat, 12 Oct 2024 19:01:37 +0000 (+0000) Subject: xfer: Decouple more from HTTP client X-Git-Tag: 0.9.30~1057 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b5824a2d9be3071b38bf0ca2bb4caf841fbd2eee;p=pakfire.git xfer: Decouple more from HTTP client We used to require a HTTP client to create an xfer but we are actually not using it any more. That way, we can avoid some more complicated code paths and we can keep a single request and multiple requests further apart in the code. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/buildservice.c b/src/libpakfire/buildservice.c index 0758c2624..e1066cbde 100644 --- a/src/libpakfire/buildservice.c +++ b/src/libpakfire/buildservice.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -44,16 +43,45 @@ struct pakfire_buildservice { struct pakfire_ctx* ctx; int nrefs; + // URL + char url[PATH_MAX]; + // Configuration char keytab[PATH_MAX]; - // A HTTP Client - struct pakfire_httpclient* httpclient; - // Kerberos Context krb5_context krb5_ctx; }; +static int pakfire_buildservice_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_buildservice* service, const char* url, ...) { + struct pakfire_xfer* x = NULL; + va_list args; + int r; + + va_start(args, url); + + // Create a new xfer + r = __pakfire_xfer_create(&x, service->ctx, url, args); + if (r < 0) + goto ERROR; + + // Set the base URL + r = pakfire_xfer_set_baseurl(x, service->url); + if (r < 0) + goto ERROR; + + // Success + *xfer = pakfire_xfer_ref(x); + +ERROR: + if (x) + pakfire_xfer_unref(x); + va_end(args); + + return r; +} + static int pakfire_buildservice_setup_auth(struct pakfire_buildservice* service) { const char* error = NULL; int r; @@ -94,14 +122,9 @@ static int pakfire_buildservice_setup(struct pakfire_buildservice* service) { goto ERROR; } - // Setup the HTTP client - r = pakfire_httpclient_create(&service->httpclient, service->ctx, NULL); - if (r) - goto ERROR; - - // Set the URL - r = pakfire_httpclient_set_baseurl(service->httpclient, url); - if (r) + // Store the URL + r = pakfire_string_set(service->url, url); + if (r < 0) goto ERROR; // Fetch the keytab @@ -127,8 +150,6 @@ ERROR: static void pakfire_buildservice_free(struct pakfire_buildservice* service) { if (service->krb5_ctx) krb5_free_context(service->krb5_ctx); - if (service->httpclient) - pakfire_httpclient_unref(service->httpclient); if (service->ctx) pakfire_ctx_unref(service->ctx); @@ -187,7 +208,7 @@ PAKFIRE_EXPORT struct pakfire_buildservice* pakfire_buildservice_unref( } PAKFIRE_EXPORT const char* pakfire_buildservice_get_url(struct pakfire_buildservice* service) { - return pakfire_httpclient_get_baseurl(service->httpclient); + return service->url; } // Build @@ -198,7 +219,7 @@ int pakfire_buildservice_build(struct pakfire_buildservice* service, const char* int r; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/builds"); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/builds"); if (r) goto ERROR; @@ -281,7 +302,7 @@ static int pakfire_buildservice_create_upload(struct pakfire_buildservice* servi goto ERROR; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/uploads"); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads"); if (r) goto ERROR; @@ -358,7 +379,7 @@ static int pakfire_buildservice_upload_payload(struct pakfire_buildservice* serv int r; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/uploads/%s", uuid); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads/%s", uuid); if (r) goto ERROR; @@ -444,7 +465,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_list_uploads( int r; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/uploads"); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads"); if (r) goto ERROR; @@ -485,7 +506,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_delete_upload( int r; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/uploads/%s", uuid); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads/%s", uuid); if (r) goto ERROR; @@ -527,7 +548,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_list_repos(struct pakfire_buildservice* return -EINVAL; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/repos/%s", distro); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/repos/%s", distro); if (r) goto ERROR; @@ -571,7 +592,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_get_repo(struct pakfire_buildservice* se return -EINVAL; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/repos/%s/%s", distro, name); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/repos/%s/%s", distro, name); if (r) goto ERROR; @@ -608,7 +629,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_create_repo(struct pakfire_buildservice* return -EINVAL; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/repos/%s", distro); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/repos/%s", distro); if (r) goto ERROR; @@ -653,7 +674,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_delete_repo(struct pakfire_buildservice* int r; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/repos/%s/%s", distro, name); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/repos/%s/%s", distro, name); if (r) goto ERROR; @@ -691,7 +712,7 @@ PAKFIRE_EXPORT int pakfire_buildservice_job_finished(struct pakfire_buildservice num_packages++; // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/jobs/%s", uuid); + r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/jobs/%s", uuid); if (r) goto ERROR; diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index 087d1ee99..27c166b1b 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #define MAX_JOBS 64 @@ -42,6 +43,9 @@ struct pakfire_daemon { // HTTP Client struct pakfire_httpclient* client; + // URL + char url[PATH_MAX]; + // Event Loop sd_event* loop; @@ -488,6 +492,31 @@ static int pakfire_daemon_close(struct pakfire_xfer* xfer, int code, void* data) return 0; } +static int pakfire_daemon_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_daemon* daemon, const char* url) { + struct pakfire_xfer* x = NULL; + int r; + + // Create a new xfer + r = pakfire_xfer_create(&x, daemon->ctx, "%s", url); + if (r < 0) + goto ERROR; + + // Set the base URL + r = pakfire_xfer_set_baseurl(x, daemon->url); + if (r < 0) + goto ERROR; + + // Success + *xfer = pakfire_xfer_ref(x); + +ERROR: + if (x) + pakfire_xfer_unref(x); + + return r; +} + static int pakfire_daemon_connected(struct pakfire_xfer* xfer, void* data) { struct pakfire_daemon* daemon = data; int r; @@ -523,7 +552,7 @@ static int pakfire_daemon_connect(sd_event_source* s, uint64_t usec, void* data) CTX_INFO(daemon->ctx, "Connecting...\n"); // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, daemon->client, "/api/v1/builders/control"); + r = pakfire_daemon_xfer_create(&xfer, daemon, "/api/v1/builders/control"); if (r) goto ERROR; @@ -609,8 +638,9 @@ static int pakfire_daemon_setup_loop(struct pakfire_daemon* daemon) { return 0; } -static int pakfire_daemon_setup_httpclient(struct pakfire_daemon* daemon) { +static int pakfire_daemon_configure(struct pakfire_daemon* daemon) { struct pakfire_config* config = NULL; + const char* url = NULL; int r; // Fetch the configuration @@ -622,20 +652,13 @@ static int pakfire_daemon_setup_httpclient(struct pakfire_daemon* daemon) { } // Fetch the URL - const char* url = pakfire_config_get(config, "daemon", "url", "https://pakfire.ipfire.org"); + url = pakfire_config_get(config, "daemon", "url", "https://pakfire.ipfire.org"); - // Create the HTTP client - r = pakfire_httpclient_create(&daemon->client, daemon->ctx, daemon->loop); - if (r) + // Store the URL + r = pakfire_string_set(daemon->url, url); + if (r < 0) goto ERROR; - // Configure the base URL - r = pakfire_httpclient_set_baseurl(daemon->client, url); - if (r) { - CTX_ERROR(daemon->ctx, "Could not configure the URL\n"); - goto ERROR; - } - ERROR: if (config) pakfire_config_unref(config); @@ -674,14 +697,19 @@ int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ct // Initialize the reference counter d->nrefs = 1; + // Read configuration + r = pakfire_daemon_configure(d); + if (r < 0) + goto ERROR; + // Setup the event loop r = pakfire_daemon_setup_loop(d); if (r) goto ERROR; - // Setup the HTTP client - r = pakfire_daemon_setup_httpclient(d); - if (r) + // Create the HTTP client + r = pakfire_httpclient_create(&d->client, d->ctx, d->loop); + if (r < 0) goto ERROR; // Reconnect after one second @@ -721,6 +749,10 @@ struct pakfire_httpclient* pakfire_daemon_httpclient(struct pakfire_daemon* daem return pakfire_httpclient_ref(daemon->client); } +const char* pakfire_daemon_url(struct pakfire_daemon* daemon) { + return daemon->url; +} + int pakfire_daemon_main(struct pakfire_daemon* daemon) { int r; diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index b92bb8288..a81c9fa47 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -280,7 +279,7 @@ ERROR: return r; } -static int pakfire_dist_download_source(struct pakfire_httpclient* httpclient, +static int pakfire_dist_download_source(struct pakfire_ctx* ctx, struct pakfire_mirrorlist* mirrorlist, const char* filename, const char* cache_path) { struct pakfire_xfer* xfer = NULL; int r; @@ -290,7 +289,7 @@ static int pakfire_dist_download_source(struct pakfire_httpclient* httpclient, return 0; // Create a new transfer - r = pakfire_httpclient_create_xfer(&xfer, httpclient, "%s", filename); + r = pakfire_xfer_create(&xfer, ctx, "%s", filename); if (r) goto ERROR; @@ -317,7 +316,7 @@ ERROR: } static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packager* packager, - struct pakfire_package* pkg, struct pakfire_httpclient* httpclient, + struct pakfire_package* pkg, struct pakfire_ctx* ctx, struct pakfire_mirrorlist* mirrorlist, const char* filename) { char archive_path[PATH_MAX]; char cache_path[PATH_MAX]; @@ -331,7 +330,7 @@ static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packa return r; // Download the source file - r = pakfire_dist_download_source(httpclient, mirrorlist, filename, cache_path); + r = pakfire_dist_download_source(ctx, mirrorlist, filename, cache_path); if (r) return r; @@ -345,7 +344,6 @@ static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packa static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_packager* packager, struct pakfire_package* pkg, struct pakfire_parser* makefile) { - struct pakfire_httpclient* httpclient = NULL; struct pakfire_mirrorlist* mirrorlist = NULL; char* sources = NULL; char* p = NULL; @@ -360,11 +358,6 @@ static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_pack struct pakfire_ctx* ctx = pakfire_ctx(pakfire); - // Create a HTTP client - r = pakfire_httpclient_create(&httpclient, ctx, NULL); - if (r) - goto ERROR; - // Fetch the mirrorlist r = pakfire_dist_get_mirrorlist(pakfire, makefile, &mirrorlist); if (r) @@ -376,7 +369,7 @@ static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_pack while (source) { DEBUG(pakfire, "Adding source file %s\n", source); - r = pakfire_dist_add_source(pakfire, packager, pkg, httpclient, mirrorlist, source); + r = pakfire_dist_add_source(pakfire, packager, pkg, ctx, mirrorlist, source); if (r) { ERROR(pakfire, "Could not add '%s' to package: %m\n", source); goto ERROR; @@ -389,8 +382,6 @@ static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_pack r = 0; ERROR: - if (httpclient) - pakfire_httpclient_unref(httpclient); if (mirrorlist) pakfire_mirrorlist_unref(mirrorlist); if (ctx) diff --git a/src/libpakfire/httpclient.c b/src/libpakfire/httpclient.c index 3a4a2dfc8..128f1eb8e 100644 --- a/src/libpakfire/httpclient.c +++ b/src/libpakfire/httpclient.c @@ -459,49 +459,6 @@ sd_event* pakfire_httpclient_loop(struct pakfire_httpclient* client) { return sd_event_ref(client->loop); } -const char* pakfire_httpclient_get_baseurl(struct pakfire_httpclient* client) { - if (*client->baseurl) - return client->baseurl; - - return NULL; -} - -int pakfire_httpclient_set_baseurl(struct pakfire_httpclient* client, const char* baseurl) { - return pakfire_string_set(client->baseurl, baseurl); -} - -int pakfire_httpclient_create_xfer(struct pakfire_xfer** xfer, - struct pakfire_httpclient* client, const char* url, ...) { - struct pakfire_xfer* x = NULL; - va_list args; - int r; - - // Create a new transfer - va_start(args, url); - r = __pakfire_xfer_create(&x, client->ctx, client, url, args); - va_end(args); - if (r) - goto ERROR; - - // Configure the base URL - if (*client->baseurl) { - r = pakfire_xfer_set_baseurl(x, client->baseurl); - if (r) - goto ERROR; - } - - // Return the result - *xfer = x; - - return 0; - -ERROR: - if (x) - pakfire_xfer_unref(x); - - return r; -} - int pakfire_httpclient_enqueue_xfer(struct pakfire_httpclient* client, struct pakfire_xfer* xfer) { int r; @@ -525,7 +482,8 @@ int pakfire_httpclient_enqueue_xfer(struct pakfire_httpclient* client, return r; } - return 0; + // Transfer enqueued + return pakfire_xfer_enqueued(xfer, client); } int pakfire_httpclient_remove_xfer(struct pakfire_httpclient* client, diff --git a/src/libpakfire/include/pakfire/daemon.h b/src/libpakfire/include/pakfire/daemon.h index e332fc8df..d1dab407b 100644 --- a/src/libpakfire/include/pakfire/daemon.h +++ b/src/libpakfire/include/pakfire/daemon.h @@ -37,6 +37,8 @@ struct pakfire_daemon* pakfire_daemon_unref(struct pakfire_daemon* daemon); sd_event* pakfire_daemon_loop(struct pakfire_daemon* daemon); struct pakfire_httpclient* pakfire_daemon_httpclient(struct pakfire_daemon* daemon); +const char* pakfire_daemon_url(struct pakfire_daemon* daemon); + int pakfire_daemon_main(struct pakfire_daemon* daemon); int pakfire_daemon_job_finished(struct pakfire_daemon* daemon, struct pakfire_job* job); diff --git a/src/libpakfire/include/pakfire/httpclient.h b/src/libpakfire/include/pakfire/httpclient.h index 1d8f36b42..663d81640 100644 --- a/src/libpakfire/include/pakfire/httpclient.h +++ b/src/libpakfire/include/pakfire/httpclient.h @@ -39,14 +39,6 @@ struct pakfire_httpclient* pakfire_httpclient_ref(struct pakfire_httpclient* dow struct pakfire_httpclient* pakfire_httpclient_unref(struct pakfire_httpclient* downloader); sd_event* pakfire_httpclient_loop(struct pakfire_httpclient* client); -CURLSH* pakfire_httpclient_share(struct pakfire_httpclient* downloader); - -const char* pakfire_httpclient_get_baseurl(struct pakfire_httpclient* client); -int pakfire_httpclient_set_baseurl(struct pakfire_httpclient* client, const char* baseurl); - -int pakfire_httpclient_create_xfer(struct pakfire_xfer** xfer, - struct pakfire_httpclient* downloader, const char* url, ...) - __attribute__((format(printf, 3, 4))); int pakfire_httpclient_enqueue_xfer( struct pakfire_httpclient* downloader, struct pakfire_xfer* xfer); diff --git a/src/libpakfire/include/pakfire/xfer.h b/src/libpakfire/include/pakfire/xfer.h index 0e10ca00f..60cbe8a48 100644 --- a/src/libpakfire/include/pakfire/xfer.h +++ b/src/libpakfire/include/pakfire/xfer.h @@ -82,17 +82,17 @@ typedef enum pakfire_transfer_method { } pakfire_xfer_method_t; int pakfire_xfer_create(struct pakfire_xfer** transfer, struct pakfire_ctx* ctx, - struct pakfire_httpclient* httpclient, const char* url, ...) - __attribute__((format(printf, 4, 5))); + const char* url, ...) __attribute__((format(printf, 3, 4))); int __pakfire_xfer_create(struct pakfire_xfer** xfer, struct pakfire_ctx* ctx, - struct pakfire_httpclient* client, const char* url, va_list args) - __attribute__((format(printf, 4, 0))); + const char* url, va_list args) __attribute__((format(printf, 3, 0))); struct pakfire_xfer* pakfire_xfer_ref(struct pakfire_xfer* xfer); struct pakfire_xfer* pakfire_xfer_unref(struct pakfire_xfer* xfer); CURL* pakfire_xfer_handle(struct pakfire_xfer* xfer); +int pakfire_xfer_enqueued(struct pakfire_xfer* xfer, struct pakfire_httpclient* client); + int pakfire_xfer_set_method(struct pakfire_xfer* xfer, const pakfire_xfer_method_t method); diff --git a/src/libpakfire/job.c b/src/libpakfire/job.c index ad78d26cd..011183d48 100644 --- a/src/libpakfire/job.c +++ b/src/libpakfire/job.c @@ -52,9 +52,6 @@ struct pakfire_job { // Event Loop sd_event* loop; - // HTTP Client - struct pakfire_httpclient* client; - uuid_t job_id; char name[NAME_MAX]; @@ -237,8 +234,6 @@ static void pakfire_job_free(struct pakfire_job* job) { if (job->log.stderr) pakfire_log_stream_unref(job->log.stderr); - if (job->client) - pakfire_httpclient_unref(job->client); if (job->loop) sd_event_unref(job->loop); if (job->daemon) @@ -616,6 +611,35 @@ static int pakfire_job_closed(struct pakfire_xfer* xfer, int code, void* data) { return 0; } +static int pakfire_job_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_job* job, const char* url, ...) { + struct pakfire_xfer* x = NULL; + va_list args; + int r; + + va_start(args, url); + + // Create a new xfer + r = __pakfire_xfer_create(&x, job->ctx, url, args); + if (r < 0) + goto ERROR; + + // Set the base URL + r = pakfire_xfer_set_baseurl(x, pakfire_daemon_url(job->daemon)); + if (r < 0) + goto ERROR; + + // Success + *xfer = pakfire_xfer_ref(x); + +ERROR: + if (x) + pakfire_xfer_unref(x); + va_end(args); + + return r; +} + static int pakfire_job_connected(struct pakfire_xfer* xfer, void* data) { struct pakfire_job* job = data; @@ -639,6 +663,7 @@ static int pakfire_job_connected(struct pakfire_xfer* xfer, void* data) { static int pakfire_job_connect(sd_event_source* s, uint64_t usec, void* data) { struct pakfire_job* job = data; + struct pakfire_httpclient* client = NULL; struct pakfire_xfer* xfer = NULL; char job_id[UUID_STR_LEN]; int r; @@ -648,8 +673,15 @@ static int pakfire_job_connect(sd_event_source* s, uint64_t usec, void* data) { CTX_INFO(job->ctx, "Connecting to job %s...\n", job_id); + // Fetch a reference to the HTTP client + client = pakfire_daemon_httpclient(job->daemon); + if (!client) { + r = -errno; + goto ERROR; + } + // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, job->client, "/api/v1/jobs/%s", job_id); + r = pakfire_job_xfer_create(&xfer, job, "/api/v1/jobs/%s", job_id); if (r) goto ERROR; @@ -665,7 +697,7 @@ static int pakfire_job_connect(sd_event_source* s, uint64_t usec, void* data) { goto ERROR; // Enqueue the transfer - r = pakfire_httpclient_enqueue_xfer(job->client, xfer); + r = pakfire_httpclient_enqueue_xfer(client, xfer); if (r) goto ERROR; @@ -674,6 +706,8 @@ static int pakfire_job_connect(sd_event_source* s, uint64_t usec, void* data) { ERROR: if (xfer) pakfire_xfer_unref(xfer); + if (client) + pakfire_httpclient_unref(client); return r; } @@ -705,14 +739,6 @@ int pakfire_job_create(struct pakfire_job** job, struct pakfire_ctx* ctx, goto ERROR; } - // Fetch a reference to the HTTP client - j->client = pakfire_daemon_httpclient(daemon); - if (!j->client) { - CTX_ERROR(j->ctx, "Could not fetch the HTTP client: %m\n"); - r = -errno; - goto ERROR; - } - // Initialize the PID file descriptor j->pidfd = -1; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 7cfa6afc5..eda64e423 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -232,48 +232,43 @@ static int __pakfire_repo_path(struct pakfire_repo* repo, This is a convenience function to create a xfer with the settings of this repository. */ -static int pakfire_repo_create_xfer(struct pakfire_xfer** xfer, - struct pakfire_repo* repo, struct pakfire_httpclient* httpclient, const char* url) { +static int pakfire_repo_xfer_create( + struct pakfire_xfer** xfer, struct pakfire_repo* repo, const char* url) { struct pakfire_mirrorlist* mirrorlist = NULL; - struct pakfire_xfer* x = NULL; const char* baseurl = NULL; int r; // Create a new transfer - r = pakfire_httpclient_create_xfer(&x, httpclient, "%s", url); - if (r) + r = pakfire_xfer_create(xfer, repo->ctx, "%s", url); + if (r < 0) goto ERROR; // Set the baseurl baseurl = pakfire_repo_get_expanded_baseurl(repo); if (baseurl) { - r = pakfire_xfer_set_baseurl(x, baseurl); - if (r) + r = pakfire_xfer_set_baseurl(*xfer, baseurl); + if (r < 0) goto ERROR; } // Set the mirrorlist mirrorlist = pakfire_repo_get_mirrorlist(repo); if (mirrorlist) { - r = pakfire_xfer_set_mirrorlist(x, mirrorlist); - if (r) + r = pakfire_xfer_set_mirrorlist(*xfer, mirrorlist); + if (r < 0) goto ERROR; } - // Success - *xfer = x; - r = 0; - - goto CLEANUP; - ERROR: - if (x) - pakfire_xfer_unref(x); - -CLEANUP: if (mirrorlist) pakfire_mirrorlist_unref(mirrorlist); + // Destroy the xfer on error + if (r) { + if (*xfer) + pakfire_xfer_unref(*xfer); + } + return r; } @@ -438,7 +433,6 @@ struct pakfire_mirrorlist* pakfire_repo_get_mirrorlist(struct pakfire_repo* repo static int pakfire_repo_download_database(struct pakfire_repo* repo, const char* filename, const char* path) { - struct pakfire_httpclient* httpclient = NULL; struct pakfire_xfer* xfer = NULL; char title[NAME_MAX]; char url[PATH_MAX]; @@ -462,13 +456,8 @@ static int pakfire_repo_download_database(struct pakfire_repo* repo, if (r) return r; - // Create a HTTP client - r = pakfire_httpclient_create(&httpclient, repo->ctx, NULL); - if (r) - goto ERROR; - // Create a new transfer - r = pakfire_repo_create_xfer(&xfer, repo, httpclient, url); + r = pakfire_repo_xfer_create(&xfer, repo, url); if (r) goto ERROR; @@ -490,8 +479,6 @@ static int pakfire_repo_download_database(struct pakfire_repo* repo, ERROR: if (xfer) pakfire_xfer_unref(xfer); - if (httpclient) - pakfire_httpclient_unref(httpclient); return r; } @@ -588,7 +575,6 @@ ERROR: } static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int force) { - struct pakfire_httpclient* httpclient = NULL; struct pakfire_xfer* xfer = NULL; char* url = NULL; int r; @@ -629,13 +615,8 @@ static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int goto ERROR; } - // Create a new HTTP client - r = pakfire_httpclient_create(&httpclient, repo->ctx, NULL); - if (r) - goto ERROR; - // Create a new xfer - r = pakfire_repo_create_xfer(&xfer, repo, httpclient, url); + r = pakfire_repo_xfer_create(&xfer, repo, url); if (r) goto ERROR; @@ -647,8 +628,6 @@ static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int ERROR: if (xfer) pakfire_xfer_unref(xfer); - if (httpclient) - pakfire_httpclient_unref(httpclient); if (url) free(url); @@ -656,7 +635,6 @@ ERROR: } static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* path, int force) { - struct pakfire_httpclient* httpclient = NULL; struct pakfire_xfer* xfer = NULL; int r; @@ -689,13 +667,8 @@ static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* } } - // Create a HTTP client - r = pakfire_httpclient_create(&httpclient, repo->ctx, NULL); - if (r) - goto ERROR; - // Create a new transfer - r = pakfire_repo_create_xfer(&xfer, repo, httpclient, "repodata/repomd.json"); + r = pakfire_repo_xfer_create(&xfer, repo, "repodata/repomd.json"); if (r) goto ERROR; @@ -712,8 +685,6 @@ static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* ERROR: if (xfer) pakfire_xfer_unref(xfer); - if (httpclient) - pakfire_httpclient_unref(httpclient); return r; } @@ -1246,7 +1217,7 @@ int pakfire_repo_download_package(struct pakfire_repo* repo, struct pakfire_http } // Create a new transfer - r = pakfire_repo_create_xfer(&x, repo, httpclient, url); + r = pakfire_repo_xfer_create(&x, repo, url); if (r) goto ERROR; @@ -1289,7 +1260,6 @@ ERROR: static int pakfire_repo_download(struct pakfire_repo* repo, const char* url, struct pakfire_package** package) { - struct pakfire_httpclient* httpclient = NULL; struct pakfire_xfer* xfer = NULL; FILE* f = NULL; int r; @@ -1303,13 +1273,8 @@ static int pakfire_repo_download(struct pakfire_repo* repo, const char* url, goto ERROR; } - // Create the HTTP client - r = pakfire_httpclient_create(&httpclient, repo->ctx, NULL); - if (r) - goto ERROR; - // Create a new transfer - r = pakfire_httpclient_create_xfer(&xfer, httpclient, "%s", url); + r = pakfire_repo_xfer_create(&xfer, repo, url); if (r) goto ERROR; @@ -1331,8 +1296,6 @@ static int pakfire_repo_download(struct pakfire_repo* repo, const char* url, ERROR: if (xfer) pakfire_xfer_unref(xfer); - if (httpclient) - pakfire_httpclient_unref(httpclient); if (f) fclose(f); diff --git a/src/libpakfire/xfer.c b/src/libpakfire/xfer.c index 7703d354d..ff615fe91 100644 --- a/src/libpakfire/xfer.c +++ b/src/libpakfire/xfer.c @@ -340,20 +340,20 @@ ERROR: return r; } -int pakfire_xfer_create(struct pakfire_xfer** xfer, struct pakfire_ctx* ctx, - struct pakfire_httpclient* client, const char* url, ...) { +int pakfire_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_ctx* ctx, const char* url, ...) { va_list args; int r; va_start(args, url); - r = __pakfire_xfer_create(xfer, ctx, client, url, args); + r = __pakfire_xfer_create(xfer, ctx, url, args); va_end(args); return r; } -int __pakfire_xfer_create(struct pakfire_xfer** xfer, struct pakfire_ctx* ctx, - struct pakfire_httpclient* client, const char* url, va_list args) { +int __pakfire_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_ctx* ctx, const char* url, va_list args) { struct pakfire_xfer* x = NULL; char buffer[PATH_MAX]; int r; @@ -374,9 +374,6 @@ int __pakfire_xfer_create(struct pakfire_xfer** xfer, struct pakfire_ctx* ctx, // Initialize the reference counter x->nrefs = 1; - // Store a reference to the HTTP client - x->client = pakfire_httpclient_ref(client); - // Store the URL r = pakfire_string_set(x->url, buffer); if (r) @@ -431,6 +428,19 @@ CURL* pakfire_xfer_handle(struct pakfire_xfer* xfer) { return xfer->handle; } +int pakfire_xfer_enqueued(struct pakfire_xfer* xfer, struct pakfire_httpclient* client) { + // Remove the reference to the old client + if (xfer->client) { + pakfire_httpclient_unref(xfer->client); + xfer->client = NULL; + } + + // Store the new client + xfer->client = pakfire_httpclient_ref(client); + + return 0; +} + int pakfire_xfer_set_method(struct pakfire_xfer* xfer, const pakfire_xfer_method_t method) { const char* m = NULL;