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/logging.h>
33 #include <pakfire/package.h>
34 #include <pakfire/pakfire.h>
35 #include <pakfire/repo.h>
36 #include <pakfire/types.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
55 static void logging_callback(void* data
, int r
, const char* msg
) {
56 Pakfire pakfire
= (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, "
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 // Create scriptlets table
316 r
= pakfire_db_execute(db
,
317 "CREATE TABLE IF NOT EXISTS scriptlets("
318 "id INTEGER PRIMARY KEY, "
322 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
327 // scriptlets: Add index over packages
328 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
335 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
336 // packages: Drop build_id column
339 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
340 // need to recreate the whole table and rename it afterwards. Annoying.
345 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
348 while (db
->schema
< CURRENT_SCHEMA
) {
349 // Begin a new transaction
350 r
= pakfire_db_begin_transaction(db
);
354 switch (db
->schema
) {
357 r
= pakfire_db_create_schema(db
);
361 db
->schema
= CURRENT_SCHEMA
;
365 r
= pakfire_db_migrate_to_schema_8(db
);
373 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
377 // Update the schema version
378 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
383 r
= pakfire_db_commit(db
);
391 pakfire_db_rollback(db
);
396 static int pakfire_db_setup(struct pakfire_db
* db
) {
400 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
402 // Enable foreign keys
403 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
405 // Make LIKE case-sensitive
406 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
408 // Fetch the current schema
409 db
->schema
= pakfire_db_get_schema(db
);
411 // Check if the schema is recent enough
412 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
413 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
418 // Done when not in read-write mode
419 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
422 // Disable secure delete
423 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
425 // Set database journal to WAL
426 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
427 if (r
!= SQLITE_OK
) {
428 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
429 sqlite3_errmsg(db
->handle
));
433 // Disable autocheckpoint
434 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
435 if (r
!= SQLITE_OK
) {
436 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
437 sqlite3_errmsg(db
->handle
));
441 // Create or migrate schema
442 r
= pakfire_db_migrate_schema(db
);
449 int pakfire_db_open(struct pakfire_db
** db
, Pakfire pakfire
, int flags
) {
452 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
456 o
->pakfire
= pakfire_ref(pakfire
);
459 int sqlite3_flags
= 0;
461 // Store mode & forward it to sqlite3
462 if (flags
& PAKFIRE_DB_READWRITE
) {
463 o
->mode
= PAKFIRE_DB_READWRITE
;
464 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
466 o
->mode
= PAKFIRE_DB_READONLY
;
467 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
471 r
= pakfire_make_path(o
->pakfire
, o
->path
, DATABASE_PATH
);
475 // Try to open the sqlite3 database file
476 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
477 if (r
!= SQLITE_OK
) {
478 ERROR(pakfire
, "Could not open database %s: %s\n",
479 o
->path
, sqlite3_errmsg(o
->handle
));
485 // Setup the database
486 r
= pakfire_db_setup(o
);
500 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
506 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
515 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
516 sqlite3_stmt
* stmt
= NULL
;
519 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
521 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
522 sqlite3_errmsg(db
->handle
));
527 unsigned long errors
= 0;
531 r
= sqlite3_step(stmt
);
532 } while (r
== SQLITE_BUSY
);
534 if (r
== SQLITE_ROW
) {
535 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
537 // If the message is "ok", the database has passed the check
538 if (strcmp(error
, "ok") == 0)
541 // Increment error counter
545 ERROR(db
->pakfire
, "%s\n", error
);
547 // Break on anything else
552 sqlite3_finalize(stmt
);
555 ERROR(db
->pakfire
, "Database integrity check failed\n");
557 INFO(db
->pakfire
, "Database integrity check passed\n");
562 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
563 sqlite3_stmt
* stmt
= NULL
;
566 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
568 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
569 sqlite3_errmsg(db
->handle
));
574 unsigned long errors
= 0;
578 r
= sqlite3_step(stmt
);
579 } while (r
== SQLITE_BUSY
);
581 if (r
== SQLITE_ROW
) {
582 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
583 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
584 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
585 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
587 // Increment error counter
591 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
592 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
594 // Break on anything else
599 sqlite3_finalize(stmt
);
602 ERROR(db
->pakfire
, "Foreign key check failed\n");
604 INFO(db
->pakfire
, "Foreign key check passed\n");
610 This function performs an integrity check of the database
612 int pakfire_db_check(struct pakfire_db
* db
) {
615 // Perform integrity check
616 r
= pakfire_db_integrity_check(db
);
620 // Perform foreign key check
621 r
= pakfire_db_foreign_key_check(db
);
628 // Returns the number of packages installed
629 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
630 sqlite3_stmt
* stmt
= NULL
;
631 ssize_t packages
= -1;
633 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
635 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
636 sqlite3_errmsg(db
->handle
));
642 r
= sqlite3_step(stmt
);
643 } while (r
== SQLITE_BUSY
);
645 if (r
== SQLITE_ROW
) {
646 packages
= sqlite3_column_int64(stmt
, 0);
649 sqlite3_finalize(stmt
);
654 static void pakfire_db_add_userinstalled(Pakfire pakfire
, const char* name
) {
655 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
657 // Convert name to ID
658 Id id
= pool_str2id(pool
, name
, 1);
660 // Append it to pooljobs
661 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
664 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, PakfirePackage pkg
) {
665 sqlite3_stmt
* stmt
= NULL
;
668 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
670 // Prepare the statement
671 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
673 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
674 sql
, sqlite3_errmsg(db
->handle
));
678 const struct __relation
{
680 char** (*func
)(PakfirePackage
);
682 { "provides", pakfire_package_get_provides
},
683 { "prerequires", pakfire_package_get_prerequires
},
684 { "requires", pakfire_package_get_requires
},
685 { "conflicts", pakfire_package_get_conflicts
},
686 { "obsoletes", pakfire_package_get_obsoletes
},
687 { "recommends", pakfire_package_get_recommends
},
688 { "suggests", pakfire_package_get_suggests
},
689 { "supplements", pakfire_package_get_supplements
},
690 { "enhances", pakfire_package_get_enhances
},
694 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
695 char** list
= relation
->func(pkg
);
699 for (char** dep
= list
; *dep
; dep
++) {
701 r
= sqlite3_bind_int64(stmt
, 1, id
);
703 ERROR(db
->pakfire
, "Could not bind id: %s\n",
704 sqlite3_errmsg(db
->handle
));
709 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
711 ERROR(db
->pakfire
, "Could not bind type: %s\n",
712 sqlite3_errmsg(db
->handle
));
717 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
719 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
720 sqlite3_errmsg(db
->handle
));
726 r
= sqlite3_step(stmt
);
727 } while (r
== SQLITE_BUSY
);
731 // Reset bound values
743 sqlite3_finalize(stmt
);
748 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, PakfireArchive archive
) {
749 sqlite3_stmt
* stmt
= NULL
;
752 // Get the filelist from the archive
753 PakfireFilelist filelist
= pakfire_archive_get_filelist(archive
);
755 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
759 // Nothing to do if the list is empty
760 if (pakfire_filelist_is_empty(filelist
)) {
765 const char* sql
= "INSERT INTO files(pkg, path, size, type, config, datafile, mode, "
766 "user, 'group', hash1, ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
768 // Prepare the statement
769 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
771 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
772 sql
, sqlite3_errmsg(db
->handle
));
776 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
777 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
780 r
= sqlite3_bind_int64(stmt
, 1, id
);
782 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
783 pakfire_file_unref(file
);
788 const char* path
= pakfire_file_get_path(file
);
790 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
792 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
793 pakfire_file_unref(file
);
798 size_t size
= pakfire_file_get_size(file
);
800 r
= sqlite3_bind_int64(stmt
, 3, size
);
802 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
803 pakfire_file_unref(file
);
807 // Bind type - XXX this is char which isn't very helpful
808 //char type = pakfire_file_get_type(file);
810 r
= sqlite3_bind_null(stmt
, 4);
812 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
813 pakfire_file_unref(file
);
817 // Bind config - XXX TODO
818 r
= sqlite3_bind_null(stmt
, 5);
820 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
821 pakfire_file_unref(file
);
825 // Bind datafile - XXX TODO
826 r
= sqlite3_bind_null(stmt
, 6);
828 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
829 pakfire_file_unref(file
);
834 mode_t mode
= pakfire_file_get_mode(file
);
836 r
= sqlite3_bind_int64(stmt
, 7, mode
);
838 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
839 pakfire_file_unref(file
);
844 const char* user
= pakfire_file_get_user(file
);
846 r
= sqlite3_bind_text(stmt
, 8, user
, -1, NULL
);
848 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
849 pakfire_file_unref(file
);
854 const char* group
= pakfire_file_get_group(file
);
856 r
= sqlite3_bind_text(stmt
, 9, group
, -1, NULL
);
858 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
859 pakfire_file_unref(file
);
864 const char* chksum
= pakfire_file_get_chksum(file
);
866 r
= sqlite3_bind_text(stmt
, 10, chksum
, -1, NULL
);
868 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
869 pakfire_file_unref(file
);
874 time_t ctime
= pakfire_file_get_ctime(file
);
876 r
= sqlite3_bind_int64(stmt
, 11, ctime
);
878 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
879 pakfire_file_unref(file
);
884 time_t mtime
= pakfire_file_get_mtime(file
);
886 r
= sqlite3_bind_int64(stmt
, 12, mtime
);
888 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
889 pakfire_file_unref(file
);
893 // Bind capabilities - XXX TODO
894 r
= sqlite3_bind_null(stmt
, 13);
896 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
897 pakfire_file_unref(file
);
903 r
= sqlite3_step(stmt
);
904 } while (r
== SQLITE_BUSY
);
906 // Move on to next file
907 pakfire_file_unref(file
);
910 if (r
!= SQLITE_DONE
) {
911 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
912 sqlite3_errmsg(db
->handle
));
916 // Reset bound values
925 sqlite3_finalize(stmt
);
927 pakfire_filelist_unref(filelist
);
932 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, PakfireArchive archive
) {
933 sqlite3_stmt
* stmt
= NULL
;
937 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
939 // Prepare the statement
940 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
942 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
943 sql
, sqlite3_errmsg(db
->handle
));
947 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
948 // Fetch the scriptlet
949 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
954 r
= sqlite3_bind_int64(stmt
, 1, id
);
956 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
957 pakfire_scriptlet_unref(scriptlet
);
962 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
964 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
965 pakfire_scriptlet_unref(scriptlet
);
969 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
972 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
974 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
975 pakfire_scriptlet_unref(scriptlet
);
981 r
= sqlite3_step(stmt
);
982 } while (r
== SQLITE_BUSY
);
985 if (r
!= SQLITE_DONE
) {
986 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
987 sqlite3_errmsg(db
->handle
));
988 pakfire_scriptlet_unref(scriptlet
);
992 pakfire_scriptlet_unref(scriptlet
);
994 // Reset bound values
1003 sqlite3_finalize(stmt
);
1008 int pakfire_db_add_package(struct pakfire_db
* db
,
1009 PakfirePackage pkg
, PakfireArchive archive
, int userinstalled
) {
1010 sqlite3_stmt
* stmt
= NULL
;
1013 // Begin a new transaction
1014 r
= pakfire_db_begin_transaction(db
);
1018 const char* sql
= "INSERT INTO packages(name, evr, arch, groups, filename, size, "
1019 "inst_size, hash1, license, summary, description, uuid, vendor, build_host, "
1020 "build_time, installed, repository, userinstalled) "
1021 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
1023 // Prepare the statement
1024 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1025 if (r
!= SQLITE_OK
) {
1026 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1027 sql
, sqlite3_errmsg(db
->handle
));
1032 const char* name
= pakfire_package_get_name(pkg
);
1034 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1036 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1041 const char* evr
= pakfire_package_get_evr(pkg
);
1043 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1045 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1050 const char* arch
= pakfire_package_get_arch(pkg
);
1052 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1054 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1059 char* groups
= pakfire_package_get_groups(pkg
);
1061 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1063 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1072 r
= sqlite3_bind_null(stmt
, 4);
1078 const char* filename
= pakfire_package_get_filename(pkg
);
1080 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1082 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1087 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1089 r
= sqlite3_bind_int64(stmt
, 6, size
);
1091 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1095 // Bind installed size
1096 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1098 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1100 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1105 const char* hash1
= pakfire_package_get_checksum(pkg
);
1107 r
= sqlite3_bind_text(stmt
, 8, hash1
, -1, NULL
);
1109 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
1114 const char* license
= pakfire_package_get_license(pkg
);
1116 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1118 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1123 const char* summary
= pakfire_package_get_summary(pkg
);
1125 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1127 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1132 const char* description
= pakfire_package_get_description(pkg
);
1134 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1136 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1141 const char* uuid
= pakfire_package_get_uuid(pkg
);
1143 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1145 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1150 const char* vendor
= pakfire_package_get_vendor(pkg
);
1152 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1154 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1159 const char* build_host
= pakfire_package_get_build_host(pkg
);
1161 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1163 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1168 time_t build_time
= pakfire_package_get_build_time(pkg
);
1170 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1172 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1176 // Bind repository name
1177 PakfireRepo repo
= pakfire_package_get_repo(pkg
);
1179 const char* repo_name
= pakfire_repo_get_name(repo
);
1180 pakfire_repo_unref(repo
);
1182 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1188 r
= sqlite3_bind_null(stmt
, 16);
1193 // installed by the user?
1194 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1196 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1202 r
= sqlite3_step(stmt
);
1203 } while (r
== SQLITE_BUSY
);
1205 if (r
!= SQLITE_DONE
) {
1206 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1207 sqlite3_errmsg(db
->handle
));
1212 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1215 r
= sqlite3_finalize(stmt
);
1220 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1225 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1230 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1234 // All done, commit!
1235 r
= pakfire_db_commit(db
);
1243 sqlite3_finalize(stmt
);
1245 pakfire_db_rollback(db
);
1250 int pakfire_db_remove_package(struct pakfire_db
* db
, PakfirePackage pkg
) {
1251 sqlite3_stmt
* stmt
= NULL
;
1254 // Fetch the package's UUID
1255 const char* uuid
= pakfire_package_get_uuid(pkg
);
1257 ERROR(db
->pakfire
, "Package has no UUID\n");
1261 r
= sqlite3_prepare_v2(db
->handle
,
1262 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1264 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1265 sqlite3_errmsg(db
->handle
));
1270 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1272 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1278 r
= sqlite3_step(stmt
);
1279 } while (r
== SQLITE_BUSY
);
1281 // Check if we have been successful
1282 if (r
!= SQLITE_DONE
) {
1283 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1292 sqlite3_finalize(stmt
);
1297 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1298 PakfirePackage pkg
, const char* type
) {
1299 struct pakfire_scriptlet
* scriptlet
= NULL
;
1300 sqlite3_stmt
* stmt
= NULL
;
1303 // Fetch the package's UUID
1304 const char* uuid
= pakfire_package_get_uuid(pkg
);
1306 ERROR(db
->pakfire
, "Package has no UUID\n");
1310 const char* sql
= "SELECT scriptlet.scriptlet FROM packages \
1311 JOIN scriptlets ON packages.id = scriptlets.pkg \
1312 WHERE packages.uuid = ? AND scriptlets.type = ?";
1314 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1316 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1317 sql
, sqlite3_errmsg(db
->handle
));
1322 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1324 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1328 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1330 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1334 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1338 r
= sqlite3_step(stmt
);
1339 } while (r
== SQLITE_BUSY
);
1341 // We have some payload
1342 if (r
== SQLITE_ROW
) {
1343 const void* data
= sqlite3_column_blob(stmt
, 1);
1344 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1346 // Create a scriptlet object
1347 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1354 sqlite3_finalize(stmt
);
1359 static int pakfire_db_load_package(struct pakfire_db
* db
, PakfireRepo repo
, sqlite3_stmt
* stmt
) {
1360 PakfirePackage pkg
= NULL
;
1364 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1366 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1371 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1373 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1378 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1380 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1385 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1387 ERROR(db
->pakfire
, "Could not create package\n");
1392 const char* groups
= (const char*)sqlite3_column_text(stmt
, 3);
1394 pakfire_package_set_groups(pkg
, groups
);
1398 const char* filename
= (const char*)sqlite3_column_text(stmt
, 4);
1400 pakfire_package_set_filename(pkg
, filename
);
1404 size_t size
= sqlite3_column_int64(stmt
, 5);
1406 pakfire_package_set_downloadsize(pkg
, size
);
1410 size
= sqlite3_column_int64(stmt
, 6);
1412 pakfire_package_set_installsize(pkg
, size
);
1416 const char* hash1
= (const char*)sqlite3_column_text(stmt
, 7);
1418 pakfire_package_set_checksum(pkg
, hash1
);
1422 const char* license
= (const char*)sqlite3_column_text(stmt
, 8);
1424 pakfire_package_set_license(pkg
, license
);
1428 const char* summary
= (const char*)sqlite3_column_text(stmt
, 9);
1430 pakfire_package_set_summary(pkg
, summary
);
1434 const char* description
= (const char*)sqlite3_column_text(stmt
, 10);
1436 pakfire_package_set_description(pkg
, description
);
1440 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 11);
1442 pakfire_package_set_uuid(pkg
, uuid
);
1446 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 12);
1448 pakfire_package_set_vendor(pkg
, vendor
);
1452 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 13);
1454 pakfire_package_set_build_host(pkg
, build_host
);
1458 time_t build_time
= sqlite3_column_int64(stmt
, 14);
1460 pakfire_package_set_build_time(pkg
, build_time
);
1464 time_t install_time
= sqlite3_column_int64(stmt
, 15);
1466 pakfire_package_set_install_time(pkg
, install_time
);
1469 // installed by user?
1470 int userinstalled
= sqlite3_column_int(stmt
, 16);
1472 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1475 const char* files
= (const char*)sqlite3_column_text(stmt
, 17);
1477 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1484 const struct dependency
{
1486 void (*func
)(PakfirePackage pkg
, const char* dep
);
1487 } dependencies
[] = {
1488 { 18, pakfire_package_add_provides
},
1489 { 19, pakfire_package_add_prerequires
},
1490 { 20, pakfire_package_add_requires
},
1491 { 21, pakfire_package_add_conflicts
},
1492 { 22, pakfire_package_add_obsoletes
},
1493 { 23, pakfire_package_add_recommends
},
1494 { 24, pakfire_package_add_suggests
},
1495 { 25, pakfire_package_add_supplements
},
1496 { 26, 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, groups, filename, size, inst_size, hash1, license, summary, "
1533 "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
);