]> git.ipfire.org Git - pakfire.git/blob - src/libpakfire/db.c
db: Fix updated table layout
[pakfire.git] / src / libpakfire / db.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2021 Pakfire development team #
5 # #
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. #
10 # #
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. #
15 # #
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/>. #
18 # #
19 #############################################################################*/
20
21 #include <errno.h>
22 #include <linux/limits.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include <openssl/sha.h>
27 #include <solv/solver.h>
28 #include <sqlite3.h>
29
30 #include <pakfire/archive.h>
31 #include <pakfire/db.h>
32 #include <pakfire/dependencies.h>
33 #include <pakfire/digest.h>
34 #include <pakfire/file.h>
35 #include <pakfire/filelist.h>
36 #include <pakfire/logging.h>
37 #include <pakfire/package.h>
38 #include <pakfire/pakfire.h>
39 #include <pakfire/repo.h>
40 #include <pakfire/string.h>
41 #include <pakfire/util.h>
42
43 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
44
45 #define CURRENT_SCHEMA 8
46 #define SCHEMA_MIN_SUP 7
47
48 struct pakfire_db {
49 struct pakfire* pakfire;
50 int nrefs;
51
52 char path[PATH_MAX];
53 int mode;
54
55 sqlite3* handle;
56 int schema;
57 time_t last_modified_at;
58 };
59
60 static void logging_callback(void* data, int r, const char* msg) {
61 struct pakfire* pakfire = (struct pakfire*)data;
62
63 ERROR(pakfire, "Database Error: %s: %s\n",
64 sqlite3_errstr(r), msg);
65 }
66
67 static sqlite3_value* pakfire_db_get(struct pakfire_db* db, const char* key) {
68 sqlite3_stmt* stmt = NULL;
69 sqlite3_value* val = NULL;
70 int r;
71
72 const char* sql = "SELECT val FROM settings WHERE key = ?";
73
74 // Prepare the statement
75 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
76 if (r != SQLITE_OK) {
77 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
78 // sql, sqlite3_errmsg(db->handle));
79 return NULL;
80 }
81
82 // Bind key
83 r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL);
84 if (r != SQLITE_OK) {
85 ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle));
86 goto ERROR;
87 }
88
89 // Execute the statement
90 do {
91 r = sqlite3_step(stmt);
92 } while (r == SQLITE_BUSY);
93
94 // We should have read a row
95 if (r != SQLITE_ROW)
96 goto ERROR;
97
98 // Read value
99 val = sqlite3_column_value(stmt, 0);
100 if (!val) {
101 ERROR(db->pakfire, "Could not read value\n");
102 goto ERROR;
103 }
104
105 // Copy value onto the heap
106 val = sqlite3_value_dup(val);
107
108 ERROR:
109 if (stmt)
110 sqlite3_finalize(stmt);
111
112 return val;
113 }
114
115 static char* pakfire_db_get_string(struct pakfire_db* db, const char* key) {
116 char* s = NULL;
117
118 // Fetch the value from the database
119 sqlite3_value* value = pakfire_db_get(db, key);
120 if (!value)
121 return NULL;
122
123 // Extract the value as string
124 const char* p = (const char*)sqlite3_value_text(value);
125 if (!p)
126 goto ERROR;
127
128 // Copy string to heap
129 s = strdup(p);
130
131 ERROR:
132 if (value)
133 sqlite3_value_free(value);
134
135 return s;
136 }
137
138 static int pakfire_db_set_string(struct pakfire_db* db, const char* key, const char* val) {
139 sqlite3_stmt* stmt = NULL;
140 int r;
141
142 DEBUG(db->pakfire, "Setting %s to '%s'\n", key, val);
143
144 const char* sql = "INSERT INTO settings(key, val) VALUES(?, ?) \
145 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
146
147 // Prepare statement
148 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
149 if (r != SQLITE_OK) {
150 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
151 sql, sqlite3_errmsg(db->handle));
152 return 1;
153 }
154
155 // Bind key
156 r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL);
157 if (r != SQLITE_OK) {
158 ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle));
159 goto ERROR;
160 }
161
162 // Bind val
163 r = sqlite3_bind_text(stmt, 2, val, strlen(val), NULL);
164 if (r != SQLITE_OK) {
165 ERROR(db->pakfire, "Could not bind val: %s\n", sqlite3_errmsg(db->handle));
166 goto ERROR;
167 }
168
169 // Execute the statement
170 do {
171 r = sqlite3_step(stmt);
172 } while (r == SQLITE_BUSY);
173
174 // Set return code
175 r = (r == SQLITE_OK);
176
177 ERROR:
178 if (stmt)
179 sqlite3_finalize(stmt);
180
181 return r;
182 }
183
184 static int pakfire_db_set_int(struct pakfire_db* db, const char* key, int val) {
185 sqlite3_stmt* stmt = NULL;
186 int r;
187
188 DEBUG(db->pakfire, "Setting %s to '%d'\n", key, val);
189
190 const char* sql = "INSERT INTO settings(key, val) VALUES(?, ?) \
191 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
192
193 // Prepare statement
194 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
195 if (r != SQLITE_OK) {
196 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
197 sql, sqlite3_errmsg(db->handle));
198 return 1;
199 }
200
201 // Bind key
202 r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL);
203 if (r != SQLITE_OK) {
204 ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle));
205 goto ERROR;
206 }
207
208 // Bind val
209 r = sqlite3_bind_int64(stmt, 2, val);
210 if (r != SQLITE_OK) {
211 ERROR(db->pakfire, "Could not bind val: %s\n", sqlite3_errmsg(db->handle));
212 goto ERROR;
213 }
214
215 // Execute the statement
216 do {
217 r = sqlite3_step(stmt);
218 } while (r == SQLITE_BUSY);
219
220 // Set return code
221 r = (r == SQLITE_OK);
222
223 ERROR:
224 if (stmt)
225 sqlite3_finalize(stmt);
226
227 return r;
228 }
229
230 static time_t pakfire_read_modification_time(struct pakfire_db* db) {
231 time_t t = 0;
232
233 // Fetch the value from the database
234 sqlite3_value* value = pakfire_db_get(db, "last_modified_at");
235 if (value) {
236 t = sqlite3_value_int64(value);
237 sqlite3_value_free(value);
238 } else {
239 DEBUG(db->pakfire, "Could not find last modification timestamp\n");
240 }
241
242 return t;
243 }
244
245 static int pakfire_update_modification_time(struct pakfire_db* db) {
246 // Get the current time in UTC
247 time_t t = time(NULL);
248
249 // Store it in the database
250 int r = pakfire_db_set_int(db, "last_modified_at", t);
251 if (r)
252 return r;
253
254 // Update the last modification timestamp
255 db->last_modified_at = t;
256
257 return r;
258 }
259
260 static int pakfire_db_execute(struct pakfire_db* db, const char* stmt) {
261 int r;
262
263 DEBUG(db->pakfire, "Executing database query: %s\n", stmt);
264
265 do {
266 r = sqlite3_exec(db->handle, stmt, NULL, NULL, NULL);
267 } while (r == SQLITE_BUSY);
268
269 // Log any errors
270 if (r) {
271 ERROR(db->pakfire, "Database query failed: %s\n", sqlite3_errmsg(db->handle));
272 }
273
274 return r;
275 }
276
277 static int pakfire_db_begin_transaction(struct pakfire_db* db) {
278 return pakfire_db_execute(db, "BEGIN TRANSACTION");
279 }
280
281 static int pakfire_db_commit(struct pakfire_db* db) {
282 /*
283 If the database was opened in read-write mode, we will store the
284 timestamp of the latest modification to compare whether the database
285 has been changed mid-transaction.
286 */
287 if (db->mode == PAKFIRE_DB_READWRITE) {
288 int r = pakfire_update_modification_time(db);
289 if (r)
290 return r;
291 }
292
293 return pakfire_db_execute(db, "COMMIT");
294 }
295
296 static int pakfire_db_rollback(struct pakfire_db* db) {
297 return pakfire_db_execute(db, "ROLLBACK");
298 }
299
300 /*
301 This function performs any fast optimization and tries to truncate the WAL log file
302 to keep the database as compact as possible on disk.
303 */
304 static void pakfire_db_optimize(struct pakfire_db* db) {
305 pakfire_db_execute(db, "PRAGMA optimize");
306 pakfire_db_execute(db, "PRAGMA wal_checkpoint = TRUNCATE");
307 }
308
309 static void pakfire_db_free(struct pakfire_db* db) {
310 if (db->handle) {
311 // Optimize the database before it is being closed
312 pakfire_db_optimize(db);
313
314 // Close database handle
315 int r = sqlite3_close(db->handle);
316 if (r != SQLITE_OK) {
317 ERROR(db->pakfire, "Could not close database handle: %s\n",
318 sqlite3_errmsg(db->handle));
319 }
320 }
321
322 pakfire_unref(db->pakfire);
323
324 free(db);
325 }
326
327 static int pakfire_db_get_schema(struct pakfire_db* db) {
328 sqlite3_value* value = pakfire_db_get(db, "schema");
329 if (!value)
330 return -1;
331
332 int schema = sqlite3_value_int64(value);
333 sqlite3_value_free(value);
334
335 DEBUG(db->pakfire, "Database has schema version %d\n", schema);
336
337 return schema;
338 }
339
340 static int pakfire_db_create_schema(struct pakfire_db* db) {
341 int r;
342
343 // Create settings table
344 r = pakfire_db_execute(db, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
345 if (r)
346 return 1;
347
348 // settings: Add a unique index on key
349 r = pakfire_db_execute(db, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
350 if (r)
351 return 1;
352
353 // Create packages table
354 r = pakfire_db_execute(db,
355 "CREATE TABLE IF NOT EXISTS packages("
356 "id INTEGER PRIMARY KEY, "
357 "name TEXT, "
358 "evr TEXT, "
359 "arch TEXT, "
360 "groups TEXT, "
361 "filename TEXT, "
362 "size INTEGER, "
363 "inst_size INTEGER, "
364 "digest_type INTEGER, "
365 "digest BLOB, "
366 "license TEXT, "
367 "summary TEXT, "
368 "description TEXT, "
369 "uuid TEXT, "
370 "vendor TEXT, "
371 "build_host TEXT, "
372 "build_time INTEGER, "
373 "installed INTEGER, "
374 "userinstalled INTEGER, "
375 "repository TEXT, "
376 "source_name TEXT, "
377 "source_evr TEXT, "
378 "source_arch TEXT, "
379 "distribution TEXT"
380 ")");
381 if (r)
382 return 1;
383
384 // packages: Create index to find package by name
385 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
386 if (r)
387 return 1;
388
389 // packages: Create unique index over UUID
390 r = pakfire_db_execute(db, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
391 if (r)
392 return 1;
393
394 // Create dependencies table
395 r = pakfire_db_execute(db,
396 "CREATE TABLE IF NOT EXISTS dependencies("
397 "pkg INTEGER, "
398 "type TEXT, "
399 "dependency TEXT, "
400 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
401 ")");
402 if (r)
403 return r;
404
405 // dependencies: Add index over packages
406 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
407 if (r)
408 return r;
409
410 // Create files table
411 r = pakfire_db_execute(db,
412 "CREATE TABLE IF NOT EXISTS files("
413 "id INTEGER PRIMARY KEY, "
414 "path TEXT, "
415 "pkg INTEGER, "
416 "size INTEGER, "
417 "config INTEGER, "
418 "datafile INTEGER, "
419 "mode INTEGER, "
420 "uname TEXT, "
421 "gname TEXT, "
422 "ctime INTEGER, "
423 "mtime INTEGER, "
424 "capabilities TEXT, "
425 "digest_sha2_512 BLOB, "
426 "digest_sha2_256 BLOB, "
427 "digest_blake2b512 BLOB, "
428 "digest_blake2s256 BLOB, "
429 "digest_sha3_512 BLOB, "
430 "digest_sha3_256 BLOB, "
431 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
432 ")");
433 if (r)
434 return 1;
435
436 // files: Add index over packages
437 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
438 if (r)
439 return 1;
440
441 // files: Add index over path
442 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS files_path_index ON files(path)");
443 if (r)
444 return 1;
445
446 // Create scriptlets table
447 r = pakfire_db_execute(db,
448 "CREATE TABLE IF NOT EXISTS scriptlets("
449 "id INTEGER PRIMARY KEY, "
450 "pkg INTEGER, "
451 "type TEXT, "
452 "scriptlet TEXT, "
453 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
454 ")");
455 if (r)
456 return 1;
457
458 // scriptlets: Add index over packages
459 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
460 if (r)
461 return 1;
462
463 const char* arch = pakfire_get_arch(db->pakfire);
464
465 // Set architecture
466 r = pakfire_db_set_string(db, "arch", arch);
467 if (r) {
468 ERROR(db->pakfire, "Could not set architecture\n");
469 return r;
470 }
471
472 return 0;
473 }
474
475 static int pakfire_db_migrate_to_schema_8(struct pakfire_db* db) {
476 // packages: Drop build_id column
477
478 // Add foreign keys
479 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
480 // need to recreate the whole table and rename it afterwards. Annoying.
481
482 return 0;
483 }
484
485 static int pakfire_db_migrate_schema(struct pakfire_db* db) {
486 int r;
487
488 while (db->schema < CURRENT_SCHEMA) {
489 // Begin a new transaction
490 r = pakfire_db_begin_transaction(db);
491 if (r)
492 goto ROLLBACK;
493
494 switch (db->schema) {
495 // No schema exists
496 case -1:
497 r = pakfire_db_create_schema(db);
498 if (r)
499 goto ROLLBACK;
500
501 db->schema = CURRENT_SCHEMA;
502 break;
503
504 case 7:
505 r = pakfire_db_migrate_to_schema_8(db);
506 if (r)
507 goto ROLLBACK;
508
509 db->schema++;
510 break;
511
512 default:
513 ERROR(db->pakfire, "Cannot migrate database from schema %d\n", db->schema);
514 goto ROLLBACK;
515 }
516
517 // Update the schema version
518 r = pakfire_db_set_int(db, "schema", CURRENT_SCHEMA);
519 if (r)
520 goto ROLLBACK;
521
522 // All done, commit!
523 r = pakfire_db_commit(db);
524 if (r)
525 goto ROLLBACK;
526 }
527
528 return 0;
529
530 ROLLBACK:
531 pakfire_db_rollback(db);
532
533 return 1;
534 }
535
536 static int pakfire_db_check_arch(struct pakfire_db* db) {
537 int r = 1;
538
539 // Fetch database architecture
540 char* db_arch = pakfire_db_get_string(db, "arch");
541 if (!db_arch) {
542 ERROR(db->pakfire, "Database is of an unknown architecture\n");
543 goto ERROR;
544 }
545
546 // Fetch the running architecture
547 const char* arch = pakfire_get_arch(db->pakfire);
548 if (!arch)
549 goto ERROR;
550
551 // They should match
552 if (strcmp(db_arch, arch) == 0)
553 r = 0;
554
555 ERROR:
556 if (db_arch)
557 free(db_arch);
558
559 return r;
560 }
561
562 static int pakfire_db_setup(struct pakfire_db* db) {
563 int r;
564
565 // Setup logging
566 sqlite3_config(SQLITE_CONFIG_LOG, logging_callback, db->pakfire);
567
568 // Enable foreign keys
569 pakfire_db_execute(db, "PRAGMA foreign_keys = ON");
570
571 // Make LIKE case-sensitive
572 pakfire_db_execute(db, "PRAGMA case_sensitive_like = ON");
573
574 // Fetch the current schema
575 db->schema = pakfire_db_get_schema(db);
576
577 // Check if the schema is recent enough
578 if (db->schema > 0 && db->schema < SCHEMA_MIN_SUP) {
579 ERROR(db->pakfire, "Database schema %d is not supported by this version of Pakfire\n",
580 db->schema);
581 return 1;
582 }
583
584 // Read modification timestamp
585 db->last_modified_at = pakfire_read_modification_time(db);
586
587 DEBUG(db->pakfire, "The database was last modified at %ld\n", db->last_modified_at);
588
589 // Done when not in read-write mode
590 if (db->mode != PAKFIRE_DB_READWRITE)
591 return 0;
592
593 // Disable secure delete
594 pakfire_db_execute(db, "PRAGMA secure_delete = OFF");
595
596 // Set database journal to WAL
597 r = pakfire_db_execute(db, "PRAGMA journal_mode = WAL");
598 if (r != SQLITE_OK) {
599 ERROR(db->pakfire, "Could not set journal mode to WAL: %s\n",
600 sqlite3_errmsg(db->handle));
601 return 1;
602 }
603
604 // Disable autocheckpoint
605 r = sqlite3_wal_autocheckpoint(db->handle, 0);
606 if (r != SQLITE_OK) {
607 ERROR(db->pakfire, "Could not disable autocheckpoint: %s\n",
608 sqlite3_errmsg(db->handle));
609 return 1;
610 }
611
612 // Create or migrate schema
613 r = pakfire_db_migrate_schema(db);
614 if (r)
615 return r;
616
617 return 0;
618 }
619
620 int pakfire_db_open(struct pakfire_db** db, struct pakfire* pakfire, int flags) {
621 int r = 1;
622
623 struct pakfire_db* o = calloc(1, sizeof(*o));
624 if (!o)
625 return -ENOMEM;
626
627 o->pakfire = pakfire_ref(pakfire);
628 o->nrefs = 1;
629
630 int sqlite3_flags = 0;
631
632 // Store mode & forward it to sqlite3
633 if (flags & PAKFIRE_DB_READWRITE) {
634 o->mode = PAKFIRE_DB_READWRITE;
635 sqlite3_flags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
636 } else {
637 o->mode = PAKFIRE_DB_READONLY;
638 sqlite3_flags |= SQLITE_OPEN_READONLY;
639 }
640
641 // Make the filename
642 r = pakfire_path(o->pakfire, o->path, "%s", DATABASE_PATH);
643 if (r)
644 goto END;
645
646 // Try to open the sqlite3 database file
647 r = sqlite3_open_v2(o->path, &o->handle, sqlite3_flags, NULL);
648 if (r != SQLITE_OK) {
649 ERROR(pakfire, "Could not open database %s: %s\n",
650 o->path, sqlite3_errmsg(o->handle));
651
652 r = 1;
653 goto END;
654 }
655
656 // Setup the database
657 r = pakfire_db_setup(o);
658 if (r)
659 goto END;
660
661 // Check for compatible architecture
662 r = pakfire_db_check_arch(o);
663 if (r)
664 goto END;
665
666 *db = o;
667 r = 0;
668
669 END:
670 if (r)
671 pakfire_db_free(o);
672
673 return r;
674 }
675
676 struct pakfire_db* pakfire_db_ref(struct pakfire_db* db) {
677 db->nrefs++;
678
679 return db;
680 }
681
682 struct pakfire_db* pakfire_db_unref(struct pakfire_db* db) {
683 if (--db->nrefs > 0)
684 return db;
685
686 pakfire_db_free(db);
687
688 return NULL;
689 }
690
691 static unsigned long pakfire_db_integrity_check(struct pakfire_db* db) {
692 sqlite3_stmt* stmt = NULL;
693 int r;
694
695 r = sqlite3_prepare_v2(db->handle, "PRAGMA integrity_check", -1, &stmt, NULL);
696 if (r) {
697 ERROR(db->pakfire, "Could not prepare integrity check: %s\n",
698 sqlite3_errmsg(db->handle));
699 return 1;
700 }
701
702 // Count any errors
703 unsigned long errors = 0;
704
705 while (1) {
706 do {
707 r = sqlite3_step(stmt);
708 } while (r == SQLITE_BUSY);
709
710 if (r == SQLITE_ROW) {
711 const char* error = (const char*)sqlite3_column_text(stmt, 0);
712
713 // If the message is "ok", the database has passed the check
714 if (strcmp(error, "ok") == 0)
715 continue;
716
717 // Increment error counter
718 errors++;
719
720 // Log the message
721 ERROR(db->pakfire, "%s\n", error);
722
723 // Break on anything else
724 } else
725 break;
726 }
727
728 sqlite3_finalize(stmt);
729
730 if (errors)
731 ERROR(db->pakfire, "Database integrity check failed\n");
732 else
733 INFO(db->pakfire, "Database integrity check passed\n");
734
735 return errors;
736 }
737
738 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db* db) {
739 sqlite3_stmt* stmt = NULL;
740 int r;
741
742 r = sqlite3_prepare_v2(db->handle, "PRAGMA foreign_key_check", -1, &stmt, NULL);
743 if (r) {
744 ERROR(db->pakfire, "Could not prepare foreign key check: %s\n",
745 sqlite3_errmsg(db->handle));
746 return 1;
747 }
748
749 // Count any errors
750 unsigned long errors = 0;
751
752 while (1) {
753 do {
754 r = sqlite3_step(stmt);
755 } while (r == SQLITE_BUSY);
756
757 if (r == SQLITE_ROW) {
758 const unsigned char* table = sqlite3_column_text(stmt, 0);
759 unsigned long rowid = sqlite3_column_int64(stmt, 1);
760 const unsigned char* foreign_table = sqlite3_column_text(stmt, 2);
761 unsigned long foreign_rowid = sqlite3_column_int64(stmt, 3);
762
763 // Increment error counter
764 errors++;
765
766 // Log the message
767 ERROR(db->pakfire, "Foreign key violation found in %s, row %lu: "
768 "%lu does not exist in table %s\n", table, rowid, foreign_rowid, foreign_table);
769
770 // Break on anything else
771 } else
772 break;
773 }
774
775 sqlite3_finalize(stmt);
776
777 if (errors)
778 ERROR(db->pakfire, "Foreign key check failed\n");
779 else
780 INFO(db->pakfire, "Foreign key check passed\n");
781
782 return errors;
783 }
784
785 /*
786 This function performs an integrity check of the database
787 */
788 int pakfire_db_check(struct pakfire_db* db) {
789 int r;
790
791 // Perform integrity check
792 r = pakfire_db_integrity_check(db);
793 if (r)
794 return 1;
795
796 // Perform foreign key check
797 r = pakfire_db_foreign_key_check(db);
798 if (r)
799 return 1;
800
801 return 0;
802 }
803
804 // Returns the number of packages installed
805 ssize_t pakfire_db_packages(struct pakfire_db* db) {
806 sqlite3_stmt* stmt = NULL;
807 ssize_t packages = -1;
808
809 int r = sqlite3_prepare_v2(db->handle, "SELECT COUNT(*) FROM packages", -1, &stmt, NULL);
810 if (r) {
811 ERROR(db->pakfire, "Could not prepare SQL statement: %s\n",
812 sqlite3_errmsg(db->handle));
813 return -1;
814 }
815
816 // Execute query
817 do {
818 r = sqlite3_step(stmt);
819 } while (r == SQLITE_BUSY);
820
821 if (r == SQLITE_ROW) {
822 packages = sqlite3_column_int64(stmt, 0);
823 }
824
825 sqlite3_finalize(stmt);
826
827 return packages;
828 }
829
830 static void pakfire_db_add_userinstalled(struct pakfire* pakfire, const char* name) {
831 Pool* pool = pakfire_get_solv_pool(pakfire);
832
833 // Convert name to ID
834 Id id = pool_str2id(pool, name, 1);
835
836 // Append it to pooljobs
837 queue_push2(&pool->pooljobs, SOLVER_USERINSTALLED|SOLVER_SOLVABLE_NAME, id);
838 }
839
840 static int pakfire_db_add_dependencies(struct pakfire_db* db, unsigned long id, struct pakfire_package* pkg) {
841 sqlite3_stmt* stmt = NULL;
842 int r = 1;
843
844 const char* sql = "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
845
846 // Prepare the statement
847 r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL);
848 if (r) {
849 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
850 sql, sqlite3_errmsg(db->handle));
851 goto END;
852 }
853
854 const struct __relation {
855 const char* type;
856 char** (*func)(struct pakfire_package*);
857 } relations[] = {
858 { "provides", pakfire_package_get_provides },
859 { "prerequires", pakfire_package_get_prerequires },
860 { "requires", pakfire_package_get_requires },
861 { "conflicts", pakfire_package_get_conflicts },
862 { "obsoletes", pakfire_package_get_obsoletes },
863 { "recommends", pakfire_package_get_recommends },
864 { "suggests", pakfire_package_get_suggests },
865 { "supplements", pakfire_package_get_supplements },
866 { "enhances", pakfire_package_get_enhances },
867 { NULL, NULL },
868 };
869
870 for (const struct __relation* relation = relations; relation->type; relation++) {
871 char** list = relation->func(pkg);
872 if (!list)
873 continue;
874
875 for (char** dep = list; *dep; dep++) {
876 // Bind package ID
877 r = sqlite3_bind_int64(stmt, 1, id);
878 if (r) {
879 ERROR(db->pakfire, "Could not bind id: %s\n",
880 sqlite3_errmsg(db->handle));
881 goto END;
882 }
883
884 // Bind type
885 r = sqlite3_bind_text(stmt, 2, relation->type, -1, NULL);
886 if (r) {
887 ERROR(db->pakfire, "Could not bind type: %s\n",
888 sqlite3_errmsg(db->handle));
889 goto END;
890 }
891
892 // Bind dependency
893 r = sqlite3_bind_text(stmt, 3, *dep, -1, NULL);
894 if (r) {
895 ERROR(db->pakfire, "Could not bind dependency: %s\n",
896 sqlite3_errmsg(db->handle));
897 goto END;
898 }
899
900 // Execute query
901 do {
902 r = sqlite3_step(stmt);
903 } while (r == SQLITE_BUSY);
904
905 free(*dep);
906
907 // Reset bound values
908 sqlite3_reset(stmt);
909 }
910
911 free(list);
912 }
913
914 // All okay
915 r = 0;
916
917 END:
918 if (stmt)
919 sqlite3_finalize(stmt);
920
921 return r;
922 }
923
924 static int pakfire_db_bind_digest(struct pakfire_db* db, sqlite3_stmt* stmt, const int field,
925 struct pakfire_file* file, const enum pakfire_digest_types type) {
926 const unsigned char* digest = NULL;
927 size_t length = 0;
928
929 // Fetch the digest
930 digest = pakfire_file_get_digest(file, type, &length);
931
932 // If this digest isn't set, just bind NULL
933 if (!digest)
934 return sqlite3_bind_null(stmt, field);
935
936 // Otherwise bind the data blob
937 return sqlite3_bind_blob(stmt, field, digest, length, NULL);
938 }
939
940 static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, struct pakfire_archive* archive) {
941 sqlite3_stmt* stmt = NULL;
942 int r = 1;
943
944 // Get the filelist from the archive
945 struct pakfire_filelist* filelist = pakfire_archive_get_filelist(archive);
946 if (!filelist) {
947 ERROR(db->pakfire, "Could not fetch filelist from archive\n");
948 return 1;
949 }
950
951 // Nothing to do if the list is empty
952 if (pakfire_filelist_is_empty(filelist)) {
953 r = 0;
954 goto END;
955 }
956
957 const char* sql =
958 "INSERT INTO "
959 " files("
960 "pkg, "
961 "path, "
962 "size, "
963 "config, "
964 "datafile, "
965 "mode, "
966 "uname, "
967 "gname, "
968 "ctime, "
969 "mtime, "
970 "capabilities, "
971 "digest_sha2_512, "
972 "digest_sha2_256, "
973 "digest_blake2b512, "
974 "digest_blake2s256, "
975 "digest_sha3_512, "
976 "digest_sha3_256"
977 ") "
978 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
979
980 // Prepare the statement
981 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
982 if (r) {
983 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
984 sql, sqlite3_errmsg(db->handle));
985 goto END;
986 }
987
988 for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) {
989 struct pakfire_file* file = pakfire_filelist_get(filelist, i);
990
991 // Bind package ID
992 r = sqlite3_bind_int64(stmt, 1, id);
993 if (r) {
994 ERROR(db->pakfire, "Could not bind id: %s\n", sqlite3_errmsg(db->handle));
995 pakfire_file_unref(file);
996 goto END;
997 }
998
999 // Bind name
1000 const char* path = pakfire_file_get_path(file);
1001
1002 r = sqlite3_bind_text(stmt, 2, path, -1, NULL);
1003 if (r) {
1004 ERROR(db->pakfire, "Could not bind path: %s\n", sqlite3_errmsg(db->handle));
1005 pakfire_file_unref(file);
1006 goto END;
1007 }
1008
1009 // Bind size
1010 size_t size = pakfire_file_get_size(file);
1011
1012 r = sqlite3_bind_int64(stmt, 3, size);
1013 if (r) {
1014 ERROR(db->pakfire, "Could not bind size: %s\n", sqlite3_errmsg(db->handle));
1015 pakfire_file_unref(file);
1016 goto END;
1017 }
1018
1019 // Bind config - XXX TODO
1020 r = sqlite3_bind_null(stmt, 4);
1021 if (r) {
1022 ERROR(db->pakfire, "Could not bind config: %s\n", sqlite3_errmsg(db->handle));
1023 pakfire_file_unref(file);
1024 goto END;
1025 }
1026
1027 // Bind datafile - XXX TODO
1028 r = sqlite3_bind_null(stmt, 5);
1029 if (r) {
1030 ERROR(db->pakfire, "Could not bind datafile: %s\n", sqlite3_errmsg(db->handle));
1031 pakfire_file_unref(file);
1032 goto END;
1033 }
1034
1035 // Bind mode
1036 mode_t mode = pakfire_file_get_mode(file);
1037
1038 r = sqlite3_bind_int64(stmt, 6, mode);
1039 if (r) {
1040 ERROR(db->pakfire, "Could not bind mode: %s\n", sqlite3_errmsg(db->handle));
1041 pakfire_file_unref(file);
1042 goto END;
1043 }
1044
1045 // Bind uname
1046 const char* uname = pakfire_file_get_uname(file);
1047
1048 r = sqlite3_bind_text(stmt, 7, uname, -1, NULL);
1049 if (r) {
1050 ERROR(db->pakfire, "Could not bind uname: %s\n", sqlite3_errmsg(db->handle));
1051 pakfire_file_unref(file);
1052 goto END;
1053 }
1054
1055 // Bind gname
1056 const char* gname = pakfire_file_get_gname(file);
1057
1058 r = sqlite3_bind_text(stmt, 8, gname, -1, NULL);
1059 if (r) {
1060 ERROR(db->pakfire, "Could not bind gname: %s\n", sqlite3_errmsg(db->handle));
1061 pakfire_file_unref(file);
1062 goto END;
1063 }
1064
1065 // Bind ctime
1066 time_t ctime = pakfire_file_get_ctime(file);
1067
1068 r = sqlite3_bind_int64(stmt, 9, ctime);
1069 if (r) {
1070 ERROR(db->pakfire, "Could not bind ctime: %s\n", sqlite3_errmsg(db->handle));
1071 pakfire_file_unref(file);
1072 goto END;
1073 }
1074
1075 // Bind mtime
1076 time_t mtime = pakfire_file_get_mtime(file);
1077
1078 r = sqlite3_bind_int64(stmt, 10, mtime);
1079 if (r) {
1080 ERROR(db->pakfire, "Could not bind mtime: %s\n", sqlite3_errmsg(db->handle));
1081 pakfire_file_unref(file);
1082 goto END;
1083 }
1084
1085 // Bind capabilities - XXX TODO
1086 r = sqlite3_bind_null(stmt, 11);
1087 if (r) {
1088 ERROR(db->pakfire, "Could not bind capabilities: %s\n", sqlite3_errmsg(db->handle));
1089 pakfire_file_unref(file);
1090 goto END;
1091 }
1092
1093 // SHA2-512 Digest
1094 r = pakfire_db_bind_digest(db, stmt, 12, file, PAKFIRE_DIGEST_SHA2_512);
1095 if (r) {
1096 ERROR(db->pakfire, "Could not bind SHA2-512 digest: %s\n",
1097 sqlite3_errmsg(db->handle));
1098 pakfire_file_unref(file);
1099 goto END;
1100 }
1101
1102 // SHA2-256 Digest
1103 r = pakfire_db_bind_digest(db, stmt, 13, file, PAKFIRE_DIGEST_SHA2_256);
1104 if (r) {
1105 ERROR(db->pakfire, "Could not bind SHA2-256 digest: %s\n",
1106 sqlite3_errmsg(db->handle));
1107 pakfire_file_unref(file);
1108 goto END;
1109 }
1110
1111 // BLAKE2b512 Digest
1112 r = pakfire_db_bind_digest(db, stmt, 14, file, PAKFIRE_DIGEST_BLAKE2B512);
1113 if (r) {
1114 ERROR(db->pakfire, "Could not bind BLAKE2b512 digest: %s\n",
1115 sqlite3_errmsg(db->handle));
1116 pakfire_file_unref(file);
1117 goto END;
1118 }
1119
1120 // BLAKE2s256 Digest
1121 r = pakfire_db_bind_digest(db, stmt, 15, file, PAKFIRE_DIGEST_BLAKE2S256);
1122 if (r) {
1123 ERROR(db->pakfire, "Could not bind BLAKE2s256 digest: %s\n",
1124 sqlite3_errmsg(db->handle));
1125 pakfire_file_unref(file);
1126 goto END;
1127 }
1128
1129 // SHA3-512 Digest
1130 r = pakfire_db_bind_digest(db, stmt, 16, file, PAKFIRE_DIGEST_SHA3_512);
1131 if (r) {
1132 ERROR(db->pakfire, "Could not bind SHA3-512 digest: %s\n",
1133 sqlite3_errmsg(db->handle));
1134 pakfire_file_unref(file);
1135 goto END;
1136 }
1137
1138 // SHA3-256 Digest
1139 r = pakfire_db_bind_digest(db, stmt, 17, file, PAKFIRE_DIGEST_SHA3_256);
1140 if (r) {
1141 ERROR(db->pakfire, "Could not bind SHA3-256 digest: %s\n",
1142 sqlite3_errmsg(db->handle));
1143 pakfire_file_unref(file);
1144 goto END;
1145 }
1146
1147 // Execute query
1148 do {
1149 r = sqlite3_step(stmt);
1150 } while (r == SQLITE_BUSY);
1151
1152 // Check for errors
1153 if (r != SQLITE_DONE) {
1154 ERROR(db->pakfire, "Could not add file to database: %s\n",
1155 sqlite3_errmsg(db->handle));
1156 pakfire_file_unref(file);
1157 goto END;
1158 }
1159
1160 // Move on to next file
1161 pakfire_file_unref(file);
1162
1163 // Reset bound values
1164 sqlite3_reset(stmt);
1165 }
1166
1167 // All okay
1168 r = 0;
1169
1170 END:
1171 if (stmt)
1172 sqlite3_finalize(stmt);
1173 pakfire_filelist_unref(filelist);
1174
1175 return r;
1176 }
1177
1178 static int pakfire_db_add_scriptlets(struct pakfire_db* db, unsigned long id, struct pakfire_archive* archive) {
1179 sqlite3_stmt* stmt = NULL;
1180 size_t size;
1181 int r = 1;
1182
1183 const char* sql = "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
1184
1185 // Prepare the statement
1186 r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL);
1187 if (r) {
1188 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
1189 sql, sqlite3_errmsg(db->handle));
1190 goto END;
1191 }
1192
1193 for (const char** type = pakfire_scriptlet_types; *type; type++) {
1194 // Fetch the scriptlet
1195 struct pakfire_scriptlet* scriptlet = pakfire_archive_get_scriptlet(archive, *type);
1196 if (!scriptlet)
1197 continue;
1198
1199 // Bind package ID
1200 r = sqlite3_bind_int64(stmt, 1, id);
1201 if (r) {
1202 ERROR(db->pakfire, "Could not bind id: %s\n", sqlite3_errmsg(db->handle));
1203 pakfire_scriptlet_unref(scriptlet);
1204 goto END;
1205 }
1206
1207 // Bind handle
1208 r = sqlite3_bind_text(stmt, 2, *type, -1, NULL);
1209 if (r) {
1210 ERROR(db->pakfire, "Could not bind type: %s\n", sqlite3_errmsg(db->handle));
1211 pakfire_scriptlet_unref(scriptlet);
1212 goto END;
1213 }
1214
1215 const char* data = pakfire_scriptlet_get_data(scriptlet, &size);
1216
1217 // Bind scriptlet
1218 r = sqlite3_bind_text(stmt, 3, data, size, NULL);
1219 if (r) {
1220 ERROR(db->pakfire, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db->handle));
1221 pakfire_scriptlet_unref(scriptlet);
1222 goto END;
1223 }
1224
1225 // Execute query
1226 do {
1227 r = sqlite3_step(stmt);
1228 } while (r == SQLITE_BUSY);
1229
1230 // Check for errors
1231 if (r != SQLITE_DONE) {
1232 ERROR(db->pakfire, "Could not add scriptlet to database: %s\n",
1233 sqlite3_errmsg(db->handle));
1234 pakfire_scriptlet_unref(scriptlet);
1235 goto END;
1236 }
1237
1238 pakfire_scriptlet_unref(scriptlet);
1239
1240 // Reset bound values
1241 sqlite3_reset(stmt);
1242 }
1243
1244 // All okay
1245 r = 0;
1246
1247 END:
1248 if (stmt)
1249 sqlite3_finalize(stmt);
1250
1251 return r;
1252 }
1253
1254 int pakfire_db_add_package(struct pakfire_db* db,
1255 struct pakfire_package* pkg, struct pakfire_archive* archive, int userinstalled) {
1256 sqlite3_stmt* stmt = NULL;
1257 int r;
1258
1259 // Begin a new transaction
1260 r = pakfire_db_begin_transaction(db);
1261 if (r)
1262 goto ERROR;
1263
1264 const char* sql = "INSERT INTO "
1265 "packages("
1266 "name, "
1267 "evr, "
1268 "arch, "
1269 "groups, "
1270 "filename, "
1271 "size, "
1272 "inst_size, "
1273 "digest_type, "
1274 "digest, "
1275 "license, "
1276 "summary, "
1277 "description, "
1278 "uuid, "
1279 "vendor, "
1280 "build_host, "
1281 "build_time, "
1282 "installed, "
1283 "repository, "
1284 "userinstalled, "
1285 "source_name, "
1286 "source_evr, "
1287 "source_arch, "
1288 "distribution"
1289 ") VALUES("
1290 "?, "
1291 "?, "
1292 "?, "
1293 "?, "
1294 "?, "
1295 "?, "
1296 "?, "
1297 "?, "
1298 "?, "
1299 "?, "
1300 "?, "
1301 "?, "
1302 "?, "
1303 "?, "
1304 "?, "
1305 "?, "
1306 "CURRENT_TIMESTAMP, "
1307 "?, "
1308 "?, "
1309 "?, "
1310 "?, "
1311 "?, "
1312 "?"
1313 ")";
1314
1315 // Prepare the statement
1316 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
1317 if (r != SQLITE_OK) {
1318 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
1319 sql, sqlite3_errmsg(db->handle));
1320 goto ERROR;
1321 }
1322
1323 // Bind name
1324 const char* name = pakfire_package_get_name(pkg);
1325
1326 r = sqlite3_bind_text(stmt, 1, name, -1, NULL);
1327 if (r) {
1328 ERROR(db->pakfire, "Could not bind name: %s\n", sqlite3_errmsg(db->handle));
1329 goto ERROR;
1330 }
1331
1332 // Bind evr
1333 const char* evr = pakfire_package_get_evr(pkg);
1334
1335 r = sqlite3_bind_text(stmt, 2, evr, -1, NULL);
1336 if (r) {
1337 ERROR(db->pakfire, "Could not bind evr: %s\n", sqlite3_errmsg(db->handle));
1338 goto ERROR;
1339 }
1340
1341 // Bind arch
1342 const char* arch = pakfire_package_get_arch(pkg);
1343
1344 r = sqlite3_bind_text(stmt, 3, arch, -1, NULL);
1345 if (r) {
1346 ERROR(db->pakfire, "Could not bind arch: %s\n", sqlite3_errmsg(db->handle));
1347 goto ERROR;
1348 }
1349
1350 // Bind groups
1351 char* groups = pakfire_package_get_groups(pkg);
1352 if (groups) {
1353 r = sqlite3_bind_text(stmt, 4, groups, -1, NULL);
1354 if (r) {
1355 ERROR(db->pakfire, "Could not bind groups: %s\n", sqlite3_errmsg(db->handle));
1356 free(groups);
1357 goto ERROR;
1358 }
1359
1360 free(groups);
1361
1362 // No groups
1363 } else {
1364 r = sqlite3_bind_null(stmt, 4);
1365 if (r)
1366 goto ERROR;
1367 }
1368
1369 // Bind filename
1370 const char* filename = pakfire_package_get_filename(pkg);
1371
1372 r = sqlite3_bind_text(stmt, 5, filename, -1, NULL);
1373 if (r) {
1374 ERROR(db->pakfire, "Could not bind filename: %s\n", sqlite3_errmsg(db->handle));
1375 goto ERROR;
1376 }
1377
1378 // Bind size
1379 unsigned long long size = pakfire_package_get_downloadsize(pkg);
1380
1381 r = sqlite3_bind_int64(stmt, 6, size);
1382 if (r) {
1383 ERROR(db->pakfire, "Could not bind size: %s\n", sqlite3_errmsg(db->handle));
1384 goto ERROR;
1385 }
1386
1387 // Bind installed size
1388 unsigned long long inst_size = pakfire_package_get_installsize(pkg);
1389
1390 r = sqlite3_bind_int64(stmt, 7, inst_size);
1391 if (r) {
1392 ERROR(db->pakfire, "Could not bind inst_size: %s\n", sqlite3_errmsg(db->handle));
1393 goto ERROR;
1394 }
1395
1396 const unsigned char* digest = NULL;
1397 enum pakfire_digest_types digest_type = 0;
1398 size_t digest_length = 0;
1399
1400 // Fetch the digest
1401 digest = pakfire_package_get_digest(pkg, &digest_type, &digest_length);
1402 if (!digest) {
1403 ERROR(db->pakfire, "Could not fetch the package's digest: %m\n");
1404 r = 1;
1405 goto ERROR;
1406 }
1407
1408 // Set the digest type
1409 r = sqlite3_bind_int64(stmt, 8, digest_type);
1410 if (r) {
1411 ERROR(db->pakfire, "Could not bind digest type: %s\n", sqlite3_errmsg(db->handle));
1412 goto ERROR;
1413 }
1414
1415 // Set the digest
1416 r = sqlite3_bind_blob(stmt, 9, digest, digest_length, NULL);
1417 if (r) {
1418 ERROR(db->pakfire, "Could not bind digest: %s\n", sqlite3_errmsg(db->handle));
1419 goto ERROR;
1420 }
1421
1422 // Bind license
1423 const char* license = pakfire_package_get_license(pkg);
1424
1425 r = sqlite3_bind_text(stmt, 10, license, -1, NULL);
1426 if (r) {
1427 ERROR(db->pakfire, "Could not bind license: %s\n", sqlite3_errmsg(db->handle));
1428 goto ERROR;
1429 }
1430
1431 // Bind summary
1432 const char* summary = pakfire_package_get_summary(pkg);
1433
1434 r = sqlite3_bind_text(stmt, 11, summary, -1, NULL);
1435 if (r) {
1436 ERROR(db->pakfire, "Could not bind summary: %s\n", sqlite3_errmsg(db->handle));
1437 goto ERROR;
1438 }
1439
1440 // Bind description
1441 const char* description = pakfire_package_get_description(pkg);
1442
1443 r = sqlite3_bind_text(stmt, 12, description, -1, NULL);
1444 if (r) {
1445 ERROR(db->pakfire, "Could not bind description: %s\n", sqlite3_errmsg(db->handle));
1446 goto ERROR;
1447 }
1448
1449 // Bind uuid
1450 const char* uuid = pakfire_package_get_uuid(pkg);
1451
1452 r = sqlite3_bind_text(stmt, 13, uuid, -1, NULL);
1453 if (r) {
1454 ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle));
1455 goto ERROR;
1456 }
1457
1458 // Bind vendor
1459 const char* vendor = pakfire_package_get_vendor(pkg);
1460
1461 r = sqlite3_bind_text(stmt, 14, vendor, -1, NULL);
1462 if (r) {
1463 ERROR(db->pakfire, "Could not bind vendor: %s\n", sqlite3_errmsg(db->handle));
1464 goto ERROR;
1465 }
1466
1467 // Bind build_host
1468 const char* build_host = pakfire_package_get_build_host(pkg);
1469
1470 r = sqlite3_bind_text(stmt, 15, build_host, -1, NULL);
1471 if (r) {
1472 ERROR(db->pakfire, "Could not bind build_host: %s\n", sqlite3_errmsg(db->handle));
1473 goto ERROR;
1474 }
1475
1476 // Bind build_time
1477 time_t build_time = pakfire_package_get_build_time(pkg);
1478
1479 r = sqlite3_bind_int64(stmt, 16, build_time);
1480 if (r) {
1481 ERROR(db->pakfire, "Could not bind build_time: %s\n", sqlite3_errmsg(db->handle));
1482 goto ERROR;
1483 }
1484
1485 // Bind repository name
1486 struct pakfire_repo* repo = pakfire_package_get_repo(pkg);
1487 if (repo) {
1488 const char* repo_name = pakfire_repo_get_name(repo);
1489 pakfire_repo_unref(repo);
1490
1491 r = sqlite3_bind_text(stmt, 17, repo_name, -1, NULL);
1492 if (r)
1493 goto ERROR;
1494
1495 // No repository?
1496 } else {
1497 r = sqlite3_bind_null(stmt, 17);
1498 if (r)
1499 goto ERROR;
1500 }
1501
1502 // installed by the user?
1503 r = sqlite3_bind_int(stmt, 18, userinstalled);
1504 if (r) {
1505 ERROR(db->pakfire, "Could not bind userinstalled: %s\n", sqlite3_errmsg(db->handle));
1506 goto ERROR;
1507 }
1508
1509 // Source package name
1510 const char* source_name = pakfire_package_get_source_name(pkg);
1511 if (source_name) {
1512 r = sqlite3_bind_text(stmt, 19, source_name, -1, NULL);
1513 if (r)
1514 goto ERROR;
1515 } else {
1516 r = sqlite3_bind_null(stmt, 19);
1517 if (r)
1518 goto ERROR;
1519 }
1520
1521 // Source EVR
1522 const char* source_evr = pakfire_package_get_source_evr(pkg);
1523 if (source_evr) {
1524 r = sqlite3_bind_text(stmt, 20, source_evr, -1, NULL);
1525 if (r)
1526 goto ERROR;
1527 } else {
1528 r = sqlite3_bind_null(stmt, 20);
1529 if (r)
1530 goto ERROR;
1531 }
1532
1533 // Source arch
1534 const char* source_arch = pakfire_package_get_source_arch(pkg);
1535 if (source_arch) {
1536 r = sqlite3_bind_text(stmt, 21, source_arch, -1, NULL);
1537 if (r)
1538 goto ERROR;
1539 } else {
1540 r = sqlite3_bind_null(stmt, 21);
1541 if (r)
1542 goto ERROR;
1543 }
1544
1545 // Distribution
1546 const char* distribution = pakfire_package_get_distribution(pkg);
1547 if (distribution) {
1548 r = sqlite3_bind_text(stmt, 22, distribution, -1, NULL);
1549 if (r)
1550 goto ERROR;
1551 } else {
1552 r = sqlite3_bind_null(stmt, 22);
1553 if (r)
1554 goto ERROR;
1555 }
1556
1557 // Run query
1558 do {
1559 r = sqlite3_step(stmt);
1560 } while (r == SQLITE_BUSY);
1561
1562 if (r != SQLITE_DONE) {
1563 ERROR(db->pakfire, "Could not add package to database: %s\n",
1564 sqlite3_errmsg(db->handle));
1565 r = 1;
1566 goto ERROR;
1567 }
1568
1569 // Save package ID
1570 unsigned long packages_id = sqlite3_last_insert_rowid(db->handle);
1571
1572 // Add dependencies
1573 r = pakfire_db_add_dependencies(db, packages_id, pkg);
1574 if (r)
1575 goto ERROR;
1576
1577 // Add files
1578 r = pakfire_db_add_files(db, packages_id, archive);
1579 if (r)
1580 goto ERROR;
1581
1582 // Add scriptlets
1583 r = pakfire_db_add_scriptlets(db, packages_id, archive);
1584 if (r)
1585 goto ERROR;
1586
1587 ERROR:
1588 if (stmt)
1589 sqlite3_finalize(stmt);
1590
1591 // Commit or rollback
1592 if (r)
1593 pakfire_db_rollback(db);
1594 else
1595 r = pakfire_db_commit(db);
1596
1597 return r;
1598 }
1599
1600 int pakfire_db_remove_package(struct pakfire_db* db, struct pakfire_package* pkg) {
1601 sqlite3_stmt* stmt = NULL;
1602 int r = 1;
1603
1604 // Begin a new transaction
1605 r = pakfire_db_begin_transaction(db);
1606 if (r)
1607 goto ERROR;
1608
1609 // Fetch the package's UUID
1610 const char* uuid = pakfire_package_get_uuid(pkg);
1611 if (!uuid) {
1612 ERROR(db->pakfire, "Package has no UUID\n");
1613 r = 1;
1614 goto ERROR;
1615 }
1616
1617 r = sqlite3_prepare_v2(db->handle,
1618 "DELETE FROM packages WHERE uuid = ?", -1, &stmt, NULL);
1619 if (r) {
1620 ERROR(db->pakfire, "Could not prepare SQL statement: %s\n",
1621 sqlite3_errmsg(db->handle));
1622 goto ERROR;
1623 }
1624
1625 // Bind UUID
1626 r = sqlite3_bind_text(stmt, 1, uuid, -1, NULL);
1627 if (r) {
1628 ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle));
1629 goto ERROR;
1630 }
1631
1632 // Execute query
1633 do {
1634 r = sqlite3_step(stmt);
1635 } while (r == SQLITE_BUSY);
1636
1637 // Check if we have been successful
1638 if (r != SQLITE_DONE) {
1639 ERROR(db->pakfire, "Could not delete package %s\n", uuid);
1640 r = 1;
1641 goto ERROR;
1642 }
1643
1644 // All done
1645 r = 0;
1646
1647 ERROR:
1648 if (stmt)
1649 sqlite3_finalize(stmt);
1650
1651 // Commit or rollback
1652 if (r)
1653 pakfire_db_rollback(db);
1654 else
1655 r = pakfire_db_commit(db);
1656
1657 return r;
1658 }
1659
1660 struct pakfire_scriptlet* pakfire_db_get_scriptlet(struct pakfire_db* db,
1661 struct pakfire_package* pkg, const char* type) {
1662 struct pakfire_scriptlet* scriptlet = NULL;
1663 sqlite3_stmt* stmt = NULL;
1664 int r = 1;
1665
1666 // Fetch the package's UUID
1667 const char* uuid = pakfire_package_get_uuid(pkg);
1668 if (!uuid) {
1669 ERROR(db->pakfire, "Package has no UUID\n");
1670 goto ERROR;
1671 }
1672
1673 const char* sql = "SELECT scriptlets.scriptlet FROM packages \
1674 JOIN scriptlets ON packages.id = scriptlets.pkg \
1675 WHERE packages.uuid = ? AND scriptlets.type = ?";
1676
1677 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
1678 if (r) {
1679 ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
1680 sql, sqlite3_errmsg(db->handle));
1681 goto ERROR;
1682 }
1683
1684 // Bind UUID
1685 r = sqlite3_bind_text(stmt, 1, uuid, -1, NULL);
1686 if (r) {
1687 ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle));
1688 goto ERROR;
1689 }
1690
1691 r = sqlite3_bind_text(stmt, 2, type, -1, NULL);
1692 if (r) {
1693 ERROR(db->pakfire, "Could not bind type: %s\n", sqlite3_errmsg(db->handle));
1694 goto ERROR;
1695 }
1696
1697 DEBUG(db->pakfire, "Searching for scriptlet for package %s of type %s\n", uuid, type);
1698
1699 // Execute query
1700 do {
1701 r = sqlite3_step(stmt);
1702 } while (r == SQLITE_BUSY);
1703
1704 // We have some payload
1705 if (r == SQLITE_ROW) {
1706 const void* data = sqlite3_column_blob(stmt, 1);
1707 ssize_t size = sqlite3_column_bytes(stmt, 1);
1708
1709 // Create a scriptlet object
1710 r = pakfire_scriptlet_create(&scriptlet, db->pakfire, type, data, size);
1711 if (r)
1712 goto ERROR;
1713 }
1714
1715 ERROR:
1716 if (stmt)
1717 sqlite3_finalize(stmt);
1718
1719 return scriptlet;
1720 }
1721
1722 static int pakfire_db_load_package(struct pakfire_db* db, struct pakfire_repo* repo, sqlite3_stmt* stmt) {
1723 struct pakfire_package* pkg = NULL;
1724 int r = 1;
1725
1726 // Name
1727 const char* name = (const char*)sqlite3_column_text(stmt, 0);
1728 if (!name) {
1729 ERROR(db->pakfire, "Could not read name: %s\n", sqlite3_errmsg(db->handle));
1730 goto ERROR;
1731 }
1732
1733 // EVR
1734 const char* evr = (const char*)sqlite3_column_text(stmt, 1);
1735 if (!evr) {
1736 ERROR(db->pakfire, "Could not read evr: %s\n", sqlite3_errmsg(db->handle));
1737 goto ERROR;
1738 }
1739
1740 // Arch
1741 const char* arch = (const char*)sqlite3_column_text(stmt, 2);
1742 if (!arch) {
1743 ERROR(db->pakfire, "Could not read arch: %s\n", sqlite3_errmsg(db->handle));
1744 goto ERROR;
1745 }
1746
1747 // Create package
1748 pkg = pakfire_package_create(db->pakfire, repo, name, evr, arch);
1749 if (!pkg) {
1750 ERROR(db->pakfire, "Could not create package\n");
1751 goto ERROR;
1752 }
1753
1754 // ID
1755 uint64_t id = sqlite3_column_int64(stmt, 3);
1756 if (id)
1757 pakfire_package_set_dbid(pkg, id);
1758
1759 // Groups
1760 const char* groups = (const char*)sqlite3_column_text(stmt, 4);
1761 if (groups) {
1762 pakfire_package_set_groups(pkg, groups);
1763 }
1764
1765 // Filename
1766 const char* filename = (const char*)sqlite3_column_text(stmt, 5);
1767 if (filename) {
1768 pakfire_package_set_filename(pkg, filename);
1769 }
1770
1771 // Size
1772 size_t size = sqlite3_column_int64(stmt, 6);
1773 if (size) {
1774 pakfire_package_set_downloadsize(pkg, size);
1775 }
1776
1777 // Installed size
1778 size = sqlite3_column_int64(stmt, 7);
1779 if (size) {
1780 pakfire_package_set_installsize(pkg, size);
1781 }
1782
1783 // Digest type
1784 enum pakfire_digest_types digest_type = sqlite3_column_int64(stmt, 8);
1785 size_t digest_length = 0;
1786
1787 // Digest length
1788
1789 // Digest
1790 const unsigned char* digest = sqlite3_column_blob(stmt, 9);
1791 if (digest_type && digest) {
1792 pakfire_package_set_digest(pkg, digest_type, digest, digest_length);
1793 }
1794
1795 // License
1796 const char* license = (const char*)sqlite3_column_text(stmt, 10);
1797 if (license) {
1798 pakfire_package_set_license(pkg, license);
1799 }
1800
1801 // Summary
1802 const char* summary = (const char*)sqlite3_column_text(stmt, 11);
1803 if (summary) {
1804 pakfire_package_set_summary(pkg, summary);
1805 }
1806
1807 // Description
1808 const char* description = (const char*)sqlite3_column_text(stmt, 12);
1809 if (description) {
1810 pakfire_package_set_description(pkg, description);
1811 }
1812
1813 // UUID
1814 const char* uuid = (const char*)sqlite3_column_text(stmt, 13);
1815 if (uuid) {
1816 pakfire_package_set_uuid(pkg, uuid);
1817 }
1818
1819 // Vendor
1820 const char* vendor = (const char*)sqlite3_column_text(stmt, 14);
1821 if (vendor) {
1822 pakfire_package_set_vendor(pkg, vendor);
1823 }
1824
1825 // Build Host
1826 const char* build_host = (const char*)sqlite3_column_text(stmt, 15);
1827 if (build_host) {
1828 pakfire_package_set_build_host(pkg, build_host);
1829 }
1830
1831 // Build Time
1832 time_t build_time = sqlite3_column_int64(stmt, 16);
1833 if (build_time) {
1834 pakfire_package_set_build_time(pkg, build_time);
1835 }
1836
1837 // Install Time
1838 time_t install_time = sqlite3_column_int64(stmt, 17);
1839 if (install_time) {
1840 pakfire_package_set_install_time(pkg, install_time);
1841 }
1842
1843 // installed by user?
1844 int userinstalled = sqlite3_column_int(stmt, 18);
1845 if (userinstalled)
1846 pakfire_db_add_userinstalled(db->pakfire, name);
1847
1848 // Files
1849 const char* files = (const char*)sqlite3_column_text(stmt, 19);
1850 if (files) {
1851 r = pakfire_package_set_filelist_from_string(pkg, files);
1852 if (r)
1853 goto ERROR;
1854 }
1855
1856 // Dependencies
1857
1858 const struct dependency {
1859 unsigned int field;
1860 void (*func)(struct pakfire_package* pkg, const char* dep);
1861 } dependencies[] = {
1862 { 20, pakfire_package_add_provides },
1863 { 21, pakfire_package_add_prerequires },
1864 { 22, pakfire_package_add_requires },
1865 { 23, pakfire_package_add_conflicts },
1866 { 24, pakfire_package_add_obsoletes },
1867 { 25, pakfire_package_add_recommends },
1868 { 26, pakfire_package_add_suggests },
1869 { 27, pakfire_package_add_supplements },
1870 { 28, pakfire_package_add_enhances },
1871 { 0, NULL },
1872 };
1873
1874 for (const struct dependency* deps = dependencies; deps->field; deps++) {
1875 const char* relations = (const char*)sqlite3_column_text(stmt, deps->field);
1876 if (relations) {
1877 pakfire_str2deps(db->pakfire, pkg, deps->func, relations);
1878 }
1879 }
1880
1881 // Source package
1882 const char* source_name = (const char*)sqlite3_column_text(stmt, 29);
1883 if (source_name)
1884 pakfire_package_set_source_name(pkg, source_name);
1885
1886 // Source EVR
1887 const char* source_evr = (const char*)sqlite3_column_text(stmt, 30);
1888 if (source_evr)
1889 pakfire_package_set_source_evr(pkg, source_evr);
1890
1891 // Source arch
1892 const char* source_arch = (const char*)sqlite3_column_text(stmt, 31);
1893 if (source_arch)
1894 pakfire_package_set_source_arch(pkg, source_arch);
1895
1896 // Distribution
1897 const char* distribution = (const char*)sqlite3_column_text(stmt, 32);
1898 if (distribution)
1899 pakfire_package_set_distribution(pkg, distribution);
1900
1901 // Success
1902 r = 0;
1903
1904 ERROR:
1905 if (pkg)
1906 pakfire_package_unref(pkg);
1907
1908 return r;
1909 }
1910
1911 int pakfire_db_load(struct pakfire_db* db, struct pakfire_repo* repo) {
1912 sqlite3_stmt* stmt = NULL;
1913 int r = 1;
1914
1915 DEBUG(db->pakfire, "Loading package database...\n");
1916
1917 // Drop contents of the repository
1918 pakfire_repo_clear(repo);
1919
1920 // Save starting time
1921 clock_t t_start = clock();
1922 clock_t t_end;
1923
1924 const char* sql =
1925 "SELECT "
1926 "name, "
1927 "evr, "
1928 "arch, "
1929 "id, "
1930 "groups, "
1931 "filename, "
1932 "size, "
1933 "inst_size, "
1934 "digest_type, "
1935 "digest, "
1936 "license, "
1937 "summary, "
1938 "description, "
1939 "uuid, "
1940 "vendor, "
1941 "build_host, "
1942 "build_time, "
1943 "strftime('%s', installed) AS installed, "
1944 "userinstalled, "
1945 "("
1946 "SELECT group_concat(path, '\n') FROM files WHERE files.pkg = packages.id"
1947 ") AS files, "
1948 "("
1949 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1950 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1951 ") AS provides, "
1952 "("
1953 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1954 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1955 ") AS prerequires, "
1956 "("
1957 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1958 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1959 ") AS requires, "
1960 "("
1961 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1962 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1963 ") AS conflicts, "
1964 "("
1965 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1966 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1967 ") AS obsoletes, "
1968 "("
1969 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1970 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1971 ") AS recommends, "
1972 "("
1973 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1974 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1975 ") AS suggests, "
1976 "("
1977 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1978 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1979 ") AS supplements, "
1980 "("
1981 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1982 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1983 ") AS enhances, "
1984 "source_name, "
1985 "source_evr, "
1986 "source_arch, "
1987 "distribution "
1988 "FROM "
1989 "packages"
1990 ";";
1991
1992 // Prepare the statement
1993 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
1994 if (r) {
1995 ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
1996 sql, sqlite3_errmsg(db->handle));
1997 goto ERROR;
1998 }
1999
2000 for (;;) {
2001 // Execute query
2002 r = sqlite3_step(stmt);
2003
2004 switch (r) {
2005 // Retry if the database was busy
2006 case SQLITE_BUSY:
2007 continue;
2008
2009 // Read a row
2010 case SQLITE_ROW:
2011 r = pakfire_db_load_package(db, repo, stmt);
2012 if (r)
2013 goto ERROR;
2014 break;
2015
2016 // All rows have been processed
2017 case SQLITE_DONE:
2018 goto END;
2019
2020 // Go to error in any other cases
2021 default:
2022 goto ERROR;
2023 }
2024 }
2025
2026 END:
2027 // Save time when we finished
2028 t_end = clock();
2029
2030 DEBUG(db->pakfire, "Loading package database completed in %.4fms\n",
2031 (double)(t_end - t_start) * 1000 / CLOCKS_PER_SEC);
2032
2033 // Mark repository as changed
2034 pakfire_repo_has_changed(repo);
2035
2036 // All done
2037 r = 0;
2038
2039 ERROR:
2040 if (r) {
2041 ERROR(db->pakfire, "Failed reading package database: %d\n", r);
2042 pakfire_repo_clear(repo);
2043 }
2044
2045 if (stmt)
2046 sqlite3_finalize(stmt);
2047
2048 return r;
2049 }
2050
2051 static int pakfire_db_load_file_digest(struct pakfire_db* db, struct pakfire_file* file,
2052 sqlite3_stmt* stmt, const enum pakfire_digest_types type, const int field) {
2053 // Fetch digest
2054 const unsigned char* digest = sqlite3_column_blob(stmt, field);
2055
2056 // Nothing further to do if field is NULL
2057 if (!digest)
2058 return 0;
2059
2060 // Length of the stored value
2061 const size_t length = sqlite3_column_bytes(stmt, field);
2062
2063 // Store digest
2064 return pakfire_file_set_digest(file, type, digest, length);
2065 }
2066
2067 static int pakfire_db_load_file(struct pakfire_db* db, struct pakfire_filelist* filelist,
2068 sqlite3_stmt* stmt) {
2069 struct pakfire_file* file = NULL;
2070 int r;
2071
2072 // Create a new file object
2073 r = pakfire_file_create(&file, db->pakfire);
2074 if (r)
2075 goto ERROR;
2076
2077 // Path
2078 const char* path = (const char*)sqlite3_column_text(stmt, 0);
2079
2080 // Abort if no path is set
2081 if (!path) {
2082 ERROR(db->pakfire, "File has no path\n");
2083 r = 1;
2084 goto ERROR;
2085 }
2086
2087 // Set path
2088 r = pakfire_file_set_path(file, path);
2089 if (r) {
2090 ERROR(db->pakfire, "%s: Could not set path '%s': %m\n", path, path);
2091 goto ERROR;
2092 }
2093
2094 // Size
2095 size_t size = sqlite3_column_int64(stmt, 1);
2096 if (size)
2097 pakfire_file_set_size(file, size);
2098
2099 // Mode
2100 mode_t mode = sqlite3_column_int(stmt, 2);
2101 if (mode)
2102 pakfire_file_set_mode(file, mode);
2103
2104 // uname
2105 const char* uname = (const char*)sqlite3_column_text(stmt, 3);
2106
2107 // Abort if no user is set
2108 if (!uname) {
2109 ERROR(db->pakfire, "%s: No user\n", path);
2110 r = 1;
2111 goto ERROR;
2112 }
2113
2114 // Set uname
2115 r = pakfire_file_set_uname(file, uname);
2116 if (r) {
2117 ERROR(db->pakfire, "%s: Could not set user '%s': %m\n", path, uname);
2118 goto ERROR;
2119 }
2120
2121 // gname
2122 const char* gname = (const char*)sqlite3_column_text(stmt, 4);
2123
2124 // Abort if no group is set
2125 if (!gname) {
2126 ERROR(db->pakfire, "%s: No group\n", path);
2127 r = 1;
2128 goto ERROR;
2129 }
2130
2131 // Set gname
2132 r = pakfire_file_set_gname(file, gname);
2133 if (r) {
2134 ERROR(db->pakfire, "%s: Could not set group '%s': %m\n", path, gname);
2135 goto ERROR;
2136 }
2137
2138 // ctime
2139 time_t ctime = sqlite3_column_int64(stmt, 5);
2140 if (ctime)
2141 pakfire_file_set_ctime(file, ctime);
2142
2143 // mtime
2144 time_t mtime = sqlite3_column_int64(stmt, 6);
2145 if (mtime)
2146 pakfire_file_set_mtime(file, mtime);
2147
2148 // SHA2-512 Digest
2149 r = pakfire_db_load_file_digest(db, file, stmt, PAKFIRE_DIGEST_SHA2_512, 7);
2150 if (r)
2151 goto ERROR;
2152
2153 // SHA2-256 Digest
2154 r = pakfire_db_load_file_digest(db, file, stmt, PAKFIRE_DIGEST_SHA2_256, 8);
2155 if (r)
2156 goto ERROR;
2157
2158 // BLAKE2b512 Digest
2159 r = pakfire_db_load_file_digest(db, file, stmt, PAKFIRE_DIGEST_BLAKE2B512, 9);
2160 if (r)
2161 goto ERROR;
2162
2163 // BLAKE2s256 Digest
2164 r = pakfire_db_load_file_digest(db, file, stmt, PAKFIRE_DIGEST_BLAKE2S256, 10);
2165 if (r)
2166 goto ERROR;
2167
2168 // SHA3-512 Digest
2169 r = pakfire_db_load_file_digest(db, file, stmt, PAKFIRE_DIGEST_SHA3_512, 11);
2170 if (r)
2171 goto ERROR;
2172
2173 // SHA3-256 Digest
2174 r = pakfire_db_load_file_digest(db, file, stmt, PAKFIRE_DIGEST_SHA3_256, 12);
2175 if (r)
2176 goto ERROR;
2177
2178 // Append the file to the filelist
2179 r = pakfire_filelist_append(filelist, file);
2180 if (r)
2181 goto ERROR;
2182
2183 ERROR:
2184 if (file)
2185 pakfire_file_unref(file);
2186
2187 return r;
2188 }
2189
2190 int pakfire_db_filelist(struct pakfire_db* db, struct pakfire_filelist** filelist) {
2191 struct pakfire_filelist* list = NULL;
2192 sqlite3_stmt* stmt = NULL;
2193 int r;
2194
2195 const char* sql =
2196 "SELECT "
2197 "path, "
2198 "size, "
2199 "mode, "
2200 "uname, "
2201 "gname, "
2202 "ctime, "
2203 "mtime, "
2204 "digest_sha2_512, "
2205 "digest_sha2_256, "
2206 "digest_blake2b512, "
2207 "digest_blake2s256, "
2208 "digest_sha3_512, "
2209 "digest_sha3_256 "
2210 "FROM files "
2211 "ORDER BY path"
2212 ";";
2213
2214 // Prepare the statement
2215 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
2216 if (r) {
2217 ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
2218 sql, sqlite3_errmsg(db->handle));
2219 goto ERROR;
2220 }
2221
2222 // Create a new filelist
2223 r = pakfire_filelist_create(&list, db->pakfire);
2224 if (r)
2225 goto ERROR;
2226
2227 // Iterate over all rows
2228 for (;;) {
2229 // Execute query
2230 r = sqlite3_step(stmt);
2231
2232 switch (r) {
2233 // Retry if the database was busy
2234 case SQLITE_BUSY:
2235 continue;
2236
2237 // Read a row
2238 case SQLITE_ROW:
2239 r = pakfire_db_load_file(db, list, stmt);
2240 if (r)
2241 goto ERROR;
2242 break;
2243
2244 // All rows have been processed
2245 case SQLITE_DONE:
2246 r = 0;
2247 goto END;
2248
2249 // Go to error in any other cases
2250 default:
2251 goto ERROR;
2252 }
2253 }
2254
2255 END:
2256 // Return the filelist
2257 *filelist = pakfire_filelist_ref(list);
2258
2259 ERROR:
2260 if (r)
2261 ERROR(db->pakfire, "Could not fetch filelist: %m\n");
2262
2263 if (stmt)
2264 sqlite3_finalize(stmt);
2265 if (list)
2266 pakfire_filelist_unref(list);
2267
2268 return r;
2269 }
2270
2271 int pakfire_db_package_filelist(struct pakfire_db* db, struct pakfire_filelist** filelist,
2272 struct pakfire_package* pkg) {
2273 struct pakfire_filelist* fl = NULL;
2274 sqlite3_stmt* stmt = NULL;
2275 int r = 1;
2276
2277 // Fetch the package ID
2278 uint64_t id = pakfire_package_get_dbid(pkg);
2279 if (!id) {
2280 ERROR(db->pakfire, "Package did not have an ID\n");
2281 return 1;
2282 }
2283
2284 // Create a new filelist
2285 r = pakfire_filelist_create(&fl, db->pakfire);
2286 if (r) {
2287 ERROR(db->pakfire, "Could not create filelist: %m\n");
2288 goto ERROR;
2289 }
2290
2291 const char* sql =
2292 "SELECT "
2293 "path, "
2294 "size, "
2295 "mode, "
2296 "uname, "
2297 "gname, "
2298 "ctime, "
2299 "mtime, "
2300 "digest_sha2_512, "
2301 "digest_sha2_256, "
2302 "digest_blake2b512, "
2303 "digest_blake2s256, "
2304 "digest_sha3_512, "
2305 "digest_sha3_256 "
2306 "FROM files "
2307
2308 // Select all files that belong to this package
2309 "WHERE "
2310 "pkg = ? "
2311
2312 // Filter out any files that are also in a different package (i.e. an update
2313 // that has already been installed and this is the cleanup of the obsolete pkg)
2314 "AND "
2315 "NOT EXISTS ("
2316 "SELECT "
2317 "1 "
2318 "FROM files AS duplicates "
2319 "WHERE "
2320 "files.path = duplicates.path "
2321 "AND "
2322 "files.pkg != duplicates.pkg"
2323 ") "
2324
2325 // Return the longest paths first
2326 "ORDER BY "
2327 "length(path) DESC"
2328 ";";
2329
2330 // Prepare the statement
2331 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
2332 if (r) {
2333 ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
2334 sql, sqlite3_errmsg(db->handle));
2335 goto ERROR;
2336 }
2337
2338 // Bind package ID
2339 r = sqlite3_bind_int64(stmt, 1, id);
2340 if (r) {
2341 ERROR(db->pakfire, "Could not bind package ID: %s\n", sqlite3_errmsg(db->handle));
2342 goto ERROR;
2343 }
2344
2345 for (;;) {
2346 // Execute query
2347 r = sqlite3_step(stmt);
2348
2349 switch (r) {
2350 // Retry if the database was busy
2351 case SQLITE_BUSY:
2352 continue;
2353
2354 // Read a row
2355 case SQLITE_ROW:
2356 r = pakfire_db_load_file(db, fl, stmt);
2357 if (r)
2358 goto ERROR;
2359 break;
2360
2361 // All rows have been processed
2362 case SQLITE_DONE:
2363 goto END;
2364
2365 // Go to error in any other cases
2366 default:
2367 goto ERROR;
2368 }
2369 }
2370
2371 END:
2372 *filelist = pakfire_filelist_ref(fl);
2373 r = 0;
2374
2375 ERROR:
2376 if (stmt)
2377 sqlite3_finalize(stmt);
2378 if (fl)
2379 pakfire_filelist_unref(fl);
2380
2381 return r;
2382 }