]> git.ipfire.org Git - pakfire.git/commitdiff
client: Tidy up the mess that are the upload create callbacks
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 10:41:15 +0000 (10:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 10:41:15 +0000 (10:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/client-build.c
src/cli/lib/upload_create.c
src/pakfire/client.c
src/pakfire/client.h
src/pakfire/job.c

index a6e79ff1edeaa1e9c7e0dcc2ead91df34f421f67..18ef929de81fe5301619ebd76a4fe3c3b2249f97 100644 (file)
@@ -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;
index 3a72d2590a7c56117ffbae8a928ce8c22e4ab354..662b94c91c32fc59d55da21ede062b8a056118d9 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <pakfire/client.h>
 
+#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;
 }
index e9afae021b3d132a8afbb9d70c15c60a1c928799..899d82b04bdd838db2cb23005c17cd38b548e2af 100644 (file)
@@ -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);
index 17ab0f953ecc41958823aac37a56c413982d0841..1f508d5a7632f361f732dd9ae5768a5e460509fd 100644 (file)
@@ -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);
index 1e2d8f30f23beb4aac1b195ce079fa6227f92fde..85d6886751225e6588f4950a96297bb6fb55e298 100644 (file)
@@ -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,