]> git.ipfire.org Git - pakfire.git/commitdiff
db: Store whether packages are installed by the user
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 23 Jun 2021 15:50:41 +0000 (15:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 23 Jun 2021 15:50:41 +0000 (15:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/db.c
src/libpakfire/include/pakfire/db.h
src/libpakfire/include/pakfire/transaction.h
src/libpakfire/request.c
src/libpakfire/transaction.c

index 17f0db68cc46c6be1de114ed1f24dc04d4df16b4..9ef7b7d3ca6d07538fc1b3d8d097b947c5e4e36c 100644 (file)
@@ -252,7 +252,7 @@ static int pakfire_db_create_schema(struct pakfire_db* db) {
                        "build_host     TEXT, "
                        "build_time     INTEGER, "
                        "installed      INTEGER, "
-                       "reason         TEXT, "
+                       "userinstalled  INTEGER, "
                        "repository     TEXT"
                ")");
        if (r)
@@ -995,7 +995,7 @@ END:
 }
 
 int pakfire_db_add_package(struct pakfire_db* db,
-               PakfirePackage pkg, PakfireArchive archive) {
+               PakfirePackage pkg, PakfireArchive archive, int userinstalled) {
        sqlite3_stmt* stmt = NULL;
        int r;
 
@@ -1006,8 +1006,8 @@ int pakfire_db_add_package(struct pakfire_db* db,
 
        const char* sql = "INSERT INTO packages(name, evr, arch, groups, filename, size, "
                "inst_size, hash1, license, summary, description, uuid, vendor, build_host, "
-               "build_time, installed, repository, reason) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, "
-               "?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
+               "build_time, installed, repository, userinstalled) "
+               "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
 
        // Prepare the statement
        r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
@@ -1179,10 +1179,12 @@ int pakfire_db_add_package(struct pakfire_db* db,
                        goto ROLLBACK;
        }
 
-       // XXX TODO Bind reason
-       r = sqlite3_bind_null(stmt, 17);
-       if (r)
+       // installed by the user?
+       r = sqlite3_bind_int(stmt, 17, userinstalled);
+       if (r) {
+               ERROR(db->pakfire, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db->handle));
                goto ROLLBACK;
+       }
 
        // Run query
        do {
index 60d8fac35b2f418dee06c3cf4d911543dfa7491d..4f3de1e9a3e771f06e95414869c2ab42706c5fbc 100644 (file)
@@ -44,7 +44,8 @@ int pakfire_db_check(struct pakfire_db* db);
 
 ssize_t pakfire_db_packages(struct pakfire_db* db);
 
-int pakfire_db_add_package(struct pakfire_db* db, PakfirePackage pkg, PakfireArchive archive);
+int pakfire_db_add_package(struct pakfire_db* db, PakfirePackage pkg,
+       PakfireArchive archive, int userinstalled);
 int pakfire_db_remove_package(struct pakfire_db* db, PakfirePackage pkg);
 
 int pakfire_db_load(struct pakfire_db* db, PakfireRepo repo);
index d437d7b080f34d825915ae21f014c777f1cb812b..da98ce23cfb969d587381d87626088a1cca62c71 100644 (file)
@@ -35,11 +35,13 @@ int pakfire_transaction_download(struct pakfire_transaction* transaction);
 
 #ifdef PAKFIRE_PRIVATE
 
+#include <solv/queue.h>
 #include <solv/transaction.h>
 
 #include <pakfire/types.h>
 
-int pakfire_transaction_create(struct pakfire_transaction** transaction, Pakfire pakfire, Transaction* trans);
+int pakfire_transaction_create(struct pakfire_transaction** transaction, Pakfire pakfire,
+       Transaction* trans, Queue* userinstalled);
 
 #endif
 
index 30455c44ee03025fa71c40c1c622222127c76ef4..c309509b1703129f474f65680e9a0b62d8b76943 100644 (file)
@@ -219,13 +219,28 @@ PAKFIRE_EXPORT PakfireProblem pakfire_request_get_problems(struct pakfire_reques
 }
 
 PAKFIRE_EXPORT struct pakfire_transaction* pakfire_request_get_transaction(struct pakfire_request* request) {
+       struct pakfire_transaction* transaction;
+       Queue userinstalled;
+
+       // Return nothing if no transaction was created
        if (!request->transaction)
                return NULL;
 
-       struct pakfire_transaction* transaction;
-       int r = pakfire_transaction_create(&transaction, request->pakfire, request->transaction);
-       if (r)
-               return NULL;
+       queue_init(&userinstalled);
+
+       // Fetch a list of all packages that are installed by the user
+       solver_get_userinstalled(request->solver, &userinstalled, GET_USERINSTALLED_NAMES);
+
+       // Create a new transaction
+       int r = pakfire_transaction_create(&transaction, request->pakfire,
+               request->transaction, &userinstalled);
+       if (r) {
+               transaction = NULL;
+               goto ERROR;
+       }
+
+ERROR:
+       queue_free(&userinstalled);
 
        return transaction;
 }
index f171ca3cdddb906d3768d887fc770836415c3460..e1fa25d3ec5ea7593e4393ebcbfa20e9d6f1e1dd 100644 (file)
@@ -43,6 +43,7 @@ struct pakfire_transaction {
        int nrefs;
 
        Transaction* transaction;
+       const char** userinstalled;
 
        PakfireArchive* archives;
        PakfirePackage* packages;
@@ -109,6 +110,8 @@ static void pakfire_transaction_free_archives_and_packages(
 static void pakfire_transaction_free(struct pakfire_transaction* transaction) {
        pakfire_transaction_free_archives_and_packages(transaction);
 
+       if (transaction->userinstalled)
+               free(transaction->userinstalled);
        transaction_free(transaction->transaction);
 
        pakfire_unref(transaction->pakfire);
@@ -145,8 +148,34 @@ static int pakfire_transaction_import_transaction(
        return 0;
 }
 
+static int pakfire_transaction_import_userinstalled(
+               struct pakfire_transaction* t, Queue* userinstalled) {
+       if (t->userinstalled) {
+               free(t->userinstalled);
+               t->userinstalled = NULL;
+       }
+
+       // Skip everything if the queue is empty
+       if (!userinstalled || !userinstalled->count)
+               return 0;
+
+       t->userinstalled = calloc(userinstalled->count, sizeof(*t->userinstalled));
+       if (!t->userinstalled) {
+               ERROR(t->pakfire, "Could not allocate userinstalled\n");
+               return 1;
+       }
+
+       Pool* pool = pakfire_get_solv_pool(t->pakfire);
+
+       // Store the names of all userinstalled packages
+       for (int i = 0; i < userinstalled->count; i++)
+               t->userinstalled[i] = pool_id2str(pool, userinstalled->elements[i]);
+
+       return 0;
+}
+
 int pakfire_transaction_create(struct pakfire_transaction** transaction,
-               Pakfire pakfire, Transaction* trans) {
+               Pakfire pakfire, Transaction* trans, Queue* userinstalled) {
        struct pakfire_transaction* t = calloc(1, sizeof(*t));
        if (!t)
                return ENOMEM;
@@ -167,6 +196,11 @@ int pakfire_transaction_create(struct pakfire_transaction** transaction,
        if (r)
                goto ERROR;
 
+       // Import userinstalled
+       r = pakfire_transaction_import_userinstalled(t, userinstalled);
+       if (r)
+               goto ERROR;
+
        *transaction = t;
        return 0;
 
@@ -533,6 +567,24 @@ static const char* pakfire_action_type_string(pakfire_action_type_t type) {
        return NULL;
 }
 
+static int pakfire_transaction_package_is_userinstalled(
+               struct pakfire_transaction* transaction, PakfirePackage pkg) {
+       // No packages on the list
+       if (!transaction->userinstalled)
+               return 0;
+
+       const char* name = pakfire_package_get_name(pkg);
+
+       // Check if the package is on the list
+       for (const char** elem = transaction->userinstalled; *elem; elem++) {
+               if (strcmp(name, *elem) == 0)
+                       return 1;
+       }
+
+       // Not found
+       return 0;
+}
+
 static int pakfire_transaction_run_step(struct pakfire_transaction* transaction,
                struct pakfire_db* db, const pakfire_action_type_t action, PakfirePackage pkg, PakfireArchive archive) {
        if (!pkg) {
@@ -625,7 +677,8 @@ static int pakfire_transaction_run_step(struct pakfire_transaction* transaction,
                                                        break;
                                        }
 
-                                       r = pakfire_db_add_package(db, pkg, archive);
+                                       r = pakfire_db_add_package(db, pkg, archive,
+                                                       pakfire_transaction_package_is_userinstalled(transaction, pkg));
                                        if (r)
                                                break;
 
@@ -644,7 +697,8 @@ static int pakfire_transaction_run_step(struct pakfire_transaction* transaction,
                                        if (r)
                                                break;
 
-                                       r = pakfire_db_add_package(db, pkg, archive);
+                                       r = pakfire_db_add_package(db, pkg, archive,
+                                                       pakfire_transaction_package_is_userinstalled(transaction, pkg));
                                        if (r)
                                                break;