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