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, "
247 "digest_sha512 TEXT, "
248 "digest_sha256 TEXT, "
255 "build_time INTEGER, "
256 "installed INTEGER, "
257 "userinstalled INTEGER, "
263 // packages: Create index to find package by name
264 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
268 // packages: Create unique index over UUID
269 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
273 // Create dependencies table
274 r
= pakfire_db_execute(db
,
275 "CREATE TABLE IF NOT EXISTS dependencies("
279 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
284 // dependencies: Add index over packages
285 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
289 // Create files table
290 r
= pakfire_db_execute(db
,
291 "CREATE TABLE IF NOT EXISTS files("
292 "id INTEGER PRIMARY KEY, "
304 "capabilities TEXT, "
305 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
310 // files: Add index over packages
311 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
315 // files: Add index over path
316 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
320 // Create scriptlets table
321 r
= pakfire_db_execute(db
,
322 "CREATE TABLE IF NOT EXISTS scriptlets("
323 "id INTEGER PRIMARY KEY, "
327 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
332 // scriptlets: Add index over packages
333 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
340 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
341 // packages: Drop build_id column
344 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
345 // need to recreate the whole table and rename it afterwards. Annoying.
350 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
353 while (db
->schema
< CURRENT_SCHEMA
) {
354 // Begin a new transaction
355 r
= pakfire_db_begin_transaction(db
);
359 switch (db
->schema
) {
362 r
= pakfire_db_create_schema(db
);
366 db
->schema
= CURRENT_SCHEMA
;
370 r
= pakfire_db_migrate_to_schema_8(db
);
378 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
382 // Update the schema version
383 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
388 r
= pakfire_db_commit(db
);
396 pakfire_db_rollback(db
);
401 static int pakfire_db_setup(struct pakfire_db
* db
) {
405 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
407 // Enable foreign keys
408 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
410 // Make LIKE case-sensitive
411 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
413 // Fetch the current schema
414 db
->schema
= pakfire_db_get_schema(db
);
416 // Check if the schema is recent enough
417 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
418 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
423 // Done when not in read-write mode
424 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
427 // Disable secure delete
428 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
430 // Set database journal to WAL
431 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
432 if (r
!= SQLITE_OK
) {
433 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
434 sqlite3_errmsg(db
->handle
));
438 // Disable autocheckpoint
439 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
440 if (r
!= SQLITE_OK
) {
441 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
442 sqlite3_errmsg(db
->handle
));
446 // Create or migrate schema
447 r
= pakfire_db_migrate_schema(db
);
454 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
457 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
461 o
->pakfire
= pakfire_ref(pakfire
);
464 int sqlite3_flags
= 0;
466 // Store mode & forward it to sqlite3
467 if (flags
& PAKFIRE_DB_READWRITE
) {
468 o
->mode
= PAKFIRE_DB_READWRITE
;
469 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
471 o
->mode
= PAKFIRE_DB_READONLY
;
472 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
476 r
= pakfire_make_path(o
->pakfire
, o
->path
, DATABASE_PATH
);
480 // Try to open the sqlite3 database file
481 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
482 if (r
!= SQLITE_OK
) {
483 ERROR(pakfire
, "Could not open database %s: %s\n",
484 o
->path
, sqlite3_errmsg(o
->handle
));
490 // Setup the database
491 r
= pakfire_db_setup(o
);
505 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
511 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
520 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
521 sqlite3_stmt
* stmt
= NULL
;
524 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
526 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
527 sqlite3_errmsg(db
->handle
));
532 unsigned long errors
= 0;
536 r
= sqlite3_step(stmt
);
537 } while (r
== SQLITE_BUSY
);
539 if (r
== SQLITE_ROW
) {
540 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
542 // If the message is "ok", the database has passed the check
543 if (strcmp(error
, "ok") == 0)
546 // Increment error counter
550 ERROR(db
->pakfire
, "%s\n", error
);
552 // Break on anything else
557 sqlite3_finalize(stmt
);
560 ERROR(db
->pakfire
, "Database integrity check failed\n");
562 INFO(db
->pakfire
, "Database integrity check passed\n");
567 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
568 sqlite3_stmt
* stmt
= NULL
;
571 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
573 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
574 sqlite3_errmsg(db
->handle
));
579 unsigned long errors
= 0;
583 r
= sqlite3_step(stmt
);
584 } while (r
== SQLITE_BUSY
);
586 if (r
== SQLITE_ROW
) {
587 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
588 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
589 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
590 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
592 // Increment error counter
596 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
597 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
599 // Break on anything else
604 sqlite3_finalize(stmt
);
607 ERROR(db
->pakfire
, "Foreign key check failed\n");
609 INFO(db
->pakfire
, "Foreign key check passed\n");
615 This function performs an integrity check of the database
617 int pakfire_db_check(struct pakfire_db
* db
) {
620 // Perform integrity check
621 r
= pakfire_db_integrity_check(db
);
625 // Perform foreign key check
626 r
= pakfire_db_foreign_key_check(db
);
633 // Returns the number of packages installed
634 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
635 sqlite3_stmt
* stmt
= NULL
;
636 ssize_t packages
= -1;
638 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
640 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
641 sqlite3_errmsg(db
->handle
));
647 r
= sqlite3_step(stmt
);
648 } while (r
== SQLITE_BUSY
);
650 if (r
== SQLITE_ROW
) {
651 packages
= sqlite3_column_int64(stmt
, 0);
654 sqlite3_finalize(stmt
);
659 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
660 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
662 // Convert name to ID
663 Id id
= pool_str2id(pool
, name
, 1);
665 // Append it to pooljobs
666 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
669 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
670 sqlite3_stmt
* stmt
= NULL
;
673 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
675 // Prepare the statement
676 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
678 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
679 sql
, sqlite3_errmsg(db
->handle
));
683 const struct __relation
{
685 char** (*func
)(struct pakfire_package
*);
687 { "provides", pakfire_package_get_provides
},
688 { "prerequires", pakfire_package_get_prerequires
},
689 { "requires", pakfire_package_get_requires
},
690 { "conflicts", pakfire_package_get_conflicts
},
691 { "obsoletes", pakfire_package_get_obsoletes
},
692 { "recommends", pakfire_package_get_recommends
},
693 { "suggests", pakfire_package_get_suggests
},
694 { "supplements", pakfire_package_get_supplements
},
695 { "enhances", pakfire_package_get_enhances
},
699 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
700 char** list
= relation
->func(pkg
);
704 for (char** dep
= list
; *dep
; dep
++) {
706 r
= sqlite3_bind_int64(stmt
, 1, id
);
708 ERROR(db
->pakfire
, "Could not bind id: %s\n",
709 sqlite3_errmsg(db
->handle
));
714 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
716 ERROR(db
->pakfire
, "Could not bind type: %s\n",
717 sqlite3_errmsg(db
->handle
));
722 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
724 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
725 sqlite3_errmsg(db
->handle
));
731 r
= sqlite3_step(stmt
);
732 } while (r
== SQLITE_BUSY
);
736 // Reset bound values
748 sqlite3_finalize(stmt
);
753 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
754 sqlite3_stmt
* stmt
= NULL
;
757 // Get the filelist from the archive
758 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
760 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
764 // Nothing to do if the list is empty
765 if (pakfire_filelist_is_empty(filelist
)) {
770 const char* sql
= "INSERT INTO files(pkg, path, size, config, datafile, mode, "
771 "user, 'group', hash1, ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
773 // Prepare the statement
774 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
776 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
777 sql
, sqlite3_errmsg(db
->handle
));
781 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
782 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
785 r
= sqlite3_bind_int64(stmt
, 1, id
);
787 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
788 pakfire_file_unref(file
);
793 const char* path
= pakfire_file_get_path(file
);
795 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
797 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
798 pakfire_file_unref(file
);
803 size_t size
= pakfire_file_get_size(file
);
805 r
= sqlite3_bind_int64(stmt
, 3, size
);
807 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
808 pakfire_file_unref(file
);
812 // Bind config - XXX TODO
813 r
= sqlite3_bind_null(stmt
, 4);
815 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
816 pakfire_file_unref(file
);
820 // Bind datafile - XXX TODO
821 r
= sqlite3_bind_null(stmt
, 5);
823 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
824 pakfire_file_unref(file
);
829 mode_t mode
= pakfire_file_get_mode(file
);
831 r
= sqlite3_bind_int64(stmt
, 6, mode
);
833 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
834 pakfire_file_unref(file
);
839 const char* user
= pakfire_file_get_user(file
);
841 r
= sqlite3_bind_text(stmt
, 7, user
, -1, NULL
);
843 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
844 pakfire_file_unref(file
);
849 const char* group
= pakfire_file_get_group(file
);
851 r
= sqlite3_bind_text(stmt
, 8, group
, -1, NULL
);
853 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
854 pakfire_file_unref(file
);
859 const char* chksum
= pakfire_file_get_chksum(file
);
861 r
= sqlite3_bind_text(stmt
, 9, chksum
, -1, NULL
);
863 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
864 pakfire_file_unref(file
);
869 time_t ctime
= pakfire_file_get_ctime(file
);
871 r
= sqlite3_bind_int64(stmt
, 10, ctime
);
873 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
874 pakfire_file_unref(file
);
879 time_t mtime
= pakfire_file_get_mtime(file
);
881 r
= sqlite3_bind_int64(stmt
, 11, mtime
);
883 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
884 pakfire_file_unref(file
);
888 // Bind capabilities - XXX TODO
889 r
= sqlite3_bind_null(stmt
, 12);
891 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
892 pakfire_file_unref(file
);
898 r
= sqlite3_step(stmt
);
899 } while (r
== SQLITE_BUSY
);
901 // Move on to next file
902 pakfire_file_unref(file
);
905 if (r
!= SQLITE_DONE
) {
906 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
907 sqlite3_errmsg(db
->handle
));
911 // Reset bound values
920 sqlite3_finalize(stmt
);
922 pakfire_filelist_unref(filelist
);
927 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
928 sqlite3_stmt
* stmt
= NULL
;
932 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
934 // Prepare the statement
935 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
937 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
938 sql
, sqlite3_errmsg(db
->handle
));
942 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
943 // Fetch the scriptlet
944 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
949 r
= sqlite3_bind_int64(stmt
, 1, id
);
951 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
952 pakfire_scriptlet_unref(scriptlet
);
957 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
959 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
960 pakfire_scriptlet_unref(scriptlet
);
964 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
967 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
969 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
970 pakfire_scriptlet_unref(scriptlet
);
976 r
= sqlite3_step(stmt
);
977 } while (r
== SQLITE_BUSY
);
980 if (r
!= SQLITE_DONE
) {
981 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
982 sqlite3_errmsg(db
->handle
));
983 pakfire_scriptlet_unref(scriptlet
);
987 pakfire_scriptlet_unref(scriptlet
);
989 // Reset bound values
998 sqlite3_finalize(stmt
);
1003 int pakfire_db_add_package(struct pakfire_db
* db
,
1004 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1005 sqlite3_stmt
* stmt
= NULL
;
1008 // Begin a new transaction
1009 r
= pakfire_db_begin_transaction(db
);
1013 const char* sql
= "INSERT INTO packages(name, evr, arch, groups, filename, size, "
1014 "inst_size, digest_sha512, digest_sha256, license, summary, description, uuid, "
1015 "vendor, build_host, build_time, installed, repository, userinstalled) "
1016 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
1018 // Prepare the statement
1019 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1020 if (r
!= SQLITE_OK
) {
1021 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1022 sql
, sqlite3_errmsg(db
->handle
));
1027 const char* name
= pakfire_package_get_name(pkg
);
1029 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1031 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1036 const char* evr
= pakfire_package_get_evr(pkg
);
1038 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1040 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1045 const char* arch
= pakfire_package_get_arch(pkg
);
1047 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1049 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1054 char* groups
= pakfire_package_get_groups(pkg
);
1056 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1058 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1067 r
= sqlite3_bind_null(stmt
, 4);
1073 const char* filename
= pakfire_package_get_filename(pkg
);
1075 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1077 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1082 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1084 r
= sqlite3_bind_int64(stmt
, 6, size
);
1086 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1090 // Bind installed size
1091 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1093 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1095 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1099 // Bind digest_sha512
1100 const char* digest_sha512
= pakfire_package_get_hexdigest(pkg
, PAKFIRE_PACKAGE_DIGEST_SHA512
);
1102 r
= sqlite3_bind_text(stmt
, 8, digest_sha512
, -1, NULL
);
1104 ERROR(db
->pakfire
, "Could not bind digest_sha512: %s\n", sqlite3_errmsg(db
->handle
));
1108 // Bind digest_sha256
1109 const char* digest_sha256
= pakfire_package_get_hexdigest(pkg
, PAKFIRE_PACKAGE_DIGEST_SHA256
);
1111 r
= sqlite3_bind_text(stmt
, 9, digest_sha256
, -1, NULL
);
1113 ERROR(db
->pakfire
, "Could not bind digest_sha256: %s\n", sqlite3_errmsg(db
->handle
));
1118 const char* license
= pakfire_package_get_license(pkg
);
1120 r
= sqlite3_bind_text(stmt
, 10, license
, -1, NULL
);
1122 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1127 const char* summary
= pakfire_package_get_summary(pkg
);
1129 r
= sqlite3_bind_text(stmt
, 11, summary
, -1, NULL
);
1131 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1136 const char* description
= pakfire_package_get_description(pkg
);
1138 r
= sqlite3_bind_text(stmt
, 12, description
, -1, NULL
);
1140 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1145 const char* uuid
= pakfire_package_get_uuid(pkg
);
1147 r
= sqlite3_bind_text(stmt
, 13, uuid
, -1, NULL
);
1149 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1154 const char* vendor
= pakfire_package_get_vendor(pkg
);
1156 r
= sqlite3_bind_text(stmt
, 14, vendor
, -1, NULL
);
1158 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1163 const char* build_host
= pakfire_package_get_build_host(pkg
);
1165 r
= sqlite3_bind_text(stmt
, 15, build_host
, -1, NULL
);
1167 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1172 time_t build_time
= pakfire_package_get_build_time(pkg
);
1174 r
= sqlite3_bind_int64(stmt
, 16, build_time
);
1176 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1180 // Bind repository name
1181 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1183 const char* repo_name
= pakfire_repo_get_name(repo
);
1184 pakfire_repo_unref(repo
);
1186 r
= sqlite3_bind_text(stmt
, 17, repo_name
, -1, NULL
);
1192 r
= sqlite3_bind_null(stmt
, 17);
1197 // installed by the user?
1198 r
= sqlite3_bind_int(stmt
, 18, userinstalled
);
1200 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1206 r
= sqlite3_step(stmt
);
1207 } while (r
== SQLITE_BUSY
);
1209 if (r
!= SQLITE_DONE
) {
1210 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1211 sqlite3_errmsg(db
->handle
));
1216 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1219 r
= sqlite3_finalize(stmt
);
1224 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1229 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1234 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1238 // All done, commit!
1239 r
= pakfire_db_commit(db
);
1247 sqlite3_finalize(stmt
);
1249 pakfire_db_rollback(db
);
1254 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1255 sqlite3_stmt
* stmt
= NULL
;
1258 // Fetch the package's UUID
1259 const char* uuid
= pakfire_package_get_uuid(pkg
);
1261 ERROR(db
->pakfire
, "Package has no UUID\n");
1265 r
= sqlite3_prepare_v2(db
->handle
,
1266 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1268 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1269 sqlite3_errmsg(db
->handle
));
1274 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1276 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1282 r
= sqlite3_step(stmt
);
1283 } while (r
== SQLITE_BUSY
);
1285 // Check if we have been successful
1286 if (r
!= SQLITE_DONE
) {
1287 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1296 sqlite3_finalize(stmt
);
1301 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1302 struct pakfire_package
* pkg
, const char* type
) {
1303 struct pakfire_scriptlet
* scriptlet
= NULL
;
1304 sqlite3_stmt
* stmt
= NULL
;
1307 // Fetch the package's UUID
1308 const char* uuid
= pakfire_package_get_uuid(pkg
);
1310 ERROR(db
->pakfire
, "Package has no UUID\n");
1314 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1315 JOIN scriptlets ON packages.id = scriptlets.pkg \
1316 WHERE packages.uuid = ? AND scriptlets.type = ?";
1318 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1320 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1321 sql
, sqlite3_errmsg(db
->handle
));
1326 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1328 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1332 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1334 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1338 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1342 r
= sqlite3_step(stmt
);
1343 } while (r
== SQLITE_BUSY
);
1345 // We have some payload
1346 if (r
== SQLITE_ROW
) {
1347 const void* data
= sqlite3_column_blob(stmt
, 1);
1348 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1350 // Create a scriptlet object
1351 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1358 sqlite3_finalize(stmt
);
1363 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1364 struct pakfire_package
* pkg
= NULL
;
1368 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1370 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1375 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1377 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1382 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1384 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1389 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1391 ERROR(db
->pakfire
, "Could not create package\n");
1396 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1398 pakfire_package_set_dbid(pkg
, id
);
1401 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1403 pakfire_package_set_groups(pkg
, groups
);
1407 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1409 pakfire_package_set_filename(pkg
, filename
);
1413 size_t size
= sqlite3_column_int64(stmt
, 6);
1415 pakfire_package_set_downloadsize(pkg
, size
);
1419 size
= sqlite3_column_int64(stmt
, 7);
1421 pakfire_package_set_installsize(pkg
, size
);
1425 const char* digest_sha512
= (const char*)sqlite3_column_text(stmt
, 8);
1426 if (digest_sha512
) {
1427 pakfire_package_set_hexdigest(pkg
, PAKFIRE_PACKAGE_DIGEST_SHA512
, digest_sha512
);
1431 const char* digest_sha256
= (const char*)sqlite3_column_text(stmt
, 9);
1432 if (digest_sha256
) {
1433 pakfire_package_set_hexdigest(pkg
, PAKFIRE_PACKAGE_DIGEST_SHA256
, digest_sha256
);
1437 const char* license
= (const char*)sqlite3_column_text(stmt
, 10);
1439 pakfire_package_set_license(pkg
, license
);
1443 const char* summary
= (const char*)sqlite3_column_text(stmt
, 11);
1445 pakfire_package_set_summary(pkg
, summary
);
1449 const char* description
= (const char*)sqlite3_column_text(stmt
, 12);
1451 pakfire_package_set_description(pkg
, description
);
1455 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 13);
1457 pakfire_package_set_uuid(pkg
, uuid
);
1461 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 14);
1463 pakfire_package_set_vendor(pkg
, vendor
);
1467 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 15);
1469 pakfire_package_set_build_host(pkg
, build_host
);
1473 time_t build_time
= sqlite3_column_int64(stmt
, 16);
1475 pakfire_package_set_build_time(pkg
, build_time
);
1479 time_t install_time
= sqlite3_column_int64(stmt
, 17);
1481 pakfire_package_set_install_time(pkg
, install_time
);
1484 // installed by user?
1485 int userinstalled
= sqlite3_column_int(stmt
, 18);
1487 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1490 const char* files
= (const char*)sqlite3_column_text(stmt
, 19);
1492 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1499 const struct dependency
{
1501 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1502 } dependencies
[] = {
1503 { 20, pakfire_package_add_provides
},
1504 { 21, pakfire_package_add_prerequires
},
1505 { 22, pakfire_package_add_requires
},
1506 { 23, pakfire_package_add_conflicts
},
1507 { 24, pakfire_package_add_obsoletes
},
1508 { 25, pakfire_package_add_recommends
},
1509 { 26, pakfire_package_add_suggests
},
1510 { 27, pakfire_package_add_supplements
},
1511 { 28, pakfire_package_add_enhances
},
1515 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1516 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1518 pakfire_parse_deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1527 pakfire_package_unref(pkg
);
1532 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1533 sqlite3_stmt
* stmt
= NULL
;
1536 DEBUG(db
->pakfire
, "Loading package database...\n");
1538 // Drop contents of the repository
1539 pakfire_repo_clear(repo
);
1541 // Save starting time
1542 clock_t t_start
= clock();
1547 "name, evr, arch, id, groups, filename, size, inst_size, "
1548 "digest_sha512, digest_sha256, license, summary, description, uuid, vendor, "
1549 "build_host, build_time, strftime('%s', installed) AS installed, userinstalled, "
1551 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1554 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1555 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1558 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1559 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1560 ") AS prerequires, "
1562 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1563 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1566 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1567 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1570 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1571 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1574 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1575 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1578 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1579 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1582 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1583 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1584 ") AS supplements, "
1586 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1587 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1593 // Prepare the statement
1594 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1596 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1597 sql
, sqlite3_errmsg(db
->handle
));
1603 r
= sqlite3_step(stmt
);
1606 // Retry if the database was busy
1612 r
= pakfire_db_load_package(db
, repo
, stmt
);
1617 // All rows have been processed
1621 // Go to error in any other cases
1628 // Save time when we finished
1631 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
1632 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
1634 // Internalize repository
1635 pakfire_repo_internalize(repo
);
1642 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
1643 pakfire_repo_clear(repo
);
1647 sqlite3_finalize(stmt
);
1652 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
1653 sqlite3_stmt
* stmt
) {
1654 struct pakfire_file
* file
= NULL
;
1655 char abspath
[PATH_MAX
];
1658 // Create a new file object
1659 r
= pakfire_file_create(&file
, db
->pakfire
);
1664 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
1666 pakfire_file_set_path(file
, path
);
1669 r
= pakfire_make_path(db
->pakfire
, abspath
, path
);
1673 pakfire_file_set_abspath(file
, abspath
);
1676 size_t size
= sqlite3_column_int64(stmt
, 1);
1678 pakfire_file_set_size(file
, size
);
1681 mode_t mode
= sqlite3_column_int(stmt
, 2);
1683 pakfire_file_set_mode(file
, mode
);
1686 const char* user
= (const char*)sqlite3_column_text(stmt
, 3);
1688 pakfire_file_set_user(file
, user
);
1691 const char* group
= (const char*)sqlite3_column_text(stmt
, 4);
1693 pakfire_file_set_group(file
, group
);
1696 time_t ctime
= sqlite3_column_int64(stmt
, 5);
1698 pakfire_file_set_ctime(file
, ctime
);
1701 time_t mtime
= sqlite3_column_int64(stmt
, 6);
1703 pakfire_file_set_mtime(file
, mtime
);
1705 // Append the file to the filelist
1706 r
= pakfire_filelist_append(filelist
, file
);
1710 pakfire_file_unref(file
);
1715 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
1716 struct pakfire_package
* pkg
) {
1717 struct pakfire_filelist
* fl
= NULL
;
1718 sqlite3_stmt
* stmt
= NULL
;
1721 // Fetch the package ID
1722 uint64_t id
= pakfire_package_get_dbid(pkg
);
1724 ERROR(db
->pakfire
, "Package did not have an ID\n");
1728 // Create a new filelist
1729 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
1731 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
1746 // Select all files that belong to this package
1750 // Filter out any files that are also in a different package (i.e. an update
1751 // that has already been installed and this is the cleanup of the obsolete pkg)
1756 "FROM files AS duplicates "
1758 "files.path = duplicates.path "
1760 "files.pkg != duplicates.pkg"
1763 // Return the longest paths first
1768 // Prepare the statement
1769 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1771 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1772 sql
, sqlite3_errmsg(db
->handle
));
1777 r
= sqlite3_bind_int64(stmt
, 1, id
);
1779 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
1785 r
= sqlite3_step(stmt
);
1788 // Retry if the database was busy
1794 r
= pakfire_db_load_file(db
, fl
, stmt
);
1799 // All rows have been processed
1803 // Go to error in any other cases
1810 *filelist
= pakfire_filelist_ref(fl
);
1815 sqlite3_finalize(stmt
);
1817 pakfire_filelist_unref(fl
);