From a4c6f990d09231e2c9b618b21589fab3e030cab3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 11 Jan 2025 17:50:32 +0000 Subject: [PATCH] pakfire: Always require a configuration It is pretty pointless to run Pakfire without configuration and so we can keep the code a bit more tidy. Signed-off-by: Michael Tremer --- src/pakfire/pakfire.c | 12 +++++------- tests/libpakfire/main.c | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/pakfire/pakfire.c b/src/pakfire/pakfire.c index 76f442d8b..7b30b6ffa 100644 --- a/src/pakfire/pakfire.c +++ b/src/pakfire/pakfire.c @@ -806,6 +806,10 @@ int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx, struct pakfire* p = NULL; int r; + // Always require a configuration + if (!config) + return -EINVAL; + // Default to the native architecture if (!arch) arch = pakfire_arch_native(); @@ -842,13 +846,7 @@ int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx, p->flags = flags; // Store a reference to the configuration - if (config) { - p->config = pakfire_config_ref(config); - } else { - r = pakfire_config_create(&p->config); - if (r < 0) - goto ERROR; - } + p->config = pakfire_config_ref(config); // Store the nominal architecture r = pakfire_string_set(p->arches.nominal, arch); diff --git a/tests/libpakfire/main.c b/tests/libpakfire/main.c index f5b9c4fd4..8b9a7be64 100644 --- a/tests/libpakfire/main.c +++ b/tests/libpakfire/main.c @@ -35,25 +35,33 @@ FAIL: } static int test_fail(const struct test* t) { + struct pakfire_config* config = NULL; struct pakfire* pakfire = NULL; + int r = EXIT_FAILURE; + + // Create a configuration + ASSERT_SUCCESS(pakfire_config_create(&config)); // Init without anything - ASSERT(pakfire_create(&pakfire, t->ctx, NULL, NULL, NULL, 0) == -EINVAL); + ASSERT_ERROR(pakfire_create(&pakfire, t->ctx, NULL, NULL, NULL, 0), EINVAL); // Invalid architecture - ASSERT(pakfire_create(&pakfire, t->ctx, NULL, NULL, "arch", 0) == -ENOTSUP); + ASSERT_ERROR(pakfire_create(&pakfire, t->ctx, config, NULL, "arch", 0), ENOTSUP); // Invalid path (must be absolute) - ASSERT(pakfire_create(&pakfire, t->ctx, NULL, "path", NULL, 0) == -EINVAL); + ASSERT_ERROR(pakfire_create(&pakfire, t->ctx, config, "path", NULL, 0), EINVAL); // Cannot use snapshots with a path - ASSERT(pakfire_create(&pakfire, t->ctx, NULL, PAKFIRE_TMP_DIR "/test", - NULL, PAKFIRE_USE_SNAPSHOT) == -EINVAL); + ASSERT_ERROR(pakfire_create(&pakfire, t->ctx, NULL, PAKFIRE_TMP_DIR "/test", + NULL, PAKFIRE_USE_SNAPSHOT), EINVAL); - return EXIT_SUCCESS; + r = EXIT_SUCCESS; FAIL: - return EXIT_FAILURE; + if (config) + pakfire_config_unref(config); + + return r; } int main(int argc, const char* argv[]) { -- 2.47.3