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/dependencies.h>
32 #include <pakfire/file.h>
33 #include <pakfire/filelist.h>
34 #include <pakfire/logging.h>
35 #include <pakfire/package.h>
36 #include <pakfire/pakfire.h>
37 #include <pakfire/repo.h>
38 #include <pakfire/string.h>
39 #include <pakfire/util.h>
41 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
43 #define CURRENT_SCHEMA 8
44 #define SCHEMA_MIN_SUP 7
47 struct pakfire
* pakfire
;
55 time_t last_modified_at
;
58 static void logging_callback(void* data
, int r
, const char* msg
) {
59 struct pakfire
* pakfire
= (struct pakfire
*)data
;
61 ERROR(pakfire
, "Database Error: %s: %s\n",
62 sqlite3_errstr(r
), msg
);
65 static sqlite3_value
* pakfire_db_get(struct pakfire_db
* db
, const char* key
) {
66 sqlite3_stmt
* stmt
= NULL
;
67 sqlite3_value
* val
= NULL
;
70 const char* sql
= "SELECT val FROM settings WHERE key = ?";
72 // Prepare the statement
73 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
75 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
76 // sql, sqlite3_errmsg(db->handle));
81 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
83 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
87 // Execute the statement
89 r
= sqlite3_step(stmt
);
90 } while (r
== SQLITE_BUSY
);
92 // We should have read a row
97 val
= sqlite3_column_value(stmt
, 0);
99 ERROR(db
->pakfire
, "Could not read value\n");
103 // Copy value onto the heap
104 val
= sqlite3_value_dup(val
);
108 sqlite3_finalize(stmt
);
113 static char* pakfire_db_get_string(struct pakfire_db
* db
, const char* key
) {
116 // Fetch the value from the database
117 sqlite3_value
* value
= pakfire_db_get(db
, key
);
121 // Extract the value as string
122 const char* p
= (const char*)sqlite3_value_text(value
);
126 // Copy string to heap
131 sqlite3_value_free(value
);
136 static int pakfire_db_set_string(struct pakfire_db
* db
, const char* key
, const char* val
) {
137 sqlite3_stmt
* stmt
= NULL
;
140 DEBUG(db
->pakfire
, "Setting %s to '%s'\n", key
, val
);
142 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
143 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
146 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
147 if (r
!= SQLITE_OK
) {
148 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
149 sql
, sqlite3_errmsg(db
->handle
));
154 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
155 if (r
!= SQLITE_OK
) {
156 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
161 r
= sqlite3_bind_text(stmt
, 2, val
, strlen(val
), NULL
);
162 if (r
!= SQLITE_OK
) {
163 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
167 // Execute the statement
169 r
= sqlite3_step(stmt
);
170 } while (r
== SQLITE_BUSY
);
173 r
= (r
== SQLITE_OK
);
177 sqlite3_finalize(stmt
);
182 static int pakfire_db_set_int(struct pakfire_db
* db
, const char* key
, int val
) {
183 sqlite3_stmt
* stmt
= NULL
;
186 DEBUG(db
->pakfire
, "Setting %s to '%d'\n", key
, val
);
188 const char* sql
= "INSERT INTO settings(key, val) VALUES(?, ?) \
189 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
192 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
193 if (r
!= SQLITE_OK
) {
194 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
195 sql
, sqlite3_errmsg(db
->handle
));
200 r
= sqlite3_bind_text(stmt
, 1, key
, strlen(key
), NULL
);
201 if (r
!= SQLITE_OK
) {
202 ERROR(db
->pakfire
, "Could not bind key: %s\n", sqlite3_errmsg(db
->handle
));
207 r
= sqlite3_bind_int64(stmt
, 2, val
);
208 if (r
!= SQLITE_OK
) {
209 ERROR(db
->pakfire
, "Could not bind val: %s\n", sqlite3_errmsg(db
->handle
));
213 // Execute the statement
215 r
= sqlite3_step(stmt
);
216 } while (r
== SQLITE_BUSY
);
219 r
= (r
== SQLITE_OK
);
223 sqlite3_finalize(stmt
);
228 static time_t pakfire_read_modification_time(struct pakfire_db
* db
) {
231 // Fetch the value from the database
232 sqlite3_value
* value
= pakfire_db_get(db
, "last_modified_at");
234 t
= sqlite3_value_int64(value
);
235 sqlite3_value_free(value
);
237 DEBUG(db
->pakfire
, "Could not find last modification timestamp\n");
243 static int pakfire_update_modification_time(struct pakfire_db
* db
) {
244 // Get the current time in UTC
245 time_t t
= time(NULL
);
247 // Store it in the database
248 int r
= pakfire_db_set_int(db
, "last_modified_at", t
);
252 // Update the last modification timestamp
253 db
->last_modified_at
= t
;
258 static int pakfire_db_execute(struct pakfire_db
* db
, const char* stmt
) {
261 DEBUG(db
->pakfire
, "Executing database query: %s\n", stmt
);
264 r
= sqlite3_exec(db
->handle
, stmt
, NULL
, NULL
, NULL
);
265 } while (r
== SQLITE_BUSY
);
269 ERROR(db
->pakfire
, "Database query failed: %s\n", sqlite3_errmsg(db
->handle
));
275 static int pakfire_db_begin_transaction(struct pakfire_db
* db
) {
276 return pakfire_db_execute(db
, "BEGIN TRANSACTION");
279 static int pakfire_db_commit(struct pakfire_db
* db
) {
281 If the database was opened in read-write mode, we will store the
282 timestamp of the latest modification to compare whether the database
283 has been changed mid-transaction.
285 if (db
->mode
== PAKFIRE_DB_READWRITE
) {
286 int r
= pakfire_update_modification_time(db
);
291 return pakfire_db_execute(db
, "COMMIT");
294 static int pakfire_db_rollback(struct pakfire_db
* db
) {
295 return pakfire_db_execute(db
, "ROLLBACK");
299 This function performs any fast optimization and tries to truncate the WAL log file
300 to keep the database as compact as possible on disk.
302 static void pakfire_db_optimize(struct pakfire_db
* db
) {
303 pakfire_db_execute(db
, "PRAGMA optimize");
304 pakfire_db_execute(db
, "PRAGMA wal_checkpoint = TRUNCATE");
307 static void pakfire_db_free(struct pakfire_db
* db
) {
309 // Optimize the database before it is being closed
310 pakfire_db_optimize(db
);
312 // Close database handle
313 int r
= sqlite3_close(db
->handle
);
314 if (r
!= SQLITE_OK
) {
315 ERROR(db
->pakfire
, "Could not close database handle: %s\n",
316 sqlite3_errmsg(db
->handle
));
320 pakfire_unref(db
->pakfire
);
325 static int pakfire_db_get_schema(struct pakfire_db
* db
) {
326 sqlite3_value
* value
= pakfire_db_get(db
, "schema");
330 int schema
= sqlite3_value_int64(value
);
331 sqlite3_value_free(value
);
333 DEBUG(db
->pakfire
, "Database has schema version %d\n", schema
);
338 static int pakfire_db_create_schema(struct pakfire_db
* db
) {
341 // Create settings table
342 r
= pakfire_db_execute(db
, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
346 // settings: Add a unique index on key
347 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
351 // Create packages table
352 r
= pakfire_db_execute(db
,
353 "CREATE TABLE IF NOT EXISTS packages("
354 "id INTEGER PRIMARY KEY, "
361 "inst_size INTEGER, "
369 "build_time INTEGER, "
370 "installed INTEGER, "
371 "userinstalled INTEGER, "
381 // packages: Create index to find package by name
382 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
386 // packages: Create unique index over UUID
387 r
= pakfire_db_execute(db
, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
391 // Create dependencies table
392 r
= pakfire_db_execute(db
,
393 "CREATE TABLE IF NOT EXISTS dependencies("
397 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
402 // dependencies: Add index over packages
403 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
407 // Create files table
408 r
= pakfire_db_execute(db
,
409 "CREATE TABLE IF NOT EXISTS files("
410 "id INTEGER PRIMARY KEY, "
421 "capabilities TEXT, "
422 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
427 // files: Add index over packages
428 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
432 // files: Add index over path
433 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
437 // Create file digests table
438 r
= pakfire_db_execute(db
,
439 "CREATE TABLE IF NOT EXISTS file_digests("
443 "FOREIGN KEY (file) REFERENCES files(id) ON DELETE CASCADE"
448 // file digests: Add index over files
449 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS file_digests_file_index "
450 "ON file_digests(file)");
454 // Create scriptlets table
455 r
= pakfire_db_execute(db
,
456 "CREATE TABLE IF NOT EXISTS scriptlets("
457 "id INTEGER PRIMARY KEY, "
461 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
466 // scriptlets: Add index over packages
467 r
= pakfire_db_execute(db
, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
471 const char* arch
= pakfire_get_arch(db
->pakfire
);
474 r
= pakfire_db_set_string(db
, "arch", arch
);
476 ERROR(db
->pakfire
, "Could not set architecture\n");
483 static int pakfire_db_migrate_to_schema_8(struct pakfire_db
* db
) {
484 // packages: Drop build_id column
487 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
488 // need to recreate the whole table and rename it afterwards. Annoying.
493 static int pakfire_db_migrate_schema(struct pakfire_db
* db
) {
496 while (db
->schema
< CURRENT_SCHEMA
) {
497 // Begin a new transaction
498 r
= pakfire_db_begin_transaction(db
);
502 switch (db
->schema
) {
505 r
= pakfire_db_create_schema(db
);
509 db
->schema
= CURRENT_SCHEMA
;
513 r
= pakfire_db_migrate_to_schema_8(db
);
521 ERROR(db
->pakfire
, "Cannot migrate database from schema %d\n", db
->schema
);
525 // Update the schema version
526 r
= pakfire_db_set_int(db
, "schema", CURRENT_SCHEMA
);
531 r
= pakfire_db_commit(db
);
539 pakfire_db_rollback(db
);
544 static int pakfire_db_check_arch(struct pakfire_db
* db
) {
547 // Fetch database architecture
548 char* db_arch
= pakfire_db_get_string(db
, "arch");
550 ERROR(db
->pakfire
, "Database is of an unknown architecture\n");
554 // Fetch the running architecture
555 const char* arch
= pakfire_get_arch(db
->pakfire
);
560 if (strcmp(db_arch
, arch
) == 0)
570 static int pakfire_db_setup(struct pakfire_db
* db
) {
574 sqlite3_config(SQLITE_CONFIG_LOG
, logging_callback
, db
->pakfire
);
576 // Enable foreign keys
577 pakfire_db_execute(db
, "PRAGMA foreign_keys = ON");
579 // Make LIKE case-sensitive
580 pakfire_db_execute(db
, "PRAGMA case_sensitive_like = ON");
582 // Fetch the current schema
583 db
->schema
= pakfire_db_get_schema(db
);
585 // Check if the schema is recent enough
586 if (db
->schema
> 0 && db
->schema
< SCHEMA_MIN_SUP
) {
587 ERROR(db
->pakfire
, "Database schema %d is not supported by this version of Pakfire\n",
592 // Read modification timestamp
593 db
->last_modified_at
= pakfire_read_modification_time(db
);
595 DEBUG(db
->pakfire
, "The database was last modified at %ld\n", db
->last_modified_at
);
597 // Done when not in read-write mode
598 if (db
->mode
!= PAKFIRE_DB_READWRITE
)
601 // Disable secure delete
602 pakfire_db_execute(db
, "PRAGMA secure_delete = OFF");
604 // Set database journal to WAL
605 r
= pakfire_db_execute(db
, "PRAGMA journal_mode = WAL");
606 if (r
!= SQLITE_OK
) {
607 ERROR(db
->pakfire
, "Could not set journal mode to WAL: %s\n",
608 sqlite3_errmsg(db
->handle
));
612 // Disable autocheckpoint
613 r
= sqlite3_wal_autocheckpoint(db
->handle
, 0);
614 if (r
!= SQLITE_OK
) {
615 ERROR(db
->pakfire
, "Could not disable autocheckpoint: %s\n",
616 sqlite3_errmsg(db
->handle
));
620 // Create or migrate schema
621 r
= pakfire_db_migrate_schema(db
);
628 int pakfire_db_open(struct pakfire_db
** db
, struct pakfire
* pakfire
, int flags
) {
631 struct pakfire_db
* o
= calloc(1, sizeof(*o
));
635 o
->pakfire
= pakfire_ref(pakfire
);
638 int sqlite3_flags
= 0;
640 // Store mode & forward it to sqlite3
641 if (flags
& PAKFIRE_DB_READWRITE
) {
642 o
->mode
= PAKFIRE_DB_READWRITE
;
643 sqlite3_flags
|= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
;
645 o
->mode
= PAKFIRE_DB_READONLY
;
646 sqlite3_flags
|= SQLITE_OPEN_READONLY
;
650 r
= pakfire_path(o
->pakfire
, o
->path
, "%s", DATABASE_PATH
);
654 // Try to open the sqlite3 database file
655 r
= sqlite3_open_v2(o
->path
, &o
->handle
, sqlite3_flags
, NULL
);
656 if (r
!= SQLITE_OK
) {
657 ERROR(pakfire
, "Could not open database %s: %s\n",
658 o
->path
, sqlite3_errmsg(o
->handle
));
664 // Setup the database
665 r
= pakfire_db_setup(o
);
669 // Check for compatible architecture
670 r
= pakfire_db_check_arch(o
);
684 struct pakfire_db
* pakfire_db_ref(struct pakfire_db
* db
) {
690 struct pakfire_db
* pakfire_db_unref(struct pakfire_db
* db
) {
699 static unsigned long pakfire_db_integrity_check(struct pakfire_db
* db
) {
700 sqlite3_stmt
* stmt
= NULL
;
703 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA integrity_check", -1, &stmt
, NULL
);
705 ERROR(db
->pakfire
, "Could not prepare integrity check: %s\n",
706 sqlite3_errmsg(db
->handle
));
711 unsigned long errors
= 0;
715 r
= sqlite3_step(stmt
);
716 } while (r
== SQLITE_BUSY
);
718 if (r
== SQLITE_ROW
) {
719 const char* error
= (const char*)sqlite3_column_text(stmt
, 0);
721 // If the message is "ok", the database has passed the check
722 if (strcmp(error
, "ok") == 0)
725 // Increment error counter
729 ERROR(db
->pakfire
, "%s\n", error
);
731 // Break on anything else
736 sqlite3_finalize(stmt
);
739 ERROR(db
->pakfire
, "Database integrity check failed\n");
741 INFO(db
->pakfire
, "Database integrity check passed\n");
746 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db
* db
) {
747 sqlite3_stmt
* stmt
= NULL
;
750 r
= sqlite3_prepare_v2(db
->handle
, "PRAGMA foreign_key_check", -1, &stmt
, NULL
);
752 ERROR(db
->pakfire
, "Could not prepare foreign key check: %s\n",
753 sqlite3_errmsg(db
->handle
));
758 unsigned long errors
= 0;
762 r
= sqlite3_step(stmt
);
763 } while (r
== SQLITE_BUSY
);
765 if (r
== SQLITE_ROW
) {
766 const unsigned char* table
= sqlite3_column_text(stmt
, 0);
767 unsigned long rowid
= sqlite3_column_int64(stmt
, 1);
768 const unsigned char* foreign_table
= sqlite3_column_text(stmt
, 2);
769 unsigned long foreign_rowid
= sqlite3_column_int64(stmt
, 3);
771 // Increment error counter
775 ERROR(db
->pakfire
, "Foreign key violation found in %s, row %lu: "
776 "%lu does not exist in table %s\n", table
, rowid
, foreign_rowid
, foreign_table
);
778 // Break on anything else
783 sqlite3_finalize(stmt
);
786 ERROR(db
->pakfire
, "Foreign key check failed\n");
788 INFO(db
->pakfire
, "Foreign key check passed\n");
794 This function performs an integrity check of the database
796 int pakfire_db_check(struct pakfire_db
* db
) {
799 // Perform integrity check
800 r
= pakfire_db_integrity_check(db
);
804 // Perform foreign key check
805 r
= pakfire_db_foreign_key_check(db
);
812 // Returns the number of packages installed
813 ssize_t
pakfire_db_packages(struct pakfire_db
* db
) {
814 sqlite3_stmt
* stmt
= NULL
;
815 ssize_t packages
= -1;
817 int r
= sqlite3_prepare_v2(db
->handle
, "SELECT COUNT(*) FROM packages", -1, &stmt
, NULL
);
819 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
820 sqlite3_errmsg(db
->handle
));
826 r
= sqlite3_step(stmt
);
827 } while (r
== SQLITE_BUSY
);
829 if (r
== SQLITE_ROW
) {
830 packages
= sqlite3_column_int64(stmt
, 0);
833 sqlite3_finalize(stmt
);
838 static void pakfire_db_add_userinstalled(struct pakfire
* pakfire
, const char* name
) {
839 Pool
* pool
= pakfire_get_solv_pool(pakfire
);
841 // Convert name to ID
842 Id id
= pool_str2id(pool
, name
, 1);
844 // Append it to pooljobs
845 queue_push2(&pool
->pooljobs
, SOLVER_USERINSTALLED
|SOLVER_SOLVABLE_NAME
, id
);
848 static const struct pakfire_digest
{
849 enum pakfire_digests type
;
851 } pakfire_digests
[] = {
852 { PAKFIRE_DIGEST_SHA512
, "sha512:" },
853 { PAKFIRE_DIGEST_SHA256
, "sha256:" },
857 static char* pakfire_db_pack_digest(enum pakfire_digests type
, const char* hexdigest
) {
861 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
862 if (digest
->type
== type
) {
863 r
= asprintf(&s
, "%s%s", digest
->prefix
, hexdigest
);
874 static const char* pakfire_db_unpack_digest(const char* hexdigest
, enum pakfire_digests
* type
) {
877 // Don't do anything for empty input
878 if (!hexdigest
|| !*hexdigest
)
881 for (const struct pakfire_digest
* digest
= pakfire_digests
; digest
->type
; digest
++) {
882 if (pakfire_string_startswith(hexdigest
, digest
->prefix
)) {
883 *type
= digest
->type
;
885 // Return the beginning of the hexdigest
886 return hexdigest
+ strlen(digest
->prefix
);
894 static int pakfire_db_add_dependencies(struct pakfire_db
* db
, unsigned long id
, struct pakfire_package
* pkg
) {
895 sqlite3_stmt
* stmt
= NULL
;
898 const char* sql
= "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
900 // Prepare the statement
901 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
903 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
904 sql
, sqlite3_errmsg(db
->handle
));
908 const struct __relation
{
910 char** (*func
)(struct pakfire_package
*);
912 { "provides", pakfire_package_get_provides
},
913 { "prerequires", pakfire_package_get_prerequires
},
914 { "requires", pakfire_package_get_requires
},
915 { "conflicts", pakfire_package_get_conflicts
},
916 { "obsoletes", pakfire_package_get_obsoletes
},
917 { "recommends", pakfire_package_get_recommends
},
918 { "suggests", pakfire_package_get_suggests
},
919 { "supplements", pakfire_package_get_supplements
},
920 { "enhances", pakfire_package_get_enhances
},
924 for (const struct __relation
* relation
= relations
; relation
->type
; relation
++) {
925 char** list
= relation
->func(pkg
);
929 for (char** dep
= list
; *dep
; dep
++) {
931 r
= sqlite3_bind_int64(stmt
, 1, id
);
933 ERROR(db
->pakfire
, "Could not bind id: %s\n",
934 sqlite3_errmsg(db
->handle
));
939 r
= sqlite3_bind_text(stmt
, 2, relation
->type
, -1, NULL
);
941 ERROR(db
->pakfire
, "Could not bind type: %s\n",
942 sqlite3_errmsg(db
->handle
));
947 r
= sqlite3_bind_text(stmt
, 3, *dep
, -1, NULL
);
949 ERROR(db
->pakfire
, "Could not bind dependency: %s\n",
950 sqlite3_errmsg(db
->handle
));
956 r
= sqlite3_step(stmt
);
957 } while (r
== SQLITE_BUSY
);
961 // Reset bound values
973 sqlite3_finalize(stmt
);
978 static int pakfire_db_file_add_digests(struct pakfire_db
* db
, unsigned long id
,
979 struct pakfire_file
* file
) {
980 sqlite3_stmt
* stmt
= NULL
;
983 // XXX add a check that avoids running through the loop for files that won't have
984 // any digests (e.g. symlinks, directories, etc.)
986 static enum pakfire_digests digests
[] = {
987 PAKFIRE_DIGEST_SHA512
,
988 PAKFIRE_DIGEST_SHA256
,
994 const char* sql
= "INSERT INTO file_digests(file, type, digest) VALUES(?, ?, ?)";
996 // Prepare the statement
997 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
999 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1000 sql
, sqlite3_errmsg(db
->handle
));
1006 for (enum pakfire_digests
* type
= digests
; *type
; type
++) {
1008 const unsigned char* digest
= pakfire_file_get_digest(file
, *type
, &length
);
1010 // Skip if the file does not have a digest of this type
1015 r
= sqlite3_bind_int64(stmt
, 1, id
);
1017 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1022 r
= sqlite3_bind_int64(stmt
, 2, (int64_t)*type
);
1024 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1029 r
= sqlite3_bind_blob(stmt
, 3, digest
, length
, NULL
);
1031 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1037 r
= sqlite3_step(stmt
);
1038 } while (r
== SQLITE_BUSY
);
1041 if (r
!= SQLITE_DONE
) {
1042 ERROR(db
->pakfire
, "Could not add digest to database: %s\n",
1043 sqlite3_errmsg(db
->handle
));
1047 // Reset bound values
1048 sqlite3_reset(stmt
);
1056 sqlite3_finalize(stmt
);
1061 static int pakfire_db_add_files(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1062 sqlite3_stmt
* stmt
= NULL
;
1065 // Get the filelist from the archive
1066 struct pakfire_filelist
* filelist
= pakfire_archive_get_filelist(archive
);
1068 ERROR(db
->pakfire
, "Could not fetch filelist from archive\n");
1072 // Nothing to do if the list is empty
1073 if (pakfire_filelist_is_empty(filelist
)) {
1078 const char* sql
= "INSERT INTO files(pkg, path, size, config, datafile, mode, "
1079 "user, 'group', ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
1081 // Prepare the statement
1082 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1084 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1085 sql
, sqlite3_errmsg(db
->handle
));
1089 for (unsigned int i
= 0; i
< pakfire_filelist_size(filelist
); i
++) {
1090 struct pakfire_file
* file
= pakfire_filelist_get(filelist
, i
);
1093 r
= sqlite3_bind_int64(stmt
, 1, id
);
1095 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1096 pakfire_file_unref(file
);
1101 const char* path
= pakfire_file_get_path(file
);
1103 r
= sqlite3_bind_text(stmt
, 2, path
, -1, NULL
);
1105 ERROR(db
->pakfire
, "Could not bind path: %s\n", sqlite3_errmsg(db
->handle
));
1106 pakfire_file_unref(file
);
1111 size_t size
= pakfire_file_get_size(file
);
1113 r
= sqlite3_bind_int64(stmt
, 3, size
);
1115 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1116 pakfire_file_unref(file
);
1120 // Bind config - XXX TODO
1121 r
= sqlite3_bind_null(stmt
, 4);
1123 ERROR(db
->pakfire
, "Could not bind config: %s\n", sqlite3_errmsg(db
->handle
));
1124 pakfire_file_unref(file
);
1128 // Bind datafile - XXX TODO
1129 r
= sqlite3_bind_null(stmt
, 5);
1131 ERROR(db
->pakfire
, "Could not bind datafile: %s\n", sqlite3_errmsg(db
->handle
));
1132 pakfire_file_unref(file
);
1137 mode_t mode
= pakfire_file_get_mode(file
);
1139 r
= sqlite3_bind_int64(stmt
, 6, mode
);
1141 ERROR(db
->pakfire
, "Could not bind mode: %s\n", sqlite3_errmsg(db
->handle
));
1142 pakfire_file_unref(file
);
1147 const char* user
= pakfire_file_get_user(file
);
1149 r
= sqlite3_bind_text(stmt
, 7, user
, -1, NULL
);
1151 ERROR(db
->pakfire
, "Could not bind user: %s\n", sqlite3_errmsg(db
->handle
));
1152 pakfire_file_unref(file
);
1157 const char* group
= pakfire_file_get_group(file
);
1159 r
= sqlite3_bind_text(stmt
, 8, group
, -1, NULL
);
1161 ERROR(db
->pakfire
, "Could not bind group: %s\n", sqlite3_errmsg(db
->handle
));
1162 pakfire_file_unref(file
);
1167 time_t ctime
= pakfire_file_get_ctime(file
);
1169 r
= sqlite3_bind_int64(stmt
, 9, ctime
);
1171 ERROR(db
->pakfire
, "Could not bind ctime: %s\n", sqlite3_errmsg(db
->handle
));
1172 pakfire_file_unref(file
);
1177 time_t mtime
= pakfire_file_get_mtime(file
);
1179 r
= sqlite3_bind_int64(stmt
, 10, mtime
);
1181 ERROR(db
->pakfire
, "Could not bind mtime: %s\n", sqlite3_errmsg(db
->handle
));
1182 pakfire_file_unref(file
);
1186 // Bind capabilities - XXX TODO
1187 r
= sqlite3_bind_null(stmt
, 11);
1189 ERROR(db
->pakfire
, "Could not bind capabilities: %s\n", sqlite3_errmsg(db
->handle
));
1190 pakfire_file_unref(file
);
1196 r
= sqlite3_step(stmt
);
1197 } while (r
== SQLITE_BUSY
);
1200 if (r
!= SQLITE_DONE
) {
1201 ERROR(db
->pakfire
, "Could not add file to database: %s\n",
1202 sqlite3_errmsg(db
->handle
));
1203 pakfire_file_unref(file
);
1208 unsigned long file_id
= sqlite3_last_insert_rowid(db
->handle
);
1211 r
= pakfire_db_file_add_digests(db
, file_id
, file
);
1213 pakfire_file_unref(file
);
1217 // Move on to next file
1218 pakfire_file_unref(file
);
1220 // Reset bound values
1221 sqlite3_reset(stmt
);
1229 sqlite3_finalize(stmt
);
1230 pakfire_filelist_unref(filelist
);
1235 static int pakfire_db_add_scriptlets(struct pakfire_db
* db
, unsigned long id
, struct pakfire_archive
* archive
) {
1236 sqlite3_stmt
* stmt
= NULL
;
1240 const char* sql
= "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1242 // Prepare the statement
1243 r
= sqlite3_prepare_v2(db
->handle
, sql
, -1, &stmt
, NULL
);
1245 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1246 sql
, sqlite3_errmsg(db
->handle
));
1250 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
1251 // Fetch the scriptlet
1252 struct pakfire_scriptlet
* scriptlet
= pakfire_archive_get_scriptlet(archive
, *type
);
1257 r
= sqlite3_bind_int64(stmt
, 1, id
);
1259 ERROR(db
->pakfire
, "Could not bind id: %s\n", sqlite3_errmsg(db
->handle
));
1260 pakfire_scriptlet_unref(scriptlet
);
1265 r
= sqlite3_bind_text(stmt
, 2, *type
, -1, NULL
);
1267 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1268 pakfire_scriptlet_unref(scriptlet
);
1272 const char* data
= pakfire_scriptlet_get_data(scriptlet
, &size
);
1275 r
= sqlite3_bind_text(stmt
, 3, data
, size
, NULL
);
1277 ERROR(db
->pakfire
, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db
->handle
));
1278 pakfire_scriptlet_unref(scriptlet
);
1284 r
= sqlite3_step(stmt
);
1285 } while (r
== SQLITE_BUSY
);
1288 if (r
!= SQLITE_DONE
) {
1289 ERROR(db
->pakfire
, "Could not add scriptlet to database: %s\n",
1290 sqlite3_errmsg(db
->handle
));
1291 pakfire_scriptlet_unref(scriptlet
);
1295 pakfire_scriptlet_unref(scriptlet
);
1297 // Reset bound values
1298 sqlite3_reset(stmt
);
1306 sqlite3_finalize(stmt
);
1311 int pakfire_db_add_package(struct pakfire_db
* db
,
1312 struct pakfire_package
* pkg
, struct pakfire_archive
* archive
, int userinstalled
) {
1313 sqlite3_stmt
* stmt
= NULL
;
1314 char* digest
= NULL
;
1317 // Begin a new transaction
1318 r
= pakfire_db_begin_transaction(db
);
1322 const char* sql
= "INSERT INTO "
1362 "CURRENT_TIMESTAMP, "
1371 // Prepare the statement
1372 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1373 if (r
!= SQLITE_OK
) {
1374 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s: %s\n",
1375 sql
, sqlite3_errmsg(db
->handle
));
1380 const char* name
= pakfire_package_get_name(pkg
);
1382 r
= sqlite3_bind_text(stmt
, 1, name
, -1, NULL
);
1384 ERROR(db
->pakfire
, "Could not bind name: %s\n", sqlite3_errmsg(db
->handle
));
1389 const char* evr
= pakfire_package_get_evr(pkg
);
1391 r
= sqlite3_bind_text(stmt
, 2, evr
, -1, NULL
);
1393 ERROR(db
->pakfire
, "Could not bind evr: %s\n", sqlite3_errmsg(db
->handle
));
1398 const char* arch
= pakfire_package_get_arch(pkg
);
1400 r
= sqlite3_bind_text(stmt
, 3, arch
, -1, NULL
);
1402 ERROR(db
->pakfire
, "Could not bind arch: %s\n", sqlite3_errmsg(db
->handle
));
1407 char* groups
= pakfire_package_get_groups(pkg
);
1409 r
= sqlite3_bind_text(stmt
, 4, groups
, -1, NULL
);
1411 ERROR(db
->pakfire
, "Could not bind groups: %s\n", sqlite3_errmsg(db
->handle
));
1420 r
= sqlite3_bind_null(stmt
, 4);
1426 const char* filename
= pakfire_package_get_filename(pkg
);
1428 r
= sqlite3_bind_text(stmt
, 5, filename
, -1, NULL
);
1430 ERROR(db
->pakfire
, "Could not bind filename: %s\n", sqlite3_errmsg(db
->handle
));
1435 unsigned long long size
= pakfire_package_get_downloadsize(pkg
);
1437 r
= sqlite3_bind_int64(stmt
, 6, size
);
1439 ERROR(db
->pakfire
, "Could not bind size: %s\n", sqlite3_errmsg(db
->handle
));
1443 // Bind installed size
1444 unsigned long long inst_size
= pakfire_package_get_installsize(pkg
);
1446 r
= sqlite3_bind_int64(stmt
, 7, inst_size
);
1448 ERROR(db
->pakfire
, "Could not bind inst_size: %s\n", sqlite3_errmsg(db
->handle
));
1452 enum pakfire_digests digest_type
= 0;
1454 const char* hexdigest
= pakfire_package_get_hexdigest(pkg
, &digest_type
);
1456 digest
= pakfire_db_pack_digest(digest_type
, hexdigest
);
1460 r
= sqlite3_bind_text(stmt
, 8, digest
, -1, NULL
);
1462 ERROR(db
->pakfire
, "Could not bind digest: %s\n", sqlite3_errmsg(db
->handle
));
1468 const char* license
= pakfire_package_get_license(pkg
);
1470 r
= sqlite3_bind_text(stmt
, 9, license
, -1, NULL
);
1472 ERROR(db
->pakfire
, "Could not bind license: %s\n", sqlite3_errmsg(db
->handle
));
1477 const char* summary
= pakfire_package_get_summary(pkg
);
1479 r
= sqlite3_bind_text(stmt
, 10, summary
, -1, NULL
);
1481 ERROR(db
->pakfire
, "Could not bind summary: %s\n", sqlite3_errmsg(db
->handle
));
1486 const char* description
= pakfire_package_get_description(pkg
);
1488 r
= sqlite3_bind_text(stmt
, 11, description
, -1, NULL
);
1490 ERROR(db
->pakfire
, "Could not bind description: %s\n", sqlite3_errmsg(db
->handle
));
1495 const char* uuid
= pakfire_package_get_uuid(pkg
);
1497 r
= sqlite3_bind_text(stmt
, 12, uuid
, -1, NULL
);
1499 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1504 const char* vendor
= pakfire_package_get_vendor(pkg
);
1506 r
= sqlite3_bind_text(stmt
, 13, vendor
, -1, NULL
);
1508 ERROR(db
->pakfire
, "Could not bind vendor: %s\n", sqlite3_errmsg(db
->handle
));
1513 const char* build_host
= pakfire_package_get_build_host(pkg
);
1515 r
= sqlite3_bind_text(stmt
, 14, build_host
, -1, NULL
);
1517 ERROR(db
->pakfire
, "Could not bind build_host: %s\n", sqlite3_errmsg(db
->handle
));
1522 time_t build_time
= pakfire_package_get_build_time(pkg
);
1524 r
= sqlite3_bind_int64(stmt
, 15, build_time
);
1526 ERROR(db
->pakfire
, "Could not bind build_time: %s\n", sqlite3_errmsg(db
->handle
));
1530 // Bind repository name
1531 struct pakfire_repo
* repo
= pakfire_package_get_repo(pkg
);
1533 const char* repo_name
= pakfire_repo_get_name(repo
);
1534 pakfire_repo_unref(repo
);
1536 r
= sqlite3_bind_text(stmt
, 16, repo_name
, -1, NULL
);
1542 r
= sqlite3_bind_null(stmt
, 16);
1547 // installed by the user?
1548 r
= sqlite3_bind_int(stmt
, 17, userinstalled
);
1550 ERROR(db
->pakfire
, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db
->handle
));
1554 // Source package name
1555 const char* source_name
= pakfire_package_get_source_name(pkg
);
1557 r
= sqlite3_bind_text(stmt
, 18, source_name
, -1, NULL
);
1561 r
= sqlite3_bind_null(stmt
, 18);
1567 const char* source_evr
= pakfire_package_get_source_evr(pkg
);
1569 r
= sqlite3_bind_text(stmt
, 19, source_evr
, -1, NULL
);
1573 r
= sqlite3_bind_null(stmt
, 19);
1579 const char* source_arch
= pakfire_package_get_source_arch(pkg
);
1581 r
= sqlite3_bind_text(stmt
, 20, source_arch
, -1, NULL
);
1585 r
= sqlite3_bind_null(stmt
, 20);
1591 const char* distribution
= pakfire_package_get_distribution(pkg
);
1593 r
= sqlite3_bind_text(stmt
, 21, distribution
, -1, NULL
);
1597 r
= sqlite3_bind_null(stmt
, 21);
1604 r
= sqlite3_step(stmt
);
1605 } while (r
== SQLITE_BUSY
);
1607 if (r
!= SQLITE_DONE
) {
1608 ERROR(db
->pakfire
, "Could not add package to database: %s\n",
1609 sqlite3_errmsg(db
->handle
));
1615 unsigned long packages_id
= sqlite3_last_insert_rowid(db
->handle
);
1618 r
= pakfire_db_add_dependencies(db
, packages_id
, pkg
);
1623 r
= pakfire_db_add_files(db
, packages_id
, archive
);
1628 r
= pakfire_db_add_scriptlets(db
, packages_id
, archive
);
1634 sqlite3_finalize(stmt
);
1638 // Commit or rollback
1640 pakfire_db_rollback(db
);
1642 r
= pakfire_db_commit(db
);
1647 int pakfire_db_remove_package(struct pakfire_db
* db
, struct pakfire_package
* pkg
) {
1648 sqlite3_stmt
* stmt
= NULL
;
1651 // Begin a new transaction
1652 r
= pakfire_db_begin_transaction(db
);
1656 // Fetch the package's UUID
1657 const char* uuid
= pakfire_package_get_uuid(pkg
);
1659 ERROR(db
->pakfire
, "Package has no UUID\n");
1664 r
= sqlite3_prepare_v2(db
->handle
,
1665 "DELETE FROM packages WHERE uuid = ?", -1, &stmt
, NULL
);
1667 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s\n",
1668 sqlite3_errmsg(db
->handle
));
1673 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1675 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1681 r
= sqlite3_step(stmt
);
1682 } while (r
== SQLITE_BUSY
);
1684 // Check if we have been successful
1685 if (r
!= SQLITE_DONE
) {
1686 ERROR(db
->pakfire
, "Could not delete package %s\n", uuid
);
1696 sqlite3_finalize(stmt
);
1698 // Commit or rollback
1700 pakfire_db_rollback(db
);
1702 r
= pakfire_db_commit(db
);
1707 struct pakfire_scriptlet
* pakfire_db_get_scriptlet(struct pakfire_db
* db
,
1708 struct pakfire_package
* pkg
, const char* type
) {
1709 struct pakfire_scriptlet
* scriptlet
= NULL
;
1710 sqlite3_stmt
* stmt
= NULL
;
1713 // Fetch the package's UUID
1714 const char* uuid
= pakfire_package_get_uuid(pkg
);
1716 ERROR(db
->pakfire
, "Package has no UUID\n");
1720 const char* sql
= "SELECT scriptlets.scriptlet FROM packages \
1721 JOIN scriptlets ON packages.id = scriptlets.pkg \
1722 WHERE packages.uuid = ? AND scriptlets.type = ?";
1724 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
1726 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
1727 sql
, sqlite3_errmsg(db
->handle
));
1732 r
= sqlite3_bind_text(stmt
, 1, uuid
, -1, NULL
);
1734 ERROR(db
->pakfire
, "Could not bind uuid: %s\n", sqlite3_errmsg(db
->handle
));
1738 r
= sqlite3_bind_text(stmt
, 2, type
, -1, NULL
);
1740 ERROR(db
->pakfire
, "Could not bind type: %s\n", sqlite3_errmsg(db
->handle
));
1744 DEBUG(db
->pakfire
, "Searching for scriptlet for package %s of type %s\n", uuid
, type
);
1748 r
= sqlite3_step(stmt
);
1749 } while (r
== SQLITE_BUSY
);
1751 // We have some payload
1752 if (r
== SQLITE_ROW
) {
1753 const void* data
= sqlite3_column_blob(stmt
, 1);
1754 ssize_t size
= sqlite3_column_bytes(stmt
, 1);
1756 // Create a scriptlet object
1757 r
= pakfire_scriptlet_create(&scriptlet
, db
->pakfire
, type
, data
, size
);
1764 sqlite3_finalize(stmt
);
1769 static int pakfire_db_load_package(struct pakfire_db
* db
, struct pakfire_repo
* repo
, sqlite3_stmt
* stmt
) {
1770 struct pakfire_package
* pkg
= NULL
;
1774 const char* name
= (const char*)sqlite3_column_text(stmt
, 0);
1776 ERROR(db
->pakfire
, "Could not read name: %s\n", sqlite3_errmsg(db
->handle
));
1781 const char* evr
= (const char*)sqlite3_column_text(stmt
, 1);
1783 ERROR(db
->pakfire
, "Could not read evr: %s\n", sqlite3_errmsg(db
->handle
));
1788 const char* arch
= (const char*)sqlite3_column_text(stmt
, 2);
1790 ERROR(db
->pakfire
, "Could not read arch: %s\n", sqlite3_errmsg(db
->handle
));
1795 pkg
= pakfire_package_create(db
->pakfire
, repo
, name
, evr
, arch
);
1797 ERROR(db
->pakfire
, "Could not create package\n");
1802 uint64_t id
= sqlite3_column_int64(stmt
, 3);
1804 pakfire_package_set_dbid(pkg
, id
);
1807 const char* groups
= (const char*)sqlite3_column_text(stmt
, 4);
1809 pakfire_package_set_groups(pkg
, groups
);
1813 const char* filename
= (const char*)sqlite3_column_text(stmt
, 5);
1815 pakfire_package_set_filename(pkg
, filename
);
1819 size_t size
= sqlite3_column_int64(stmt
, 6);
1821 pakfire_package_set_downloadsize(pkg
, size
);
1825 size
= sqlite3_column_int64(stmt
, 7);
1827 pakfire_package_set_installsize(pkg
, size
);
1831 const char* digest
= (const char*)sqlite3_column_text(stmt
, 8);
1833 enum pakfire_digests digest_type
= 0;
1836 const char* hexdigest
= pakfire_db_unpack_digest(digest
, &digest_type
);
1838 pakfire_package_set_hexdigest(pkg
, digest_type
, hexdigest
);
1842 const char* license
= (const char*)sqlite3_column_text(stmt
, 9);
1844 pakfire_package_set_license(pkg
, license
);
1848 const char* summary
= (const char*)sqlite3_column_text(stmt
, 10);
1850 pakfire_package_set_summary(pkg
, summary
);
1854 const char* description
= (const char*)sqlite3_column_text(stmt
, 11);
1856 pakfire_package_set_description(pkg
, description
);
1860 const char* uuid
= (const char*)sqlite3_column_text(stmt
, 12);
1862 pakfire_package_set_uuid(pkg
, uuid
);
1866 const char* vendor
= (const char*)sqlite3_column_text(stmt
, 13);
1868 pakfire_package_set_vendor(pkg
, vendor
);
1872 const char* build_host
= (const char*)sqlite3_column_text(stmt
, 14);
1874 pakfire_package_set_build_host(pkg
, build_host
);
1878 time_t build_time
= sqlite3_column_int64(stmt
, 15);
1880 pakfire_package_set_build_time(pkg
, build_time
);
1884 time_t install_time
= sqlite3_column_int64(stmt
, 16);
1886 pakfire_package_set_install_time(pkg
, install_time
);
1889 // installed by user?
1890 int userinstalled
= sqlite3_column_int(stmt
, 17);
1892 pakfire_db_add_userinstalled(db
->pakfire
, name
);
1895 const char* files
= (const char*)sqlite3_column_text(stmt
, 18);
1897 r
= pakfire_package_set_filelist_from_string(pkg
, files
);
1904 const struct dependency
{
1906 void (*func
)(struct pakfire_package
* pkg
, const char* dep
);
1907 } dependencies
[] = {
1908 { 19, pakfire_package_add_provides
},
1909 { 20, pakfire_package_add_prerequires
},
1910 { 21, pakfire_package_add_requires
},
1911 { 22, pakfire_package_add_conflicts
},
1912 { 23, pakfire_package_add_obsoletes
},
1913 { 24, pakfire_package_add_recommends
},
1914 { 25, pakfire_package_add_suggests
},
1915 { 26, pakfire_package_add_supplements
},
1916 { 27, pakfire_package_add_enhances
},
1920 for (const struct dependency
* deps
= dependencies
; deps
->field
; deps
++) {
1921 const char* relations
= (const char*)sqlite3_column_text(stmt
, deps
->field
);
1923 pakfire_str2deps(db
->pakfire
, pkg
, deps
->func
, relations
);
1928 const char* source_name
= (const char*)sqlite3_column_text(stmt
, 28);
1930 pakfire_package_set_source_name(pkg
, source_name
);
1933 const char* source_evr
= (const char*)sqlite3_column_text(stmt
, 29);
1935 pakfire_package_set_source_evr(pkg
, source_evr
);
1938 const char* source_arch
= (const char*)sqlite3_column_text(stmt
, 30);
1940 pakfire_package_set_source_arch(pkg
, source_arch
);
1943 const char* distribution
= (const char*)sqlite3_column_text(stmt
, 31);
1945 pakfire_package_set_distribution(pkg
, distribution
);
1952 pakfire_package_unref(pkg
);
1957 int pakfire_db_load(struct pakfire_db
* db
, struct pakfire_repo
* repo
) {
1958 sqlite3_stmt
* stmt
= NULL
;
1961 DEBUG(db
->pakfire
, "Loading package database...\n");
1963 // Drop contents of the repository
1964 pakfire_repo_clear(repo
);
1966 // Save starting time
1967 clock_t t_start
= clock();
1988 "strftime('%s', installed) AS installed, "
1991 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1994 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1995 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1998 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1999 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
2000 ") AS prerequires, "
2002 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2003 "WHERE d.pkg = packages.id AND d.type = 'requires'"
2006 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2007 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
2010 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2011 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
2014 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2015 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
2018 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2019 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
2022 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2023 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
2024 ") AS supplements, "
2026 "SELECT group_concat(dependency, '\n') FROM dependencies d "
2027 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
2037 // Prepare the statement
2038 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2040 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2041 sql
, sqlite3_errmsg(db
->handle
));
2047 r
= sqlite3_step(stmt
);
2050 // Retry if the database was busy
2056 r
= pakfire_db_load_package(db
, repo
, stmt
);
2061 // All rows have been processed
2065 // Go to error in any other cases
2072 // Save time when we finished
2075 DEBUG(db
->pakfire
, "Loading package database completed in %.4fms\n",
2076 (double)(t_end
- t_start
) * 1000 / CLOCKS_PER_SEC
);
2078 // Mark repository as changed
2079 pakfire_repo_has_changed(repo
);
2086 ERROR(db
->pakfire
, "Failed reading package database: %d\n", r
);
2087 pakfire_repo_clear(repo
);
2091 sqlite3_finalize(stmt
);
2096 static int pakfire_db_load_file(struct pakfire_db
* db
, struct pakfire_filelist
* filelist
,
2097 sqlite3_stmt
* stmt
) {
2098 struct pakfire_file
* file
= NULL
;
2099 char abspath
[PATH_MAX
];
2102 // Create a new file object
2103 r
= pakfire_file_create(&file
, db
->pakfire
);
2108 const char* path
= (const char*)sqlite3_column_text(stmt
, 0);
2110 pakfire_file_set_path(file
, path
);
2113 r
= pakfire_path(db
->pakfire
, abspath
, "%s", path
);
2117 pakfire_file_set_abspath(file
, abspath
);
2120 size_t size
= sqlite3_column_int64(stmt
, 1);
2122 pakfire_file_set_size(file
, size
);
2125 mode_t mode
= sqlite3_column_int(stmt
, 2);
2127 pakfire_file_set_mode(file
, mode
);
2130 const char* user
= (const char*)sqlite3_column_text(stmt
, 3);
2132 pakfire_file_set_user(file
, user
);
2135 const char* group
= (const char*)sqlite3_column_text(stmt
, 4);
2137 pakfire_file_set_group(file
, group
);
2140 time_t ctime
= sqlite3_column_int64(stmt
, 5);
2142 pakfire_file_set_ctime(file
, ctime
);
2145 time_t mtime
= sqlite3_column_int64(stmt
, 6);
2147 pakfire_file_set_mtime(file
, mtime
);
2149 // Append the file to the filelist
2150 r
= pakfire_filelist_append(filelist
, file
);
2154 pakfire_file_unref(file
);
2159 int pakfire_db_package_filelist(struct pakfire_db
* db
, struct pakfire_filelist
** filelist
,
2160 struct pakfire_package
* pkg
) {
2161 struct pakfire_filelist
* fl
= NULL
;
2162 sqlite3_stmt
* stmt
= NULL
;
2165 // Fetch the package ID
2166 uint64_t id
= pakfire_package_get_dbid(pkg
);
2168 ERROR(db
->pakfire
, "Package did not have an ID\n");
2172 // Create a new filelist
2173 r
= pakfire_filelist_create(&fl
, db
->pakfire
);
2175 ERROR(db
->pakfire
, "Could not create filelist: %m\n");
2190 // Select all files that belong to this package
2194 // Filter out any files that are also in a different package (i.e. an update
2195 // that has already been installed and this is the cleanup of the obsolete pkg)
2200 "FROM files AS duplicates "
2202 "files.path = duplicates.path "
2204 "files.pkg != duplicates.pkg"
2207 // Return the longest paths first
2212 // Prepare the statement
2213 r
= sqlite3_prepare_v2(db
->handle
, sql
, strlen(sql
), &stmt
, NULL
);
2215 ERROR(db
->pakfire
, "Could not prepare SQL statement: %s %s\n",
2216 sql
, sqlite3_errmsg(db
->handle
));
2221 r
= sqlite3_bind_int64(stmt
, 1, id
);
2223 ERROR(db
->pakfire
, "Could not bind package ID: %s\n", sqlite3_errmsg(db
->handle
));
2229 r
= sqlite3_step(stmt
);
2232 // Retry if the database was busy
2238 r
= pakfire_db_load_file(db
, fl
, stmt
);
2243 // All rows have been processed
2247 // Go to error in any other cases
2254 *filelist
= pakfire_filelist_ref(fl
);
2259 sqlite3_finalize(stmt
);
2261 pakfire_filelist_unref(fl
);