]> git.ipfire.org Git - pakfire.git/commitdiff
db: Load a couple of attributes for packages into repo
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Feb 2021 19:05:09 +0000 (19:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Feb 2021 19:16:46 +0000 (19:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/db.c
src/libpakfire/include/pakfire/db.h
src/libpakfire/pakfire.c

index 1d78a6b2de4d35ec674ccabc2c007528e0eb5143..bdbda89a1ff6d078fe0456c003e08ffe44d7939b 100644 (file)
@@ -1349,3 +1349,137 @@ ERROR:
 
        return scriptlet;
 }
+
+static int pakfire_db_load_package(struct pakfire_db* db, PakfireRepo repo, sqlite3_stmt* stmt) {
+       PakfirePackage pkg = NULL;
+       char* evr = NULL;
+       int r = 1;
+
+       // Name
+       const char* name = (const char*)sqlite3_column_text(stmt, 0);
+       if (!name) {
+               ERROR(db->pakfire, "Could not read name: %s\n", sqlite3_errmsg(db->handle));
+               goto ERROR;
+       }
+
+       // Epoch (INTEGER but cast as string)
+       const char* epoch = (const char*)sqlite3_column_text(stmt, 1);
+       if (!epoch) {
+               ERROR(db->pakfire, "Could not read epoch: %s\n", sqlite3_errmsg(db->handle));
+               goto ERROR;
+       }
+
+       // Version
+       const char* version = (const char*)sqlite3_column_text(stmt, 2);
+       if (!version) {
+               ERROR(db->pakfire, "Could not read version: %s\n", sqlite3_errmsg(db->handle));
+               goto ERROR;
+       }
+
+       // Release
+       const char* release = (const char*)sqlite3_column_text(stmt, 3);
+       if (!release) {
+               ERROR(db->pakfire, "Could not read release: %s\n", sqlite3_errmsg(db->handle));
+               goto ERROR;
+       }
+
+       // Arch
+       const char* arch = (const char*)sqlite3_column_text(stmt, 4);
+       if (!arch) {
+               ERROR(db->pakfire, "Could not read arch: %s\n", sqlite3_errmsg(db->handle));
+               goto ERROR;
+       }
+
+       // Join EVR together
+       evr = pakfire_package_join_evr(epoch, version, release);
+       if (!evr) {
+               ERROR(db->pakfire, "Could not join evr\n");
+               goto ERROR;
+       }
+
+       // Create package
+       pkg = pakfire_package_create2(db->pakfire, repo, name, evr, arch);
+       if (!pkg) {
+               ERROR(db->pakfire, "Could not create package\n");
+               goto ERROR;
+       }
+
+       // Success
+       r = 0;
+
+ERROR:
+       if (evr)
+               free(evr);
+
+       if (pkg)
+               pakfire_package_unref(pkg);
+
+       return r;
+}
+
+int pakfire_db_load(struct pakfire_db* db, PakfireRepo repo) {
+       sqlite3_stmt* stmt = NULL;
+       int r = 1;
+
+       DEBUG(db->pakfire, "Loading package database...\n");
+
+       const char* sql =
+               "SELECT "
+                       "name, "
+                       "epoch, "
+                       "version, "
+                       "release, "
+                       "arch "
+               "FROM "
+                       "packages"
+               ";";
+
+       // Prepare the statement
+       r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
+       if (r) {
+               ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
+                       sql, sqlite3_errmsg(db->handle));
+               goto ERROR;
+       }
+
+       for (;;) {
+               // Execute query
+               r = sqlite3_step(stmt);
+
+               switch (r) {
+                       // Retry if the database was busy
+                       case SQLITE_BUSY:
+                               continue;
+
+                       // Read a row
+                       case SQLITE_ROW:
+                               r = pakfire_db_load_package(db, repo, stmt);
+                               if (r)
+                                       goto ERROR;
+                               break;
+
+                       // All rows have been processed
+                       case SQLITE_DONE:
+                               goto END;
+
+                       // Go to error in any other cases
+                       default:
+                               goto ERROR;
+               }
+       }
+
+END:
+       DEBUG(db->pakfire, "Loading package database completed\n");
+
+       // All done
+       r = 0;
+
+ERROR:
+       if (r)
+               ERROR(db->pakfire, "Failed reading package database: %d\n", r);
+
+       if (stmt)
+               sqlite3_finalize(stmt);
+
+       return r;
+}
index b10301929f8e4ec2949870d909cde44c16947c09..e88645b4c354e2a878033dc1d56194c38bddbbd3 100644 (file)
@@ -48,6 +48,8 @@ int pakfire_db_remove_package(struct pakfire_db* db, PakfirePackage pkg);
 
 #include <pakfire/scriptlet.h>
 
+int pakfire_db_load(struct pakfire_db* db, PakfireRepo repo);
+
 struct pakfire_scriptlet* pakfire_db_get_scriptlet(
        struct pakfire_db* db, PakfirePackage pkg, pakfire_scriptlet_type type);
 
index 7ec15acf2f6a5fee7ea15b82bc58b733cc023602..d614e7f8a8f5ee75bb93845bbd9bf6fcd145f1c2 100644 (file)
@@ -84,6 +84,7 @@ static int log_priority(const char* priority) {
 
 static int pakfire_populate_pool(Pakfire pakfire) {
        struct pakfire_db* db;
+       PakfireRepo repo = NULL;
        int r;
 
        // Initialize the pool
@@ -100,21 +101,30 @@ static int pakfire_populate_pool(Pakfire pakfire) {
        // 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;
+               goto ERROR;
 
        // Create the system repository
-       PakfireRepo repo = pakfire_repo_create(pakfire, "@local");
+       repo = pakfire_repo_create(pakfire, "@local");
        if (!repo)
-               return 1;
+               goto ERROR;
 
        // Set this repository as the installed one
        pakfire_set_installed_repo(pakfire, repo);
 
-       // Free database
-       pakfire_db_unref(db);
-       pakfire_repo_unref(repo);
+       // Load database content
+       r = pakfire_db_load(db, repo);
+       if (r)
+               goto ERROR;
 
-       return 0;
+ERROR:
+       // Cleanup
+       if (db)
+               pakfire_db_unref(db);
+
+       if (repo)
+               pakfire_repo_unref(repo);
+
+       return r;
 }
 
 static void pakfire_free(Pakfire pakfire) {