From: Michael Tremer Date: Wed, 29 Jan 2025 10:40:57 +0000 (+0000) Subject: buildservice: Use the URL to upload the payload X-Git-Tag: 0.9.30~308 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e886c6617aec9c373ff7474d5a5f4d13e6814eef;p=pakfire.git buildservice: Use the URL to upload the payload This is just a change in the protocol which hopefully will give us some more flexibility in the future. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/buildservice.c b/src/pakfire/buildservice.c index 67e602a4..ed4a0b72 100644 --- a/src/pakfire/buildservice.c +++ b/src/pakfire/buildservice.c @@ -224,15 +224,55 @@ ERROR: return r; } +static int pakfire_buildservice_api_response_string(struct pakfire_buildservice* self, + struct json_object* response, const char* key, char** value) { + struct json_object* object = NULL; + const char* s = NULL; + char* buffer = NULL; + int r; + + // Fetch the object at key + r = json_object_object_get_ex(response, key, &object); + if (r == 0) { + ERROR(self->ctx, "Could not fetch '%s' from response\n", key); + r = -EBADMSG; + goto ERROR; + } + + // Read the string + s = json_object_get_string(object); + if (!s) { + ERROR(self->ctx, "Could not fetch value from '%s'\n", key); + r = -EBADMSG; + goto ERROR; + } + + // Copy the value + buffer = strdup(s); + if (!buffer) { + ERROR(self->ctx, "Could not copy the string: %m\n"); + r = -errno; + goto ERROR; + } + + // Return the value + *value = buffer; + return 0; + +ERROR: + if (buffer) + free(buffer); + + return r; +} + // Uploads static int pakfire_buildservice_create_upload(struct pakfire_buildservice* service, - const char* path, const char* filename, FILE* f, char** uuid) { + const char* path, const char* filename, FILE* f, char** url, char** uuid) { struct pakfire_xfer* xfer = NULL; struct pakfire_digests digests = {}; struct json_object* response = NULL; - struct json_object* id = NULL; - const char* __id = NULL; char* hexdigest = NULL; struct stat stat; int r; @@ -294,27 +334,18 @@ static int pakfire_buildservice_create_upload(struct pakfire_buildservice* servi if (r) goto ERROR; - // Fetch the ID - r = json_object_object_get_ex(response, "id", &id); - if (r == 0) { - ERROR(service->ctx, "Could not fetch ID from response\n"); - r = -EBADMSG; - goto ERROR; - } - - // Extract the UUID - __id = json_object_get_string(id); - if (!__id) { - ERROR(service->ctx, "Could not fetch ID from response\n"); - r = -EBADMSG; - goto ERROR; + // Fetch the URL + if (url) { + r = pakfire_buildservice_api_response_string(service, response, "url", url); + if (r < 0) + goto ERROR; } - // Return the UUID - *uuid = strdup(__id); - if (!*uuid) { - r = -errno; - goto ERROR; + // Fetch the ID + if (uuid) { + r = pakfire_buildservice_api_response_string(service, response, "id", uuid); + if (r < 0) + goto ERROR; } // Success @@ -332,12 +363,12 @@ ERROR: } static int pakfire_buildservice_upload_payload(struct pakfire_buildservice* service, - const char* filename, const char* uuid, FILE* f) { + const char* filename, const char* url, FILE* f) { struct pakfire_xfer* xfer = NULL; int r; // Create a new xfer - r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads/%s", uuid); + r = pakfire_buildservice_xfer_create(&xfer, service, "%s", url); if (r) goto ERROR; @@ -371,6 +402,7 @@ ERROR: int pakfire_buildservice_upload(struct pakfire_buildservice* service, const char* path, const char* filename, char** uuid) { char basename[NAME_MAX]; + char* url = NULL; FILE* f = NULL; int r; @@ -391,14 +423,14 @@ int pakfire_buildservice_upload(struct pakfire_buildservice* service, } // Create a new upload - r = pakfire_buildservice_create_upload(service, path, filename, f, uuid); + r = pakfire_buildservice_create_upload(service, path, filename, f, &url, uuid); if (r) goto ERROR; - DEBUG(service->ctx, "Created a new download (%s)\n", *uuid); + DEBUG(service->ctx, "Created a new upload (%s)\n", *uuid); // Send the payload - r = pakfire_buildservice_upload_payload(service, filename, *uuid, f); + r = pakfire_buildservice_upload_payload(service, filename, url, f); if (r) goto ERROR; @@ -409,6 +441,8 @@ ERROR: *uuid = NULL; } + if (url) + free(url); if (f) fclose(f);