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 #############################################################################*/
26 #include <pakfire/archive.h>
27 #include <pakfire/db.h>
28 #include <pakfire/file.h>
29 #include <pakfire/logging.h>
30 #include <pakfire/package.h>
31 #include <pakfire/pakfire.h>
32 #include <pakfire/private.h>
33 #include <pakfire/relationlist.h>
34 #include <pakfire/repo.h>
35 #include <pakfire/types.h>
36 #include <pakfire/util.h>
38 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
40 #define CURRENT_SCHEMA 8
41 #define SCHEMA_MIN_SUP 7
53 static void logging_callback(void* data
, int r
, const char* msg
) {
54 Pakfire pakfire
= (Pakfire
)data
;
56 ERROR(pakfire
, "Database Error: %s: %s\n",
57 sqlite3_errstr(r
), msg
);
60 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
63 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
66 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
67 } while (r
== SQLITE_BUSY
);
71 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
77 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
78 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
81 static int pakfire_db_commit(struct pakfire_db
* db
) {
82 return pakfire_db_execute(db
, "COMMIT");
85 static int pakfire_db_rollback(struct pakfire_db
* db
) {
86 return pakfire_db_execute(db
, "ROLLBACK");
90 This function performs any fast optimization and tries to truncate the WAL log file
91 to keep the database as compact as possible on disk.
93 static void pakfire_db_optimize(struct pakfire_db
* db
) {
94 pakfire_db_execute(db
, "PRAGMA optimize");
95 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
98 static void pakfire_db_free(struct pakfire_db
* db
) {
99 DEBUG(db
->pakfire
, "Releasing database at %p\n", 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, "
248 "inst_size INTEGER, "
256 "build_time INTEGER, "
257 "installed INTEGER, "
264 // packages: Create index to find package by name
265 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
269 // packages: Create unique index over UUID
270 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
274 // Create dependencies table
275 r
= pakfire_db_execute(db
,
276 "CREATE TABLE IF NOT EXISTS dependencies("
280 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
285 // dependencies: Add index over packages
286 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
290 // Create files table
291 r
= pakfire_db_execute(db
,
292 "CREATE TABLE IF NOT EXISTS files("
293 "id INTEGER PRIMARY KEY, "
305 "capabilities TEXT, "
306 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
311 // files: Add index over packages
312 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
316 // Create scriptlets table
317 r
= pakfire_db_execute(db
,
318 "CREATE TABLE IF NOT EXISTS scriptlets("
319 "id INTEGER PRIMARY KEY, "
323 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
328 // scriptlets: Add index over packages
329 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
336 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
337 // packages: Drop build_id column
340 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
341 // need to recreate the whole table and rename it afterwards. Annoying.
346 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
349 while (db
->schema
< CURRENT_SCHEMA
) {
350 // Begin a new transaction
351 r
= pakfire_db_begin_transaction(db
);
355 switch (db
->schema
) {
358 r
= pakfire_db_create_schema(db
);
362 db
->schema
= CURRENT_SCHEMA
;
366 r
= pakfire_db_migrate_to_schema_8(db
);
374 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
378 // Update the schema version
379 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
384 r
= pakfire_db_commit(db
);
392 pakfire_db_rollback(db
);
397 static int pakfire_db_setup(struct pakfire_db
* db
) {
401 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
403 // Enable foreign keys
404 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
406 // Make LIKE case-sensitive
407 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
409 // Fetch the current schema
410 db
->schema
= pakfire_db_get_schema(db
);
412 // Check if the schema is recent enough
413 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
414 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
419 // Done when not in read-write mode
420 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
423 // Disable secure delete
424 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
426 // Set database journal to WAL
427 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
428 if (r
!= SQLITE_OK
) {
429 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
430 sqlite3_errmsg(db
->handle
));
434 // Disable autocheckpoint
435 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
436 if (r
!= SQLITE_OK
) {
437 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
438 sqlite3_errmsg(db
->handle
));
442 // Create or migrate schema
443 r
= pakfire_db_migrate_schema(db
);
450 PAKFIRE_EXPORT
int pakfire_db_open(struct pakfire_db
** db
, Pakfire pakfire
, int flags
) {
453 struct pakfire_db
* o
= pakfire_calloc(1, sizeof(*o
));
457 DEBUG(pakfire
, "Allocated database at %p\n", o
);
459 o
->pakfire
= pakfire_ref(pakfire
);
462 int sqlite3_flags
= 0;
464 // Store mode & forward it to sqlite3
465 if (flags
& PAKFIRE_DB_READWRITE
) {
466 o
->mode
= PAKFIRE_DB_READWRITE
;
467 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
469 o
->mode
= PAKFIRE_DB_READONLY
;
470 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
474 char* path
= pakfire_make_path(o
->pakfire
, DATABASE_PATH
);
478 // Try to open the sqlite3 database file
479 r
= sqlite3_open_v2(path
, &o
->handle
, sqlite3_flags
, NULL
);
480 if (r
!= SQLITE_OK
) {
481 ERROR(pakfire
, "Could not open database %s: %s\n",
482 path
, sqlite3_errmsg(o
->handle
));
488 // Setup the database
489 r
= pakfire_db_setup(o
);
506 PAKFIRE_EXPORT
struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
512 PAKFIRE_EXPORT
struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
521 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
522 sqlite3_stmt
* stmt
= NULL
;
525 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
527 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
528 sqlite3_errmsg(db
->handle
));
533 unsigned long errors
= 0;
537 r
= sqlite3_step(stmt
);
538 } while (r
== SQLITE_BUSY
);
540 if (r
== SQLITE_ROW
) {
541 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
543 // If the message is "ok", the database has passed the check
544 if (strcmp(error
, "ok") == 0)
547 // Increment error counter
551 ERROR(db
->pakfire
, "%s\n", error
);
553 // Break on anything else
558 sqlite3_finalize(stmt
);
561 ERROR(db
->pakfire
, "Database integrity check failed\n");
563 INFO(db
->pakfire
, "Database integrity check passed\n");
568 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
569 sqlite3_stmt
* stmt
= NULL
;
572 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
574 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
575 sqlite3_errmsg(db
->handle
));
580 unsigned long errors
= 0;
584 r
= sqlite3_step(stmt
);
585 } while (r
== SQLITE_BUSY
);
587 if (r
== SQLITE_ROW
) {
588 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
589 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
590 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
591 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
593 // Increment error counter
597 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
598 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
600 // Break on anything else
605 sqlite3_finalize(stmt
);
608 ERROR(db
->pakfire
, "Foreign key check failed\n");
610 INFO(db
->pakfire
, "Foreign key check passed\n");
616 This function performs an integrity check of the database
618 PAKFIRE_EXPORT
int pakfire_db_check(struct pakfire_db
* db
) {
621 // Perform integrity check
622 r
= pakfire_db_integrity_check(db
);
626 // Perform foreign key check
627 r
= pakfire_db_foreign_key_check(db
);
634 // Returns the number of packages installed
635 PAKFIRE_EXPORT ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
636 sqlite3_stmt
* stmt
= NULL
;
637 ssize_t packages
= -1;
639 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
641 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
642 sqlite3_errmsg(db
->handle
));
648 r
= sqlite3_step(stmt
);
649 } while (r
== SQLITE_BUSY
);
651 if (r
== SQLITE_ROW
) {
652 packages
= sqlite3_column_int64(stmt
, 0);
655 sqlite3_finalize(stmt
);
660 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, PakfirePackage pkg
) {
661 sqlite3_stmt
* stmt
= NULL
;
664 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
666 // Prepare the statement
667 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
669 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
670 sql
, sqlite3_errmsg(db
->handle
));
674 const struct __relation
{
676 PakfireRelationList (*func
)(PakfirePackage
);
678 { "provides", pakfire_package_get_provides
},
679 { "prerequires", pakfire_package_get_prerequires
},
680 { "requires", pakfire_package_get_requires
},
681 { "conflicts", pakfire_package_get_conflicts
},
682 { "obsoletes", pakfire_package_get_obsoletes
},
683 { "recommends", pakfire_package_get_recommends
},
684 { "suggests", pakfire_package_get_suggests
},
688 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
689 PakfireRelationList list
= relation
->func(pkg
);
693 for (unsigned int i
= 0; i
< pakfire_relationlist_size(list
); i
++) {
694 PakfireRelation rel
= pakfire_relationlist_get(list
, i
);
698 char* dependency
= pakfire_relation_str(rel
);
700 pakfire_relation_unref(rel
);
706 r
= sqlite3_bind_int64(stmt
, 1, id
);
708 ERROR(db
->pakfire
, "Could not bind id: %s\n",
709 sqlite3_errmsg(db
->handle
));
710 pakfire_relation_unref(rel
);
715 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
717 ERROR(db
->pakfire
, "Could not bind type: %s\n",
718 sqlite3_errmsg(db
->handle
));
719 pakfire_relation_unref(rel
);
724 r
= sqlite3_bind_text(stmt
, 3, dependency
, -1, NULL
);
726 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
727 sqlite3_errmsg(db
->handle
));
728 pakfire_relation_unref(rel
);
734 r
= sqlite3_step(stmt
);
735 } while (r
== SQLITE_BUSY
);
737 pakfire_relation_unref(rel
);
740 // Reset bound values
744 pakfire_relationlist_unref(list
);
752 sqlite3_finalize(stmt
);
757 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, PakfireArchive archive
) {
758 sqlite3_stmt
* stmt
= NULL
;
761 // Get the filelist from the archive
762 PakfireFilelist filelist
= pakfire_archive_get_filelist(archive
);
764 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
768 // Nothing to do if the list is empty
769 if (pakfire_filelist_is_empty(filelist
))
772 const char* sql
= "INSERT INTO files(pkg, name, size, type, config, datafile, mode, "
773 "user, 'group', hash1, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
775 // Prepare the statement
776 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
778 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
779 sql
, sqlite3_errmsg(db
->handle
));
783 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
784 PakfireFile file
= pakfire_filelist_get(filelist
, i
);
787 r
= sqlite3_bind_int64(stmt
, 1, id
);
789 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
790 pakfire_file_unref(file
);
795 const char* name
= pakfire_file_get_name(file
);
797 r
= sqlite3_bind_text(stmt
, 2, name
, -1, NULL
);
799 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
800 pakfire_file_unref(file
);
805 size_t size
= pakfire_file_get_size(file
);
807 r
= sqlite3_bind_int64(stmt
, 3, size
);
809 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
810 pakfire_file_unref(file
);
814 // Bind type - XXX this is char which isn't very helpful
815 //char type = pakfire_file_get_type(file);
817 r
= sqlite3_bind_null(stmt
, 4);
819 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
820 pakfire_file_unref(file
);
824 // Bind config - XXX TODO
825 r
= sqlite3_bind_null(stmt
, 5);
827 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
828 pakfire_file_unref(file
);
832 // Bind datafile - XXX TODO
833 r
= sqlite3_bind_null(stmt
, 6);
835 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
836 pakfire_file_unref(file
);
841 mode_t mode
= pakfire_file_get_mode(file
);
843 r
= sqlite3_bind_int64(stmt
, 7, mode
);
845 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
846 pakfire_file_unref(file
);
851 const char* user
= pakfire_file_get_user(file
);
853 r
= sqlite3_bind_text(stmt
, 8, user
, -1, NULL
);
855 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
856 pakfire_file_unref(file
);
861 const char* group
= pakfire_file_get_group(file
);
863 r
= sqlite3_bind_text(stmt
, 9, group
, -1, NULL
);
865 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
866 pakfire_file_unref(file
);
871 const char* chksum
= pakfire_file_get_chksum(file
);
873 r
= sqlite3_bind_text(stmt
, 10, chksum
, -1, NULL
);
875 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
876 pakfire_file_unref(file
);
881 time_t mtime
= pakfire_file_get_time(file
);
883 r
= sqlite3_bind_int64(stmt
, 11, mtime
);
885 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
886 pakfire_file_unref(file
);
890 // Bind capabilities - XXX TODO
891 r
= sqlite3_bind_null(stmt
, 12);
893 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
894 pakfire_file_unref(file
);
900 r
= sqlite3_step(stmt
);
901 } while (r
== SQLITE_BUSY
);
903 // Move on to next file
904 pakfire_file_unref(file
);
906 // Reset bound values
915 sqlite3_finalize(stmt
);
917 pakfire_filelist_unref(filelist
);
922 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, PakfireArchive archive
) {
923 sqlite3_stmt
* stmt
= NULL
;
926 struct pakfire_scriptlet_type
* scriptlet_type
= PAKFIRE_SCRIPTLET_TYPES
;
927 struct pakfire_scriptlet
* scriptlet
;
929 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
931 // Prepare the statement
932 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
934 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
935 sql
, sqlite3_errmsg(db
->handle
));
939 while (scriptlet_type
->type
) {
940 // Fetch the scriptlet
941 scriptlet
= pakfire_archive_get_scriptlet(archive
, scriptlet_type
->type
);
943 // Go to next one if the archive does not have a scriptlet of the given type
950 r
= sqlite3_bind_int64(stmt
, 1, id
);
952 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
957 r
= sqlite3_bind_text(stmt
, 2, scriptlet_type
->handle
, -1, NULL
);
959 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
964 r
= sqlite3_bind_text(stmt
, 3, scriptlet
->data
, scriptlet
->size
, NULL
);
966 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
972 r
= sqlite3_step(stmt
);
973 } while (r
== SQLITE_BUSY
);
975 // Reset bound values
986 sqlite3_finalize(stmt
);
991 PAKFIRE_EXPORT
int pakfire_db_add_package(struct pakfire_db
* db
,
992 PakfirePackage pkg
, PakfireArchive archive
) {
993 sqlite3_stmt
* stmt
= NULL
;
996 // Begin a new transaction
997 r
= pakfire_db_begin_transaction(db
);
1001 const char* sql
= "INSERT INTO packages(name, epoch, version, release, arch, groups, "
1002 "filename, size, inst_size, hash1, license, summary, description, uuid, vendor, "
1003 "build_host, build_time, installed, repository, reason) VALUES(?, ?, "
1004 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
1006 // Prepare the statement
1007 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1008 if (r
!= SQLITE_OK
) {
1009 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1010 sql
, sqlite3_errmsg(db
->handle
));
1015 const char* name
= pakfire_package_get_name(pkg
);
1017 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1019 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1024 unsigned long epoch
= pakfire_package_get_epoch(pkg
);
1026 r
= sqlite3_bind_int64(stmt
, 2, epoch
);
1028 ERROR(db
->pakfire
, "Could not bind epoch: %s\n", sqlite3_errmsg(db
->handle
));
1033 const char* version
= pakfire_package_get_version(pkg
);
1035 r
= sqlite3_bind_text(stmt
, 3, version
, -1, NULL
);
1037 ERROR(db
->pakfire
, "Could not bind version: %s\n", sqlite3_errmsg(db
->handle
));
1042 const char* release
= pakfire_package_get_release(pkg
);
1044 r
= sqlite3_bind_text(stmt
, 4, release
, -1, NULL
);
1046 ERROR(db
->pakfire
, "Could not bind release: %s\n", sqlite3_errmsg(db
->handle
));
1051 const char* arch
= pakfire_package_get_arch(pkg
);
1053 r
= sqlite3_bind_text(stmt
, 5, arch
, -1, NULL
);
1055 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1060 const char* groups
= pakfire_package_get_groups(pkg
);
1062 r
= sqlite3_bind_text(stmt
, 6, groups
, -1, NULL
);
1064 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1069 const char* filename
= pakfire_package_get_filename(pkg
);
1071 r
= sqlite3_bind_text(stmt
, 7, filename
, -1, NULL
);
1073 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1078 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1080 r
= sqlite3_bind_int64(stmt
, 8, size
);
1082 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1086 // Bind installed size
1087 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1089 r
= sqlite3_bind_int64(stmt
, 9, inst_size
);
1091 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1096 const char* hash1
= pakfire_package_get_checksum(pkg
);
1098 r
= sqlite3_bind_text(stmt
, 10, hash1
, -1, NULL
);
1100 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
1105 const char* license
= pakfire_package_get_license(pkg
);
1107 r
= sqlite3_bind_text(stmt
, 11, license
, -1, NULL
);
1109 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1114 const char* summary
= pakfire_package_get_summary(pkg
);
1116 r
= sqlite3_bind_text(stmt
, 12, summary
, -1, NULL
);
1118 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1123 const char* description
= pakfire_package_get_description(pkg
);
1125 r
= sqlite3_bind_text(stmt
, 13, description
, -1, NULL
);
1127 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1132 const char* uuid
= pakfire_package_get_uuid(pkg
);
1134 r
= sqlite3_bind_text(stmt
, 14, uuid
, -1, NULL
);
1136 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1141 const char* vendor
= pakfire_package_get_vendor(pkg
);
1143 r
= sqlite3_bind_text(stmt
, 15, vendor
, -1, NULL
);
1145 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1150 const char* buildhost
= pakfire_package_get_buildhost(pkg
);
1152 r
= sqlite3_bind_text(stmt
, 16, buildhost
, -1, NULL
);
1154 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1159 unsigned long long build_time
= pakfire_package_get_buildtime(pkg
);
1161 r
= sqlite3_bind_int64(stmt
, 17, build_time
);
1163 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1167 // Bind repository name
1168 PakfireRepo repo
= pakfire_package_get_repo(pkg
);
1170 const char* repo_name
= pakfire_repo_get_name(repo
);
1171 pakfire_repo_unref(repo
);
1173 r
= sqlite3_bind_text(stmt
, 18, repo_name
, -1, NULL
);
1179 r
= sqlite3_bind_null(stmt
, 18);
1184 // XXX TODO Bind reason
1185 r
= sqlite3_bind_null(stmt
, 19);
1191 r
= sqlite3_step(stmt
);
1192 } while (r
== SQLITE_BUSY
);
1194 if (r
!= SQLITE_DONE
) {
1195 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1196 sqlite3_errmsg(db
->handle
));
1201 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1204 r
= sqlite3_finalize(stmt
);
1209 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1214 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1219 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1223 // All done, commit!
1224 r
= pakfire_db_commit(db
);
1232 sqlite3_finalize(stmt
);
1234 pakfire_db_rollback(db
);
1239 PAKFIRE_EXPORT
int pakfire_db_remove_package(struct pakfire_db
* db
, PakfirePackage pkg
) {
1240 sqlite3_stmt
* stmt
= NULL
;
1243 // Fetch the package's UUID
1244 const char* uuid
= pakfire_package_get_uuid(pkg
);
1246 ERROR(db
->pakfire
, "Package has no UUID\n");
1250 r
= sqlite3_prepare_v2(db
->handle
,
1251 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1253 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1254 sqlite3_errmsg(db
->handle
));
1259 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1261 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1267 r
= sqlite3_step(stmt
);
1268 } while (r
== SQLITE_BUSY
);
1270 // Check if we have been successful
1271 if (r
!= SQLITE_DONE
) {
1272 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1281 sqlite3_finalize(stmt
);