]>
Commit | Line | Data |
---|---|---|
33d55ab4 MT |
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 | ||
26affd69 MT |
24 | #include <sqlite3.h> |
25 | ||
f5c77233 | 26 | #include <pakfire/archive.h> |
33d55ab4 | 27 | #include <pakfire/db.h> |
f5c77233 | 28 | #include <pakfire/file.h> |
33d55ab4 | 29 | #include <pakfire/logging.h> |
e49b93d1 | 30 | #include <pakfire/package.h> |
18bf891d MT |
31 | #include <pakfire/pakfire.h> |
32 | #include <pakfire/private.h> | |
e49b93d1 | 33 | #include <pakfire/repo.h> |
33d55ab4 MT |
34 | #include <pakfire/types.h> |
35 | #include <pakfire/util.h> | |
36 | ||
0cb487ff MT |
37 | #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db" |
38 | ||
5ba550fe | 39 | #define CURRENT_SCHEMA 8 |
c745fb2d MT |
40 | #define SCHEMA_MIN_SUP 7 |
41 | ||
33d55ab4 MT |
42 | struct pakfire_db { |
43 | Pakfire pakfire; | |
44 | int nrefs; | |
77a4b3a3 MT |
45 | |
46 | int mode; | |
26affd69 MT |
47 | |
48 | sqlite3* handle; | |
c745fb2d | 49 | int schema; |
33d55ab4 MT |
50 | }; |
51 | ||
9c5938ea MT |
52 | static void logging_callback(void* data, int r, const char* msg) { |
53 | Pakfire pakfire = (Pakfire)data; | |
54 | ||
55 | ERROR(pakfire, "Database Error: %s: %s\n", | |
56 | sqlite3_errstr(r), msg); | |
57 | } | |
58 | ||
0076c50d MT |
59 | static int pakfire_db_execute(struct pakfire_db* db, const char* stmt) { |
60 | int r; | |
c745fb2d MT |
61 | |
62 | DEBUG(db->pakfire, "Executing database query: %s\n", stmt); | |
0076c50d MT |
63 | |
64 | do { | |
c745fb2d | 65 | r = sqlite3_exec(db->handle, stmt, NULL, NULL, NULL); |
0076c50d MT |
66 | } while (r == SQLITE_BUSY); |
67 | ||
c745fb2d MT |
68 | // Log any errors |
69 | if (r) { | |
70 | ERROR(db->pakfire, "Database query failed: %s\n", sqlite3_errmsg(db->handle)); | |
71 | } | |
72 | ||
25753290 MT |
73 | return r; |
74 | } | |
75 | ||
c745fb2d MT |
76 | static int pakfire_db_begin_transaction(struct pakfire_db* db) { |
77 | return pakfire_db_execute(db, "BEGIN TRANSACTION"); | |
78 | } | |
79 | ||
80 | static int pakfire_db_commit(struct pakfire_db* db) { | |
81 | return pakfire_db_execute(db, "COMMIT"); | |
82 | } | |
83 | ||
84 | static int pakfire_db_rollback(struct pakfire_db* db) { | |
85 | return pakfire_db_execute(db, "ROLLBACK"); | |
86 | } | |
87 | ||
25753290 MT |
88 | /* |
89 | This function performs any fast optimization and tries to truncate the WAL log file | |
90 | to keep the database as compact as possible on disk. | |
91 | */ | |
92 | static void pakfire_db_optimize(struct pakfire_db* db) { | |
44d392ef | 93 | pakfire_db_execute(db, "PRAGMA optimize"); |
25753290 | 94 | pakfire_db_execute(db, "PRAGMA wal_checkpoint = TRUNCATE"); |
0076c50d MT |
95 | } |
96 | ||
26affd69 MT |
97 | static void pakfire_db_free(struct pakfire_db* db) { |
98 | DEBUG(db->pakfire, "Releasing database at %p\n", db); | |
99 | ||
26affd69 | 100 | if (db->handle) { |
25753290 MT |
101 | // Optimize the database before it is being closed |
102 | pakfire_db_optimize(db); | |
103 | ||
104 | // Close database handle | |
26affd69 MT |
105 | int r = sqlite3_close(db->handle); |
106 | if (r != SQLITE_OK) { | |
107 | ERROR(db->pakfire, "Could not close database handle: %s\n", | |
108 | sqlite3_errmsg(db->handle)); | |
109 | } | |
110 | } | |
111 | ||
112 | pakfire_unref(db->pakfire); | |
113 | ||
114 | pakfire_free(db); | |
115 | } | |
116 | ||
c745fb2d MT |
117 | static sqlite3_value* pakfire_db_get(struct pakfire_db* db, const char* key) { |
118 | sqlite3_stmt* stmt = NULL; | |
119 | sqlite3_value* val = NULL; | |
120 | int r; | |
121 | ||
122 | const char* sql = "SELECT val FROM settings WHERE key = ?"; | |
123 | ||
124 | // Prepare the statement | |
125 | r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL); | |
126 | if (r != SQLITE_OK) { | |
127 | //ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n", | |
128 | // sql, sqlite3_errmsg(db->handle)); | |
129 | return NULL; | |
130 | } | |
131 | ||
132 | // Bind key | |
133 | r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL); | |
134 | if (r != SQLITE_OK) { | |
135 | ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle)); | |
136 | goto ERROR; | |
137 | } | |
138 | ||
139 | // Execute the statement | |
140 | do { | |
141 | r = sqlite3_step(stmt); | |
142 | } while (r == SQLITE_BUSY); | |
143 | ||
f168dd0c MT |
144 | // We should have read a row |
145 | if (r != SQLITE_ROW) | |
146 | goto ERROR; | |
147 | ||
c745fb2d | 148 | // Read value |
f168dd0c | 149 | val = sqlite3_column_value(stmt, 0); |
c745fb2d MT |
150 | if (!val) { |
151 | ERROR(db->pakfire, "Could not read value\n"); | |
152 | goto ERROR; | |
153 | } | |
154 | ||
155 | // Copy value onto the heap | |
156 | val = sqlite3_value_dup(val); | |
157 | ||
158 | ERROR: | |
159 | if (stmt) | |
160 | sqlite3_finalize(stmt); | |
161 | ||
162 | return val; | |
163 | } | |
164 | ||
165 | static int pakfire_db_set_int(struct pakfire_db* db, const char* key, int val) { | |
166 | sqlite3_stmt* stmt = NULL; | |
167 | int r; | |
168 | ||
169 | const char* sql = "INSERT INTO settings(key, val) VALUES(?, ?) \ | |
170 | ON CONFLICT (key) DO UPDATE SET val = excluded.val"; | |
171 | ||
172 | // Prepare statement | |
173 | r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL); | |
174 | if (r != SQLITE_OK) { | |
175 | ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n", | |
176 | sql, sqlite3_errmsg(db->handle)); | |
177 | return 1; | |
178 | } | |
179 | ||
180 | // Bind key | |
181 | r = sqlite3_bind_text(stmt, 1, key, strlen(key), NULL); | |
182 | if (r != SQLITE_OK) { | |
183 | ERROR(db->pakfire, "Could not bind key: %s\n", sqlite3_errmsg(db->handle)); | |
184 | goto ERROR; | |
185 | } | |
186 | ||
187 | // Bind val | |
188 | r = sqlite3_bind_int64(stmt, 2, val); | |
189 | if (r != SQLITE_OK) { | |
190 | ERROR(db->pakfire, "Could not bind val: %s\n", sqlite3_errmsg(db->handle)); | |
191 | goto ERROR; | |
192 | } | |
193 | ||
194 | // Execute the statement | |
195 | do { | |
196 | r = sqlite3_step(stmt); | |
197 | } while (r == SQLITE_BUSY); | |
198 | ||
199 | // Set return code | |
200 | r = (r == SQLITE_OK); | |
201 | ||
202 | ERROR: | |
203 | if (stmt) | |
204 | sqlite3_finalize(stmt); | |
205 | ||
206 | return r; | |
207 | } | |
208 | ||
209 | static int pakfire_db_get_schema(struct pakfire_db* db) { | |
210 | sqlite3_value* value = pakfire_db_get(db, "schema"); | |
211 | if (!value) | |
f168dd0c | 212 | return -1; |
c745fb2d MT |
213 | |
214 | int schema = sqlite3_value_int64(value); | |
215 | sqlite3_value_free(value); | |
216 | ||
217 | DEBUG(db->pakfire, "Database has schema version %d\n", schema); | |
218 | ||
219 | return schema; | |
220 | } | |
221 | ||
222 | static int pakfire_db_create_schema(struct pakfire_db* db) { | |
223 | int r; | |
224 | ||
225 | // Create settings table | |
226 | r = pakfire_db_execute(db, "CREATE TABLE IF NOT EXISTS settings(key TEXT, val TEXT)"); | |
227 | if (r) | |
228 | return 1; | |
229 | ||
230 | // settings: Add a unique index on key | |
231 | r = pakfire_db_execute(db, "CREATE UNIQUE INDEX IF NOT EXISTS settings_key ON settings(key)"); | |
232 | if (r) | |
233 | return 1; | |
234 | ||
704b3993 MT |
235 | // Create packages table |
236 | r = pakfire_db_execute(db, | |
237 | "CREATE TABLE IF NOT EXISTS packages(" | |
238 | "id INTEGER PRIMARY KEY, " | |
239 | "name TEXT, " | |
240 | "epoch INTEGER, " | |
241 | "version TEXT, " | |
242 | "release TEXT, " | |
243 | "arch TEXT, " | |
244 | "groups TEXT, " | |
245 | "filename TEXT, " | |
246 | "size INTEGER, " | |
247 | "inst_size INTEGER, " | |
248 | "hash1 TEXT, " | |
249 | "license TEXT, " | |
250 | "summary TEXT, " | |
251 | "description TEXT, " | |
252 | "uuid TEXT, " | |
253 | "vendor TEXT, " | |
704b3993 MT |
254 | "build_host TEXT, " |
255 | "build_date TEXT, " | |
256 | "build_time INTEGER, " | |
257 | "installed INTEGER, " | |
258 | "reason TEXT, " | |
259 | "repository TEXT" | |
260 | ")"); | |
261 | if (r) | |
262 | return 1; | |
263 | ||
264 | // packages: Create index to find package by name | |
265 | r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS packages_name ON packages(name)"); | |
266 | if (r) | |
267 | return 1; | |
268 | ||
2359ca14 MT |
269 | // Create dependencies table |
270 | r = pakfire_db_execute(db, | |
271 | "CREATE TABLE IF NOT EXISTS dependencies(" | |
272 | "pkg INTEGER, " | |
273 | "type TEXT, " | |
46257c5f MT |
274 | "dependency TEXT, " |
275 | "FOREIGN KEY (pkg) REFERENCES packages(id)" | |
2359ca14 MT |
276 | ")"); |
277 | if (r) | |
278 | return r; | |
279 | ||
280 | // dependencies: Add index over packages | |
281 | r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS dependencies_pkg_index ON dependencies(pkg)"); | |
282 | if (r) | |
283 | return r; | |
284 | ||
68036506 MT |
285 | // Create files table |
286 | r = pakfire_db_execute(db, | |
287 | "CREATE TABLE IF NOT EXISTS files(" | |
288 | "id INTEGER PRIMARY KEY, " | |
289 | "name TEXT, " | |
290 | "pkg INTEGER, " | |
291 | "size INTEGER, " | |
292 | "type INTEGER, " | |
293 | "config INTEGER, " | |
294 | "datafile INTEGER, " | |
295 | "mode INTEGER, " | |
296 | "user TEXT, " | |
297 | "'group' TEXT, " | |
298 | "hash1 TEXT, " | |
299 | "mtime INTEGER, " | |
46257c5f MT |
300 | "capabilities TEXT, " |
301 | "FOREIGN KEY (pkg) REFERENCES packages(id)" | |
68036506 MT |
302 | ")"); |
303 | if (r) | |
304 | return 1; | |
305 | ||
306 | // files: Add index over packages | |
307 | r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS files_pkg_index ON files(pkg)"); | |
308 | if (r) | |
309 | return 1; | |
310 | ||
d83414aa MT |
311 | // Create scriptlets table |
312 | r = pakfire_db_execute(db, | |
313 | "CREATE TABLE IF NOT EXISTS scriptlets(" | |
314 | "id INTEGER PRIMARY KEY, " | |
315 | "pkg INTEGER, " | |
316 | "action TEXT, " | |
46257c5f MT |
317 | "scriptlet TEXT, " |
318 | "FOREIGN KEY (pkg) REFERENCES packages(id)" | |
d83414aa MT |
319 | ")"); |
320 | if (r) | |
321 | return 1; | |
322 | ||
323 | // scriptlets: Add index over packages | |
324 | r = pakfire_db_execute(db, "CREATE INDEX IF NOT EXISTS scriptlets_pkg_index ON scriptlets(pkg)"); | |
325 | if (r) | |
326 | return 1; | |
327 | ||
c745fb2d MT |
328 | return 0; |
329 | } | |
330 | ||
46257c5f | 331 | static int pakfire_db_migrate_to_schema_8(struct pakfire_db* db) { |
5ba550fe MT |
332 | // packages: Drop build_id column |
333 | ||
46257c5f MT |
334 | // Add foreign keys |
335 | // TODO sqlite doesn't support adding foreign keys to existing tables and so we would | |
336 | // need to recreate the whole table and rename it afterwards. Annoying. | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
c745fb2d MT |
341 | static int pakfire_db_migrate_schema(struct pakfire_db* db) { |
342 | int r; | |
343 | ||
344 | while (db->schema < CURRENT_SCHEMA) { | |
345 | // Begin a new transaction | |
346 | r = pakfire_db_begin_transaction(db); | |
347 | if (r) | |
348 | goto ROLLBACK; | |
349 | ||
350 | switch (db->schema) { | |
351 | // No schema exists | |
f168dd0c | 352 | case -1: |
c745fb2d MT |
353 | r = pakfire_db_create_schema(db); |
354 | if (r) | |
355 | goto ROLLBACK; | |
356 | ||
357 | db->schema = CURRENT_SCHEMA; | |
358 | break; | |
359 | ||
46257c5f MT |
360 | case 7: |
361 | r = pakfire_db_migrate_to_schema_8(db); | |
362 | if (r) | |
363 | goto ROLLBACK; | |
364 | ||
365 | db->schema++; | |
366 | break; | |
367 | ||
c745fb2d MT |
368 | default: |
369 | ERROR(db->pakfire, "Cannot migrate database from schema %d\n", db->schema); | |
370 | goto ROLLBACK; | |
371 | } | |
372 | ||
373 | // Update the schema version | |
374 | r = pakfire_db_set_int(db, "schema", CURRENT_SCHEMA); | |
375 | if (r) | |
376 | goto ROLLBACK; | |
377 | ||
378 | // All done, commit! | |
379 | r = pakfire_db_commit(db); | |
380 | if (r) | |
381 | goto ROLLBACK; | |
382 | } | |
383 | ||
384 | return 0; | |
385 | ||
386 | ROLLBACK: | |
387 | pakfire_db_rollback(db); | |
388 | ||
389 | return 1; | |
390 | } | |
391 | ||
9c5938ea | 392 | static int pakfire_db_setup(struct pakfire_db* db) { |
25753290 MT |
393 | int r; |
394 | ||
9c5938ea MT |
395 | // Setup logging |
396 | sqlite3_config(SQLITE_CONFIG_LOG, logging_callback, db->pakfire); | |
397 | ||
46257c5f MT |
398 | // Enable foreign keys |
399 | pakfire_db_execute(db, "PRAGMA foreign_keys = ON"); | |
400 | ||
0076c50d MT |
401 | // Make LIKE case-sensitive |
402 | pakfire_db_execute(db, "PRAGMA case_sensitive_like = ON"); | |
403 | ||
c745fb2d MT |
404 | // Fetch the current schema |
405 | db->schema = pakfire_db_get_schema(db); | |
406 | ||
407 | // Check if the schema is recent enough | |
408 | if (db->schema > 0 && db->schema < SCHEMA_MIN_SUP) { | |
409 | ERROR(db->pakfire, "Database schema %d is not supported by this version of Pakfire\n", | |
410 | db->schema); | |
411 | return 1; | |
412 | } | |
413 | ||
9c5938ea MT |
414 | // Done when not in read-write mode |
415 | if (db->mode != PAKFIRE_DB_READWRITE) | |
416 | return 0; | |
417 | ||
0076c50d MT |
418 | // Disable secure delete |
419 | pakfire_db_execute(db, "PRAGMA secure_delete = OFF"); | |
420 | ||
25753290 MT |
421 | // Set database journal to WAL |
422 | r = pakfire_db_execute(db, "PRAGMA journal_mode = WAL"); | |
423 | if (r != SQLITE_OK) { | |
424 | ERROR(db->pakfire, "Could not set journal mode to WAL: %s\n", | |
425 | sqlite3_errmsg(db->handle)); | |
426 | return 1; | |
427 | } | |
428 | ||
429 | // Disable autocheckpoint | |
430 | r = sqlite3_wal_autocheckpoint(db->handle, 0); | |
431 | if (r != SQLITE_OK) { | |
432 | ERROR(db->pakfire, "Could not disable autocheckpoint: %s\n", | |
433 | sqlite3_errmsg(db->handle)); | |
434 | return 1; | |
435 | } | |
436 | ||
c745fb2d MT |
437 | // Create or migrate schema |
438 | r = pakfire_db_migrate_schema(db); | |
439 | if (r) | |
440 | return r; | |
9c5938ea MT |
441 | |
442 | return 0; | |
443 | } | |
444 | ||
77a4b3a3 | 445 | PAKFIRE_EXPORT int pakfire_db_open(struct pakfire_db** db, Pakfire pakfire, int flags) { |
26affd69 MT |
446 | int r = 1; |
447 | ||
33d55ab4 MT |
448 | struct pakfire_db* o = pakfire_calloc(1, sizeof(*o)); |
449 | if (!o) | |
450 | return -ENOMEM; | |
451 | ||
452 | DEBUG(pakfire, "Allocated database at %p\n", o); | |
453 | ||
454 | o->pakfire = pakfire_ref(pakfire); | |
455 | o->nrefs = 1; | |
456 | ||
26affd69 MT |
457 | int sqlite3_flags = 0; |
458 | ||
459 | // Store mode & forward it to sqlite3 | |
460 | if (flags & PAKFIRE_DB_READWRITE) { | |
77a4b3a3 | 461 | o->mode = PAKFIRE_DB_READWRITE; |
26affd69 MT |
462 | sqlite3_flags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; |
463 | } else { | |
77a4b3a3 | 464 | o->mode = PAKFIRE_DB_READONLY; |
26affd69 MT |
465 | sqlite3_flags |= SQLITE_OPEN_READONLY; |
466 | } | |
467 | ||
468 | // Make the filename | |
469 | char* path = pakfire_make_path(o->pakfire, DATABASE_PATH); | |
470 | if (!path) | |
471 | goto END; | |
472 | ||
473 | // Try to open the sqlite3 database file | |
474 | r = sqlite3_open_v2(path, &o->handle, sqlite3_flags, NULL); | |
475 | if (r != SQLITE_OK) { | |
476 | ERROR(pakfire, "Could not open database %s: %s\n", | |
477 | path, sqlite3_errmsg(o->handle)); | |
478 | ||
479 | r = 1; | |
480 | goto END; | |
481 | } | |
77a4b3a3 | 482 | |
9c5938ea MT |
483 | // Setup the database |
484 | r = pakfire_db_setup(o); | |
485 | if (r) | |
486 | goto END; | |
487 | ||
33d55ab4 | 488 | *db = o; |
26affd69 MT |
489 | r = 0; |
490 | ||
491 | END: | |
492 | if (r) | |
493 | pakfire_db_free(o); | |
33d55ab4 | 494 | |
26affd69 MT |
495 | if (path) |
496 | free(path); | |
497 | ||
498 | return r; | |
33d55ab4 MT |
499 | } |
500 | ||
18bf891d | 501 | PAKFIRE_EXPORT struct pakfire_db* pakfire_db_ref(struct pakfire_db* db) { |
33d55ab4 MT |
502 | db->nrefs++; |
503 | ||
504 | return db; | |
505 | } | |
506 | ||
18bf891d | 507 | PAKFIRE_EXPORT struct pakfire_db* pakfire_db_unref(struct pakfire_db* db) { |
33d55ab4 MT |
508 | if (--db->nrefs > 0) |
509 | return db; | |
510 | ||
511 | pakfire_db_free(db); | |
512 | ||
513 | return NULL; | |
514 | } | |
eafbe2ce | 515 | |
a1571786 MT |
516 | static unsigned long pakfire_db_integrity_check(struct pakfire_db* db) { |
517 | sqlite3_stmt* stmt = NULL; | |
518 | int r; | |
519 | ||
520 | r = sqlite3_prepare_v2(db->handle, "PRAGMA integrity_check", -1, &stmt, NULL); | |
521 | if (r) { | |
522 | ERROR(db->pakfire, "Could not prepare integrity check: %s\n", | |
523 | sqlite3_errmsg(db->handle)); | |
524 | return 1; | |
525 | } | |
526 | ||
527 | // Count any errors | |
528 | unsigned long errors = 0; | |
529 | ||
530 | while (1) { | |
531 | do { | |
532 | r = sqlite3_step(stmt); | |
533 | } while (r == SQLITE_BUSY); | |
534 | ||
535 | if (r == SQLITE_ROW) { | |
536 | const char* error = (const char*)sqlite3_column_text(stmt, 0); | |
537 | ||
538 | // If the message is "ok", the database has passed the check | |
539 | if (strcmp(error, "ok") == 0) | |
540 | continue; | |
541 | ||
542 | // Increment error counter | |
543 | errors++; | |
544 | ||
545 | // Log the message | |
546 | ERROR(db->pakfire, "%s\n", error); | |
547 | ||
548 | // Break on anything else | |
549 | } else | |
550 | break; | |
551 | } | |
552 | ||
553 | sqlite3_finalize(stmt); | |
554 | ||
555 | if (errors) | |
556 | ERROR(db->pakfire, "Database integrity check failed\n"); | |
557 | else | |
558 | INFO(db->pakfire, "Database integrity check passed\n"); | |
559 | ||
560 | return errors; | |
561 | } | |
562 | ||
563 | static unsigned long pakfire_db_foreign_key_check(struct pakfire_db* db) { | |
564 | sqlite3_stmt* stmt = NULL; | |
565 | int r; | |
566 | ||
567 | r = sqlite3_prepare_v2(db->handle, "PRAGMA foreign_key_check", -1, &stmt, NULL); | |
568 | if (r) { | |
569 | ERROR(db->pakfire, "Could not prepare foreign key check: %s\n", | |
570 | sqlite3_errmsg(db->handle)); | |
571 | return 1; | |
572 | } | |
573 | ||
574 | // Count any errors | |
575 | unsigned long errors = 0; | |
576 | ||
577 | while (1) { | |
578 | do { | |
579 | r = sqlite3_step(stmt); | |
580 | } while (r == SQLITE_BUSY); | |
581 | ||
582 | if (r == SQLITE_ROW) { | |
583 | const unsigned char* table = sqlite3_column_text(stmt, 0); | |
584 | unsigned long rowid = sqlite3_column_int64(stmt, 1); | |
585 | const unsigned char* foreign_table = sqlite3_column_text(stmt, 2); | |
586 | unsigned long foreign_rowid = sqlite3_column_int64(stmt, 3); | |
587 | ||
588 | // Increment error counter | |
589 | errors++; | |
590 | ||
591 | // Log the message | |
592 | ERROR(db->pakfire, "Foreign key violation found in %s, row %lu: " | |
593 | "%lu does not exist in table %s\n", table, rowid, foreign_rowid, foreign_table); | |
594 | ||
595 | // Break on anything else | |
596 | } else | |
597 | break; | |
598 | } | |
599 | ||
600 | sqlite3_finalize(stmt); | |
601 | ||
602 | if (errors) | |
603 | ERROR(db->pakfire, "Foreign key check failed\n"); | |
604 | else | |
605 | INFO(db->pakfire, "Foreign key check passed\n"); | |
606 | ||
607 | return errors; | |
608 | } | |
609 | ||
610 | /* | |
611 | This function performs an integrity check of the database | |
612 | */ | |
613 | PAKFIRE_EXPORT int pakfire_db_check(struct pakfire_db* db) { | |
614 | int r; | |
615 | ||
616 | // Perform integrity check | |
617 | r = pakfire_db_integrity_check(db); | |
618 | if (r) | |
619 | return 1; | |
620 | ||
621 | // Perform foreign key check | |
622 | r = pakfire_db_foreign_key_check(db); | |
623 | if (r) | |
624 | return 1; | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
f5c77233 MT |
629 | static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, PakfireArchive archive) { |
630 | sqlite3_stmt* stmt = NULL; | |
631 | int r; | |
632 | ||
633 | // Get the filelist from the archive | |
634 | PakfireFile file = pakfire_archive_get_filelist(archive); | |
635 | if (!file) { | |
636 | ERROR(db->pakfire, "Could not fetch filelist from archive\n"); | |
637 | return 1; | |
638 | } | |
639 | ||
640 | const char* sql = "INSERT INTO files(pkg, name, size, type, config, datafile, mode, " | |
641 | "user, 'group', hash1, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
642 | ||
643 | // Prepare the statement | |
644 | r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL); | |
645 | if (r) { | |
646 | ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n", | |
647 | sql, sqlite3_errmsg(db->handle)); | |
648 | goto END; | |
649 | } | |
650 | ||
651 | while (file) { | |
652 | // Bind package ID | |
653 | r = sqlite3_bind_int64(stmt, 1, id); | |
654 | if (r) { | |
655 | ERROR(db->pakfire, "Could not bind id: %s\n", sqlite3_errmsg(db->handle)); | |
656 | goto END; | |
657 | } | |
658 | ||
659 | // Bind name | |
660 | const char* name = pakfire_file_get_name(file); | |
661 | ||
662 | r = sqlite3_bind_text(stmt, 2, name, -1, NULL); | |
663 | if (r) { | |
664 | ERROR(db->pakfire, "Could not bind name: %s\n", sqlite3_errmsg(db->handle)); | |
665 | goto END; | |
666 | } | |
667 | ||
668 | // Bind size | |
669 | size_t size = pakfire_file_get_size(file); | |
670 | ||
671 | r = sqlite3_bind_int64(stmt, 3, size); | |
672 | if (r) { | |
673 | ERROR(db->pakfire, "Could not bind size: %s\n", sqlite3_errmsg(db->handle)); | |
674 | goto END; | |
675 | } | |
676 | ||
677 | // Bind type - XXX this is char which isn't very helpful | |
678 | //char type = pakfire_file_get_type(file); | |
679 | ||
680 | r = sqlite3_bind_null(stmt, 4); | |
681 | if (r) { | |
682 | ERROR(db->pakfire, "Could not bind type: %s\n", sqlite3_errmsg(db->handle)); | |
683 | goto END; | |
684 | } | |
685 | ||
686 | // Bind config - XXX TODO | |
687 | r = sqlite3_bind_null(stmt, 5); | |
688 | if (r) { | |
689 | ERROR(db->pakfire, "Could not bind config: %s\n", sqlite3_errmsg(db->handle)); | |
690 | goto END; | |
691 | } | |
692 | ||
693 | // Bind datafile - XXX TODO | |
694 | r = sqlite3_bind_null(stmt, 6); | |
695 | if (r) { | |
696 | ERROR(db->pakfire, "Could not bind datafile: %s\n", sqlite3_errmsg(db->handle)); | |
697 | goto END; | |
698 | } | |
699 | ||
700 | // Bind mode | |
701 | mode_t mode = pakfire_file_get_mode(file); | |
702 | ||
703 | r = sqlite3_bind_int64(stmt, 7, mode); | |
704 | if (r) { | |
705 | ERROR(db->pakfire, "Could not bind mode: %s\n", sqlite3_errmsg(db->handle)); | |
706 | goto END; | |
707 | } | |
708 | ||
709 | // Bind user | |
710 | const char* user = pakfire_file_get_user(file); | |
711 | ||
712 | r = sqlite3_bind_text(stmt, 8, user, -1, NULL); | |
713 | if (r) { | |
714 | ERROR(db->pakfire, "Could not bind user: %s\n", sqlite3_errmsg(db->handle)); | |
715 | goto END; | |
716 | } | |
717 | ||
718 | // Bind group | |
719 | const char* group = pakfire_file_get_group(file); | |
720 | ||
721 | r = sqlite3_bind_text(stmt, 9, group, -1, NULL); | |
722 | if (r) { | |
723 | ERROR(db->pakfire, "Could not bind group: %s\n", sqlite3_errmsg(db->handle)); | |
724 | goto END; | |
725 | } | |
726 | ||
727 | // Bind hash1 | |
728 | const char* chksum = pakfire_file_get_chksum(file); | |
729 | ||
730 | r = sqlite3_bind_text(stmt, 10, chksum, -1, NULL); | |
731 | if (r) { | |
732 | ERROR(db->pakfire, "Could not bind hash1: %s\n", sqlite3_errmsg(db->handle)); | |
733 | goto END; | |
734 | } | |
735 | ||
736 | // Bind mtime | |
737 | time_t mtime = pakfire_file_get_time(file); | |
738 | ||
739 | r = sqlite3_bind_int64(stmt, 11, mtime); | |
740 | if (r) { | |
741 | ERROR(db->pakfire, "Could not bind mtime: %s\n", sqlite3_errmsg(db->handle)); | |
742 | goto END; | |
743 | } | |
744 | ||
745 | // Bind capabilities - XXX TODO | |
746 | r = sqlite3_bind_null(stmt, 12); | |
747 | if (r) { | |
748 | ERROR(db->pakfire, "Could not bind capabilities: %s\n", sqlite3_errmsg(db->handle)); | |
749 | goto END; | |
750 | } | |
751 | ||
752 | // Execute query | |
753 | do { | |
754 | r = sqlite3_step(stmt); | |
755 | } while (r == SQLITE_BUSY); | |
756 | ||
757 | // Move on to next file | |
758 | file = pakfire_file_get_next(file); | |
759 | ||
760 | // Reset bound values | |
761 | sqlite3_reset(stmt); | |
762 | } | |
763 | ||
764 | // All okay | |
765 | r = 0; | |
766 | ||
767 | END: | |
768 | if (stmt) | |
769 | sqlite3_finalize(stmt); | |
770 | ||
771 | return r; | |
772 | } | |
773 | ||
774 | PAKFIRE_EXPORT int pakfire_db_add_package(struct pakfire_db* db, | |
775 | PakfirePackage pkg, PakfireArchive archive) { | |
e49b93d1 MT |
776 | sqlite3_stmt* stmt = NULL; |
777 | int r; | |
778 | ||
779 | // Begin a new transaction | |
780 | r = pakfire_db_begin_transaction(db); | |
781 | if (r) | |
782 | goto ROLLBACK; | |
783 | ||
784 | const char* sql = "INSERT INTO packages(name, epoch, version, release, arch, groups, " | |
785 | "filename, size, inst_size, hash1, license, summary, description, uuid, vendor, " | |
1ca6a13d MT |
786 | "build_host, build_time, installed, repository, reason) VALUES(?, ?, " |
787 | "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)"; | |
e49b93d1 MT |
788 | |
789 | // Prepare the statement | |
790 | r = sqlite3_prepare_v2(db->handle, sql, strlen(sql), &stmt, NULL); | |
791 | if (r != SQLITE_OK) { | |
792 | ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n", | |
793 | sql, sqlite3_errmsg(db->handle)); | |
794 | goto ROLLBACK; | |
795 | } | |
796 | ||
797 | // Bind name | |
798 | const char* name = pakfire_package_get_name(pkg); | |
799 | ||
800 | r = sqlite3_bind_text(stmt, 1, name, -1, NULL); | |
801 | if (r) { | |
802 | ERROR(db->pakfire, "Could not bind name: %s\n", sqlite3_errmsg(db->handle)); | |
803 | goto ROLLBACK; | |
804 | } | |
805 | ||
806 | // Bind epoch | |
807 | unsigned long epoch = pakfire_package_get_epoch(pkg); | |
808 | ||
809 | r = sqlite3_bind_int64(stmt, 2, epoch); | |
810 | if (r) { | |
811 | ERROR(db->pakfire, "Could not bind epoch: %s\n", sqlite3_errmsg(db->handle)); | |
812 | goto ROLLBACK; | |
813 | } | |
814 | ||
815 | // Bind version | |
816 | const char* version = pakfire_package_get_version(pkg); | |
817 | ||
818 | r = sqlite3_bind_text(stmt, 3, version, -1, NULL); | |
819 | if (r) { | |
820 | ERROR(db->pakfire, "Could not bind version: %s\n", sqlite3_errmsg(db->handle)); | |
821 | goto ROLLBACK; | |
822 | } | |
823 | ||
824 | // Bind release | |
825 | const char* release = pakfire_package_get_release(pkg); | |
826 | ||
827 | r = sqlite3_bind_text(stmt, 4, release, -1, NULL); | |
828 | if (r) { | |
829 | ERROR(db->pakfire, "Could not bind release: %s\n", sqlite3_errmsg(db->handle)); | |
830 | goto ROLLBACK; | |
831 | } | |
832 | ||
833 | // Bind arch | |
834 | const char* arch = pakfire_package_get_arch(pkg); | |
835 | ||
836 | r = sqlite3_bind_text(stmt, 5, arch, -1, NULL); | |
837 | if (r) { | |
838 | ERROR(db->pakfire, "Could not bind arch: %s\n", sqlite3_errmsg(db->handle)); | |
839 | goto ROLLBACK; | |
840 | } | |
841 | ||
842 | // Bind groups | |
843 | const char* groups = pakfire_package_get_groups(pkg); | |
844 | ||
845 | r = sqlite3_bind_text(stmt, 6, groups, -1, NULL); | |
846 | if (r) { | |
847 | ERROR(db->pakfire, "Could not bind groups: %s\n", sqlite3_errmsg(db->handle)); | |
848 | goto ROLLBACK; | |
849 | } | |
850 | ||
851 | // Bind filename | |
852 | const char* filename = pakfire_package_get_filename(pkg); | |
853 | ||
854 | r = sqlite3_bind_text(stmt, 7, filename, -1, NULL); | |
855 | if (r) { | |
856 | ERROR(db->pakfire, "Could not bind filename: %s\n", sqlite3_errmsg(db->handle)); | |
857 | goto ROLLBACK; | |
858 | } | |
859 | ||
860 | // Bind size | |
861 | unsigned long long size = pakfire_package_get_downloadsize(pkg); | |
862 | ||
863 | r = sqlite3_bind_int64(stmt, 8, size); | |
864 | if (r) { | |
865 | ERROR(db->pakfire, "Could not bind size: %s\n", sqlite3_errmsg(db->handle)); | |
866 | goto ROLLBACK; | |
867 | } | |
868 | ||
869 | // Bind installed size | |
870 | unsigned long long inst_size = pakfire_package_get_installsize(pkg); | |
871 | ||
872 | r = sqlite3_bind_int64(stmt, 9, inst_size); | |
873 | if (r) { | |
874 | ERROR(db->pakfire, "Could not bind inst_size: %s\n", sqlite3_errmsg(db->handle)); | |
875 | goto ROLLBACK; | |
876 | } | |
877 | ||
878 | // Bind hash1 | |
879 | const char* hash1 = pakfire_package_get_checksum(pkg); | |
880 | ||
881 | r = sqlite3_bind_text(stmt, 10, hash1, -1, NULL); | |
882 | if (r) { | |
883 | ERROR(db->pakfire, "Could not bind hash1: %s\n", sqlite3_errmsg(db->handle)); | |
884 | goto ROLLBACK; | |
885 | } | |
886 | ||
887 | // Bind license | |
888 | const char* license = pakfire_package_get_license(pkg); | |
889 | ||
890 | r = sqlite3_bind_text(stmt, 11, license, -1, NULL); | |
891 | if (r) { | |
892 | ERROR(db->pakfire, "Could not bind license: %s\n", sqlite3_errmsg(db->handle)); | |
893 | goto ROLLBACK; | |
894 | } | |
895 | ||
896 | // Bind summary | |
897 | const char* summary = pakfire_package_get_summary(pkg); | |
898 | ||
899 | r = sqlite3_bind_text(stmt, 12, summary, -1, NULL); | |
900 | if (r) { | |
901 | ERROR(db->pakfire, "Could not bind summary: %s\n", sqlite3_errmsg(db->handle)); | |
902 | goto ROLLBACK; | |
903 | } | |
904 | ||
905 | // Bind description | |
906 | const char* description = pakfire_package_get_description(pkg); | |
907 | ||
908 | r = sqlite3_bind_text(stmt, 13, description, -1, NULL); | |
909 | if (r) { | |
910 | ERROR(db->pakfire, "Could not bind description: %s\n", sqlite3_errmsg(db->handle)); | |
911 | goto ROLLBACK; | |
912 | } | |
913 | ||
914 | // Bind uuid | |
915 | const char* uuid = pakfire_package_get_uuid(pkg); | |
916 | ||
917 | r = sqlite3_bind_text(stmt, 14, uuid, -1, NULL); | |
918 | if (r) { | |
919 | ERROR(db->pakfire, "Could not bind uuid: %s\n", sqlite3_errmsg(db->handle)); | |
920 | goto ROLLBACK; | |
921 | } | |
922 | ||
923 | // Bind vendor | |
924 | const char* vendor = pakfire_package_get_vendor(pkg); | |
925 | ||
926 | r = sqlite3_bind_text(stmt, 14, vendor, -1, NULL); | |
927 | if (r) { | |
928 | ERROR(db->pakfire, "Could not bind vendor: %s\n", sqlite3_errmsg(db->handle)); | |
929 | goto ROLLBACK; | |
930 | } | |
931 | ||
e49b93d1 MT |
932 | // Bind build_host |
933 | const char* buildhost = pakfire_package_get_buildhost(pkg); | |
934 | ||
935 | r = sqlite3_bind_text(stmt, 16, buildhost, -1, NULL); | |
936 | if (r) { | |
937 | ERROR(db->pakfire, "Could not bind build_host: %s\n", sqlite3_errmsg(db->handle)); | |
938 | goto ROLLBACK; | |
939 | } | |
940 | ||
941 | // Bind build_time | |
942 | unsigned long long build_time = pakfire_package_get_buildtime(pkg); | |
943 | ||
944 | r = sqlite3_bind_int64(stmt, 17, build_time); | |
945 | if (r) { | |
946 | ERROR(db->pakfire, "Could not bind build_time: %s\n", sqlite3_errmsg(db->handle)); | |
947 | goto ROLLBACK; | |
948 | } | |
949 | ||
950 | // Bind repository name | |
951 | PakfireRepo repo = pakfire_package_get_repo(pkg); | |
952 | if (repo) { | |
953 | const char* repo_name = pakfire_repo_get_name(repo); | |
954 | pakfire_repo_unref(repo); | |
955 | ||
956 | r = sqlite3_bind_text(stmt, 18, repo_name, -1, NULL); | |
957 | if (r) | |
958 | goto ROLLBACK; | |
959 | ||
960 | // No repository? | |
961 | } else { | |
962 | r = sqlite3_bind_null(stmt, 18); | |
963 | if (r) | |
964 | goto ROLLBACK; | |
965 | } | |
966 | ||
967 | // XXX TODO Bind reason | |
968 | r = sqlite3_bind_null(stmt, 19); | |
969 | if (r) | |
970 | goto ROLLBACK; | |
971 | ||
972 | // Run query | |
973 | do { | |
974 | r = sqlite3_step(stmt); | |
975 | } while (r == SQLITE_BUSY); | |
976 | ||
977 | if (r != SQLITE_DONE) { | |
978 | ERROR(db->pakfire, "Could not add package to database: %s\n", | |
979 | sqlite3_errmsg(db->handle)); | |
980 | goto ROLLBACK; | |
981 | } | |
982 | ||
f5c77233 MT |
983 | // Save package ID |
984 | unsigned long packages_id = sqlite3_last_insert_rowid(db->handle); | |
985 | ||
e49b93d1 | 986 | // This is done |
f5c77233 MT |
987 | r = sqlite3_finalize(stmt); |
988 | if (r == SQLITE_OK) | |
989 | stmt = NULL; | |
990 | ||
991 | // Add files | |
992 | r = pakfire_db_add_files(db, packages_id, archive); | |
993 | if (r) | |
994 | goto ROLLBACK; | |
e49b93d1 MT |
995 | |
996 | // All done, commit! | |
997 | r = pakfire_db_commit(db); | |
998 | if (r) | |
999 | goto ROLLBACK; | |
1000 | ||
1001 | return 0; | |
1002 | ||
1003 | ROLLBACK: | |
f5c77233 MT |
1004 | if (stmt) |
1005 | sqlite3_finalize(stmt); | |
e49b93d1 MT |
1006 | |
1007 | pakfire_db_rollback(db); | |
1008 | ||
1009 | return 1; | |
eafbe2ce MT |
1010 | } |
1011 | ||
18bf891d | 1012 | PAKFIRE_EXPORT int pakfire_db_remove_package(struct pakfire_db* db, PakfirePackage pkg) { |
eafbe2ce MT |
1013 | return 0; // TODO |
1014 | } |