]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
libpakfire: db: Use SQLite3
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 20 Jan 2021 22:03:51 +0000 (22:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 20 Jan 2021 22:03:51 +0000 (22:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
configure.ac
src/libpakfire/db.c
src/libpakfire/pakfire.c

index 10df2c61c7aaa9917a718298c99fddb1e6a629b7..79c0d697ae6c77ddfcd4a7fae9ff4e107a21833f 100644 (file)
@@ -323,7 +323,8 @@ pkginclude_HEADERS += \
 libpakfire_la_CFLAGS = \
        $(AM_CFLAGS) \
        -fvisibility=hidden \
-       $(LIBGCRYPT_CFLAGS)
+       $(LIBGCRYPT_CFLAGS) \
+       $(SQLITE3_CFLAGS)
 
 libpakfire_la_CPPFLAGS = \
        $(AM_CPPFLAGS) \
@@ -342,7 +343,8 @@ libpakfire_la_LIBADD = \
        $(GPGME_LIBS) \
        $(LIBGCRYPT_LIBS) \
        $(LZMA_LIBS) \
-       $(SOLV_LIBS)
+       $(SOLV_LIBS) \
+       $(SQLITE3_LIBS)
 
 libpakfire_la_DEPENDENCIES = \
        src/libpakfire/libpakfire.sym \
index b0b5172e4da2e75b090fbaaeac2c89ea17bea446..6a0b97946e6120be55b42eb325e0f8d0a4d4f521 100644 (file)
@@ -175,6 +175,7 @@ LIBS="$save_LIBS"
 PKG_CHECK_MODULES([ARCHIVE], [libarchive >= 3.3.3])
 PKG_CHECK_MODULES([PYTHON_DEVEL], [python-${PYTHON_VERSION}])
 PKG_CHECK_MODULES([LZMA], [liblzma])
+PKG_CHECK_MODULES([SQLITE3], [sqlite3])
 
 AM_PATH_LIBGCRYPT([1.8.0], [], [AC_MSG_ERROR([*** libgcrypt not found])])
 AM_PATH_GPGME([1.6.0], [], [AC_MSG_ERROR([*** GPGME not found])])
index d40fb212d24a65483411762aab8bcbd1a913d6db..db27817a4b589ad18fb4fd4a6ee2a367f7eb4ef8 100644 (file)
@@ -21,6 +21,8 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include <sqlite3.h>
+
 #include <pakfire/db.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
@@ -35,9 +37,30 @@ struct pakfire_db {
        int nrefs;
 
        int mode;
+
+       sqlite3* handle;
 };
 
+static void pakfire_db_free(struct pakfire_db* db) {
+       DEBUG(db->pakfire, "Releasing database at %p\n", db);
+
+       // Close database handle
+       if (db->handle) {
+               int r = sqlite3_close(db->handle);
+               if (r != SQLITE_OK) {
+                       ERROR(db->pakfire, "Could not close database handle: %s\n",
+                               sqlite3_errmsg(db->handle));
+               }
+       }
+
+       pakfire_unref(db->pakfire);
+
+       pakfire_free(db);
+}
+
 PAKFIRE_EXPORT int pakfire_db_open(struct pakfire_db** db, Pakfire pakfire, int flags) {
+       int r = 1;
+
        struct pakfire_db* o = pakfire_calloc(1, sizeof(*o));
        if (!o)
                return -ENOMEM;
@@ -47,15 +70,43 @@ PAKFIRE_EXPORT int pakfire_db_open(struct pakfire_db** db, Pakfire pakfire, int
        o->pakfire = pakfire_ref(pakfire);
        o->nrefs = 1;
 
-       // Store mode
-       if (flags & PAKFIRE_DB_READWRITE)
+       int sqlite3_flags = 0;
+
+       // Store mode & forward it to sqlite3
+       if (flags & PAKFIRE_DB_READWRITE) {
                o->mode = PAKFIRE_DB_READWRITE;
-       else
+               sqlite3_flags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
+       } else {
                o->mode = PAKFIRE_DB_READONLY;
+               sqlite3_flags |= SQLITE_OPEN_READONLY;
+       }
+
+       // Make the filename
+       char* path = pakfire_make_path(o->pakfire, DATABASE_PATH);
+       if (!path)
+               goto END;
+
+       // Try to open the sqlite3 database file
+       r = sqlite3_open_v2(path, &o->handle, sqlite3_flags, NULL);
+       if (r != SQLITE_OK) {
+               ERROR(pakfire, "Could not open database %s: %s\n",
+                       path, sqlite3_errmsg(o->handle));
+
+               r = 1;
+               goto END;
+       }
 
        *db = o;
+       r = 0;
+
+END:
+       if (r)
+               pakfire_db_free(o);
 
-       return 0;
+       if (path)
+               free(path);
+
+       return r;
 }
 
 PAKFIRE_EXPORT struct pakfire_db* pakfire_db_ref(struct pakfire_db* db) {
@@ -64,14 +115,6 @@ PAKFIRE_EXPORT struct pakfire_db* pakfire_db_ref(struct pakfire_db* db) {
        return db;
 }
 
-static void pakfire_db_free(struct pakfire_db* db) {
-       DEBUG(db->pakfire, "Releasing database at %p\n", db);
-
-       pakfire_unref(db->pakfire);
-
-       pakfire_free(db);
-}
-
 PAKFIRE_EXPORT struct pakfire_db* pakfire_db_unref(struct pakfire_db* db) {
        if (--db->nrefs > 0)
                return db;
index 2a18021210828bf26ee889d6879912768ab26dd4..b822d8cfc2af9dfd7573c1068a5c7db1d2a8e395 100644 (file)
@@ -82,6 +82,23 @@ static int log_priority(const char* priority) {
        return 0;
 }
 
+static int pakfire_populate_pool(Pakfire pakfire) {
+       struct pakfire_db* db;
+       int r;
+
+       // Open database in read-only mode and try to load all installed packages
+       r = pakfire_db_open(&db, pakfire, PAKFIRE_DB_READWRITE);
+       if (r)
+               return r;
+
+       // TODO
+
+       // Free database
+       pakfire_db_unref(db);
+
+       return 0;
+}
+
 // A utility function is already called pakfire_free
 static void _pakfire_free(Pakfire pakfire) {
        DEBUG(pakfire, "Releasing Pakfire at %p\n", pakfire);
@@ -172,6 +189,14 @@ PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char
        // Set architecture of the pool
        pool_setarch(p->pool, p->arch);
 
+       // Populate pool
+       r = pakfire_populate_pool(p);
+       if (r) {
+               _pakfire_free(p);
+
+               return r;
+       }
+
        // Initialise cache
        pakfire_set_cache_path(p, CACHE_PATH);