--- /dev/null
+/*#############################################################################
+# #
+# Pakfire - The IPFire package management system #
+# Copyright (C) 2023 Pakfire development team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#include <argp.h>
+
+#include <pakfire/buildservice.h>
+#include <pakfire/ctx.h>
+
+#include "command.h"
+#include "upload_create.h"
+
+static const char* args_doc = "FILES...";
+
+static const char* doc = "Uploads a file";
+
+#define MAX_FILES 32
+
+struct config {
+ const char* files[MAX_FILES];
+ unsigned int num_files;
+};
+
+static error_t parse(int key, char* arg, void* data) {
+ struct config* config = data;
+
+ switch (key) {
+ case ARGP_KEY_ARG:
+ if (config->num_files >= MAX_FILES)
+ return -ENOBUFS;
+
+ config->files[config->num_files++] = arg;
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
+
+int cli_upload_create(void* data, int argc, char* argv[]) {
+ struct pakfire_buildservice* service = NULL;
+ struct config config = {};
+ int r;
+
+ struct pakfire_ctx* ctx = data;
+
+ // Parse the command line
+ r = cli_parse(NULL, NULL, args_doc, doc, parse, argc, argv, &config);
+ if (r)
+ goto ERROR;
+
+ // Connect to the build service
+ r = pakfire_buildservice_create(&service, ctx);
+ if (r)
+ goto ERROR;
+
+ // List uploads
+ for (unsigned int i = 0; i < config.num_files; i++) {
+ r = pakfire_buildservice_upload(service, config.files[i], NULL);
+ if (r)
+ goto ERROR;
+ }
+
+ERROR:
+ if (service)
+ pakfire_buildservice_unref(service);
+
+ return r;
+}
--- /dev/null
+/*#############################################################################
+# #
+# Pakfire - The IPFire package management system #
+# Copyright (C) 2023 Pakfire development team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_UPLOAD_CREATE_H
+#define PAKFIRE_CLI_UPLOAD_CREATE_H
+
+int cli_upload_create(void* data, int argc, char* argv[]);
+
+#endif /* PAKFIRE_CLI_UPLOAD_CREATE_H */
#include <pakfire/buildservice.h>
#include <pakfire/config.h>
#include <pakfire/ctx.h>
+#include <pakfire/digest.h>
#include <pakfire/downloader.h>
#include <pakfire/logging.h>
+#include <pakfire/path.h>
#include <pakfire/private.h>
#include <pakfire/string.h>
#include <pakfire/util.h>
// Uploads
+static int pakfire_buildservice_create_upload(struct pakfire_buildservice* service,
+ const char* path, const char* filename, FILE* f, char** uuid) {
+ struct pakfire_transfer* transfer = NULL;
+ struct json_object* response = NULL;
+ struct json_object* id = NULL;
+ struct pakfire_digests digests;
+ const char* __id = NULL;
+ char* hexdigest = NULL;
+ char* buffer = NULL;
+ size_t length = 0;
+ struct stat stat;
+ int r;
+
+ const int fd = fileno(f);
+
+ // Stat the file
+ r = fstat(fd, &stat);
+ if (r) {
+ CTX_ERROR(service->ctx, "Could not stat %s: %s\n", path, strerror(errno));
+ goto ERROR;
+ }
+
+ // Compute the digest
+ r = pakfire_digests_compute_from_file(service->ctx, &digests, PAKFIRE_DIGEST_BLAKE2B512, f);
+ if (r) {
+ CTX_ERROR(service->ctx, "Could not compute the digest of %s: %s\n",
+ path, strerror(-r));
+ goto ERROR;
+ }
+
+ // Convert the digest into hex format
+ hexdigest = pakfire_digest_get_hex(&digests, PAKFIRE_DIGEST_BLAKE2B512);
+ if (!hexdigest)
+ goto ERROR;
+
+ // Create a new transfer
+ r = pakfire_buildservice_create_transfer(&transfer, service, "/api/v1/uploads");
+ if (r)
+ goto ERROR;
+
+ // Enable authentication
+ r = pakfire_downloader_transfer_auth(transfer);
+ if (r)
+ goto ERROR;
+
+ // Add the filename parameter
+ r = pakfire_downloader_transfer_add_param(transfer, "filename", "%s", filename);
+ if (r)
+ goto ERROR;
+
+ // Add the size parameter
+ r = pakfire_downloader_transfer_add_param(transfer, "size", "%jd", stat.st_size);
+ if (r)
+ goto ERROR;
+
+ // Add the hexdigest algo parameter
+ r = pakfire_downloader_transfer_add_param(transfer, "hexdigest_algo", "%s", "blake2b512");
+ if (r)
+ goto ERROR;
+
+ // Add the hexdigest parameter
+ r = pakfire_downloader_transfer_add_param(transfer, "hexdigest", "%s", hexdigest);
+ if (r)
+ goto ERROR;
+
+ // Write the response to the buffer
+ r = pakfire_downloader_transfer_set_output_buffer(transfer, &buffer, &length);
+ if (r)
+ goto ERROR;
+
+ // Run the transfer
+ r = pakfire_downloader_transfer_run(transfer, PAKFIRE_TRANSFER_NO_PROGRESS);
+ if (r)
+ goto ERROR;
+
+ // Parse the JSON response
+ response = pakfire_json_parse(service->ctx, buffer, length);
+ if (!response) {
+ CTX_ERROR(service->ctx, "Could not parse response\n");
+ r = -EBADMSG;
+ goto ERROR;
+ }
+
+ // Fetch the ID
+ r = json_object_object_get_ex(response, "id", &id);
+ if (r == 0) {
+ CTX_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) {
+ CTX_ERROR(service->ctx, "Could not fetch ID from response\n");
+ r = -EBADMSG;
+ goto ERROR;
+ }
+
+ // Return the UUID
+ *uuid = strdup(__id);
+ if (!*uuid) {
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Success
+ r = 0;
+
+ERROR:
+ if (transfer)
+ pakfire_downloader_transfer_unref(transfer);
+ if (response)
+ json_object_put(response);
+ if (hexdigest)
+ free(hexdigest);
+ if (buffer)
+ free(buffer);
+
+ return r;
+}
+
+static int pakfire_buildservice_upload_payload(struct pakfire_buildservice* service,
+ const char* filename, const char* uuid, FILE* f) {
+ struct pakfire_transfer* transfer = NULL;
+ char url[PATH_MAX];
+ int r;
+
+ // Make the URL
+ r = pakfire_string_format(url, "/api/v1/uploads/%s", uuid);
+ if (r)
+ goto ERROR;
+
+ // Create a new transfer
+ r = pakfire_buildservice_create_transfer(&transfer, service, url);
+ if (r)
+ goto ERROR;
+
+ // Set the title
+ r = pakfire_downloader_transfer_set_title(transfer, filename);
+ if (r)
+ goto ERROR;
+
+ // Set source file
+ r = pakfire_downloader_transfer_set_input(transfer, f);
+ if (r)
+ goto ERROR;
+
+ // Run the transfer
+ r = pakfire_downloader_transfer_run(transfer, 0);
+ if (r)
+ goto ERROR;
+
+ERROR:
+ if (transfer)
+ pakfire_downloader_transfer_unref(transfer);
+
+ return r;
+}
+
+PAKFIRE_EXPORT int pakfire_buildservice_upload(struct pakfire_buildservice* service,
+ const char* path, const char* filename) {
+ char basename[NAME_MAX];
+ char* uuid = NULL;
+ FILE* f = NULL;
+ int r;
+
+ // Compute the basename
+ r = pakfire_path_basename(basename, path);
+ if (r)
+ goto ERROR;
+
+ // Set the basename as default filename
+ if (!filename)
+ filename = basename;
+
+ // Open the source file
+ f = fopen(path, "r");
+ if (!f) {
+ CTX_ERROR(service->ctx, "Could not open file for upload %s: %m\n", path);
+ return -errno;
+ }
+
+ // Create a new upload
+ r = pakfire_buildservice_create_upload(service, path, filename, f, &uuid);
+ if (r)
+ goto ERROR;
+
+ CTX_DEBUG(service->ctx, "Created a new download (%s)\n", uuid);
+
+ // Send the payload
+ r = pakfire_buildservice_upload_payload(service, filename, uuid, f);
+ if (r)
+ goto ERROR;
+
+ERROR:
+ if (uuid)
+ free(uuid);
+ if (f)
+ fclose(f);
+
+ return r;
+}
+
PAKFIRE_EXPORT int pakfire_buildservice_list_uploads(
struct pakfire_buildservice* service, struct json_object** p) {
struct pakfire_transfer* transfer = NULL;