1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2021 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <linux/limits.h>
26 #include <solv/solver.h>
29 #include <pakfire/archive.h>
30 #include <pakfire/db.h>
31 #include <pakfire/file.h>
32 #include <pakfire/filelist.h>
33 #include <pakfire/logging.h>
34 #include <pakfire/package.h>
35 #include <pakfire/pakfire.h>
36 #include <pakfire/repo.h>
37 #include <pakfire/util.h>
39 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
41 #define CURRENT_SCHEMA 8
42 #define SCHEMA_MIN_SUP 7
45 struct pakfire
* pakfire
;
53 time_t last_modified_at
;
56 static void logging_callback(void* data
, int r
, const char* msg
) {
57 struct pakfire
* pakfire
= (struct pakfire
*)data
;
59 ERROR(pakfire
, "Database Error: %s: %s\n",
60 sqlite3_errstr(r
), msg
);
63 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
64 sqlite3_stmt
* stmt
= NULL
;
65 sqlite3_value
* val
= NULL
;
68 const char* sql
= "SELECT val FROM settings WHERE key = ?";
70 // Prepare the statement
71 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
73 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
74 // sql, sqlite3_errmsg(db->handle));
79 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
81 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
85 // Execute the statement
87 r
= sqlite3_step(stmt
);
88 } while (r
== SQLITE_BUSY
);
90 // We should have read a row
95 val
= sqlite3_column_value(stmt
, 0);
97 ERROR(db
->pakfire
, "Could not read value\n");
101 // Copy value onto the heap
102 val
= sqlite3_value_dup(val
);
106 sqlite3_finalize(stmt
);
111 static char* pakfire_db_get_string(struct pakfire_db
* db
, const char* key
) {
114 // Fetch the value from the database
115 sqlite3_value
* value
= pakfire_db_get(db
, key
);
119 // Extract the value as string
120 const char* p
= (const char*)sqlite3_value_text(value
);
124 // Copy string to heap
129 sqlite3_value_free(value
);
134 static int pakfire_db_set_string(struct pakfire_db
* db
, const char* key
, const char* val
) {
135 sqlite3_stmt
* stmt
= NULL
;
138 DEBUG(db
->pakfire
, "Setting %s to '%s'\n", key
, val
);
140 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
141 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
144 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
145 if (r
!= SQLITE_OK
) {
146 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
147 sql
, sqlite3_errmsg(db
->handle
));
152 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
153 if (r
!= SQLITE_OK
) {
154 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
159 r
= sqlite3_bind_text(stmt
, 2, val
, strlen(val
), NULL
);
160 if (r
!= SQLITE_OK
) {
161 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
165 // Execute the statement
167 r
= sqlite3_step(stmt
);
168 } while (r
== SQLITE_BUSY
);
171 r
= (r
== SQLITE_OK
);
175 sqlite3_finalize(stmt
);
180 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
181 sqlite3_stmt
* stmt
= NULL
;
184 DEBUG(db
->pakfire
, "Setting %s to '%d'\n", key
, val
);
186 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
187 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
190 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
191 if (r
!= SQLITE_OK
) {
192 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
193 sql
, sqlite3_errmsg(db
->handle
));
198 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
199 if (r
!= SQLITE_OK
) {
200 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
205 r
= sqlite3_bind_int64(stmt
, 2, val
);
206 if (r
!= SQLITE_OK
) {
207 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
211 // Execute the statement
213 r
= sqlite3_step(stmt
);
214 } while (r
== SQLITE_BUSY
);
217 r
= (r
== SQLITE_OK
);
221 sqlite3_finalize(stmt
);
226 static time_t pakfire_read_modification_time(struct pakfire_db
* db
) {
229 // Fetch the value from the database
230 sqlite3_value
* value
= pakfire_db_get(db
, "last_modified_at");
232 t
= sqlite3_value_int64(value
);
233 sqlite3_value_free(value
);
235 DEBUG(db
->pakfire
, "Could not find last modification timestamp\n");
241 static int pakfire_update_modification_time(struct pakfire_db
* db
) {
242 // Get the current time in UTC
243 time_t t
= time(NULL
);
245 // Store it in the database
246 int r
= pakfire_db_set_int(db
, "last_modified_at", t
);
250 // Update the last modification timestamp
251 db
->last_modified_at
= t
;
256 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
259 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
262 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
263 } while (r
== SQLITE_BUSY
);
267 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
273 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
274 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
277 static int pakfire_db_commit(struct pakfire_db
* db
) {
279 If the database was opened in read-write mode, we will store the
280 timestamp of the latest modification to compare whether the database
281 has been changed mid-transaction.
283 if (db
->mode
== PAKFIRE_DB_READWRITE
) {
284 int r
= pakfire_update_modification_time(db
);
289 return pakfire_db_execute(db
, "COMMIT");
292 static int pakfire_db_rollback(struct pakfire_db
* db
) {
293 return pakfire_db_execute(db
, "ROLLBACK");
297 This function performs any fast optimization and tries to truncate the WAL log file
298 to keep the database as compact as possible on disk.
300 static void pakfire_db_optimize(struct pakfire_db
* db
) {
301 pakfire_db_execute(db
, "PRAGMA optimize");
302 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
305 static void pakfire_db_free(struct pakfire_db
* db
) {
307 // Optimize the database before it is being closed
308 pakfire_db_optimize(db
);
310 // Close database handle
311 int r
= sqlite3_close(db
->handle
);
312 if (r
!= SQLITE_OK
) {
313 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
314 sqlite3_errmsg(db
->handle
));
318 pakfire_unref(db
->pakfire
);
323 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
324 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
328 int schema
= sqlite3_value_int64(value
);
329 sqlite3_value_free(value
);
331 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
336 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
339 // Create settings table
340 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
344 // settings: Add a unique index on key
345 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
349 // Create packages table
350 r
= pakfire_db_execute(db
,
351 "CREATE TABLE IF NOT EXISTS packages("
352 "id INTEGER PRIMARY KEY, "
359 "inst_size INTEGER, "
367 "build_time INTEGER, "
368 "installed INTEGER, "
369 "userinstalled INTEGER, "
378 // packages: Create index to find package by name
379 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
383 // packages: Create unique index over UUID
384 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
388 // Create dependencies table
389 r
= pakfire_db_execute(db
,
390 "CREATE TABLE IF NOT EXISTS dependencies("
394 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
399 // dependencies: Add index over packages
400 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
404 // Create files table
405 r
= pakfire_db_execute(db
,
406 "CREATE TABLE IF NOT EXISTS files("
407 "id INTEGER PRIMARY KEY, "
419 "capabilities TEXT, "
420 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
425 // files: Add index over packages
426 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
430 // files: Add index over path
431 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
435 // Create scriptlets table
436 r
= pakfire_db_execute(db
,
437 "CREATE TABLE IF NOT EXISTS scriptlets("
438 "id INTEGER PRIMARY KEY, "
442 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
447 // scriptlets: Add index over packages
448 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
452 const char* arch
= pakfire_get_arch(db
->pakfire
);
455 r
= pakfire_db_set_string(db
, "arch", arch
);
457 ERROR(db
->pakfire
, "Could not set architecture\n");
464 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
465 // packages: Drop build_id column
468 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
469 // need to recreate the whole table and rename it afterwards. Annoying.
474 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
477 while (db
->schema
< CURRENT_SCHEMA
) {
478 // Begin a new transaction
479 r
= pakfire_db_begin_transaction(db
);
483 switch (db
->schema
) {
486 r
= pakfire_db_create_schema(db
);
490 db
->schema
= CURRENT_SCHEMA
;
494 r
= pakfire_db_migrate_to_schema_8(db
);
502 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
506 // Update the schema version
507 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
512 r
= pakfire_db_commit(db
);
520 pakfire_db_rollback(db
);
525 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
528 // Fetch database architecture
529 char* db_arch
= pakfire_db_get_string(db
, "arch");
531 ERROR(db
->pakfire
, "Database is of an unknown architecture\n");
535 // Fetch the running architecture
536 const char* arch
= pakfire_get_arch(db
->pakfire
);
541 if (strcmp(db_arch
, arch
) == 0)
551 static int pakfire_db_setup(struct pakfire_db
* db
) {
555 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
557 // Enable foreign keys
558 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
560 // Make LIKE case-sensitive
561 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
563 // Fetch the current schema
564 db
->schema
= pakfire_db_get_schema(db
);
566 // Check if the schema is recent enough
567 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
568 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
573 // Read modification timestamp
574 db
->last_modified_at
= pakfire_read_modification_time(db
);
576 DEBUG(db
->pakfire
, "The database was last modified at %ld\n", db
->last_modified_at
);
578 // Done when not in read-write mode
579 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
582 // Disable secure delete
583 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
585 // Set database journal to WAL
586 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
587 if (r
!= SQLITE_OK
) {
588 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
589 sqlite3_errmsg(db
->handle
));
593 // Disable autocheckpoint
594 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
595 if (r
!= SQLITE_OK
) {
596 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
597 sqlite3_errmsg(db
->handle
));
601 // Create or migrate schema
602 r
= pakfire_db_migrate_schema(db
);
609 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
612 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
616 o
->pakfire
= pakfire_ref(pakfire
);
619 int sqlite3_flags
= 0;
621 // Store mode & forward it to sqlite3
622 if (flags
& PAKFIRE_DB_READWRITE
) {
623 o
->mode
= PAKFIRE_DB_READWRITE
;
624 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
626 o
->mode
= PAKFIRE_DB_READONLY
;
627 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
631 r
= pakfire_make_path(o
->pakfire
, o
->path
, DATABASE_PATH
);
635 // Try to open the sqlite3 database file
636 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
637 if (r
!= SQLITE_OK
) {
638 ERROR(pakfire
, "Could not open database %s: %s\n",
639 o
->path
, sqlite3_errmsg(o
->handle
));
645 // Setup the database
646 r
= pakfire_db_setup(o
);
650 // Check for compatible architecture
651 r
= pakfire_db_check_arch(o
);
665 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
671 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
680 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
681 sqlite3_stmt
* stmt
= NULL
;
684 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
686 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
687 sqlite3_errmsg(db
->handle
));
692 unsigned long errors
= 0;
696 r
= sqlite3_step(stmt
);
697 } while (r
== SQLITE_BUSY
);
699 if (r
== SQLITE_ROW
) {
700 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
702 // If the message is "ok", the database has passed the check
703 if (strcmp(error
, "ok") == 0)
706 // Increment error counter
710 ERROR(db
->pakfire
, "%s\n", error
);
712 // Break on anything else
717 sqlite3_finalize(stmt
);
720 ERROR(db
->pakfire
, "Database integrity check failed\n");
722 INFO(db
->pakfire
, "Database integrity check passed\n");
727 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
728 sqlite3_stmt
* stmt
= NULL
;
731 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
733 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
734 sqlite3_errmsg(db
->handle
));
739 unsigned long errors
= 0;
743 r
= sqlite3_step(stmt
);
744 } while (r
== SQLITE_BUSY
);
746 if (r
== SQLITE_ROW
) {
747 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
748 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
749 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
750 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
752 // Increment error counter
756 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
757 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
759 // Break on anything else
764 sqlite3_finalize(stmt
);
767 ERROR(db
->pakfire
, "Foreign key check failed\n");
769 INFO(db
->pakfire
, "Foreign key check passed\n");
775 This function performs an integrity check of the database
777 int pakfire_db_check(struct pakfire_db
* db
) {
780 // Perform integrity check
781 r
= pakfire_db_integrity_check(db
);
785 // Perform foreign key check
786 r
= pakfire_db_foreign_key_check(db
);
793 // Returns the number of packages installed
794 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
795 sqlite3_stmt
* stmt
= NULL
;
796 ssize_t packages
= -1;
798 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
800 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
801 sqlite3_errmsg(db
->handle
));
807 r
= sqlite3_step(stmt
);
808 } while (r
== SQLITE_BUSY
);
810 if (r
== SQLITE_ROW
) {
811 packages
= sqlite3_column_int64(stmt
, 0);
814 sqlite3_finalize(stmt
);
819 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
820 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
822 // Convert name to ID
823 Id id
= pool_str2id(pool
, name
, 1);
825 // Append it to pooljobs
826 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
829 static const struct pakfire_digest
{
830 enum pakfire_digests type
;
832 } pakfire_digests
[] = {
833 { PAKFIRE_DIGEST_SHA512
, "sha512:" },
834 { PAKFIRE_DIGEST_SHA256
, "sha256:" },
835 { PAKFIRE_DIGEST_SHA1
, "sha1:" },
836 { PAKFIRE_DIGEST_NONE
, NULL
},
839 static char* pakfire_db_pack_digest(enum pakfire_digests type
, const char* hexdigest
) {
843 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
844 if (digest
->type
== type
) {
845 r
= asprintf(&s
, "%s%s", digest
->prefix
, hexdigest
);
856 static const char* pakfire_db_unpack_digest(const char* hexdigest
, enum pakfire_digests
* type
) {
857 *type
= PAKFIRE_DIGEST_NONE
;
859 // Don't do anything for empty input
860 if (!hexdigest
|| !*hexdigest
)
863 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
864 if (pakfire_string_startswith(hexdigest
, digest
->prefix
)) {
865 *type
= digest
->type
;
867 // Return the beginning of the hexdigest
868 return hexdigest
+ strlen(digest
->prefix
);
876 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
877 sqlite3_stmt
* stmt
= NULL
;
880 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
882 // Prepare the statement
883 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
885 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
886 sql
, sqlite3_errmsg(db
->handle
));
890 const struct __relation
{
892 char** (*func
)(struct pakfire_package
*);
894 { "provides", pakfire_package_get_provides
},
895 { "prerequires", pakfire_package_get_prerequires
},
896 { "requires", pakfire_package_get_requires
},
897 { "conflicts", pakfire_package_get_conflicts
},
898 { "obsoletes", pakfire_package_get_obsoletes
},
899 { "recommends", pakfire_package_get_recommends
},
900 { "suggests", pakfire_package_get_suggests
},
901 { "supplements", pakfire_package_get_supplements
},
902 { "enhances", pakfire_package_get_enhances
},
906 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
907 char** list
= relation
->func(pkg
);
911 for (char** dep
= list
; *dep
; dep
++) {
913 r
= sqlite3_bind_int64(stmt
, 1, id
);
915 ERROR(db
->pakfire
, "Could not bind id: %s\n",
916 sqlite3_errmsg(db
->handle
));
921 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
923 ERROR(db
->pakfire
, "Could not bind type: %s\n",
924 sqlite3_errmsg(db
->handle
));
929 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
931 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
932 sqlite3_errmsg(db
->handle
));
938 r
= sqlite3_step(stmt
);
939 } while (r
== SQLITE_BUSY
);
943 // Reset bound values
955 sqlite3_finalize(stmt
);
960 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
961 sqlite3_stmt
* stmt
= NULL
;
965 // Get the filelist from the archive
966 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
968 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
972 // Nothing to do if the list is empty
973 if (pakfire_filelist_is_empty(filelist
)) {
978 const char* sql
= "INSERT INTO files(pkg, path, size, config, datafile, mode, "
979 "user, 'group', digest, ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
981 // Prepare the statement
982 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
984 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
985 sql
, sqlite3_errmsg(db
->handle
));
989 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
990 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
993 r
= sqlite3_bind_int64(stmt
, 1, id
);
995 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
996 pakfire_file_unref(file
);
1001 const char* path
= pakfire_file_get_path(file
);
1003 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
1005 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
1006 pakfire_file_unref(file
);
1011 size_t size
= pakfire_file_get_size(file
);
1013 r
= sqlite3_bind_int64(stmt
, 3, size
);
1015 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1016 pakfire_file_unref(file
);
1020 // Bind config - XXX TODO
1021 r
= sqlite3_bind_null(stmt
, 4);
1023 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
1024 pakfire_file_unref(file
);
1028 // Bind datafile - XXX TODO
1029 r
= sqlite3_bind_null(stmt
, 5);
1031 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
1032 pakfire_file_unref(file
);
1037 mode_t mode
= pakfire_file_get_mode(file
);
1039 r
= sqlite3_bind_int64(stmt
, 6, mode
);
1041 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
1042 pakfire_file_unref(file
);
1047 const char* user
= pakfire_file_get_user(file
);
1049 r
= sqlite3_bind_text(stmt
, 7, user
, -1, NULL
);
1051 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
1052 pakfire_file_unref(file
);
1057 const char* group
= pakfire_file_get_group(file
);
1059 r
= sqlite3_bind_text(stmt
, 8, group
, -1, NULL
);
1061 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
1062 pakfire_file_unref(file
);
1066 enum pakfire_digests digest_type
= PAKFIRE_DIGEST_NONE
;
1069 const char* hexdigest
= pakfire_file_get_hexdigest(file
, &digest_type
);
1071 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
1073 ERROR(db
->pakfire
, "Could not pack digest: %m\n");
1074 pakfire_file_unref(file
);
1078 r
= sqlite3_bind_text(stmt
, 9, digest
, -1, NULL
);
1080 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1081 pakfire_file_unref(file
);
1085 r
= sqlite3_bind_null(stmt
, 9);
1087 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1088 pakfire_file_unref(file
);
1094 time_t ctime
= pakfire_file_get_ctime(file
);
1096 r
= sqlite3_bind_int64(stmt
, 10, ctime
);
1098 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
1099 pakfire_file_unref(file
);
1104 time_t mtime
= pakfire_file_get_mtime(file
);
1106 r
= sqlite3_bind_int64(stmt
, 11, mtime
);
1108 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
1109 pakfire_file_unref(file
);
1113 // Bind capabilities - XXX TODO
1114 r
= sqlite3_bind_null(stmt
, 12);
1116 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1117 pakfire_file_unref(file
);
1123 r
= sqlite3_step(stmt
);
1124 } while (r
== SQLITE_BUSY
);
1126 // Move on to next file
1127 pakfire_file_unref(file
);
1130 if (r
!= SQLITE_DONE
) {
1131 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
1132 sqlite3_errmsg(db
->handle
));
1136 // Reset bound values
1137 sqlite3_reset(stmt
);
1151 sqlite3_finalize(stmt
);
1155 pakfire_filelist_unref(filelist
);
1160 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1161 sqlite3_stmt
* stmt
= NULL
;
1165 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1167 // Prepare the statement
1168 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1170 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1171 sql
, sqlite3_errmsg(db
->handle
));
1175 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1176 // Fetch the scriptlet
1177 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1182 r
= sqlite3_bind_int64(stmt
, 1, id
);
1184 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1185 pakfire_scriptlet_unref(scriptlet
);
1190 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1192 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1193 pakfire_scriptlet_unref(scriptlet
);
1197 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1200 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1202 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1203 pakfire_scriptlet_unref(scriptlet
);
1209 r
= sqlite3_step(stmt
);
1210 } while (r
== SQLITE_BUSY
);
1213 if (r
!= SQLITE_DONE
) {
1214 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1215 sqlite3_errmsg(db
->handle
));
1216 pakfire_scriptlet_unref(scriptlet
);
1220 pakfire_scriptlet_unref(scriptlet
);
1222 // Reset bound values
1223 sqlite3_reset(stmt
);
1231 sqlite3_finalize(stmt
);
1236 int pakfire_db_add_package(struct pakfire_db
* db
,
1237 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1238 sqlite3_stmt
* stmt
= NULL
;
1239 char* digest
= NULL
;
1242 // Begin a new transaction
1243 r
= pakfire_db_begin_transaction(db
);
1247 const char* sql
= "INSERT INTO "
1286 "CURRENT_TIMESTAMP, "
1294 // Prepare the statement
1295 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1296 if (r
!= SQLITE_OK
) {
1297 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1298 sql
, sqlite3_errmsg(db
->handle
));
1303 const char* name
= pakfire_package_get_name(pkg
);
1305 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1307 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1312 const char* evr
= pakfire_package_get_evr(pkg
);
1314 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1316 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1321 const char* arch
= pakfire_package_get_arch(pkg
);
1323 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1325 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1330 char* groups
= pakfire_package_get_groups(pkg
);
1332 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1334 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1343 r
= sqlite3_bind_null(stmt
, 4);
1349 const char* filename
= pakfire_package_get_filename(pkg
);
1351 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1353 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1358 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1360 r
= sqlite3_bind_int64(stmt
, 6, size
);
1362 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1366 // Bind installed size
1367 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1369 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1371 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1375 enum pakfire_digests digest_type
= PAKFIRE_DIGEST_NONE
;
1377 const char* hexdigest
= pakfire_package_get_hexdigest(pkg
, &digest_type
);
1379 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
1383 r
= sqlite3_bind_text(stmt
, 8, digest
, -1, NULL
);
1385 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1391 const char* license
= pakfire_package_get_license(pkg
);
1393 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1395 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1400 const char* summary
= pakfire_package_get_summary(pkg
);
1402 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1404 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1409 const char* description
= pakfire_package_get_description(pkg
);
1411 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1413 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1418 const char* uuid
= pakfire_package_get_uuid(pkg
);
1420 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1422 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1427 const char* vendor
= pakfire_package_get_vendor(pkg
);
1429 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1431 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1436 const char* build_host
= pakfire_package_get_build_host(pkg
);
1438 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1440 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1445 time_t build_time
= pakfire_package_get_build_time(pkg
);
1447 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1449 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1453 // Bind repository name
1454 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1456 const char* repo_name
= pakfire_repo_get_name(repo
);
1457 pakfire_repo_unref(repo
);
1459 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1465 r
= sqlite3_bind_null(stmt
, 16);
1470 // installed by the user?
1471 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1473 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1477 // Source package name
1478 const char* source_name
= pakfire_package_get_source_name(pkg
);
1480 r
= sqlite3_bind_text(stmt
, 18, source_name
, -1, NULL
);
1484 r
= sqlite3_bind_null(stmt
, 18);
1490 const char* source_evr
= pakfire_package_get_source_evr(pkg
);
1492 r
= sqlite3_bind_text(stmt
, 19, source_evr
, -1, NULL
);
1496 r
= sqlite3_bind_null(stmt
, 19);
1502 const char* source_arch
= pakfire_package_get_source_arch(pkg
);
1504 r
= sqlite3_bind_text(stmt
, 20, source_arch
, -1, NULL
);
1508 r
= sqlite3_bind_null(stmt
, 20);
1515 r
= sqlite3_step(stmt
);
1516 } while (r
== SQLITE_BUSY
);
1518 if (r
!= SQLITE_DONE
) {
1519 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1520 sqlite3_errmsg(db
->handle
));
1526 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1529 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1534 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1539 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1545 sqlite3_finalize(stmt
);
1549 // Commit or rollback
1551 pakfire_db_rollback(db
);
1553 r
= pakfire_db_commit(db
);
1558 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1559 sqlite3_stmt
* stmt
= NULL
;
1562 // Begin a new transaction
1563 r
= pakfire_db_begin_transaction(db
);
1567 // Fetch the package's UUID
1568 const char* uuid
= pakfire_package_get_uuid(pkg
);
1570 ERROR(db
->pakfire
, "Package has no UUID\n");
1575 r
= sqlite3_prepare_v2(db
->handle
,
1576 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1578 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1579 sqlite3_errmsg(db
->handle
));
1584 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1586 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1592 r
= sqlite3_step(stmt
);
1593 } while (r
== SQLITE_BUSY
);
1595 // Check if we have been successful
1596 if (r
!= SQLITE_DONE
) {
1597 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1607 sqlite3_finalize(stmt
);
1609 // Commit or rollback
1611 pakfire_db_rollback(db
);
1613 r
= pakfire_db_commit(db
);
1618 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1619 struct pakfire_package
* pkg
, const char* type
) {
1620 struct pakfire_scriptlet
* scriptlet
= NULL
;
1621 sqlite3_stmt
* stmt
= NULL
;
1624 // Fetch the package's UUID
1625 const char* uuid
= pakfire_package_get_uuid(pkg
);
1627 ERROR(db
->pakfire
, "Package has no UUID\n");
1631 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1632 JOIN scriptlets ON packages.id = scriptlets.pkg \
1633 WHERE packages.uuid = ? AND scriptlets.type = ?";
1635 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1637 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1638 sql
, sqlite3_errmsg(db
->handle
));
1643 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1645 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1649 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1651 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1655 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1659 r
= sqlite3_step(stmt
);
1660 } while (r
== SQLITE_BUSY
);
1662 // We have some payload
1663 if (r
== SQLITE_ROW
) {
1664 const void* data
= sqlite3_column_blob(stmt
, 1);
1665 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1667 // Create a scriptlet object
1668 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1675 sqlite3_finalize(stmt
);
1680 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1681 struct pakfire_package
* pkg
= NULL
;
1685 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1687 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1692 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1694 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1699 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1701 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1706 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1708 ERROR(db
->pakfire
, "Could not create package\n");
1713 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1715 pakfire_package_set_dbid(pkg
, id
);
1718 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1720 pakfire_package_set_groups(pkg
, groups
);
1724 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1726 pakfire_package_set_filename(pkg
, filename
);
1730 size_t size
= sqlite3_column_int64(stmt
, 6);
1732 pakfire_package_set_downloadsize(pkg
, size
);
1736 size
= sqlite3_column_int64(stmt
, 7);
1738 pakfire_package_set_installsize(pkg
, size
);
1742 const char* digest
= (const char*)sqlite3_column_text(stmt
, 8);
1744 enum pakfire_digests digest_type
= PAKFIRE_DIGEST_NONE
;
1747 const char* hexdigest
= pakfire_db_unpack_digest(digest
, &digest_type
);
1749 pakfire_package_set_hexdigest(pkg
, digest_type
, hexdigest
);
1753 const char* license
= (const char*)sqlite3_column_text(stmt
, 9);
1755 pakfire_package_set_license(pkg
, license
);
1759 const char* summary
= (const char*)sqlite3_column_text(stmt
, 10);
1761 pakfire_package_set_summary(pkg
, summary
);
1765 const char* description
= (const char*)sqlite3_column_text(stmt
, 11);
1767 pakfire_package_set_description(pkg
, description
);
1771 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 12);
1773 pakfire_package_set_uuid(pkg
, uuid
);
1777 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 13);
1779 pakfire_package_set_vendor(pkg
, vendor
);
1783 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 14);
1785 pakfire_package_set_build_host(pkg
, build_host
);
1789 time_t build_time
= sqlite3_column_int64(stmt
, 15);
1791 pakfire_package_set_build_time(pkg
, build_time
);
1795 time_t install_time
= sqlite3_column_int64(stmt
, 16);
1797 pakfire_package_set_install_time(pkg
, install_time
);
1800 // installed by user?
1801 int userinstalled
= sqlite3_column_int(stmt
, 17);
1803 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1806 const char* files
= (const char*)sqlite3_column_text(stmt
, 18);
1808 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1815 const struct dependency
{
1817 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1818 } dependencies
[] = {
1819 { 19, pakfire_package_add_provides
},
1820 { 20, pakfire_package_add_prerequires
},
1821 { 21, pakfire_package_add_requires
},
1822 { 22, pakfire_package_add_conflicts
},
1823 { 23, pakfire_package_add_obsoletes
},
1824 { 24, pakfire_package_add_recommends
},
1825 { 25, pakfire_package_add_suggests
},
1826 { 26, pakfire_package_add_supplements
},
1827 { 27, pakfire_package_add_enhances
},
1831 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1832 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1834 pakfire_str2deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1839 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 28);
1841 pakfire_package_set_source_name(pkg
, source_name
);
1844 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 29);
1846 pakfire_package_set_source_evr(pkg
, source_evr
);
1849 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 30);
1851 pakfire_package_set_source_arch(pkg
, source_arch
);
1858 pakfire_package_unref(pkg
);
1863 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1864 sqlite3_stmt
* stmt
= NULL
;
1867 DEBUG(db
->pakfire
, "Loading package database...\n");
1869 // Drop contents of the repository
1870 pakfire_repo_clear(repo
);
1872 // Save starting time
1873 clock_t t_start
= clock();
1894 "strftime('%s', installed) AS installed, "
1897 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1900 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1901 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1904 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1905 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1906 ") AS prerequires, "
1908 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1909 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1912 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1913 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1916 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1917 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1920 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1921 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1924 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1925 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1928 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1929 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1930 ") AS supplements, "
1932 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1933 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1942 // Prepare the statement
1943 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1945 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1946 sql
, sqlite3_errmsg(db
->handle
));
1952 r
= sqlite3_step(stmt
);
1955 // Retry if the database was busy
1961 r
= pakfire_db_load_package(db
, repo
, stmt
);
1966 // All rows have been processed
1970 // Go to error in any other cases
1977 // Save time when we finished
1980 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
1981 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
1983 // Mark repository as changed
1984 pakfire_repo_has_changed(repo
);
1991 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
1992 pakfire_repo_clear(repo
);
1996 sqlite3_finalize(stmt
);
2001 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2002 sqlite3_stmt
* stmt
) {
2003 struct pakfire_file
* file
= NULL
;
2004 char abspath
[PATH_MAX
];
2007 // Create a new file object
2008 r
= pakfire_file_create(&file
, db
->pakfire
);
2013 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2015 pakfire_file_set_path(file
, path
);
2018 r
= pakfire_make_path(db
->pakfire
, abspath
, path
);
2022 pakfire_file_set_abspath(file
, abspath
);
2025 size_t size
= sqlite3_column_int64(stmt
, 1);
2027 pakfire_file_set_size(file
, size
);
2030 mode_t mode
= sqlite3_column_int(stmt
, 2);
2032 pakfire_file_set_mode(file
, mode
);
2035 const char* user
= (const char*)sqlite3_column_text(stmt
, 3);
2037 pakfire_file_set_user(file
, user
);
2040 const char* group
= (const char*)sqlite3_column_text(stmt
, 4);
2042 pakfire_file_set_group(file
, group
);
2045 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2047 pakfire_file_set_ctime(file
, ctime
);
2050 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2052 pakfire_file_set_mtime(file
, mtime
);
2054 // Append the file to the filelist
2055 r
= pakfire_filelist_append(filelist
, file
);
2059 pakfire_file_unref(file
);
2064 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2065 struct pakfire_package
* pkg
) {
2066 struct pakfire_filelist
* fl
= NULL
;
2067 sqlite3_stmt
* stmt
= NULL
;
2070 // Fetch the package ID
2071 uint64_t id
= pakfire_package_get_dbid(pkg
);
2073 ERROR(db
->pakfire
, "Package did not have an ID\n");
2077 // Create a new filelist
2078 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2080 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
2095 // Select all files that belong to this package
2099 // Filter out any files that are also in a different package (i.e. an update
2100 // that has already been installed and this is the cleanup of the obsolete pkg)
2105 "FROM files AS duplicates "
2107 "files.path = duplicates.path "
2109 "files.pkg != duplicates.pkg"
2112 // Return the longest paths first
2117 // Prepare the statement
2118 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2120 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2121 sql
, sqlite3_errmsg(db
->handle
));
2126 r
= sqlite3_bind_int64(stmt
, 1, id
);
2128 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2134 r
= sqlite3_step(stmt
);
2137 // Retry if the database was busy
2143 r
= pakfire_db_load_file(db
, fl
, stmt
);
2148 // All rows have been processed
2152 // Go to error in any other cases
2159 *filelist
= pakfire_filelist_ref(fl
);
2164 sqlite3_finalize(stmt
);
2166 pakfire_filelist_unref(fl
);