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/file.h>
34 #include <pakfire/filelist.h>
35 #include <pakfire/logging.h>
36 #include <pakfire/package.h>
37 #include <pakfire/pakfire.h>
38 #include <pakfire/repo.h>
39 #include <pakfire/string.h>
40 #include <pakfire/util.h>
42 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
44 #define CURRENT_SCHEMA 8
45 #define SCHEMA_MIN_SUP 7
48 struct pakfire
* pakfire
;
56 time_t last_modified_at
;
59 static void logging_callback(void* data
, int r
, const char* msg
) {
60 struct pakfire
* pakfire
= (struct pakfire
*)data
;
62 ERROR(pakfire
, "Database Error: %s: %s\n",
63 sqlite3_errstr(r
), msg
);
66 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
67 sqlite3_stmt
* stmt
= NULL
;
68 sqlite3_value
* val
= NULL
;
71 const char* sql
= "SELECT val FROM settings WHERE key = ?";
73 // Prepare the statement
74 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
76 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
77 // sql, sqlite3_errmsg(db->handle));
82 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
84 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
88 // Execute the statement
90 r
= sqlite3_step(stmt
);
91 } while (r
== SQLITE_BUSY
);
93 // We should have read a row
98 val
= sqlite3_column_value(stmt
, 0);
100 ERROR(db
->pakfire
, "Could not read value\n");
104 // Copy value onto the heap
105 val
= sqlite3_value_dup(val
);
109 sqlite3_finalize(stmt
);
114 static char* pakfire_db_get_string(struct pakfire_db
* db
, const char* key
) {
117 // Fetch the value from the database
118 sqlite3_value
* value
= pakfire_db_get(db
, key
);
122 // Extract the value as string
123 const char* p
= (const char*)sqlite3_value_text(value
);
127 // Copy string to heap
132 sqlite3_value_free(value
);
137 static int pakfire_db_set_string(struct pakfire_db
* db
, const char* key
, const char* val
) {
138 sqlite3_stmt
* stmt
= NULL
;
141 DEBUG(db
->pakfire
, "Setting %s to '%s'\n", key
, val
);
143 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
144 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
147 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
148 if (r
!= SQLITE_OK
) {
149 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
150 sql
, sqlite3_errmsg(db
->handle
));
155 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
156 if (r
!= SQLITE_OK
) {
157 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
162 r
= sqlite3_bind_text(stmt
, 2, val
, strlen(val
), NULL
);
163 if (r
!= SQLITE_OK
) {
164 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
168 // Execute the statement
170 r
= sqlite3_step(stmt
);
171 } while (r
== SQLITE_BUSY
);
174 r
= (r
== SQLITE_OK
);
178 sqlite3_finalize(stmt
);
183 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
184 sqlite3_stmt
* stmt
= NULL
;
187 DEBUG(db
->pakfire
, "Setting %s to '%d'\n", key
, val
);
189 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
190 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
193 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
194 if (r
!= SQLITE_OK
) {
195 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
196 sql
, sqlite3_errmsg(db
->handle
));
201 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
202 if (r
!= SQLITE_OK
) {
203 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
208 r
= sqlite3_bind_int64(stmt
, 2, val
);
209 if (r
!= SQLITE_OK
) {
210 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
214 // Execute the statement
216 r
= sqlite3_step(stmt
);
217 } while (r
== SQLITE_BUSY
);
220 r
= (r
== SQLITE_OK
);
224 sqlite3_finalize(stmt
);
229 static time_t pakfire_read_modification_time(struct pakfire_db
* db
) {
232 // Fetch the value from the database
233 sqlite3_value
* value
= pakfire_db_get(db
, "last_modified_at");
235 t
= sqlite3_value_int64(value
);
236 sqlite3_value_free(value
);
238 DEBUG(db
->pakfire
, "Could not find last modification timestamp\n");
244 static int pakfire_update_modification_time(struct pakfire_db
* db
) {
245 // Get the current time in UTC
246 time_t t
= time(NULL
);
248 // Store it in the database
249 int r
= pakfire_db_set_int(db
, "last_modified_at", t
);
253 // Update the last modification timestamp
254 db
->last_modified_at
= t
;
259 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
262 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
265 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
266 } while (r
== SQLITE_BUSY
);
270 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
276 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
277 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
280 static int pakfire_db_commit(struct pakfire_db
* db
) {
282 If the database was opened in read-write mode, we will store the
283 timestamp of the latest modification to compare whether the database
284 has been changed mid-transaction.
286 if (db
->mode
== PAKFIRE_DB_READWRITE
) {
287 int r
= pakfire_update_modification_time(db
);
292 return pakfire_db_execute(db
, "COMMIT");
295 static int pakfire_db_rollback(struct pakfire_db
* db
) {
296 return pakfire_db_execute(db
, "ROLLBACK");
300 This function performs any fast optimization and tries to truncate the WAL log file
301 to keep the database as compact as possible on disk.
303 static void pakfire_db_optimize(struct pakfire_db
* db
) {
304 pakfire_db_execute(db
, "PRAGMA optimize");
305 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
308 static void pakfire_db_free(struct pakfire_db
* db
) {
310 // Optimize the database before it is being closed
311 pakfire_db_optimize(db
);
313 // Close database handle
314 int r
= sqlite3_close(db
->handle
);
315 if (r
!= SQLITE_OK
) {
316 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
317 sqlite3_errmsg(db
->handle
));
321 pakfire_unref(db
->pakfire
);
326 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
327 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
331 int schema
= sqlite3_value_int64(value
);
332 sqlite3_value_free(value
);
334 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
339 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
342 // Create settings table
343 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
347 // settings: Add a unique index on key
348 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
352 // Create packages table
353 r
= pakfire_db_execute(db
,
354 "CREATE TABLE IF NOT EXISTS packages("
355 "id INTEGER PRIMARY KEY, "
362 "inst_size INTEGER, "
370 "build_time INTEGER, "
371 "installed INTEGER, "
372 "userinstalled INTEGER, "
382 // packages: Create index to find package by name
383 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
387 // packages: Create unique index over UUID
388 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
392 // Create dependencies table
393 r
= pakfire_db_execute(db
,
394 "CREATE TABLE IF NOT EXISTS dependencies("
398 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
403 // dependencies: Add index over packages
404 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
408 // Create files table
409 r
= pakfire_db_execute(db
,
410 "CREATE TABLE IF NOT EXISTS files("
411 "id INTEGER PRIMARY KEY, "
422 "capabilities TEXT, "
423 "digest_sha512 BLOB, "
424 "digest_sha256 BLOB, "
425 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
430 // files: Add index over packages
431 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
435 // files: Add index over path
436 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
440 // Create scriptlets table
441 r
= pakfire_db_execute(db
,
442 "CREATE TABLE IF NOT EXISTS scriptlets("
443 "id INTEGER PRIMARY KEY, "
447 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
452 // scriptlets: Add index over packages
453 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
457 const char* arch
= pakfire_get_arch(db
->pakfire
);
460 r
= pakfire_db_set_string(db
, "arch", arch
);
462 ERROR(db
->pakfire
, "Could not set architecture\n");
469 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
470 // packages: Drop build_id column
473 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
474 // need to recreate the whole table and rename it afterwards. Annoying.
479 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
482 while (db
->schema
< CURRENT_SCHEMA
) {
483 // Begin a new transaction
484 r
= pakfire_db_begin_transaction(db
);
488 switch (db
->schema
) {
491 r
= pakfire_db_create_schema(db
);
495 db
->schema
= CURRENT_SCHEMA
;
499 r
= pakfire_db_migrate_to_schema_8(db
);
507 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
511 // Update the schema version
512 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
517 r
= pakfire_db_commit(db
);
525 pakfire_db_rollback(db
);
530 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
533 // Fetch database architecture
534 char* db_arch
= pakfire_db_get_string(db
, "arch");
536 ERROR(db
->pakfire
, "Database is of an unknown architecture\n");
540 // Fetch the running architecture
541 const char* arch
= pakfire_get_arch(db
->pakfire
);
546 if (strcmp(db_arch
, arch
) == 0)
556 static int pakfire_db_setup(struct pakfire_db
* db
) {
560 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
562 // Enable foreign keys
563 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
565 // Make LIKE case-sensitive
566 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
568 // Fetch the current schema
569 db
->schema
= pakfire_db_get_schema(db
);
571 // Check if the schema is recent enough
572 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
573 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
578 // Read modification timestamp
579 db
->last_modified_at
= pakfire_read_modification_time(db
);
581 DEBUG(db
->pakfire
, "The database was last modified at %ld\n", db
->last_modified_at
);
583 // Done when not in read-write mode
584 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
587 // Disable secure delete
588 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
590 // Set database journal to WAL
591 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
592 if (r
!= SQLITE_OK
) {
593 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
594 sqlite3_errmsg(db
->handle
));
598 // Disable autocheckpoint
599 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
600 if (r
!= SQLITE_OK
) {
601 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
602 sqlite3_errmsg(db
->handle
));
606 // Create or migrate schema
607 r
= pakfire_db_migrate_schema(db
);
614 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
617 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
621 o
->pakfire
= pakfire_ref(pakfire
);
624 int sqlite3_flags
= 0;
626 // Store mode & forward it to sqlite3
627 if (flags
& PAKFIRE_DB_READWRITE
) {
628 o
->mode
= PAKFIRE_DB_READWRITE
;
629 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
631 o
->mode
= PAKFIRE_DB_READONLY
;
632 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
636 r
= pakfire_path(o
->pakfire
, o
->path
, "%s", DATABASE_PATH
);
640 // Try to open the sqlite3 database file
641 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
642 if (r
!= SQLITE_OK
) {
643 ERROR(pakfire
, "Could not open database %s: %s\n",
644 o
->path
, sqlite3_errmsg(o
->handle
));
650 // Setup the database
651 r
= pakfire_db_setup(o
);
655 // Check for compatible architecture
656 r
= pakfire_db_check_arch(o
);
670 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
676 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
685 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
686 sqlite3_stmt
* stmt
= NULL
;
689 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
691 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
692 sqlite3_errmsg(db
->handle
));
697 unsigned long errors
= 0;
701 r
= sqlite3_step(stmt
);
702 } while (r
== SQLITE_BUSY
);
704 if (r
== SQLITE_ROW
) {
705 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
707 // If the message is "ok", the database has passed the check
708 if (strcmp(error
, "ok") == 0)
711 // Increment error counter
715 ERROR(db
->pakfire
, "%s\n", error
);
717 // Break on anything else
722 sqlite3_finalize(stmt
);
725 ERROR(db
->pakfire
, "Database integrity check failed\n");
727 INFO(db
->pakfire
, "Database integrity check passed\n");
732 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
733 sqlite3_stmt
* stmt
= NULL
;
736 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
738 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
739 sqlite3_errmsg(db
->handle
));
744 unsigned long errors
= 0;
748 r
= sqlite3_step(stmt
);
749 } while (r
== SQLITE_BUSY
);
751 if (r
== SQLITE_ROW
) {
752 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
753 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
754 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
755 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
757 // Increment error counter
761 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
762 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
764 // Break on anything else
769 sqlite3_finalize(stmt
);
772 ERROR(db
->pakfire
, "Foreign key check failed\n");
774 INFO(db
->pakfire
, "Foreign key check passed\n");
780 This function performs an integrity check of the database
782 int pakfire_db_check(struct pakfire_db
* db
) {
785 // Perform integrity check
786 r
= pakfire_db_integrity_check(db
);
790 // Perform foreign key check
791 r
= pakfire_db_foreign_key_check(db
);
798 // Returns the number of packages installed
799 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
800 sqlite3_stmt
* stmt
= NULL
;
801 ssize_t packages
= -1;
803 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
805 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
806 sqlite3_errmsg(db
->handle
));
812 r
= sqlite3_step(stmt
);
813 } while (r
== SQLITE_BUSY
);
815 if (r
== SQLITE_ROW
) {
816 packages
= sqlite3_column_int64(stmt
, 0);
819 sqlite3_finalize(stmt
);
824 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
825 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
827 // Convert name to ID
828 Id id
= pool_str2id(pool
, name
, 1);
830 // Append it to pooljobs
831 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
834 static const struct pakfire_digest
{
835 enum pakfire_digests type
;
837 } pakfire_digests
[] = {
838 { PAKFIRE_DIGEST_SHA512
, "sha512:" },
839 { PAKFIRE_DIGEST_SHA256
, "sha256:" },
843 static char* pakfire_db_pack_digest(enum pakfire_digests type
, const char* hexdigest
) {
847 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
848 if (digest
->type
== type
) {
849 r
= asprintf(&s
, "%s%s", digest
->prefix
, hexdigest
);
860 static const char* pakfire_db_unpack_digest(const char* hexdigest
, enum pakfire_digests
* type
) {
863 // Don't do anything for empty input
864 if (!hexdigest
|| !*hexdigest
)
867 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
868 if (pakfire_string_startswith(hexdigest
, digest
->prefix
)) {
869 *type
= digest
->type
;
871 // Return the beginning of the hexdigest
872 return hexdigest
+ strlen(digest
->prefix
);
880 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
881 sqlite3_stmt
* stmt
= NULL
;
884 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
886 // Prepare the statement
887 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
889 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
890 sql
, sqlite3_errmsg(db
->handle
));
894 const struct __relation
{
896 char** (*func
)(struct pakfire_package
*);
898 { "provides", pakfire_package_get_provides
},
899 { "prerequires", pakfire_package_get_prerequires
},
900 { "requires", pakfire_package_get_requires
},
901 { "conflicts", pakfire_package_get_conflicts
},
902 { "obsoletes", pakfire_package_get_obsoletes
},
903 { "recommends", pakfire_package_get_recommends
},
904 { "suggests", pakfire_package_get_suggests
},
905 { "supplements", pakfire_package_get_supplements
},
906 { "enhances", pakfire_package_get_enhances
},
910 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
911 char** list
= relation
->func(pkg
);
915 for (char** dep
= list
; *dep
; dep
++) {
917 r
= sqlite3_bind_int64(stmt
, 1, id
);
919 ERROR(db
->pakfire
, "Could not bind id: %s\n",
920 sqlite3_errmsg(db
->handle
));
925 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
927 ERROR(db
->pakfire
, "Could not bind type: %s\n",
928 sqlite3_errmsg(db
->handle
));
933 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
935 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
936 sqlite3_errmsg(db
->handle
));
942 r
= sqlite3_step(stmt
);
943 } while (r
== SQLITE_BUSY
);
947 // Reset bound values
959 sqlite3_finalize(stmt
);
964 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
965 sqlite3_stmt
* stmt
= NULL
;
968 // Get the filelist from the archive
969 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
971 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
975 // Nothing to do if the list is empty
976 if (pakfire_filelist_is_empty(filelist
)) {
981 const char* sql
= "INSERT INTO files(pkg, path, size, config, datafile, mode, "
982 "user, 'group', ctime, mtime, capabilities, digest_sha512, digest_sha256) "
983 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
985 // Prepare the statement
986 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
988 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
989 sql
, sqlite3_errmsg(db
->handle
));
993 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
994 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
997 r
= sqlite3_bind_int64(stmt
, 1, id
);
999 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1000 pakfire_file_unref(file
);
1005 const char* path
= pakfire_file_get_path(file
);
1007 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
1009 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
1010 pakfire_file_unref(file
);
1015 size_t size
= pakfire_file_get_size(file
);
1017 r
= sqlite3_bind_int64(stmt
, 3, size
);
1019 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1020 pakfire_file_unref(file
);
1024 // Bind config - XXX TODO
1025 r
= sqlite3_bind_null(stmt
, 4);
1027 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
1028 pakfire_file_unref(file
);
1032 // Bind datafile - XXX TODO
1033 r
= sqlite3_bind_null(stmt
, 5);
1035 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
1036 pakfire_file_unref(file
);
1041 mode_t mode
= pakfire_file_get_mode(file
);
1043 r
= sqlite3_bind_int64(stmt
, 6, mode
);
1045 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
1046 pakfire_file_unref(file
);
1051 const char* user
= pakfire_file_get_user(file
);
1053 r
= sqlite3_bind_text(stmt
, 7, user
, -1, NULL
);
1055 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
1056 pakfire_file_unref(file
);
1061 const char* group
= pakfire_file_get_group(file
);
1063 r
= sqlite3_bind_text(stmt
, 8, group
, -1, NULL
);
1065 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
1066 pakfire_file_unref(file
);
1071 time_t ctime
= pakfire_file_get_ctime(file
);
1073 r
= sqlite3_bind_int64(stmt
, 9, ctime
);
1075 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
1076 pakfire_file_unref(file
);
1081 time_t mtime
= pakfire_file_get_mtime(file
);
1083 r
= sqlite3_bind_int64(stmt
, 10, mtime
);
1085 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
1086 pakfire_file_unref(file
);
1090 // Bind capabilities - XXX TODO
1091 r
= sqlite3_bind_null(stmt
, 11);
1093 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1094 pakfire_file_unref(file
);
1098 const unsigned char* sha512_digest
= NULL
;
1099 size_t sha512_length
= 0;
1102 sha512_digest
= pakfire_file_get_digest(file
, PAKFIRE_DIGEST_SHA512
, &sha512_length
);
1103 if (sha512_digest
) {
1104 r
= sqlite3_bind_blob(stmt
, 12, sha512_digest
, sha512_length
, NULL
);
1106 r
= sqlite3_bind_null(stmt
, 12);
1109 ERROR(db
->pakfire
, "Could not bind SHA-512 digest: %s\n",
1110 sqlite3_errmsg(db
->handle
));
1111 pakfire_file_unref(file
);
1115 const unsigned char* sha256_digest
= NULL
;
1116 size_t sha256_length
= 0;
1119 sha256_digest
= pakfire_file_get_digest(file
, PAKFIRE_DIGEST_SHA256
, &sha256_length
);
1120 if (sha256_digest
) {
1121 r
= sqlite3_bind_blob(stmt
, 13, sha256_digest
, sha256_length
, NULL
);
1123 r
= sqlite3_bind_null(stmt
, 13);
1126 ERROR(db
->pakfire
, "Could not bind SHA-256 digest: %s\n",
1127 sqlite3_errmsg(db
->handle
));
1128 pakfire_file_unref(file
);
1134 r
= sqlite3_step(stmt
);
1135 } while (r
== SQLITE_BUSY
);
1138 if (r
!= SQLITE_DONE
) {
1139 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
1140 sqlite3_errmsg(db
->handle
));
1141 pakfire_file_unref(file
);
1145 // Move on to next file
1146 pakfire_file_unref(file
);
1148 // Reset bound values
1149 sqlite3_reset(stmt
);
1157 sqlite3_finalize(stmt
);
1158 pakfire_filelist_unref(filelist
);
1163 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1164 sqlite3_stmt
* stmt
= NULL
;
1168 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1170 // Prepare the statement
1171 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1173 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1174 sql
, sqlite3_errmsg(db
->handle
));
1178 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1179 // Fetch the scriptlet
1180 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1185 r
= sqlite3_bind_int64(stmt
, 1, id
);
1187 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1188 pakfire_scriptlet_unref(scriptlet
);
1193 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1195 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1196 pakfire_scriptlet_unref(scriptlet
);
1200 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1203 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1205 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1206 pakfire_scriptlet_unref(scriptlet
);
1212 r
= sqlite3_step(stmt
);
1213 } while (r
== SQLITE_BUSY
);
1216 if (r
!= SQLITE_DONE
) {
1217 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1218 sqlite3_errmsg(db
->handle
));
1219 pakfire_scriptlet_unref(scriptlet
);
1223 pakfire_scriptlet_unref(scriptlet
);
1225 // Reset bound values
1226 sqlite3_reset(stmt
);
1234 sqlite3_finalize(stmt
);
1239 int pakfire_db_add_package(struct pakfire_db
* db
,
1240 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1241 sqlite3_stmt
* stmt
= NULL
;
1242 char* digest
= NULL
;
1245 // Begin a new transaction
1246 r
= pakfire_db_begin_transaction(db
);
1250 const char* sql
= "INSERT INTO "
1290 "CURRENT_TIMESTAMP, "
1299 // Prepare the statement
1300 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1301 if (r
!= SQLITE_OK
) {
1302 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1303 sql
, sqlite3_errmsg(db
->handle
));
1308 const char* name
= pakfire_package_get_name(pkg
);
1310 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1312 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1317 const char* evr
= pakfire_package_get_evr(pkg
);
1319 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1321 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1326 const char* arch
= pakfire_package_get_arch(pkg
);
1328 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1330 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1335 char* groups
= pakfire_package_get_groups(pkg
);
1337 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1339 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1348 r
= sqlite3_bind_null(stmt
, 4);
1354 const char* filename
= pakfire_package_get_filename(pkg
);
1356 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1358 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1363 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1365 r
= sqlite3_bind_int64(stmt
, 6, size
);
1367 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1371 // Bind installed size
1372 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1374 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1376 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1380 enum pakfire_digests digest_type
= 0;
1382 const char* hexdigest
= pakfire_package_get_hexdigest(pkg
, &digest_type
);
1384 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
1388 r
= sqlite3_bind_text(stmt
, 8, digest
, -1, NULL
);
1390 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1396 const char* license
= pakfire_package_get_license(pkg
);
1398 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1400 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1405 const char* summary
= pakfire_package_get_summary(pkg
);
1407 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1409 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1414 const char* description
= pakfire_package_get_description(pkg
);
1416 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1418 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1423 const char* uuid
= pakfire_package_get_uuid(pkg
);
1425 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1427 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1432 const char* vendor
= pakfire_package_get_vendor(pkg
);
1434 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1436 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1441 const char* build_host
= pakfire_package_get_build_host(pkg
);
1443 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1445 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1450 time_t build_time
= pakfire_package_get_build_time(pkg
);
1452 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1454 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1458 // Bind repository name
1459 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1461 const char* repo_name
= pakfire_repo_get_name(repo
);
1462 pakfire_repo_unref(repo
);
1464 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1470 r
= sqlite3_bind_null(stmt
, 16);
1475 // installed by the user?
1476 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1478 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1482 // Source package name
1483 const char* source_name
= pakfire_package_get_source_name(pkg
);
1485 r
= sqlite3_bind_text(stmt
, 18, source_name
, -1, NULL
);
1489 r
= sqlite3_bind_null(stmt
, 18);
1495 const char* source_evr
= pakfire_package_get_source_evr(pkg
);
1497 r
= sqlite3_bind_text(stmt
, 19, source_evr
, -1, NULL
);
1501 r
= sqlite3_bind_null(stmt
, 19);
1507 const char* source_arch
= pakfire_package_get_source_arch(pkg
);
1509 r
= sqlite3_bind_text(stmt
, 20, source_arch
, -1, NULL
);
1513 r
= sqlite3_bind_null(stmt
, 20);
1519 const char* distribution
= pakfire_package_get_distribution(pkg
);
1521 r
= sqlite3_bind_text(stmt
, 21, distribution
, -1, NULL
);
1525 r
= sqlite3_bind_null(stmt
, 21);
1532 r
= sqlite3_step(stmt
);
1533 } while (r
== SQLITE_BUSY
);
1535 if (r
!= SQLITE_DONE
) {
1536 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1537 sqlite3_errmsg(db
->handle
));
1543 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1546 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1551 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1556 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1562 sqlite3_finalize(stmt
);
1566 // Commit or rollback
1568 pakfire_db_rollback(db
);
1570 r
= pakfire_db_commit(db
);
1575 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1576 sqlite3_stmt
* stmt
= NULL
;
1579 // Begin a new transaction
1580 r
= pakfire_db_begin_transaction(db
);
1584 // Fetch the package's UUID
1585 const char* uuid
= pakfire_package_get_uuid(pkg
);
1587 ERROR(db
->pakfire
, "Package has no UUID\n");
1592 r
= sqlite3_prepare_v2(db
->handle
,
1593 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1595 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1596 sqlite3_errmsg(db
->handle
));
1601 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1603 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1609 r
= sqlite3_step(stmt
);
1610 } while (r
== SQLITE_BUSY
);
1612 // Check if we have been successful
1613 if (r
!= SQLITE_DONE
) {
1614 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1624 sqlite3_finalize(stmt
);
1626 // Commit or rollback
1628 pakfire_db_rollback(db
);
1630 r
= pakfire_db_commit(db
);
1635 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1636 struct pakfire_package
* pkg
, const char* type
) {
1637 struct pakfire_scriptlet
* scriptlet
= NULL
;
1638 sqlite3_stmt
* stmt
= NULL
;
1641 // Fetch the package's UUID
1642 const char* uuid
= pakfire_package_get_uuid(pkg
);
1644 ERROR(db
->pakfire
, "Package has no UUID\n");
1648 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1649 JOIN scriptlets ON packages.id = scriptlets.pkg \
1650 WHERE packages.uuid = ? AND scriptlets.type = ?";
1652 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1654 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1655 sql
, sqlite3_errmsg(db
->handle
));
1660 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1662 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1666 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1668 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1672 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1676 r
= sqlite3_step(stmt
);
1677 } while (r
== SQLITE_BUSY
);
1679 // We have some payload
1680 if (r
== SQLITE_ROW
) {
1681 const void* data
= sqlite3_column_blob(stmt
, 1);
1682 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1684 // Create a scriptlet object
1685 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1692 sqlite3_finalize(stmt
);
1697 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1698 struct pakfire_package
* pkg
= NULL
;
1702 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1704 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1709 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1711 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1716 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1718 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1723 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1725 ERROR(db
->pakfire
, "Could not create package\n");
1730 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1732 pakfire_package_set_dbid(pkg
, id
);
1735 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1737 pakfire_package_set_groups(pkg
, groups
);
1741 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1743 pakfire_package_set_filename(pkg
, filename
);
1747 size_t size
= sqlite3_column_int64(stmt
, 6);
1749 pakfire_package_set_downloadsize(pkg
, size
);
1753 size
= sqlite3_column_int64(stmt
, 7);
1755 pakfire_package_set_installsize(pkg
, size
);
1759 const char* digest
= (const char*)sqlite3_column_text(stmt
, 8);
1761 enum pakfire_digests digest_type
= 0;
1764 const char* hexdigest
= pakfire_db_unpack_digest(digest
, &digest_type
);
1766 pakfire_package_set_hexdigest(pkg
, digest_type
, hexdigest
);
1770 const char* license
= (const char*)sqlite3_column_text(stmt
, 9);
1772 pakfire_package_set_license(pkg
, license
);
1776 const char* summary
= (const char*)sqlite3_column_text(stmt
, 10);
1778 pakfire_package_set_summary(pkg
, summary
);
1782 const char* description
= (const char*)sqlite3_column_text(stmt
, 11);
1784 pakfire_package_set_description(pkg
, description
);
1788 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 12);
1790 pakfire_package_set_uuid(pkg
, uuid
);
1794 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 13);
1796 pakfire_package_set_vendor(pkg
, vendor
);
1800 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 14);
1802 pakfire_package_set_build_host(pkg
, build_host
);
1806 time_t build_time
= sqlite3_column_int64(stmt
, 15);
1808 pakfire_package_set_build_time(pkg
, build_time
);
1812 time_t install_time
= sqlite3_column_int64(stmt
, 16);
1814 pakfire_package_set_install_time(pkg
, install_time
);
1817 // installed by user?
1818 int userinstalled
= sqlite3_column_int(stmt
, 17);
1820 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1823 const char* files
= (const char*)sqlite3_column_text(stmt
, 18);
1825 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1832 const struct dependency
{
1834 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1835 } dependencies
[] = {
1836 { 19, pakfire_package_add_provides
},
1837 { 20, pakfire_package_add_prerequires
},
1838 { 21, pakfire_package_add_requires
},
1839 { 22, pakfire_package_add_conflicts
},
1840 { 23, pakfire_package_add_obsoletes
},
1841 { 24, pakfire_package_add_recommends
},
1842 { 25, pakfire_package_add_suggests
},
1843 { 26, pakfire_package_add_supplements
},
1844 { 27, pakfire_package_add_enhances
},
1848 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1849 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1851 pakfire_str2deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1856 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 28);
1858 pakfire_package_set_source_name(pkg
, source_name
);
1861 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 29);
1863 pakfire_package_set_source_evr(pkg
, source_evr
);
1866 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 30);
1868 pakfire_package_set_source_arch(pkg
, source_arch
);
1871 const char* distribution
= (const char*)sqlite3_column_text(stmt
, 31);
1873 pakfire_package_set_distribution(pkg
, distribution
);
1880 pakfire_package_unref(pkg
);
1885 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1886 sqlite3_stmt
* stmt
= NULL
;
1889 DEBUG(db
->pakfire
, "Loading package database...\n");
1891 // Drop contents of the repository
1892 pakfire_repo_clear(repo
);
1894 // Save starting time
1895 clock_t t_start
= clock();
1916 "strftime('%s', installed) AS installed, "
1919 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1922 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1923 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1926 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1927 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1928 ") AS prerequires, "
1930 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1931 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1934 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1935 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1938 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1939 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1942 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1943 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1946 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1947 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1950 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1951 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1952 ") AS supplements, "
1954 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1955 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1965 // Prepare the statement
1966 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1968 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1969 sql
, sqlite3_errmsg(db
->handle
));
1975 r
= sqlite3_step(stmt
);
1978 // Retry if the database was busy
1984 r
= pakfire_db_load_package(db
, repo
, stmt
);
1989 // All rows have been processed
1993 // Go to error in any other cases
2000 // Save time when we finished
2003 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
2004 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
2006 // Mark repository as changed
2007 pakfire_repo_has_changed(repo
);
2014 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
2015 pakfire_repo_clear(repo
);
2019 sqlite3_finalize(stmt
);
2024 static int pakfire_db_load_file_digest(struct pakfire_db
* db
, struct pakfire_file
* file
,
2025 sqlite3_stmt
* stmt
, const enum pakfire_digests type
, const int field
) {
2027 const unsigned char* digest
= sqlite3_column_blob(stmt
, field
);
2029 // Nothing further to do if field is NULL
2033 // Length of the stored value
2034 const size_t length
= sqlite3_column_bytes(stmt
, field
);
2036 // Check if the digest has the correct length
2037 if (pakfire_digest_length(type
) != length
) {
2038 const char* path
= pakfire_file_get_path(file
);
2040 ERROR(db
->pakfire
, "%s: Digest has an incorrect size of %zu byte(s)\n",
2046 return pakfire_file_set_digest(file
, type
, digest
);
2049 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2050 sqlite3_stmt
* stmt
) {
2051 struct pakfire_file
* file
= NULL
;
2054 // Create a new file object
2055 r
= pakfire_file_create(&file
, db
->pakfire
);
2060 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2062 // Abort if no path is set
2064 ERROR(db
->pakfire
, "File has no path\n");
2070 r
= pakfire_file_set_path(file
, path
);
2072 ERROR(db
->pakfire
, "%s: Could not set path '%s': %m\n", path
, path
);
2077 size_t size
= sqlite3_column_int64(stmt
, 1);
2079 pakfire_file_set_size(file
, size
);
2082 mode_t mode
= sqlite3_column_int(stmt
, 2);
2084 pakfire_file_set_mode(file
, mode
);
2087 const char* user
= (const char*)sqlite3_column_text(stmt
, 3);
2089 // Abort if no user is set
2091 ERROR(db
->pakfire
, "%s: No user\n", path
);
2097 r
= pakfire_file_set_user(file
, user
);
2099 ERROR(db
->pakfire
, "%s: Could not set user '%s': %m\n", path
, user
);
2104 const char* group
= (const char*)sqlite3_column_text(stmt
, 4);
2106 // Abort if no group is set
2108 ERROR(db
->pakfire
, "%s: No group\n", path
);
2114 r
= pakfire_file_set_group(file
, group
);
2116 ERROR(db
->pakfire
, "%s: Could not set group '%s': %m\n", path
, group
);
2121 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2123 pakfire_file_set_ctime(file
, ctime
);
2126 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2128 pakfire_file_set_mtime(file
, mtime
);
2131 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA512
, 7);
2136 r
= pakfire_db_load_file_digest(db
, file
, stmt
, PAKFIRE_DIGEST_SHA256
, 8);
2140 // Append the file to the filelist
2141 r
= pakfire_filelist_append(filelist
, file
);
2147 pakfire_file_unref(file
);
2152 int pakfire_db_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
) {
2153 struct pakfire_filelist
* list
= NULL
;
2154 sqlite3_stmt
* stmt
= NULL
;
2172 // Prepare the statement
2173 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2175 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2176 sql
, sqlite3_errmsg(db
->handle
));
2180 // Create a new filelist
2181 r
= pakfire_filelist_create(&list
, db
->pakfire
);
2185 // Iterate over all rows
2188 r
= sqlite3_step(stmt
);
2191 // Retry if the database was busy
2197 r
= pakfire_db_load_file(db
, list
, stmt
);
2202 // All rows have been processed
2207 // Go to error in any other cases
2214 // Return the filelist
2215 *filelist
= pakfire_filelist_ref(list
);
2219 ERROR(db
->pakfire
, "Could not fetch filelist: %m\n");
2222 sqlite3_finalize(stmt
);
2224 pakfire_filelist_unref(list
);
2229 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2230 struct pakfire_package
* pkg
) {
2231 struct pakfire_filelist
* fl
= NULL
;
2232 sqlite3_stmt
* stmt
= NULL
;
2235 // Fetch the package ID
2236 uint64_t id
= pakfire_package_get_dbid(pkg
);
2238 ERROR(db
->pakfire
, "Package did not have an ID\n");
2242 // Create a new filelist
2243 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2245 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
2262 // Select all files that belong to this package
2266 // Filter out any files that are also in a different package (i.e. an update
2267 // that has already been installed and this is the cleanup of the obsolete pkg)
2272 "FROM files AS duplicates "
2274 "files.path = duplicates.path "
2276 "files.pkg != duplicates.pkg"
2279 // Return the longest paths first
2284 // Prepare the statement
2285 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2287 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2288 sql
, sqlite3_errmsg(db
->handle
));
2293 r
= sqlite3_bind_int64(stmt
, 1, id
);
2295 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2301 r
= sqlite3_step(stmt
);
2304 // Retry if the database was busy
2310 r
= pakfire_db_load_file(db
, fl
, stmt
);
2315 // All rows have been processed
2319 // Go to error in any other cases
2326 *filelist
= pakfire_filelist_ref(fl
);
2331 sqlite3_finalize(stmt
);
2333 pakfire_filelist_unref(fl
);