Line data Source code
1 : /*-
2 : * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
3 : * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
4 : * Copyright (c) 2011 Will Andrews <will@FreeBSD.org>
5 : * Copyright (c) 2011 Philippe Pepiot <phil@philpep.org>
6 : * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
7 : * Copyright (c) 2012-2013 Matthew Seaman <matthew@FreeBSD.org>
8 : * Copyright (c) 2012 Bryan Drewery <bryan@shatow.net>
9 : * Copyright (c) 2013 Gerald Pfeifer <gerald@pfeifer.com>
10 : * Copyright (c) 2013-2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
11 : * All rights reserved.
12 : *
13 : * Redistribution and use in source and binary forms, with or without
14 : * modification, are permitted provided that the following conditions
15 : * are met:
16 : * 1. Redistributions of source code must retain the above copyright
17 : * notice, this list of conditions and the following disclaimer
18 : * in this position and unchanged.
19 : * 2. Redistributions in binary form must reproduce the above copyright
20 : * notice, this list of conditions and the following disclaimer in the
21 : * documentation and/or other materials provided with the distribution.
22 : *
23 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
24 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 : * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
27 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 : */
34 :
35 : #ifdef HAVE_CONFIG_H
36 : #include "pkg_config.h"
37 : #endif
38 :
39 : #include <bsd_compat.h>
40 :
41 : #include <sys/param.h>
42 : #include <sys/mount.h>
43 :
44 : #include <assert.h>
45 : #include <errno.h>
46 : #include <regex.h>
47 : #include <grp.h>
48 : #ifdef HAVE_LIBUTIL_H
49 : #include <libutil.h>
50 : #endif
51 : #include <stdlib.h>
52 : #include <stdio.h>
53 : #include <stdbool.h>
54 : #include <string.h>
55 : #include <unistd.h>
56 : #include <signal.h>
57 :
58 : #include <sqlite3.h>
59 :
60 : #ifdef HAVE_SYS_STATFS_H
61 : #include <sys/statfs.h>
62 : #elif defined(HAVE_SYS_STATVFS_H)
63 : #include <sys/statvfs.h>
64 : #endif
65 :
66 : #include "pkg.h"
67 : #include "private/event.h"
68 : #include "private/pkg.h"
69 : #include "private/pkgdb.h"
70 : #include "private/utils.h"
71 : #include "private/pkg_deps.h"
72 : #include "kvec.h"
73 :
74 : #include "private/db_upgrades.h"
75 :
76 : /* An application using a libpkg() DBVERSION is assumed to be compatible
77 : with:
78 :
79 : * Any lower schema version of the DB, by updating the schema to DBVERSION
80 : * Any equal schema version of the DB
81 : * Any greater schema version of the DB with the same DB_SCHEMA_MAJOR
82 : -- In general, it is OK to add new tables, but modifying or removing old
83 : tables must be avoided. If necessary, this may be achieved by creating
84 : appropriate VIEWS and TRIGGERS to mimic the older structure.
85 :
86 : Anyone wishing to make a schema change that necessitates incrementing
87 : DB_SCHEMA_MAJOR must first present every other pkgng developer with one
88 : of the Golden Apples of the Hesperides
89 : */
90 :
91 : #define DB_SCHEMA_MAJOR 0
92 : #define DB_SCHEMA_MINOR 32
93 :
94 : #define DBVERSION (DB_SCHEMA_MAJOR * 1000 + DB_SCHEMA_MINOR)
95 :
96 : static int pkgdb_upgrade(struct pkgdb *);
97 : static int prstmt_initialize(struct pkgdb *db);
98 : /* static int run_prstmt(sql_prstmt_index s, ...); */
99 : static void prstmt_finalize(struct pkgdb *db);
100 : static int pkgdb_insert_scripts(struct pkg *pkg, int64_t package_id, sqlite3 *s);
101 :
102 :
103 : extern int sqlite3_shell(int, char**);
104 :
105 : void
106 0 : pkgdb_regex(sqlite3_context *ctx, int argc, sqlite3_value **argv)
107 : {
108 0 : const unsigned char *regex = NULL;
109 : const unsigned char *str;
110 : regex_t *re;
111 : int ret;
112 :
113 0 : if (argc != 2 || (regex = sqlite3_value_text(argv[0])) == NULL ||
114 0 : (str = sqlite3_value_text(argv[1])) == NULL) {
115 0 : sqlite3_result_error(ctx, "SQL function regex() called "
116 : "with invalid arguments.\n", -1);
117 0 : return;
118 : }
119 :
120 0 : re = (regex_t *)sqlite3_get_auxdata(ctx, 0);
121 0 : if (re == NULL) {
122 : int cflags;
123 :
124 0 : if (pkgdb_case_sensitive())
125 0 : cflags = REG_EXTENDED | REG_NOSUB;
126 : else
127 0 : cflags = REG_EXTENDED | REG_NOSUB | REG_ICASE;
128 :
129 0 : re = malloc(sizeof(regex_t));
130 0 : if (regcomp(re, regex, cflags) != 0) {
131 0 : sqlite3_result_error(ctx, "Invalid regex\n", -1);
132 0 : free(re);
133 0 : return;
134 : }
135 :
136 0 : sqlite3_set_auxdata(ctx, 0, re, pkgdb_regex_delete);
137 : }
138 :
139 0 : ret = regexec(re, str, 0, NULL, 0);
140 0 : sqlite3_result_int(ctx, (ret != REG_NOMATCH));
141 : }
142 :
143 : static void
144 280 : pkgdb_split_common(sqlite3_context *ctx, int argc, sqlite3_value **argv,
145 : char delim, const char *first, const char *second)
146 : {
147 280 : const unsigned char *what = NULL;
148 : const unsigned char *data;
149 : const unsigned char *pos;
150 :
151 560 : if (argc != 2 || (what = sqlite3_value_text(argv[0])) == NULL ||
152 280 : (data = sqlite3_value_text(argv[1])) == NULL) {
153 0 : sqlite3_result_error(ctx, "SQL function split_*() called "
154 : "with invalid arguments.\n", -1);
155 280 : return;
156 : }
157 :
158 280 : if (strcasecmp(what, first) == 0) {
159 140 : pos = strrchr(data, delim);
160 140 : if (pos != NULL)
161 54 : sqlite3_result_text(ctx, data, (pos - data), NULL);
162 : else
163 86 : sqlite3_result_text(ctx, data, -1, NULL);
164 : }
165 140 : else if (strcasecmp(what, second) == 0) {
166 140 : pos = strrchr(data, delim);
167 140 : if (pos != NULL)
168 54 : sqlite3_result_text(ctx, pos + 1, -1, NULL);
169 : else
170 86 : sqlite3_result_text(ctx, data, -1, NULL);
171 : }
172 : else {
173 0 : sqlite3_result_error(ctx, "SQL function split_*() called "
174 : "with invalid arguments.\n", -1);
175 : }
176 : }
177 :
178 : void
179 280 : pkgdb_split_version(sqlite3_context *ctx, int argc, sqlite3_value **argv)
180 : {
181 280 : pkgdb_split_common(ctx, argc, argv, '-', "name", "version");
182 280 : }
183 :
184 : void
185 0 : pkgdb_regex_delete(void *p)
186 : {
187 0 : regex_t *re = (regex_t *)p;
188 :
189 0 : regfree(re);
190 0 : free(re);
191 0 : }
192 :
193 : void
194 40 : pkgdb_now(sqlite3_context *ctx, int argc, __unused sqlite3_value **argv)
195 : {
196 40 : if (argc != 0) {
197 0 : sqlite3_result_error(ctx, "Invalid usage of now() "
198 : "no arguments expected\n", -1);
199 40 : return;
200 : }
201 :
202 40 : sqlite3_result_int64(ctx, (int64_t)time(NULL));
203 : }
204 :
205 : void
206 0 : pkgdb_myarch(sqlite3_context *ctx, int argc, sqlite3_value **argv)
207 : {
208 0 : const unsigned char *arch = NULL;
209 : char myarch[BUFSIZ];
210 :
211 0 : if (argc > 1) {
212 0 : sqlite3_result_error(ctx, "Invalid usage of myarch\n", -1);
213 0 : return;
214 : }
215 :
216 0 : if (argc == 0 || (arch = sqlite3_value_text(argv[0])) == NULL) {
217 0 : pkg_get_myarch(myarch, BUFSIZ);
218 0 : sqlite3_result_text(ctx, myarch, strlen(myarch), NULL);
219 0 : return;
220 : }
221 0 : sqlite3_result_text(ctx, arch, strlen(arch), NULL);
222 : }
223 :
224 : static void
225 0 : pkgdb_vercmp(sqlite3_context *ctx, int argc, sqlite3_value **argv)
226 : {
227 : const char *op_str, *arg1, *arg2;
228 : enum pkg_dep_version_op op;
229 : int cmp;
230 : bool ret;
231 :
232 0 : if (argc != 3) {
233 0 : sqlite3_result_error(ctx, "Invalid usage of vercmp\n", -1);
234 0 : return;
235 : }
236 :
237 0 : op_str = sqlite3_value_text(argv[0]);
238 0 : arg1 = sqlite3_value_text(argv[1]);
239 0 : arg2 = sqlite3_value_text(argv[2]);
240 :
241 0 : if (op_str == NULL || arg1 == NULL || arg2 == NULL) {
242 0 : sqlite3_result_error(ctx, "Invalid usage of vercmp\n", -1);
243 0 : return;
244 : }
245 :
246 0 : op = pkg_deps_string_toop(op_str);
247 0 : cmp = pkg_version_cmp(arg1, arg2);
248 :
249 0 : switch(op) {
250 : case VERSION_ANY:
251 : default:
252 0 : ret = true;
253 0 : break;
254 : case VERSION_EQ:
255 0 : ret = (cmp == 0);
256 0 : break;
257 : case VERSION_GE:
258 0 : ret = (cmp >= 0);
259 0 : break;
260 : case VERSION_LE:
261 0 : ret = (cmp <= 0);
262 0 : break;
263 : case VERSION_GT:
264 0 : ret = (cmp > 0);
265 0 : break;
266 : case VERSION_LT:
267 0 : ret = (cmp < 0);
268 0 : break;
269 : case VERSION_NOT:
270 0 : ret = (cmp != 0);
271 0 : break;
272 : }
273 :
274 0 : sqlite3_result_int(ctx, ret);
275 : }
276 :
277 : static int
278 83 : pkgdb_upgrade(struct pkgdb *db)
279 : {
280 83 : int64_t db_version = -1;
281 : const char *sql_upgrade;
282 : int i, ret;
283 :
284 83 : assert(db != NULL);
285 :
286 83 : ret = get_pragma(db->sqlite, "PRAGMA user_version;", &db_version, false);
287 83 : if (ret != EPKG_OK)
288 0 : return (EPKG_FATAL);
289 :
290 83 : if (db_version == DBVERSION)
291 83 : return (EPKG_OK);
292 0 : else if (db_version > DBVERSION) {
293 0 : if (db_version / 1000 <= DB_SCHEMA_MAJOR) {
294 : /* VIEWS and TRIGGERS used as compatibility hack */
295 0 : pkg_emit_error("warning: database version %" PRId64
296 : " is newer than libpkg(3) version %d, but still "
297 : "compatible", db_version, DBVERSION);
298 0 : return (EPKG_OK);
299 : } else {
300 0 : pkg_emit_error("database version %" PRId64 " is newer "
301 : "than and incompatible with libpkg(3) version %d",
302 : db_version, DBVERSION);
303 0 : return (EPKG_FATAL);
304 : }
305 : }
306 :
307 0 : while (db_version < DBVERSION) {
308 : const char *sql_str;
309 0 : if (sqlite3_db_readonly(db->sqlite, "main")) {
310 0 : pkg_emit_error("The database is outdated and "
311 : "opened readonly");
312 0 : return (EPKG_FATAL);
313 : }
314 0 : db_version++;
315 :
316 0 : i = 0;
317 0 : sql_upgrade = NULL;
318 0 : while (db_upgrades[i].version != -1) {
319 0 : if (db_upgrades[i].version == db_version) {
320 0 : sql_upgrade = db_upgrades[i].sql;
321 0 : break;
322 : }
323 0 : i++;
324 : }
325 :
326 : /*
327 : * We can't find the statements to upgrade to the next version,
328 : * maybe because the current version is too old and upgrade
329 : * support has been removed.
330 : */
331 0 : if (sql_upgrade == NULL) {
332 0 : pkg_emit_error("can not upgrade to db version %" PRId64,
333 : db_version);
334 0 : return (EPKG_FATAL);
335 : }
336 :
337 0 : if (pkgdb_transaction_begin_sqlite(db->sqlite, NULL) != EPKG_OK)
338 0 : return (EPKG_FATAL);
339 :
340 0 : if (sql_exec(db->sqlite, sql_upgrade) != EPKG_OK) {
341 0 : pkgdb_transaction_rollback_sqlite(db->sqlite, NULL);
342 0 : return (EPKG_FATAL);
343 : }
344 :
345 0 : sql_str = "PRAGMA user_version = %" PRId64 ";";
346 0 : ret = sql_exec(db->sqlite, sql_str, db_version);
347 0 : if (ret != EPKG_OK) {
348 0 : pkgdb_transaction_rollback_sqlite(db->sqlite, NULL);
349 0 : return (EPKG_FATAL);
350 : }
351 :
352 0 : if (pkgdb_transaction_commit_sqlite(db->sqlite, NULL) != EPKG_OK)
353 0 : return (EPKG_FATAL);
354 : }
355 :
356 0 : return (EPKG_OK);
357 : }
358 :
359 : /*
360 : * in the database :
361 : * scripts.type can be:
362 : * - 0: PRE_INSTALL
363 : * - 1: POST_INSTALL
364 : * - 2: PRE_DEINSTALL
365 : * - 3: POST_DEINSTALL
366 : * - 4: PRE_UPGRADE
367 : * - 5: POST_UPGRADE
368 : * - 6: INSTALL
369 : * - 7: DEINSTALL
370 : * - 8: UPGRADE
371 : */
372 :
373 : static int
374 24 : pkgdb_init(sqlite3 *sdb)
375 : {
376 24 : const char sql[] = ""
377 : "BEGIN;"
378 : "CREATE TABLE packages ("
379 : "id INTEGER PRIMARY KEY,"
380 : "origin TEXT NOT NULL,"
381 : "name TEXT NOT NULL,"
382 : "version TEXT NOT NULL,"
383 : "comment TEXT NOT NULL,"
384 : "desc TEXT NOT NULL,"
385 : "mtree_id INTEGER REFERENCES mtree(id) ON DELETE RESTRICT"
386 : " ON UPDATE CASCADE,"
387 : "message TEXT,"
388 : "arch TEXT NOT NULL,"
389 : "maintainer TEXT NOT NULL, "
390 : "www TEXT,"
391 : "prefix TEXT NOT NULL,"
392 : "flatsize INTEGER NOT NULL,"
393 : "automatic INTEGER NOT NULL,"
394 : "locked INTEGER NOT NULL DEFAULT 0,"
395 : "licenselogic INTEGER NOT NULL,"
396 : "time INTEGER, "
397 : "manifestdigest TEXT NULL, "
398 : "pkg_format_version INTEGER,"
399 : "dep_formula TEXT NULL"
400 : ");"
401 : "CREATE UNIQUE INDEX packages_unique ON packages(name);"
402 : "CREATE TABLE mtree ("
403 : "id INTEGER PRIMARY KEY,"
404 : "content TEXT NOT NULL UNIQUE"
405 : ");"
406 : "CREATE TABLE pkg_script ("
407 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
408 : " ON UPDATE CASCADE,"
409 : "type INTEGER,"
410 : "script_id INTEGER REFERENCES script(script_id)"
411 : " ON DELETE RESTRICT ON UPDATE CASCADE,"
412 : "PRIMARY KEY (package_id, type)"
413 : ");"
414 : "CREATE TABLE script ("
415 : "script_id INTEGER PRIMARY KEY,"
416 : "script TEXT NOT NULL UNIQUE"
417 : ");"
418 : "CREATE TABLE option ("
419 : "option_id INTEGER PRIMARY KEY,"
420 : "option TEXT NOT NULL UNIQUE"
421 : ");"
422 : "CREATE TABLE option_desc ("
423 : "option_desc_id INTEGER PRIMARY KEY,"
424 : "option_desc TEXT NOT NULL UNIQUE"
425 : ");"
426 : "CREATE TABLE pkg_option ("
427 : "package_id INTEGER NOT NULL REFERENCES packages(id) "
428 : "ON DELETE CASCADE ON UPDATE CASCADE,"
429 : "option_id INTEGER NOT NULL REFERENCES option(option_id) "
430 : "ON DELETE RESTRICT ON UPDATE CASCADE,"
431 : "value TEXT NOT NULL,"
432 : "PRIMARY KEY(package_id, option_id)"
433 : ");"
434 : "CREATE TABLE pkg_option_desc ("
435 : "package_id INTEGER NOT NULL REFERENCES packages(id) "
436 : "ON DELETE CASCADE ON UPDATE CASCADE,"
437 : "option_id INTEGER NOT NULL REFERENCES option(option_id) "
438 : "ON DELETE RESTRICT ON UPDATE CASCADE,"
439 : "option_desc_id INTEGER NOT NULL "
440 : "REFERENCES option_desc(option_desc_id) "
441 : "ON DELETE RESTRICT ON UPDATE CASCADE,"
442 : "PRIMARY KEY(package_id, option_id)"
443 : ");"
444 : "CREATE TABLE pkg_option_default ("
445 : "package_id INTEGER NOT NULL REFERENCES packages(id) "
446 : "ON DELETE CASCADE ON UPDATE CASCADE,"
447 : "option_id INTEGER NOT NULL REFERENCES option(option_id) "
448 : "ON DELETE RESTRICT ON UPDATE CASCADE,"
449 : "default_value TEXT NOT NULL,"
450 : "PRIMARY KEY(package_id, option_id)"
451 : ");"
452 : "CREATE TABLE deps ("
453 : "origin TEXT NOT NULL,"
454 : "name TEXT NOT NULL,"
455 : "version TEXT NOT NULL,"
456 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
457 : " ON UPDATE CASCADE"
458 : ");"
459 : "CREATE UNIQUE INDEX deps_unique ON deps(name, version, package_id);"
460 : "CREATE TABLE files ("
461 : "path TEXT PRIMARY KEY,"
462 : "sha256 TEXT,"
463 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
464 : " ON UPDATE CASCADE"
465 : ");"
466 : "CREATE TABLE directories ("
467 : "id INTEGER PRIMARY KEY,"
468 : "path TEXT NOT NULL UNIQUE"
469 : ");"
470 : "CREATE TABLE pkg_directories ("
471 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
472 : " ON UPDATE CASCADE,"
473 : "directory_id INTEGER REFERENCES directories(id) ON DELETE RESTRICT"
474 : " ON UPDATE RESTRICT,"
475 : "try INTEGER,"
476 : "PRIMARY KEY (package_id, directory_id)"
477 : ");"
478 : "CREATE TABLE categories ("
479 : "id INTEGER PRIMARY KEY,"
480 : "name TEXT NOT NULL UNIQUE"
481 : ");"
482 : "CREATE TABLE pkg_categories ("
483 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
484 : " ON UPDATE CASCADE,"
485 : "category_id INTEGER REFERENCES categories(id) ON DELETE RESTRICT"
486 : " ON UPDATE RESTRICT,"
487 : "PRIMARY KEY (package_id, category_id)"
488 : ");"
489 : "CREATE TABLE licenses ("
490 : "id INTEGER PRIMARY KEY,"
491 : "name TEXT NOT NULL UNIQUE"
492 : ");"
493 : "CREATE TABLE pkg_licenses ("
494 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
495 : " ON UPDATE CASCADE,"
496 : "license_id INTEGER REFERENCES licenses(id) ON DELETE RESTRICT"
497 : " ON UPDATE RESTRICT,"
498 : "PRIMARY KEY (package_id, license_id)"
499 : ");"
500 : "CREATE TABLE users ("
501 : "id INTEGER PRIMARY KEY,"
502 : "name TEXT NOT NULL UNIQUE"
503 : ");"
504 : "CREATE TABLE pkg_users ("
505 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
506 : " ON UPDATE CASCADE,"
507 : "user_id INTEGER REFERENCES users(id) ON DELETE RESTRICT"
508 : " ON UPDATE RESTRICT,"
509 : "UNIQUE(package_id, user_id)"
510 : ");"
511 : "CREATE TABLE groups ("
512 : "id INTEGER PRIMARY KEY,"
513 : "name TEXT NOT NULL UNIQUE"
514 : ");"
515 : "CREATE TABLE pkg_groups ("
516 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
517 : " ON UPDATE CASCADE,"
518 : "group_id INTEGER REFERENCES groups(id) ON DELETE RESTRICT"
519 : " ON UPDATE RESTRICT,"
520 : "UNIQUE(package_id, group_id)"
521 : ");"
522 : "CREATE TABLE shlibs ("
523 : "id INTEGER PRIMARY KEY,"
524 : "name TEXT NOT NULL UNIQUE"
525 : ");"
526 : "CREATE TABLE pkg_shlibs_required ("
527 : "package_id INTEGER NOT NULL REFERENCES packages(id)"
528 : " ON DELETE CASCADE ON UPDATE CASCADE,"
529 : "shlib_id INTEGER NOT NULL REFERENCES shlibs(id)"
530 : " ON DELETE RESTRICT ON UPDATE RESTRICT,"
531 : "UNIQUE (package_id, shlib_id)"
532 : ");"
533 : "CREATE TABLE pkg_shlibs_provided ("
534 : "package_id INTEGER NOT NULL REFERENCES packages(id)"
535 : " ON DELETE CASCADE ON UPDATE CASCADE,"
536 : "shlib_id INTEGER NOT NULL REFERENCES shlibs(id)"
537 : " ON DELETE RESTRICT ON UPDATE RESTRICT,"
538 : "UNIQUE (package_id, shlib_id)"
539 : ");"
540 : "CREATE TABLE annotation ("
541 : "annotation_id INTEGER PRIMARY KEY,"
542 : "annotation TEXT NOT NULL UNIQUE"
543 : ");"
544 : "CREATE TABLE pkg_annotation ("
545 : "package_id INTERGER REFERENCES packages(id)"
546 : " ON DELETE CASCADE ON UPDATE RESTRICT,"
547 : "tag_id INTEGER NOT NULL REFERENCES annotation(annotation_id)"
548 : " ON DELETE CASCADE ON UPDATE RESTRICT,"
549 : "value_id INTEGER NOT NULL REFERENCES annotation(annotation_id)"
550 : " ON DELETE CASCADE ON UPDATE RESTRICT,"
551 : "UNIQUE (package_id, tag_id)"
552 : ");"
553 : "CREATE TABLE pkg_conflicts ("
554 : "package_id INTEGER NOT NULL REFERENCES packages(id)"
555 : " ON DELETE CASCADE ON UPDATE CASCADE,"
556 : "conflict_id INTEGER NOT NULL,"
557 : "UNIQUE(package_id, conflict_id)"
558 : ");"
559 : "CREATE TABLE pkg_lock ("
560 : "exclusive INTEGER(1),"
561 : "advisory INTEGER(1),"
562 : "read INTEGER(8)"
563 : ");"
564 : "CREATE TABLE pkg_lock_pid ("
565 : "pid INTEGER PRIMARY KEY"
566 : ");"
567 : "INSERT INTO pkg_lock VALUES(0,0,0);"
568 : "CREATE TABLE provides("
569 : " id INTEGER PRIMARY KEY,"
570 : " provide TEXT NOT NULL"
571 : ");"
572 : "CREATE TABLE pkg_provides ("
573 : "package_id INTEGER NOT NULL REFERENCES packages(id)"
574 : " ON DELETE CASCADE ON UPDATE CASCADE,"
575 : "provide_id INTEGER NOT NULL REFERENCES provides(id)"
576 : " ON DELETE RESTRICT ON UPDATE RESTRICT,"
577 : "UNIQUE(package_id, provide_id)"
578 : ");"
579 : "CREATE TABLE config_files ("
580 : "path TEXT NOT NULL UNIQUE, "
581 : "content TEXT, "
582 : "package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
583 : " ON UPDATE CASCADE"
584 : ");"
585 :
586 : /* FTS search table */
587 :
588 : "CREATE VIRTUAL TABLE pkg_search USING fts4(id, name, origin);"
589 :
590 : /* Mark the end of the array */
591 :
592 : "CREATE INDEX deporigini on deps(origin);"
593 : "CREATE INDEX pkg_script_package_id ON pkg_script(package_id);"
594 : "CREATE INDEX deps_package_id ON deps (package_id);"
595 : "CREATE INDEX files_package_id ON files (package_id);"
596 : "CREATE INDEX pkg_directories_package_id ON pkg_directories (package_id);"
597 : "CREATE INDEX pkg_categories_package_id ON pkg_categories (package_id);"
598 : "CREATE INDEX pkg_licenses_package_id ON pkg_licenses (package_id);"
599 : "CREATE INDEX pkg_users_package_id ON pkg_users (package_id);"
600 : "CREATE INDEX pkg_groups_package_id ON pkg_groups (package_id);"
601 : "CREATE INDEX pkg_shlibs_required_package_id ON pkg_shlibs_required (package_id);"
602 : "CREATE INDEX pkg_shlibs_provided_package_id ON pkg_shlibs_provided (package_id);"
603 : "CREATE INDEX pkg_directories_directory_id ON pkg_directories (directory_id);"
604 : "CREATE INDEX pkg_annotation_package_id ON pkg_annotation(package_id);"
605 : "CREATE INDEX pkg_digest_id ON packages(origin, manifestdigest);"
606 : "CREATE INDEX pkg_conflicts_pid ON pkg_conflicts(package_id);"
607 : "CREATE INDEX pkg_conflicts_cid ON pkg_conflicts(conflict_id);"
608 : "CREATE INDEX pkg_provides_id ON pkg_provides(package_id);"
609 : "CREATE INDEX packages_origin ON packages(origin COLLATE NOCASE);"
610 : "CREATE INDEX packages_name ON packages(name COLLATE NOCASE);"
611 :
612 : "CREATE VIEW pkg_shlibs AS SELECT * FROM pkg_shlibs_required;"
613 : "CREATE TRIGGER pkg_shlibs_update "
614 : "INSTEAD OF UPDATE ON pkg_shlibs "
615 : "FOR EACH ROW BEGIN "
616 : "UPDATE pkg_shlibs_required "
617 : "SET package_id = new.package_id, "
618 : " shlib_id = new.shlib_id "
619 : "WHERE shlib_id = old.shlib_id "
620 : "AND package_id = old.package_id; "
621 : "END;"
622 : "CREATE TRIGGER pkg_shlibs_insert "
623 : "INSTEAD OF INSERT ON pkg_shlibs "
624 : "FOR EACH ROW BEGIN "
625 : "INSERT INTO pkg_shlibs_required (shlib_id, package_id) "
626 : "VALUES (new.shlib_id, new.package_id); "
627 : "END;"
628 : "CREATE TRIGGER pkg_shlibs_delete "
629 : "INSTEAD OF DELETE ON pkg_shlibs "
630 : "FOR EACH ROW BEGIN "
631 : "DELETE FROM pkg_shlibs_required "
632 : "WHERE shlib_id = old.shlib_id "
633 : "AND package_id = old.package_id; "
634 : "END;"
635 :
636 : "CREATE VIEW scripts AS SELECT package_id, script, type"
637 : " FROM pkg_script ps JOIN script s"
638 : " ON (ps.script_id = s.script_id);"
639 : "CREATE TRIGGER scripts_update"
640 : " INSTEAD OF UPDATE ON scripts "
641 : "FOR EACH ROW BEGIN"
642 : " INSERT OR IGNORE INTO script(script)"
643 : " VALUES(new.script);"
644 : " UPDATE pkg_script"
645 : " SET package_id = new.package_id,"
646 : " type = new.type,"
647 : " script_id = ( SELECT script_id"
648 : " FROM script WHERE script = new.script )"
649 : " WHERE package_id = old.package_id"
650 : " AND type = old.type;"
651 : "END;"
652 : "CREATE TRIGGER scripts_insert"
653 : " INSTEAD OF INSERT ON scripts "
654 : "FOR EACH ROW BEGIN"
655 : " INSERT OR IGNORE INTO script(script)"
656 : " VALUES(new.script);"
657 : " INSERT INTO pkg_script(package_id, type, script_id) "
658 : " SELECT new.package_id, new.type, s.script_id"
659 : " FROM script s WHERE new.script = s.script;"
660 : "END;"
661 : "CREATE TRIGGER scripts_delete"
662 : " INSTEAD OF DELETE ON scripts "
663 : "FOR EACH ROW BEGIN"
664 : " DELETE FROM pkg_script"
665 : " WHERE package_id = old.package_id"
666 : " AND type = old.type;"
667 : " DELETE FROM script"
668 : " WHERE script_id NOT IN"
669 : " (SELECT DISTINCT script_id FROM pkg_script);"
670 : "END;"
671 : "CREATE VIEW options AS "
672 : "SELECT package_id, option, value "
673 : "FROM pkg_option JOIN option USING(option_id);"
674 : "CREATE TRIGGER options_update "
675 : "INSTEAD OF UPDATE ON options "
676 : "FOR EACH ROW BEGIN "
677 : "UPDATE pkg_option "
678 : "SET value = new.value "
679 : "WHERE package_id = old.package_id AND "
680 : "option_id = ( SELECT option_id FROM option "
681 : "WHERE option = old.option );"
682 : "END;"
683 : "CREATE TRIGGER options_insert "
684 : "INSTEAD OF INSERT ON options "
685 : "FOR EACH ROW BEGIN "
686 : "INSERT OR IGNORE INTO option(option) "
687 : "VALUES(new.option);"
688 : "INSERT INTO pkg_option(package_id, option_id, value) "
689 : "VALUES (new.package_id, "
690 : "(SELECT option_id FROM option "
691 : "WHERE option = new.option), "
692 : "new.value);"
693 : "END;"
694 : "CREATE TRIGGER options_delete "
695 : "INSTEAD OF DELETE ON options "
696 : "FOR EACH ROW BEGIN "
697 : "DELETE FROM pkg_option "
698 : "WHERE package_id = old.package_id AND "
699 : "option_id = ( SELECT option_id FROM option "
700 : "WHERE option = old.option );"
701 : "DELETE FROM option "
702 : "WHERE option_id NOT IN "
703 : "( SELECT DISTINCT option_id FROM pkg_option );"
704 : "END;"
705 : "CREATE TABLE requires("
706 : " id INTEGER PRIMARY KEY,"
707 : " require TEXT NOT NULL"
708 : ");"
709 : "CREATE TABLE pkg_requires ("
710 : "package_id INTEGER NOT NULL REFERENCES packages(id)"
711 : " ON DELETE CASCADE ON UPDATE CASCADE,"
712 : "require_id INTEGER NOT NULL REFERENCES requires(id)"
713 : " ON DELETE RESTRICT ON UPDATE RESTRICT,"
714 : "UNIQUE(package_id, require_id)"
715 : ");"
716 :
717 : "PRAGMA user_version = %d;"
718 : "COMMIT;"
719 : ;
720 :
721 24 : return (sql_exec(sdb, sql, DBVERSION));
722 : }
723 :
724 : static int
725 192 : pkgdb_is_insecure_mode(const char *path, bool install_as_user)
726 : {
727 : uid_t fileowner;
728 : gid_t filegroup;
729 192 : bool bad_perms = false;
730 192 : bool wrong_owner = false;
731 : struct stat sb;
732 :
733 192 : if (install_as_user) {
734 192 : fileowner = geteuid();
735 192 : filegroup = getegid();
736 : } else {
737 0 : fileowner = 0;
738 0 : filegroup = 0;
739 : }
740 :
741 192 : if (stat(path, &sb) != 0) {
742 38 : if (errno == EACCES)
743 0 : return (EPKG_ENOACCESS);
744 38 : else if (errno == ENOENT)
745 38 : return (EPKG_ENODB);
746 : else
747 0 : return (EPKG_FATAL);
748 : }
749 :
750 : /* if fileowner == 0, root ownership and no group or other
751 : read access. if fileowner != 0, require no other read
752 : access and group read access IFF the group ownership ==
753 : filegroup */
754 :
755 154 : if ( fileowner == 0 ) {
756 0 : if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
757 0 : bad_perms = true;
758 0 : if (sb.st_uid != fileowner)
759 0 : wrong_owner = true;
760 : } else {
761 154 : if ((sb.st_mode & S_IWOTH) != 0)
762 0 : bad_perms = true;
763 154 : if (sb.st_gid != filegroup && (sb.st_mode & S_IWGRP) != 0)
764 0 : bad_perms = true;
765 154 : if (sb.st_uid != 0 && sb.st_uid != fileowner && sb.st_gid != filegroup)
766 0 : wrong_owner = true;
767 : }
768 :
769 154 : if (bad_perms) {
770 0 : pkg_emit_error("%s permissions (%#o) too lax", path,
771 0 : (sb.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)));
772 0 : return (EPKG_INSECURE);
773 : }
774 154 : if (wrong_owner) {
775 0 : pkg_emit_error("%s wrong user or group ownership"
776 : " (expected %d/%d versus actual %d/%d)",
777 : path, fileowner, filegroup, sb.st_uid, sb.st_gid);
778 0 : return (EPKG_INSECURE);
779 : }
780 :
781 154 : return (EPKG_OK);
782 : }
783 :
784 : int
785 192 : pkgdb_check_access(unsigned mode, const char* dbdir, const char *dbname)
786 : {
787 : char dbpath[MAXPATHLEN];
788 : int retval;
789 : bool database_exists;
790 : bool install_as_user;
791 :
792 192 : if (dbname != NULL)
793 99 : snprintf(dbpath, sizeof(dbpath), "%s/%s", dbdir, dbname);
794 : else
795 93 : strlcpy(dbpath, dbdir, sizeof(dbpath));
796 :
797 192 : install_as_user = (getenv("INSTALL_AS_USER") != NULL);
798 :
799 192 : retval = pkgdb_is_insecure_mode(dbpath, install_as_user);
800 :
801 192 : database_exists = (retval != EPKG_ENODB);
802 :
803 192 : if (database_exists && retval != EPKG_OK)
804 0 : return (retval);
805 :
806 192 : if (!database_exists && (mode & PKGDB_MODE_CREATE) != 0)
807 38 : return (EPKG_OK);
808 :
809 154 : switch(mode & (PKGDB_MODE_READ|PKGDB_MODE_WRITE)) {
810 : case 0: /* Existence test */
811 0 : retval = eaccess(dbpath, F_OK);
812 0 : break;
813 : case PKGDB_MODE_READ:
814 68 : retval = eaccess(dbpath, R_OK);
815 68 : break;
816 : case PKGDB_MODE_WRITE:
817 0 : retval = eaccess(dbpath, W_OK);
818 0 : if (retval != 0 && errno == ENOENT) {
819 0 : mkdirs(dbdir);
820 0 : retval = eaccess(dbpath, W_OK);
821 : }
822 0 : break;
823 : case PKGDB_MODE_READ|PKGDB_MODE_WRITE:
824 86 : retval = eaccess(dbpath, R_OK|W_OK);
825 86 : if (retval != 0 && errno == ENOENT) {
826 0 : mkdirs(dbdir);
827 0 : retval = eaccess(dbpath, W_OK);
828 : }
829 86 : break;
830 : }
831 :
832 154 : if (retval != 0) {
833 0 : if (errno == ENOENT)
834 0 : return (EPKG_ENODB);
835 0 : else if (errno == EACCES)
836 0 : return (EPKG_ENOACCESS);
837 : else
838 0 : return (EPKG_FATAL);
839 : }
840 :
841 154 : return (EPKG_OK);
842 : }
843 :
844 :
845 : int
846 93 : pkgdb_access(unsigned mode, unsigned database)
847 : {
848 : const pkg_object *o;
849 : const char *dbdir;
850 93 : int retval = EPKG_OK;
851 :
852 : /*
853 : * This will return one of:
854 : *
855 : * EPKG_ENODB: a database doesn't exist and we don't want to create
856 : * it, or dbdir doesn't exist
857 : *
858 : * EPKG_INSECURE: the dbfile or one of the directories in the
859 : * path to it are writable by other than root or
860 : * (if $INSTALL_AS_USER is set) the current euid
861 : * and egid
862 : *
863 : * EPKG_ENOACCESS: we don't have privileges to read or write
864 : *
865 : * EPKG_FATAL: Couldn't determine the answer for other reason,
866 : * like configuration screwed up, invalid argument values,
867 : * read-only filesystem, etc.
868 : *
869 : * EPKG_OK: We can go ahead
870 : */
871 :
872 93 : o = pkg_config_get("PKG_DBDIR");
873 93 : dbdir = pkg_object_string(o);
874 93 : if ((mode & ~(PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE))
875 : != 0)
876 0 : return (EPKG_FATAL); /* EINVAL */
877 :
878 93 : if ((database & ~(PKGDB_DB_LOCAL|PKGDB_DB_REPO)) != 0)
879 0 : return (EPKG_FATAL); /* EINVAL */
880 :
881 : /* Test the enclosing directory: if we're going to create the
882 : DB, then we need read and write permissions on the dir.
883 : Otherwise, just test for read access */
884 :
885 93 : if ((mode & PKGDB_MODE_CREATE) != 0) {
886 46 : retval = pkgdb_check_access(PKGDB_MODE_READ|PKGDB_MODE_WRITE,
887 : dbdir, NULL);
888 : } else
889 47 : retval = pkgdb_check_access(PKGDB_MODE_READ, dbdir, NULL);
890 93 : if (retval != EPKG_OK)
891 0 : return (retval);
892 :
893 : /* Test local.sqlite, if required */
894 :
895 93 : if ((database & PKGDB_DB_LOCAL) != 0) {
896 83 : retval = pkgdb_check_access(mode, dbdir, "local.sqlite");
897 83 : if (retval != EPKG_OK)
898 0 : return (retval);
899 : }
900 :
901 93 : if ((database & PKGDB_DB_REPO) != 0) {
902 18 : struct pkg_repo *r = NULL;
903 :
904 52 : while (pkg_repos(&r) == EPKG_OK) {
905 : /* Ignore any repos marked as inactive */
906 16 : if (!pkg_repo_enabled(r))
907 0 : continue;
908 :
909 16 : retval = r->ops->access(r, mode);
910 16 : if (retval != EPKG_OK) {
911 0 : if (retval == EPKG_ENODB &&
912 : mode == PKGDB_MODE_READ)
913 0 : pkg_emit_error("Repository %s missing."
914 0 : " 'pkg update' required", r->name);
915 0 : return (retval);
916 : }
917 : }
918 : }
919 93 : return (retval);
920 : }
921 :
922 : static void
923 0 : pkgdb_profile_callback(void *ud __unused, const char *req, sqlite3_uint64 nsec)
924 : {
925 : /* According to sqlite3 documentation, nsec has milliseconds accuracy */
926 0 : nsec /= 1000000LLU;
927 0 : if (nsec > 0)
928 0 : pkg_debug(1, "Sqlite request %s was executed in %lu milliseconds",
929 : req, (unsigned long)nsec);
930 0 : }
931 :
932 : int
933 75 : pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
934 : {
935 75 : return (pkgdb_open_all(db_p, type, NULL));
936 : }
937 :
938 : static int
939 8 : pkgdb_open_repos(struct pkgdb *db, const char *reponame)
940 : {
941 8 : struct pkg_repo *r = NULL;
942 : struct _pkg_repo_list_item *item;
943 :
944 24 : while (pkg_repos(&r) == EPKG_OK) {
945 8 : if (reponame == NULL || strcasecmp(r->name, reponame) == 0) {
946 : /* We need read only access here */
947 8 : if (r->ops->open(r, R_OK) == EPKG_OK) {
948 8 : item = malloc(sizeof(*item));
949 8 : if (item == NULL) {
950 0 : pkg_emit_errno("malloc", "_pkg_repo_list_item");
951 0 : return (EPKG_FATAL);
952 : }
953 :
954 8 : r->ops->init(r);
955 8 : item->repo = r;
956 8 : LL_PREPEND(db->repos, item);
957 : } else
958 0 : pkg_emit_error("Repository %s cannot be opened."
959 0 : " 'pkg update' required", r->name);
960 : }
961 : }
962 :
963 8 : return (EPKG_OK);
964 : }
965 :
966 : int
967 83 : pkgdb_open_all(struct pkgdb **db_p, pkgdb_t type, const char *reponame)
968 : {
969 83 : struct pkgdb *db = NULL;
970 83 : bool reopen = false;
971 83 : bool profile = false;
972 : char localpath[MAXPATHLEN];
973 : const char *dbdir;
974 83 : bool create = false;
975 83 : bool createdir = false;
976 : int ret;
977 :
978 83 : if (*db_p != NULL) {
979 0 : reopen = true;
980 0 : db = *db_p;
981 : }
982 :
983 83 : dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
984 83 : if (!reopen && (db = calloc(1, sizeof(struct pkgdb))) == NULL) {
985 0 : pkg_emit_errno("malloc", "pkgdb");
986 0 : return (EPKG_FATAL);
987 : }
988 :
989 83 : db->prstmt_initialized = false;
990 :
991 83 : if (!reopen) {
992 83 : snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
993 :
994 83 : if (eaccess(localpath, R_OK) != 0) {
995 24 : if (errno != ENOENT) {
996 0 : pkg_emit_nolocaldb();
997 0 : pkgdb_close(db);
998 0 : return (EPKG_ENODB);
999 24 : } else if ((eaccess(dbdir, W_OK) != 0)) {
1000 : /*
1001 : * If we need to create the db but cannot
1002 : * write to it, fail early
1003 : */
1004 0 : if (errno == ENOENT) {
1005 0 : createdir = true;
1006 0 : create = true;
1007 : } else {
1008 0 : pkg_emit_nolocaldb();
1009 0 : pkgdb_close(db);
1010 0 : return (EPKG_ENODB);
1011 : }
1012 : } else {
1013 24 : create = true;
1014 : }
1015 : }
1016 :
1017 : /* Create the directory if it doesn't exist */
1018 83 : if (createdir && mkdirs(dbdir) != EPKG_OK) {
1019 0 : pkgdb_close(db);
1020 0 : return (EPKG_FATAL);
1021 : }
1022 :
1023 83 : sqlite3_initialize();
1024 :
1025 : /*
1026 : * Fall back on unix-dotfile locking strategy if on a network filesystem
1027 : */
1028 : #if defined(HAVE_SYS_STATVFS_H) && defined(ST_LOCAL)
1029 : struct statvfs stfs;
1030 :
1031 : if (statvfs(dbdir, &stfs) == 0) {
1032 : if ((stfs.f_flag & ST_LOCAL) != ST_LOCAL)
1033 : sqlite3_vfs_register(sqlite3_vfs_find("unix-dotfile"), 1);
1034 : }
1035 : #elif defined(HAVE_STATFS) && defined(MNT_LOCAL)
1036 : struct statfs stfs;
1037 :
1038 83 : if (statfs(dbdir, &stfs) == 0) {
1039 83 : if ((stfs.f_flags & MNT_LOCAL) != MNT_LOCAL)
1040 0 : sqlite3_vfs_register(sqlite3_vfs_find("unix-dotfile"), 1);
1041 : }
1042 : #endif
1043 :
1044 83 : if (sqlite3_open(localpath, &db->sqlite) != SQLITE_OK) {
1045 0 : ERROR_SQLITE(db->sqlite, "sqlite open");
1046 0 : pkgdb_close(db);
1047 0 : return (EPKG_FATAL);
1048 : }
1049 :
1050 : /* Wait up to 5 seconds if database is busy */
1051 83 : sqlite3_busy_timeout(db->sqlite, 5000);
1052 :
1053 : /* If the database is missing we have to initialize it */
1054 83 : if (create && pkgdb_init(db->sqlite) != EPKG_OK) {
1055 0 : pkgdb_close(db);
1056 0 : return (EPKG_FATAL);
1057 : }
1058 :
1059 : /* Create our functions */
1060 83 : pkgdb_sqlcmd_init(db->sqlite, NULL, NULL);
1061 :
1062 83 : if (pkgdb_upgrade(db) != EPKG_OK) {
1063 0 : pkgdb_close(db);
1064 0 : return (EPKG_FATAL);
1065 : }
1066 :
1067 : /*
1068 : * allow foreign key option which will allow to have
1069 : * clean support for reinstalling
1070 : */
1071 83 : ret = sql_exec(db->sqlite, "PRAGMA foreign_keys = ON;");
1072 83 : if (ret != EPKG_OK) {
1073 0 : pkgdb_close(db);
1074 0 : return (EPKG_FATAL);
1075 : }
1076 : }
1077 :
1078 83 : if (type == PKGDB_REMOTE || type == PKGDB_MAYBE_REMOTE) {
1079 8 : if (reponame != NULL || pkg_repos_activated_count() > 0) {
1080 8 : ret = pkgdb_open_repos(db, reponame);
1081 16 : if (ret != EPKG_OK) {
1082 0 : pkgdb_close(db);
1083 0 : return (ret);
1084 : }
1085 0 : } else if (type == PKGDB_REMOTE) {
1086 0 : if (*db_p == NULL)
1087 0 : pkgdb_close(db);
1088 0 : pkg_emit_error("No active remote repositories configured");
1089 0 : return (EPKG_FATAL);
1090 : }
1091 : }
1092 :
1093 83 : if (prstmt_initialize(db) != EPKG_OK) {
1094 0 : pkgdb_close(db);
1095 0 : return (EPKG_FATAL);
1096 : }
1097 :
1098 :
1099 83 : profile = pkg_object_bool(pkg_config_get("SQLITE_PROFILE"));
1100 83 : if (profile) {
1101 0 : pkg_debug(1, "pkgdb profiling is enabled");
1102 0 : sqlite3_profile(db->sqlite, pkgdb_profile_callback, NULL);
1103 : }
1104 :
1105 83 : *db_p = db;
1106 83 : return (EPKG_OK);
1107 : }
1108 :
1109 : void
1110 64 : pkgdb_close(struct pkgdb *db)
1111 : {
1112 : struct _pkg_repo_list_item *cur, *tmp;
1113 :
1114 64 : if (db == NULL)
1115 64 : return;
1116 :
1117 64 : if (db->prstmt_initialized)
1118 64 : prstmt_finalize(db);
1119 :
1120 64 : if (db->sqlite != NULL) {
1121 :
1122 72 : LL_FOREACH_SAFE(db->repos, cur, tmp) {
1123 8 : cur->repo->ops->close(cur->repo, false);
1124 8 : free(cur);
1125 : }
1126 :
1127 64 : if (!sqlite3_db_readonly(db->sqlite, "main"))
1128 64 : pkg_plugins_hook_run(PKG_PLUGIN_HOOK_PKGDB_CLOSE_RW, NULL, db);
1129 :
1130 64 : sqlite3_close(db->sqlite);
1131 : }
1132 :
1133 64 : sqlite3_shutdown();
1134 64 : free(db);
1135 : }
1136 :
1137 : /* How many times to try COMMIT or ROLLBACK if the DB is busy */
1138 : #define BUSY_RETRIES 6
1139 : #define BUSY_SLEEP 200
1140 :
1141 : /* This is a MACRO instead of a function as any sqlite3_* function that
1142 : * queries the DB can return SQLITE_BUSY. We would need a function to
1143 : * wrap all sqlite3_* API since we cannot pass anonymous functions/blocks
1144 : * in C. This can be used to wrap existing code. */
1145 : #define PKGDB_SQLITE_RETRY_ON_BUSY(ret) \
1146 : ret = SQLITE_BUSY; \
1147 : for (int _sqlite_busy_retries = 0; \
1148 : _sqlite_busy_retries < BUSY_RETRIES && ret == SQLITE_BUSY; \
1149 : ++_sqlite_busy_retries, ret == SQLITE_BUSY && \
1150 : sqlite3_sleep(BUSY_SLEEP))
1151 :
1152 : int
1153 52 : pkgdb_transaction_begin_sqlite(sqlite3 *sqlite, const char *savepoint)
1154 : {
1155 : int ret;
1156 : sqlite3_stmt *stmt;
1157 : const char *psql;
1158 :
1159 52 : assert(sqlite != NULL);
1160 :
1161 89 : if (savepoint == NULL || savepoint[0] == '\0') {
1162 37 : const char sql[] = "BEGIN IMMEDIATE TRANSACTION";
1163 :
1164 37 : psql = sql;
1165 37 : pkg_debug(4, "Pkgdb: running '%s'", sql);
1166 37 : ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1,
1167 : &stmt, NULL);
1168 : } else {
1169 15 : char sql[128] = "SAVEPOINT ";
1170 :
1171 15 : strlcat(sql, savepoint, sizeof(sql));
1172 :
1173 15 : psql = sql;
1174 15 : pkg_debug(4, "Pkgdb: running '%s'", sql);
1175 15 : ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1,
1176 : &stmt, NULL);
1177 : }
1178 :
1179 52 : if (ret == SQLITE_OK)
1180 104 : PKGDB_SQLITE_RETRY_ON_BUSY(ret)
1181 52 : ret = sqlite3_step(stmt);
1182 :
1183 52 : sqlite3_finalize(stmt);
1184 :
1185 52 : if (ret != SQLITE_OK && ret != SQLITE_DONE)
1186 0 : ERROR_SQLITE(sqlite, psql);
1187 :
1188 52 : return (ret == SQLITE_OK || ret == SQLITE_DONE ? EPKG_OK : EPKG_FATAL);
1189 : }
1190 :
1191 : int
1192 51 : pkgdb_transaction_commit_sqlite(sqlite3 *sqlite, const char *savepoint)
1193 : {
1194 : int ret;
1195 : sqlite3_stmt *stmt;
1196 : const char *psql;
1197 :
1198 51 : assert(sqlite != NULL);
1199 :
1200 87 : if (savepoint == NULL || savepoint[0] == '\0') {
1201 36 : const char sql[] = "COMMIT TRANSACTION";
1202 36 : psql = sql;
1203 :
1204 36 : pkg_debug(4, "Pkgdb: running '%s'", sql);
1205 36 : ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1,
1206 : &stmt, NULL);
1207 : } else {
1208 15 : char sql[128] = "RELEASE SAVEPOINT ";
1209 :
1210 15 : strlcat(sql, savepoint, sizeof(sql));
1211 :
1212 15 : psql = sql;
1213 :
1214 15 : pkg_debug(4, "Pkgdb: running '%s'", sql);
1215 15 : ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1,
1216 : &stmt, NULL);
1217 : }
1218 :
1219 51 : if (ret == SQLITE_OK)
1220 102 : PKGDB_SQLITE_RETRY_ON_BUSY(ret)
1221 51 : ret = sqlite3_step(stmt);
1222 :
1223 51 : sqlite3_finalize(stmt);
1224 :
1225 51 : if (ret != SQLITE_OK && ret != SQLITE_DONE)
1226 0 : ERROR_SQLITE(sqlite, psql);
1227 :
1228 51 : return (ret == SQLITE_OK || ret == SQLITE_DONE ? EPKG_OK : EPKG_FATAL);
1229 : }
1230 :
1231 : int
1232 1 : pkgdb_transaction_rollback_sqlite(sqlite3 *sqlite, const char *savepoint)
1233 : {
1234 : int ret;
1235 : sqlite3_stmt *stmt;
1236 : const char *psql;
1237 :
1238 1 : assert(sqlite != NULL);
1239 :
1240 2 : if (savepoint == NULL || savepoint[0] == '\0') {
1241 1 : const char sql[] = "ROLLBACK TRANSACTION";
1242 :
1243 1 : psql = sql;
1244 1 : pkg_debug(4, "Pkgdb: running '%s'", sql);
1245 1 : ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1,
1246 : &stmt, NULL);
1247 : } else {
1248 0 : char sql[128] = "ROLLBACK TO SAVEPOINT ";
1249 :
1250 0 : strlcat(sql, savepoint, sizeof(sql));
1251 :
1252 0 : psql = sql;
1253 0 : pkg_debug(4, "Pkgdb: running '%s'", sql);
1254 0 : ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1,
1255 : &stmt, NULL);
1256 : }
1257 :
1258 1 : if (ret == SQLITE_OK)
1259 2 : PKGDB_SQLITE_RETRY_ON_BUSY(ret)
1260 1 : ret = sqlite3_step(stmt);
1261 :
1262 1 : sqlite3_finalize(stmt);
1263 :
1264 1 : if (ret != SQLITE_OK && ret != SQLITE_DONE)
1265 0 : ERROR_SQLITE(sqlite, psql);
1266 :
1267 1 : return (ret == SQLITE_OK || ret == SQLITE_DONE ? EPKG_OK : EPKG_FATAL);
1268 : }
1269 :
1270 : /*
1271 : * Public API
1272 : */
1273 : int
1274 4 : pkgdb_transaction_begin(struct pkgdb *db, const char *savepoint)
1275 : {
1276 4 : return (pkgdb_transaction_begin_sqlite(db->sqlite, savepoint));
1277 : }
1278 : int
1279 4 : pkgdb_transaction_commit(struct pkgdb *db, const char *savepoint)
1280 : {
1281 4 : return (pkgdb_transaction_commit_sqlite(db->sqlite, savepoint));
1282 : }
1283 : int
1284 0 : pkgdb_transaction_rollback(struct pkgdb *db, const char *savepoint)
1285 : {
1286 0 : return (pkgdb_transaction_rollback_sqlite(db->sqlite, savepoint));
1287 : }
1288 :
1289 :
1290 : /* By default, MATCH_EXACT and MATCH_REGEX are case sensitive. This
1291 : * is modified in many actions according to the value of
1292 : * CASE_SENSITIVE_MATCH in pkg.conf and then possbily reset again in
1293 : * pkg search et al according to command line flags */
1294 :
1295 : static bool _case_sensitive_flag = false;
1296 :
1297 : void
1298 11 : pkgdb_set_case_sensitivity(bool case_sensitive)
1299 : {
1300 11 : _case_sensitive_flag = case_sensitive;
1301 11 : return;
1302 : }
1303 :
1304 : bool
1305 141 : pkgdb_case_sensitive(void)
1306 : {
1307 141 : return (_case_sensitive_flag);
1308 : }
1309 :
1310 : typedef enum _sql_prstmt_index {
1311 : MTREE = 0,
1312 : PKG,
1313 : DEPS_UPDATE,
1314 : DEPS,
1315 : FILES,
1316 : FILES_REPLACE,
1317 : DIRS1,
1318 : DIRS2,
1319 : CATEGORY1,
1320 : CATEGORY2,
1321 : LICENSES1,
1322 : LICENSES2,
1323 : USERS1,
1324 : USERS2,
1325 : GROUPS1,
1326 : GROUPS2,
1327 : SCRIPT1,
1328 : SCRIPT2,
1329 : OPTION1,
1330 : OPTION2,
1331 : SHLIBS1,
1332 : SHLIBS_REQD,
1333 : SHLIBS_PROV,
1334 : ANNOTATE1,
1335 : ANNOTATE2,
1336 : ANNOTATE_ADD1,
1337 : ANNOTATE_DEL1,
1338 : ANNOTATE_DEL2,
1339 : CONFLICT,
1340 : PKG_PROVIDE,
1341 : PROVIDE,
1342 : FTS_APPEND,
1343 : UPDATE_DIGEST,
1344 : CONFIG_FILES,
1345 : UPDATE_CONFIG_FILE,
1346 : PKG_REQUIRE,
1347 : REQUIRE,
1348 : PRSTMT_LAST,
1349 : } sql_prstmt_index;
1350 :
1351 : static sql_prstmt sql_prepared_statements[PRSTMT_LAST] = {
1352 : [MTREE] = {
1353 : NULL,
1354 : "INSERT OR IGNORE INTO mtree(content) VALUES(?1)",
1355 : "T",
1356 : },
1357 : [PKG] = {
1358 : NULL,
1359 : "INSERT OR REPLACE INTO packages( "
1360 : "origin, name, version, comment, desc, message, arch, "
1361 : "maintainer, www, prefix, flatsize, automatic, "
1362 : "licenselogic, mtree_id, time, manifestdigest, dep_formula) "
1363 : "VALUES( ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, "
1364 : "?13, (SELECT id FROM mtree WHERE content = ?14), NOW(), ?15, ?16)",
1365 : "TTTTTTTTTTIIITTT",
1366 : },
1367 : [DEPS_UPDATE] = {
1368 : NULL,
1369 : "UPDATE deps SET origin=?1, version=?2 WHERE name=?3;",
1370 : "TTT",
1371 : },
1372 : [DEPS] = {
1373 : NULL,
1374 : "INSERT INTO deps (origin, name, version, package_id) "
1375 : "VALUES (?1, ?2, ?3, ?4)",
1376 : "TTTI",
1377 : },
1378 : [FILES] = {
1379 : NULL,
1380 : "INSERT INTO files (path, sha256, package_id) "
1381 : "VALUES (?1, ?2, ?3)",
1382 : "TTI",
1383 : },
1384 : [FILES_REPLACE] = {
1385 : NULL,
1386 : "INSERT OR REPLACE INTO files (path, sha256, package_id) "
1387 : "VALUES (?1, ?2, ?3)",
1388 : "TTI",
1389 : },
1390 : [DIRS1] = {
1391 : NULL,
1392 : "INSERT OR IGNORE INTO directories(path) VALUES(?1)",
1393 : "T",
1394 : },
1395 : [DIRS2] = {
1396 : NULL,
1397 : "INSERT INTO pkg_directories(package_id, directory_id, try) "
1398 : "VALUES (?1, "
1399 : "(SELECT id FROM directories WHERE path = ?2), ?3)",
1400 : "ITI",
1401 : },
1402 : [CATEGORY1] = {
1403 : NULL,
1404 : "INSERT OR IGNORE INTO categories(name) VALUES(?1)",
1405 : "T",
1406 : },
1407 : [CATEGORY2] = {
1408 : NULL,
1409 : "INSERT INTO pkg_categories(package_id, category_id) "
1410 : "VALUES (?1, (SELECT id FROM categories WHERE name = ?2))",
1411 : "IT",
1412 : },
1413 : [LICENSES1] = {
1414 : NULL,
1415 : "INSERT OR IGNORE INTO licenses(name) VALUES(?1)",
1416 : "T",
1417 : },
1418 : [LICENSES2] = {
1419 : NULL,
1420 : "INSERT INTO pkg_licenses(package_id, license_id) "
1421 : "VALUES (?1, (SELECT id FROM licenses WHERE name = ?2))",
1422 : "IT",
1423 : },
1424 : [USERS1] = {
1425 : NULL,
1426 : "INSERT OR IGNORE INTO users(name) VALUES(?1)",
1427 : "T",
1428 : },
1429 : [USERS2] = {
1430 : NULL,
1431 : "INSERT INTO pkg_users(package_id, user_id) "
1432 : "VALUES (?1, (SELECT id FROM users WHERE name = ?2))",
1433 : "IT",
1434 : },
1435 : [GROUPS1] = {
1436 : NULL,
1437 : "INSERT OR IGNORE INTO groups(name) VALUES(?1)",
1438 : "T",
1439 : },
1440 : [GROUPS2] = {
1441 : NULL,
1442 : "INSERT INTO pkg_groups(package_id, group_id) "
1443 : "VALUES (?1, (SELECT id FROM groups WHERE name = ?2))",
1444 : "IT",
1445 : },
1446 : [SCRIPT1] = {
1447 : NULL,
1448 : "INSERT OR IGNORE INTO script(script) VALUES (?1)",
1449 : "T",
1450 : },
1451 : [SCRIPT2] = {
1452 : NULL,
1453 : "INSERT INTO pkg_script(script_id, package_id, type) "
1454 : "VALUES ((SELECT script_id FROM script WHERE script = ?1), "
1455 : "?2, ?3)",
1456 : "TII",
1457 : },
1458 : [OPTION1] = {
1459 : NULL,
1460 : "INSERT OR IGNORE INTO option (option) "
1461 : "VALUES (?1)",
1462 : "T",
1463 : },
1464 : [OPTION2] = {
1465 : NULL,
1466 : "INSERT INTO pkg_option(package_id, option_id, value) "
1467 : "VALUES (?1, "
1468 : "(SELECT option_id FROM option WHERE option = ?2),"
1469 : "?3)",
1470 : "ITT",
1471 : },
1472 : [SHLIBS1] = {
1473 : NULL,
1474 : "INSERT OR IGNORE INTO shlibs(name) VALUES(?1)",
1475 : "T",
1476 : },
1477 : [SHLIBS_REQD] = {
1478 : NULL,
1479 : "INSERT OR IGNORE INTO pkg_shlibs_required(package_id, shlib_id) "
1480 : "VALUES (?1, (SELECT id FROM shlibs WHERE name = ?2))",
1481 : "IT",
1482 : },
1483 : [SHLIBS_PROV] = {
1484 : NULL,
1485 : "INSERT OR IGNORE INTO pkg_shlibs_provided(package_id, shlib_id) "
1486 : "VALUES (?1, (SELECT id FROM shlibs WHERE name = ?2))",
1487 : "IT",
1488 : },
1489 : [ANNOTATE1] = {
1490 : NULL,
1491 : "INSERT OR IGNORE INTO annotation(annotation) "
1492 : "VALUES (?1)",
1493 : "T",
1494 : },
1495 : [ANNOTATE2] = {
1496 : NULL,
1497 : "INSERT OR ROLLBACK INTO pkg_annotation(package_id, tag_id, value_id) "
1498 : "VALUES (?1,"
1499 : " (SELECT annotation_id FROM annotation WHERE annotation = ?2),"
1500 : " (SELECT annotation_id FROM annotation WHERE annotation = ?3))",
1501 : "ITT",
1502 : },
1503 : [ANNOTATE_ADD1] = {
1504 : NULL,
1505 : "INSERT OR IGNORE INTO pkg_annotation(package_id, tag_id, value_id) "
1506 : "VALUES ("
1507 : " (SELECT id FROM packages WHERE name = ?1 ),"
1508 : " (SELECT annotation_id FROM annotation WHERE annotation = ?2),"
1509 : " (SELECT annotation_id FROM annotation WHERE annotation = ?3))",
1510 : "TTTT",
1511 : },
1512 : [ANNOTATE_DEL1] = {
1513 : NULL,
1514 : "DELETE FROM pkg_annotation WHERE "
1515 : "package_id IN"
1516 : " (SELECT id FROM packages WHERE name = ?1) "
1517 : "AND tag_id IN"
1518 : " (SELECT annotation_id FROM annotation WHERE annotation = ?2)",
1519 : "TTT",
1520 : },
1521 : [ANNOTATE_DEL2] = {
1522 : NULL,
1523 : "DELETE FROM annotation WHERE"
1524 : " annotation_id NOT IN (SELECT tag_id FROM pkg_annotation) AND"
1525 : " annotation_id NOT IN (SELECT value_id FROM pkg_annotation)",
1526 : "",
1527 : },
1528 : [CONFLICT] = {
1529 : NULL,
1530 : "INSERT INTO pkg_conflicts(package_id, conflict_id) "
1531 : "VALUES (?1, (SELECT id FROM packages WHERE name = ?2))",
1532 : "IT",
1533 : },
1534 : [PKG_PROVIDE] = {
1535 : NULL,
1536 : "INSERT INTO pkg_provides(package_id, provide_id) "
1537 : "VALUES (?1, (SELECT id FROM provides WHERE provide = ?2))",
1538 : "IT",
1539 : },
1540 : [PROVIDE] = {
1541 : NULL,
1542 : "INSERT OR IGNORE INTO provides(provide) VALUES(?1)",
1543 : "T",
1544 : },
1545 : [FTS_APPEND] = {
1546 : NULL,
1547 : "INSERT OR ROLLBACK INTO pkg_search(id, name, origin) "
1548 : "VALUES (?1, ?2 || '-' || ?3, ?4);",
1549 : "ITTT"
1550 : },
1551 : [UPDATE_DIGEST] = {
1552 : NULL,
1553 : "UPDATE packages SET manifestdigest=?1 WHERE id=?2;",
1554 : "TI"
1555 : },
1556 : [CONFIG_FILES] = {
1557 : NULL,
1558 : "INSERT INTO config_files(path, content, package_id) "
1559 : "VALUES (?1, ?2, ?3);",
1560 : "TTI"
1561 : },
1562 : [UPDATE_CONFIG_FILE] = {
1563 : NULL,
1564 : "UPDATE config_files SET content=?1 WHERE path=?2;",
1565 : "TT"
1566 : },
1567 : [PKG_REQUIRE] = {
1568 : NULL,
1569 : "INSERT INTO pkg_requires(package_id, require_id) "
1570 : "VALUES (?1, (SELECT id FROM requires WHERE require = ?2))",
1571 : "IT",
1572 : },
1573 : [REQUIRE] = {
1574 : NULL,
1575 : "INSERT OR IGNORE INTO requires(require) VALUES(?1)",
1576 : "T"
1577 : }
1578 : /* PRSTMT_LAST */
1579 : };
1580 :
1581 : static int
1582 83 : prstmt_initialize(struct pkgdb *db)
1583 : {
1584 : sql_prstmt_index i;
1585 : sqlite3 *sqlite;
1586 : int ret;
1587 :
1588 83 : assert(db != NULL);
1589 :
1590 83 : if (!db->prstmt_initialized) {
1591 83 : sqlite = db->sqlite;
1592 :
1593 3154 : for (i = 0; i < PRSTMT_LAST; i++)
1594 : {
1595 3071 : pkg_debug(4, "Pkgdb: preparing statement '%s'", SQL(i));
1596 3071 : ret = sqlite3_prepare_v2(sqlite, SQL(i), -1, &STMT(i), NULL);
1597 3071 : if (ret != SQLITE_OK) {
1598 0 : ERROR_SQLITE(sqlite, SQL(i));
1599 0 : return (EPKG_FATAL);
1600 : }
1601 : }
1602 83 : db->prstmt_initialized = true;
1603 : }
1604 :
1605 83 : return (EPKG_OK);
1606 : }
1607 :
1608 : static int
1609 610 : run_prstmt(sql_prstmt_index s, ...)
1610 : {
1611 : int retcode; /* Returns SQLITE error code */
1612 : va_list ap;
1613 : sqlite3_stmt *stmt;
1614 : int i;
1615 : const char *argtypes;
1616 :
1617 610 : stmt = STMT(s);
1618 610 : argtypes = sql_prepared_statements[s].argtypes;
1619 :
1620 610 : sqlite3_reset(stmt);
1621 :
1622 610 : va_start(ap, s);
1623 :
1624 2571 : for (i = 0; argtypes[i] != '\0'; i++)
1625 : {
1626 1961 : switch (argtypes[i]) {
1627 : case 'T':
1628 1484 : sqlite3_bind_text(stmt, i + 1, va_arg(ap, const char*),
1629 : -1, SQLITE_STATIC);
1630 1484 : break;
1631 : case 'I':
1632 477 : sqlite3_bind_int64(stmt, i + 1, va_arg(ap, int64_t));
1633 477 : break;
1634 : }
1635 : }
1636 :
1637 610 : va_end(ap);
1638 :
1639 610 : retcode = sqlite3_step(stmt);
1640 :
1641 610 : return (retcode);
1642 : }
1643 :
1644 : static void
1645 64 : prstmt_finalize(struct pkgdb *db)
1646 : {
1647 : sql_prstmt_index i;
1648 :
1649 2432 : for (i = 0; i < PRSTMT_LAST; i++)
1650 : {
1651 2368 : if (STMT(i) != NULL) {
1652 2368 : sqlite3_finalize(STMT(i));
1653 2368 : STMT(i) = NULL;
1654 : }
1655 : }
1656 64 : db->prstmt_initialized = false;
1657 64 : return;
1658 : }
1659 :
1660 : int
1661 40 : pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
1662 : {
1663 40 : struct pkg *pkg2 = NULL;
1664 : struct pkg_strel *el;
1665 40 : struct pkg_dep *dep = NULL;
1666 40 : struct pkg_file *file = NULL;
1667 40 : struct pkg_dir *dir = NULL;
1668 40 : struct pkg_option *option = NULL;
1669 40 : struct pkg_conflict *conflict = NULL;
1670 40 : struct pkg_config_file *cf = NULL;
1671 40 : struct pkgdb_it *it = NULL;
1672 : char *buf;
1673 :
1674 : sqlite3 *s;
1675 :
1676 : int ret;
1677 40 : int retcode = EPKG_FATAL;
1678 : int64_t package_id;
1679 :
1680 : const char *arch;
1681 :
1682 40 : assert(db != NULL);
1683 :
1684 40 : if (pkg_is_valid(pkg) != EPKG_OK) {
1685 0 : pkg_emit_error("the package is not valid");
1686 0 : return (EPKG_FATAL);
1687 : }
1688 :
1689 40 : s = db->sqlite;
1690 :
1691 40 : if (!complete && pkgdb_transaction_begin_sqlite(s, NULL) != EPKG_OK)
1692 0 : return (EPKG_FATAL);
1693 :
1694 : /* Prefer new ABI over old one */
1695 40 : arch = pkg->abi != NULL ? pkg->abi : pkg->arch;
1696 :
1697 : /*
1698 : * Insert package record
1699 : */
1700 120 : ret = run_prstmt(PKG, pkg->origin, pkg->name, pkg->version,
1701 : pkg->comment, pkg->desc, pkg->message, arch, pkg->maintainer,
1702 40 : pkg->www, pkg->prefix, pkg->flatsize, (int64_t)pkg->automatic,
1703 40 : (int64_t)pkg->licenselogic, NULL, pkg->digest, pkg->dep_formula);
1704 40 : if (ret != SQLITE_DONE) {
1705 0 : ERROR_SQLITE(s, SQL(PKG));
1706 0 : goto cleanup;
1707 : }
1708 :
1709 40 : package_id = sqlite3_last_insert_rowid(s);
1710 :
1711 40 : if (run_prstmt(FTS_APPEND, package_id, pkg->name, pkg->version,
1712 : pkg->origin) != SQLITE_DONE) {
1713 0 : ERROR_SQLITE(s, SQL(FTS_APPEND));
1714 0 : goto cleanup;
1715 : }
1716 :
1717 : /*
1718 : * update dep informations on packages that depends on the insert
1719 : * package
1720 : */
1721 :
1722 80 : if (run_prstmt(DEPS_UPDATE, pkg->origin,
1723 40 : pkg->version ? pkg->version : "", pkg->name)
1724 : != SQLITE_DONE) {
1725 0 : ERROR_SQLITE(s, SQL(DEPS_UPDATE));
1726 0 : goto cleanup;
1727 : }
1728 :
1729 : /*
1730 : * Insert dependencies list
1731 : */
1732 :
1733 96 : while (pkg_deps(pkg, &dep) == EPKG_OK) {
1734 31 : if (run_prstmt(DEPS, dep->origin, dep->name,
1735 31 : dep->version ? dep->version : "",
1736 : package_id) != SQLITE_DONE) {
1737 0 : ERROR_SQLITE(s, SQL(DEPS));
1738 0 : goto cleanup;
1739 : }
1740 : }
1741 :
1742 : /*
1743 : * Insert files.
1744 : */
1745 :
1746 185 : while (pkg_files(pkg, &file) == EPKG_OK) {
1747 106 : bool permissive = false;
1748 106 : bool devmode = false;
1749 :
1750 106 : ret = run_prstmt(FILES, file->path, file->sum, package_id);
1751 106 : if (ret == SQLITE_DONE)
1752 105 : continue;
1753 1 : if (ret != SQLITE_CONSTRAINT) {
1754 0 : ERROR_SQLITE(s, SQL(FILES));
1755 0 : goto cleanup;
1756 : }
1757 1 : it = pkgdb_query_which(db, file->path, false);
1758 1 : if (it == NULL) {
1759 0 : ERROR_SQLITE(s, "pkg which");
1760 0 : goto cleanup;
1761 : }
1762 1 : pkg2 = NULL;
1763 1 : ret = pkgdb_it_next(it, &pkg2, PKG_LOAD_BASIC);
1764 1 : if (ret == EPKG_END) {
1765 : /* Stray entry in the files table not related to
1766 : any known package: overwrite this */
1767 0 : ret = run_prstmt(FILES_REPLACE, file->path, file->sum,
1768 : package_id);
1769 0 : pkgdb_it_free(it);
1770 0 : if (ret == SQLITE_DONE)
1771 0 : continue;
1772 : else {
1773 0 : ERROR_SQLITE(s, SQL(FILES_REPLACE));
1774 0 : goto cleanup;
1775 : }
1776 : }
1777 1 : if (ret != EPKG_OK && ret != EPKG_END) {
1778 0 : pkgdb_it_free(it);
1779 0 : ERROR_SQLITE(s, SQL(FILES_REPLACE));
1780 0 : goto cleanup;
1781 : }
1782 1 : if (!forced) {
1783 1 : devmode = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
1784 1 : if (!devmode)
1785 1 : permissive = pkg_object_bool(pkg_config_get("PERMISSIVE"));
1786 3 : pkg_emit_error("%s-%s conflicts with %s-%s"
1787 : " (installs files into the same place). "
1788 : " Problematic file: %s%s",
1789 2 : pkg->name, pkg->version, pkg2->name, pkg2->version, file->path,
1790 : permissive ? " ignored by permissive mode" : "");
1791 1 : pkg_free(pkg2);
1792 1 : if (!permissive) {
1793 1 : pkgdb_it_free(it);
1794 1 : goto cleanup;
1795 : }
1796 : } else {
1797 0 : pkg_emit_error("%s-%s conflicts with %s-%s"
1798 : " (installs files into the same place). "
1799 : " Problematic file: %s ignored by forced mode",
1800 0 : pkg->name, pkg->version, pkg2->name, pkg2->version, file->path);
1801 0 : pkg_free(pkg2);
1802 : }
1803 0 : pkgdb_it_free(it);
1804 : }
1805 :
1806 : /*
1807 : * Insert config files
1808 : */
1809 78 : while (pkg_config_files(pkg, &cf) == EPKG_OK) {
1810 0 : if ((ret = run_prstmt(CONFIG_FILES, cf->path, cf->content, package_id)
1811 0 : != SQLITE_DONE)) {
1812 0 : if (ret == SQLITE_CONSTRAINT) {
1813 0 : pkg_emit_error("Another package already owns :%s",
1814 0 : cf->path);
1815 : } else
1816 0 : ERROR_SQLITE(s, SQL(CONFIG_FILES));
1817 0 : goto cleanup;
1818 : }
1819 : }
1820 :
1821 : /*
1822 : * Insert dirs.
1823 : */
1824 :
1825 87 : while (pkg_dirs(pkg, &dir) == EPKG_OK) {
1826 9 : if (run_prstmt(DIRS1, dir->path) != SQLITE_DONE) {
1827 0 : ERROR_SQLITE(s, SQL(DIRS1));
1828 0 : goto cleanup;
1829 : }
1830 9 : if ((ret = run_prstmt(DIRS2, package_id, dir->path,
1831 : true)) != SQLITE_DONE) {
1832 0 : if (ret == SQLITE_CONSTRAINT) {
1833 0 : pkg_emit_error("Another package is already "
1834 : "providing directory: %s",
1835 0 : dir->path);
1836 : } else
1837 0 : ERROR_SQLITE(s, SQL(DIRS2));
1838 0 : goto cleanup;
1839 : }
1840 : }
1841 :
1842 : /*
1843 : * Insert categories
1844 : */
1845 :
1846 76 : LL_FOREACH(pkg->categories, el) {
1847 37 : ret = run_prstmt(CATEGORY1, el->value);
1848 37 : if (ret == SQLITE_DONE)
1849 37 : ret = run_prstmt(CATEGORY2, package_id, el->value);
1850 37 : if (ret != SQLITE_DONE) {
1851 0 : ERROR_SQLITE(s, SQL(CATEGORY2));
1852 0 : goto cleanup;
1853 : }
1854 : }
1855 :
1856 : /*
1857 : * Insert licenses
1858 : */
1859 :
1860 41 : LL_FOREACH(pkg->licenses, el) {
1861 2 : if (run_prstmt(LICENSES1, el->value)
1862 : != SQLITE_DONE
1863 2 : ||
1864 2 : run_prstmt(LICENSES2, package_id, el->value)
1865 : != SQLITE_DONE) {
1866 0 : ERROR_SQLITE(s, SQL(LICENSES2));
1867 0 : goto cleanup;
1868 : }
1869 : }
1870 :
1871 : /*
1872 : * Insert users
1873 : */
1874 :
1875 39 : buf = NULL;
1876 78 : while (pkg_users(pkg, &buf) == EPKG_OK) {
1877 0 : if (run_prstmt(USERS1, buf)
1878 : != SQLITE_DONE
1879 0 : ||
1880 0 : run_prstmt(USERS2, package_id, buf)
1881 : != SQLITE_DONE) {
1882 0 : ERROR_SQLITE(s, SQL(USERS2));
1883 0 : goto cleanup;
1884 : }
1885 : }
1886 :
1887 : /*
1888 : * Insert groups
1889 : */
1890 :
1891 39 : buf = NULL;
1892 78 : while (pkg_groups(pkg, &buf) == EPKG_OK) {
1893 0 : if (run_prstmt(GROUPS1, buf)
1894 : != SQLITE_DONE
1895 0 : ||
1896 0 : run_prstmt(GROUPS2, package_id, buf)
1897 : != SQLITE_DONE) {
1898 0 : ERROR_SQLITE(s, SQL(GROUPS2));
1899 0 : goto cleanup;
1900 : }
1901 : }
1902 :
1903 : /*
1904 : * Insert scripts
1905 : */
1906 :
1907 39 : if (pkgdb_insert_scripts(pkg, package_id, s) != EPKG_OK)
1908 0 : goto cleanup;
1909 :
1910 : /*
1911 : * Insert options
1912 : */
1913 :
1914 128 : while (pkg_options(pkg, &option) == EPKG_OK) {
1915 50 : if (run_prstmt(OPTION1, option->key) != SQLITE_DONE
1916 50 : ||
1917 50 : run_prstmt(OPTION2, package_id, option->key, option->value)
1918 : != SQLITE_DONE) {
1919 0 : ERROR_SQLITE(s, SQL(OPTION2));
1920 0 : goto cleanup;
1921 : }
1922 : }
1923 :
1924 : /*
1925 : * Insert shlibs
1926 : */
1927 39 : if (pkgdb_update_shlibs_required(pkg, package_id, s) != EPKG_OK)
1928 0 : goto cleanup;
1929 39 : if (pkgdb_update_shlibs_provided(pkg, package_id, s) != EPKG_OK)
1930 0 : goto cleanup;
1931 :
1932 : /*
1933 : * Insert annotation
1934 : */
1935 39 : if (pkgdb_insert_annotations(pkg, package_id, s) != EPKG_OK)
1936 0 : goto cleanup;
1937 :
1938 : /*
1939 : * Insert conflicts
1940 : */
1941 78 : while (pkg_conflicts(pkg, &conflict) == EPKG_OK) {
1942 0 : if (run_prstmt(CONFLICT, package_id, conflict->uid)
1943 : != SQLITE_DONE) {
1944 0 : ERROR_SQLITE(s, SQL(CONFLICT));
1945 0 : goto cleanup;
1946 : }
1947 : }
1948 :
1949 : /*
1950 : * Insert provides
1951 : */
1952 39 : if (pkgdb_update_provides(pkg, package_id, s) != EPKG_OK)
1953 0 : goto cleanup;
1954 39 : if (pkgdb_update_requires(pkg, package_id, s) != EPKG_OK)
1955 0 : goto cleanup;
1956 :
1957 39 : retcode = EPKG_OK;
1958 :
1959 : cleanup:
1960 :
1961 40 : return (retcode);
1962 : }
1963 :
1964 : static int
1965 39 : pkgdb_insert_scripts(struct pkg *pkg, int64_t package_id, sqlite3 *s)
1966 : {
1967 : const char *script;
1968 : int64_t i;
1969 :
1970 390 : for (i = 0; i < PKG_NUM_SCRIPTS; i++) {
1971 351 : script = pkg_script_get(pkg, i);
1972 :
1973 351 : if (script == NULL)
1974 327 : continue;
1975 24 : if (run_prstmt(SCRIPT1, script) != SQLITE_DONE
1976 24 : ||
1977 24 : run_prstmt(SCRIPT2, script, package_id, i) != SQLITE_DONE) {
1978 0 : ERROR_SQLITE(s, SQL(SCRIPT2));
1979 0 : return (EPKG_FATAL);
1980 : }
1981 : }
1982 :
1983 39 : return (EPKG_OK);
1984 : }
1985 :
1986 : int
1987 39 : pkgdb_update_shlibs_required(struct pkg *pkg, int64_t package_id, sqlite3 *s)
1988 : {
1989 39 : char *shlib = NULL;
1990 :
1991 78 : while (pkg_shlibs_required(pkg, &shlib) == EPKG_OK) {
1992 0 : if (run_prstmt(SHLIBS1, shlib)
1993 : != SQLITE_DONE
1994 0 : ||
1995 0 : run_prstmt(SHLIBS_REQD, package_id, shlib)
1996 : != SQLITE_DONE) {
1997 0 : ERROR_SQLITE(s, SQL(SHLIBS_REQD));
1998 0 : return (EPKG_FATAL);
1999 : }
2000 : }
2001 :
2002 39 : return (EPKG_OK);
2003 : }
2004 :
2005 : int
2006 21 : pkgdb_update_config_file_content(struct pkg *p, sqlite3 *s)
2007 : {
2008 21 : struct pkg_config_file *cf = NULL;
2009 :
2010 42 : while (pkg_config_files(p, &cf) == EPKG_OK) {
2011 0 : if (run_prstmt(UPDATE_CONFIG_FILE, cf->content, cf->path)
2012 : != SQLITE_DONE) {
2013 0 : ERROR_SQLITE(s, SQL(SHLIBS_REQD));
2014 0 : return (EPKG_FATAL);
2015 : }
2016 : }
2017 :
2018 21 : return (EPKG_OK);
2019 : }
2020 :
2021 : int
2022 39 : pkgdb_update_shlibs_provided(struct pkg *pkg, int64_t package_id, sqlite3 *s)
2023 : {
2024 39 : char *shlib = NULL;
2025 :
2026 82 : while (pkg_shlibs_provided(pkg, &shlib) == EPKG_OK) {
2027 4 : if (run_prstmt(SHLIBS1, shlib)
2028 : != SQLITE_DONE
2029 4 : ||
2030 4 : run_prstmt(SHLIBS_PROV, package_id, shlib)
2031 : != SQLITE_DONE) {
2032 0 : ERROR_SQLITE(s, SQL(SHLIBS_PROV));
2033 0 : return (EPKG_FATAL);
2034 : }
2035 : }
2036 :
2037 39 : return (EPKG_OK);
2038 : }
2039 :
2040 : int
2041 39 : pkgdb_update_requires(struct pkg *pkg, int64_t package_id, sqlite3 *s)
2042 : {
2043 39 : char *require = NULL;
2044 :
2045 80 : while (pkg_requires(pkg, &require) == EPKG_OK) {
2046 2 : if (run_prstmt(REQUIRE, require)
2047 : != SQLITE_DONE
2048 2 : ||
2049 2 : run_prstmt(PKG_REQUIRE, package_id, require)
2050 : != SQLITE_DONE) {
2051 0 : ERROR_SQLITE(s, SQL(PKG_REQUIRE));
2052 0 : return (EPKG_FATAL);
2053 : }
2054 : }
2055 :
2056 39 : return (EPKG_OK);
2057 : }
2058 :
2059 : int
2060 39 : pkgdb_update_provides(struct pkg *pkg, int64_t package_id, sqlite3 *s)
2061 : {
2062 39 : char *provide = NULL;
2063 :
2064 79 : while (pkg_provides(pkg, &provide) == EPKG_OK) {
2065 1 : if (run_prstmt(PROVIDE, provide)
2066 : != SQLITE_DONE
2067 1 : ||
2068 1 : run_prstmt(PKG_PROVIDE, package_id, provide)
2069 : != SQLITE_DONE) {
2070 0 : ERROR_SQLITE(s, SQL(PKG_PROVIDE));
2071 0 : return (EPKG_FATAL);
2072 : }
2073 : }
2074 :
2075 39 : return (EPKG_OK);
2076 : }
2077 :
2078 : int
2079 39 : pkgdb_insert_annotations(struct pkg *pkg, int64_t package_id, sqlite3 *s)
2080 : {
2081 : struct pkg_kv *kv;
2082 :
2083 65 : LL_FOREACH(pkg->annotations, kv) {
2084 26 : if (run_prstmt(ANNOTATE1, kv->key)
2085 : != SQLITE_DONE
2086 26 : ||
2087 26 : run_prstmt(ANNOTATE1,kv->value)
2088 : != SQLITE_DONE
2089 26 : ||
2090 26 : run_prstmt(ANNOTATE2, package_id,
2091 : kv->key, kv->value)
2092 : != SQLITE_DONE) {
2093 0 : ERROR_SQLITE(s, SQL(ANNOTATE2));
2094 0 : return (EPKG_FATAL);
2095 : }
2096 : }
2097 39 : return (EPKG_OK);
2098 : }
2099 :
2100 : int
2101 0 : pkgdb_reanalyse_shlibs(struct pkgdb *db, struct pkg *pkg)
2102 : {
2103 : sqlite3 *s;
2104 : int64_t package_id;
2105 0 : int ret = EPKG_OK;
2106 : int i;
2107 0 : const char *sql[] = {
2108 : "DELETE FROM pkg_shlibs_required WHERE package_id = ?1",
2109 :
2110 : "DELETE FROM pkg_shlibs_provided WHERE package_id = ?1",
2111 :
2112 : "DELETE FROM shlibs "
2113 : "WHERE id NOT IN "
2114 : "(SELECT DISTINCT shlib_id FROM pkg_shlibs_required)"
2115 : "AND id NOT IN "
2116 : "(SELECT DISTINCT shlib_id FROM pkg_shlibs_provided)",
2117 : };
2118 :
2119 : sqlite3_stmt *stmt_del;
2120 :
2121 0 : assert(db != NULL);
2122 :
2123 0 : if (pkg_is_valid(pkg) != EPKG_OK) {
2124 0 : pkg_emit_error("the package is not valid");
2125 0 : return (EPKG_FATAL);
2126 : }
2127 :
2128 0 : if ((ret = pkg_analyse_files(db, pkg, NULL)) == EPKG_OK) {
2129 0 : s = db->sqlite;
2130 0 : package_id = pkg->id;
2131 :
2132 0 : for (i = 0; i < 2; i++) {
2133 : /* Clean out old shlibs first */
2134 0 : pkg_debug(4, "Pkgdb: running '%s'", sql[i]);
2135 0 : if (sqlite3_prepare_v2(db->sqlite, sql[i], -1,
2136 : &stmt_del, NULL)
2137 : != SQLITE_OK) {
2138 0 : ERROR_SQLITE(db->sqlite, sql[i]);
2139 0 : return (EPKG_FATAL);
2140 : }
2141 :
2142 0 : sqlite3_bind_int64(stmt_del, 1, package_id);
2143 :
2144 0 : ret = sqlite3_step(stmt_del);
2145 0 : sqlite3_finalize(stmt_del);
2146 :
2147 0 : if (ret != SQLITE_DONE) {
2148 0 : ERROR_SQLITE(db->sqlite, sql[i]);
2149 0 : return (EPKG_FATAL);
2150 : }
2151 : }
2152 :
2153 0 : if (sql_exec(db->sqlite, sql[2]) != EPKG_OK)
2154 0 : return (EPKG_FATAL);
2155 :
2156 : /* Save shlibs */
2157 0 : ret = pkgdb_update_shlibs_required(pkg, package_id, s);
2158 0 : if (ret == EPKG_OK)
2159 0 : ret = pkgdb_update_shlibs_provided(pkg, package_id, s);
2160 : }
2161 :
2162 0 : return (ret);
2163 : }
2164 :
2165 : int
2166 4 : pkgdb_add_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
2167 : const char *value)
2168 : {
2169 : int rows_changed;
2170 :
2171 4 : assert(pkg != NULL);
2172 4 : assert(tag != NULL);
2173 4 : assert(value != NULL);
2174 :
2175 4 : if (run_prstmt(ANNOTATE1, tag) != SQLITE_DONE
2176 4 : ||
2177 4 : run_prstmt(ANNOTATE1, value) != SQLITE_DONE
2178 4 : ||
2179 4 : run_prstmt(ANNOTATE_ADD1, pkg->uid, tag, value)
2180 : != SQLITE_DONE) {
2181 0 : ERROR_SQLITE(db->sqlite, SQL(ANNOTATE_ADD1));
2182 0 : pkgdb_transaction_rollback_sqlite(db->sqlite, NULL);
2183 0 : return (EPKG_FATAL);
2184 : }
2185 :
2186 : /* Expect rows_changed == 1 unless there's already an
2187 : annotation using the given tag */
2188 :
2189 4 : rows_changed = sqlite3_changes(db->sqlite);
2190 :
2191 4 : return (rows_changed == 1 ? EPKG_OK : EPKG_WARN);
2192 : }
2193 :
2194 : int
2195 0 : pkgdb_set_pkg_digest(struct pkgdb *db, struct pkg *pkg)
2196 : {
2197 :
2198 0 : assert(pkg != NULL);
2199 0 : assert(db != NULL);
2200 :
2201 0 : if (run_prstmt(UPDATE_DIGEST, pkg->digest, pkg->id) != SQLITE_DONE) {
2202 0 : ERROR_SQLITE(db->sqlite, SQL(UPDATE_DIGEST));
2203 0 : return (EPKG_FATAL);
2204 : }
2205 :
2206 0 : return (EPKG_OK);
2207 : }
2208 :
2209 : int
2210 1 : pkgdb_modify_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag,
2211 : const char *value)
2212 : {
2213 : int rows_changed;
2214 :
2215 1 : assert(pkg!= NULL);
2216 1 : assert(tag != NULL);
2217 1 : assert(value != NULL);
2218 :
2219 1 : if (pkgdb_transaction_begin_sqlite(db->sqlite, NULL) != EPKG_OK)
2220 0 : return (EPKG_FATAL);
2221 :
2222 1 : if (run_prstmt(ANNOTATE_DEL1, pkg->uid, tag) != SQLITE_DONE
2223 1 : ||
2224 1 : run_prstmt(ANNOTATE1, tag) != SQLITE_DONE
2225 1 : ||
2226 1 : run_prstmt(ANNOTATE1, value) != SQLITE_DONE
2227 1 : ||
2228 1 : run_prstmt(ANNOTATE_ADD1, pkg->uid, tag, value) !=
2229 : SQLITE_DONE
2230 1 : ||
2231 1 : run_prstmt(ANNOTATE_DEL2) != SQLITE_DONE) {
2232 0 : ERROR_SQLITE(db->sqlite, SQL(ANNOTATE_DEL2));
2233 0 : pkgdb_transaction_rollback_sqlite(db->sqlite, NULL);
2234 :
2235 0 : return (EPKG_FATAL);
2236 : }
2237 :
2238 : /* Something has gone very wrong if rows_changed != 1 here */
2239 :
2240 1 : rows_changed = sqlite3_changes(db->sqlite);
2241 :
2242 1 : if (pkgdb_transaction_commit_sqlite(db->sqlite, NULL) != EPKG_OK)
2243 0 : return (EPKG_FATAL);
2244 :
2245 1 : return (rows_changed == 1 ? EPKG_OK : EPKG_WARN);
2246 : }
2247 :
2248 : int
2249 4 : pkgdb_delete_annotation(struct pkgdb *db, struct pkg *pkg, const char *tag)
2250 : {
2251 : int rows_changed;
2252 : bool result;
2253 :
2254 4 : assert(pkg != NULL);
2255 4 : assert(tag != NULL);
2256 :
2257 4 : if (pkgdb_transaction_begin_sqlite(db->sqlite, NULL) != EPKG_OK)
2258 0 : return (EPKG_FATAL);
2259 :
2260 4 : result = (run_prstmt(ANNOTATE_DEL1, pkg->uid, tag)
2261 : == SQLITE_DONE);
2262 :
2263 4 : rows_changed = sqlite3_changes(db->sqlite);
2264 :
2265 4 : if (!result
2266 4 : ||
2267 4 : run_prstmt(ANNOTATE_DEL2) != SQLITE_DONE) {
2268 0 : ERROR_SQLITE(db->sqlite, SQL(ANNOTATE_DEL2));
2269 0 : pkgdb_transaction_rollback_sqlite(db->sqlite, NULL);
2270 0 : return (EPKG_FATAL);
2271 : }
2272 :
2273 4 : if (pkgdb_transaction_commit_sqlite(db->sqlite, NULL) != EPKG_OK)
2274 0 : return (EPKG_FATAL);
2275 :
2276 4 : return (rows_changed == 1 ? EPKG_OK : EPKG_WARN);
2277 : }
2278 :
2279 :
2280 : int
2281 28 : pkgdb_register_finale(struct pkgdb *db, int retcode)
2282 : {
2283 28 : int ret = EPKG_OK;
2284 :
2285 28 : assert(db != NULL);
2286 :
2287 28 : if (retcode == EPKG_OK)
2288 27 : ret = pkgdb_transaction_commit_sqlite(db->sqlite, NULL);
2289 : else
2290 1 : ret = pkgdb_transaction_rollback_sqlite(db->sqlite, NULL);
2291 :
2292 28 : return (ret);
2293 : }
2294 :
2295 : int
2296 0 : pkgdb_register_ports(struct pkgdb *db, struct pkg *pkg)
2297 : {
2298 : int ret;
2299 :
2300 0 : pkg_emit_install_begin(pkg);
2301 :
2302 0 : ret = pkgdb_register_pkg(db, pkg, 0, 0);
2303 0 : if (ret == EPKG_OK)
2304 0 : pkg_emit_install_finished(pkg);
2305 :
2306 0 : pkgdb_register_finale(db, ret);
2307 :
2308 0 : return (ret);
2309 : }
2310 :
2311 : int
2312 6 : pkgdb_unregister_pkg(struct pkgdb *db, int64_t id)
2313 : {
2314 : sqlite3_stmt *stmt_del;
2315 : unsigned int obj;
2316 : int ret;
2317 6 : const char sql[] = ""
2318 : "DELETE FROM packages WHERE id = ?1;";
2319 6 : const char *deletions[] = {
2320 : "directories WHERE id NOT IN "
2321 : "(SELECT DISTINCT directory_id FROM pkg_directories)",
2322 : "categories WHERE id NOT IN "
2323 : "(SELECT DISTINCT category_id FROM pkg_categories)",
2324 : "licenses WHERE id NOT IN "
2325 : "(SELECT DISTINCT license_id FROM pkg_licenses)",
2326 : "mtree WHERE id NOT IN "
2327 : "(SELECT DISTINCT mtree_id FROM packages)",
2328 : /* TODO print the users that are not used anymore */
2329 : "users WHERE id NOT IN "
2330 : "(SELECT DISTINCT user_id FROM pkg_users)",
2331 : /* TODO print the groups trhat are not used anymore */
2332 : "groups WHERE id NOT IN "
2333 : "(SELECT DISTINCT group_id FROM pkg_groups)",
2334 : "shlibs WHERE id NOT IN "
2335 : "(SELECT DISTINCT shlib_id FROM pkg_shlibs_required)"
2336 : "AND id NOT IN "
2337 : "(SELECT DISTINCT shlib_id FROM pkg_shlibs_provided)",
2338 : "script WHERE script_id NOT IN "
2339 : "(SELECT DISTINCT script_id FROM pkg_script)",
2340 : };
2341 :
2342 6 : assert(db != NULL);
2343 :
2344 6 : pkg_debug(4, "Pkgdb: running '%s'", sql);
2345 6 : if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt_del, NULL)
2346 : != SQLITE_OK){
2347 0 : ERROR_SQLITE(db->sqlite, sql);
2348 0 : return (EPKG_FATAL);
2349 : }
2350 :
2351 6 : sqlite3_bind_int64(stmt_del, 1, id);
2352 :
2353 6 : ret = sqlite3_step(stmt_del);
2354 6 : sqlite3_finalize(stmt_del);
2355 :
2356 6 : if (ret != SQLITE_DONE) {
2357 0 : ERROR_SQLITE(db->sqlite, sql);
2358 0 : return (EPKG_FATAL);
2359 : }
2360 :
2361 54 : for (obj = 0 ;obj < NELEM(deletions); obj++) {
2362 48 : ret = sql_exec(db->sqlite, "DELETE FROM %s;", deletions[obj]);
2363 48 : if (ret != EPKG_OK)
2364 0 : return (EPKG_FATAL);
2365 : }
2366 6 : return (EPKG_OK);
2367 : }
2368 :
2369 : int
2370 288 : sql_exec(sqlite3 *s, const char *sql, ...)
2371 : {
2372 : va_list ap;
2373 : const char *sql_to_exec;
2374 288 : char *sqlbuf = NULL;
2375 : char *errmsg;
2376 288 : int ret = EPKG_FATAL;
2377 :
2378 288 : assert(s != NULL);
2379 288 : assert(sql != NULL);
2380 :
2381 288 : if (strchr(sql, '%') != NULL) {
2382 86 : va_start(ap, sql);
2383 86 : sqlbuf = sqlite3_vmprintf(sql, ap);
2384 86 : va_end(ap);
2385 86 : sql_to_exec = sqlbuf;
2386 : } else {
2387 202 : sql_to_exec = sql;
2388 : }
2389 :
2390 288 : pkg_debug(4, "Pkgdb: executing '%s'", sql_to_exec);
2391 288 : if (sqlite3_exec(s, sql_to_exec, NULL, NULL, &errmsg) != SQLITE_OK) {
2392 0 : ERROR_SQLITE(s, sql_to_exec);
2393 0 : sqlite3_free(errmsg);
2394 0 : goto cleanup;
2395 : }
2396 :
2397 288 : ret = EPKG_OK;
2398 :
2399 : cleanup:
2400 288 : if (sqlbuf != NULL)
2401 86 : sqlite3_free(sqlbuf);
2402 :
2403 288 : return (ret);
2404 : }
2405 :
2406 : int
2407 123 : get_pragma(sqlite3 *s, const char *sql, int64_t *res, bool silence)
2408 : {
2409 : sqlite3_stmt *stmt;
2410 : int ret;
2411 :
2412 123 : assert(s != NULL && sql != NULL);
2413 :
2414 123 : pkg_debug(4, "Pkgdb: running '%s'", sql);
2415 123 : if (sqlite3_prepare_v2(s, sql, -1, &stmt, NULL) != SQLITE_OK) {
2416 0 : if (!silence)
2417 0 : ERROR_SQLITE(s, sql);
2418 0 : return (EPKG_OK);
2419 : }
2420 :
2421 246 : PKGDB_SQLITE_RETRY_ON_BUSY(ret)
2422 123 : ret = sqlite3_step(stmt);
2423 :
2424 123 : if (ret == SQLITE_ROW)
2425 123 : *res = sqlite3_column_int64(stmt, 0);
2426 :
2427 123 : sqlite3_finalize(stmt);
2428 :
2429 123 : if (ret != SQLITE_ROW) {
2430 0 : if (!silence)
2431 0 : ERROR_SQLITE(s, sql);
2432 0 : return (EPKG_FATAL);
2433 : }
2434 :
2435 123 : return (EPKG_OK);
2436 : }
2437 :
2438 : int
2439 0 : get_sql_string(sqlite3 *s, const char *sql, char **res)
2440 : {
2441 : sqlite3_stmt *stmt;
2442 : int ret;
2443 :
2444 0 : assert(s != NULL && sql != NULL);
2445 :
2446 0 : pkg_debug(4, "Pkgdb: running '%s'", sql);
2447 0 : if (sqlite3_prepare_v2(s, sql, -1, &stmt, NULL) != SQLITE_OK) {
2448 0 : ERROR_SQLITE(s, sql);
2449 0 : return (EPKG_OK);
2450 : }
2451 :
2452 0 : ret = sqlite3_step(stmt);
2453 :
2454 0 : if (ret == SQLITE_ROW) {
2455 : const unsigned char *tmp;
2456 0 : tmp = sqlite3_column_text(stmt, 0);
2457 0 : *res = (tmp == NULL ? NULL : strdup(tmp));
2458 : }
2459 :
2460 0 : if (ret == SQLITE_DONE)
2461 0 : *res = NULL;
2462 :
2463 0 : sqlite3_finalize(stmt);
2464 :
2465 0 : if (ret != SQLITE_ROW && ret != SQLITE_DONE) {
2466 0 : ERROR_SQLITE(s, sql);
2467 0 : return (EPKG_FATAL);
2468 : }
2469 :
2470 0 : return (EPKG_OK);
2471 : }
2472 :
2473 : int
2474 4 : pkgdb_compact(struct pkgdb *db)
2475 : {
2476 4 : int64_t page_count = 0;
2477 4 : int64_t freelist_count = 0;
2478 : int ret;
2479 :
2480 4 : assert(db != NULL);
2481 :
2482 4 : ret = get_pragma(db->sqlite, "PRAGMA page_count;", &page_count, false);
2483 4 : if (ret != EPKG_OK)
2484 0 : return (EPKG_FATAL);
2485 :
2486 4 : ret = get_pragma(db->sqlite, "PRAGMA freelist_count;",
2487 : &freelist_count, false);
2488 4 : if (ret != EPKG_OK)
2489 0 : return (EPKG_FATAL);
2490 :
2491 : /*
2492 : * Only compact if we will save 25% (or more) of the current
2493 : * used space.
2494 : */
2495 :
2496 4 : if (freelist_count / (float)page_count < 0.25)
2497 4 : return (EPKG_OK);
2498 :
2499 0 : return (sql_exec(db->sqlite, "VACUUM;"));
2500 : }
2501 :
2502 : struct pkgdb_it *
2503 0 : pkgdb_integrity_conflict_local(struct pkgdb *db, const char *uniqueid)
2504 : {
2505 : sqlite3_stmt *stmt;
2506 : int ret;
2507 :
2508 0 : assert(db != NULL && uniqueid != NULL);
2509 :
2510 0 : const char sql_conflicts [] = ""
2511 : "SELECT DISTINCT p.id AS rowid, p.origin, p.name, p.version, "
2512 : "p.prefix "
2513 : "FROM packages AS p, files AS f, integritycheck AS i "
2514 : "WHERE p.id = f.package_id AND f.path = i.path "
2515 : "AND i.uid = ?1 AND "
2516 : "i.uid != p.name" ;
2517 :
2518 0 : pkg_debug(4, "Pkgdb: running '%s'", sql_conflicts);
2519 0 : ret = sqlite3_prepare_v2(db->sqlite, sql_conflicts, -1, &stmt, NULL);
2520 0 : if (ret != SQLITE_OK) {
2521 0 : ERROR_SQLITE(db->sqlite, sql_conflicts);
2522 0 : return (NULL);
2523 : }
2524 :
2525 0 : sqlite3_bind_text(stmt, 1, uniqueid, -1, SQLITE_TRANSIENT);
2526 :
2527 0 : return (pkgdb_it_new_sqlite(db, stmt, PKG_INSTALLED, PKGDB_IT_FLAG_ONCE));
2528 : }
2529 :
2530 : static int
2531 10 : pkgdb_vset(struct pkgdb *db, int64_t id, va_list ap)
2532 : {
2533 : int attr;
2534 : sqlite3_stmt *stmt;
2535 : int64_t flatsize;
2536 : bool automatic, locked;
2537 : char *oldval;
2538 : char *newval;
2539 :
2540 : /* Ensure there is an entry for each of the pkg_set_attr enum values */
2541 10 : const char *sql[PKG_SET_MAX] = {
2542 : [PKG_SET_FLATSIZE] =
2543 : "UPDATE packages SET flatsize = ?1 WHERE id = ?2",
2544 : [PKG_SET_AUTOMATIC] =
2545 : "UPDATE packages SET automatic = ?1 WHERE id = ?2",
2546 : [PKG_SET_LOCKED] =
2547 : "UPDATE packages SET locked = ?1 WHERE id = ?2",
2548 : [PKG_SET_DEPORIGIN] =
2549 : "UPDATE deps SET origin = ?1, "
2550 : "name=(SELECT name FROM packages WHERE origin = ?1), "
2551 : "version=(SELECT version FROM packages WHERE origin = ?1) "
2552 : "WHERE package_id = ?2 AND origin = ?3",
2553 : [PKG_SET_ORIGIN] =
2554 : "UPDATE packages SET origin=?1 WHERE id=?2",
2555 : [PKG_SET_DEPNAME] =
2556 : "UPDATE deps SET name = ?1, "
2557 : "version=(SELECT version FROM packages WHERE name = ?1) "
2558 : "WHERE package_id = ?2 AND name = ?3",
2559 : [PKG_SET_NAME] =
2560 : "UPDATE packages SET name=?1 WHERE id=?2",
2561 : };
2562 :
2563 30 : while ((attr = va_arg(ap, int)) > 0) {
2564 10 : pkg_debug(4, "Pkgdb: running '%s'", sql[attr]);
2565 10 : if (sqlite3_prepare_v2(db->sqlite, sql[attr], -1, &stmt, NULL)
2566 : != SQLITE_OK) {
2567 0 : ERROR_SQLITE(db->sqlite, sql[attr]);
2568 0 : return (EPKG_FATAL);
2569 : }
2570 :
2571 10 : switch (attr) {
2572 : case PKG_SET_FLATSIZE:
2573 0 : flatsize = va_arg(ap, int64_t);
2574 0 : sqlite3_bind_int64(stmt, 1, flatsize);
2575 0 : sqlite3_bind_int64(stmt, 2, id);
2576 0 : break;
2577 : case PKG_SET_AUTOMATIC:
2578 2 : automatic = (bool)va_arg(ap, int);
2579 2 : if (automatic != 0 && automatic != 1) {
2580 0 : sqlite3_finalize(stmt);
2581 0 : continue;
2582 : }
2583 2 : sqlite3_bind_int64(stmt, 1, automatic);
2584 2 : sqlite3_bind_int64(stmt, 2, id);
2585 2 : break;
2586 : case PKG_SET_LOCKED:
2587 6 : locked = (bool)va_arg(ap, int);
2588 6 : if (locked != 0 && locked != 1)
2589 0 : continue;
2590 6 : sqlite3_bind_int64(stmt, 1, locked);
2591 6 : sqlite3_bind_int64(stmt, 2, id);
2592 6 : break;
2593 : case PKG_SET_DEPORIGIN:
2594 : case PKG_SET_DEPNAME:
2595 0 : oldval = va_arg(ap, char *);
2596 0 : newval = va_arg(ap, char *);
2597 0 : sqlite3_bind_text(stmt, 1, newval, -1, SQLITE_STATIC);
2598 0 : sqlite3_bind_int64(stmt, 2, id);
2599 0 : sqlite3_bind_text(stmt, 3, oldval, -1, SQLITE_STATIC);
2600 0 : break;
2601 : case PKG_SET_ORIGIN:
2602 : case PKG_SET_NAME:
2603 2 : newval = va_arg(ap, char *);
2604 2 : sqlite3_bind_text(stmt, 1, newval, -1, SQLITE_STATIC);
2605 2 : sqlite3_bind_int64(stmt, 2, id);
2606 2 : break;
2607 : }
2608 :
2609 10 : if (sqlite3_step(stmt) != SQLITE_DONE) {
2610 0 : ERROR_SQLITE(db->sqlite, sql[attr]);
2611 0 : sqlite3_finalize(stmt);
2612 0 : return (EPKG_FATAL);
2613 : }
2614 :
2615 10 : sqlite3_finalize(stmt);
2616 : }
2617 10 : return (EPKG_OK);
2618 : }
2619 :
2620 : int
2621 10 : pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...)
2622 : {
2623 10 : int ret = EPKG_OK;
2624 : va_list ap;
2625 :
2626 10 : assert(pkg != NULL);
2627 :
2628 10 : va_start(ap, pkg);
2629 10 : ret = pkgdb_vset(db, pkg->id, ap);
2630 10 : va_end(ap);
2631 :
2632 10 : return (ret);
2633 : }
2634 :
2635 : int
2636 0 : pkgdb_file_set_cksum(struct pkgdb *db, struct pkg_file *file,
2637 : const char *sum)
2638 : {
2639 0 : sqlite3_stmt *stmt = NULL;
2640 0 : const char sql_file_update[] = ""
2641 : "UPDATE files SET sha256 = ?1 WHERE path = ?2";
2642 : int ret;
2643 :
2644 0 : pkg_debug(4, "Pkgdb: running '%s'", sql_file_update);
2645 0 : ret = sqlite3_prepare_v2(db->sqlite, sql_file_update, -1, &stmt, NULL);
2646 0 : if (ret != SQLITE_OK) {
2647 0 : ERROR_SQLITE(db->sqlite, sql_file_update);
2648 0 : return (EPKG_FATAL);
2649 : }
2650 0 : sqlite3_bind_text(stmt, 1, sum, -1, SQLITE_STATIC);
2651 0 : sqlite3_bind_text(stmt, 2, file->path, -1, SQLITE_STATIC);
2652 :
2653 0 : if (sqlite3_step(stmt) != SQLITE_DONE) {
2654 0 : ERROR_SQLITE(db->sqlite, sql_file_update);
2655 0 : sqlite3_finalize(stmt);
2656 0 : return (EPKG_FATAL);
2657 : }
2658 0 : sqlite3_finalize(stmt);
2659 0 : file->sum = strdup(sum);
2660 :
2661 0 : return (EPKG_OK);
2662 : }
2663 :
2664 : /*
2665 : * create our custom functions in the sqlite3 connection.
2666 : * Used both in the shell and pkgdb_open
2667 : */
2668 : int
2669 98 : pkgdb_sqlcmd_init(sqlite3 *db, __unused const char **err,
2670 : __unused const void *noused)
2671 : {
2672 98 : sqlite3_create_function(db, "now", 0, SQLITE_ANY|SQLITE_DETERMINISTIC, NULL,
2673 : pkgdb_now, NULL, NULL);
2674 98 : sqlite3_create_function(db, "myarch", 0, SQLITE_ANY|SQLITE_DETERMINISTIC, NULL,
2675 : pkgdb_myarch, NULL, NULL);
2676 98 : sqlite3_create_function(db, "myarch", 1, SQLITE_ANY|SQLITE_DETERMINISTIC, NULL,
2677 : pkgdb_myarch, NULL, NULL);
2678 98 : sqlite3_create_function(db, "regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, NULL,
2679 : pkgdb_regex, NULL, NULL);
2680 98 : sqlite3_create_function(db, "split_version", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, NULL,
2681 : pkgdb_split_version, NULL, NULL);
2682 98 : sqlite3_create_function(db, "vercmp", 3, SQLITE_ANY|SQLITE_DETERMINISTIC, NULL,
2683 : pkgdb_vercmp, NULL, NULL);
2684 :
2685 98 : return SQLITE_OK;
2686 : }
2687 :
2688 : void
2689 0 : pkgdb_cmd(int argc, char **argv)
2690 : {
2691 0 : sqlite3_initialize();
2692 0 : sqlite3_shell(argc, argv);
2693 0 : }
2694 :
2695 : void
2696 0 : pkgshell_open(const char **reponame)
2697 : {
2698 : char localpath[MAXPATHLEN];
2699 : const char *dbdir;
2700 :
2701 0 : sqlite3_auto_extension((void(*)(void))pkgdb_sqlcmd_init);
2702 :
2703 0 : dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
2704 :
2705 0 : snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
2706 0 : *reponame = strdup(localpath);
2707 0 : }
2708 :
2709 : static int
2710 58 : pkgdb_write_lock_pid(struct pkgdb *db)
2711 : {
2712 58 : const char lock_pid_sql[] = ""
2713 : "INSERT INTO pkg_lock_pid VALUES (?1);";
2714 58 : sqlite3_stmt *stmt = NULL;
2715 : int ret;
2716 :
2717 58 : ret = sqlite3_prepare_v2(db->sqlite, lock_pid_sql, -1, &stmt, NULL);
2718 58 : if (ret != SQLITE_OK) {
2719 0 : ERROR_SQLITE(db->sqlite, lock_pid_sql);
2720 0 : return (EPKG_FATAL);
2721 : }
2722 58 : sqlite3_bind_int64(stmt, 1, (int64_t)getpid());
2723 :
2724 58 : if (sqlite3_step(stmt) != SQLITE_DONE) {
2725 0 : ERROR_SQLITE(db->sqlite, lock_pid_sql);
2726 0 : sqlite3_finalize(stmt);
2727 0 : return (EPKG_FATAL);
2728 : }
2729 58 : sqlite3_finalize(stmt);
2730 :
2731 58 : return (EPKG_OK);
2732 : }
2733 :
2734 : static int
2735 63 : pkgdb_remove_lock_pid(struct pkgdb *db, int64_t pid)
2736 : {
2737 63 : const char lock_pid_sql[] = ""
2738 : "DELETE FROM pkg_lock_pid WHERE pid = ?1;";
2739 63 : sqlite3_stmt *stmt = NULL;
2740 : int ret;
2741 :
2742 63 : ret = sqlite3_prepare_v2(db->sqlite, lock_pid_sql, -1, &stmt, NULL);
2743 63 : if (ret != SQLITE_OK) {
2744 0 : ERROR_SQLITE(db->sqlite, lock_pid_sql);
2745 0 : return (EPKG_FATAL);
2746 : }
2747 63 : sqlite3_bind_int64(stmt, 1, pid);
2748 :
2749 63 : if (sqlite3_step(stmt) != SQLITE_DONE) {
2750 0 : ERROR_SQLITE(db->sqlite, lock_pid_sql);
2751 0 : sqlite3_finalize(stmt);
2752 0 : return (EPKG_FATAL);
2753 : }
2754 63 : sqlite3_finalize(stmt);
2755 :
2756 63 : return (EPKG_OK);
2757 : }
2758 :
2759 : static int
2760 16 : pkgdb_check_lock_pid(struct pkgdb *db)
2761 : {
2762 16 : sqlite3_stmt *stmt = NULL;
2763 16 : int ret, found = 0;
2764 : int64_t pid, lpid;
2765 16 : const char query[] = "SELECT pid FROM pkg_lock_pid;";
2766 :
2767 16 : ret = sqlite3_prepare_v2(db->sqlite, query, -1,
2768 : &stmt, NULL);
2769 16 : if (ret != SQLITE_OK) {
2770 0 : ERROR_SQLITE(db->sqlite, query);
2771 0 : return (EPKG_FATAL);
2772 : }
2773 :
2774 16 : lpid = getpid();
2775 :
2776 48 : while (sqlite3_step(stmt) != SQLITE_DONE) {
2777 16 : pid = sqlite3_column_int64(stmt, 0);
2778 16 : if (pid != lpid) {
2779 16 : if (kill((pid_t)pid, 0) == -1) {
2780 16 : pkg_debug(1, "found stale pid %lld in lock database, my pid is: %lld",
2781 : (long long)pid, (long long)lpid);
2782 16 : if (pkgdb_remove_lock_pid(db, pid) != EPKG_OK){
2783 0 : sqlite3_finalize(stmt);
2784 0 : return (EPKG_FATAL);
2785 : }
2786 : }
2787 : else {
2788 0 : pkg_emit_notice("process with pid %lld still holds the lock",
2789 : (long long int)pid);
2790 0 : found ++;
2791 : }
2792 : }
2793 : }
2794 :
2795 16 : if (found == 0)
2796 16 : return (EPKG_END);
2797 :
2798 0 : return (EPKG_OK);
2799 : }
2800 :
2801 : static int
2802 16 : pkgdb_reset_lock(struct pkgdb *db)
2803 : {
2804 16 : const char init_sql[] = ""
2805 : "UPDATE pkg_lock SET exclusive=0, advisory=0, read=0;";
2806 : int ret;
2807 :
2808 16 : ret = sqlite3_exec(db->sqlite, init_sql, NULL, NULL, NULL);
2809 :
2810 16 : if (ret == SQLITE_OK)
2811 16 : return (EPKG_OK);
2812 :
2813 0 : return (EPKG_FATAL);
2814 : }
2815 :
2816 : static int
2817 66 : pkgdb_try_lock(struct pkgdb *db, const char *lock_sql, pkgdb_lock_t type,
2818 : bool upgrade)
2819 : {
2820 66 : unsigned int tries = 0;
2821 : struct timespec ts;
2822 66 : int ret = EPKG_END;
2823 : const pkg_object *timeout, *max_tries;
2824 66 : int64_t num_timeout = 1, num_maxtries = 1;
2825 :
2826 66 : timeout = pkg_config_get("LOCK_WAIT");
2827 66 : max_tries = pkg_config_get("LOCK_RETRIES");
2828 :
2829 66 : if (timeout)
2830 66 : num_timeout = pkg_object_int(timeout);
2831 66 : if (max_tries)
2832 66 : num_maxtries = pkg_object_int(max_tries);
2833 :
2834 148 : while (tries <= num_maxtries) {
2835 82 : ret = sqlite3_exec(db->sqlite, lock_sql, NULL, NULL, NULL);
2836 82 : if (ret != SQLITE_OK) {
2837 0 : if (ret == SQLITE_READONLY && type == PKGDB_LOCK_READONLY) {
2838 0 : pkg_debug(1, "want read lock but cannot write to database, "
2839 : "slightly ignore this error for now");
2840 0 : return (EPKG_OK);
2841 : }
2842 0 : return (EPKG_FATAL);
2843 : }
2844 :
2845 82 : ret = EPKG_END;
2846 82 : if (sqlite3_changes(db->sqlite) == 0) {
2847 16 : if (pkgdb_check_lock_pid(db) == EPKG_END) {
2848 : /* No live processes found, so we can safely reset lock */
2849 16 : pkg_debug(1, "no concurrent processes found, cleanup the lock");
2850 16 : pkgdb_reset_lock(db);
2851 16 : if (upgrade) {
2852 : /*
2853 : * In case of upgrade we should obtain a lock from the beginning,
2854 : * hence switch upgrade to retain
2855 : */
2856 0 : pkgdb_remove_lock_pid(db, (int64_t)getpid());
2857 0 : return pkgdb_obtain_lock(db, type);
2858 : }
2859 16 : continue;
2860 : }
2861 0 : else if (num_timeout > 0) {
2862 0 : ts.tv_sec = (int)num_timeout;
2863 0 : ts.tv_nsec = (num_timeout - (int)num_timeout) * 1000000000.;
2864 0 : pkg_debug(1, "waiting for database lock for %d times, "
2865 : "next try in %.2f seconds", tries, num_timeout);
2866 0 : (void)nanosleep(&ts, NULL);
2867 : }
2868 : else {
2869 0 : break;
2870 : }
2871 : }
2872 66 : else if (!upgrade) {
2873 58 : ret = pkgdb_write_lock_pid(db);
2874 58 : break;
2875 : }
2876 : else {
2877 8 : ret = EPKG_OK;
2878 8 : break;
2879 : }
2880 0 : tries ++;
2881 : }
2882 :
2883 66 : return (ret);
2884 : }
2885 :
2886 : int
2887 79 : pkgdb_obtain_lock(struct pkgdb *db, pkgdb_lock_t type)
2888 : {
2889 : int ret;
2890 :
2891 79 : const char readonly_lock_sql[] = ""
2892 : "UPDATE pkg_lock SET read=read+1 WHERE exclusive=0;";
2893 79 : const char advisory_lock_sql[] = ""
2894 : "UPDATE pkg_lock SET advisory=1 WHERE exclusive=0 AND advisory=0;";
2895 79 : const char exclusive_lock_sql[] = ""
2896 : "UPDATE pkg_lock SET exclusive=1 WHERE exclusive=0 AND advisory=0 AND read=0;";
2897 79 : const char *lock_sql = NULL;
2898 :
2899 79 : assert(db != NULL);
2900 :
2901 79 : switch (type) {
2902 : case PKGDB_LOCK_READONLY:
2903 21 : if (!ucl_object_toboolean(pkg_config_get("READ_LOCK")))
2904 21 : return (EPKG_OK);
2905 0 : lock_sql = readonly_lock_sql;
2906 0 : pkg_debug(1, "want to get a read only lock on a database");
2907 0 : break;
2908 : case PKGDB_LOCK_ADVISORY:
2909 8 : lock_sql = advisory_lock_sql;
2910 8 : pkg_debug(1, "want to get an advisory lock on a database");
2911 8 : break;
2912 : case PKGDB_LOCK_EXCLUSIVE:
2913 50 : pkg_debug(1, "want to get an exclusive lock on a database");
2914 50 : lock_sql = exclusive_lock_sql;
2915 50 : break;
2916 : }
2917 :
2918 58 : ret = pkgdb_try_lock(db, lock_sql, type, false);
2919 :
2920 58 : return (ret);
2921 : }
2922 :
2923 : int
2924 8 : pkgdb_upgrade_lock(struct pkgdb *db, pkgdb_lock_t old_type, pkgdb_lock_t new_type)
2925 : {
2926 8 : const char advisory_exclusive_lock_sql[] = ""
2927 : "UPDATE pkg_lock SET exclusive=1,advisory=1 WHERE exclusive=0 AND advisory=1 AND read=0;";
2928 8 : int ret = EPKG_FATAL;
2929 :
2930 8 : assert(db != NULL);
2931 :
2932 8 : if (old_type == PKGDB_LOCK_ADVISORY && new_type == PKGDB_LOCK_EXCLUSIVE) {
2933 8 : pkg_debug(1, "want to upgrade advisory to exclusive lock");
2934 8 : ret = pkgdb_try_lock(db, advisory_exclusive_lock_sql,
2935 : new_type, true);
2936 : }
2937 :
2938 8 : return (ret);
2939 : }
2940 :
2941 : int
2942 0 : pkgdb_downgrade_lock(struct pkgdb *db, pkgdb_lock_t old_type,
2943 : pkgdb_lock_t new_type)
2944 : {
2945 0 : const char downgrade_exclusive_lock_sql[] = ""
2946 : "UPDATE pkg_lock SET exclusive=0,advisory=1 WHERE exclusive=1 "
2947 : "AND advisory=1 AND read=0;";
2948 0 : int ret = EPKG_FATAL;
2949 :
2950 0 : assert(db != NULL);
2951 :
2952 0 : if (old_type == PKGDB_LOCK_EXCLUSIVE &&
2953 : new_type == PKGDB_LOCK_ADVISORY) {
2954 0 : pkg_debug(1, "want to downgrade exclusive to advisory lock");
2955 0 : ret = pkgdb_try_lock(db, downgrade_exclusive_lock_sql,
2956 : new_type, true);
2957 : }
2958 :
2959 0 : return (ret);
2960 : }
2961 :
2962 : int
2963 68 : pkgdb_release_lock(struct pkgdb *db, pkgdb_lock_t type)
2964 : {
2965 68 : const char readonly_unlock_sql[] = ""
2966 : "UPDATE pkg_lock SET read=read-1 WHERE read>0;";
2967 68 : const char advisory_unlock_sql[] = ""
2968 : "UPDATE pkg_lock SET advisory=0 WHERE advisory=1;";
2969 68 : const char exclusive_unlock_sql[] = ""
2970 : "UPDATE pkg_lock SET exclusive=0 WHERE exclusive=1;";
2971 68 : const char *unlock_sql = NULL;
2972 68 : int ret = EPKG_FATAL;
2973 :
2974 68 : if (db == NULL)
2975 0 : return (EPKG_OK);
2976 :
2977 68 : switch (type) {
2978 : case PKGDB_LOCK_READONLY:
2979 21 : if (!ucl_object_toboolean(pkg_config_get("READ_LOCK")))
2980 21 : return (EPKG_OK);
2981 :
2982 0 : unlock_sql = readonly_unlock_sql;
2983 0 : pkg_debug(1, "release a read only lock on a database");
2984 :
2985 0 : break;
2986 : case PKGDB_LOCK_ADVISORY:
2987 8 : unlock_sql = advisory_unlock_sql;
2988 8 : pkg_debug(1, "release an advisory lock on a database");
2989 8 : break;
2990 : case PKGDB_LOCK_EXCLUSIVE:
2991 39 : pkg_debug(1, "release an exclusive lock on a database");
2992 39 : unlock_sql = exclusive_unlock_sql;
2993 39 : break;
2994 : }
2995 :
2996 47 : ret = sqlite3_exec(db->sqlite, unlock_sql, NULL, NULL, NULL);
2997 47 : if (ret != SQLITE_OK)
2998 0 : return (EPKG_FATAL);
2999 :
3000 47 : if (sqlite3_changes(db->sqlite) == 0)
3001 0 : return (EPKG_END);
3002 :
3003 47 : return pkgdb_remove_lock_pid(db, (int64_t)getpid());
3004 : }
3005 :
3006 : int64_t
3007 0 : pkgdb_stats(struct pkgdb *db, pkg_stats_t type)
3008 : {
3009 0 : sqlite3_stmt *stmt = NULL;
3010 0 : int64_t stats = 0;
3011 0 : struct sbuf *sql = NULL;
3012 : int ret;
3013 : struct _pkg_repo_list_item *rit;
3014 :
3015 0 : assert(db != NULL);
3016 :
3017 0 : sql = sbuf_new_auto();
3018 :
3019 0 : switch(type) {
3020 : case PKG_STATS_LOCAL_COUNT:
3021 0 : sbuf_printf(sql, "SELECT COUNT(id) FROM main.packages;");
3022 0 : break;
3023 : case PKG_STATS_LOCAL_SIZE:
3024 0 : sbuf_printf(sql, "SELECT SUM(flatsize) FROM main.packages;");
3025 0 : break;
3026 : case PKG_STATS_REMOTE_UNIQUE:
3027 : case PKG_STATS_REMOTE_COUNT:
3028 : case PKG_STATS_REMOTE_SIZE:
3029 0 : LL_FOREACH(db->repos, rit) {
3030 0 : struct pkg_repo *repo = rit->repo;
3031 :
3032 0 : if (repo->ops->stat != NULL)
3033 0 : stats += repo->ops->stat(repo, type);
3034 : }
3035 0 : goto remote;
3036 : break;
3037 : case PKG_STATS_REMOTE_REPOS:
3038 0 : LL_FOREACH(db->repos, rit) {
3039 0 : stats ++;
3040 : }
3041 0 : goto remote;
3042 : break;
3043 : }
3044 :
3045 0 : sbuf_finish(sql);
3046 0 : pkg_debug(4, "Pkgdb: running '%s'", sbuf_data(sql));
3047 0 : ret = sqlite3_prepare_v2(db->sqlite, sbuf_data(sql), -1, &stmt, NULL);
3048 0 : if (ret != SQLITE_OK) {
3049 0 : ERROR_SQLITE(db->sqlite, sbuf_data(sql));
3050 0 : sbuf_free(sql);
3051 0 : return (-1);
3052 : }
3053 :
3054 0 : while (sqlite3_step(stmt) != SQLITE_DONE) {
3055 0 : stats = sqlite3_column_int64(stmt, 0);
3056 : }
3057 :
3058 0 : sqlite3_finalize(stmt);
3059 :
3060 : remote:
3061 0 : sbuf_free(sql);
3062 :
3063 0 : return (stats);
3064 : }
3065 :
3066 :
3067 : int
3068 15 : pkgdb_begin_solver(struct pkgdb *db)
3069 : {
3070 15 : const char solver_sql[] = ""
3071 : "PRAGMA synchronous = OFF;"
3072 : "PRAGMA journal_mode = MEMORY;"
3073 : "BEGIN TRANSACTION;";
3074 15 : const char update_digests_sql[] = ""
3075 : "DROP INDEX IF EXISTS pkg_digest_id;"
3076 : "BEGIN TRANSACTION;";
3077 15 : const char end_update_sql[] = ""
3078 : "END TRANSACTION;"
3079 : "CREATE INDEX pkg_digest_id ON packages(origin, manifestdigest);";
3080 : struct pkgdb_it *it;
3081 15 : struct pkg *p = NULL;
3082 : kvec_t(struct pkg *) pkglist;
3083 15 : int rc = EPKG_OK;
3084 15 : int64_t cnt = 0, cur = 0;
3085 :
3086 15 : it = pkgdb_query(db, " WHERE manifestdigest IS NULL OR manifestdigest==''",
3087 : MATCH_CONDITION);
3088 15 : if (it != NULL) {
3089 15 : kv_init(pkglist);
3090 37 : while (pkgdb_it_next(it, &p, PKG_LOAD_BASIC|PKG_LOAD_OPTIONS) == EPKG_OK) {
3091 7 : pkg_checksum_calculate(p, NULL);
3092 7 : kv_prepend(typeof(p), pkglist, p);
3093 7 : p = NULL;
3094 7 : cnt ++;
3095 : }
3096 15 : pkgdb_it_free(it);
3097 :
3098 15 : if (kv_size(pkglist) > 0) {
3099 5 : rc = sql_exec(db->sqlite, update_digests_sql);
3100 5 : if (rc != EPKG_OK) {
3101 0 : ERROR_SQLITE(db->sqlite, update_digests_sql);
3102 : }
3103 : else {
3104 5 : pkg_emit_progress_start("Updating database digests format");
3105 12 : for (int i = 0; i < kv_size(pkglist); i++) {
3106 7 : p = kv_A(pkglist, i);
3107 7 : pkg_emit_progress_tick(cur++, cnt);
3108 7 : rc = run_prstmt(UPDATE_DIGEST, p->digest, p->id);
3109 7 : if (rc != SQLITE_DONE) {
3110 0 : assert(0);
3111 : ERROR_SQLITE(db->sqlite, SQL(UPDATE_DIGEST));
3112 : }
3113 : }
3114 :
3115 5 : pkg_emit_progress_tick(cnt, cnt);
3116 5 : if (rc == SQLITE_DONE)
3117 5 : rc = sql_exec(db->sqlite, end_update_sql);
3118 :
3119 5 : if (rc != SQLITE_OK)
3120 0 : ERROR_SQLITE(db->sqlite, end_update_sql);
3121 : }
3122 : }
3123 :
3124 15 : if (rc == EPKG_OK)
3125 15 : rc = sql_exec(db->sqlite, solver_sql);
3126 :
3127 37 : while (kv_size(pkglist) > 0 && (p = kv_pop(pkglist)))
3128 7 : pkg_free(p);
3129 15 : kv_destroy(pkglist);
3130 : } else {
3131 0 : rc = sql_exec(db->sqlite, solver_sql);
3132 : }
3133 :
3134 15 : return (rc);
3135 : }
3136 :
3137 : int
3138 15 : pkgdb_end_solver(struct pkgdb *db)
3139 : {
3140 15 : const char solver_sql[] = ""
3141 : "END TRANSACTION;"
3142 : "PRAGMA synchronous = NORMAL;"
3143 : "PRAGMA journal_mode = DELETE;";
3144 :
3145 15 : return (sql_exec(db->sqlite, solver_sql));
3146 : }
3147 :
3148 : int
3149 7 : pkgdb_is_dir_used(struct pkgdb *db, struct pkg *p, const char *dir, int64_t *res)
3150 : {
3151 : sqlite3_stmt *stmt;
3152 : int ret;
3153 :
3154 7 : const char sql[] = ""
3155 : "SELECT count(package_id) FROM pkg_directories, directories "
3156 : "WHERE directory_id = directories.id AND directories.path = ?1 "
3157 : "AND package_id != ?2;";
3158 :
3159 7 : if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
3160 0 : ERROR_SQLITE(db->sqlite, sql);
3161 0 : return (EPKG_FATAL);
3162 : }
3163 :
3164 7 : sqlite3_bind_text(stmt, 1, dir, -1, SQLITE_TRANSIENT);
3165 7 : sqlite3_bind_int64(stmt, 2, p->id);
3166 :
3167 7 : ret = sqlite3_step(stmt);
3168 :
3169 7 : if (ret == SQLITE_ROW)
3170 7 : *res = sqlite3_column_int64(stmt, 0);
3171 :
3172 7 : sqlite3_finalize(stmt);
3173 :
3174 7 : if (ret != SQLITE_ROW) {
3175 0 : ERROR_SQLITE(db->sqlite, sql);
3176 0 : return (EPKG_FATAL);
3177 : }
3178 :
3179 7 : return (EPKG_OK);
3180 : }
3181 :
3182 : int
3183 0 : pkgdb_repo_count(struct pkgdb *db)
3184 : {
3185 : struct _pkg_repo_list_item *cur;
3186 0 : int result = 0;
3187 :
3188 0 : if (db != NULL) {
3189 0 : LL_FOREACH(db->repos, cur)
3190 0 : result ++;
3191 : }
3192 :
3193 0 : return (result);
3194 : }
|