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 sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
68 sqlite3_stmt
* stmt
= NULL
;
69 sqlite3_value
* val
= NULL
;
72 const char* sql
= "SELECT val FROM settings WHERE key = ?";
74 // Prepare the statement
75 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
77 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
78 // sql, sqlite3_errmsg(db->handle));
83 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
85 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
89 // Execute the statement
91 r
= sqlite3_step(stmt
);
92 } while (r
== SQLITE_BUSY
);
94 // We should have read a row
99 val
= sqlite3_column_value(stmt
, 0);
101 ERROR(db
->pakfire
, "Could not read value\n");
105 // Copy value onto the heap
106 val
= sqlite3_value_dup(val
);
110 sqlite3_finalize(stmt
);
115 static char* pakfire_db_get_string(struct pakfire_db
* db
, const char* key
) {
118 // Fetch the value from the database
119 sqlite3_value
* value
= pakfire_db_get(db
, key
);
123 // Extract the value as string
124 const char* p
= (const char*)sqlite3_value_text(value
);
128 // Copy string to heap
133 sqlite3_value_free(value
);
138 static int pakfire_db_set_string(struct pakfire_db
* db
, const char* key
, const char* val
) {
139 sqlite3_stmt
* stmt
= NULL
;
142 DEBUG(db
->pakfire
, "Setting %s to '%s'\n", key
, val
);
144 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
145 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
148 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
149 if (r
!= SQLITE_OK
) {
150 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
151 sql
, sqlite3_errmsg(db
->handle
));
156 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
157 if (r
!= SQLITE_OK
) {
158 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
163 r
= sqlite3_bind_text(stmt
, 2, val
, strlen(val
), NULL
);
164 if (r
!= SQLITE_OK
) {
165 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
169 // Execute the statement
171 r
= sqlite3_step(stmt
);
172 } while (r
== SQLITE_BUSY
);
175 r
= (r
== SQLITE_OK
);
179 sqlite3_finalize(stmt
);
184 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
185 sqlite3_stmt
* stmt
= NULL
;
188 DEBUG(db
->pakfire
, "Setting %s to '%d'\n", key
, val
);
190 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
191 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
194 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
195 if (r
!= SQLITE_OK
) {
196 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
197 sql
, sqlite3_errmsg(db
->handle
));
202 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
203 if (r
!= SQLITE_OK
) {
204 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
209 r
= sqlite3_bind_int64(stmt
, 2, val
);
210 if (r
!= SQLITE_OK
) {
211 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
215 // Execute the statement
217 r
= sqlite3_step(stmt
);
218 } while (r
== SQLITE_BUSY
);
221 r
= (r
== SQLITE_OK
);
225 sqlite3_finalize(stmt
);
230 static time_t pakfire_read_modification_time(struct pakfire_db
* db
) {
233 // Fetch the value from the database
234 sqlite3_value
* value
= pakfire_db_get(db
, "last_modified_at");
236 t
= sqlite3_value_int64(value
);
237 sqlite3_value_free(value
);
239 DEBUG(db
->pakfire
, "Could not find last modification timestamp\n");
245 static int pakfire_update_modification_time(struct pakfire_db
* db
) {
246 // Get the current time in UTC
247 time_t t
= time(NULL
);
249 // Store it in the database
250 int r
= pakfire_db_set_int(db
, "last_modified_at", t
);
254 // Update the last modification timestamp
255 db
->last_modified_at
= t
;
260 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
263 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
266 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
267 } while (r
== SQLITE_BUSY
);
271 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
277 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
278 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
281 static int pakfire_db_commit(struct pakfire_db
* db
) {
283 If the database was opened in read-write mode, we will store the
284 timestamp of the latest modification to compare whether the database
285 has been changed mid-transaction.
287 if (db
->mode
== PAKFIRE_DB_READWRITE
) {
288 int r
= pakfire_update_modification_time(db
);
293 return pakfire_db_execute(db
, "COMMIT");
296 static int pakfire_db_rollback(struct pakfire_db
* db
) {
297 return pakfire_db_execute(db
, "ROLLBACK");
301 This function performs any fast optimization and tries to truncate the WAL log file
302 to keep the database as compact as possible on disk.
304 static void pakfire_db_optimize(struct pakfire_db
* db
) {
305 pakfire_db_execute(db
, "PRAGMA optimize");
306 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
309 static void pakfire_db_free(struct pakfire_db
* db
) {
311 // Optimize the database before it is being closed
312 pakfire_db_optimize(db
);
314 // Close database handle
315 int r
= sqlite3_close(db
->handle
);
316 if (r
!= SQLITE_OK
) {
317 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
318 sqlite3_errmsg(db
->handle
));
322 pakfire_unref(db
->pakfire
);
327 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
328 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
332 int schema
= sqlite3_value_int64(value
);
333 sqlite3_value_free(value
);
335 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
340 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
343 // Create settings table
344 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
348 // settings: Add a unique index on key
349 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
353 // Create packages table
354 r
= pakfire_db_execute(db
,
355 "CREATE TABLE IF NOT EXISTS packages("
356 "id INTEGER PRIMARY KEY, "
363 "inst_size INTEGER, "
371 "build_time INTEGER, "
372 "installed INTEGER, "
373 "userinstalled INTEGER, "
383 // packages: Create index to find package by name
384 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
388 // packages: Create unique index over UUID
389 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
393 // Create dependencies table
394 r
= pakfire_db_execute(db
,
395 "CREATE TABLE IF NOT EXISTS dependencies("
399 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
404 // dependencies: Add index over packages
405 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
409 // Create files table
410 r
= pakfire_db_execute(db
,
411 "CREATE TABLE IF NOT EXISTS files("
412 "id INTEGER PRIMARY KEY, "
423 "capabilities TEXT, "
424 "digest_sha2_512 BLOB, "
425 "digest_sha2_256 BLOB, "
426 "digest_blake2b512 BLOB, "
427 "digest_blake2s256 BLOB, "
428 "digest_sha3_512 BLOB, "
429 "digest_sha3_256 BLOB, "
430 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
435 // files: Add index over packages
436 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
440 // files: Add index over path
441 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
445 // Create scriptlets table
446 r
= pakfire_db_execute(db
,
447 "CREATE TABLE IF NOT EXISTS scriptlets("
448 "id INTEGER PRIMARY KEY, "
452 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
457 // scriptlets: Add index over packages
458 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
462 const char* arch
= pakfire_get_arch(db
->pakfire
);
465 r
= pakfire_db_set_string(db
, "arch", arch
);
467 ERROR(db
->pakfire
, "Could not set architecture\n");
474 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
475 // packages: Drop build_id column
478 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
479 // need to recreate the whole table and rename it afterwards. Annoying.
484 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
487 while (db
->schema
< CURRENT_SCHEMA
) {
488 // Begin a new transaction
489 r
= pakfire_db_begin_transaction(db
);
493 switch (db
->schema
) {
496 r
= pakfire_db_create_schema(db
);
500 db
->schema
= CURRENT_SCHEMA
;
504 r
= pakfire_db_migrate_to_schema_8(db
);
512 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
516 // Update the schema version
517 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
522 r
= pakfire_db_commit(db
);
530 pakfire_db_rollback(db
);
535 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
538 // Fetch database architecture
539 char* db_arch
= pakfire_db_get_string(db
, "arch");
541 ERROR(db
->pakfire
, "Database is of an unknown architecture\n");
545 // Fetch the running architecture
546 const char* arch
= pakfire_get_arch(db
->pakfire
);
551 if (strcmp(db_arch
, arch
) == 0)
561 static int pakfire_db_setup(struct pakfire_db
* db
) {
565 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
567 // Enable foreign keys
568 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
570 // Make LIKE case-sensitive
571 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
573 // Fetch the current schema
574 db
->schema
= pakfire_db_get_schema(db
);
576 // Check if the schema is recent enough
577 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
578 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
583 // Read modification timestamp
584 db
->last_modified_at
= pakfire_read_modification_time(db
);
586 DEBUG(db
->pakfire
, "The database was last modified at %ld\n", db
->last_modified_at
);
588 // Done when not in read-write mode
589 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
592 // Disable secure delete
593 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
595 // Set database journal to WAL
596 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
597 if (r
!= SQLITE_OK
) {
598 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
599 sqlite3_errmsg(db
->handle
));
603 // Disable autocheckpoint
604 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
605 if (r
!= SQLITE_OK
) {
606 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
607 sqlite3_errmsg(db
->handle
));
611 // Create or migrate schema
612 r
= pakfire_db_migrate_schema(db
);
619 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
622 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
626 o
->pakfire
= pakfire_ref(pakfire
);
629 int sqlite3_flags
= 0;
631 // Store mode & forward it to sqlite3
632 if (flags
& PAKFIRE_DB_READWRITE
) {
633 o
->mode
= PAKFIRE_DB_READWRITE
;
634 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
636 o
->mode
= PAKFIRE_DB_READONLY
;
637 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
641 r
= pakfire_path(o
->pakfire
, o
->path
, "%s", DATABASE_PATH
);
645 // Try to open the sqlite3 database file
646 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
647 if (r
!= SQLITE_OK
) {
648 ERROR(pakfire
, "Could not open database %s: %s\n",
649 o
->path
, sqlite3_errmsg(o
->handle
));
655 // Setup the database
656 r
= pakfire_db_setup(o
);
660 // Check for compatible architecture
661 r
= pakfire_db_check_arch(o
);
675 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
681 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
690 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
691 sqlite3_stmt
* stmt
= NULL
;
694 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
696 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
697 sqlite3_errmsg(db
->handle
));
702 unsigned long errors
= 0;
706 r
= sqlite3_step(stmt
);
707 } while (r
== SQLITE_BUSY
);
709 if (r
== SQLITE_ROW
) {
710 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
712 // If the message is "ok", the database has passed the check
713 if (strcmp(error
, "ok") == 0)
716 // Increment error counter
720 ERROR(db
->pakfire
, "%s\n", error
);
722 // Break on anything else
727 sqlite3_finalize(stmt
);
730 ERROR(db
->pakfire
, "Database integrity check failed\n");
732 INFO(db
->pakfire
, "Database integrity check passed\n");
737 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
738 sqlite3_stmt
* stmt
= NULL
;
741 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
743 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
744 sqlite3_errmsg(db
->handle
));
749 unsigned long errors
= 0;
753 r
= sqlite3_step(stmt
);
754 } while (r
== SQLITE_BUSY
);
756 if (r
== SQLITE_ROW
) {
757 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
758 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
759 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
760 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
762 // Increment error counter
766 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
767 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
769 // Break on anything else
774 sqlite3_finalize(stmt
);
777 ERROR(db
->pakfire
, "Foreign key check failed\n");
779 INFO(db
->pakfire
, "Foreign key check passed\n");
785 This function performs an integrity check of the database
787 int pakfire_db_check(struct pakfire_db
* db
) {
790 // Perform integrity check
791 r
= pakfire_db_integrity_check(db
);
795 // Perform foreign key check
796 r
= pakfire_db_foreign_key_check(db
);
803 // Returns the number of packages installed
804 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
805 sqlite3_stmt
* stmt
= NULL
;
806 ssize_t packages
= -1;
808 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
810 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
811 sqlite3_errmsg(db
->handle
));
817 r
= sqlite3_step(stmt
);
818 } while (r
== SQLITE_BUSY
);
820 if (r
== SQLITE_ROW
) {
821 packages
= sqlite3_column_int64(stmt
, 0);
824 sqlite3_finalize(stmt
);
829 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
830 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
832 // Convert name to ID
833 Id id
= pool_str2id(pool
, name
, 1);
835 // Append it to pooljobs
836 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
839 static const struct pakfire_digest
{
840 enum pakfire_digest_types type
;
842 } pakfire_digests
[] = {
843 { PAKFIRE_DIGEST_SHA2_512
, "sha512:" },
844 { PAKFIRE_DIGEST_SHA2_256
, "sha256:" },
848 static char* pakfire_db_pack_digest(enum pakfire_digest_types type
, const char* hexdigest
) {
852 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
853 if (digest
->type
== type
) {
854 r
= asprintf(&s
, "%s%s", digest
->prefix
, hexdigest
);
865 static const char* pakfire_db_unpack_digest(const char* hexdigest
, enum pakfire_digest_types
* type
) {
868 // Don't do anything for empty input
869 if (!hexdigest
|| !*hexdigest
)
872 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
873 if (pakfire_string_startswith(hexdigest
, digest
->prefix
)) {
874 *type
= digest
->type
;
876 // Return the beginning of the hexdigest
877 return hexdigest
+ strlen(digest
->prefix
);
885 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
886 sqlite3_stmt
* stmt
= NULL
;
889 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
891 // Prepare the statement
892 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
894 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
895 sql
, sqlite3_errmsg(db
->handle
));
899 const struct __relation
{
901 char** (*func
)(struct pakfire_package
*);
903 { "provides", pakfire_package_get_provides
},
904 { "prerequires", pakfire_package_get_prerequires
},
905 { "requires", pakfire_package_get_requires
},
906 { "conflicts", pakfire_package_get_conflicts
},
907 { "obsoletes", pakfire_package_get_obsoletes
},
908 { "recommends", pakfire_package_get_recommends
},
909 { "suggests", pakfire_package_get_suggests
},
910 { "supplements", pakfire_package_get_supplements
},
911 { "enhances", pakfire_package_get_enhances
},
915 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
916 char** list
= relation
->func(pkg
);
920 for (char** dep
= list
; *dep
; dep
++) {
922 r
= sqlite3_bind_int64(stmt
, 1, id
);
924 ERROR(db
->pakfire
, "Could not bind id: %s\n",
925 sqlite3_errmsg(db
->handle
));
930 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
932 ERROR(db
->pakfire
, "Could not bind type: %s\n",
933 sqlite3_errmsg(db
->handle
));
938 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
940 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
941 sqlite3_errmsg(db
->handle
));
947 r
= sqlite3_step(stmt
);
948 } while (r
== SQLITE_BUSY
);
952 // Reset bound values
964 sqlite3_finalize(stmt
);
969 static int pakfire_db_bind_digest(struct pakfire_db
* db
, sqlite3_stmt
* stmt
, const int field
,
970 struct pakfire_file
* file
, const enum pakfire_digest_types type
) {
971 const unsigned char* digest
= NULL
;
975 digest
= pakfire_file_get_digest(file
, type
, &length
);
977 // If this digest isn't set, just bind NULL
979 return sqlite3_bind_null(stmt
, field
);
981 // Otherwise bind the data blob
982 return sqlite3_bind_blob(stmt
, field
, digest
, length
, NULL
);
985 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
986 sqlite3_stmt
* stmt
= NULL
;
989 // Get the filelist from the archive
990 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
992 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
996 // Nothing to do if the list is empty
997 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_size(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
);
1130 // Bind capabilities - XXX TODO
1131 r
= sqlite3_bind_null(stmt
, 11);
1133 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1134 pakfire_file_unref(file
);
1139 r
= pakfire_db_bind_digest(db
, stmt
, 12, file
, PAKFIRE_DIGEST_SHA2_512
);
1141 ERROR(db
->pakfire
, "Could not bind SHA2-512 digest: %s\n",
1142 sqlite3_errmsg(db
->handle
));
1143 pakfire_file_unref(file
);
1148 r
= pakfire_db_bind_digest(db
, stmt
, 13, file
, PAKFIRE_DIGEST_SHA2_256
);
1150 ERROR(db
->pakfire
, "Could not bind SHA2-256 digest: %s\n",
1151 sqlite3_errmsg(db
->handle
));
1152 pakfire_file_unref(file
);
1156 // BLAKE2b512 Digest
1157 r
= pakfire_db_bind_digest(db
, stmt
, 14, file
, PAKFIRE_DIGEST_BLAKE2B512
);
1159 ERROR(db
->pakfire
, "Could not bind BLAKE2b512 digest: %s\n",
1160 sqlite3_errmsg(db
->handle
));
1161 pakfire_file_unref(file
);
1165 // BLAKE2s256 Digest
1166 r
= pakfire_db_bind_digest(db
, stmt
, 15, file
, PAKFIRE_DIGEST_BLAKE2S256
);
1168 ERROR(db
->pakfire
, "Could not bind BLAKE2s256 digest: %s\n",
1169 sqlite3_errmsg(db
->handle
));
1170 pakfire_file_unref(file
);
1175 r
= pakfire_db_bind_digest(db
, stmt
, 16, file
, PAKFIRE_DIGEST_SHA3_512
);
1177 ERROR(db
->pakfire
, "Could not bind SHA3-512 digest: %s\n",
1178 sqlite3_errmsg(db
->handle
));
1179 pakfire_file_unref(file
);
1184 r
= pakfire_db_bind_digest(db
, stmt
, 17, file
, PAKFIRE_DIGEST_SHA3_256
);
1186 ERROR(db
->pakfire
, "Could not bind SHA3-256 digest: %s\n",
1187 sqlite3_errmsg(db
->handle
));
1188 pakfire_file_unref(file
);
1194 r
= sqlite3_step(stmt
);
1195 } while (r
== SQLITE_BUSY
);
1198 if (r
!= SQLITE_DONE
) {
1199 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
1200 sqlite3_errmsg(db
->handle
));
1201 pakfire_file_unref(file
);
1205 // Move on to next file
1206 pakfire_file_unref(file
);
1208 // Reset bound values
1209 sqlite3_reset(stmt
);
1217 sqlite3_finalize(stmt
);
1218 pakfire_filelist_unref(filelist
);
1223 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1224 sqlite3_stmt
* stmt
= NULL
;
1228 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1230 // Prepare the statement
1231 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1233 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1234 sql
, sqlite3_errmsg(db
->handle
));
1238 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1239 // Fetch the scriptlet
1240 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1245 r
= sqlite3_bind_int64(stmt
, 1, id
);
1247 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1248 pakfire_scriptlet_unref(scriptlet
);
1253 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1255 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1256 pakfire_scriptlet_unref(scriptlet
);
1260 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1263 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1265 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1266 pakfire_scriptlet_unref(scriptlet
);
1272 r
= sqlite3_step(stmt
);
1273 } while (r
== SQLITE_BUSY
);
1276 if (r
!= SQLITE_DONE
) {
1277 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1278 sqlite3_errmsg(db
->handle
));
1279 pakfire_scriptlet_unref(scriptlet
);
1283 pakfire_scriptlet_unref(scriptlet
);
1285 // Reset bound values
1286 sqlite3_reset(stmt
);
1294 sqlite3_finalize(stmt
);
1299 int pakfire_db_add_package(struct pakfire_db
* db
,
1300 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1301 sqlite3_stmt
* stmt
= NULL
;
1302 char* digest
= NULL
;
1305 // Begin a new transaction
1306 r
= pakfire_db_begin_transaction(db
);
1310 const char* sql
= "INSERT INTO "
1350 "CURRENT_TIMESTAMP, "
1359 // Prepare the statement
1360 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1361 if (r
!= SQLITE_OK
) {
1362 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1363 sql
, sqlite3_errmsg(db
->handle
));
1368 const char* name
= pakfire_package_get_name(pkg
);
1370 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1372 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1377 const char* evr
= pakfire_package_get_evr(pkg
);
1379 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1381 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1386 const char* arch
= pakfire_package_get_arch(pkg
);
1388 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1390 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1395 char* groups
= pakfire_package_get_groups(pkg
);
1397 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1399 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1408 r
= sqlite3_bind_null(stmt
, 4);
1414 const char* filename
= pakfire_package_get_filename(pkg
);
1416 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1418 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1423 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1425 r
= sqlite3_bind_int64(stmt
, 6, size
);
1427 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1431 // Bind installed size
1432 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1434 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1436 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1440 enum pakfire_digest_types digest_type
= 0;
1442 const char* hexdigest
= pakfire_package_get_hexdigest(pkg
, &digest_type
);
1444 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
1448 r
= sqlite3_bind_text(stmt
, 8, digest
, -1, NULL
);
1450 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1456 const char* license
= pakfire_package_get_license(pkg
);
1458 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1460 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1465 const char* summary
= pakfire_package_get_summary(pkg
);
1467 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1469 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1474 const char* description
= pakfire_package_get_description(pkg
);
1476 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1478 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1483 const char* uuid
= pakfire_package_get_uuid(pkg
);
1485 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1487 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1492 const char* vendor
= pakfire_package_get_vendor(pkg
);
1494 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1496 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1501 const char* build_host
= pakfire_package_get_build_host(pkg
);
1503 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1505 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1510 time_t build_time
= pakfire_package_get_build_time(pkg
);
1512 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1514 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1518 // Bind repository name
1519 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1521 const char* repo_name
= pakfire_repo_get_name(repo
);
1522 pakfire_repo_unref(repo
);
1524 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1530 r
= sqlite3_bind_null(stmt
, 16);
1535 // installed by the user?
1536 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1538 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1542 // Source package name
1543 const char* source_name
= pakfire_package_get_source_name(pkg
);
1545 r
= sqlite3_bind_text(stmt
, 18, source_name
, -1, NULL
);
1549 r
= sqlite3_bind_null(stmt
, 18);
1555 const char* source_evr
= pakfire_package_get_source_evr(pkg
);
1557 r
= sqlite3_bind_text(stmt
, 19, source_evr
, -1, NULL
);
1561 r
= sqlite3_bind_null(stmt
, 19);
1567 const char* source_arch
= pakfire_package_get_source_arch(pkg
);
1569 r
= sqlite3_bind_text(stmt
, 20, source_arch
, -1, NULL
);
1573 r
= sqlite3_bind_null(stmt
, 20);
1579 const char* distribution
= pakfire_package_get_distribution(pkg
);
1581 r
= sqlite3_bind_text(stmt
, 21, distribution
, -1, NULL
);
1585 r
= sqlite3_bind_null(stmt
, 21);
1592 r
= sqlite3_step(stmt
);
1593 } while (r
== SQLITE_BUSY
);
1595 if (r
!= SQLITE_DONE
) {
1596 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1597 sqlite3_errmsg(db
->handle
));
1603 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1606 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1611 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1616 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1622 sqlite3_finalize(stmt
);
1626 // Commit or rollback
1628 pakfire_db_rollback(db
);
1630 r
= pakfire_db_commit(db
);
1635 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1636 sqlite3_stmt
* stmt
= NULL
;
1639 // Begin a new transaction
1640 r
= pakfire_db_begin_transaction(db
);
1644 // Fetch the package's UUID
1645 const char* uuid
= pakfire_package_get_uuid(pkg
);
1647 ERROR(db
->pakfire
, "Package has no UUID\n");
1652 r
= sqlite3_prepare_v2(db
->handle
,
1653 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1655 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1656 sqlite3_errmsg(db
->handle
));
1661 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1663 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1669 r
= sqlite3_step(stmt
);
1670 } while (r
== SQLITE_BUSY
);
1672 // Check if we have been successful
1673 if (r
!= SQLITE_DONE
) {
1674 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1684 sqlite3_finalize(stmt
);
1686 // Commit or rollback
1688 pakfire_db_rollback(db
);
1690 r
= pakfire_db_commit(db
);
1695 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1696 struct pakfire_package
* pkg
, const char* type
) {
1697 struct pakfire_scriptlet
* scriptlet
= NULL
;
1698 sqlite3_stmt
* stmt
= NULL
;
1701 // Fetch the package's UUID
1702 const char* uuid
= pakfire_package_get_uuid(pkg
);
1704 ERROR(db
->pakfire
, "Package has no UUID\n");
1708 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1709 JOIN scriptlets ON packages.id = scriptlets.pkg \
1710 WHERE packages.uuid = ? AND scriptlets.type = ?";
1712 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1714 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1715 sql
, sqlite3_errmsg(db
->handle
));
1720 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1722 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1726 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1728 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1732 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1736 r
= sqlite3_step(stmt
);
1737 } while (r
== SQLITE_BUSY
);
1739 // We have some payload
1740 if (r
== SQLITE_ROW
) {
1741 const void* data
= sqlite3_column_blob(stmt
, 1);
1742 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1744 // Create a scriptlet object
1745 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1752 sqlite3_finalize(stmt
);
1757 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1758 struct pakfire_package
* pkg
= NULL
;
1762 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1764 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1769 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1771 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1776 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1778 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1783 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1785 ERROR(db
->pakfire
, "Could not create package\n");
1790 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1792 pakfire_package_set_dbid(pkg
, id
);
1795 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1797 pakfire_package_set_groups(pkg
, groups
);
1801 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1803 pakfire_package_set_filename(pkg
, filename
);
1807 size_t size
= sqlite3_column_int64(stmt
, 6);
1809 pakfire_package_set_downloadsize(pkg
, size
);
1813 size
= sqlite3_column_int64(stmt
, 7);
1815 pakfire_package_set_installsize(pkg
, size
);
1819 const char* digest
= (const char*)sqlite3_column_text(stmt
, 8);
1821 enum pakfire_digest_types digest_type
= 0;
1824 const char* hexdigest
= pakfire_db_unpack_digest(digest
, &digest_type
);
1826 pakfire_package_set_hexdigest(pkg
, digest_type
, hexdigest
);
1830 const char* license
= (const char*)sqlite3_column_text(stmt
, 9);
1832 pakfire_package_set_license(pkg
, license
);
1836 const char* summary
= (const char*)sqlite3_column_text(stmt
, 10);
1838 pakfire_package_set_summary(pkg
, summary
);
1842 const char* description
= (const char*)sqlite3_column_text(stmt
, 11);
1844 pakfire_package_set_description(pkg
, description
);
1848 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 12);
1850 pakfire_package_set_uuid(pkg
, uuid
);
1854 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 13);
1856 pakfire_package_set_vendor(pkg
, vendor
);
1860 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 14);
1862 pakfire_package_set_build_host(pkg
, build_host
);
1866 time_t build_time
= sqlite3_column_int64(stmt
, 15);
1868 pakfire_package_set_build_time(pkg
, build_time
);
1872 time_t install_time
= sqlite3_column_int64(stmt
, 16);
1874 pakfire_package_set_install_time(pkg
, install_time
);
1877 // installed by user?
1878 int userinstalled
= sqlite3_column_int(stmt
, 17);
1880 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1883 const char* files
= (const char*)sqlite3_column_text(stmt
, 18);
1885 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1892 const struct dependency
{
1894 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1895 } dependencies
[] = {
1896 { 19, pakfire_package_add_provides
},
1897 { 20, pakfire_package_add_prerequires
},
1898 { 21, pakfire_package_add_requires
},
1899 { 22, pakfire_package_add_conflicts
},
1900 { 23, pakfire_package_add_obsoletes
},
1901 { 24, pakfire_package_add_recommends
},
1902 { 25, pakfire_package_add_suggests
},
1903 { 26, pakfire_package_add_supplements
},
1904 { 27, pakfire_package_add_enhances
},
1908 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1909 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1911 pakfire_str2deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1916 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 28);
1918 pakfire_package_set_source_name(pkg
, source_name
);
1921 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 29);
1923 pakfire_package_set_source_evr(pkg
, source_evr
);
1926 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 30);
1928 pakfire_package_set_source_arch(pkg
, source_arch
);
1931 const char* distribution
= (const char*)sqlite3_column_text(stmt
, 31);
1933 pakfire_package_set_distribution(pkg
, distribution
);
1940 pakfire_package_unref(pkg
);
1945 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1946 sqlite3_stmt
* stmt
= NULL
;
1949 DEBUG(db
->pakfire
, "Loading package database...\n");
1951 // Drop contents of the repository
1952 pakfire_repo_clear(repo
);
1954 // Save starting time
1955 clock_t t_start
= clock();
1976 "strftime('%s', installed) AS installed, "
1979 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1982 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1983 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1986 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1987 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1988 ") AS prerequires, "
1990 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1991 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1994 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1995 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1998 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1999 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
2002 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2003 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
2006 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2007 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
2010 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2011 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
2012 ") AS supplements, "
2014 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2015 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
2025 // Prepare the statement
2026 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2028 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2029 sql
, sqlite3_errmsg(db
->handle
));
2035 r
= sqlite3_step(stmt
);
2038 // Retry if the database was busy
2044 r
= pakfire_db_load_package(db
, repo
, stmt
);
2049 // All rows have been processed
2053 // Go to error in any other cases
2060 // Save time when we finished
2063 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
2064 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
2066 // Mark repository as changed
2067 pakfire_repo_has_changed(repo
);
2074 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
2075 pakfire_repo_clear(repo
);
2079 sqlite3_finalize(stmt
);
2084 static int pakfire_db_load_file_digest(struct pakfire_db
* db
, struct pakfire_file
* file
,
2085 sqlite3_stmt
* stmt
, const enum pakfire_digest_types type
, const int field
) {
2087 const unsigned char* digest
= sqlite3_column_blob(stmt
, field
);
2089 // Nothing further to do if field is NULL
2093 // Length of the stored value
2094 const size_t length
= sqlite3_column_bytes(stmt
, field
);
2097 return pakfire_file_set_digest(file
, type
, digest
, length
);
2100 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2101 sqlite3_stmt
* stmt
) {
2102 struct pakfire_file
* file
= NULL
;
2105 // Create a new file object
2106 r
= pakfire_file_create(&file
, db
->pakfire
);
2111 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2113 // Abort if no path is set
2115 ERROR(db
->pakfire
, "File has no path\n");
2121 r
= pakfire_file_set_path(file
, path
);
2123 ERROR(db
->pakfire
, "%s: Could not set path '%s': %m\n", path
, path
);
2128 size_t size
= sqlite3_column_int64(stmt
, 1);
2130 pakfire_file_set_size(file
, size
);
2133 mode_t mode
= sqlite3_column_int(stmt
, 2);
2135 pakfire_file_set_mode(file
, mode
);
2138 const char* uname
= (const char*)sqlite3_column_text(stmt
, 3);
2140 // Abort if no user is set
2142 ERROR(db
->pakfire
, "%s: No user\n", path
);
2148 r
= pakfire_file_set_uname(file
, uname
);
2150 ERROR(db
->pakfire
, "%s: Could not set user '%s': %m\n", path
, uname
);
2155 const char* gname
= (const char*)sqlite3_column_text(stmt
, 4);
2157 // Abort if no group is set
2159 ERROR(db
->pakfire
, "%s: No group\n", path
);
2165 r
= pakfire_file_set_gname(file
, gname
);
2167 ERROR(db
->pakfire
, "%s: Could not set group '%s': %m\n", path
, gname
);
2172 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2174 pakfire_file_set_ctime(file
, ctime
);
2177 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2179 pakfire_file_set_mtime(file
, mtime
);
2182 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_512
, 7);
2187 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_256
, 8);
2191 // BLAKE2b512 Digest
2192 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2B512
, 9);
2196 // BLAKE2s256 Digest
2197 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2S256
, 10);
2202 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_512
, 11);
2207 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_256
, 12);
2211 // Append the file to the filelist
2212 r
= pakfire_filelist_append(filelist
, file
);
2218 pakfire_file_unref(file
);
2223 int pakfire_db_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
) {
2224 struct pakfire_filelist
* list
= NULL
;
2225 sqlite3_stmt
* stmt
= NULL
;
2239 "digest_blake2b512, "
2240 "digest_blake2s256, "
2247 // Prepare the statement
2248 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2250 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2251 sql
, sqlite3_errmsg(db
->handle
));
2255 // Create a new filelist
2256 r
= pakfire_filelist_create(&list
, db
->pakfire
);
2260 // Iterate over all rows
2263 r
= sqlite3_step(stmt
);
2266 // Retry if the database was busy
2272 r
= pakfire_db_load_file(db
, list
, stmt
);
2277 // All rows have been processed
2282 // Go to error in any other cases
2289 // Return the filelist
2290 *filelist
= pakfire_filelist_ref(list
);
2294 ERROR(db
->pakfire
, "Could not fetch filelist: %m\n");
2297 sqlite3_finalize(stmt
);
2299 pakfire_filelist_unref(list
);
2304 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2305 struct pakfire_package
* pkg
) {
2306 struct pakfire_filelist
* fl
= NULL
;
2307 sqlite3_stmt
* stmt
= NULL
;
2310 // Fetch the package ID
2311 uint64_t id
= pakfire_package_get_dbid(pkg
);
2313 ERROR(db
->pakfire
, "Package did not have an ID\n");
2317 // Create a new filelist
2318 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2320 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
2335 "digest_blake2b512, "
2336 "digest_blake2s256, "
2341 // Select all files that belong to this package
2345 // Filter out any files that are also in a different package (i.e. an update
2346 // that has already been installed and this is the cleanup of the obsolete pkg)
2351 "FROM files AS duplicates "
2353 "files.path = duplicates.path "
2355 "files.pkg != duplicates.pkg"
2358 // Return the longest paths first
2363 // Prepare the statement
2364 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2366 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2367 sql
, sqlite3_errmsg(db
->handle
));
2372 r
= sqlite3_bind_int64(stmt
, 1, id
);
2374 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2380 r
= sqlite3_step(stmt
);
2383 // Retry if the database was busy
2389 r
= pakfire_db_load_file(db
, fl
, stmt
);
2394 // All rows have been processed
2398 // Go to error in any other cases
2405 *filelist
= pakfire_filelist_ref(fl
);
2410 sqlite3_finalize(stmt
);
2412 pakfire_filelist_unref(fl
);