From 4f4c2d9c09546a8ee2e3d4d815b50bde3c798718 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 1 Jul 2025 10:41:15 +0000 Subject: [PATCH] client: Tidy up the mess that are the upload create callbacks Signed-off-by: Michael Tremer --- src/cli/lib/client-build.c | 9 +++- src/cli/lib/upload_create.c | 19 ++++---- src/pakfire/client.c | 19 ++------ src/pakfire/client.h | 9 +--- src/pakfire/job.c | 86 +++++++++++++++++++++++-------------- 5 files changed, 77 insertions(+), 65 deletions(-) diff --git a/src/cli/lib/client-build.c b/src/cli/lib/client-build.c index a6e79ff1..18ef929d 100644 --- a/src/cli/lib/client-build.c +++ b/src/cli/lib/client-build.c @@ -122,15 +122,20 @@ static int build_callback(pakfire_xfer* xfer, // Called when an upload was successful static int upload_callback(pakfire_client* client, - pakfire_client_upload_status status, const char* path, const char* uuid, void* data) { + const pakfire_xfer_response* response, const char* path, const char* uuid, void* data) { struct cli_local_args* local_args = data; int r; + // Handle errors + r = cli_api_error(response, "Failed to upload the archive"); + if (r < 0) + return 0; + // Create the build r = pakfire_client_build(client, uuid, local_args->repo, local_args->arches, local_args->flags, build_callback, NULL); if (r < 0) { - fprintf(stderr, "Failed to create build from %s: %s\n", path, strerror(-r)); + fprintf(stderr, "Failed to create build: %s\n", strerror(-r)); // Store the status local_args->status = r; diff --git a/src/cli/lib/upload_create.c b/src/cli/lib/upload_create.c index 3a72d259..662b94c9 100644 --- a/src/cli/lib/upload_create.c +++ b/src/cli/lib/upload_create.c @@ -22,6 +22,7 @@ #include +#include "api.h" #include "command.h" #include "pakfire.h" #include "upload_create.h" @@ -55,16 +56,16 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { return 0; } -static int upload_callback(pakfire_client* client, - pakfire_client_upload_status status, const char* path, const char* uuid, void* data) { - switch (status) { - case PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL: - printf("Successfully uploaded %s as %s\n", path, uuid); - return 0; +static int upload_callback(pakfire_client* client, const pakfire_xfer_response* response, + const char* path, const char* uuid, void* data) { + int r; - default: - break; - } + // Handle errors + r = cli_api_error(response, "Failed to upload the archive"); + if (r < 0) + return 0; + + printf("Successfully uploaded %s as %s\n", path, uuid); return 0; } diff --git a/src/pakfire/client.c b/src/pakfire/client.c index e9afae02..899d82b0 100644 --- a/src/pakfire/client.c +++ b/src/pakfire/client.c @@ -1018,7 +1018,7 @@ static int __pakfire_client_upload_create(pakfire_client_upload** upload, goto ERROR; } - // Reference the client + // Store a reference to the client self->client = client; // Store the path @@ -1085,25 +1085,14 @@ ERROR: return r; } -static int pakfire_upload_payload_callback(pakfire_xfer* xfer, - const pakfire_xfer_response* response, void* data) { - pakfire_client_upload_status s = PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL; +static int pakfire_upload_payload_callback( + pakfire_xfer* xfer, const pakfire_xfer_response* response, void* data) { pakfire_client_upload* upload = data; int r = 0; - // Determine the status - switch (response->error) { - case PAKFIRE_XFER_OK: - break; - - default: - s = PAKFIRE_CLIENT_UPLOAD_ERROR; - break; - } - // Call the callback (if any) if (upload->callback) - r = upload->callback(upload->client, s, upload->path, upload->uuid, upload->data); + r = upload->callback(upload->client, response, upload->path, upload->uuid, upload->data); // Free the upload pakfire_client_upload_free(upload); diff --git a/src/pakfire/client.h b/src/pakfire/client.h index 17ab0f95..1f508d5a 100644 --- a/src/pakfire/client.h +++ b/src/pakfire/client.h @@ -81,14 +81,9 @@ int pakfire_client_build(pakfire_client* client, const char* upload, const char* // Uploads -typedef enum { - PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL, - PAKFIRE_CLIENT_UPLOAD_ERROR, -} pakfire_client_upload_status; - typedef int (*pakfire_client_upload_callback) - (pakfire_client* client, pakfire_client_upload_status status, - const char* path, const char* uuid, void* data); + (pakfire_client* client, const pakfire_xfer_response* response, + const char* path, const char* uuid, void* data); int pakfire_client_upload_create(pakfire_client* client, const char* path, const char* filename, pakfire_client_upload_callback callback, void* data); diff --git a/src/pakfire/job.c b/src/pakfire/job.c index 1e2d8f30..85d68867 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -296,35 +296,45 @@ static int pakfire_job_finished(pakfire_job* self) { } static int pakfire_job_package_uploaded(pakfire_client* client, - pakfire_client_upload_status status, const char* path, const char* uuid, void* data) { + const pakfire_xfer_response* response, const char* path, const char* uuid, void* data) { pakfire_job* self = data; + char* error = NULL; int r; // Now we have one less uploads running self->uploads.running--; - switch (status) { - case PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL: - // Store the ID of the upload - r = pakfire_strings_append(&self->uploads.packages, uuid); - if (r < 0) - return r; - break; + // Handle any errors + if (response->error) { + r = pakfire_xfer_response_get_error(response, &error); + if (r < 0) + goto ERROR; - default: - ERROR(self->ctx, "Failed to upload %s: %s\n", path, strerror(status)); + ERROR(self->ctx, "Failed to upload: %s\n", error); - // XXX What do we do here? + // XXX What do we do here? - return status; + r = -ECANCELED; + goto ERROR; } + // Store the ID of the upload + r = pakfire_strings_append(&self->uploads.packages, uuid); + if (r < 0) + goto ERROR; + // Don't do anything if there are any other uploads still running if (self->uploads.running) - return 0; + goto ERROR; // Finish the job - return pakfire_job_finished(self); + r = pakfire_job_finished(self); + +ERROR: + if (error) + free(error); + + return r; } static int pakfire_job_upload_packages(pakfire_job* self) { @@ -380,30 +390,42 @@ ERROR: Called when the log file has been uploaded... */ static int pakfire_job_logfile_uploaded(pakfire_client* client, - pakfire_client_upload_status status, const char* path, const char* uuid, void* data) { + const pakfire_xfer_response* response, const char* path, const char* uuid, void* data) { pakfire_job* self = data; + char* error = NULL; + int r; - // Check the status - switch (status) { - case PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL: - // Free any previous upload IDs - if (self->uploads.logfile) - free(self->uploads.logfile); - - // Store the UUID of the upload - self->uploads.logfile = strdup(uuid); - if (self->uploads.logfile) - ERROR(self->ctx, "Failed to store the log file UUID: %m\n"); - break; + // Handle any errors + if (response->error) { + r = pakfire_xfer_response_get_error(response, &error); + if (r < 0) + goto ERROR; - // Log an error if we could not upload the log file - default: - ERROR(self->ctx, "Failed to upload the log file: %s\n", strerror(status)); - break; + ERROR(self->ctx, "Failed to upload the log file: %s\n", error); + r = -ECANCELED; + goto ERROR; + } + + // Free any previous upload IDs + if (self->uploads.logfile) + free(self->uploads.logfile); + + // Store the UUID of the upload + self->uploads.logfile = strdup(uuid); + if (!self->uploads.logfile) { + ERROR(self->ctx, "Failed to store the log file UUID: %m\n"); + r = -errno; + goto ERROR; } // Continue with uploading the packages - return pakfire_job_upload_packages(self); + r = pakfire_job_upload_packages(self); + +ERROR: + if (error) + free(error); + + return r; } static int pakfire_job_result(pakfire_ctx* ctx, pakfire_root* root, -- 2.47.3