From: Michael Tremer Date: Tue, 9 Feb 2021 19:05:09 +0000 (+0000) Subject: db: Load a couple of attributes for packages into repo X-Git-Tag: 0.9.28~1285^2~775 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=361ca45fb06a906f25f824dc303ffd05dbb3aba4;p=pakfire.git db: Load a couple of attributes for packages into repo Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 1d78a6b2d..bdbda89a1 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -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; +} diff --git a/src/libpakfire/include/pakfire/db.h b/src/libpakfire/include/pakfire/db.h index b10301929..e88645b4c 100644 --- a/src/libpakfire/include/pakfire/db.h +++ b/src/libpakfire/include/pakfire/db.h @@ -48,6 +48,8 @@ int pakfire_db_remove_package(struct pakfire_db* db, PakfirePackage pkg); #include +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); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 7ec15acf2..d614e7f8a 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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) {