You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

3260 lines
79 KiB

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