]> git.ipfire.org Git - pakfire.git/blob - src/libpakfire/db.c
archive: Read file attributes from archive
[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 <stdlib.h>
23 #include <time.h>
24
25 #include <sqlite3.h>
26
27 #include <pakfire/archive.h>
28 #include <pakfire/db.h>
29 #include <pakfire/file.h>
30 #include <pakfire/logging.h>
31 #include <pakfire/package.h>
32 #include <pakfire/pakfire.h>
33 #include <pakfire/private.h>
34 #include <pakfire/relationlist.h>
35 #include <pakfire/repo.h>
36 #include <pakfire/types.h>
37 #include <pakfire/util.h>
38
39 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
40
41 #define CURRENT_SCHEMA 8
42 #define SCHEMA_MIN_SUP 7
43
44 struct pakfire_db {
45 Pakfire pakfire;
46 int nrefs;
47
48 int mode;
49
50 sqlite3* handle;
51 int schema;
52 };
53
54 static void logging_callback(void* data, int r, const char* msg) {
55 Pakfire pakfire = (Pakfire)data;
56
57 ERROR(pakfire, "Database Error: %s: %s\n",
58 sqlite3_errstr(r), msg);
59 }
60
61 static int pakfire_db_execute(struct pakfire_db* db, const char* stmt) {
62 int r;
63
64 DEBUG(db->pakfire, "Executing database query: %s\n", stmt);
65
66 do {
67 r = sqlite3_exec(db->handle, stmt, NULL, NULL, NULL);
68 } while (r == SQLITE_BUSY);
69
70 // Log any errors
71 if (r) {
72 ERROR(db->pakfire, "Database query failed: %s\n", sqlite3_errmsg(db->handle));
73 }
74
75 return r;
76 }
77
78 static int pakfire_db_begin_transaction(struct pakfire_db* db) {
79 return pakfire_db_execute(db, "BEGIN TRANSACTION");
80 }
81
82 static int pakfire_db_commit(struct pakfire_db* db) {
83 return pakfire_db_execute(db, "COMMIT");
84 }
85
86 static int pakfire_db_rollback(struct pakfire_db* db) {
87 return pakfire_db_execute(db, "ROLLBACK");
88 }
89
90 /*
91 This function performs any fast optimization and tries to truncate the WAL log file
92 to keep the database as compact as possible on disk.
93 */
94 static void pakfire_db_optimize(struct pakfire_db* db) {
95 pakfire_db_execute(db, "PRAGMA optimize");
96 pakfire_db_execute(db, "PRAGMA wal_checkpoint = TRUNCATE");
97 }
98
99 static void pakfire_db_free(struct pakfire_db* db) {
100 DEBUG(db->pakfire, "Releasing database at %p\n", db);
101
102 if (db->handle) {
103 // Optimize the database before it is being closed
104 pakfire_db_optimize(db);
105
106 // Close database handle
107 int r = sqlite3_close(db->handle);
108 if (r != SQLITE_OK) {
109 ERROR(db->pakfire, "Could not close database handle: %s\n",
110 sqlite3_errmsg(db->handle));
111 }
112 }
113
114 pakfire_unref(db->pakfire);
115
116 free(db);
117 }
118
119 static sqlite3_value* pakfire_db_get(struct pakfire_db* db, const char* key) {
120 sqlite3_stmt* stmt = NULL;
121 sqlite3_value* val = NULL;
122 int r;
123
124 const char* sql = "SELECT val FROM settings WHERE key = ?";
125
126 // Prepare the statement
127 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
128 if (r != SQLITE_OK) {
129 //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
130 // sql, sqlite3_errmsg(db->handle));
131 return NULL;
132 }
133
134 // Bind key
135 r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL);
136 if (r != SQLITE_OK) {
137 ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle));
138 goto ERROR;
139 }
140
141 // Execute the statement
142 do {
143 r = sqlite3_step(stmt);
144 } while (r == SQLITE_BUSY);
145
146 // We should have read a row
147 if (r != SQLITE_ROW)
148 goto ERROR;
149
150 // Read value
151 val = sqlite3_column_value(stmt, 0);
152 if (!val) {
153 ERROR(db->pakfire, "Could not read value\n");
154 goto ERROR;
155 }
156
157 // Copy value onto the heap
158 val = sqlite3_value_dup(val);
159
160 ERROR:
161 if (stmt)
162 sqlite3_finalize(stmt);
163
164 return val;
165 }
166
167 static int pakfire_db_set_int(struct pakfire_db* db, const char* key, int val) {
168 sqlite3_stmt* stmt = NULL;
169 int r;
170
171 const char* sql = "INSERT INTO settings(key, val) VALUES(?, ?) \
172 ON CONFLICT (key) DO UPDATE SET val = excluded.val";
173
174 // Prepare statement
175 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
176 if (r != SQLITE_OK) {
177 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
178 sql, sqlite3_errmsg(db->handle));
179 return 1;
180 }
181
182 // Bind key
183 r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL);
184 if (r != SQLITE_OK) {
185 ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle));
186 goto ERROR;
187 }
188
189 // Bind val
190 r = sqlite3_bind_int64(stmt, 2, val);
191 if (r != SQLITE_OK) {
192 ERROR(db->pakfire, "Could not bind val: %s\n", sqlite3_errmsg(db->handle));
193 goto ERROR;
194 }
195
196 // Execute the statement
197 do {
198 r = sqlite3_step(stmt);
199 } while (r == SQLITE_BUSY);
200
201 // Set return code
202 r = (r == SQLITE_OK);
203
204 ERROR:
205 if (stmt)
206 sqlite3_finalize(stmt);
207
208 return r;
209 }
210
211 static int pakfire_db_get_schema(struct pakfire_db* db) {
212 sqlite3_value* value = pakfire_db_get(db, "schema");
213 if (!value)
214 return -1;
215
216 int schema = sqlite3_value_int64(value);
217 sqlite3_value_free(value);
218
219 DEBUG(db->pakfire, "Database has schema version %d\n", schema);
220
221 return schema;
222 }
223
224 static int pakfire_db_create_schema(struct pakfire_db* db) {
225 int r;
226
227 // Create settings table
228 r = pakfire_db_execute(db, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)");
229 if (r)
230 return 1;
231
232 // settings: Add a unique index on key
233 r = pakfire_db_execute(db, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)");
234 if (r)
235 return 1;
236
237 // Create packages table
238 r = pakfire_db_execute(db,
239 "CREATE TABLE IF NOT EXISTS packages("
240 "id INTEGER PRIMARY KEY, "
241 "name TEXT, "
242 "epoch INTEGER, "
243 "version TEXT, "
244 "release TEXT, "
245 "arch TEXT, "
246 "groups TEXT, "
247 "filename TEXT, "
248 "size INTEGER, "
249 "inst_size INTEGER, "
250 "hash1 TEXT, "
251 "license TEXT, "
252 "summary TEXT, "
253 "description TEXT, "
254 "uuid TEXT, "
255 "vendor TEXT, "
256 "build_host TEXT, "
257 "build_time INTEGER, "
258 "installed INTEGER, "
259 "reason TEXT, "
260 "repository TEXT"
261 ")");
262 if (r)
263 return 1;
264
265 // packages: Create index to find package by name
266 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)");
267 if (r)
268 return 1;
269
270 // packages: Create unique index over UUID
271 r = pakfire_db_execute(db, "CREATE UNIQUE INDEX IF NOT EXISTS packages_uuid ON packages(uuid)");
272 if (r)
273 return 1;
274
275 // Create dependencies table
276 r = pakfire_db_execute(db,
277 "CREATE TABLE IF NOT EXISTS dependencies("
278 "pkg INTEGER, "
279 "type TEXT, "
280 "dependency TEXT, "
281 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
282 ")");
283 if (r)
284 return r;
285
286 // dependencies: Add index over packages
287 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)");
288 if (r)
289 return r;
290
291 // Create files table
292 r = pakfire_db_execute(db,
293 "CREATE TABLE IF NOT EXISTS files("
294 "id INTEGER PRIMARY KEY, "
295 "name TEXT, "
296 "pkg INTEGER, "
297 "size INTEGER, "
298 "type INTEGER, "
299 "config INTEGER, "
300 "datafile INTEGER, "
301 "mode INTEGER, "
302 "user TEXT, "
303 "'group' TEXT, "
304 "hash1 TEXT, "
305 "mtime INTEGER, "
306 "capabilities TEXT, "
307 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
308 ")");
309 if (r)
310 return 1;
311
312 // files: Add index over packages
313 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)");
314 if (r)
315 return 1;
316
317 // Create scriptlets table
318 r = pakfire_db_execute(db,
319 "CREATE TABLE IF NOT EXISTS scriptlets("
320 "id INTEGER PRIMARY KEY, "
321 "pkg INTEGER, "
322 "type TEXT, "
323 "scriptlet TEXT, "
324 "FOREIGN KEY (pkg) REFERENCES packages(id) ON DELETE CASCADE"
325 ")");
326 if (r)
327 return 1;
328
329 // scriptlets: Add index over packages
330 r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)");
331 if (r)
332 return 1;
333
334 return 0;
335 }
336
337 static int pakfire_db_migrate_to_schema_8(struct pakfire_db* db) {
338 // packages: Drop build_id column
339
340 // Add foreign keys
341 // TODO sqlite doesn't support adding foreign keys to existing tables and so we would
342 // need to recreate the whole table and rename it afterwards. Annoying.
343
344 return 0;
345 }
346
347 static int pakfire_db_migrate_schema(struct pakfire_db* db) {
348 int r;
349
350 while (db->schema < CURRENT_SCHEMA) {
351 // Begin a new transaction
352 r = pakfire_db_begin_transaction(db);
353 if (r)
354 goto ROLLBACK;
355
356 switch (db->schema) {
357 // No schema exists
358 case -1:
359 r = pakfire_db_create_schema(db);
360 if (r)
361 goto ROLLBACK;
362
363 db->schema = CURRENT_SCHEMA;
364 break;
365
366 case 7:
367 r = pakfire_db_migrate_to_schema_8(db);
368 if (r)
369 goto ROLLBACK;
370
371 db->schema++;
372 break;
373
374 default:
375 ERROR(db->pakfire, "Cannot migrate database from schema %d\n", db->schema);
376 goto ROLLBACK;
377 }
378
379 // Update the schema version
380 r = pakfire_db_set_int(db, "schema", CURRENT_SCHEMA);
381 if (r)
382 goto ROLLBACK;
383
384 // All done, commit!
385 r = pakfire_db_commit(db);
386 if (r)
387 goto ROLLBACK;
388 }
389
390 return 0;
391
392 ROLLBACK:
393 pakfire_db_rollback(db);
394
395 return 1;
396 }
397
398 static int pakfire_db_setup(struct pakfire_db* db) {
399 int r;
400
401 // Setup logging
402 sqlite3_config(SQLITE_CONFIG_LOG, logging_callback, db->pakfire);
403
404 // Enable foreign keys
405 pakfire_db_execute(db, "PRAGMA foreign_keys = ON");
406
407 // Make LIKE case-sensitive
408 pakfire_db_execute(db, "PRAGMA case_sensitive_like = ON");
409
410 // Fetch the current schema
411 db->schema = pakfire_db_get_schema(db);
412
413 // Check if the schema is recent enough
414 if (db->schema > 0 && db->schema < SCHEMA_MIN_SUP) {
415 ERROR(db->pakfire, "Database schema %d is not supported by this version of Pakfire\n",
416 db->schema);
417 return 1;
418 }
419
420 // Done when not in read-write mode
421 if (db->mode != PAKFIRE_DB_READWRITE)
422 return 0;
423
424 // Disable secure delete
425 pakfire_db_execute(db, "PRAGMA secure_delete = OFF");
426
427 // Set database journal to WAL
428 r = pakfire_db_execute(db, "PRAGMA journal_mode = WAL");
429 if (r != SQLITE_OK) {
430 ERROR(db->pakfire, "Could not set journal mode to WAL: %s\n",
431 sqlite3_errmsg(db->handle));
432 return 1;
433 }
434
435 // Disable autocheckpoint
436 r = sqlite3_wal_autocheckpoint(db->handle, 0);
437 if (r != SQLITE_OK) {
438 ERROR(db->pakfire, "Could not disable autocheckpoint: %s\n",
439 sqlite3_errmsg(db->handle));
440 return 1;
441 }
442
443 // Create or migrate schema
444 r = pakfire_db_migrate_schema(db);
445 if (r)
446 return r;
447
448 return 0;
449 }
450
451 PAKFIRE_EXPORT int pakfire_db_open(struct pakfire_db** db, Pakfire pakfire, int flags) {
452 int r = 1;
453
454 struct pakfire_db* o = calloc(1, sizeof(*o));
455 if (!o)
456 return -ENOMEM;
457
458 DEBUG(pakfire, "Allocated database at %p\n", o);
459
460 o->pakfire = pakfire_ref(pakfire);
461 o->nrefs = 1;
462
463 int sqlite3_flags = 0;
464
465 // Store mode & forward it to sqlite3
466 if (flags & PAKFIRE_DB_READWRITE) {
467 o->mode = PAKFIRE_DB_READWRITE;
468 sqlite3_flags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
469 } else {
470 o->mode = PAKFIRE_DB_READONLY;
471 sqlite3_flags |= SQLITE_OPEN_READONLY;
472 }
473
474 // Make the filename
475 char* path = pakfire_make_path(o->pakfire, DATABASE_PATH);
476 if (!path)
477 goto END;
478
479 // Try to open the sqlite3 database file
480 r = sqlite3_open_v2(path, &o->handle, sqlite3_flags, NULL);
481 if (r != SQLITE_OK) {
482 ERROR(pakfire, "Could not open database %s: %s\n",
483 path, sqlite3_errmsg(o->handle));
484
485 r = 1;
486 goto END;
487 }
488
489 // Setup the database
490 r = pakfire_db_setup(o);
491 if (r)
492 goto END;
493
494 *db = o;
495 r = 0;
496
497 END:
498 if (r)
499 pakfire_db_free(o);
500
501 if (path)
502 free(path);
503
504 return r;
505 }
506
507 PAKFIRE_EXPORT struct pakfire_db* pakfire_db_ref(struct pakfire_db* db) {
508 db->nrefs++;
509
510 return db;
511 }
512
513 PAKFIRE_EXPORT struct pakfire_db* pakfire_db_unref(struct pakfire_db* db) {
514 if (--db->nrefs > 0)
515 return db;
516
517 pakfire_db_free(db);
518
519 return NULL;
520 }
521
522 static unsigned long pakfire_db_integrity_check(struct pakfire_db* db) {
523 sqlite3_stmt* stmt = NULL;
524 int r;
525
526 r = sqlite3_prepare_v2(db->handle, "PRAGMA integrity_check", -1, &stmt, NULL);
527 if (r) {
528 ERROR(db->pakfire, "Could not prepare integrity check: %s\n",
529 sqlite3_errmsg(db->handle));
530 return 1;
531 }
532
533 // Count any errors
534 unsigned long errors = 0;
535
536 while (1) {
537 do {
538 r = sqlite3_step(stmt);
539 } while (r == SQLITE_BUSY);
540
541 if (r == SQLITE_ROW) {
542 const char* error = (const char*)sqlite3_column_text(stmt, 0);
543
544 // If the message is "ok", the database has passed the check
545 if (strcmp(error, "ok") == 0)
546 continue;
547
548 // Increment error counter
549 errors++;
550
551 // Log the message
552 ERROR(db->pakfire, "%s\n", error);
553
554 // Break on anything else
555 } else
556 break;
557 }
558
559 sqlite3_finalize(stmt);
560
561 if (errors)
562 ERROR(db->pakfire, "Database integrity check failed\n");
563 else
564 INFO(db->pakfire, "Database integrity check passed\n");
565
566 return errors;
567 }
568
569 static unsigned long pakfire_db_foreign_key_check(struct pakfire_db* db) {
570 sqlite3_stmt* stmt = NULL;
571 int r;
572
573 r = sqlite3_prepare_v2(db->handle, "PRAGMA foreign_key_check", -1, &stmt, NULL);
574 if (r) {
575 ERROR(db->pakfire, "Could not prepare foreign key check: %s\n",
576 sqlite3_errmsg(db->handle));
577 return 1;
578 }
579
580 // Count any errors
581 unsigned long errors = 0;
582
583 while (1) {
584 do {
585 r = sqlite3_step(stmt);
586 } while (r == SQLITE_BUSY);
587
588 if (r == SQLITE_ROW) {
589 const unsigned char* table = sqlite3_column_text(stmt, 0);
590 unsigned long rowid = sqlite3_column_int64(stmt, 1);
591 const unsigned char* foreign_table = sqlite3_column_text(stmt, 2);
592 unsigned long foreign_rowid = sqlite3_column_int64(stmt, 3);
593
594 // Increment error counter
595 errors++;
596
597 // Log the message
598 ERROR(db->pakfire, "Foreign key violation found in %s, row %lu: "
599 "%lu does not exist in table %s\n", table, rowid, foreign_rowid, foreign_table);
600
601 // Break on anything else
602 } else
603 break;
604 }
605
606 sqlite3_finalize(stmt);
607
608 if (errors)
609 ERROR(db->pakfire, "Foreign key check failed\n");
610 else
611 INFO(db->pakfire, "Foreign key check passed\n");
612
613 return errors;
614 }
615
616 /*
617 This function performs an integrity check of the database
618 */
619 PAKFIRE_EXPORT int pakfire_db_check(struct pakfire_db* db) {
620 int r;
621
622 // Perform integrity check
623 r = pakfire_db_integrity_check(db);
624 if (r)
625 return 1;
626
627 // Perform foreign key check
628 r = pakfire_db_foreign_key_check(db);
629 if (r)
630 return 1;
631
632 return 0;
633 }
634
635 // Returns the number of packages installed
636 PAKFIRE_EXPORT ssize_t pakfire_db_packages(struct pakfire_db* db) {
637 sqlite3_stmt* stmt = NULL;
638 ssize_t packages = -1;
639
640 int r = sqlite3_prepare_v2(db->handle, "SELECT COUNT(*) FROM packages", -1, &stmt, NULL);
641 if (r) {
642 ERROR(db->pakfire, "Could not prepare SQL statement: %s\n",
643 sqlite3_errmsg(db->handle));
644 return -1;
645 }
646
647 // Execute query
648 do {
649 r = sqlite3_step(stmt);
650 } while (r == SQLITE_BUSY);
651
652 if (r == SQLITE_ROW) {
653 packages = sqlite3_column_int64(stmt, 0);
654 }
655
656 sqlite3_finalize(stmt);
657
658 return packages;
659 }
660
661 static int pakfire_db_add_dependencies(struct pakfire_db* db, unsigned long id, PakfirePackage pkg) {
662 sqlite3_stmt* stmt = NULL;
663 int r = 1;
664
665 const char* sql = "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
666
667 // Prepare the statement
668 r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL);
669 if (r) {
670 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
671 sql, sqlite3_errmsg(db->handle));
672 goto END;
673 }
674
675 const struct __relation {
676 const char* type;
677 PakfireRelationList (*func)(PakfirePackage);
678 } relations[] = {
679 { "provides", pakfire_package_get_provides },
680 { "prerequires", pakfire_package_get_prerequires },
681 { "requires", pakfire_package_get_requires },
682 { "conflicts", pakfire_package_get_conflicts },
683 { "obsoletes", pakfire_package_get_obsoletes },
684 { "recommends", pakfire_package_get_recommends },
685 { "suggests", pakfire_package_get_suggests },
686 { "supplements", pakfire_package_get_supplements },
687 { "enhances", pakfire_package_get_enhances },
688 { NULL, NULL },
689 };
690
691 for (const struct __relation* relation = relations; relation->type; relation++) {
692 PakfireRelationList list = relation->func(pkg);
693 if (!list)
694 continue;
695
696 for (unsigned int i = 0; i < pakfire_relationlist_size(list); i++) {
697 PakfireRelation rel = pakfire_relationlist_get(list, i);
698 if (!rel)
699 goto END;
700
701 char* dependency = pakfire_relation_str(rel);
702 if (!dependency) {
703 pakfire_relation_unref(rel);
704 r = 1;
705 goto END;
706 }
707
708 // Bind package ID
709 r = sqlite3_bind_int64(stmt, 1, id);
710 if (r) {
711 ERROR(db->pakfire, "Could not bind id: %s\n",
712 sqlite3_errmsg(db->handle));
713 pakfire_relation_unref(rel);
714 goto END;
715 }
716
717 // Bind type
718 r = sqlite3_bind_text(stmt, 2, relation->type, -1, NULL);
719 if (r) {
720 ERROR(db->pakfire, "Could not bind type: %s\n",
721 sqlite3_errmsg(db->handle));
722 pakfire_relation_unref(rel);
723 goto END;
724 }
725
726 // Bind dependency
727 r = sqlite3_bind_text(stmt, 3, dependency, -1, NULL);
728 if (r) {
729 ERROR(db->pakfire, "Could not bind dependency: %s\n",
730 sqlite3_errmsg(db->handle));
731 pakfire_relation_unref(rel);
732 goto END;
733 }
734
735 // Execute query
736 do {
737 r = sqlite3_step(stmt);
738 } while (r == SQLITE_BUSY);
739
740 pakfire_relation_unref(rel);
741 free(dependency);
742
743 // Reset bound values
744 sqlite3_reset(stmt);
745 }
746
747 pakfire_relationlist_unref(list);
748 }
749
750 // All okay
751 r = 0;
752
753 END:
754 if (stmt)
755 sqlite3_finalize(stmt);
756
757 return r;
758 }
759
760 static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, PakfireArchive archive) {
761 sqlite3_stmt* stmt = NULL;
762 int r = 1;
763
764 // Get the filelist from the archive
765 PakfireFilelist filelist = pakfire_archive_get_filelist(archive);
766 if (!filelist) {
767 ERROR(db->pakfire, "Could not fetch filelist from archive\n");
768 return 1;
769 }
770
771 // Nothing to do if the list is empty
772 if (pakfire_filelist_is_empty(filelist)) {
773 r = 0;
774 goto END;
775 }
776
777 const char* sql = "INSERT INTO files(pkg, name, size, type, config, datafile, mode, "
778 "user, 'group', hash1, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
779
780 // Prepare the statement
781 r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL);
782 if (r) {
783 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
784 sql, sqlite3_errmsg(db->handle));
785 goto END;
786 }
787
788 for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) {
789 PakfireFile file = pakfire_filelist_get(filelist, i);
790
791 // Bind package ID
792 r = sqlite3_bind_int64(stmt, 1, id);
793 if (r) {
794 ERROR(db->pakfire, "Could not bind id: %s\n", sqlite3_errmsg(db->handle));
795 pakfire_file_unref(file);
796 goto END;
797 }
798
799 // Bind name
800 const char* name = pakfire_file_get_name(file);
801
802 r = sqlite3_bind_text(stmt, 2, name, -1, NULL);
803 if (r) {
804 ERROR(db->pakfire, "Could not bind name: %s\n", sqlite3_errmsg(db->handle));
805 pakfire_file_unref(file);
806 goto END;
807 }
808
809 // Bind size
810 size_t size = pakfire_file_get_size(file);
811
812 r = sqlite3_bind_int64(stmt, 3, size);
813 if (r) {
814 ERROR(db->pakfire, "Could not bind size: %s\n", sqlite3_errmsg(db->handle));
815 pakfire_file_unref(file);
816 goto END;
817 }
818
819 // Bind type - XXX this is char which isn't very helpful
820 //char type = pakfire_file_get_type(file);
821
822 r = sqlite3_bind_null(stmt, 4);
823 if (r) {
824 ERROR(db->pakfire, "Could not bind type: %s\n", sqlite3_errmsg(db->handle));
825 pakfire_file_unref(file);
826 goto END;
827 }
828
829 // Bind config - XXX TODO
830 r = sqlite3_bind_null(stmt, 5);
831 if (r) {
832 ERROR(db->pakfire, "Could not bind config: %s\n", sqlite3_errmsg(db->handle));
833 pakfire_file_unref(file);
834 goto END;
835 }
836
837 // Bind datafile - XXX TODO
838 r = sqlite3_bind_null(stmt, 6);
839 if (r) {
840 ERROR(db->pakfire, "Could not bind datafile: %s\n", sqlite3_errmsg(db->handle));
841 pakfire_file_unref(file);
842 goto END;
843 }
844
845 // Bind mode
846 mode_t mode = pakfire_file_get_mode(file);
847
848 r = sqlite3_bind_int64(stmt, 7, mode);
849 if (r) {
850 ERROR(db->pakfire, "Could not bind mode: %s\n", sqlite3_errmsg(db->handle));
851 pakfire_file_unref(file);
852 goto END;
853 }
854
855 // Bind user
856 const char* user = pakfire_file_get_user(file);
857
858 r = sqlite3_bind_text(stmt, 8, user, -1, NULL);
859 if (r) {
860 ERROR(db->pakfire, "Could not bind user: %s\n", sqlite3_errmsg(db->handle));
861 pakfire_file_unref(file);
862 goto END;
863 }
864
865 // Bind group
866 const char* group = pakfire_file_get_group(file);
867
868 r = sqlite3_bind_text(stmt, 9, group, -1, NULL);
869 if (r) {
870 ERROR(db->pakfire, "Could not bind group: %s\n", sqlite3_errmsg(db->handle));
871 pakfire_file_unref(file);
872 goto END;
873 }
874
875 // Bind hash1
876 const char* chksum = pakfire_file_get_chksum(file);
877
878 r = sqlite3_bind_text(stmt, 10, chksum, -1, NULL);
879 if (r) {
880 ERROR(db->pakfire, "Could not bind hash1: %s\n", sqlite3_errmsg(db->handle));
881 pakfire_file_unref(file);
882 goto END;
883 }
884
885 // Bind mtime
886 time_t mtime = pakfire_file_get_time(file);
887
888 r = sqlite3_bind_int64(stmt, 11, mtime);
889 if (r) {
890 ERROR(db->pakfire, "Could not bind mtime: %s\n", sqlite3_errmsg(db->handle));
891 pakfire_file_unref(file);
892 goto END;
893 }
894
895 // Bind capabilities - XXX TODO
896 r = sqlite3_bind_null(stmt, 12);
897 if (r) {
898 ERROR(db->pakfire, "Could not bind capabilities: %s\n", sqlite3_errmsg(db->handle));
899 pakfire_file_unref(file);
900 goto END;
901 }
902
903 // Execute query
904 do {
905 r = sqlite3_step(stmt);
906 } while (r == SQLITE_BUSY);
907
908 // Move on to next file
909 pakfire_file_unref(file);
910
911 // Check for errors
912 if (r != SQLITE_DONE) {
913 ERROR(db->pakfire, "Could not add file to database: %s\n",
914 sqlite3_errmsg(db->handle));
915 goto END;
916 }
917
918 // Reset bound values
919 sqlite3_reset(stmt);
920 }
921
922 // All okay
923 r = 0;
924
925 END:
926 if (stmt)
927 sqlite3_finalize(stmt);
928
929 pakfire_filelist_unref(filelist);
930
931 return r;
932 }
933
934 static int pakfire_db_add_scriptlets(struct pakfire_db* db, unsigned long id, PakfireArchive archive) {
935 sqlite3_stmt* stmt = NULL;
936 int r = 1;
937
938 const struct pakfire_scriptlet_type* scriptlet_type = PAKFIRE_SCRIPTLET_TYPES;
939 struct pakfire_scriptlet* scriptlet;
940
941 const char* sql = "INSERT INTO scriptlets(pkg, type, scriptlet) VALUES(?, ?, ?)";
942
943 // Prepare the statement
944 r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL);
945 if (r) {
946 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
947 sql, sqlite3_errmsg(db->handle));
948 goto END;
949 }
950
951 while (scriptlet_type->type) {
952 // Fetch the scriptlet
953 scriptlet = pakfire_archive_get_scriptlet(archive, scriptlet_type->type);
954
955 // Go to next one if the archive does not have a scriptlet of the given type
956 if (!scriptlet) {
957 scriptlet_type++;
958 continue;
959 }
960
961 // Bind package ID
962 r = sqlite3_bind_int64(stmt, 1, id);
963 if (r) {
964 ERROR(db->pakfire, "Could not bind id: %s\n", sqlite3_errmsg(db->handle));
965 goto END;
966 }
967
968 // Bind handle
969 r = sqlite3_bind_text(stmt, 2, scriptlet_type->handle, -1, NULL);
970 if (r) {
971 ERROR(db->pakfire, "Could not bind type: %s\n", sqlite3_errmsg(db->handle));
972 goto END;
973 }
974
975 // Bind scriptlet
976 r = sqlite3_bind_text(stmt, 3, scriptlet->data, scriptlet->size, NULL);
977 if (r) {
978 ERROR(db->pakfire, "Could not bind scriptlet: %s\n", sqlite3_errmsg(db->handle));
979 goto END;
980 }
981
982 // Execute query
983 do {
984 r = sqlite3_step(stmt);
985 } while (r == SQLITE_BUSY);
986
987 // Check for errors
988 if (r != SQLITE_DONE) {
989 ERROR(db->pakfire, "Could not add scriptlet to database: %s\n",
990 sqlite3_errmsg(db->handle));
991 goto END;
992 }
993
994 // Reset bound values
995 sqlite3_reset(stmt);
996
997 scriptlet_type++;
998 }
999
1000 // All okay
1001 r = 0;
1002
1003 END:
1004 if (stmt)
1005 sqlite3_finalize(stmt);
1006
1007 return r;
1008 }
1009
1010 PAKFIRE_EXPORT int pakfire_db_add_package(struct pakfire_db* db,
1011 PakfirePackage pkg, PakfireArchive archive) {
1012 sqlite3_stmt* stmt = NULL;
1013 int r;
1014
1015 // Begin a new transaction
1016 r = pakfire_db_begin_transaction(db);
1017 if (r)
1018 goto ROLLBACK;
1019
1020 const char* sql = "INSERT INTO packages(name, epoch, version, release, arch, groups, "
1021 "filename, size, inst_size, hash1, license, summary, description, uuid, vendor, "
1022 "build_host, build_time, installed, repository, reason) VALUES(?, ?, "
1023 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)";
1024
1025 // Prepare the statement
1026 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
1027 if (r != SQLITE_OK) {
1028 ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
1029 sql, sqlite3_errmsg(db->handle));
1030 goto ROLLBACK;
1031 }
1032
1033 // Bind name
1034 const char* name = pakfire_package_get_name(pkg);
1035
1036 r = sqlite3_bind_text(stmt, 1, name, -1, NULL);
1037 if (r) {
1038 ERROR(db->pakfire, "Could not bind name: %s\n", sqlite3_errmsg(db->handle));
1039 goto ROLLBACK;
1040 }
1041
1042 // Bind epoch
1043 unsigned long epoch = pakfire_package_get_epoch(pkg);
1044
1045 r = sqlite3_bind_int64(stmt, 2, epoch);
1046 if (r) {
1047 ERROR(db->pakfire, "Could not bind epoch: %s\n", sqlite3_errmsg(db->handle));
1048 goto ROLLBACK;
1049 }
1050
1051 // Bind version
1052 const char* version = pakfire_package_get_version(pkg);
1053
1054 r = sqlite3_bind_text(stmt, 3, version, -1, NULL);
1055 if (r) {
1056 ERROR(db->pakfire, "Could not bind version: %s\n", sqlite3_errmsg(db->handle));
1057 goto ROLLBACK;
1058 }
1059
1060 // Bind release
1061 const char* release = pakfire_package_get_release(pkg);
1062
1063 r = sqlite3_bind_text(stmt, 4, release, -1, NULL);
1064 if (r) {
1065 ERROR(db->pakfire, "Could not bind release: %s\n", sqlite3_errmsg(db->handle));
1066 goto ROLLBACK;
1067 }
1068
1069 // Bind arch
1070 const char* arch = pakfire_package_get_arch(pkg);
1071
1072 r = sqlite3_bind_text(stmt, 5, arch, -1, NULL);
1073 if (r) {
1074 ERROR(db->pakfire, "Could not bind arch: %s\n", sqlite3_errmsg(db->handle));
1075 goto ROLLBACK;
1076 }
1077
1078 // Bind groups
1079 const char* groups = pakfire_package_get_groups(pkg);
1080
1081 r = sqlite3_bind_text(stmt, 6, groups, -1, NULL);
1082 if (r) {
1083 ERROR(db->pakfire, "Could not bind groups: %s\n", sqlite3_errmsg(db->handle));
1084 goto ROLLBACK;
1085 }
1086
1087 // Bind filename
1088 const char* filename = pakfire_package_get_filename(pkg);
1089
1090 r = sqlite3_bind_text(stmt, 7, filename, -1, NULL);
1091 if (r) {
1092 ERROR(db->pakfire, "Could not bind filename: %s\n", sqlite3_errmsg(db->handle));
1093 goto ROLLBACK;
1094 }
1095
1096 // Bind size
1097 unsigned long long size = pakfire_package_get_downloadsize(pkg);
1098
1099 r = sqlite3_bind_int64(stmt, 8, size);
1100 if (r) {
1101 ERROR(db->pakfire, "Could not bind size: %s\n", sqlite3_errmsg(db->handle));
1102 goto ROLLBACK;
1103 }
1104
1105 // Bind installed size
1106 unsigned long long inst_size = pakfire_package_get_installsize(pkg);
1107
1108 r = sqlite3_bind_int64(stmt, 9, inst_size);
1109 if (r) {
1110 ERROR(db->pakfire, "Could not bind inst_size: %s\n", sqlite3_errmsg(db->handle));
1111 goto ROLLBACK;
1112 }
1113
1114 // Bind hash1
1115 const char* hash1 = pakfire_package_get_checksum(pkg);
1116
1117 r = sqlite3_bind_text(stmt, 10, hash1, -1, NULL);
1118 if (r) {
1119 ERROR(db->pakfire, "Could not bind hash1: %s\n", sqlite3_errmsg(db->handle));
1120 goto ROLLBACK;
1121 }
1122
1123 // Bind license
1124 const char* license = pakfire_package_get_license(pkg);
1125
1126 r = sqlite3_bind_text(stmt, 11, license, -1, NULL);
1127 if (r) {
1128 ERROR(db->pakfire, "Could not bind license: %s\n", sqlite3_errmsg(db->handle));
1129 goto ROLLBACK;
1130 }
1131
1132 // Bind summary
1133 const char* summary = pakfire_package_get_summary(pkg);
1134
1135 r = sqlite3_bind_text(stmt, 12, summary, -1, NULL);
1136 if (r) {
1137 ERROR(db->pakfire, "Could not bind summary: %s\n", sqlite3_errmsg(db->handle));
1138 goto ROLLBACK;
1139 }
1140
1141 // Bind description
1142 const char* description = pakfire_package_get_description(pkg);
1143
1144 r = sqlite3_bind_text(stmt, 13, description, -1, NULL);
1145 if (r) {
1146 ERROR(db->pakfire, "Could not bind description: %s\n", sqlite3_errmsg(db->handle));
1147 goto ROLLBACK;
1148 }
1149
1150 // Bind uuid
1151 const char* uuid = pakfire_package_get_uuid(pkg);
1152
1153 r = sqlite3_bind_text(stmt, 14, uuid, -1, NULL);
1154 if (r) {
1155 ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle));
1156 goto ROLLBACK;
1157 }
1158
1159 // Bind vendor
1160 const char* vendor = pakfire_package_get_vendor(pkg);
1161
1162 r = sqlite3_bind_text(stmt, 15, vendor, -1, NULL);
1163 if (r) {
1164 ERROR(db->pakfire, "Could not bind vendor: %s\n", sqlite3_errmsg(db->handle));
1165 goto ROLLBACK;
1166 }
1167
1168 // Bind build_host
1169 const char* build_host = pakfire_package_get_build_host(pkg);
1170
1171 r = sqlite3_bind_text(stmt, 16, build_host, -1, NULL);
1172 if (r) {
1173 ERROR(db->pakfire, "Could not bind build_host: %s\n", sqlite3_errmsg(db->handle));
1174 goto ROLLBACK;
1175 }
1176
1177 // Bind build_time
1178 time_t build_time = pakfire_package_get_build_time(pkg);
1179
1180 r = sqlite3_bind_int64(stmt, 17, build_time);
1181 if (r) {
1182 ERROR(db->pakfire, "Could not bind build_time: %s\n", sqlite3_errmsg(db->handle));
1183 goto ROLLBACK;
1184 }
1185
1186 // Bind repository name
1187 PakfireRepo repo = pakfire_package_get_repo(pkg);
1188 if (repo) {
1189 const char* repo_name = pakfire_repo_get_name(repo);
1190 pakfire_repo_unref(repo);
1191
1192 r = sqlite3_bind_text(stmt, 18, repo_name, -1, NULL);
1193 if (r)
1194 goto ROLLBACK;
1195
1196 // No repository?
1197 } else {
1198 r = sqlite3_bind_null(stmt, 18);
1199 if (r)
1200 goto ROLLBACK;
1201 }
1202
1203 // XXX TODO Bind reason
1204 r = sqlite3_bind_null(stmt, 19);
1205 if (r)
1206 goto ROLLBACK;
1207
1208 // Run query
1209 do {
1210 r = sqlite3_step(stmt);
1211 } while (r == SQLITE_BUSY);
1212
1213 if (r != SQLITE_DONE) {
1214 ERROR(db->pakfire, "Could not add package to database: %s\n",
1215 sqlite3_errmsg(db->handle));
1216 goto ROLLBACK;
1217 }
1218
1219 // Save package ID
1220 unsigned long packages_id = sqlite3_last_insert_rowid(db->handle);
1221
1222 // This is done
1223 r = sqlite3_finalize(stmt);
1224 if (r == SQLITE_OK)
1225 stmt = NULL;
1226
1227 // Add dependencies
1228 r = pakfire_db_add_dependencies(db, packages_id, pkg);
1229 if (r)
1230 goto ROLLBACK;
1231
1232 // Add files
1233 r = pakfire_db_add_files(db, packages_id, archive);
1234 if (r)
1235 goto ROLLBACK;
1236
1237 // Add scriptlets
1238 r = pakfire_db_add_scriptlets(db, packages_id, archive);
1239 if (r)
1240 goto ROLLBACK;
1241
1242 // All done, commit!
1243 r = pakfire_db_commit(db);
1244 if (r)
1245 goto ROLLBACK;
1246
1247 return 0;
1248
1249 ROLLBACK:
1250 if (stmt)
1251 sqlite3_finalize(stmt);
1252
1253 pakfire_db_rollback(db);
1254
1255 return 1;
1256 }
1257
1258 PAKFIRE_EXPORT int pakfire_db_remove_package(struct pakfire_db* db, PakfirePackage pkg) {
1259 sqlite3_stmt* stmt = NULL;
1260 int r = 1;
1261
1262 // Fetch the package's UUID
1263 const char* uuid = pakfire_package_get_uuid(pkg);
1264 if (!uuid) {
1265 ERROR(db->pakfire, "Package has no UUID\n");
1266 goto ERROR;
1267 }
1268
1269 r = sqlite3_prepare_v2(db->handle,
1270 "DELETE FROM packages WHERE uuid = ?", -1, &stmt, NULL);
1271 if (r) {
1272 ERROR(db->pakfire, "Could not prepare SQL statement: %s\n",
1273 sqlite3_errmsg(db->handle));
1274 goto ERROR;
1275 }
1276
1277 // Bind UUID
1278 r = sqlite3_bind_text(stmt, 1, uuid, -1, NULL);
1279 if (r) {
1280 ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle));
1281 return 1;
1282 }
1283
1284 // Execute query
1285 do {
1286 r = sqlite3_step(stmt);
1287 } while (r == SQLITE_BUSY);
1288
1289 // Check if we have been successful
1290 if (r != SQLITE_DONE) {
1291 ERROR(db->pakfire, "Could not delete package %s\n", uuid);
1292 r = 1;
1293 goto ERROR;
1294 }
1295
1296 r = 0;
1297
1298 ERROR:
1299 if (stmt)
1300 sqlite3_finalize(stmt);
1301
1302 return r;
1303 }
1304
1305 struct pakfire_scriptlet* pakfire_db_get_scriptlet(struct pakfire_db* db,
1306 PakfirePackage pkg, pakfire_scriptlet_type type) {
1307 struct pakfire_scriptlet* scriptlet = NULL;
1308 sqlite3_stmt* stmt = NULL;
1309 int r = 1;
1310
1311 // Fetch the package's UUID
1312 const char* uuid = pakfire_package_get_uuid(pkg);
1313 if (!uuid) {
1314 ERROR(db->pakfire, "Package has no UUID\n");
1315 goto ERROR;
1316 }
1317
1318 const char* sql = "SELECT scriptlet.scriptlet FROM packages \
1319 JOIN scriptlets ON packages.id = scriptlets.pkg \
1320 WHERE packages.uuid = ? AND scriptlets.type = ?";
1321
1322 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
1323 if (r) {
1324 ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
1325 sql, sqlite3_errmsg(db->handle));
1326 goto ERROR;
1327 }
1328
1329 // Bind UUID
1330 r = sqlite3_bind_text(stmt, 1, uuid, -1, NULL);
1331 if (r) {
1332 ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle));
1333 goto ERROR;
1334 }
1335
1336 // Bind type
1337 const char* handle = pakfire_scriptlet_handle_from_type(type);
1338 if (!handle)
1339 goto ERROR;
1340
1341 r = sqlite3_bind_text(stmt, 2, handle, -1, NULL);
1342 if (r) {
1343 ERROR(db->pakfire, "Could not bind type: %s\n", sqlite3_errmsg(db->handle));
1344 goto ERROR;
1345 }
1346
1347 DEBUG(db->pakfire, "Searching for scriptlet for package %s of type %s\n", uuid, handle);
1348
1349 // Execute query
1350 do {
1351 r = sqlite3_step(stmt);
1352 } while (r == SQLITE_BUSY);
1353
1354 // We have some payload
1355 if (r == SQLITE_ROW) {
1356 const void* data = sqlite3_column_blob(stmt, 1);
1357 ssize_t size = sqlite3_column_bytes(stmt, 1);
1358
1359 // Create a scriptlet object
1360 scriptlet = pakfire_scriptlet_create(db->pakfire, data, size);
1361 if (!scriptlet)
1362 goto ERROR;
1363 }
1364
1365 ERROR:
1366 if (stmt)
1367 sqlite3_finalize(stmt);
1368
1369 return scriptlet;
1370 }
1371
1372 static int pakfire_db_load_package(struct pakfire_db* db, PakfireRepo repo, sqlite3_stmt* stmt) {
1373 PakfirePackage pkg = NULL;
1374 char* evr = NULL;
1375 int r = 1;
1376
1377 // Name
1378 const char* name = (const char*)sqlite3_column_text(stmt, 0);
1379 if (!name) {
1380 ERROR(db->pakfire, "Could not read name: %s\n", sqlite3_errmsg(db->handle));
1381 goto ERROR;
1382 }
1383
1384 // Epoch (INTEGER but cast as string)
1385 const char* epoch = (const char*)sqlite3_column_text(stmt, 1);
1386 if (!epoch) {
1387 ERROR(db->pakfire, "Could not read epoch: %s\n", sqlite3_errmsg(db->handle));
1388 goto ERROR;
1389 }
1390
1391 // Version
1392 const char* version = (const char*)sqlite3_column_text(stmt, 2);
1393 if (!version) {
1394 ERROR(db->pakfire, "Could not read version: %s\n", sqlite3_errmsg(db->handle));
1395 goto ERROR;
1396 }
1397
1398 // Release
1399 const char* release = (const char*)sqlite3_column_text(stmt, 3);
1400 if (!release) {
1401 ERROR(db->pakfire, "Could not read release: %s\n", sqlite3_errmsg(db->handle));
1402 goto ERROR;
1403 }
1404
1405 // Arch
1406 const char* arch = (const char*)sqlite3_column_text(stmt, 4);
1407 if (!arch) {
1408 ERROR(db->pakfire, "Could not read arch: %s\n", sqlite3_errmsg(db->handle));
1409 goto ERROR;
1410 }
1411
1412 // Join EVR together
1413 evr = pakfire_package_join_evr(epoch, version, release);
1414 if (!evr) {
1415 ERROR(db->pakfire, "Could not join evr\n");
1416 goto ERROR;
1417 }
1418
1419 // Create package
1420 pkg = pakfire_package_create(db->pakfire, repo, name, evr, arch);
1421 if (!pkg) {
1422 ERROR(db->pakfire, "Could not create package\n");
1423 goto ERROR;
1424 }
1425
1426 // Groups
1427 const char* groups = (const char*)sqlite3_column_text(stmt, 5);
1428 if (groups) {
1429 pakfire_package_set_groups(pkg, groups);
1430 }
1431
1432 // Filename
1433 const char* filename = (const char*)sqlite3_column_text(stmt, 6);
1434 if (filename) {
1435 pakfire_package_set_filename(pkg, filename);
1436 }
1437
1438 // Size
1439 size_t size = sqlite3_column_int64(stmt, 7);
1440 if (size) {
1441 pakfire_package_set_downloadsize(pkg, size);
1442 }
1443
1444 // Installed size
1445 size = sqlite3_column_int64(stmt, 8);
1446 if (size) {
1447 pakfire_package_set_installsize(pkg, size);
1448 }
1449
1450 // Hash 1
1451 const char* hash1 = (const char*)sqlite3_column_text(stmt, 9);
1452 if (hash1) {
1453 pakfire_package_set_checksum(pkg, hash1);
1454 }
1455
1456 // License
1457 const char* license = (const char*)sqlite3_column_text(stmt, 10);
1458 if (license) {
1459 pakfire_package_set_license(pkg, license);
1460 }
1461
1462 // Summary
1463 const char* summary = (const char*)sqlite3_column_text(stmt, 11);
1464 if (summary) {
1465 pakfire_package_set_summary(pkg, summary);
1466 }
1467
1468 // Description
1469 const char* description = (const char*)sqlite3_column_text(stmt, 12);
1470 if (description) {
1471 pakfire_package_set_description(pkg, description);
1472 }
1473
1474 // UUID
1475 const char* uuid = (const char*)sqlite3_column_text(stmt, 13);
1476 if (uuid) {
1477 pakfire_package_set_uuid(pkg, uuid);
1478 }
1479
1480 // Vendor
1481 const char* vendor = (const char*)sqlite3_column_text(stmt, 14);
1482 if (vendor) {
1483 pakfire_package_set_vendor(pkg, vendor);
1484 }
1485
1486 // Build Host
1487 const char* build_host = (const char*)sqlite3_column_text(stmt, 15);
1488 if (build_host) {
1489 pakfire_package_set_build_host(pkg, build_host);
1490 }
1491
1492 // Build Time
1493 time_t build_time = sqlite3_column_int64(stmt, 16);
1494 if (build_time) {
1495 pakfire_package_set_build_time(pkg, build_time);
1496 }
1497
1498 // Install Time
1499 time_t install_time = sqlite3_column_int64(stmt, 17);
1500 if (install_time) {
1501 pakfire_package_set_install_time(pkg, install_time);
1502 }
1503
1504 // Files
1505 const char* files = (const char*)sqlite3_column_text(stmt, 18);
1506 if (files) {
1507 r = pakfire_package_set_filelist_from_string(pkg, files);
1508 if (r)
1509 goto ERROR;
1510 }
1511
1512 // Dependencies
1513
1514 const struct dependency {
1515 unsigned int field;
1516 void (*func)(PakfirePackage pkg, PakfireRelationList list);
1517 } dependencies[] = {
1518 { 19, pakfire_package_set_provides },
1519 { 20, pakfire_package_set_prerequires },
1520 { 21, pakfire_package_set_requires },
1521 { 22, pakfire_package_set_conflicts },
1522 { 23, pakfire_package_set_obsoletes },
1523 { 24, pakfire_package_set_recommends },
1524 { 25, pakfire_package_set_suggests },
1525 { 26, pakfire_package_set_supplements },
1526 { 27, pakfire_package_set_enhances },
1527 { 0, NULL },
1528 };
1529
1530 for (const struct dependency* deps = dependencies; deps->field; deps++) {
1531 const char* relations = (const char*)sqlite3_column_text(stmt, deps->field);
1532 if (relations) {
1533 PakfireRelationList list;
1534
1535 r = pakfire_relationlist_create_from_string(&list, db->pakfire, relations);
1536 if (r)
1537 goto ERROR;
1538
1539 deps->func(pkg, list);
1540 pakfire_relationlist_unref(list);
1541 }
1542 }
1543
1544 // Success
1545 r = 0;
1546
1547 ERROR:
1548 if (evr)
1549 free(evr);
1550
1551 if (pkg)
1552 pakfire_package_unref(pkg);
1553
1554 return r;
1555 }
1556
1557 int pakfire_db_load(struct pakfire_db* db, PakfireRepo repo) {
1558 sqlite3_stmt* stmt = NULL;
1559 int r = 1;
1560
1561 DEBUG(db->pakfire, "Loading package database...\n");
1562
1563 // Save starting time
1564 clock_t t_start = clock();
1565 clock_t t_end;
1566
1567 const char* sql =
1568 "SELECT "
1569 "name, epoch, version, release, arch, groups, filename, size, inst_size, "
1570 "hash1, license, summary, description, uuid, vendor, build_host, build_time, "
1571 "strftime('%s', installed) AS installed, "
1572 "("
1573 "SELECT group_concat(name, '\n') FROM files WHERE files.pkg = packages.id"
1574 ") AS files, "
1575 "("
1576 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1577 "WHERE d.pkg = packages.id AND d.type = 'provides'"
1578 ") AS provides, "
1579 "("
1580 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1581 "WHERE d.pkg = packages.id AND d.type = 'prerequires'"
1582 ") AS prerequires, "
1583 "("
1584 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1585 "WHERE d.pkg = packages.id AND d.type = 'requires'"
1586 ") AS requires, "
1587 "("
1588 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1589 "WHERE d.pkg = packages.id AND d.type = 'conflicts'"
1590 ") AS conflicts, "
1591 "("
1592 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1593 "WHERE d.pkg = packages.id AND d.type = 'obsoletes'"
1594 ") AS obsoletes, "
1595 "("
1596 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1597 "WHERE d.pkg = packages.id AND d.type = 'recommends'"
1598 ") AS recommends, "
1599 "("
1600 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1601 "WHERE d.pkg = packages.id AND d.type = 'suggests'"
1602 ") AS suggests, "
1603 "("
1604 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1605 "WHERE d.pkg = packages.id AND d.type = 'supplements'"
1606 ") AS supplements, "
1607 "("
1608 "SELECT group_concat(dependency, '\n') FROM dependencies d "
1609 "WHERE d.pkg = packages.id AND d.type = 'enhances'"
1610 ") AS enhances "
1611 "FROM "
1612 "packages"
1613 ";";
1614
1615 // Prepare the statement
1616 r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL);
1617 if (r) {
1618 ERROR(db->pakfire, "Could not prepare SQL statement: %s %s\n",
1619 sql, sqlite3_errmsg(db->handle));
1620 goto ERROR;
1621 }
1622
1623 for (;;) {
1624 // Execute query
1625 r = sqlite3_step(stmt);
1626
1627 switch (r) {
1628 // Retry if the database was busy
1629 case SQLITE_BUSY:
1630 continue;
1631
1632 // Read a row
1633 case SQLITE_ROW:
1634 r = pakfire_db_load_package(db, repo, stmt);
1635 if (r)
1636 goto ERROR;
1637 break;
1638
1639 // All rows have been processed
1640 case SQLITE_DONE:
1641 goto END;
1642
1643 // Go to error in any other cases
1644 default:
1645 goto ERROR;
1646 }
1647 }
1648
1649 END:
1650 // Save time when we finished
1651 t_end = clock();
1652
1653 DEBUG(db->pakfire, "Loading package database completed in %.4fms\n",
1654 (double)(t_end - t_start) * 1000 / CLOCKS_PER_SEC);
1655
1656 // Internalize repository
1657 pakfire_repo_internalize(repo);
1658
1659 // All done
1660 r = 0;
1661
1662 ERROR:
1663 if (r)
1664 ERROR(db->pakfire, "Failed reading package database: %d\n", r);
1665
1666 if (stmt)
1667 sqlite3_finalize(stmt);
1668
1669 return r;
1670 }