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 <solv/solver.h>
29 #include <pakfire/archive.h>
30 #include <pakfire/db.h>
31 #include <pakfire/file.h>
32 #include <pakfire/filelist.h>
33 #include <pakfire/logging.h>
34 #include <pakfire/package.h>
35 #include <pakfire/pakfire.h>
36 #include <pakfire/repo.h>
37 #include <pakfire/util.h>
39 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
41 #define CURRENT_SCHEMA 8
42 #define SCHEMA_MIN_SUP 7
45 struct pakfire
* pakfire
;
55 static void logging_callback(void* data
, int r
, const char* msg
) {
56 struct pakfire
* pakfire
= (struct pakfire
*)data
;
58 ERROR(pakfire
, "Database Error: %s: %s\n",
59 sqlite3_errstr(r
), msg
);
62 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
65 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
68 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
69 } while (r
== SQLITE_BUSY
);
73 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
79 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
80 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
83 static int pakfire_db_commit(struct pakfire_db
* db
) {
84 return pakfire_db_execute(db
, "COMMIT");
87 static int pakfire_db_rollback(struct pakfire_db
* db
) {
88 return pakfire_db_execute(db
, "ROLLBACK");
92 This function performs any fast optimization and tries to truncate the WAL log file
93 to keep the database as compact as possible on disk.
95 static void pakfire_db_optimize(struct pakfire_db
* db
) {
96 pakfire_db_execute(db
, "PRAGMA optimize");
97 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
100 static void pakfire_db_free(struct pakfire_db
* db
) {
102 // Optimize the database before it is being closed
103 pakfire_db_optimize(db
);
105 // Close database handle
106 int r
= sqlite3_close(db
->handle
);
107 if (r
!= SQLITE_OK
) {
108 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
109 sqlite3_errmsg(db
->handle
));
113 pakfire_unref(db
->pakfire
);
118 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
119 sqlite3_stmt
* stmt
= NULL
;
120 sqlite3_value
* val
= NULL
;
123 const char* sql
= "SELECT val FROM settings WHERE key = ?";
125 // Prepare the statement
126 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
127 if (r
!= SQLITE_OK
) {
128 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
129 // sql, sqlite3_errmsg(db->handle));
134 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
135 if (r
!= SQLITE_OK
) {
136 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
140 // Execute the statement
142 r
= sqlite3_step(stmt
);
143 } while (r
== SQLITE_BUSY
);
145 // We should have read a row
150 val
= sqlite3_column_value(stmt
, 0);
152 ERROR(db
->pakfire
, "Could not read value\n");
156 // Copy value onto the heap
157 val
= sqlite3_value_dup(val
);
161 sqlite3_finalize(stmt
);
166 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
167 sqlite3_stmt
* stmt
= NULL
;
170 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
171 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
174 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
175 if (r
!= SQLITE_OK
) {
176 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
177 sql
, sqlite3_errmsg(db
->handle
));
182 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
183 if (r
!= SQLITE_OK
) {
184 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
189 r
= sqlite3_bind_int64(stmt
, 2, val
);
190 if (r
!= SQLITE_OK
) {
191 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
195 // Execute the statement
197 r
= sqlite3_step(stmt
);
198 } while (r
== SQLITE_BUSY
);
201 r
= (r
== SQLITE_OK
);
205 sqlite3_finalize(stmt
);
210 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
211 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
215 int schema
= sqlite3_value_int64(value
);
216 sqlite3_value_free(value
);
218 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
223 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
226 // Create settings table
227 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
231 // settings: Add a unique index on key
232 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
236 // Create packages table
237 r
= pakfire_db_execute(db
,
238 "CREATE TABLE IF NOT EXISTS packages("
239 "id INTEGER PRIMARY KEY, "
246 "inst_size INTEGER, "
254 "build_time INTEGER, "
255 "installed INTEGER, "
256 "userinstalled INTEGER, "
262 // packages: Create index to find package by name
263 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
267 // packages: Create unique index over UUID
268 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
272 // Create dependencies table
273 r
= pakfire_db_execute(db
,
274 "CREATE TABLE IF NOT EXISTS dependencies("
278 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
283 // dependencies: Add index over packages
284 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
288 // Create files table
289 r
= pakfire_db_execute(db
,
290 "CREATE TABLE IF NOT EXISTS files("
291 "id INTEGER PRIMARY KEY, "
303 "capabilities TEXT, "
304 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
309 // files: Add index over packages
310 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
314 // files: Add index over path
315 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
319 // Create scriptlets table
320 r
= pakfire_db_execute(db
,
321 "CREATE TABLE IF NOT EXISTS scriptlets("
322 "id INTEGER PRIMARY KEY, "
326 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
331 // scriptlets: Add index over packages
332 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
339 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
340 // packages: Drop build_id column
343 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
344 // need to recreate the whole table and rename it afterwards. Annoying.
349 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
352 while (db
->schema
< CURRENT_SCHEMA
) {
353 // Begin a new transaction
354 r
= pakfire_db_begin_transaction(db
);
358 switch (db
->schema
) {
361 r
= pakfire_db_create_schema(db
);
365 db
->schema
= CURRENT_SCHEMA
;
369 r
= pakfire_db_migrate_to_schema_8(db
);
377 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
381 // Update the schema version
382 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
387 r
= pakfire_db_commit(db
);
395 pakfire_db_rollback(db
);
400 static int pakfire_db_setup(struct pakfire_db
* db
) {
404 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
406 // Enable foreign keys
407 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
409 // Make LIKE case-sensitive
410 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
412 // Fetch the current schema
413 db
->schema
= pakfire_db_get_schema(db
);
415 // Check if the schema is recent enough
416 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
417 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
422 // Done when not in read-write mode
423 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
426 // Disable secure delete
427 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
429 // Set database journal to WAL
430 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
431 if (r
!= SQLITE_OK
) {
432 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
433 sqlite3_errmsg(db
->handle
));
437 // Disable autocheckpoint
438 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
439 if (r
!= SQLITE_OK
) {
440 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
441 sqlite3_errmsg(db
->handle
));
445 // Create or migrate schema
446 r
= pakfire_db_migrate_schema(db
);
453 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
456 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
460 o
->pakfire
= pakfire_ref(pakfire
);
463 int sqlite3_flags
= 0;
465 // Store mode & forward it to sqlite3
466 if (flags
& PAKFIRE_DB_READWRITE
) {
467 o
->mode
= PAKFIRE_DB_READWRITE
;
468 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
470 o
->mode
= PAKFIRE_DB_READONLY
;
471 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
475 r
= pakfire_make_path(o
->pakfire
, o
->path
, DATABASE_PATH
);
479 // Try to open the sqlite3 database file
480 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
481 if (r
!= SQLITE_OK
) {
482 ERROR(pakfire
, "Could not open database %s: %s\n",
483 o
->path
, sqlite3_errmsg(o
->handle
));
489 // Setup the database
490 r
= pakfire_db_setup(o
);
504 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
510 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
519 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
520 sqlite3_stmt
* stmt
= NULL
;
523 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
525 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
526 sqlite3_errmsg(db
->handle
));
531 unsigned long errors
= 0;
535 r
= sqlite3_step(stmt
);
536 } while (r
== SQLITE_BUSY
);
538 if (r
== SQLITE_ROW
) {
539 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
541 // If the message is "ok", the database has passed the check
542 if (strcmp(error
, "ok") == 0)
545 // Increment error counter
549 ERROR(db
->pakfire
, "%s\n", error
);
551 // Break on anything else
556 sqlite3_finalize(stmt
);
559 ERROR(db
->pakfire
, "Database integrity check failed\n");
561 INFO(db
->pakfire
, "Database integrity check passed\n");
566 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
567 sqlite3_stmt
* stmt
= NULL
;
570 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
572 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
573 sqlite3_errmsg(db
->handle
));
578 unsigned long errors
= 0;
582 r
= sqlite3_step(stmt
);
583 } while (r
== SQLITE_BUSY
);
585 if (r
== SQLITE_ROW
) {
586 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
587 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
588 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
589 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
591 // Increment error counter
595 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
596 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
598 // Break on anything else
603 sqlite3_finalize(stmt
);
606 ERROR(db
->pakfire
, "Foreign key check failed\n");
608 INFO(db
->pakfire
, "Foreign key check passed\n");
614 This function performs an integrity check of the database
616 int pakfire_db_check(struct pakfire_db
* db
) {
619 // Perform integrity check
620 r
= pakfire_db_integrity_check(db
);
624 // Perform foreign key check
625 r
= pakfire_db_foreign_key_check(db
);
632 // Returns the number of packages installed
633 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
634 sqlite3_stmt
* stmt
= NULL
;
635 ssize_t packages
= -1;
637 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
639 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
640 sqlite3_errmsg(db
->handle
));
646 r
= sqlite3_step(stmt
);
647 } while (r
== SQLITE_BUSY
);
649 if (r
== SQLITE_ROW
) {
650 packages
= sqlite3_column_int64(stmt
, 0);
653 sqlite3_finalize(stmt
);
658 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
659 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
661 // Convert name to ID
662 Id id
= pool_str2id(pool
, name
, 1);
664 // Append it to pooljobs
665 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
668 static const struct pakfire_digest
{
669 enum pakfire_digests type
;
671 } pakfire_digests
[] = {
672 { PAKFIRE_DIGEST_SHA512
, "sha512:" },
673 { PAKFIRE_DIGEST_SHA256
, "sha256:" },
674 { PAKFIRE_DIGEST_SHA1
, "sha1:" },
675 { PAKFIRE_DIGEST_NONE
, NULL
},
678 static char* pakfire_db_pack_digest(enum pakfire_digests type
, const char* hexdigest
) {
682 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
683 if (digest
->type
== type
) {
684 r
= asprintf(&s
, "%s%s", digest
->prefix
, hexdigest
);
695 static const char* pakfire_db_unpack_digest(const char* hexdigest
, enum pakfire_digests
* type
) {
696 *type
= PAKFIRE_DIGEST_NONE
;
698 // Don't do anything for empty input
699 if (!hexdigest
|| !*hexdigest
)
702 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
703 if (pakfire_string_startswith(hexdigest
, digest
->prefix
)) {
704 *type
= digest
->type
;
706 // Return the beginning of the hexdigest
707 return hexdigest
+ strlen(digest
->prefix
);
715 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
716 sqlite3_stmt
* stmt
= NULL
;
719 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
721 // Prepare the statement
722 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
724 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
725 sql
, sqlite3_errmsg(db
->handle
));
729 const struct __relation
{
731 char** (*func
)(struct pakfire_package
*);
733 { "provides", pakfire_package_get_provides
},
734 { "prerequires", pakfire_package_get_prerequires
},
735 { "requires", pakfire_package_get_requires
},
736 { "conflicts", pakfire_package_get_conflicts
},
737 { "obsoletes", pakfire_package_get_obsoletes
},
738 { "recommends", pakfire_package_get_recommends
},
739 { "suggests", pakfire_package_get_suggests
},
740 { "supplements", pakfire_package_get_supplements
},
741 { "enhances", pakfire_package_get_enhances
},
745 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
746 char** list
= relation
->func(pkg
);
750 for (char** dep
= list
; *dep
; dep
++) {
752 r
= sqlite3_bind_int64(stmt
, 1, id
);
754 ERROR(db
->pakfire
, "Could not bind id: %s\n",
755 sqlite3_errmsg(db
->handle
));
760 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
762 ERROR(db
->pakfire
, "Could not bind type: %s\n",
763 sqlite3_errmsg(db
->handle
));
768 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
770 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
771 sqlite3_errmsg(db
->handle
));
777 r
= sqlite3_step(stmt
);
778 } while (r
== SQLITE_BUSY
);
782 // Reset bound values
794 sqlite3_finalize(stmt
);
799 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
800 sqlite3_stmt
* stmt
= NULL
;
804 // Get the filelist from the archive
805 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
807 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
811 // Nothing to do if the list is empty
812 if (pakfire_filelist_is_empty(filelist
)) {
817 const char* sql
= "INSERT INTO files(pkg, path, size, config, datafile, mode, "
818 "user, 'group', digest, ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
820 // Prepare the statement
821 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
823 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
824 sql
, sqlite3_errmsg(db
->handle
));
828 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
829 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
832 r
= sqlite3_bind_int64(stmt
, 1, id
);
834 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
835 pakfire_file_unref(file
);
840 const char* path
= pakfire_file_get_path(file
);
842 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
844 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
845 pakfire_file_unref(file
);
850 size_t size
= pakfire_file_get_size(file
);
852 r
= sqlite3_bind_int64(stmt
, 3, size
);
854 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
855 pakfire_file_unref(file
);
859 // Bind config - XXX TODO
860 r
= sqlite3_bind_null(stmt
, 4);
862 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
863 pakfire_file_unref(file
);
867 // Bind datafile - XXX TODO
868 r
= sqlite3_bind_null(stmt
, 5);
870 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
871 pakfire_file_unref(file
);
876 mode_t mode
= pakfire_file_get_mode(file
);
878 r
= sqlite3_bind_int64(stmt
, 6, mode
);
880 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
881 pakfire_file_unref(file
);
886 const char* user
= pakfire_file_get_user(file
);
888 r
= sqlite3_bind_text(stmt
, 7, user
, -1, NULL
);
890 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
891 pakfire_file_unref(file
);
896 const char* group
= pakfire_file_get_group(file
);
898 r
= sqlite3_bind_text(stmt
, 8, group
, -1, NULL
);
900 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
901 pakfire_file_unref(file
);
905 enum pakfire_digests digest_type
= PAKFIRE_DIGEST_NONE
;
908 const char* hexdigest
= pakfire_file_get_hexdigest(file
, &digest_type
);
910 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
912 ERROR(db
->pakfire
, "Could not pack digest: %m\n");
913 pakfire_file_unref(file
);
917 r
= sqlite3_bind_text(stmt
, 9, digest
, -1, NULL
);
919 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
920 pakfire_file_unref(file
);
924 r
= sqlite3_bind_null(stmt
, 9);
926 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
927 pakfire_file_unref(file
);
933 time_t ctime
= pakfire_file_get_ctime(file
);
935 r
= sqlite3_bind_int64(stmt
, 10, ctime
);
937 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
938 pakfire_file_unref(file
);
943 time_t mtime
= pakfire_file_get_mtime(file
);
945 r
= sqlite3_bind_int64(stmt
, 11, mtime
);
947 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
948 pakfire_file_unref(file
);
952 // Bind capabilities - XXX TODO
953 r
= sqlite3_bind_null(stmt
, 12);
955 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
956 pakfire_file_unref(file
);
962 r
= sqlite3_step(stmt
);
963 } while (r
== SQLITE_BUSY
);
965 // Move on to next file
966 pakfire_file_unref(file
);
969 if (r
!= SQLITE_DONE
) {
970 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
971 sqlite3_errmsg(db
->handle
));
975 // Reset bound values
990 sqlite3_finalize(stmt
);
994 pakfire_filelist_unref(filelist
);
999 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1000 sqlite3_stmt
* stmt
= NULL
;
1004 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1006 // Prepare the statement
1007 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1009 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1010 sql
, sqlite3_errmsg(db
->handle
));
1014 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1015 // Fetch the scriptlet
1016 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1021 r
= sqlite3_bind_int64(stmt
, 1, id
);
1023 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1024 pakfire_scriptlet_unref(scriptlet
);
1029 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1031 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1032 pakfire_scriptlet_unref(scriptlet
);
1036 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1039 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1041 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1042 pakfire_scriptlet_unref(scriptlet
);
1048 r
= sqlite3_step(stmt
);
1049 } while (r
== SQLITE_BUSY
);
1052 if (r
!= SQLITE_DONE
) {
1053 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1054 sqlite3_errmsg(db
->handle
));
1055 pakfire_scriptlet_unref(scriptlet
);
1059 pakfire_scriptlet_unref(scriptlet
);
1061 // Reset bound values
1062 sqlite3_reset(stmt
);
1070 sqlite3_finalize(stmt
);
1075 int pakfire_db_add_package(struct pakfire_db
* db
,
1076 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1077 sqlite3_stmt
* stmt
= NULL
;
1078 char* digest
= NULL
;
1081 // Begin a new transaction
1082 r
= pakfire_db_begin_transaction(db
);
1086 const char* sql
= "INSERT INTO packages(name, evr, arch, groups, filename, size, "
1087 "inst_size, digest, license, summary, description, uuid, "
1088 "vendor, build_host, build_time, installed, repository, userinstalled) "
1089 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
1091 // Prepare the statement
1092 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1093 if (r
!= SQLITE_OK
) {
1094 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1095 sql
, sqlite3_errmsg(db
->handle
));
1100 const char* name
= pakfire_package_get_name(pkg
);
1102 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1104 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1109 const char* evr
= pakfire_package_get_evr(pkg
);
1111 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1113 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1118 const char* arch
= pakfire_package_get_arch(pkg
);
1120 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1122 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1127 char* groups
= pakfire_package_get_groups(pkg
);
1129 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1131 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1140 r
= sqlite3_bind_null(stmt
, 4);
1146 const char* filename
= pakfire_package_get_filename(pkg
);
1148 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1150 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1155 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1157 r
= sqlite3_bind_int64(stmt
, 6, size
);
1159 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1163 // Bind installed size
1164 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1166 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1168 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1172 enum pakfire_digests digest_type
= PAKFIRE_DIGEST_NONE
;
1174 const char* hexdigest
= pakfire_package_get_hexdigest(pkg
, &digest_type
);
1176 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
1180 r
= sqlite3_bind_text(stmt
, 8, digest
, -1, NULL
);
1182 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1188 const char* license
= pakfire_package_get_license(pkg
);
1190 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1192 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1197 const char* summary
= pakfire_package_get_summary(pkg
);
1199 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1201 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1206 const char* description
= pakfire_package_get_description(pkg
);
1208 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1210 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1215 const char* uuid
= pakfire_package_get_uuid(pkg
);
1217 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1219 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1224 const char* vendor
= pakfire_package_get_vendor(pkg
);
1226 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1228 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1233 const char* build_host
= pakfire_package_get_build_host(pkg
);
1235 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1237 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1242 time_t build_time
= pakfire_package_get_build_time(pkg
);
1244 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1246 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1250 // Bind repository name
1251 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1253 const char* repo_name
= pakfire_repo_get_name(repo
);
1254 pakfire_repo_unref(repo
);
1256 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1262 r
= sqlite3_bind_null(stmt
, 16);
1267 // installed by the user?
1268 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1270 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1276 r
= sqlite3_step(stmt
);
1277 } while (r
== SQLITE_BUSY
);
1279 if (r
!= SQLITE_DONE
) {
1280 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1281 sqlite3_errmsg(db
->handle
));
1286 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1289 r
= sqlite3_finalize(stmt
);
1294 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1299 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1304 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1308 // All done, commit!
1309 r
= pakfire_db_commit(db
);
1317 sqlite3_finalize(stmt
);
1321 pakfire_db_rollback(db
);
1326 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1327 sqlite3_stmt
* stmt
= NULL
;
1330 // Fetch the package's UUID
1331 const char* uuid
= pakfire_package_get_uuid(pkg
);
1333 ERROR(db
->pakfire
, "Package has no UUID\n");
1337 r
= sqlite3_prepare_v2(db
->handle
,
1338 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1340 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1341 sqlite3_errmsg(db
->handle
));
1346 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1348 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1354 r
= sqlite3_step(stmt
);
1355 } while (r
== SQLITE_BUSY
);
1357 // Check if we have been successful
1358 if (r
!= SQLITE_DONE
) {
1359 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1368 sqlite3_finalize(stmt
);
1373 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1374 struct pakfire_package
* pkg
, const char* type
) {
1375 struct pakfire_scriptlet
* scriptlet
= NULL
;
1376 sqlite3_stmt
* stmt
= NULL
;
1379 // Fetch the package's UUID
1380 const char* uuid
= pakfire_package_get_uuid(pkg
);
1382 ERROR(db
->pakfire
, "Package has no UUID\n");
1386 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1387 JOIN scriptlets ON packages.id = scriptlets.pkg \
1388 WHERE packages.uuid = ? AND scriptlets.type = ?";
1390 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1392 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1393 sql
, sqlite3_errmsg(db
->handle
));
1398 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1400 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1404 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1406 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1410 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1414 r
= sqlite3_step(stmt
);
1415 } while (r
== SQLITE_BUSY
);
1417 // We have some payload
1418 if (r
== SQLITE_ROW
) {
1419 const void* data
= sqlite3_column_blob(stmt
, 1);
1420 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1422 // Create a scriptlet object
1423 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1430 sqlite3_finalize(stmt
);
1435 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1436 struct pakfire_package
* pkg
= NULL
;
1440 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1442 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1447 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1449 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1454 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1456 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1461 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1463 ERROR(db
->pakfire
, "Could not create package\n");
1468 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1470 pakfire_package_set_dbid(pkg
, id
);
1473 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1475 pakfire_package_set_groups(pkg
, groups
);
1479 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1481 pakfire_package_set_filename(pkg
, filename
);
1485 size_t size
= sqlite3_column_int64(stmt
, 6);
1487 pakfire_package_set_downloadsize(pkg
, size
);
1491 size
= sqlite3_column_int64(stmt
, 7);
1493 pakfire_package_set_installsize(pkg
, size
);
1497 const char* digest
= (const char*)sqlite3_column_text(stmt
, 8);
1499 enum pakfire_digests digest_type
= PAKFIRE_DIGEST_NONE
;
1502 const char* hexdigest
= pakfire_db_unpack_digest(digest
, &digest_type
);
1504 pakfire_package_set_hexdigest(pkg
, digest_type
, hexdigest
);
1508 const char* license
= (const char*)sqlite3_column_text(stmt
, 9);
1510 pakfire_package_set_license(pkg
, license
);
1514 const char* summary
= (const char*)sqlite3_column_text(stmt
, 10);
1516 pakfire_package_set_summary(pkg
, summary
);
1520 const char* description
= (const char*)sqlite3_column_text(stmt
, 11);
1522 pakfire_package_set_description(pkg
, description
);
1526 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 12);
1528 pakfire_package_set_uuid(pkg
, uuid
);
1532 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 13);
1534 pakfire_package_set_vendor(pkg
, vendor
);
1538 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 14);
1540 pakfire_package_set_build_host(pkg
, build_host
);
1544 time_t build_time
= sqlite3_column_int64(stmt
, 15);
1546 pakfire_package_set_build_time(pkg
, build_time
);
1550 time_t install_time
= sqlite3_column_int64(stmt
, 16);
1552 pakfire_package_set_install_time(pkg
, install_time
);
1555 // installed by user?
1556 int userinstalled
= sqlite3_column_int(stmt
, 17);
1558 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1561 const char* files
= (const char*)sqlite3_column_text(stmt
, 18);
1563 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1570 const struct dependency
{
1572 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1573 } dependencies
[] = {
1574 { 19, pakfire_package_add_provides
},
1575 { 20, pakfire_package_add_prerequires
},
1576 { 21, pakfire_package_add_requires
},
1577 { 22, pakfire_package_add_conflicts
},
1578 { 23, pakfire_package_add_obsoletes
},
1579 { 24, pakfire_package_add_recommends
},
1580 { 25, pakfire_package_add_suggests
},
1581 { 26, pakfire_package_add_supplements
},
1582 { 27, pakfire_package_add_enhances
},
1586 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1587 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1589 pakfire_parse_deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1598 pakfire_package_unref(pkg
);
1603 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1604 sqlite3_stmt
* stmt
= NULL
;
1607 DEBUG(db
->pakfire
, "Loading package database...\n");
1609 // Drop contents of the repository
1610 pakfire_repo_clear(repo
);
1612 // Save starting time
1613 clock_t t_start
= clock();
1618 "name, evr, arch, id, groups, filename, size, inst_size, "
1619 "digest, license, summary, description, uuid, vendor, "
1620 "build_host, build_time, strftime('%s', installed) AS installed, userinstalled, "
1622 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1625 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1626 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1629 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1630 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1631 ") AS prerequires, "
1633 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1634 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1637 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1638 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1641 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1642 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1645 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1646 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1649 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1650 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1653 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1654 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1655 ") AS supplements, "
1657 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1658 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1664 // Prepare the statement
1665 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1667 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1668 sql
, sqlite3_errmsg(db
->handle
));
1674 r
= sqlite3_step(stmt
);
1677 // Retry if the database was busy
1683 r
= pakfire_db_load_package(db
, repo
, stmt
);
1688 // All rows have been processed
1692 // Go to error in any other cases
1699 // Save time when we finished
1702 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
1703 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
1705 // Internalize repository
1706 pakfire_repo_internalize(repo
);
1713 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
1714 pakfire_repo_clear(repo
);
1718 sqlite3_finalize(stmt
);
1723 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
1724 sqlite3_stmt
* stmt
) {
1725 struct pakfire_file
* file
= NULL
;
1726 char abspath
[PATH_MAX
];
1729 // Create a new file object
1730 r
= pakfire_file_create(&file
, db
->pakfire
);
1735 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
1737 pakfire_file_set_path(file
, path
);
1740 r
= pakfire_make_path(db
->pakfire
, abspath
, path
);
1744 pakfire_file_set_abspath(file
, abspath
);
1747 size_t size
= sqlite3_column_int64(stmt
, 1);
1749 pakfire_file_set_size(file
, size
);
1752 mode_t mode
= sqlite3_column_int(stmt
, 2);
1754 pakfire_file_set_mode(file
, mode
);
1757 const char* user
= (const char*)sqlite3_column_text(stmt
, 3);
1759 pakfire_file_set_user(file
, user
);
1762 const char* group
= (const char*)sqlite3_column_text(stmt
, 4);
1764 pakfire_file_set_group(file
, group
);
1767 time_t ctime
= sqlite3_column_int64(stmt
, 5);
1769 pakfire_file_set_ctime(file
, ctime
);
1772 time_t mtime
= sqlite3_column_int64(stmt
, 6);
1774 pakfire_file_set_mtime(file
, mtime
);
1776 // Append the file to the filelist
1777 r
= pakfire_filelist_append(filelist
, file
);
1781 pakfire_file_unref(file
);
1786 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
1787 struct pakfire_package
* pkg
) {
1788 struct pakfire_filelist
* fl
= NULL
;
1789 sqlite3_stmt
* stmt
= NULL
;
1792 // Fetch the package ID
1793 uint64_t id
= pakfire_package_get_dbid(pkg
);
1795 ERROR(db
->pakfire
, "Package did not have an ID\n");
1799 // Create a new filelist
1800 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
1802 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
1817 // Select all files that belong to this package
1821 // Filter out any files that are also in a different package (i.e. an update
1822 // that has already been installed and this is the cleanup of the obsolete pkg)
1827 "FROM files AS duplicates "
1829 "files.path = duplicates.path "
1831 "files.pkg != duplicates.pkg"
1834 // Return the longest paths first
1839 // Prepare the statement
1840 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1842 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1843 sql
, sqlite3_errmsg(db
->handle
));
1848 r
= sqlite3_bind_int64(stmt
, 1, id
);
1850 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
1856 r
= sqlite3_step(stmt
);
1859 // Retry if the database was busy
1865 r
= pakfire_db_load_file(db
, fl
, stmt
);
1870 // All rows have been processed
1874 // Go to error in any other cases
1881 *filelist
= pakfire_filelist_ref(fl
);
1886 sqlite3_finalize(stmt
);
1888 pakfire_filelist_unref(fl
);