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;
+}
static int pakfire_populate_pool(Pakfire pakfire) {
struct pakfire_db* db;
+ PakfireRepo repo = NULL;
int r;
// Initialize the pool
// 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) {