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/types.h>
38 #include <pakfire/util.h>
40 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
42 #define CURRENT_SCHEMA 8
43 #define SCHEMA_MIN_SUP 7
56 static void logging_callback(void* data
, int r
, const char* msg
) {
57 Pakfire pakfire
= (Pakfire
)data
;
59 ERROR(pakfire
, "Database Error: %s: %s\n",
60 sqlite3_errstr(r
), msg
);
63 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
66 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
69 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
70 } while (r
== SQLITE_BUSY
);
74 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
80 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
81 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
84 static int pakfire_db_commit(struct pakfire_db
* db
) {
85 return pakfire_db_execute(db
, "COMMIT");
88 static int pakfire_db_rollback(struct pakfire_db
* db
) {
89 return pakfire_db_execute(db
, "ROLLBACK");
93 This function performs any fast optimization and tries to truncate the WAL log file
94 to keep the database as compact as possible on disk.
96 static void pakfire_db_optimize(struct pakfire_db
* db
) {
97 pakfire_db_execute(db
, "PRAGMA optimize");
98 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
101 static void pakfire_db_free(struct pakfire_db
* db
) {
103 // Optimize the database before it is being closed
104 pakfire_db_optimize(db
);
106 // Close database handle
107 int r
= sqlite3_close(db
->handle
);
108 if (r
!= SQLITE_OK
) {
109 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
110 sqlite3_errmsg(db
->handle
));
114 pakfire_unref(db
->pakfire
);
119 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
120 sqlite3_stmt
* stmt
= NULL
;
121 sqlite3_value
* val
= NULL
;
124 const char* sql
= "SELECT val FROM settings WHERE key = ?";
126 // Prepare the statement
127 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
128 if (r
!= SQLITE_OK
) {
129 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
130 // sql, sqlite3_errmsg(db->handle));
135 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
136 if (r
!= SQLITE_OK
) {
137 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
141 // Execute the statement
143 r
= sqlite3_step(stmt
);
144 } while (r
== SQLITE_BUSY
);
146 // We should have read a row
151 val
= sqlite3_column_value(stmt
, 0);
153 ERROR(db
->pakfire
, "Could not read value\n");
157 // Copy value onto the heap
158 val
= sqlite3_value_dup(val
);
162 sqlite3_finalize(stmt
);
167 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
168 sqlite3_stmt
* stmt
= NULL
;
171 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
172 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
175 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
176 if (r
!= SQLITE_OK
) {
177 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
178 sql
, sqlite3_errmsg(db
->handle
));
183 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
184 if (r
!= SQLITE_OK
) {
185 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
190 r
= sqlite3_bind_int64(stmt
, 2, val
);
191 if (r
!= SQLITE_OK
) {
192 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
196 // Execute the statement
198 r
= sqlite3_step(stmt
);
199 } while (r
== SQLITE_BUSY
);
202 r
= (r
== SQLITE_OK
);
206 sqlite3_finalize(stmt
);
211 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
212 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
216 int schema
= sqlite3_value_int64(value
);
217 sqlite3_value_free(value
);
219 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
224 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
227 // Create settings table
228 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
232 // settings: Add a unique index on key
233 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
237 // Create packages table
238 r
= pakfire_db_execute(db
,
239 "CREATE TABLE IF NOT EXISTS packages("
240 "id INTEGER PRIMARY KEY, "
247 "inst_size INTEGER, "
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
, 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(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
, PakfireArchive archive
) {
754 sqlite3_stmt
* stmt
= NULL
;
757 // Get the filelist from the archive
758 PakfireFilelist 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
, PakfireArchive 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
, PakfireArchive 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, hash1, license, summary, description, uuid, vendor, build_host, "
1015 "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
));
1100 const char* hash1
= pakfire_package_get_checksum(pkg
);
1102 r
= sqlite3_bind_text(stmt
, 8, hash1
, -1, NULL
);
1104 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
1109 const char* license
= pakfire_package_get_license(pkg
);
1111 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1113 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1118 const char* summary
= pakfire_package_get_summary(pkg
);
1120 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1122 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1127 const char* description
= pakfire_package_get_description(pkg
);
1129 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1131 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1136 const char* uuid
= pakfire_package_get_uuid(pkg
);
1138 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1140 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1145 const char* vendor
= pakfire_package_get_vendor(pkg
);
1147 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1149 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1154 const char* build_host
= pakfire_package_get_build_host(pkg
);
1156 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1158 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1163 time_t build_time
= pakfire_package_get_build_time(pkg
);
1165 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1167 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1171 // Bind repository name
1172 PakfireRepo repo
= pakfire_package_get_repo(pkg
);
1174 const char* repo_name
= pakfire_repo_get_name(repo
);
1175 pakfire_repo_unref(repo
);
1177 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1183 r
= sqlite3_bind_null(stmt
, 16);
1188 // installed by the user?
1189 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1191 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1197 r
= sqlite3_step(stmt
);
1198 } while (r
== SQLITE_BUSY
);
1200 if (r
!= SQLITE_DONE
) {
1201 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1202 sqlite3_errmsg(db
->handle
));
1207 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1210 r
= sqlite3_finalize(stmt
);
1215 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1220 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1225 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1229 // All done, commit!
1230 r
= pakfire_db_commit(db
);
1238 sqlite3_finalize(stmt
);
1240 pakfire_db_rollback(db
);
1245 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1246 sqlite3_stmt
* stmt
= NULL
;
1249 // Fetch the package's UUID
1250 const char* uuid
= pakfire_package_get_uuid(pkg
);
1252 ERROR(db
->pakfire
, "Package has no UUID\n");
1256 r
= sqlite3_prepare_v2(db
->handle
,
1257 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1259 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1260 sqlite3_errmsg(db
->handle
));
1265 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1267 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1273 r
= sqlite3_step(stmt
);
1274 } while (r
== SQLITE_BUSY
);
1276 // Check if we have been successful
1277 if (r
!= SQLITE_DONE
) {
1278 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1287 sqlite3_finalize(stmt
);
1292 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1293 struct pakfire_package
* pkg
, const char* type
) {
1294 struct pakfire_scriptlet
* scriptlet
= NULL
;
1295 sqlite3_stmt
* stmt
= NULL
;
1298 // Fetch the package's UUID
1299 const char* uuid
= pakfire_package_get_uuid(pkg
);
1301 ERROR(db
->pakfire
, "Package has no UUID\n");
1305 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1306 JOIN scriptlets ON packages.id = scriptlets.pkg \
1307 WHERE packages.uuid = ? AND scriptlets.type = ?";
1309 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1311 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1312 sql
, sqlite3_errmsg(db
->handle
));
1317 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1319 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1323 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1325 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1329 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1333 r
= sqlite3_step(stmt
);
1334 } while (r
== SQLITE_BUSY
);
1336 // We have some payload
1337 if (r
== SQLITE_ROW
) {
1338 const void* data
= sqlite3_column_blob(stmt
, 1);
1339 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1341 // Create a scriptlet object
1342 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1349 sqlite3_finalize(stmt
);
1354 static int pakfire_db_load_package(struct pakfire_db
* db
, PakfireRepo repo
, sqlite3_stmt
* stmt
) {
1355 struct pakfire_package
* pkg
= NULL
;
1359 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1361 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1366 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1368 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1373 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1375 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1380 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1382 ERROR(db
->pakfire
, "Could not create package\n");
1387 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1389 pakfire_package_set_dbid(pkg
, id
);
1392 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1394 pakfire_package_set_groups(pkg
, groups
);
1398 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1400 pakfire_package_set_filename(pkg
, filename
);
1404 size_t size
= sqlite3_column_int64(stmt
, 6);
1406 pakfire_package_set_downloadsize(pkg
, size
);
1410 size
= sqlite3_column_int64(stmt
, 7);
1412 pakfire_package_set_installsize(pkg
, size
);
1416 const char* hash1
= (const char*)sqlite3_column_text(stmt
, 8);
1418 pakfire_package_set_checksum(pkg
, hash1
);
1422 const char* license
= (const char*)sqlite3_column_text(stmt
, 9);
1424 pakfire_package_set_license(pkg
, license
);
1428 const char* summary
= (const char*)sqlite3_column_text(stmt
, 10);
1430 pakfire_package_set_summary(pkg
, summary
);
1434 const char* description
= (const char*)sqlite3_column_text(stmt
, 11);
1436 pakfire_package_set_description(pkg
, description
);
1440 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 12);
1442 pakfire_package_set_uuid(pkg
, uuid
);
1446 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 13);
1448 pakfire_package_set_vendor(pkg
, vendor
);
1452 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 14);
1454 pakfire_package_set_build_host(pkg
, build_host
);
1458 time_t build_time
= sqlite3_column_int64(stmt
, 15);
1460 pakfire_package_set_build_time(pkg
, build_time
);
1464 time_t install_time
= sqlite3_column_int64(stmt
, 16);
1466 pakfire_package_set_install_time(pkg
, install_time
);
1469 // installed by user?
1470 int userinstalled
= sqlite3_column_int(stmt
, 17);
1472 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1475 const char* files
= (const char*)sqlite3_column_text(stmt
, 18);
1477 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1484 const struct dependency
{
1486 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1487 } dependencies
[] = {
1488 { 19, pakfire_package_add_provides
},
1489 { 20, pakfire_package_add_prerequires
},
1490 { 21, pakfire_package_add_requires
},
1491 { 22, pakfire_package_add_conflicts
},
1492 { 23, pakfire_package_add_obsoletes
},
1493 { 24, pakfire_package_add_recommends
},
1494 { 25, pakfire_package_add_suggests
},
1495 { 26, pakfire_package_add_supplements
},
1496 { 27, pakfire_package_add_enhances
},
1500 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1501 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1503 pakfire_parse_deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1512 pakfire_package_unref(pkg
);
1517 int pakfire_db_load(struct pakfire_db
* db
, PakfireRepo repo
) {
1518 sqlite3_stmt
* stmt
= NULL
;
1521 DEBUG(db
->pakfire
, "Loading package database...\n");
1523 // Drop contents of the repository
1524 pakfire_repo_clear(repo
);
1526 // Save starting time
1527 clock_t t_start
= clock();
1532 "name, evr, arch, id, groups, filename, size, inst_size, hash1, license, "
1533 "summary, description, uuid, vendor, build_host, build_time, "
1534 "strftime('%s', installed) AS installed, userinstalled, "
1536 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1539 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1540 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1543 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1544 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1545 ") AS prerequires, "
1547 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1548 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1551 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1552 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1555 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1556 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1559 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1560 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1563 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1564 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1567 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1568 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1569 ") AS supplements, "
1571 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1572 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1578 // Prepare the statement
1579 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1581 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1582 sql
, sqlite3_errmsg(db
->handle
));
1588 r
= sqlite3_step(stmt
);
1591 // Retry if the database was busy
1597 r
= pakfire_db_load_package(db
, repo
, stmt
);
1602 // All rows have been processed
1606 // Go to error in any other cases
1613 // Save time when we finished
1616 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
1617 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
1619 // Internalize repository
1620 pakfire_repo_internalize(repo
);
1627 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
1628 pakfire_repo_clear(repo
);
1632 sqlite3_finalize(stmt
);
1637 static int pakfire_db_load_file(struct pakfire_db
* db
, PakfireFilelist filelist
,
1638 sqlite3_stmt
* stmt
) {
1639 struct pakfire_file
* file
= NULL
;
1640 char abspath
[PATH_MAX
];
1643 // Create a new file object
1644 r
= pakfire_file_create(&file
, db
->pakfire
);
1649 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
1651 pakfire_file_set_path(file
, path
);
1654 r
= pakfire_make_path(db
->pakfire
, abspath
, path
);
1658 pakfire_file_set_abspath(file
, abspath
);
1661 size_t size
= sqlite3_column_int64(stmt
, 1);
1663 pakfire_file_set_size(file
, size
);
1666 mode_t mode
= sqlite3_column_int(stmt
, 2);
1668 pakfire_file_set_mode(file
, mode
);
1671 const char* user
= (const char*)sqlite3_column_text(stmt
, 3);
1673 pakfire_file_set_user(file
, user
);
1676 const char* group
= (const char*)sqlite3_column_text(stmt
, 4);
1678 pakfire_file_set_group(file
, group
);
1681 time_t ctime
= sqlite3_column_int64(stmt
, 5);
1683 pakfire_file_set_ctime(file
, ctime
);
1686 time_t mtime
= sqlite3_column_int64(stmt
, 6);
1688 pakfire_file_set_mtime(file
, mtime
);
1690 // Append the file to the filelist
1691 r
= pakfire_filelist_append(filelist
, file
);
1695 pakfire_file_unref(file
);
1700 int pakfire_db_package_filelist(struct pakfire_db
* db
, PakfireFilelist
* filelist
,
1701 struct pakfire_package
* pkg
) {
1702 PakfireFilelist fl
= NULL
;
1703 sqlite3_stmt
* stmt
= NULL
;
1706 // Fetch the package ID
1707 uint64_t id
= pakfire_package_get_dbid(pkg
);
1709 ERROR(db
->pakfire
, "Package did not have an ID\n");
1713 // Create a new filelist
1714 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
1716 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
1731 // Select all files that belong to this package
1735 // Filter out any files that are also in a different package (i.e. an update
1736 // that has already been installed and this is the cleanup of the obsolete pkg)
1741 "FROM files AS duplicates "
1743 "files.path = duplicates.path "
1745 "files.pkg != duplicates.pkg"
1748 // Return the longest paths first
1753 // Prepare the statement
1754 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1756 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1757 sql
, sqlite3_errmsg(db
->handle
));
1762 r
= sqlite3_bind_int64(stmt
, 1, id
);
1764 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
1770 r
= sqlite3_step(stmt
);
1773 // Retry if the database was busy
1779 r
= pakfire_db_load_file(db
, fl
, stmt
);
1784 // All rows have been processed
1788 // Go to error in any other cases
1795 *filelist
= pakfire_filelist_ref(fl
);
1800 sqlite3_finalize(stmt
);
1802 pakfire_filelist_unref(fl
);