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, "
364 "digest_type INTEGER, "
372 "build_time INTEGER, "
373 "installed INTEGER, "
374 "userinstalled INTEGER, "
384 // packages: Create index to find package by name
385 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
389 // packages: Create unique index over UUID
390 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
394 // Create dependencies table
395 r
= pakfire_db_execute(db
,
396 "CREATE TABLE IF NOT EXISTS dependencies("
400 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
405 // dependencies: Add index over packages
406 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
410 // Create files table
411 r
= pakfire_db_execute(db
,
412 "CREATE TABLE IF NOT EXISTS files("
413 "id INTEGER PRIMARY KEY, "
424 "capabilities TEXT, "
425 "digest_sha2_512 BLOB, "
426 "digest_sha2_256 BLOB, "
427 "digest_blake2b512 BLOB, "
428 "digest_blake2s256 BLOB, "
429 "digest_sha3_512 BLOB, "
430 "digest_sha3_256 BLOB, "
431 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
436 // files: Add index over packages
437 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
441 // files: Add index over path
442 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
446 // Create scriptlets table
447 r
= pakfire_db_execute(db
,
448 "CREATE TABLE IF NOT EXISTS scriptlets("
449 "id INTEGER PRIMARY KEY, "
453 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
458 // scriptlets: Add index over packages
459 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
463 const char* arch
= pakfire_get_arch(db
->pakfire
);
466 r
= pakfire_db_set_string(db
, "arch", arch
);
468 ERROR(db
->pakfire
, "Could not set architecture\n");
475 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
476 // packages: Drop build_id column
479 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
480 // need to recreate the whole table and rename it afterwards. Annoying.
485 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
488 while (db
->schema
< CURRENT_SCHEMA
) {
489 // Begin a new transaction
490 r
= pakfire_db_begin_transaction(db
);
494 switch (db
->schema
) {
497 r
= pakfire_db_create_schema(db
);
501 db
->schema
= CURRENT_SCHEMA
;
505 r
= pakfire_db_migrate_to_schema_8(db
);
513 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
517 // Update the schema version
518 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
523 r
= pakfire_db_commit(db
);
531 pakfire_db_rollback(db
);
536 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
539 // Fetch database architecture
540 char* db_arch
= pakfire_db_get_string(db
, "arch");
542 ERROR(db
->pakfire
, "Database is of an unknown architecture\n");
546 // Fetch the running architecture
547 const char* arch
= pakfire_get_arch(db
->pakfire
);
552 if (strcmp(db_arch
, arch
) == 0)
562 static int pakfire_db_setup(struct pakfire_db
* db
) {
566 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
568 // Enable foreign keys
569 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
571 // Make LIKE case-sensitive
572 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
574 // Fetch the current schema
575 db
->schema
= pakfire_db_get_schema(db
);
577 // Check if the schema is recent enough
578 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
579 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
584 // Read modification timestamp
585 db
->last_modified_at
= pakfire_read_modification_time(db
);
587 DEBUG(db
->pakfire
, "The database was last modified at %ld\n", db
->last_modified_at
);
589 // Done when not in read-write mode
590 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
593 // Disable secure delete
594 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
596 // Set database journal to WAL
597 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
598 if (r
!= SQLITE_OK
) {
599 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
600 sqlite3_errmsg(db
->handle
));
604 // Disable autocheckpoint
605 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
606 if (r
!= SQLITE_OK
) {
607 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
608 sqlite3_errmsg(db
->handle
));
612 // Create or migrate schema
613 r
= pakfire_db_migrate_schema(db
);
620 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
623 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
627 o
->pakfire
= pakfire_ref(pakfire
);
630 int sqlite3_flags
= 0;
632 // Store mode & forward it to sqlite3
633 if (flags
& PAKFIRE_DB_READWRITE
) {
634 o
->mode
= PAKFIRE_DB_READWRITE
;
635 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
637 o
->mode
= PAKFIRE_DB_READONLY
;
638 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
642 r
= pakfire_path(o
->pakfire
, o
->path
, "%s", DATABASE_PATH
);
646 // Try to open the sqlite3 database file
647 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
648 if (r
!= SQLITE_OK
) {
649 ERROR(pakfire
, "Could not open database %s: %s\n",
650 o
->path
, sqlite3_errmsg(o
->handle
));
656 // Setup the database
657 r
= pakfire_db_setup(o
);
661 // Check for compatible architecture
662 r
= pakfire_db_check_arch(o
);
676 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
682 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
691 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
692 sqlite3_stmt
* stmt
= NULL
;
695 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
697 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
698 sqlite3_errmsg(db
->handle
));
703 unsigned long errors
= 0;
707 r
= sqlite3_step(stmt
);
708 } while (r
== SQLITE_BUSY
);
710 if (r
== SQLITE_ROW
) {
711 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
713 // If the message is "ok", the database has passed the check
714 if (strcmp(error
, "ok") == 0)
717 // Increment error counter
721 ERROR(db
->pakfire
, "%s\n", error
);
723 // Break on anything else
728 sqlite3_finalize(stmt
);
731 ERROR(db
->pakfire
, "Database integrity check failed\n");
733 INFO(db
->pakfire
, "Database integrity check passed\n");
738 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
739 sqlite3_stmt
* stmt
= NULL
;
742 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
744 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
745 sqlite3_errmsg(db
->handle
));
750 unsigned long errors
= 0;
754 r
= sqlite3_step(stmt
);
755 } while (r
== SQLITE_BUSY
);
757 if (r
== SQLITE_ROW
) {
758 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
759 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
760 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
761 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
763 // Increment error counter
767 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
768 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
770 // Break on anything else
775 sqlite3_finalize(stmt
);
778 ERROR(db
->pakfire
, "Foreign key check failed\n");
780 INFO(db
->pakfire
, "Foreign key check passed\n");
786 This function performs an integrity check of the database
788 int pakfire_db_check(struct pakfire_db
* db
) {
791 // Perform integrity check
792 r
= pakfire_db_integrity_check(db
);
796 // Perform foreign key check
797 r
= pakfire_db_foreign_key_check(db
);
804 // Returns the number of packages installed
805 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
806 sqlite3_stmt
* stmt
= NULL
;
807 ssize_t packages
= -1;
809 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
811 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
812 sqlite3_errmsg(db
->handle
));
818 r
= sqlite3_step(stmt
);
819 } while (r
== SQLITE_BUSY
);
821 if (r
== SQLITE_ROW
) {
822 packages
= sqlite3_column_int64(stmt
, 0);
825 sqlite3_finalize(stmt
);
830 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
831 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
833 // Convert name to ID
834 Id id
= pool_str2id(pool
, name
, 1);
836 // Append it to pooljobs
837 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
840 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
841 sqlite3_stmt
* stmt
= NULL
;
844 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
846 // Prepare the statement
847 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
849 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
850 sql
, sqlite3_errmsg(db
->handle
));
854 const struct __relation
{
856 char** (*func
)(struct pakfire_package
*);
858 { "provides", pakfire_package_get_provides
},
859 { "prerequires", pakfire_package_get_prerequires
},
860 { "requires", pakfire_package_get_requires
},
861 { "conflicts", pakfire_package_get_conflicts
},
862 { "obsoletes", pakfire_package_get_obsoletes
},
863 { "recommends", pakfire_package_get_recommends
},
864 { "suggests", pakfire_package_get_suggests
},
865 { "supplements", pakfire_package_get_supplements
},
866 { "enhances", pakfire_package_get_enhances
},
870 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
871 char** list
= relation
->func(pkg
);
875 for (char** dep
= list
; *dep
; dep
++) {
877 r
= sqlite3_bind_int64(stmt
, 1, id
);
879 ERROR(db
->pakfire
, "Could not bind id: %s\n",
880 sqlite3_errmsg(db
->handle
));
885 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
887 ERROR(db
->pakfire
, "Could not bind type: %s\n",
888 sqlite3_errmsg(db
->handle
));
893 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
895 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
896 sqlite3_errmsg(db
->handle
));
902 r
= sqlite3_step(stmt
);
903 } while (r
== SQLITE_BUSY
);
907 // Reset bound values
919 sqlite3_finalize(stmt
);
924 static int pakfire_db_bind_digest(struct pakfire_db
* db
, sqlite3_stmt
* stmt
, const int field
,
925 struct pakfire_file
* file
, const enum pakfire_digest_types type
) {
926 const unsigned char* digest
= NULL
;
930 digest
= pakfire_file_get_digest(file
, type
, &length
);
932 // If this digest isn't set, just bind NULL
934 return sqlite3_bind_null(stmt
, field
);
936 // Otherwise bind the data blob
937 return sqlite3_bind_blob(stmt
, field
, digest
, length
, NULL
);
940 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
941 sqlite3_stmt
* stmt
= NULL
;
944 // Get the filelist from the archive
945 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
947 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
951 // Nothing to do if the list is empty
952 if (pakfire_filelist_is_empty(filelist
)) {
973 "digest_blake2b512, "
974 "digest_blake2s256, "
978 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
980 // Prepare the statement
981 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
983 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
984 sql
, sqlite3_errmsg(db
->handle
));
988 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
989 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
992 r
= sqlite3_bind_int64(stmt
, 1, id
);
994 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
995 pakfire_file_unref(file
);
1000 const char* path
= pakfire_file_get_path(file
);
1002 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
1004 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
1005 pakfire_file_unref(file
);
1010 size_t size
= pakfire_file_get_size(file
);
1012 r
= sqlite3_bind_int64(stmt
, 3, size
);
1014 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1015 pakfire_file_unref(file
);
1019 // Bind config - XXX TODO
1020 r
= sqlite3_bind_null(stmt
, 4);
1022 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
1023 pakfire_file_unref(file
);
1027 // Bind datafile - XXX TODO
1028 r
= sqlite3_bind_null(stmt
, 5);
1030 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
1031 pakfire_file_unref(file
);
1036 mode_t mode
= pakfire_file_get_mode(file
);
1038 r
= sqlite3_bind_int64(stmt
, 6, mode
);
1040 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
1041 pakfire_file_unref(file
);
1046 const char* uname
= pakfire_file_get_uname(file
);
1048 r
= sqlite3_bind_text(stmt
, 7, uname
, -1, NULL
);
1050 ERROR(db
->pakfire
, "Could not bind uname: %s\n", sqlite3_errmsg(db
->handle
));
1051 pakfire_file_unref(file
);
1056 const char* gname
= pakfire_file_get_gname(file
);
1058 r
= sqlite3_bind_text(stmt
, 8, gname
, -1, NULL
);
1060 ERROR(db
->pakfire
, "Could not bind gname: %s\n", sqlite3_errmsg(db
->handle
));
1061 pakfire_file_unref(file
);
1066 time_t ctime
= pakfire_file_get_ctime(file
);
1068 r
= sqlite3_bind_int64(stmt
, 9, ctime
);
1070 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
1071 pakfire_file_unref(file
);
1076 time_t mtime
= pakfire_file_get_mtime(file
);
1078 r
= sqlite3_bind_int64(stmt
, 10, mtime
);
1080 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
1081 pakfire_file_unref(file
);
1085 // Bind capabilities - XXX TODO
1086 r
= sqlite3_bind_null(stmt
, 11);
1088 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1089 pakfire_file_unref(file
);
1094 r
= pakfire_db_bind_digest(db
, stmt
, 12, file
, PAKFIRE_DIGEST_SHA2_512
);
1096 ERROR(db
->pakfire
, "Could not bind SHA2-512 digest: %s\n",
1097 sqlite3_errmsg(db
->handle
));
1098 pakfire_file_unref(file
);
1103 r
= pakfire_db_bind_digest(db
, stmt
, 13, file
, PAKFIRE_DIGEST_SHA2_256
);
1105 ERROR(db
->pakfire
, "Could not bind SHA2-256 digest: %s\n",
1106 sqlite3_errmsg(db
->handle
));
1107 pakfire_file_unref(file
);
1111 // BLAKE2b512 Digest
1112 r
= pakfire_db_bind_digest(db
, stmt
, 14, file
, PAKFIRE_DIGEST_BLAKE2B512
);
1114 ERROR(db
->pakfire
, "Could not bind BLAKE2b512 digest: %s\n",
1115 sqlite3_errmsg(db
->handle
));
1116 pakfire_file_unref(file
);
1120 // BLAKE2s256 Digest
1121 r
= pakfire_db_bind_digest(db
, stmt
, 15, file
, PAKFIRE_DIGEST_BLAKE2S256
);
1123 ERROR(db
->pakfire
, "Could not bind BLAKE2s256 digest: %s\n",
1124 sqlite3_errmsg(db
->handle
));
1125 pakfire_file_unref(file
);
1130 r
= pakfire_db_bind_digest(db
, stmt
, 16, file
, PAKFIRE_DIGEST_SHA3_512
);
1132 ERROR(db
->pakfire
, "Could not bind SHA3-512 digest: %s\n",
1133 sqlite3_errmsg(db
->handle
));
1134 pakfire_file_unref(file
);
1139 r
= pakfire_db_bind_digest(db
, stmt
, 17, file
, PAKFIRE_DIGEST_SHA3_256
);
1141 ERROR(db
->pakfire
, "Could not bind SHA3-256 digest: %s\n",
1142 sqlite3_errmsg(db
->handle
));
1143 pakfire_file_unref(file
);
1149 r
= sqlite3_step(stmt
);
1150 } while (r
== SQLITE_BUSY
);
1153 if (r
!= SQLITE_DONE
) {
1154 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
1155 sqlite3_errmsg(db
->handle
));
1156 pakfire_file_unref(file
);
1160 // Move on to next file
1161 pakfire_file_unref(file
);
1163 // Reset bound values
1164 sqlite3_reset(stmt
);
1172 sqlite3_finalize(stmt
);
1173 pakfire_filelist_unref(filelist
);
1178 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1179 sqlite3_stmt
* stmt
= NULL
;
1183 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1185 // Prepare the statement
1186 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1188 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1189 sql
, sqlite3_errmsg(db
->handle
));
1193 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1194 // Fetch the scriptlet
1195 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1200 r
= sqlite3_bind_int64(stmt
, 1, id
);
1202 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1203 pakfire_scriptlet_unref(scriptlet
);
1208 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1210 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1211 pakfire_scriptlet_unref(scriptlet
);
1215 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1218 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1220 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1221 pakfire_scriptlet_unref(scriptlet
);
1227 r
= sqlite3_step(stmt
);
1228 } while (r
== SQLITE_BUSY
);
1231 if (r
!= SQLITE_DONE
) {
1232 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1233 sqlite3_errmsg(db
->handle
));
1234 pakfire_scriptlet_unref(scriptlet
);
1238 pakfire_scriptlet_unref(scriptlet
);
1240 // Reset bound values
1241 sqlite3_reset(stmt
);
1249 sqlite3_finalize(stmt
);
1254 int pakfire_db_add_package(struct pakfire_db
* db
,
1255 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1256 sqlite3_stmt
* stmt
= NULL
;
1259 // Begin a new transaction
1260 r
= pakfire_db_begin_transaction(db
);
1264 const char* sql
= "INSERT INTO "
1306 "CURRENT_TIMESTAMP, "
1315 // Prepare the statement
1316 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1317 if (r
!= SQLITE_OK
) {
1318 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1319 sql
, sqlite3_errmsg(db
->handle
));
1324 const char* name
= pakfire_package_get_name(pkg
);
1326 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1328 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1333 const char* evr
= pakfire_package_get_evr(pkg
);
1335 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1337 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1342 const char* arch
= pakfire_package_get_arch(pkg
);
1344 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1346 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1351 char* groups
= pakfire_package_get_groups(pkg
);
1353 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1355 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1364 r
= sqlite3_bind_null(stmt
, 4);
1370 const char* filename
= pakfire_package_get_filename(pkg
);
1372 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1374 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1379 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1381 r
= sqlite3_bind_int64(stmt
, 6, size
);
1383 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1387 // Bind installed size
1388 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1390 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1392 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1396 const unsigned char* digest
= NULL
;
1397 enum pakfire_digest_types digest_type
= 0;
1398 size_t digest_length
= 0;
1401 digest
= pakfire_package_get_digest(pkg
, &digest_type
, &digest_length
);
1403 ERROR(db
->pakfire
, "Could not fetch the package's digest: %m\n");
1408 // Set the digest type
1409 r
= sqlite3_bind_int64(stmt
, 8, digest_type
);
1411 ERROR(db
->pakfire
, "Could not bind digest type: %s\n", sqlite3_errmsg(db
->handle
));
1416 r
= sqlite3_bind_blob(stmt
, 9, digest
, digest_length
, NULL
);
1418 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1423 const char* license
= pakfire_package_get_license(pkg
);
1425 r
= sqlite3_bind_text(stmt
, 10, license
, -1, NULL
);
1427 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1432 const char* summary
= pakfire_package_get_summary(pkg
);
1434 r
= sqlite3_bind_text(stmt
, 11, summary
, -1, NULL
);
1436 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1441 const char* description
= pakfire_package_get_description(pkg
);
1443 r
= sqlite3_bind_text(stmt
, 12, description
, -1, NULL
);
1445 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1450 const char* uuid
= pakfire_package_get_uuid(pkg
);
1452 r
= sqlite3_bind_text(stmt
, 13, uuid
, -1, NULL
);
1454 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1459 const char* vendor
= pakfire_package_get_vendor(pkg
);
1461 r
= sqlite3_bind_text(stmt
, 14, vendor
, -1, NULL
);
1463 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1468 const char* build_host
= pakfire_package_get_build_host(pkg
);
1470 r
= sqlite3_bind_text(stmt
, 15, build_host
, -1, NULL
);
1472 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1477 time_t build_time
= pakfire_package_get_build_time(pkg
);
1479 r
= sqlite3_bind_int64(stmt
, 16, build_time
);
1481 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1485 // Bind repository name
1486 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1488 const char* repo_name
= pakfire_repo_get_name(repo
);
1489 pakfire_repo_unref(repo
);
1491 r
= sqlite3_bind_text(stmt
, 17, repo_name
, -1, NULL
);
1497 r
= sqlite3_bind_null(stmt
, 17);
1502 // installed by the user?
1503 r
= sqlite3_bind_int(stmt
, 18, userinstalled
);
1505 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1509 // Source package name
1510 const char* source_name
= pakfire_package_get_source_name(pkg
);
1512 r
= sqlite3_bind_text(stmt
, 19, source_name
, -1, NULL
);
1516 r
= sqlite3_bind_null(stmt
, 19);
1522 const char* source_evr
= pakfire_package_get_source_evr(pkg
);
1524 r
= sqlite3_bind_text(stmt
, 20, source_evr
, -1, NULL
);
1528 r
= sqlite3_bind_null(stmt
, 20);
1534 const char* source_arch
= pakfire_package_get_source_arch(pkg
);
1536 r
= sqlite3_bind_text(stmt
, 21, source_arch
, -1, NULL
);
1540 r
= sqlite3_bind_null(stmt
, 21);
1546 const char* distribution
= pakfire_package_get_distribution(pkg
);
1548 r
= sqlite3_bind_text(stmt
, 22, distribution
, -1, NULL
);
1552 r
= sqlite3_bind_null(stmt
, 22);
1559 r
= sqlite3_step(stmt
);
1560 } while (r
== SQLITE_BUSY
);
1562 if (r
!= SQLITE_DONE
) {
1563 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1564 sqlite3_errmsg(db
->handle
));
1570 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1573 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1578 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1583 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1589 sqlite3_finalize(stmt
);
1591 // Commit or rollback
1593 pakfire_db_rollback(db
);
1595 r
= pakfire_db_commit(db
);
1600 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1601 sqlite3_stmt
* stmt
= NULL
;
1604 // Begin a new transaction
1605 r
= pakfire_db_begin_transaction(db
);
1609 // Fetch the package's UUID
1610 const char* uuid
= pakfire_package_get_uuid(pkg
);
1612 ERROR(db
->pakfire
, "Package has no UUID\n");
1617 r
= sqlite3_prepare_v2(db
->handle
,
1618 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1620 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1621 sqlite3_errmsg(db
->handle
));
1626 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1628 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1634 r
= sqlite3_step(stmt
);
1635 } while (r
== SQLITE_BUSY
);
1637 // Check if we have been successful
1638 if (r
!= SQLITE_DONE
) {
1639 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1649 sqlite3_finalize(stmt
);
1651 // Commit or rollback
1653 pakfire_db_rollback(db
);
1655 r
= pakfire_db_commit(db
);
1660 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1661 struct pakfire_package
* pkg
, const char* type
) {
1662 struct pakfire_scriptlet
* scriptlet
= NULL
;
1663 sqlite3_stmt
* stmt
= NULL
;
1666 // Fetch the package's UUID
1667 const char* uuid
= pakfire_package_get_uuid(pkg
);
1669 ERROR(db
->pakfire
, "Package has no UUID\n");
1673 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1674 JOIN scriptlets ON packages.id = scriptlets.pkg \
1675 WHERE packages.uuid = ? AND scriptlets.type = ?";
1677 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1679 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1680 sql
, sqlite3_errmsg(db
->handle
));
1685 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1687 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1691 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1693 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1697 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1701 r
= sqlite3_step(stmt
);
1702 } while (r
== SQLITE_BUSY
);
1704 // We have some payload
1705 if (r
== SQLITE_ROW
) {
1706 const void* data
= sqlite3_column_blob(stmt
, 1);
1707 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1709 // Create a scriptlet object
1710 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1717 sqlite3_finalize(stmt
);
1722 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1723 struct pakfire_package
* pkg
= NULL
;
1727 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1729 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1734 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1736 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1741 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1743 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1748 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1750 ERROR(db
->pakfire
, "Could not create package\n");
1755 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1757 pakfire_package_set_dbid(pkg
, id
);
1760 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1762 pakfire_package_set_groups(pkg
, groups
);
1766 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1768 pakfire_package_set_filename(pkg
, filename
);
1772 size_t size
= sqlite3_column_int64(stmt
, 6);
1774 pakfire_package_set_downloadsize(pkg
, size
);
1778 size
= sqlite3_column_int64(stmt
, 7);
1780 pakfire_package_set_installsize(pkg
, size
);
1784 enum pakfire_digest_types digest_type
= sqlite3_column_int64(stmt
, 8);
1785 size_t digest_length
= 0;
1790 const unsigned char* digest
= sqlite3_column_blob(stmt
, 9);
1791 if (digest_type
&& digest
) {
1792 pakfire_package_set_digest(pkg
, digest_type
, digest
, digest_length
);
1796 const char* license
= (const char*)sqlite3_column_text(stmt
, 10);
1798 pakfire_package_set_license(pkg
, license
);
1802 const char* summary
= (const char*)sqlite3_column_text(stmt
, 11);
1804 pakfire_package_set_summary(pkg
, summary
);
1808 const char* description
= (const char*)sqlite3_column_text(stmt
, 12);
1810 pakfire_package_set_description(pkg
, description
);
1814 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 13);
1816 pakfire_package_set_uuid(pkg
, uuid
);
1820 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 14);
1822 pakfire_package_set_vendor(pkg
, vendor
);
1826 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 15);
1828 pakfire_package_set_build_host(pkg
, build_host
);
1832 time_t build_time
= sqlite3_column_int64(stmt
, 16);
1834 pakfire_package_set_build_time(pkg
, build_time
);
1838 time_t install_time
= sqlite3_column_int64(stmt
, 17);
1840 pakfire_package_set_install_time(pkg
, install_time
);
1843 // installed by user?
1844 int userinstalled
= sqlite3_column_int(stmt
, 18);
1846 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1849 const char* files
= (const char*)sqlite3_column_text(stmt
, 19);
1851 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1858 const struct dependency
{
1860 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1861 } dependencies
[] = {
1862 { 20, pakfire_package_add_provides
},
1863 { 21, pakfire_package_add_prerequires
},
1864 { 22, pakfire_package_add_requires
},
1865 { 23, pakfire_package_add_conflicts
},
1866 { 24, pakfire_package_add_obsoletes
},
1867 { 25, pakfire_package_add_recommends
},
1868 { 26, pakfire_package_add_suggests
},
1869 { 27, pakfire_package_add_supplements
},
1870 { 28, pakfire_package_add_enhances
},
1874 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1875 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1877 pakfire_str2deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1882 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 29);
1884 pakfire_package_set_source_name(pkg
, source_name
);
1887 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 30);
1889 pakfire_package_set_source_evr(pkg
, source_evr
);
1892 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 31);
1894 pakfire_package_set_source_arch(pkg
, source_arch
);
1897 const char* distribution
= (const char*)sqlite3_column_text(stmt
, 32);
1899 pakfire_package_set_distribution(pkg
, distribution
);
1906 pakfire_package_unref(pkg
);
1911 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1912 sqlite3_stmt
* stmt
= NULL
;
1915 DEBUG(db
->pakfire
, "Loading package database...\n");
1917 // Drop contents of the repository
1918 pakfire_repo_clear(repo
);
1920 // Save starting time
1921 clock_t t_start
= clock();
1943 "strftime('%s', installed) AS installed, "
1946 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1949 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1950 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1953 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1954 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1955 ") AS prerequires, "
1957 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1958 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1961 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1962 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1965 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1966 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1969 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1970 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1973 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1974 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1977 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1978 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1979 ") AS supplements, "
1981 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1982 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1992 // Prepare the statement
1993 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1995 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1996 sql
, sqlite3_errmsg(db
->handle
));
2002 r
= sqlite3_step(stmt
);
2005 // Retry if the database was busy
2011 r
= pakfire_db_load_package(db
, repo
, stmt
);
2016 // All rows have been processed
2020 // Go to error in any other cases
2027 // Save time when we finished
2030 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
2031 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
2033 // Mark repository as changed
2034 pakfire_repo_has_changed(repo
);
2041 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
2042 pakfire_repo_clear(repo
);
2046 sqlite3_finalize(stmt
);
2051 static int pakfire_db_load_file_digest(struct pakfire_db
* db
, struct pakfire_file
* file
,
2052 sqlite3_stmt
* stmt
, const enum pakfire_digest_types type
, const int field
) {
2054 const unsigned char* digest
= sqlite3_column_blob(stmt
, field
);
2056 // Nothing further to do if field is NULL
2060 // Length of the stored value
2061 const size_t length
= sqlite3_column_bytes(stmt
, field
);
2064 return pakfire_file_set_digest(file
, type
, digest
, length
);
2067 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2068 sqlite3_stmt
* stmt
) {
2069 struct pakfire_file
* file
= NULL
;
2072 // Create a new file object
2073 r
= pakfire_file_create(&file
, db
->pakfire
);
2078 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2080 // Abort if no path is set
2082 ERROR(db
->pakfire
, "File has no path\n");
2088 r
= pakfire_file_set_path(file
, path
);
2090 ERROR(db
->pakfire
, "%s: Could not set path '%s': %m\n", path
, path
);
2095 size_t size
= sqlite3_column_int64(stmt
, 1);
2097 pakfire_file_set_size(file
, size
);
2100 mode_t mode
= sqlite3_column_int(stmt
, 2);
2102 pakfire_file_set_mode(file
, mode
);
2105 const char* uname
= (const char*)sqlite3_column_text(stmt
, 3);
2107 // Abort if no user is set
2109 ERROR(db
->pakfire
, "%s: No user\n", path
);
2115 r
= pakfire_file_set_uname(file
, uname
);
2117 ERROR(db
->pakfire
, "%s: Could not set user '%s': %m\n", path
, uname
);
2122 const char* gname
= (const char*)sqlite3_column_text(stmt
, 4);
2124 // Abort if no group is set
2126 ERROR(db
->pakfire
, "%s: No group\n", path
);
2132 r
= pakfire_file_set_gname(file
, gname
);
2134 ERROR(db
->pakfire
, "%s: Could not set group '%s': %m\n", path
, gname
);
2139 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2141 pakfire_file_set_ctime(file
, ctime
);
2144 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2146 pakfire_file_set_mtime(file
, mtime
);
2149 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_512
, 7);
2154 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA2_256
, 8);
2158 // BLAKE2b512 Digest
2159 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2B512
, 9);
2163 // BLAKE2s256 Digest
2164 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_BLAKE2S256
, 10);
2169 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_512
, 11);
2174 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA3_256
, 12);
2178 // Append the file to the filelist
2179 r
= pakfire_filelist_append(filelist
, file
);
2185 pakfire_file_unref(file
);
2190 int pakfire_db_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
) {
2191 struct pakfire_filelist
* list
= NULL
;
2192 sqlite3_stmt
* stmt
= NULL
;
2206 "digest_blake2b512, "
2207 "digest_blake2s256, "
2214 // Prepare the statement
2215 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2217 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2218 sql
, sqlite3_errmsg(db
->handle
));
2222 // Create a new filelist
2223 r
= pakfire_filelist_create(&list
, db
->pakfire
);
2227 // Iterate over all rows
2230 r
= sqlite3_step(stmt
);
2233 // Retry if the database was busy
2239 r
= pakfire_db_load_file(db
, list
, stmt
);
2244 // All rows have been processed
2249 // Go to error in any other cases
2256 // Return the filelist
2257 *filelist
= pakfire_filelist_ref(list
);
2261 ERROR(db
->pakfire
, "Could not fetch filelist: %m\n");
2264 sqlite3_finalize(stmt
);
2266 pakfire_filelist_unref(list
);
2271 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2272 struct pakfire_package
* pkg
) {
2273 struct pakfire_filelist
* fl
= NULL
;
2274 sqlite3_stmt
* stmt
= NULL
;
2277 // Fetch the package ID
2278 uint64_t id
= pakfire_package_get_dbid(pkg
);
2280 ERROR(db
->pakfire
, "Package did not have an ID\n");
2284 // Create a new filelist
2285 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2287 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
2302 "digest_blake2b512, "
2303 "digest_blake2s256, "
2308 // Select all files that belong to this package
2312 // Filter out any files that are also in a different package (i.e. an update
2313 // that has already been installed and this is the cleanup of the obsolete pkg)
2318 "FROM files AS duplicates "
2320 "files.path = duplicates.path "
2322 "files.pkg != duplicates.pkg"
2325 // Return the longest paths first
2330 // Prepare the statement
2331 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2333 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2334 sql
, sqlite3_errmsg(db
->handle
));
2339 r
= sqlite3_bind_int64(stmt
, 1, id
);
2341 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2347 r
= sqlite3_step(stmt
);
2350 // Retry if the database was busy
2356 r
= pakfire_db_load_file(db
, fl
, stmt
);
2361 // All rows have been processed
2365 // Go to error in any other cases
2372 *filelist
= pakfire_filelist_ref(fl
);
2377 sqlite3_finalize(stmt
);
2379 pakfire_filelist_unref(fl
);