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 // Create dependencies table
270 r
= pakfire_db_execute(db
,
271 "CREATE TABLE IF NOT EXISTS dependencies("
275 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
280 // dependencies: Add index over packages
281 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
285 // Create files table
286 r
= pakfire_db_execute(db
,
287 "CREATE TABLE IF NOT EXISTS files("
288 "id INTEGER PRIMARY KEY, "
300 "capabilities TEXT, "
301 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
306 // files: Add index over packages
307 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
311 // Create scriptlets table
312 r
= pakfire_db_execute(db
,
313 "CREATE TABLE IF NOT EXISTS scriptlets("
314 "id INTEGER PRIMARY KEY, "
318 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
323 // scriptlets: Add index over packages
324 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
331 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
332 // packages: Drop build_id column
335 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
336 // need to recreate the whole table and rename it afterwards. Annoying.
341 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
344 while (db
->schema
< CURRENT_SCHEMA
) {
345 // Begin a new transaction
346 r
= pakfire_db_begin_transaction(db
);
350 switch (db
->schema
) {
353 r
= pakfire_db_create_schema(db
);
357 db
->schema
= CURRENT_SCHEMA
;
361 r
= pakfire_db_migrate_to_schema_8(db
);
369 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
373 // Update the schema version
374 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
379 r
= pakfire_db_commit(db
);
387 pakfire_db_rollback(db
);
392 static int pakfire_db_setup(struct pakfire_db
* db
) {
396 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
398 // Enable foreign keys
399 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
401 // Make LIKE case-sensitive
402 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
404 // Fetch the current schema
405 db
->schema
= pakfire_db_get_schema(db
);
407 // Check if the schema is recent enough
408 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
409 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
414 // Done when not in read-write mode
415 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
418 // Disable secure delete
419 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
421 // Set database journal to WAL
422 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
423 if (r
!= SQLITE_OK
) {
424 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
425 sqlite3_errmsg(db
->handle
));
429 // Disable autocheckpoint
430 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
431 if (r
!= SQLITE_OK
) {
432 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
433 sqlite3_errmsg(db
->handle
));
437 // Create or migrate schema
438 r
= pakfire_db_migrate_schema(db
);
445 PAKFIRE_EXPORT
int pakfire_db_open(struct pakfire_db
** db
, Pakfire pakfire
, int flags
) {
448 struct pakfire_db
* o
= pakfire_calloc(1, sizeof(*o
));
452 DEBUG(pakfire
, "Allocated database at %p\n", o
);
454 o
->pakfire
= pakfire_ref(pakfire
);
457 int sqlite3_flags
= 0;
459 // Store mode & forward it to sqlite3
460 if (flags
& PAKFIRE_DB_READWRITE
) {
461 o
->mode
= PAKFIRE_DB_READWRITE
;
462 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
464 o
->mode
= PAKFIRE_DB_READONLY
;
465 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
469 char* path
= pakfire_make_path(o
->pakfire
, DATABASE_PATH
);
473 // Try to open the sqlite3 database file
474 r
= sqlite3_open_v2(path
, &o
->handle
, sqlite3_flags
, NULL
);
475 if (r
!= SQLITE_OK
) {
476 ERROR(pakfire
, "Could not open database %s: %s\n",
477 path
, sqlite3_errmsg(o
->handle
));
483 // Setup the database
484 r
= pakfire_db_setup(o
);
501 PAKFIRE_EXPORT
struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
507 PAKFIRE_EXPORT
struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
516 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
517 sqlite3_stmt
* stmt
= NULL
;
520 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
522 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
523 sqlite3_errmsg(db
->handle
));
528 unsigned long errors
= 0;
532 r
= sqlite3_step(stmt
);
533 } while (r
== SQLITE_BUSY
);
535 if (r
== SQLITE_ROW
) {
536 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
538 // If the message is "ok", the database has passed the check
539 if (strcmp(error
, "ok") == 0)
542 // Increment error counter
546 ERROR(db
->pakfire
, "%s\n", error
);
548 // Break on anything else
553 sqlite3_finalize(stmt
);
556 ERROR(db
->pakfire
, "Database integrity check failed\n");
558 INFO(db
->pakfire
, "Database integrity check passed\n");
563 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
564 sqlite3_stmt
* stmt
= NULL
;
567 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
569 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
570 sqlite3_errmsg(db
->handle
));
575 unsigned long errors
= 0;
579 r
= sqlite3_step(stmt
);
580 } while (r
== SQLITE_BUSY
);
582 if (r
== SQLITE_ROW
) {
583 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
584 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
585 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
586 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
588 // Increment error counter
592 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
593 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
595 // Break on anything else
600 sqlite3_finalize(stmt
);
603 ERROR(db
->pakfire
, "Foreign key check failed\n");
605 INFO(db
->pakfire
, "Foreign key check passed\n");
611 This function performs an integrity check of the database
613 PAKFIRE_EXPORT
int pakfire_db_check(struct pakfire_db
* db
) {
616 // Perform integrity check
617 r
= pakfire_db_integrity_check(db
);
621 // Perform foreign key check
622 r
= pakfire_db_foreign_key_check(db
);
629 // Returns the number of packages installed
630 PAKFIRE_EXPORT ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
631 sqlite3_stmt
* stmt
= NULL
;
632 ssize_t packages
= -1;
634 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
636 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
637 sqlite3_errmsg(db
->handle
));
643 r
= sqlite3_step(stmt
);
644 } while (r
== SQLITE_BUSY
);
646 if (r
== SQLITE_ROW
) {
647 packages
= sqlite3_column_int64(stmt
, 0);
650 sqlite3_finalize(stmt
);
655 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, PakfirePackage pkg
) {
656 sqlite3_stmt
* stmt
= NULL
;
659 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
661 // Prepare the statement
662 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
664 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
665 sql
, sqlite3_errmsg(db
->handle
));
669 const struct __relation
{
671 PakfireRelationList (*func
)(PakfirePackage
);
673 { "provides", pakfire_package_get_provides
},
674 { "prerequires", pakfire_package_get_prerequires
},
675 { "requires", pakfire_package_get_requires
},
676 { "conflicts", pakfire_package_get_conflicts
},
677 { "obsoletes", pakfire_package_get_obsoletes
},
678 { "recommends", pakfire_package_get_recommends
},
679 { "suggests", pakfire_package_get_suggests
},
683 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
684 PakfireRelationList list
= relation
->func(pkg
);
688 for (unsigned int i
= 0; i
< pakfire_relationlist_size(list
); i
++) {
689 PakfireRelation rel
= pakfire_relationlist_get(list
, i
);
693 char* dependency
= pakfire_relation_str(rel
);
695 pakfire_relation_unref(rel
);
701 r
= sqlite3_bind_int64(stmt
, 1, id
);
703 ERROR(db
->pakfire
, "Could not bind id: %s\n",
704 sqlite3_errmsg(db
->handle
));
705 pakfire_relation_unref(rel
);
710 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
712 ERROR(db
->pakfire
, "Could not bind type: %s\n",
713 sqlite3_errmsg(db
->handle
));
714 pakfire_relation_unref(rel
);
719 r
= sqlite3_bind_text(stmt
, 3, dependency
, -1, NULL
);
721 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
722 sqlite3_errmsg(db
->handle
));
723 pakfire_relation_unref(rel
);
729 r
= sqlite3_step(stmt
);
730 } while (r
== SQLITE_BUSY
);
732 pakfire_relation_unref(rel
);
735 // Reset bound values
739 pakfire_relationlist_unref(list
);
747 sqlite3_finalize(stmt
);
752 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, PakfireArchive archive
) {
753 sqlite3_stmt
* stmt
= NULL
;
756 // Get the filelist from the archive
757 PakfireFilelist filelist
= pakfire_archive_get_filelist(archive
);
759 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
763 // Nothing to do if the list is empty
764 if (pakfire_filelist_is_empty(filelist
))
767 const char* sql
= "INSERT INTO files(pkg, name, size, type, config, datafile, mode, "
768 "user, 'group', hash1, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
770 // Prepare the statement
771 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
773 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
774 sql
, sqlite3_errmsg(db
->handle
));
778 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
779 PakfireFile file
= pakfire_filelist_get(filelist
, i
);
782 r
= sqlite3_bind_int64(stmt
, 1, id
);
784 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
785 pakfire_file_unref(file
);
790 const char* name
= pakfire_file_get_name(file
);
792 r
= sqlite3_bind_text(stmt
, 2, name
, -1, NULL
);
794 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
795 pakfire_file_unref(file
);
800 size_t size
= pakfire_file_get_size(file
);
802 r
= sqlite3_bind_int64(stmt
, 3, size
);
804 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
805 pakfire_file_unref(file
);
809 // Bind type - XXX this is char which isn't very helpful
810 //char type = pakfire_file_get_type(file);
812 r
= sqlite3_bind_null(stmt
, 4);
814 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
815 pakfire_file_unref(file
);
819 // Bind config - XXX TODO
820 r
= sqlite3_bind_null(stmt
, 5);
822 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
823 pakfire_file_unref(file
);
827 // Bind datafile - XXX TODO
828 r
= sqlite3_bind_null(stmt
, 6);
830 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
831 pakfire_file_unref(file
);
836 mode_t mode
= pakfire_file_get_mode(file
);
838 r
= sqlite3_bind_int64(stmt
, 7, mode
);
840 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
841 pakfire_file_unref(file
);
846 const char* user
= pakfire_file_get_user(file
);
848 r
= sqlite3_bind_text(stmt
, 8, user
, -1, NULL
);
850 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
851 pakfire_file_unref(file
);
856 const char* group
= pakfire_file_get_group(file
);
858 r
= sqlite3_bind_text(stmt
, 9, group
, -1, NULL
);
860 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
861 pakfire_file_unref(file
);
866 const char* chksum
= pakfire_file_get_chksum(file
);
868 r
= sqlite3_bind_text(stmt
, 10, chksum
, -1, NULL
);
870 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
871 pakfire_file_unref(file
);
876 time_t mtime
= pakfire_file_get_time(file
);
878 r
= sqlite3_bind_int64(stmt
, 11, mtime
);
880 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
881 pakfire_file_unref(file
);
885 // Bind capabilities - XXX TODO
886 r
= sqlite3_bind_null(stmt
, 12);
888 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
889 pakfire_file_unref(file
);
895 r
= sqlite3_step(stmt
);
896 } while (r
== SQLITE_BUSY
);
898 // Move on to next file
899 pakfire_file_unref(file
);
901 // Reset bound values
910 sqlite3_finalize(stmt
);
912 pakfire_filelist_unref(filelist
);
917 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, PakfireArchive archive
) {
918 sqlite3_stmt
* stmt
= NULL
;
921 struct pakfire_scriptlet_type
* scriptlet_type
= PAKFIRE_SCRIPTLET_TYPES
;
922 struct pakfire_scriptlet
* scriptlet
;
924 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
926 // Prepare the statement
927 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
929 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
930 sql
, sqlite3_errmsg(db
->handle
));
934 while (scriptlet_type
->type
) {
935 // Fetch the scriptlet
936 scriptlet
= pakfire_archive_get_scriptlet(archive
, scriptlet_type
->type
);
938 // Go to next one if the archive does not have a scriptlet of the given type
945 r
= sqlite3_bind_int64(stmt
, 1, id
);
947 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
952 r
= sqlite3_bind_text(stmt
, 2, scriptlet_type
->handle
, -1, NULL
);
954 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
959 r
= sqlite3_bind_text(stmt
, 3, scriptlet
->data
, scriptlet
->size
, NULL
);
961 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
967 r
= sqlite3_step(stmt
);
968 } while (r
== SQLITE_BUSY
);
970 // Reset bound values
981 sqlite3_finalize(stmt
);
986 PAKFIRE_EXPORT
int pakfire_db_add_package(struct pakfire_db
* db
,
987 PakfirePackage pkg
, PakfireArchive archive
) {
988 sqlite3_stmt
* stmt
= NULL
;
991 // Begin a new transaction
992 r
= pakfire_db_begin_transaction(db
);
996 const char* sql
= "INSERT INTO packages(name, epoch, version, release, arch, groups, "
997 "filename, size, inst_size, hash1, license, summary, description, uuid, vendor, "
998 "build_host, build_time, installed, repository, reason) VALUES(?, ?, "
999 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
1001 // Prepare the statement
1002 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1003 if (r
!= SQLITE_OK
) {
1004 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1005 sql
, sqlite3_errmsg(db
->handle
));
1010 const char* name
= pakfire_package_get_name(pkg
);
1012 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1014 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1019 unsigned long epoch
= pakfire_package_get_epoch(pkg
);
1021 r
= sqlite3_bind_int64(stmt
, 2, epoch
);
1023 ERROR(db
->pakfire
, "Could not bind epoch: %s\n", sqlite3_errmsg(db
->handle
));
1028 const char* version
= pakfire_package_get_version(pkg
);
1030 r
= sqlite3_bind_text(stmt
, 3, version
, -1, NULL
);
1032 ERROR(db
->pakfire
, "Could not bind version: %s\n", sqlite3_errmsg(db
->handle
));
1037 const char* release
= pakfire_package_get_release(pkg
);
1039 r
= sqlite3_bind_text(stmt
, 4, release
, -1, NULL
);
1041 ERROR(db
->pakfire
, "Could not bind release: %s\n", sqlite3_errmsg(db
->handle
));
1046 const char* arch
= pakfire_package_get_arch(pkg
);
1048 r
= sqlite3_bind_text(stmt
, 5, arch
, -1, NULL
);
1050 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1055 const char* groups
= pakfire_package_get_groups(pkg
);
1057 r
= sqlite3_bind_text(stmt
, 6, groups
, -1, NULL
);
1059 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1064 const char* filename
= pakfire_package_get_filename(pkg
);
1066 r
= sqlite3_bind_text(stmt
, 7, filename
, -1, NULL
);
1068 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1073 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1075 r
= sqlite3_bind_int64(stmt
, 8, size
);
1077 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1081 // Bind installed size
1082 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1084 r
= sqlite3_bind_int64(stmt
, 9, inst_size
);
1086 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1091 const char* hash1
= pakfire_package_get_checksum(pkg
);
1093 r
= sqlite3_bind_text(stmt
, 10, hash1
, -1, NULL
);
1095 ERROR(db
->pakfire
, "Could not bind hash1: %s\n", sqlite3_errmsg(db
->handle
));
1100 const char* license
= pakfire_package_get_license(pkg
);
1102 r
= sqlite3_bind_text(stmt
, 11, license
, -1, NULL
);
1104 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1109 const char* summary
= pakfire_package_get_summary(pkg
);
1111 r
= sqlite3_bind_text(stmt
, 12, summary
, -1, NULL
);
1113 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1118 const char* description
= pakfire_package_get_description(pkg
);
1120 r
= sqlite3_bind_text(stmt
, 13, description
, -1, NULL
);
1122 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1127 const char* uuid
= pakfire_package_get_uuid(pkg
);
1129 r
= sqlite3_bind_text(stmt
, 14, uuid
, -1, NULL
);
1131 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1136 const char* vendor
= pakfire_package_get_vendor(pkg
);
1138 r
= sqlite3_bind_text(stmt
, 15, vendor
, -1, NULL
);
1140 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1145 const char* buildhost
= pakfire_package_get_buildhost(pkg
);
1147 r
= sqlite3_bind_text(stmt
, 16, buildhost
, -1, NULL
);
1149 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1154 unsigned long long build_time
= pakfire_package_get_buildtime(pkg
);
1156 r
= sqlite3_bind_int64(stmt
, 17, build_time
);
1158 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1162 // Bind repository name
1163 PakfireRepo repo
= pakfire_package_get_repo(pkg
);
1165 const char* repo_name
= pakfire_repo_get_name(repo
);
1166 pakfire_repo_unref(repo
);
1168 r
= sqlite3_bind_text(stmt
, 18, repo_name
, -1, NULL
);
1174 r
= sqlite3_bind_null(stmt
, 18);
1179 // XXX TODO Bind reason
1180 r
= sqlite3_bind_null(stmt
, 19);
1186 r
= sqlite3_step(stmt
);
1187 } while (r
== SQLITE_BUSY
);
1189 if (r
!= SQLITE_DONE
) {
1190 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1191 sqlite3_errmsg(db
->handle
));
1196 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1199 r
= sqlite3_finalize(stmt
);
1204 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1209 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1214 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1218 // All done, commit!
1219 r
= pakfire_db_commit(db
);
1227 sqlite3_finalize(stmt
);
1229 pakfire_db_rollback(db
);
1234 PAKFIRE_EXPORT
int pakfire_db_remove_package(struct pakfire_db
* db
, PakfirePackage pkg
) {
1235 sqlite3_stmt
* stmt
= NULL
;
1238 // Fetch the package's UUID
1239 const char* uuid
= pakfire_package_get_uuid(pkg
);
1241 ERROR(db
->pakfire
, "Package has no UUID\n");
1245 r
= sqlite3_prepare_v2(db
->handle
,
1246 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1248 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1249 sqlite3_errmsg(db
->handle
));
1254 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1256 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1262 r
= sqlite3_step(stmt
);
1263 } while (r
== SQLITE_BUSY
);
1265 // Check if we have been successful
1266 if (r
!= SQLITE_DONE
) {
1267 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1276 sqlite3_finalize(stmt
);