From: Michael Tremer Date: Wed, 13 Jan 2021 14:10:06 +0000 (+0000) Subject: libpakfire: Pass Pakfire to pakfire_create as argument X-Git-Tag: 0.9.28~1285^2~874 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6dbda957130e14b3c2a7fe252ec132224bfdc488;p=pakfire.git libpakfire: Pass Pakfire to pakfire_create as argument Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 80789223c..6e2575b49 100644 --- a/Makefile.am +++ b/Makefile.am @@ -617,4 +617,4 @@ EXTRA_DIST += \ .PHONY: clean-local-check clean-local-check: - -rm -rf $(TEST_ROOTFS) + -rm -rf $(TEST_ROOTFS)/* diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 88e1d9b1a..797051dce 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -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"); diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index af4398a66..a975a73ba 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -28,7 +28,7 @@ #include -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); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index fe5c8246c..f90199bcb 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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) { diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 2b615ecc8..d434f532b 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -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(); } diff --git a/tests/libpakfire/main.c b/tests/libpakfire/main.c index 001edf3ad..ccb1fbdb8 100644 --- a/tests/libpakfire/main.c +++ b/tests/libpakfire/main.c @@ -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; } diff --git a/tests/testsuite.c b/tests/testsuite.c index e21d76251..7144dfd16 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -18,6 +18,9 @@ # # #############################################################################*/ +#include +#include + #include "testsuite.h" #include @@ -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; } diff --git a/tests/testsuite.h b/tests/testsuite.h index 2723314ba..f48b438e5 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -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 */