]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Pass Pakfire to pakfire_create as argument
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 Jan 2021 14:10:06 +0000 (14:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 Jan 2021 14:10:06 +0000 (14:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/_pakfire/pakfire.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/pakfire.c
tests/libpakfire/key.c
tests/libpakfire/main.c
tests/testsuite.c
tests/testsuite.h

index 80789223cecfe5ea41db8cce3eab317451a94488..6e2575b491766bf3e9aefa2a13651430cf89bac5 100644 (file)
@@ -617,4 +617,4 @@ EXTRA_DIST += \
 
 .PHONY: clean-local-check
 clean-local-check:
-       -rm -rf $(TEST_ROOTFS)
+       -rm -rf $(TEST_ROOTFS)/*
index 88e1d9b1ae3eaf3012bf49d70e4953f918ee5d07..797051dce9c51430efb598d4ca101a690808c1cd 100644 (file)
@@ -53,9 +53,9 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                return -1;
 
        // Create a new Pakfire instance
-       self->pakfire = pakfire_create(path, arch);
-       if (!self->pakfire) {
-               switch (errno) {
+       int r = pakfire_create(&self->pakfire, path, arch);
+       if (r) {
+               switch (r) {
                        // Invalid architecture
                        case -EINVAL:
                                PyErr_SetString(PyExc_ValueError, "Invalid architecture or path");
index af4398a66f82724e8975508d3b7e8799a1366fd3..a975a73bab760c7e4cbe1d85d207d897d163eeae 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <pakfire/types.h>
 
-Pakfire pakfire_create(const char* path, const char* arch);
+int pakfire_create(Pakfire* pakfire, const char* path, const char* arch);
 
 Pakfire pakfire_ref(Pakfire pakfire);
 Pakfire pakfire_unref(Pakfire pakfire);
index fe5c8246c55abc35552895ed620b5f68720756de..f90199bcbd177141303a009ea04c824121cc8d00 100644 (file)
@@ -81,60 +81,60 @@ static int log_priority(const char* priority) {
        return 0;
 }
 
-PAKFIRE_EXPORT Pakfire pakfire_create(const char* path, const char* arch) {
+PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char* arch) {
        // Default to the native architecture
        if (!arch)
                arch = pakfire_arch_native();
 
        // Check if the architecture is supported
        if (!pakfire_arch_supported(arch)) {
-               errno = -EINVAL;
-               return NULL;
+               return -EINVAL;
        }
 
        // Path must be absolute
        if (!pakfire_string_startswith(path, "/")) {
-               errno = -EINVAL;
-               return NULL;
+               return -EINVAL;
        }
 
        // Check if path exists
        if (!pakfire_path_isdir(path)) {
-               errno = -ENOENT;
-               return NULL;
+               return -ENOENT;
        }
 
-       Pakfire pakfire = pakfire_calloc(1, sizeof(*pakfire));
-       if (pakfire) {
-               pakfire->nrefs = 1;
+       Pakfire p = pakfire_calloc(1, sizeof(*p) * 2);
+       if (!p)
+               return -ENOMEM;
 
-               pakfire->path = pakfire_strdup(path);
+       p->nrefs = 1;
 
-               // Set architecture
-               pakfire->arch = pakfire_strdup(arch);
+       p->path = pakfire_strdup(path);
 
-               // Setup logging
-               pakfire->log_function = pakfire_log_syslog;
+       // Set architecture
+       p->arch = pakfire_strdup(arch);
 
-               const char* env = secure_getenv("PAKFIRE_LOG");
-               if (env)
-                       pakfire_log_set_priority(pakfire, log_priority(env));
+       // Setup logging
+       p->log_function = pakfire_log_syslog;
 
-               DEBUG(pakfire, "Pakfire initialized at %p\n", pakfire);
-               DEBUG(pakfire, "  arch = %s\n", pakfire_get_arch(pakfire));
-               DEBUG(pakfire, "  path = %s\n", pakfire_get_path(pakfire));
+       const char* env = secure_getenv("PAKFIRE_LOG");
+       if (env)
+               pakfire_log_set_priority(p, log_priority(env));
 
-               // Initialize the pool
-               pakfire->pool = pool_create();
+       DEBUG(p, "Pakfire initialized at %p\n", p);
+       DEBUG(p, "  arch = %s\n", pakfire_get_arch(p));
+       DEBUG(p, "  path = %s\n", pakfire_get_path(p));
 
-               // Set architecture of the pool
-               pool_setarch(pakfire->pool, pakfire->arch);
+       // Initialize the pool
+       p->pool = pool_create();
 
-               // Initialise cache
-               pakfire_set_cache_path(pakfire, CACHE_PATH);
-       }
+       // Set architecture of the pool
+       pool_setarch(p->pool, p->arch);
 
-       return pakfire;
+       // Initialise cache
+       pakfire_set_cache_path(p, CACHE_PATH);
+
+       *pakfire = p;
+
+       return 0;
 }
 
 PAKFIRE_EXPORT Pakfire pakfire_ref(Pakfire pakfire) {
index 2b615ecc8ca8b1325f89535cc5da3dfab1de026f..d434f532be8a60671c11bf8c27769aaf50572f77 100644 (file)
@@ -45,7 +45,7 @@ static int test_init(const struct test* t) {
        return EXIT_SUCCESS;
 }
 
-static int test_import(const struct test* t) {
+static int test_import_export(const struct test* t) {
        // Try to delete the key just in case it
        // has been imported before
        PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT);
@@ -69,21 +69,13 @@ static int test_import(const struct test* t) {
        const char* fingerprint = pakfire_key_get_fingerprint(key);
        ASSERT(strcmp(fingerprint, TEST_KEY_FINGERPRINT) == 0);
 
-       pakfire_key_unref(key);
-
-       return EXIT_SUCCESS;
-}
-
-static int test_export(const struct test* t) {
-       PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT);
-       ASSERT(key);
-
        // Dump key description
        char* dump = pakfire_key_dump(key);
        ASSERT(dump);
        LOG("%s\n", dump);
        pakfire_free(dump);
 
+       // Export the key
        char* data = pakfire_key_export(key, 0);
        ASSERT(data);
 
@@ -97,8 +89,7 @@ static int test_export(const struct test* t) {
 
 int main(int argc, char** argv) {
        testsuite_add_test(test_init);
-       testsuite_add_test(test_import);
-       testsuite_add_test(test_export);
+       testsuite_add_test(test_import_export);
 
        return testsuite_run();
 }
index 001edf3ad966e82f27c2f561bae7d0ff4f5d7f65..ccb1fbdb870b8a3628f0dff380976701ddf5d115 100644 (file)
@@ -30,7 +30,7 @@ static int test_init(const struct test* t) {
 
 static int test_path(const struct test* t) {
        const char* path = pakfire_get_path(t->pakfire);
-       ASSERT_STRING_EQUALS(path, TEST_ROOTFS);
+       ASSERT_STRING_STARTSWITH(path, TEST_ROOTFS);
 
        return EXIT_SUCCESS;
 }
index e21d7625158eb7a4039badf2bdfa60fcc25898c8..7144dfd16859ae75109ee49d02caca43eacb07e2 100644 (file)
@@ -18,6 +18,9 @@
 #                                                                             #
 #############################################################################*/
 
+#include <linux/limits.h>
+#include <stdlib.h>
+
 #include "testsuite.h"
 
 #include <pakfire/logging.h>
@@ -28,9 +31,23 @@ const char* TEST_SRC_PATH = ABS_TOP_SRCDIR "/tests";
 struct testsuite ts;
 
 static int test_run(struct test* t) {
-       LOG("running %s\n", t->name);
+       char root[PATH_MAX];
+       int r;
+
+       // Create test root directory
+       snprintf(root, PATH_MAX - 1, "%s/pakfire-test-XXXXXX", TEST_ROOTFS);
+       char* tmp = mkdtemp(root);
+       ASSERT(root == tmp);
+
+       LOG("running %s (%s)\n", t->name, root);
+
+       // Create a pakfire instance
+       r = pakfire_create(&t->pakfire, root, NULL);
+       if (r) {
+               LOG("ERROR: Could not initialize Pakfire: %s\n", strerror(-r));
+               exit(1);
+       }
 
-       t->pakfire = pakfire_create(TEST_ROOTFS, NULL);
        ASSERT(t->pakfire);
 
        // Log to stderr
@@ -39,19 +56,22 @@ static int test_run(struct test* t) {
        // Enable debug logging
        pakfire_log_set_priority(t->pakfire, LOG_DEBUG);
 
-       int r = t->func(t);
+       r = t->func(t);
        if (r)
                LOG("Test failed with error code: %d\n", r);
 
        // Release pakfire
-       t->pakfire = pakfire_unref(t->pakfire);
+       Pakfire p = pakfire_unref(t->pakfire);
 
        // Check if Pakfire was actually released
-       if (t->pakfire) {
+       if (p) {
                LOG("Error: Pakfire instance was not released\n");
                return 1;
        }
 
+       // Cleanup root
+       // TODO
+
        return r;
 }
 
index 2723314ba092f6b6988ae354820e9f5c2dcc74cf..f48b438e5ae735f9b004c2d04ec7b6fd200a1b3b 100644 (file)
@@ -72,4 +72,13 @@ int testsuite_run();
                } \
        } while (0)
 
+#define ASSERT_STRING_STARTSWITH(string, start) \
+       do { \
+               if (strncmp(string, start, strlen(start)) != 0) { \
+                       LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \
+                               __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+                       return EXIT_FAILURE; \
+               } \
+       } while (0)
+
 #endif /* PAKFIRE_TESTSUITE_H */