1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2021 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <linux/limits.h>
26 #include <openssl/sha.h>
27 #include <solv/solver.h>
30 #include <pakfire/archive.h>
31 #include <pakfire/db.h>
32 #include <pakfire/dependencies.h>
33 #include <pakfire/digest.h>
34 #include <pakfire/file.h>
35 #include <pakfire/filelist.h>
36 #include <pakfire/logging.h>
37 #include <pakfire/package.h>
38 #include <pakfire/pakfire.h>
39 #include <pakfire/repo.h>
40 #include <pakfire/string.h>
41 #include <pakfire/util.h>
43 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
45 #define CURRENT_SCHEMA 8
46 #define SCHEMA_MIN_SUP 7
49 struct pakfire
* pakfire
;
57 time_t last_modified_at
;
60 static void logging_callback(void* data
, int r
, const char* msg
) {
61 struct pakfire
* pakfire
= (struct pakfire
*)data
;
63 ERROR(pakfire
, "Database Error: %s: %s\n",
64 sqlite3_errstr(r
), msg
);
67 static int pakfire_db_check_table(struct pakfire_db
* db
, const char* table
) {
68 sqlite3_stmt
* stmt
= NULL
;
71 const char* sql
= "SELECT * FROM sqlite_master WHERE type = ? AND name = ?";
73 // Prepare the statement
74 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
76 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
77 sql
, sqlite3_errmsg(db
->handle
));
82 r
= sqlite3_bind_text(stmt
, 1, "table", -1, NULL
);
84 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
89 r
= sqlite3_bind_text(stmt
, 2, table
, -1, NULL
);
91 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
95 // Execute the statement
97 r
= sqlite3_step(stmt
);
98 } while (r
== SQLITE_BUSY
);
100 // We should have read a row
104 // If we get here, the table exists.
109 sqlite3_finalize(stmt
);
114 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
115 sqlite3_stmt
* stmt
= NULL
;
116 sqlite3_value
* val
= NULL
;
119 // If the schema has not been initialized, yet, check if the settings table exist, at all
121 r
= pakfire_db_check_table(db
, "settings");
126 const char* sql
= "SELECT val FROM settings WHERE key = ?";
128 // Prepare the statement
129 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
130 if (r
!= SQLITE_OK
) {
131 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
132 // sql, sqlite3_errmsg(db->handle));
137 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
138 if (r
!= SQLITE_OK
) {
139 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
143 // Execute the statement
145 r
= sqlite3_step(stmt
);
146 } while (r
== SQLITE_BUSY
);
148 // We should have read a row
153 val
= sqlite3_column_value(stmt
, 0);
155 ERROR(db
->pakfire
, "Could not read value\n");
159 // Copy value onto the heap
160 val
= sqlite3_value_dup(val
);
164 sqlite3_finalize(stmt
);
169 static char* pakfire_db_get_string(struct pakfire_db
* db
, const char* key
) {
172 // Fetch the value from the database
173 sqlite3_value
* value
= pakfire_db_get(db
, key
);
177 // Extract the value as string
178 const char* p
= (const char*)sqlite3_value_text(value
);
182 // Copy string to heap
187 sqlite3_value_free(value
);
192 static int pakfire_db_set_string(struct pakfire_db
* db
, const char* key
, const char* val
) {
193 sqlite3_stmt
* stmt
= NULL
;
196 DEBUG(db
->pakfire
, "Setting %s to '%s'\n", key
, val
);
198 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
199 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
202 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
203 if (r
!= SQLITE_OK
) {
204 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
205 sql
, sqlite3_errmsg(db
->handle
));
210 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
211 if (r
!= SQLITE_OK
) {
212 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
217 r
= sqlite3_bind_text(stmt
, 2, val
, strlen(val
), NULL
);
218 if (r
!= SQLITE_OK
) {
219 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
223 // Execute the statement
225 r
= sqlite3_step(stmt
);
226 } while (r
== SQLITE_BUSY
);
229 r
= (r
== SQLITE_OK
);
233 sqlite3_finalize(stmt
);
238 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
239 sqlite3_stmt
* stmt
= NULL
;
242 DEBUG(db
->pakfire
, "Setting %s to '%d'\n", key
, val
);
244 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
245 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
248 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
249 if (r
!= SQLITE_OK
) {
250 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
251 sql
, sqlite3_errmsg(db
->handle
));
256 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
257 if (r
!= SQLITE_OK
) {
258 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
263 r
= sqlite3_bind_int64(stmt
, 2, val
);
264 if (r
!= SQLITE_OK
) {
265 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
269 // Execute the statement
271 r
= sqlite3_step(stmt
);
272 } while (r
== SQLITE_BUSY
);
275 r
= (r
== SQLITE_OK
);
279 sqlite3_finalize(stmt
);
284 static time_t pakfire_read_modification_time(struct pakfire_db
* db
) {
287 // Fetch the value from the database
288 sqlite3_value
* value
= pakfire_db_get(db
, "last_modified_at");
290 t
= sqlite3_value_int64(value
);
291 sqlite3_value_free(value
);
293 DEBUG(db
->pakfire
, "Could not find last modification timestamp\n");
299 static int pakfire_update_modification_time(struct pakfire_db
* db
) {
300 // Get the current time in UTC
301 time_t t
= time(NULL
);
303 // Store it in the database
304 int r
= pakfire_db_set_int(db
, "last_modified_at", t
);
308 // Update the last modification timestamp
309 db
->last_modified_at
= t
;
314 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
317 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
320 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
321 } while (r
== SQLITE_BUSY
);
325 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
331 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
332 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
335 static int pakfire_db_commit(struct pakfire_db
* db
) {
337 If the database was opened in read-write mode, we will store the
338 timestamp of the latest modification to compare whether the database
339 has been changed mid-transaction.
341 if (db
->mode
== PAKFIRE_DB_READWRITE
) {
342 int r
= pakfire_update_modification_time(db
);
347 return pakfire_db_execute(db
, "COMMIT");
350 static int pakfire_db_rollback(struct pakfire_db
* db
) {
351 return pakfire_db_execute(db
, "ROLLBACK");
355 This function performs any fast optimization and tries to truncate the WAL log file
356 to keep the database as compact as possible on disk.
358 static void pakfire_db_optimize(struct pakfire_db
* db
) {
359 pakfire_db_execute(db
, "PRAGMA optimize");
360 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
363 static void pakfire_db_free(struct pakfire_db
* db
) {
365 // Optimize the database before it is being closed
366 pakfire_db_optimize(db
);
368 // Close database handle
369 int r
= sqlite3_close(db
->handle
);
370 if (r
!= SQLITE_OK
) {
371 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
372 sqlite3_errmsg(db
->handle
));
376 pakfire_unref(db
->pakfire
);
381 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
382 // Fetch the schema version
383 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
387 int schema
= sqlite3_value_int64(value
);
388 sqlite3_value_free(value
);
390 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
395 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
398 // Create settings table
399 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
403 // settings: Add a unique index on key
404 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
408 // Create packages table
409 r
= pakfire_db_execute(db
,
410 "CREATE TABLE IF NOT EXISTS packages("
411 "id INTEGER PRIMARY KEY, "
418 "inst_size INTEGER, "
419 "digest_type INTEGER, "
427 "build_time INTEGER, "
428 "installed INTEGER, "
429 "userinstalled INTEGER, "
439 // packages: Create index to find package by name
440 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
444 // packages: Create unique index over UUID
445 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
449 // Create dependencies table
450 r
= pakfire_db_execute(db
,
451 "CREATE TABLE IF NOT EXISTS dependencies("
455 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
460 // dependencies: Add index over packages
461 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
465 // Create files table
466 r
= pakfire_db_execute(db
,
467 "CREATE TABLE IF NOT EXISTS files("
468 "id INTEGER PRIMARY KEY, "
480 "capabilities TEXT, "
481 "digest_sha2_512 BLOB, "
482 "digest_sha2_256 BLOB, "
483 "digest_blake2b512 BLOB, "
484 "digest_blake2s256 BLOB, "
485 "digest_sha3_512 BLOB, "
486 "digest_sha3_256 BLOB, "
487 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
492 // files: Add index over packages
493 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
497 // files: Add index over path
498 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
502 // Create scriptlets table
503 r
= pakfire_db_execute(db
,
504 "CREATE TABLE IF NOT EXISTS scriptlets("
505 "id INTEGER PRIMARY KEY, "
509 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
514 // scriptlets: Add index over packages
515 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
519 const char* arch
= pakfire_get_effective_arch(db
->pakfire
);
522 r
= pakfire_db_set_string(db
, "arch", arch
);
524 ERROR(db
->pakfire
, "Could not set architecture\n");
531 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
532 // packages: Drop build_id column
535 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
536 // need to recreate the whole table and rename it afterwards. Annoying.
541 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
544 while (db
->schema
< CURRENT_SCHEMA
) {
545 // Begin a new transaction
546 r
= pakfire_db_begin_transaction(db
);
550 switch (db
->schema
) {
553 r
= pakfire_db_create_schema(db
);
557 db
->schema
= CURRENT_SCHEMA
;
561 r
= pakfire_db_migrate_to_schema_8(db
);
569 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
573 // Update the schema version
574 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
579 r
= pakfire_db_commit(db
);
587 pakfire_db_rollback(db
);
592 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
595 // Fetch database architecture
596 char* db_arch
= pakfire_db_get_string(db
, "arch");
598 ERROR(db
->pakfire
, "Database is of an unknown architecture\n");
602 // Fetch the running architecture
603 const char* arch
= pakfire_get_effective_arch(db
->pakfire
);
608 if (strcmp(db_arch
, arch
) == 0)
618 static int pakfire_db_setup(struct pakfire_db
* db
) {
622 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
624 // Enable foreign keys
625 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
627 // Make LIKE case-sensitive
628 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
630 // Fetch the current schema
631 db
->schema
= pakfire_db_get_schema(db
);
633 // Check if the schema is recent enough
634 if (db
->schema
&& db
->schema
< SCHEMA_MIN_SUP
) {
635 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
640 // Read modification timestamp
641 db
->last_modified_at
= pakfire_read_modification_time(db
);
643 DEBUG(db
->pakfire
, "The database was last modified at %ld\n", db
->last_modified_at
);
645 // Done when not in read-write mode
646 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
649 // Disable secure delete
650 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
652 // Set database journal to WAL
653 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
654 if (r
!= SQLITE_OK
) {
655 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
656 sqlite3_errmsg(db
->handle
));
660 // Disable autocheckpoint
661 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
662 if (r
!= SQLITE_OK
) {
663 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
664 sqlite3_errmsg(db
->handle
));
668 // Create or migrate schema
669 r
= pakfire_db_migrate_schema(db
);
676 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
679 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
683 o
->pakfire
= pakfire_ref(pakfire
);
686 int sqlite3_flags
= 0;
688 // Store mode & forward it to sqlite3
689 if (flags
& PAKFIRE_DB_READWRITE
) {
690 o
->mode
= PAKFIRE_DB_READWRITE
;
691 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
693 o
->mode
= PAKFIRE_DB_READONLY
;
694 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
698 r
= pakfire_path(o
->pakfire
, o
->path
, "%s", DATABASE_PATH
);
702 // Try to open the sqlite3 database file
703 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
704 if (r
!= SQLITE_OK
) {
705 ERROR(pakfire
, "Could not open database %s: %s\n",
706 o
->path
, sqlite3_errmsg(o
->handle
));
712 // Setup the database
713 r
= pakfire_db_setup(o
);
717 // Check for compatible architecture
718 r
= pakfire_db_check_arch(o
);
732 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
738 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
747 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
748 sqlite3_stmt
* stmt
= NULL
;
751 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
753 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
754 sqlite3_errmsg(db
->handle
));
759 unsigned long errors
= 0;
763 r
= sqlite3_step(stmt
);
764 } while (r
== SQLITE_BUSY
);
766 if (r
== SQLITE_ROW
) {
767 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
769 // If the message is "ok", the database has passed the check
770 if (strcmp(error
, "ok") == 0)
773 // Increment error counter
777 ERROR(db
->pakfire
, "%s\n", error
);
779 // Break on anything else
784 sqlite3_finalize(stmt
);
787 ERROR(db
->pakfire
, "Database integrity check failed\n");
789 INFO(db
->pakfire
, "Database integrity check passed\n");
794 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
795 sqlite3_stmt
* stmt
= NULL
;
798 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
800 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
801 sqlite3_errmsg(db
->handle
));
806 unsigned long errors
= 0;
810 r
= sqlite3_step(stmt
);
811 } while (r
== SQLITE_BUSY
);
813 if (r
== SQLITE_ROW
) {
814 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
815 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
816 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
817 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
819 // Increment error counter
823 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
824 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
826 // Break on anything else
831 sqlite3_finalize(stmt
);
834 ERROR(db
->pakfire
, "Foreign key check failed\n");
836 INFO(db
->pakfire
, "Foreign key check passed\n");
842 This function performs an integrity check of the database
844 int pakfire_db_check(struct pakfire_db
* db
) {
847 // Perform integrity check
848 r
= pakfire_db_integrity_check(db
);
852 // Perform foreign key check
853 r
= pakfire_db_foreign_key_check(db
);
860 // Returns the number of packages installed
861 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
862 sqlite3_stmt
* stmt
= NULL
;
863 ssize_t packages
= -1;
865 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
867 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
868 sqlite3_errmsg(db
->handle
));
874 r
= sqlite3_step(stmt
);
875 } while (r
== SQLITE_BUSY
);
877 if (r
== SQLITE_ROW
) {
878 packages
= sqlite3_column_int64(stmt
, 0);
881 sqlite3_finalize(stmt
);
886 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
887 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
889 // Convert name to ID
890 Id id
= pool_str2id(pool
, name
, 1);
892 // Append it to pooljobs
893 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
896 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
897 sqlite3_stmt
* stmt
= NULL
;
901 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
903 // Prepare the statement
904 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
906 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
907 sql
, sqlite3_errmsg(db
->handle
));
911 for (const struct pakfire_dep
* dep
= pakfire_deps
; dep
->key
; dep
++) {
912 list
= pakfire_package_get_deps(pkg
, dep
->key
);
918 for (char** d
= list
; *d
; d
++) {
920 r
= sqlite3_bind_int64(stmt
, 1, id
);
922 ERROR(db
->pakfire
, "Could not bind id: %s\n",
923 sqlite3_errmsg(db
->handle
));
928 r
= sqlite3_bind_text(stmt
, 2, dep
->name
, -1, NULL
);
930 ERROR(db
->pakfire
, "Could not bind type: %s\n",
931 sqlite3_errmsg(db
->handle
));
936 r
= sqlite3_bind_text(stmt
, 3, *d
, -1, NULL
);
938 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
939 sqlite3_errmsg(db
->handle
));
945 r
= sqlite3_step(stmt
);
946 } while (r
== SQLITE_BUSY
);
948 // Reset bound values
958 sqlite3_finalize(stmt
);
960 for (char** dep
= list
; *dep
; dep
++)
968 static int pakfire_db_bind_digest(struct pakfire_db
* db
, sqlite3_stmt
* stmt
, const int field
,
969 struct pakfire_file
* file
, const enum pakfire_digest_types type
) {
970 const unsigned char* digest
= NULL
;
974 digest
= pakfire_file_get_digest(file
, type
, &length
);
976 // If this digest isn't set, just bind NULL
978 return sqlite3_bind_null(stmt
, field
);
980 // Otherwise bind the data blob
981 return sqlite3_bind_blob(stmt
, field
, digest
, length
, NULL
);
984 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
985 sqlite3_stmt
* stmt
= NULL
;
988 // Get the filelist from the archive
989 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
991 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
995 // Nothing to do if the list is empty
996 if (pakfire_filelist_is_empty(filelist
)) {
1018 "digest_blake2b512, "
1019 "digest_blake2s256, "
1023 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
1025 // Prepare the statement
1026 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1028 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1029 sql
, sqlite3_errmsg(db
->handle
));
1033 for (unsigned int i
= 0; i
< pakfire_filelist_length(filelist
); i
++) {
1034 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
1037 r
= sqlite3_bind_int64(stmt
, 1, id
);
1039 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1040 pakfire_file_unref(file
);
1045 const char* path
= pakfire_file_get_path(file
);
1047 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
1049 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
1050 pakfire_file_unref(file
);
1055 size_t size
= pakfire_file_get_size(file
);
1057 r
= sqlite3_bind_int64(stmt
, 3, size
);
1059 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1060 pakfire_file_unref(file
);
1064 // Bind config - XXX TODO
1065 r
= sqlite3_bind_null(stmt
, 4);
1067 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
1068 pakfire_file_unref(file
);
1072 // Bind datafile - XXX TODO
1073 r
= sqlite3_bind_null(stmt
, 5);
1075 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
1076 pakfire_file_unref(file
);
1081 mode_t mode
= pakfire_file_get_mode(file
);
1083 r
= sqlite3_bind_int64(stmt
, 6, mode
);
1085 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
1086 pakfire_file_unref(file
);
1091 const char* uname
= pakfire_file_get_uname(file
);
1093 r
= sqlite3_bind_text(stmt
, 7, uname
, -1, NULL
);
1095 ERROR(db
->pakfire
, "Could not bind uname: %s\n", sqlite3_errmsg(db
->handle
));
1096 pakfire_file_unref(file
);
1101 const char* gname
= pakfire_file_get_gname(file
);
1103 r
= sqlite3_bind_text(stmt
, 8, gname
, -1, NULL
);
1105 ERROR(db
->pakfire
, "Could not bind gname: %s\n", sqlite3_errmsg(db
->handle
));
1106 pakfire_file_unref(file
);
1111 time_t ctime
= pakfire_file_get_ctime(file
);
1113 r
= sqlite3_bind_int64(stmt
, 9, ctime
);
1115 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
1116 pakfire_file_unref(file
);
1121 time_t mtime
= pakfire_file_get_mtime(file
);
1123 r
= sqlite3_bind_int64(stmt
, 10, mtime
);
1125 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
1126 pakfire_file_unref(file
);
1131 const char* mimetype
= pakfire_file_get_mimetype(file
);
1134 r
= sqlite3_bind_text(stmt
, 11, mimetype
, -1, NULL
);
1136 ERROR(db
->pakfire
, "Could not bind MIME type: %s\n",
1137 sqlite3_errmsg(db
->handle
));
1138 pakfire_file_unref(file
);
1142 r
= sqlite3_bind_null(stmt
, 11);
1144 ERROR(db
->pakfire
, "Could not bind an empty MIME type: %s\n",
1145 sqlite3_errmsg(db
->handle
));
1146 pakfire_file_unref(file
);
1151 // Bind capabilities - XXX TODO
1152 r
= sqlite3_bind_null(stmt
, 12);
1154 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1155 pakfire_file_unref(file
);
1160 r
= pakfire_db_bind_digest(db
, stmt
, 13, file
, PAKFIRE_DIGEST_SHA2_512
);
1162 ERROR(db
->pakfire
, "Could not bind SHA2-512 digest: %s\n",
1163 sqlite3_errmsg(db
->handle
));
1164 pakfire_file_unref(file
);
1169 r
= pakfire_db_bind_digest(db
, stmt
, 14, file
, PAKFIRE_DIGEST_SHA2_256
);
1171 ERROR(db
->pakfire
, "Could not bind SHA2-256 digest: %s\n",
1172 sqlite3_errmsg(db
->handle
));
1173 pakfire_file_unref(file
);
1177 // BLAKE2b512 Digest
1178 r
= pakfire_db_bind_digest(db
, stmt
, 15, file
, PAKFIRE_DIGEST_BLAKE2B512
);
1180 ERROR(db
->pakfire
, "Could not bind BLAKE2b512 digest: %s\n",
1181 sqlite3_errmsg(db
->handle
));
1182 pakfire_file_unref(file
);
1186 // BLAKE2s256 Digest
1187 r
= pakfire_db_bind_digest(db
, stmt
, 16, file
, PAKFIRE_DIGEST_BLAKE2S256
);
1189 ERROR(db
->pakfire
, "Could not bind BLAKE2s256 digest: %s\n",
1190 sqlite3_errmsg(db
->handle
));
1191 pakfire_file_unref(file
);
1196 r
= pakfire_db_bind_digest(db
, stmt
, 17, file
, PAKFIRE_DIGEST_SHA3_512
);
1198 ERROR(db
->pakfire
, "Could not bind SHA3-512 digest: %s\n",
1199 sqlite3_errmsg(db
->handle
));
1200 pakfire_file_unref(file
);
1205 r
= pakfire_db_bind_digest(db
, stmt
, 18, file
, PAKFIRE_DIGEST_SHA3_256
);
1207 ERROR(db
->pakfire
, "Could not bind SHA3-256 digest: %s\n",
1208 sqlite3_errmsg(db
->handle
));
1209 pakfire_file_unref(file
);
1215 r
= sqlite3_step(stmt
);
1216 } while (r
== SQLITE_BUSY
);
1219 if (r
!= SQLITE_DONE
) {
1220 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
1221 sqlite3_errmsg(db
->handle
));
1222 pakfire_file_unref(file
);
1226 // Move on to next file
1227 pakfire_file_unref(file
);
1229 // Reset bound values
1230 sqlite3_reset(stmt
);
1238 sqlite3_finalize(stmt
);
1239 pakfire_filelist_unref(filelist
);
1244 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1245 sqlite3_stmt
* stmt
= NULL
;
1249 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1251 // Prepare the statement
1252 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1254 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1255 sql
, sqlite3_errmsg(db
->handle
));
1259 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1260 // Fetch the scriptlet
1261 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1266 r
= sqlite3_bind_int64(stmt
, 1, id
);
1268 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1269 pakfire_scriptlet_unref(scriptlet
);
1274 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1276 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1277 pakfire_scriptlet_unref(scriptlet
);
1281 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1284 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1286 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1287 pakfire_scriptlet_unref(scriptlet
);
1293 r
= sqlite3_step(stmt
);
1294 } while (r
== SQLITE_BUSY
);
1297 if (r
!= SQLITE_DONE
) {
1298 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1299 sqlite3_errmsg(db
->handle
));
1300 pakfire_scriptlet_unref(scriptlet
);
1304 pakfire_scriptlet_unref(scriptlet
);
1306 // Reset bound values
1307 sqlite3_reset(stmt
);
1315 sqlite3_finalize(stmt
);
1320 int pakfire_db_add_package(struct pakfire_db
* db
,
1321 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1322 sqlite3_stmt
* stmt
= NULL
;
1325 char** groups
= NULL
;
1326 char* __groups
= NULL
;
1328 // Begin a new transaction
1329 r
= pakfire_db_begin_transaction(db
);
1333 const char* sql
= "INSERT INTO "
1375 "CURRENT_TIMESTAMP, "
1384 // Prepare the statement
1385 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1386 if (r
!= SQLITE_OK
) {
1387 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1388 sql
, sqlite3_errmsg(db
->handle
));
1393 const char* name
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_NAME
);
1395 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1397 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1402 const char* evr
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_EVR
);
1404 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1406 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1411 const char* arch
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_ARCH
);
1413 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1415 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1420 groups
= pakfire_package_get_strings(pkg
, PAKFIRE_PKG_GROUPS
);
1422 // Join everything together as SQLite doesn't support arrays
1423 __groups
= pakfire_string_join(groups
, " ");
1429 r
= sqlite3_bind_text(stmt
, 4, __groups
, -1, NULL
);
1431 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1437 r
= sqlite3_bind_null(stmt
, 4);
1443 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
1445 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1447 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1452 unsigned long long size
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_DOWNLOADSIZE
, 0);
1454 r
= sqlite3_bind_int64(stmt
, 6, size
);
1456 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1460 // Bind installed size
1461 unsigned long long inst_size
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_INSTALLSIZE
, 0);
1463 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1465 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1469 const unsigned char* digest
= NULL
;
1470 enum pakfire_digest_types digest_type
= 0;
1471 size_t digest_length
= 0;
1474 digest
= pakfire_package_get_digest(pkg
, &digest_type
, &digest_length
);
1476 ERROR(db
->pakfire
, "Could not fetch the package's digest: %m\n");
1481 // Set the digest type
1482 r
= sqlite3_bind_int64(stmt
, 8, digest_type
);
1484 ERROR(db
->pakfire
, "Could not bind digest type: %s\n", sqlite3_errmsg(db
->handle
));
1489 r
= sqlite3_bind_blob(stmt
, 9, digest
, digest_length
, NULL
);
1491 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1496 const char* license
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_LICENSE
);
1498 r
= sqlite3_bind_text(stmt
, 10, license
, -1, NULL
);
1500 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1505 const char* summary
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SUMMARY
);
1507 r
= sqlite3_bind_text(stmt
, 11, summary
, -1, NULL
);
1509 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1514 const char* description
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_DESCRIPTION
);
1516 r
= sqlite3_bind_text(stmt
, 12, description
, -1, NULL
);
1518 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1523 const char* uuid
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_UUID
);
1525 r
= sqlite3_bind_text(stmt
, 13, uuid
, -1, NULL
);
1527 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1532 const char* vendor
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_VENDOR
);
1534 r
= sqlite3_bind_text(stmt
, 14, vendor
, -1, NULL
);
1536 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1541 const char* build_host
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_BUILD_HOST
);
1543 r
= sqlite3_bind_text(stmt
, 15, build_host
, -1, NULL
);
1545 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1550 time_t build_time
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_BUILD_TIME
, 0);
1552 r
= sqlite3_bind_int64(stmt
, 16, build_time
);
1554 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1558 // Bind repository name
1559 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1561 const char* repo_name
= pakfire_repo_get_name(repo
);
1562 pakfire_repo_unref(repo
);
1564 r
= sqlite3_bind_text(stmt
, 17, repo_name
, -1, NULL
);
1570 r
= sqlite3_bind_null(stmt
, 17);
1575 // installed by the user?
1576 r
= sqlite3_bind_int(stmt
, 18, userinstalled
);
1578 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1582 // Source package name
1583 const char* source_name
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
);
1585 r
= sqlite3_bind_text(stmt
, 19, source_name
, -1, NULL
);
1589 r
= sqlite3_bind_null(stmt
, 19);
1595 const char* source_evr
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
);
1597 r
= sqlite3_bind_text(stmt
, 20, source_evr
, -1, NULL
);
1601 r
= sqlite3_bind_null(stmt
, 20);
1607 const char* source_arch
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
);
1609 r
= sqlite3_bind_text(stmt
, 21, source_arch
, -1, NULL
);
1613 r
= sqlite3_bind_null(stmt
, 21);
1619 const char* distribution
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_DISTRO
);
1621 r
= sqlite3_bind_text(stmt
, 22, distribution
, -1, NULL
);
1625 r
= sqlite3_bind_null(stmt
, 22);
1632 r
= sqlite3_step(stmt
);
1633 } while (r
== SQLITE_BUSY
);
1635 if (r
!= SQLITE_DONE
) {
1636 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1637 sqlite3_errmsg(db
->handle
));
1643 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1646 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1651 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1656 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1662 pakfire_strings_free(groups
);
1666 sqlite3_finalize(stmt
);
1668 // Commit or rollback
1670 pakfire_db_rollback(db
);
1672 r
= pakfire_db_commit(db
);
1677 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1678 sqlite3_stmt
* stmt
= NULL
;
1681 // Begin a new transaction
1682 r
= pakfire_db_begin_transaction(db
);
1686 // Fetch the package's UUID
1687 const char* uuid
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_UUID
);
1689 ERROR(db
->pakfire
, "Package has no UUID\n");
1694 r
= sqlite3_prepare_v2(db
->handle
,
1695 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1697 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1698 sqlite3_errmsg(db
->handle
));
1703 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1705 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1711 r
= sqlite3_step(stmt
);
1712 } while (r
== SQLITE_BUSY
);
1714 // Check if we have been successful
1715 if (r
!= SQLITE_DONE
) {
1716 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1726 sqlite3_finalize(stmt
);
1728 // Commit or rollback
1730 pakfire_db_rollback(db
);
1732 r
= pakfire_db_commit(db
);
1737 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1738 struct pakfire_package
* pkg
, const char* type
) {
1739 struct pakfire_scriptlet
* scriptlet
= NULL
;
1740 sqlite3_stmt
* stmt
= NULL
;
1743 // Fetch the package's UUID
1744 const char* uuid
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_UUID
);
1746 ERROR(db
->pakfire
, "Package has no UUID\n");
1750 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1751 JOIN scriptlets ON packages.id = scriptlets.pkg \
1752 WHERE packages.uuid = ? AND scriptlets.type = ?";
1754 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1756 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1757 sql
, sqlite3_errmsg(db
->handle
));
1762 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1764 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1768 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1770 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1774 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1778 r
= sqlite3_step(stmt
);
1779 } while (r
== SQLITE_BUSY
);
1781 // We have some payload
1782 if (r
== SQLITE_ROW
) {
1783 const void* data
= sqlite3_column_blob(stmt
, 1);
1784 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1786 // Create a scriptlet object
1787 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1794 sqlite3_finalize(stmt
);
1799 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1800 struct pakfire_package
* pkg
= NULL
;
1804 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1806 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1811 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1813 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1818 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1820 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1825 r
= pakfire_package_create(&pkg
, db
->pakfire
, repo
, name
, evr
, arch
);
1827 ERROR(db
->pakfire
, "Could not create package '%s-%s.%s': %m\n", name
, evr
, arch
);
1832 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1834 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_DBID
, id
);
1840 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1842 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_GROUPS
, groups
);
1848 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1850 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_FILENAME
, filename
);
1856 size_t size
= sqlite3_column_int64(stmt
, 6);
1858 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_DOWNLOADSIZE
, size
);
1864 size
= sqlite3_column_int64(stmt
, 7);
1866 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_INSTALLSIZE
, size
);
1872 enum pakfire_digest_types digest_type
= sqlite3_column_int64(stmt
, 8);
1873 size_t digest_length
= 0;
1878 const unsigned char* digest
= sqlite3_column_blob(stmt
, 9);
1879 if (digest_type
&& digest
) {
1880 pakfire_package_set_digest(pkg
, digest_type
, digest
, digest_length
);
1884 const char* license
= (const char*)sqlite3_column_text(stmt
, 10);
1886 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_LICENSE
, license
);
1892 const char* summary
= (const char*)sqlite3_column_text(stmt
, 11);
1894 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SUMMARY
, summary
);
1900 const char* description
= (const char*)sqlite3_column_text(stmt
, 12);
1902 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DESCRIPTION
, description
);
1908 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 13);
1910 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_UUID
, uuid
);
1916 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 14);
1918 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_VENDOR
, vendor
);
1924 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 15);
1926 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_BUILD_HOST
, build_host
);
1932 time_t build_time
= sqlite3_column_int64(stmt
, 16);
1934 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_BUILD_TIME
, build_time
);
1940 time_t install_time
= sqlite3_column_int64(stmt
, 17);
1942 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_INSTALLTIME
, install_time
);
1947 // installed by user?
1948 int userinstalled
= sqlite3_column_int(stmt
, 18);
1950 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1953 const char* files
= (const char*)sqlite3_column_text(stmt
, 19);
1955 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1962 const struct dependency
{
1964 const enum pakfire_package_key key
;
1965 } dependencies
[] = {
1966 { 20, PAKFIRE_PKG_PROVIDES
},
1967 { 21, PAKFIRE_PKG_PREREQUIRES
},
1968 { 22, PAKFIRE_PKG_REQUIRES
},
1969 { 23, PAKFIRE_PKG_CONFLICTS
},
1970 { 24, PAKFIRE_PKG_OBSOLETES
},
1971 { 25, PAKFIRE_PKG_RECOMMENDS
},
1972 { 26, PAKFIRE_PKG_SUGGESTS
},
1973 { 27, PAKFIRE_PKG_SUPPLEMENTS
},
1974 { 28, PAKFIRE_PKG_ENHANCES
},
1978 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1979 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1981 r
= pakfire_str2deps(db
->pakfire
, pkg
, deps
->key
, relations
);
1988 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 29);
1990 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
1996 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 30);
1998 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
2004 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 31);
2006 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, source_arch
);
2012 const char* distribution
= (const char*)sqlite3_column_text(stmt
, 32);
2014 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DESCRIPTION
, description
);
2024 pakfire_package_unref(pkg
);
2029 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
2030 sqlite3_stmt
* stmt
= NULL
;
2033 DEBUG(db
->pakfire
, "Loading package database...\n");
2035 // Drop contents of the repository
2036 pakfire_repo_clear(repo
);
2038 // Save starting time
2039 clock_t t_start
= clock();
2061 "strftime('%s', installed) AS installed, "
2064 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
2067 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2068 "WHERE d.pkg = packages.id AND d.type = 'provides'"
2071 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2072 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
2073 ") AS prerequires, "
2075 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2076 "WHERE d.pkg = packages.id AND d.type = 'requires'"
2079 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2080 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
2083 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2084 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
2087 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2088 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
2091 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2092 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
2095 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2096 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
2097 ") AS supplements, "
2099 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2100 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
2110 // Prepare the statement
2111 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2113 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2114 sql
, sqlite3_errmsg(db
->handle
));
2120 r
= sqlite3_step(stmt
);
2123 // Retry if the database was busy
2129 r
= pakfire_db_load_package(db
, repo
, stmt
);
2134 // All rows have been processed
2138 // Go to error in any other cases
2145 // Save time when we finished
2148 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
2149 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
2151 // Mark repository as changed
2152 pakfire_repo_has_changed(repo
);
2159 ERROR(db
->pakfire
, "Failed reading package database: %m\n");
2160 pakfire_repo_clear(repo
);
2164 sqlite3_finalize(stmt
);
2169 static int pakfire_db_load_file_digest(struct pakfire_db
* db
, struct pakfire_file
* file
,
2170 sqlite3_stmt
* stmt
, const enum pakfire_digest_types type
, const int field
) {
2172 const unsigned char* digest
= sqlite3_column_blob(stmt
, field
);
2174 // Nothing further to do if field is NULL
2178 // Length of the stored value
2179 const size_t length
= sqlite3_column_bytes(stmt
, field
);
2182 return pakfire_file_set_digest(file
, type
, digest
, length
);
2185 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2186 sqlite3_stmt
* stmt
) {
2187 struct pakfire_file
* file
= NULL
;
2188 char abspath
[PATH_MAX
];
2191 // Create a new file object
2192 r
= pakfire_file_create(&file
, db
->pakfire
);
2197 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2199 // Abort if no path is set
2201 ERROR(db
->pakfire
, "File has no path\n");
2207 r
= pakfire_file_set_path(file
, path
);
2209 ERROR(db
->pakfire
, "%s: Could not set path '%s': %m\n", path
, path
);
2213 // Make the absolute path
2214 r
= pakfire_path(db
->pakfire
, abspath
, "%s", path
);
2216 ERROR(db
->pakfire
, "%s: Could not make absolute path: %m\n", path
);
2220 // Set the absolute path
2221 r
= pakfire_file_set_abspath(file
, abspath
);
2223 ERROR(db
->pakfire
, "%s: Could not set absolute path %s: %m\n", path
, abspath
);
2228 size_t size
= sqlite3_column_int64(stmt
, 1);
2230 pakfire_file_set_size(file
, size
);
2233 mode_t mode
= sqlite3_column_int(stmt
, 2);
2235 pakfire_file_set_mode(file
, mode
);
2238 const char* uname
= (const char*)sqlite3_column_text(stmt
, 3);
2240 // Abort if no user is set
2242 ERROR(db
->pakfire
, "%s: No user\n", path
);
2248 r
= pakfire_file_set_uname(file
, uname
);
2250 ERROR(db
->pakfire
, "%s: Could not set user '%s': %m\n", path
, uname
);
2255 const char* gname
= (const char*)sqlite3_column_text(stmt
, 4);
2257 // Abort if no group is set
2259 ERROR(db
->pakfire
, "%s: No group\n", path
);
2265 r
= pakfire_file_set_gname(file
, gname
);
2267 ERROR(db
->pakfire
, "%s: Could not set group '%s': %m\n", path
, gname
);
2272 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2274 pakfire_file_set_ctime(file
, ctime
);
2277 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2279 pakfire_file_set_mtime(file
, mtime
);
2281 const char* mimetype
= (const char*)sqlite3_column_text(stmt
, 7);
2284 r
= pakfire_file_set_mimetype(file
, mimetype
);
2289 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_512
, 8);
2294 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_256
, 9);
2298 // BLAKE2b512 Digest
2299 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2B512
, 10);
2303 // BLAKE2s256 Digest
2304 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2S256
, 11);
2309 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_512
, 12);
2314 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_256
, 13);
2318 // Append the file to the filelist
2319 r
= pakfire_filelist_add(filelist
, file
);
2325 pakfire_file_unref(file
);
2330 int pakfire_db_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
) {
2331 struct pakfire_filelist
* list
= NULL
;
2332 sqlite3_stmt
* stmt
= NULL
;
2347 "digest_blake2b512, "
2348 "digest_blake2s256, "
2355 // Prepare the statement
2356 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2358 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2359 sql
, sqlite3_errmsg(db
->handle
));
2363 // Create a new filelist
2364 r
= pakfire_filelist_create(&list
, db
->pakfire
);
2368 // Iterate over all rows
2371 r
= sqlite3_step(stmt
);
2374 // Retry if the database was busy
2380 r
= pakfire_db_load_file(db
, list
, stmt
);
2385 // All rows have been processed
2390 // Go to error in any other cases
2397 // Return the filelist
2398 *filelist
= pakfire_filelist_ref(list
);
2402 ERROR(db
->pakfire
, "Could not fetch filelist: %m\n");
2405 sqlite3_finalize(stmt
);
2407 pakfire_filelist_unref(list
);
2412 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2413 struct pakfire_package
* pkg
) {
2414 struct pakfire_filelist
* fl
= NULL
;
2415 sqlite3_stmt
* stmt
= NULL
;
2418 // Fetch the package ID
2419 uint64_t id
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_DBID
, 0);
2421 ERROR(db
->pakfire
, "Package did not have an ID\n");
2425 // Create a new filelist
2426 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2428 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
2444 "digest_blake2b512, "
2445 "digest_blake2s256, "
2450 // Select all files that belong to this package
2454 // Filter out any files that are also in a different package (i.e. an update
2455 // that has already been installed and this is the cleanup of the obsolete pkg)
2460 "FROM files AS duplicates "
2462 "files.path = duplicates.path "
2464 "files.pkg != duplicates.pkg"
2467 // Return ordered by path
2471 // Prepare the statement
2472 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2474 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2475 sql
, sqlite3_errmsg(db
->handle
));
2480 r
= sqlite3_bind_int64(stmt
, 1, id
);
2482 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2488 r
= sqlite3_step(stmt
);
2491 // Retry if the database was busy
2497 r
= pakfire_db_load_file(db
, fl
, stmt
);
2502 // All rows have been processed
2506 // Go to error in any other cases
2513 *filelist
= pakfire_filelist_ref(fl
);
2518 sqlite3_finalize(stmt
);
2520 pakfire_filelist_unref(fl
);