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/ctx.h>
32 #include <pakfire/db.h>
33 #include <pakfire/dependencies.h>
34 #include <pakfire/digest.h>
35 #include <pakfire/file.h>
36 #include <pakfire/filelist.h>
37 #include <pakfire/logging.h>
38 #include <pakfire/package.h>
39 #include <pakfire/pakfire.h>
40 #include <pakfire/repo.h>
41 #include <pakfire/string.h>
42 #include <pakfire/util.h>
44 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
46 #define CURRENT_SCHEMA 8
47 #define SCHEMA_MIN_SUP 7
50 struct pakfire_ctx
* ctx
;
51 struct pakfire
* pakfire
;
59 time_t last_modified_at
;
62 static void logging_callback(void* data
, int r
, const char* msg
) {
63 struct pakfire_ctx
* ctx
= data
;
65 CTX_ERROR(ctx
, "Database Error: %s: %s\n",
66 sqlite3_errstr(r
), msg
);
69 static int pakfire_db_check_table(struct pakfire_db
* db
, const char* table
) {
70 sqlite3_stmt
* stmt
= NULL
;
73 const char* sql
= "SELECT * FROM sqlite_master WHERE type = ? AND name = ?";
75 // Prepare the statement
76 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
78 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
79 sql
, sqlite3_errmsg(db
->handle
));
84 r
= sqlite3_bind_text(stmt
, 1, "table", -1, NULL
);
86 CTX_ERROR(db
->ctx
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
91 r
= sqlite3_bind_text(stmt
, 2, table
, -1, NULL
);
93 CTX_ERROR(db
->ctx
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
97 // Execute the statement
99 r
= sqlite3_step(stmt
);
100 } while (r
== SQLITE_BUSY
);
102 // We should have read a row
106 // If we get here, the table exists.
111 sqlite3_finalize(stmt
);
116 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
117 sqlite3_stmt
* stmt
= NULL
;
118 sqlite3_value
* val
= NULL
;
121 // If the schema has not been initialized, yet, check if the settings table exist, at all
123 r
= pakfire_db_check_table(db
, "settings");
128 const char* sql
= "SELECT val FROM settings WHERE key = ?";
130 // Prepare the statement
131 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
132 if (r
!= SQLITE_OK
) {
133 //CTX_ERROR(db->ctx, "Could not prepare SQL statement: %s: %s\n",
134 // sql, sqlite3_errmsg(db->handle));
139 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
140 if (r
!= SQLITE_OK
) {
141 CTX_ERROR(db
->ctx
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
145 // Execute the statement
147 r
= sqlite3_step(stmt
);
148 } while (r
== SQLITE_BUSY
);
150 // We should have read a row
155 val
= sqlite3_column_value(stmt
, 0);
157 CTX_ERROR(db
->ctx
, "Could not read value\n");
161 // Copy value onto the heap
162 val
= sqlite3_value_dup(val
);
166 sqlite3_finalize(stmt
);
171 static char* pakfire_db_get_string(struct pakfire_db
* db
, const char* key
) {
174 // Fetch the value from the database
175 sqlite3_value
* value
= pakfire_db_get(db
, key
);
179 // Extract the value as string
180 const char* p
= (const char*)sqlite3_value_text(value
);
184 // Copy string to heap
189 sqlite3_value_free(value
);
194 static int pakfire_db_set_string(struct pakfire_db
* db
, const char* key
, const char* val
) {
195 sqlite3_stmt
* stmt
= NULL
;
198 CTX_DEBUG(db
->ctx
, "Setting %s to '%s'\n", key
, val
);
200 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
201 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
204 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
205 if (r
!= SQLITE_OK
) {
206 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
207 sql
, sqlite3_errmsg(db
->handle
));
212 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
213 if (r
!= SQLITE_OK
) {
214 CTX_ERROR(db
->ctx
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
219 r
= sqlite3_bind_text(stmt
, 2, val
, strlen(val
), NULL
);
220 if (r
!= SQLITE_OK
) {
221 CTX_ERROR(db
->ctx
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
225 // Execute the statement
227 r
= sqlite3_step(stmt
);
228 } while (r
== SQLITE_BUSY
);
231 r
= (r
== SQLITE_OK
);
235 sqlite3_finalize(stmt
);
240 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
241 sqlite3_stmt
* stmt
= NULL
;
244 CTX_DEBUG(db
->ctx
, "Setting %s to '%d'\n", key
, val
);
246 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
247 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
250 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
251 if (r
!= SQLITE_OK
) {
252 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
253 sql
, sqlite3_errmsg(db
->handle
));
258 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
259 if (r
!= SQLITE_OK
) {
260 CTX_ERROR(db
->ctx
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
265 r
= sqlite3_bind_int64(stmt
, 2, val
);
266 if (r
!= SQLITE_OK
) {
267 CTX_ERROR(db
->ctx
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
271 // Execute the statement
273 r
= sqlite3_step(stmt
);
274 } while (r
== SQLITE_BUSY
);
277 r
= (r
== SQLITE_OK
);
281 sqlite3_finalize(stmt
);
286 static time_t pakfire_read_modification_time(struct pakfire_db
* db
) {
289 // Fetch the value from the database
290 sqlite3_value
* value
= pakfire_db_get(db
, "last_modified_at");
292 t
= sqlite3_value_int64(value
);
293 sqlite3_value_free(value
);
295 CTX_DEBUG(db
->ctx
, "Could not find last modification timestamp\n");
301 static int pakfire_update_modification_time(struct pakfire_db
* db
) {
302 // Get the current time in UTC
303 time_t t
= time(NULL
);
305 // Store it in the database
306 int r
= pakfire_db_set_int(db
, "last_modified_at", t
);
310 // Update the last modification timestamp
311 db
->last_modified_at
= t
;
316 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
319 CTX_DEBUG(db
->ctx
, "Executing database query: %s\n", stmt
);
322 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
323 } while (r
== SQLITE_BUSY
);
327 CTX_ERROR(db
->ctx
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
333 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
334 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
337 static int pakfire_db_commit(struct pakfire_db
* db
) {
339 If the database was opened in read-write mode, we will store the
340 timestamp of the latest modification to compare whether the database
341 has been changed mid-transaction.
343 if (db
->mode
== PAKFIRE_DB_READWRITE
) {
344 int r
= pakfire_update_modification_time(db
);
349 return pakfire_db_execute(db
, "COMMIT");
352 static int pakfire_db_rollback(struct pakfire_db
* db
) {
353 return pakfire_db_execute(db
, "ROLLBACK");
357 This function performs any fast optimization and tries to truncate the WAL log file
358 to keep the database as compact as possible on disk.
360 static void pakfire_db_optimize(struct pakfire_db
* db
) {
361 pakfire_db_execute(db
, "PRAGMA optimize");
362 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
365 static void pakfire_db_free(struct pakfire_db
* db
) {
367 // Optimize the database before it is being closed
368 pakfire_db_optimize(db
);
370 // Close database handle
371 int r
= sqlite3_close(db
->handle
);
372 if (r
!= SQLITE_OK
) {
373 CTX_ERROR(db
->ctx
, "Could not close database handle: %s\n",
374 sqlite3_errmsg(db
->handle
));
379 pakfire_unref(db
->pakfire
);
381 pakfire_ctx_unref(db
->ctx
);
385 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
386 // Fetch the schema version
387 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
391 int schema
= sqlite3_value_int64(value
);
392 sqlite3_value_free(value
);
394 CTX_DEBUG(db
->ctx
, "Database has schema version %d\n", schema
);
399 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
402 // Create settings table
403 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
407 // settings: Add a unique index on key
408 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
412 // Create packages table
413 r
= pakfire_db_execute(db
,
414 "CREATE TABLE IF NOT EXISTS packages("
415 "id INTEGER PRIMARY KEY, "
422 "inst_size INTEGER, "
423 "digest_type INTEGER, "
431 "build_time INTEGER, "
432 "installed INTEGER, "
433 "userinstalled INTEGER, "
443 // packages: Create index to find package by name
444 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
448 // packages: Create unique index over UUID
449 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
453 // Create dependencies table
454 r
= pakfire_db_execute(db
,
455 "CREATE TABLE IF NOT EXISTS dependencies("
459 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
464 // dependencies: Add index over packages
465 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
469 // Create files table
470 r
= pakfire_db_execute(db
,
471 "CREATE TABLE IF NOT EXISTS files("
472 "id INTEGER PRIMARY KEY, "
484 "capabilities TEXT, "
485 "digest_sha2_512 BLOB, "
486 "digest_sha2_256 BLOB, "
487 "digest_blake2b512 BLOB, "
488 "digest_blake2s256 BLOB, "
489 "digest_sha3_512 BLOB, "
490 "digest_sha3_256 BLOB, "
491 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
496 // files: Add index over packages
497 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
501 // files: Add index over path
502 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
506 // Create scriptlets table
507 r
= pakfire_db_execute(db
,
508 "CREATE TABLE IF NOT EXISTS scriptlets("
509 "id INTEGER PRIMARY KEY, "
513 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
518 // scriptlets: Add index over packages
519 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
523 const char* arch
= pakfire_get_effective_arch(db
->pakfire
);
526 r
= pakfire_db_set_string(db
, "arch", arch
);
528 CTX_ERROR(db
->ctx
, "Could not set architecture\n");
535 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
536 // packages: Drop build_id column
539 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
540 // need to recreate the whole table and rename it afterwards. Annoying.
545 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
548 while (db
->schema
< CURRENT_SCHEMA
) {
549 // Begin a new transaction
550 r
= pakfire_db_begin_transaction(db
);
554 switch (db
->schema
) {
557 r
= pakfire_db_create_schema(db
);
561 db
->schema
= CURRENT_SCHEMA
;
565 r
= pakfire_db_migrate_to_schema_8(db
);
573 CTX_ERROR(db
->ctx
, "Cannot migrate database from schema %d\n", db
->schema
);
577 // Update the schema version
578 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
583 r
= pakfire_db_commit(db
);
591 pakfire_db_rollback(db
);
596 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
599 // Fetch database architecture
600 char* db_arch
= pakfire_db_get_string(db
, "arch");
602 CTX_ERROR(db
->ctx
, "Database is of an unknown architecture\n");
606 // Fetch the running architecture
607 const char* arch
= pakfire_get_effective_arch(db
->pakfire
);
612 if (strcmp(db_arch
, arch
) == 0)
622 static int pakfire_db_setup(struct pakfire_db
* db
) {
626 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->ctx
);
628 // Enable foreign keys
629 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
631 // Make LIKE case-sensitive
632 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
634 // Fetch the current schema
635 db
->schema
= pakfire_db_get_schema(db
);
637 // Check if the schema is recent enough
638 if (db
->schema
&& db
->schema
< SCHEMA_MIN_SUP
) {
639 CTX_ERROR(db
->ctx
, "Database schema %d is not supported by this version of Pakfire\n",
644 // Read modification timestamp
645 db
->last_modified_at
= pakfire_read_modification_time(db
);
647 CTX_DEBUG(db
->ctx
, "The database was last modified at %ld\n", db
->last_modified_at
);
649 // Done when not in read-write mode
650 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
653 // Disable secure delete
654 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
656 // Set database journal to WAL
657 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
658 if (r
!= SQLITE_OK
) {
659 CTX_ERROR(db
->ctx
, "Could not set journal mode to WAL: %s\n",
660 sqlite3_errmsg(db
->handle
));
664 // Disable autocheckpoint
665 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
666 if (r
!= SQLITE_OK
) {
667 CTX_ERROR(db
->ctx
, "Could not disable autocheckpoint: %s\n",
668 sqlite3_errmsg(db
->handle
));
672 // Create or migrate schema
673 r
= pakfire_db_migrate_schema(db
);
680 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
683 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
687 // Store a reference to the context
688 o
->ctx
= pakfire_ctx(pakfire
);
690 o
->pakfire
= pakfire_ref(pakfire
);
693 int sqlite3_flags
= 0;
695 // Store mode & forward it to sqlite3
696 if (flags
& PAKFIRE_DB_READWRITE
) {
697 o
->mode
= PAKFIRE_DB_READWRITE
;
698 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
700 o
->mode
= PAKFIRE_DB_READONLY
;
701 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
705 r
= pakfire_path(o
->pakfire
, o
->path
, "%s", DATABASE_PATH
);
709 // Try to open the sqlite3 database file
710 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
711 if (r
!= SQLITE_OK
) {
712 CTX_ERROR(o
->ctx
, "Could not open database %s: %s\n",
713 o
->path
, sqlite3_errmsg(o
->handle
));
719 // Setup the database
720 r
= pakfire_db_setup(o
);
724 // Check for compatible architecture
725 r
= pakfire_db_check_arch(o
);
739 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
745 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
754 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
755 sqlite3_stmt
* stmt
= NULL
;
758 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
760 CTX_ERROR(db
->ctx
, "Could not prepare integrity check: %s\n",
761 sqlite3_errmsg(db
->handle
));
766 unsigned long errors
= 0;
770 r
= sqlite3_step(stmt
);
771 } while (r
== SQLITE_BUSY
);
773 if (r
== SQLITE_ROW
) {
774 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
776 // If the message is "ok", the database has passed the check
777 if (strcmp(error
, "ok") == 0)
780 // Increment error counter
784 CTX_ERROR(db
->ctx
, "%s\n", error
);
786 // Break on anything else
791 sqlite3_finalize(stmt
);
794 CTX_ERROR(db
->ctx
, "Database integrity check failed\n");
796 CTX_INFO(db
->ctx
, "Database integrity check passed\n");
801 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
802 sqlite3_stmt
* stmt
= NULL
;
805 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
807 CTX_ERROR(db
->ctx
, "Could not prepare foreign key check: %s\n",
808 sqlite3_errmsg(db
->handle
));
813 unsigned long errors
= 0;
817 r
= sqlite3_step(stmt
);
818 } while (r
== SQLITE_BUSY
);
820 if (r
== SQLITE_ROW
) {
821 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
822 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
823 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
824 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
826 // Increment error counter
830 CTX_ERROR(db
->ctx
, "Foreign key violation found in %s, row %lu: "
831 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
833 // Break on anything else
838 sqlite3_finalize(stmt
);
841 CTX_ERROR(db
->ctx
, "Foreign key check failed\n");
843 CTX_INFO(db
->ctx
, "Foreign key check passed\n");
849 This function performs an integrity check of the database
851 int pakfire_db_check(struct pakfire_db
* db
) {
854 // Perform integrity check
855 r
= pakfire_db_integrity_check(db
);
859 // Perform foreign key check
860 r
= pakfire_db_foreign_key_check(db
);
867 // Returns the number of packages installed
868 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
869 sqlite3_stmt
* stmt
= NULL
;
870 ssize_t packages
= -1;
872 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
874 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s\n",
875 sqlite3_errmsg(db
->handle
));
881 r
= sqlite3_step(stmt
);
882 } while (r
== SQLITE_BUSY
);
884 if (r
== SQLITE_ROW
) {
885 packages
= sqlite3_column_int64(stmt
, 0);
888 sqlite3_finalize(stmt
);
893 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
894 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
896 // Convert name to ID
897 Id id
= pool_str2id(pool
, name
, 1);
899 // Append it to pooljobs
900 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
903 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
904 sqlite3_stmt
* stmt
= NULL
;
908 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
910 // Prepare the statement
911 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
913 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
914 sql
, sqlite3_errmsg(db
->handle
));
918 for (const struct pakfire_dep
* dep
= pakfire_deps
; dep
->key
; dep
++) {
919 list
= pakfire_package_get_deps(pkg
, dep
->key
);
925 for (char** d
= list
; *d
; d
++) {
927 r
= sqlite3_bind_int64(stmt
, 1, id
);
929 CTX_ERROR(db
->ctx
, "Could not bind id: %s\n",
930 sqlite3_errmsg(db
->handle
));
935 r
= sqlite3_bind_text(stmt
, 2, dep
->name
, -1, NULL
);
937 CTX_ERROR(db
->ctx
, "Could not bind type: %s\n",
938 sqlite3_errmsg(db
->handle
));
943 r
= sqlite3_bind_text(stmt
, 3, *d
, -1, NULL
);
945 CTX_ERROR(db
->ctx
, "Could not bind dependency: %s\n",
946 sqlite3_errmsg(db
->handle
));
952 r
= sqlite3_step(stmt
);
953 } while (r
== SQLITE_BUSY
);
955 // Reset bound values
965 sqlite3_finalize(stmt
);
967 for (char** dep
= list
; *dep
; dep
++)
975 static int pakfire_db_bind_digest(struct pakfire_db
* db
, sqlite3_stmt
* stmt
, const int field
,
976 struct pakfire_file
* file
, const enum pakfire_digest_types type
) {
977 const unsigned char* digest
= NULL
;
981 digest
= pakfire_file_get_digest(file
, type
, &length
);
983 // If this digest isn't set, just bind NULL
985 return sqlite3_bind_null(stmt
, field
);
987 // Otherwise bind the data blob
988 return sqlite3_bind_blob(stmt
, field
, digest
, length
, NULL
);
991 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
992 sqlite3_stmt
* stmt
= NULL
;
995 // Get the filelist from the archive
996 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
998 CTX_ERROR(db
->ctx
, "Could not fetch filelist from archive\n");
1002 // Nothing to do if the list is empty
1003 if (pakfire_filelist_is_empty(filelist
)) {
1025 "digest_blake2b512, "
1026 "digest_blake2s256, "
1030 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
1032 // Prepare the statement
1033 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1035 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
1036 sql
, sqlite3_errmsg(db
->handle
));
1040 for (unsigned int i
= 0; i
< pakfire_filelist_length(filelist
); i
++) {
1041 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
1044 r
= sqlite3_bind_int64(stmt
, 1, id
);
1046 CTX_ERROR(db
->ctx
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1047 pakfire_file_unref(file
);
1052 const char* path
= pakfire_file_get_path(file
);
1054 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
1056 CTX_ERROR(db
->ctx
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
1057 pakfire_file_unref(file
);
1062 size_t size
= pakfire_file_get_size(file
);
1064 r
= sqlite3_bind_int64(stmt
, 3, size
);
1066 CTX_ERROR(db
->ctx
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1067 pakfire_file_unref(file
);
1071 // Bind config - XXX TODO
1072 r
= sqlite3_bind_null(stmt
, 4);
1074 CTX_ERROR(db
->ctx
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
1075 pakfire_file_unref(file
);
1079 // Bind datafile - XXX TODO
1080 r
= sqlite3_bind_null(stmt
, 5);
1082 CTX_ERROR(db
->ctx
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
1083 pakfire_file_unref(file
);
1088 mode_t mode
= pakfire_file_get_mode(file
);
1090 r
= sqlite3_bind_int64(stmt
, 6, mode
);
1092 CTX_ERROR(db
->ctx
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
1093 pakfire_file_unref(file
);
1098 const char* uname
= pakfire_file_get_uname(file
);
1100 r
= sqlite3_bind_text(stmt
, 7, uname
, -1, NULL
);
1102 CTX_ERROR(db
->ctx
, "Could not bind uname: %s\n", sqlite3_errmsg(db
->handle
));
1103 pakfire_file_unref(file
);
1108 const char* gname
= pakfire_file_get_gname(file
);
1110 r
= sqlite3_bind_text(stmt
, 8, gname
, -1, NULL
);
1112 CTX_ERROR(db
->ctx
, "Could not bind gname: %s\n", sqlite3_errmsg(db
->handle
));
1113 pakfire_file_unref(file
);
1118 time_t ctime
= pakfire_file_get_ctime(file
);
1120 r
= sqlite3_bind_int64(stmt
, 9, ctime
);
1122 CTX_ERROR(db
->ctx
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
1123 pakfire_file_unref(file
);
1128 time_t mtime
= pakfire_file_get_mtime(file
);
1130 r
= sqlite3_bind_int64(stmt
, 10, mtime
);
1132 CTX_ERROR(db
->ctx
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
1133 pakfire_file_unref(file
);
1138 const char* mimetype
= pakfire_file_get_mimetype(file
);
1141 r
= sqlite3_bind_text(stmt
, 11, mimetype
, -1, NULL
);
1143 CTX_ERROR(db
->ctx
, "Could not bind MIME type: %s\n",
1144 sqlite3_errmsg(db
->handle
));
1145 pakfire_file_unref(file
);
1149 r
= sqlite3_bind_null(stmt
, 11);
1151 CTX_ERROR(db
->ctx
, "Could not bind an empty MIME type: %s\n",
1152 sqlite3_errmsg(db
->handle
));
1153 pakfire_file_unref(file
);
1158 // Bind capabilities - XXX TODO
1159 r
= sqlite3_bind_null(stmt
, 12);
1161 CTX_ERROR(db
->ctx
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1162 pakfire_file_unref(file
);
1167 r
= pakfire_db_bind_digest(db
, stmt
, 13, file
, PAKFIRE_DIGEST_SHA2_512
);
1169 CTX_ERROR(db
->ctx
, "Could not bind SHA2-512 digest: %s\n",
1170 sqlite3_errmsg(db
->handle
));
1171 pakfire_file_unref(file
);
1176 r
= pakfire_db_bind_digest(db
, stmt
, 14, file
, PAKFIRE_DIGEST_SHA2_256
);
1178 CTX_ERROR(db
->ctx
, "Could not bind SHA2-256 digest: %s\n",
1179 sqlite3_errmsg(db
->handle
));
1180 pakfire_file_unref(file
);
1184 // BLAKE2b512 Digest
1185 r
= pakfire_db_bind_digest(db
, stmt
, 15, file
, PAKFIRE_DIGEST_BLAKE2B512
);
1187 CTX_ERROR(db
->ctx
, "Could not bind BLAKE2b512 digest: %s\n",
1188 sqlite3_errmsg(db
->handle
));
1189 pakfire_file_unref(file
);
1193 // BLAKE2s256 Digest
1194 r
= pakfire_db_bind_digest(db
, stmt
, 16, file
, PAKFIRE_DIGEST_BLAKE2S256
);
1196 CTX_ERROR(db
->ctx
, "Could not bind BLAKE2s256 digest: %s\n",
1197 sqlite3_errmsg(db
->handle
));
1198 pakfire_file_unref(file
);
1203 r
= pakfire_db_bind_digest(db
, stmt
, 17, file
, PAKFIRE_DIGEST_SHA3_512
);
1205 CTX_ERROR(db
->ctx
, "Could not bind SHA3-512 digest: %s\n",
1206 sqlite3_errmsg(db
->handle
));
1207 pakfire_file_unref(file
);
1212 r
= pakfire_db_bind_digest(db
, stmt
, 18, file
, PAKFIRE_DIGEST_SHA3_256
);
1214 CTX_ERROR(db
->ctx
, "Could not bind SHA3-256 digest: %s\n",
1215 sqlite3_errmsg(db
->handle
));
1216 pakfire_file_unref(file
);
1222 r
= sqlite3_step(stmt
);
1223 } while (r
== SQLITE_BUSY
);
1226 if (r
!= SQLITE_DONE
) {
1227 CTX_ERROR(db
->ctx
, "Could not add file to database: %s\n",
1228 sqlite3_errmsg(db
->handle
));
1229 pakfire_file_unref(file
);
1233 // Move on to next file
1234 pakfire_file_unref(file
);
1236 // Reset bound values
1237 sqlite3_reset(stmt
);
1245 sqlite3_finalize(stmt
);
1246 pakfire_filelist_unref(filelist
);
1251 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1252 sqlite3_stmt
* stmt
= NULL
;
1256 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1258 // Prepare the statement
1259 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1261 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
1262 sql
, sqlite3_errmsg(db
->handle
));
1266 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1267 // Fetch the scriptlet
1268 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1273 r
= sqlite3_bind_int64(stmt
, 1, id
);
1275 CTX_ERROR(db
->ctx
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1276 pakfire_scriptlet_unref(scriptlet
);
1281 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1283 CTX_ERROR(db
->ctx
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1284 pakfire_scriptlet_unref(scriptlet
);
1288 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1291 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1293 CTX_ERROR(db
->ctx
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1294 pakfire_scriptlet_unref(scriptlet
);
1300 r
= sqlite3_step(stmt
);
1301 } while (r
== SQLITE_BUSY
);
1304 if (r
!= SQLITE_DONE
) {
1305 CTX_ERROR(db
->ctx
, "Could not add scriptlet to database: %s\n",
1306 sqlite3_errmsg(db
->handle
));
1307 pakfire_scriptlet_unref(scriptlet
);
1311 pakfire_scriptlet_unref(scriptlet
);
1313 // Reset bound values
1314 sqlite3_reset(stmt
);
1322 sqlite3_finalize(stmt
);
1327 int pakfire_db_add_package(struct pakfire_db
* db
,
1328 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1329 sqlite3_stmt
* stmt
= NULL
;
1332 char** groups
= NULL
;
1333 char* __groups
= NULL
;
1335 // Begin a new transaction
1336 r
= pakfire_db_begin_transaction(db
);
1340 const char* sql
= "INSERT INTO "
1382 "CURRENT_TIMESTAMP, "
1391 // Prepare the statement
1392 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1393 if (r
!= SQLITE_OK
) {
1394 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s: %s\n",
1395 sql
, sqlite3_errmsg(db
->handle
));
1400 const char* name
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_NAME
);
1402 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1404 CTX_ERROR(db
->ctx
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1409 const char* evr
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_EVR
);
1411 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1413 CTX_ERROR(db
->ctx
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1418 const char* arch
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_ARCH
);
1420 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1422 CTX_ERROR(db
->ctx
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1427 groups
= pakfire_package_get_strings(pkg
, PAKFIRE_PKG_GROUPS
);
1429 // Join everything together as SQLite doesn't support arrays
1430 __groups
= pakfire_string_join(groups
, " ");
1436 r
= sqlite3_bind_text(stmt
, 4, __groups
, -1, NULL
);
1438 CTX_ERROR(db
->ctx
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1444 r
= sqlite3_bind_null(stmt
, 4);
1450 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
1452 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1454 CTX_ERROR(db
->ctx
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1459 unsigned long long size
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_DOWNLOADSIZE
, 0);
1461 r
= sqlite3_bind_int64(stmt
, 6, size
);
1463 CTX_ERROR(db
->ctx
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1467 // Bind installed size
1468 unsigned long long inst_size
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_INSTALLSIZE
, 0);
1470 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1472 CTX_ERROR(db
->ctx
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1476 const unsigned char* digest
= NULL
;
1477 enum pakfire_digest_types digest_type
= 0;
1478 size_t digest_length
= 0;
1481 digest
= pakfire_package_get_digest(pkg
, &digest_type
, &digest_length
);
1483 CTX_ERROR(db
->ctx
, "Could not fetch the package's digest: %m\n");
1488 // Set the digest type
1489 r
= sqlite3_bind_int64(stmt
, 8, digest_type
);
1491 CTX_ERROR(db
->ctx
, "Could not bind digest type: %s\n", sqlite3_errmsg(db
->handle
));
1496 r
= sqlite3_bind_blob(stmt
, 9, digest
, digest_length
, NULL
);
1498 CTX_ERROR(db
->ctx
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1503 const char* license
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_LICENSE
);
1505 r
= sqlite3_bind_text(stmt
, 10, license
, -1, NULL
);
1507 CTX_ERROR(db
->ctx
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1512 const char* summary
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SUMMARY
);
1514 r
= sqlite3_bind_text(stmt
, 11, summary
, -1, NULL
);
1516 CTX_ERROR(db
->ctx
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1521 const char* description
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_DESCRIPTION
);
1523 r
= sqlite3_bind_text(stmt
, 12, description
, -1, NULL
);
1525 CTX_ERROR(db
->ctx
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1530 const char* uuid
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_UUID
);
1532 r
= sqlite3_bind_text(stmt
, 13, uuid
, -1, NULL
);
1534 CTX_ERROR(db
->ctx
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1539 const char* vendor
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_VENDOR
);
1541 r
= sqlite3_bind_text(stmt
, 14, vendor
, -1, NULL
);
1543 CTX_ERROR(db
->ctx
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1548 const char* build_host
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_BUILD_HOST
);
1550 r
= sqlite3_bind_text(stmt
, 15, build_host
, -1, NULL
);
1552 CTX_ERROR(db
->ctx
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1557 time_t build_time
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_BUILD_TIME
, 0);
1559 r
= sqlite3_bind_int64(stmt
, 16, build_time
);
1561 CTX_ERROR(db
->ctx
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1565 // Bind repository name
1566 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1568 const char* repo_name
= pakfire_repo_get_name(repo
);
1569 pakfire_repo_unref(repo
);
1571 r
= sqlite3_bind_text(stmt
, 17, repo_name
, -1, NULL
);
1577 r
= sqlite3_bind_null(stmt
, 17);
1582 // installed by the user?
1583 r
= sqlite3_bind_int(stmt
, 18, userinstalled
);
1585 CTX_ERROR(db
->ctx
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1589 // Source package name
1590 const char* source_name
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
);
1592 r
= sqlite3_bind_text(stmt
, 19, source_name
, -1, NULL
);
1596 r
= sqlite3_bind_null(stmt
, 19);
1602 const char* source_evr
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
);
1604 r
= sqlite3_bind_text(stmt
, 20, source_evr
, -1, NULL
);
1608 r
= sqlite3_bind_null(stmt
, 20);
1614 const char* source_arch
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
);
1616 r
= sqlite3_bind_text(stmt
, 21, source_arch
, -1, NULL
);
1620 r
= sqlite3_bind_null(stmt
, 21);
1626 const char* distribution
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_DISTRO
);
1628 r
= sqlite3_bind_text(stmt
, 22, distribution
, -1, NULL
);
1632 r
= sqlite3_bind_null(stmt
, 22);
1639 r
= sqlite3_step(stmt
);
1640 } while (r
== SQLITE_BUSY
);
1642 if (r
!= SQLITE_DONE
) {
1643 CTX_ERROR(db
->ctx
, "Could not add package to database: %s\n",
1644 sqlite3_errmsg(db
->handle
));
1650 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1653 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1658 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1663 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1669 pakfire_strings_free(groups
);
1673 sqlite3_finalize(stmt
);
1675 // Commit or rollback
1677 pakfire_db_rollback(db
);
1679 r
= pakfire_db_commit(db
);
1684 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1685 sqlite3_stmt
* stmt
= NULL
;
1688 // Begin a new transaction
1689 r
= pakfire_db_begin_transaction(db
);
1693 // Fetch the package's UUID
1694 const char* uuid
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_UUID
);
1696 CTX_ERROR(db
->ctx
, "Package has no UUID\n");
1701 r
= sqlite3_prepare_v2(db
->handle
,
1702 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1704 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s\n",
1705 sqlite3_errmsg(db
->handle
));
1710 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1712 CTX_ERROR(db
->ctx
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1718 r
= sqlite3_step(stmt
);
1719 } while (r
== SQLITE_BUSY
);
1721 // Check if we have been successful
1722 if (r
!= SQLITE_DONE
) {
1723 CTX_ERROR(db
->ctx
, "Could not delete package %s\n", uuid
);
1733 sqlite3_finalize(stmt
);
1735 // Commit or rollback
1737 pakfire_db_rollback(db
);
1739 r
= pakfire_db_commit(db
);
1744 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1745 struct pakfire_package
* pkg
, const char* type
) {
1746 struct pakfire_scriptlet
* scriptlet
= NULL
;
1747 sqlite3_stmt
* stmt
= NULL
;
1750 // Fetch the package's UUID
1751 const char* uuid
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_UUID
);
1753 CTX_ERROR(db
->ctx
, "Package has no UUID\n");
1757 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1758 JOIN scriptlets ON packages.id = scriptlets.pkg \
1759 WHERE packages.uuid = ? AND scriptlets.type = ?";
1761 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1763 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s %s\n",
1764 sql
, sqlite3_errmsg(db
->handle
));
1769 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1771 CTX_ERROR(db
->ctx
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1775 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1777 CTX_ERROR(db
->ctx
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1781 CTX_DEBUG(db
->ctx
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1785 r
= sqlite3_step(stmt
);
1786 } while (r
== SQLITE_BUSY
);
1788 // We have some payload
1789 if (r
== SQLITE_ROW
) {
1790 const void* data
= sqlite3_column_blob(stmt
, 1);
1791 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1793 // Create a scriptlet object
1794 r
= pakfire_scriptlet_create(&scriptlet
, db
->ctx
, type
, data
, size
);
1801 sqlite3_finalize(stmt
);
1806 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1807 struct pakfire_package
* pkg
= NULL
;
1811 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1813 CTX_ERROR(db
->ctx
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1818 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1820 CTX_ERROR(db
->ctx
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1825 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1827 CTX_ERROR(db
->ctx
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1832 r
= pakfire_package_create(&pkg
, db
->pakfire
, repo
, name
, evr
, arch
);
1834 CTX_ERROR(db
->ctx
, "Could not create package '%s-%s.%s': %m\n", name
, evr
, arch
);
1839 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1841 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_DBID
, id
);
1847 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1849 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_GROUPS
, groups
);
1855 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1857 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_FILENAME
, filename
);
1863 size_t size
= sqlite3_column_int64(stmt
, 6);
1865 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_DOWNLOADSIZE
, size
);
1871 size
= sqlite3_column_int64(stmt
, 7);
1873 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_INSTALLSIZE
, size
);
1879 enum pakfire_digest_types digest_type
= sqlite3_column_int64(stmt
, 8);
1880 size_t digest_length
= 0;
1885 const unsigned char* digest
= sqlite3_column_blob(stmt
, 9);
1886 if (digest_type
&& digest
) {
1887 pakfire_package_set_digest(pkg
, digest_type
, digest
, digest_length
);
1891 const char* license
= (const char*)sqlite3_column_text(stmt
, 10);
1893 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_LICENSE
, license
);
1899 const char* summary
= (const char*)sqlite3_column_text(stmt
, 11);
1901 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SUMMARY
, summary
);
1907 const char* description
= (const char*)sqlite3_column_text(stmt
, 12);
1909 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DESCRIPTION
, description
);
1915 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 13);
1917 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_UUID
, uuid
);
1923 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 14);
1925 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_VENDOR
, vendor
);
1931 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 15);
1933 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_BUILD_HOST
, build_host
);
1939 time_t build_time
= sqlite3_column_int64(stmt
, 16);
1941 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_BUILD_TIME
, build_time
);
1947 time_t install_time
= sqlite3_column_int64(stmt
, 17);
1949 r
= pakfire_package_set_num(pkg
, PAKFIRE_PKG_INSTALLTIME
, install_time
);
1954 // installed by user?
1955 int userinstalled
= sqlite3_column_int(stmt
, 18);
1957 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1960 const char* files
= (const char*)sqlite3_column_text(stmt
, 19);
1962 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1969 const struct dependency
{
1971 const enum pakfire_package_key key
;
1972 } dependencies
[] = {
1973 { 20, PAKFIRE_PKG_PROVIDES
},
1974 { 21, PAKFIRE_PKG_PREREQUIRES
},
1975 { 22, PAKFIRE_PKG_REQUIRES
},
1976 { 23, PAKFIRE_PKG_CONFLICTS
},
1977 { 24, PAKFIRE_PKG_OBSOLETES
},
1978 { 25, PAKFIRE_PKG_RECOMMENDS
},
1979 { 26, PAKFIRE_PKG_SUGGESTS
},
1980 { 27, PAKFIRE_PKG_SUPPLEMENTS
},
1981 { 28, PAKFIRE_PKG_ENHANCES
},
1985 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1986 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1988 r
= pakfire_str2deps(db
->ctx
, pkg
, deps
->key
, relations
);
1995 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 29);
1997 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
2003 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 30);
2005 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
2011 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 31);
2013 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, source_arch
);
2019 const char* distribution
= (const char*)sqlite3_column_text(stmt
, 32);
2021 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DESCRIPTION
, description
);
2031 pakfire_package_unref(pkg
);
2036 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
2037 sqlite3_stmt
* stmt
= NULL
;
2040 CTX_DEBUG(db
->ctx
, "Loading package database...\n");
2042 // Drop contents of the repository
2043 pakfire_repo_clear(repo
);
2045 // Save starting time
2046 clock_t t_start
= clock();
2068 "strftime('%s', installed) AS installed, "
2071 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
2074 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2075 "WHERE d.pkg = packages.id AND d.type = 'provides'"
2078 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2079 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
2080 ") AS prerequires, "
2082 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2083 "WHERE d.pkg = packages.id AND d.type = 'requires'"
2086 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2087 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
2090 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2091 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
2094 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2095 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
2098 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2099 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
2102 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2103 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
2104 ") AS supplements, "
2106 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2107 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
2117 // Prepare the statement
2118 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2120 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s %s\n",
2121 sql
, sqlite3_errmsg(db
->handle
));
2127 r
= sqlite3_step(stmt
);
2130 // Retry if the database was busy
2136 r
= pakfire_db_load_package(db
, repo
, stmt
);
2141 // All rows have been processed
2145 // Go to error in any other cases
2152 // Save time when we finished
2155 CTX_DEBUG(db
->ctx
, "Loading package database completed in %.4fms\n",
2156 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
2158 // Mark repository as changed
2159 pakfire_repo_has_changed(repo
);
2166 CTX_ERROR(db
->ctx
, "Failed reading package database: %m\n");
2167 pakfire_repo_clear(repo
);
2171 sqlite3_finalize(stmt
);
2176 static int pakfire_db_load_file_digest(struct pakfire_db
* db
, struct pakfire_file
* file
,
2177 sqlite3_stmt
* stmt
, const enum pakfire_digest_types type
, const int field
) {
2179 const unsigned char* digest
= sqlite3_column_blob(stmt
, field
);
2181 // Nothing further to do if field is NULL
2185 // Length of the stored value
2186 const size_t length
= sqlite3_column_bytes(stmt
, field
);
2189 return pakfire_file_set_digest(file
, type
, digest
, length
);
2192 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2193 sqlite3_stmt
* stmt
) {
2194 struct pakfire_file
* file
= NULL
;
2195 char abspath
[PATH_MAX
];
2198 // Create a new file object
2199 r
= pakfire_file_create(&file
, db
->pakfire
);
2204 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2206 // Abort if no path is set
2208 CTX_ERROR(db
->ctx
, "File has no path\n");
2214 r
= pakfire_file_set_path(file
, path
);
2216 CTX_ERROR(db
->ctx
, "%s: Could not set path '%s': %m\n", path
, path
);
2220 // Make the absolute path
2221 r
= pakfire_path(db
->pakfire
, abspath
, "%s", path
);
2223 CTX_ERROR(db
->ctx
, "%s: Could not make absolute path: %m\n", path
);
2227 // Set the absolute path
2228 r
= pakfire_file_set_abspath(file
, abspath
);
2230 CTX_ERROR(db
->ctx
, "%s: Could not set absolute path %s: %m\n", path
, abspath
);
2235 size_t size
= sqlite3_column_int64(stmt
, 1);
2237 pakfire_file_set_size(file
, size
);
2240 mode_t mode
= sqlite3_column_int(stmt
, 2);
2242 pakfire_file_set_mode(file
, mode
);
2245 const char* uname
= (const char*)sqlite3_column_text(stmt
, 3);
2247 // Abort if no user is set
2249 CTX_ERROR(db
->ctx
, "%s: No user\n", path
);
2255 r
= pakfire_file_set_uname(file
, uname
);
2257 CTX_ERROR(db
->ctx
, "%s: Could not set user '%s': %m\n", path
, uname
);
2262 const char* gname
= (const char*)sqlite3_column_text(stmt
, 4);
2264 // Abort if no group is set
2266 CTX_ERROR(db
->ctx
, "%s: No group\n", path
);
2272 r
= pakfire_file_set_gname(file
, gname
);
2274 CTX_ERROR(db
->ctx
, "%s: Could not set group '%s': %m\n", path
, gname
);
2279 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2281 pakfire_file_set_ctime(file
, ctime
);
2284 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2286 pakfire_file_set_mtime(file
, mtime
);
2288 const char* mimetype
= (const char*)sqlite3_column_text(stmt
, 7);
2291 r
= pakfire_file_set_mimetype(file
, mimetype
);
2296 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_512
, 8);
2301 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_256
, 9);
2305 // BLAKE2b512 Digest
2306 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2B512
, 10);
2310 // BLAKE2s256 Digest
2311 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2S256
, 11);
2316 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_512
, 12);
2321 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_256
, 13);
2325 // Append the file to the filelist
2326 r
= pakfire_filelist_add(filelist
, file
);
2332 pakfire_file_unref(file
);
2337 int pakfire_db_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
) {
2338 struct pakfire_filelist
* list
= NULL
;
2339 sqlite3_stmt
* stmt
= NULL
;
2354 "digest_blake2b512, "
2355 "digest_blake2s256, "
2362 // Prepare the statement
2363 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2365 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s %s\n",
2366 sql
, sqlite3_errmsg(db
->handle
));
2370 // Create a new filelist
2371 r
= pakfire_filelist_create(&list
, db
->pakfire
);
2375 // Iterate over all rows
2378 r
= sqlite3_step(stmt
);
2381 // Retry if the database was busy
2387 r
= pakfire_db_load_file(db
, list
, stmt
);
2392 // All rows have been processed
2397 // Go to error in any other cases
2404 // Return the filelist
2405 *filelist
= pakfire_filelist_ref(list
);
2409 CTX_ERROR(db
->ctx
, "Could not fetch filelist: %m\n");
2412 sqlite3_finalize(stmt
);
2414 pakfire_filelist_unref(list
);
2419 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2420 struct pakfire_package
* pkg
) {
2421 struct pakfire_filelist
* fl
= NULL
;
2422 sqlite3_stmt
* stmt
= NULL
;
2425 // Fetch the package ID
2426 uint64_t id
= pakfire_package_get_num(pkg
, PAKFIRE_PKG_DBID
, 0);
2428 CTX_ERROR(db
->ctx
, "Package did not have an ID\n");
2432 // Create a new filelist
2433 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2435 CTX_ERROR(db
->ctx
, "Could not create filelist: %m\n");
2451 "digest_blake2b512, "
2452 "digest_blake2s256, "
2457 // Select all files that belong to this package
2461 // Filter out any files that are also in a different package (i.e. an update
2462 // that has already been installed and this is the cleanup of the obsolete pkg)
2467 "FROM files AS duplicates "
2469 "files.path = duplicates.path "
2471 "files.pkg != duplicates.pkg"
2474 // Return ordered by path
2478 // Prepare the statement
2479 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2481 CTX_ERROR(db
->ctx
, "Could not prepare SQL statement: %s %s\n",
2482 sql
, sqlite3_errmsg(db
->handle
));
2487 r
= sqlite3_bind_int64(stmt
, 1, id
);
2489 CTX_ERROR(db
->ctx
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2495 r
= sqlite3_step(stmt
);
2498 // Retry if the database was busy
2504 r
= pakfire_db_load_file(db
, fl
, stmt
);
2509 // All rows have been processed
2513 // Go to error in any other cases
2520 *filelist
= pakfire_filelist_ref(fl
);
2525 sqlite3_finalize(stmt
);
2527 pakfire_filelist_unref(fl
);