michal-grzedzicki / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone

Blame SOURCES/rpm-4.14.3-add-read-only-support-for-sqlite.patch

bfc6f7
From 34790c335fe6e5e1099c9320d7b3134398104120 Mon Sep 17 00:00:00 2001
bfc6f7
Message-Id: <34790c335fe6e5e1099c9320d7b3134398104120.1624429665.git.pmatilai@redhat.com>
bfc6f7
From: Panu Matilainen <pmatilai@redhat.com>
bfc6f7
Date: Wed, 23 Jun 2021 08:24:44 +0300
bfc6f7
Subject: [PATCH] Add read-only support for sqlite
bfc6f7
bfc6f7
Based on latest upstream sqlite backend version, chainsaw write support
bfc6f7
out and adjust for the infra differences (which there are more than a
bfc6f7
few) and add an error message instead.
bfc6f7
---
bfc6f7
 configure.ac         |  23 ++
bfc6f7
 lib/Makefile.am      |   6 +
bfc6f7
 lib/backend/dbi.c    |  14 +
bfc6f7
 lib/backend/dbi.h    |   5 +
bfc6f7
 lib/backend/sqlite.c | 659 +++++++++++++++++++++++++++++++++++++++++++
bfc6f7
 macros.in            |   1 +
bfc6f7
 6 files changed, 708 insertions(+)
bfc6f7
 create mode 100644 lib/backend/sqlite.c
bfc6f7
bfc6f7
diff --git a/configure.ac b/configure.ac
bfc6f7
index 3fcb3ff20..e04aced68 100644
bfc6f7
--- a/configure.ac
bfc6f7
+++ b/configure.ac
bfc6f7
@@ -589,6 +589,29 @@ AS_IF([test "$enable_ndb" = yes],[
bfc6f7
 ])
bfc6f7
 AM_CONDITIONAL([NDB], [test "$enable_ndb" = yes])
bfc6f7
 
bfc6f7
+# Check for SQLITE support
bfc6f7
+AC_ARG_ENABLE([sqlite],
bfc6f7
+              [AS_HELP_STRING([--enable-sqlite=@<:@yes/no/auto@:>@)],
bfc6f7
+                              [build with sqlite rpm database format support (default=yes)])],
bfc6f7
+              [enable_sqlite="$enableval"],
bfc6f7
+              [enable_sqlite=yes])
bfc6f7
+
bfc6f7
+AS_IF([test "x$enable_sqlite" != "xno"], [
bfc6f7
+  PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.22.0], [have_sqlite=yes], [have_sqlite=no])
bfc6f7
+  AS_IF([test "$enable_sqlite" = "yes"], [
bfc6f7
+    if test "$have_sqlite" = "no"; then
bfc6f7
+      AC_MSG_ERROR([--enable-sqlite specified, but not available])
bfc6f7
+    fi
bfc6f7
+  ])
bfc6f7
+])
bfc6f7
+
bfc6f7
+if test "x$have_sqlite" = "xyes"; then
bfc6f7
+  AC_DEFINE([WITH_SQLITE], [1], [Define if SQLITE is available])
bfc6f7
+  SQLITE_REQUIRES=sqlite
bfc6f7
+  AC_SUBST(SQLITE_REQUIRES)
bfc6f7
+fi
bfc6f7
+AM_CONDITIONAL([SQLITE], [test "x$have_sqlite" = "xyes"])
bfc6f7
+
bfc6f7
 #=================
bfc6f7
 # Check for LMDB support
bfc6f7
 AC_ARG_ENABLE([lmdb],
bfc6f7
diff --git a/lib/Makefile.am b/lib/Makefile.am
bfc6f7
index baf3238ee..8a9fe77bd 100644
bfc6f7
--- a/lib/Makefile.am
bfc6f7
+++ b/lib/Makefile.am
bfc6f7
@@ -76,6 +76,12 @@ librpm_la_SOURCES += \
bfc6f7
 	backend/ndb/rpmxdb.h
bfc6f7
 endif
bfc6f7
 
bfc6f7
+if SQLITE
bfc6f7
+AM_CPPFLAGS += $(SQLITE_CFLAGS)
bfc6f7
+librpm_la_LIBADD += $(SQLITE_LIBS)
bfc6f7
+librpm_la_SOURCES += backend/sqlite.c
bfc6f7
+endif
bfc6f7
+
bfc6f7
 if LMDB
bfc6f7
 AM_CPPFLAGS += $(LMDB_CFLAGS)
bfc6f7
 librpm_la_LIBADD += $(LMDB_LIBS)
bfc6f7
diff --git a/lib/backend/dbi.c b/lib/backend/dbi.c
bfc6f7
index e99a5f2b2..dc3587f58 100644
bfc6f7
--- a/lib/backend/dbi.c
bfc6f7
+++ b/lib/backend/dbi.c
bfc6f7
@@ -48,6 +48,11 @@ dbDetectBackend(rpmdb rdb)
bfc6f7
     if (!strcmp(db_backend, "ndb")) {
bfc6f7
 	rdb->db_ops = &ndb_dbops;
bfc6f7
     } else
bfc6f7
+#endif
bfc6f7
+#ifdef WITH_SQLITE
bfc6f7
+    if (!strcmp(db_backend, "sqlite")) {
bfc6f7
+	rdb->db_ops = &sqlite_dbops;
bfc6f7
+    } else
bfc6f7
 #endif
bfc6f7
     {
bfc6f7
 	rdb->db_ops = &db3_dbops;
bfc6f7
@@ -75,6 +80,15 @@ dbDetectBackend(rpmdb rdb)
bfc6f7
     free(path);
bfc6f7
 #endif
bfc6f7
 
bfc6f7
+#ifdef WITH_SQLITE
bfc6f7
+    path = rstrscat(NULL, dbhome, "/rpmdb.sqlite", NULL);
bfc6f7
+    if (access(path, F_OK) == 0 && rdb->db_ops != &sqlite_dbops) {
bfc6f7
+	rdb->db_ops = &sqlite_dbops;
bfc6f7
+	rpmlog(RPMLOG_WARNING, _("Found SQLITE rpmdb.sqlite database while attempting %s backend: using sqlite backend.\n"), db_backend);
bfc6f7
+    }
bfc6f7
+    free(path);
bfc6f7
+#endif
bfc6f7
+
bfc6f7
     path = rstrscat(NULL, dbhome, "/Packages", NULL);
bfc6f7
     if (access(path, F_OK) == 0 && rdb->db_ops != &db3_dbops) {
bfc6f7
 	rdb->db_ops = &db3_dbops;
bfc6f7
diff --git a/lib/backend/dbi.h b/lib/backend/dbi.h
bfc6f7
index 02f49c8fd..ff2b4f974 100644
bfc6f7
--- a/lib/backend/dbi.h
bfc6f7
+++ b/lib/backend/dbi.h
bfc6f7
@@ -275,6 +275,11 @@ RPM_GNUC_INTERNAL
bfc6f7
 extern struct rpmdbOps_s lmdb_dbops;
bfc6f7
 #endif
bfc6f7
 
bfc6f7
+#if defined(WITH_SQLITE)
bfc6f7
+RPM_GNUC_INTERNAL
bfc6f7
+extern struct rpmdbOps_s sqlite_dbops;
bfc6f7
+#endif
bfc6f7
+
bfc6f7
 #ifdef __cplusplus
bfc6f7
 }
bfc6f7
 #endif
bfc6f7
diff --git a/lib/backend/sqlite.c b/lib/backend/sqlite.c
bfc6f7
new file mode 100644
bfc6f7
index 000000000..3caeba5f0
bfc6f7
--- /dev/null
bfc6f7
+++ b/lib/backend/sqlite.c
bfc6f7
@@ -0,0 +1,659 @@
bfc6f7
+#include "system.h"
bfc6f7
+
bfc6f7
+#include <sqlite3.h>
bfc6f7
+#include <fcntl.h>
bfc6f7
+
bfc6f7
+#include <rpm/rpmlog.h>
bfc6f7
+#include <rpm/rpmfileutil.h>
bfc6f7
+#include <rpm/rpmmacro.h>
bfc6f7
+#include "lib/rpmdb_internal.h"
bfc6f7
+
bfc6f7
+#include "debug.h"
bfc6f7
+
bfc6f7
+static const int sleep_ms = 50;
bfc6f7
+
bfc6f7
+struct dbiCursor_s {
bfc6f7
+    sqlite3 *sdb;
bfc6f7
+    sqlite3_stmt *stmt;
bfc6f7
+    const char *fmt;
bfc6f7
+    int flags;
bfc6f7
+    rpmTagVal tag;
bfc6f7
+    int ctype;
bfc6f7
+    struct dbiCursor_s *subc;
bfc6f7
+
bfc6f7
+    const void *key;
bfc6f7
+    unsigned int keylen;
bfc6f7
+};
bfc6f7
+
bfc6f7
+static int sqlexec(sqlite3 *sdb, const char *fmt, ...);
bfc6f7
+
bfc6f7
+static void rpm_match3(sqlite3_context *sctx, int argc, sqlite3_value **argv)
bfc6f7
+{
bfc6f7
+    int match = 0;
bfc6f7
+    if (argc == 3) {
bfc6f7
+	int b1len = sqlite3_value_bytes(argv[0]);
bfc6f7
+	int b2len = sqlite3_value_bytes(argv[1]);
bfc6f7
+	int n = sqlite3_value_int(argv[2]);
bfc6f7
+	if (b1len >= n && b2len >= n) {
bfc6f7
+	    const char *b1 = sqlite3_value_blob(argv[0]);
bfc6f7
+	    const char *b2 = sqlite3_value_blob(argv[1]);
bfc6f7
+	    match = (memcmp(b1, b2, n) == 0);
bfc6f7
+	}
bfc6f7
+    }
bfc6f7
+    sqlite3_result_int(sctx, match);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static void errCb(void *data, int err, const char *msg)
bfc6f7
+{
bfc6f7
+    rpmdb rdb = data;
bfc6f7
+    rpmlog(RPMLOG_WARNING, "%s: %s: %s\n",
bfc6f7
+		rdb->db_descr, sqlite3_errstr(err), msg);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int dbiCursorReset(dbiCursor dbc)
bfc6f7
+{
bfc6f7
+    if (dbc->stmt) {
bfc6f7
+	sqlite3_reset(dbc->stmt);
bfc6f7
+	sqlite3_clear_bindings(dbc->stmt);
bfc6f7
+    }
bfc6f7
+    return 0;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int dbiCursorResult(dbiCursor dbc)
bfc6f7
+{
bfc6f7
+    int rc = sqlite3_errcode(dbc->sdb);
bfc6f7
+    int err = (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW);
bfc6f7
+    if (err) {
bfc6f7
+	rpmlog(RPMLOG_ERR, "%s: %d: %s\n", sqlite3_sql(dbc->stmt),
bfc6f7
+		sqlite3_errcode(dbc->sdb), sqlite3_errmsg(dbc->sdb));
bfc6f7
+    }
bfc6f7
+    return err ? RPMRC_FAIL : RPMRC_OK;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int dbiCursorPrep(dbiCursor dbc, const char *fmt, ...)
bfc6f7
+{
bfc6f7
+    if (dbc->stmt == NULL) {
bfc6f7
+	char *cmd = NULL;
bfc6f7
+	va_list ap;
bfc6f7
+
bfc6f7
+	va_start(ap, fmt); 
bfc6f7
+	cmd = sqlite3_vmprintf(fmt, ap);
bfc6f7
+	va_end(ap);
bfc6f7
+
bfc6f7
+	sqlite3_prepare_v2(dbc->sdb, cmd, -1, &dbc->stmt, NULL);
bfc6f7
+	sqlite3_free(cmd);
bfc6f7
+    } else {
bfc6f7
+	dbiCursorReset(dbc);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return dbiCursorResult(dbc);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int dbiCursorBindPkg(dbiCursor dbc, unsigned int hnum,
bfc6f7
+				void *blob, unsigned int bloblen)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+
bfc6f7
+    if (hnum)
bfc6f7
+	rc = sqlite3_bind_int(dbc->stmt, 1, hnum);
bfc6f7
+    else
bfc6f7
+	rc = sqlite3_bind_null(dbc->stmt, 1);
bfc6f7
+
bfc6f7
+    if (blob) {
bfc6f7
+	if (!rc)
bfc6f7
+	    rc = sqlite3_bind_blob(dbc->stmt, 2, blob, bloblen, NULL);
bfc6f7
+    }
bfc6f7
+    return dbiCursorResult(dbc);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int dbiCursorBindIdx(dbiCursor dbc, const void *key, int keylen,
bfc6f7
+				dbiIndexItem rec)
bfc6f7
+{
bfc6f7
+    int rc;
bfc6f7
+    if (dbc->ctype == SQLITE_TEXT) {
bfc6f7
+	rc = sqlite3_bind_text(dbc->stmt, 1, key, keylen, NULL);
bfc6f7
+    } else {
bfc6f7
+	rc = sqlite3_bind_blob(dbc->stmt, 1, key, keylen, NULL);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    if (rec) {
bfc6f7
+	if (!rc)
bfc6f7
+	    rc = sqlite3_bind_int(dbc->stmt, 2, rec->hdrNum);
bfc6f7
+	if (!rc)
bfc6f7
+	    rc = sqlite3_bind_int(dbc->stmt, 3, rec->tagNum);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return dbiCursorResult(dbc);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlite_init(rpmdb rdb, const char * dbhome)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+    char *dbfile = NULL;
bfc6f7
+
bfc6f7
+    if (rdb->db_dbenv == NULL) {
bfc6f7
+	dbfile = rpmGenPath(dbhome, "rpmdb.sqlite", NULL);
bfc6f7
+	sqlite3 *sdb = NULL;
bfc6f7
+	int xx, flags = 0;
bfc6f7
+	int retry_open = 1;
bfc6f7
+
bfc6f7
+	free(rdb->db_descr);
bfc6f7
+	rdb->db_descr = xstrdup("sqlite");
bfc6f7
+
bfc6f7
+	if ((rdb->db_mode & O_ACCMODE) == O_RDONLY)
bfc6f7
+	    flags |= SQLITE_OPEN_READONLY;
bfc6f7
+	else {
bfc6f7
+	    rpmlog(RPMLOG_ERR,
bfc6f7
+		_("unable to open sqlite database %s for writing, sqlite support is read-only\n"), dbfile);
bfc6f7
+	    rc = RPMRC_FAIL;
bfc6f7
+	    goto exit;
bfc6f7
+	}
bfc6f7
+
bfc6f7
+	while (retry_open--) {
bfc6f7
+	    xx = sqlite3_open_v2(dbfile, &sdb, flags, NULL);
bfc6f7
+	    /* Attempt to create if missing, discarding OPEN_READONLY (!) */
bfc6f7
+	    if (xx == SQLITE_CANTOPEN && (flags & SQLITE_OPEN_READONLY)) {
bfc6f7
+		/* Sqlite allocates resources even on failure to open (!) */
bfc6f7
+		sqlite3_close(sdb);
bfc6f7
+		flags &= ~SQLITE_OPEN_READONLY;
bfc6f7
+		flags |= (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
bfc6f7
+		retry_open++;
bfc6f7
+	    }
bfc6f7
+	}
bfc6f7
+
bfc6f7
+	if (xx != SQLITE_OK) {
bfc6f7
+	    rpmlog(RPMLOG_ERR, _("Unable to open sqlite database %s: %s\n"),
bfc6f7
+		    dbfile, sqlite3_errstr(xx));
bfc6f7
+	    rc = 1;
bfc6f7
+	    goto exit;
bfc6f7
+	}
bfc6f7
+
bfc6f7
+	sqlite3_create_function(sdb, "match", 3,
bfc6f7
+				(SQLITE_UTF8|SQLITE_DETERMINISTIC),
bfc6f7
+				NULL, rpm_match3, NULL, NULL);
bfc6f7
+
bfc6f7
+	sqlite3_busy_timeout(sdb, sleep_ms);
bfc6f7
+	sqlite3_config(SQLITE_CONFIG_LOG, errCb, rdb);
bfc6f7
+
bfc6f7
+	sqlexec(sdb, "PRAGMA secure_delete = OFF");
bfc6f7
+	sqlexec(sdb, "PRAGMA case_sensitive_like = ON");
bfc6f7
+
bfc6f7
+	if (sqlite3_db_readonly(sdb, NULL) == 0) {
bfc6f7
+	    if (sqlexec(sdb, "PRAGMA journal_mode = WAL") == 0) {
bfc6f7
+		int one = 1;
bfc6f7
+		/* Annoying but necessary to support non-privileged readers */
bfc6f7
+		sqlite3_file_control(sdb, NULL, SQLITE_FCNTL_PERSIST_WAL, &one);
bfc6f7
+
bfc6f7
+		if (!rpmExpandNumeric("%{?_flush_io}"))
bfc6f7
+		    sqlexec(sdb, "PRAGMA wal_autocheckpoint = 0");
bfc6f7
+	    }
bfc6f7
+	}
bfc6f7
+
bfc6f7
+	rdb->db_dbenv = sdb;
bfc6f7
+    }
bfc6f7
+    rdb->db_opens++;
bfc6f7
+
bfc6f7
+exit:
bfc6f7
+    free(dbfile);
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlite_fini(rpmdb rdb)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+    if (rdb) {
bfc6f7
+	sqlite3 *sdb = rdb->db_dbenv;
bfc6f7
+	if (rdb->db_opens > 1) {
bfc6f7
+	    rdb->db_opens--;
bfc6f7
+	} else {
bfc6f7
+	    if (sqlite3_db_readonly(sdb, NULL) == 0) {
bfc6f7
+		sqlexec(sdb, "PRAGMA optimize");
bfc6f7
+		sqlexec(sdb, "PRAGMA wal_checkpoint = TRUNCATE");
bfc6f7
+	    }
bfc6f7
+	    rdb->db_dbenv = NULL;
bfc6f7
+	    int xx = sqlite3_close(sdb);
bfc6f7
+	    rc = (xx != SQLITE_OK);
bfc6f7
+	}
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlexec(sqlite3 *sdb, const char *fmt, ...)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+    char *cmd = NULL;
bfc6f7
+    char *err = NULL;
bfc6f7
+    va_list ap;
bfc6f7
+
bfc6f7
+    va_start(ap, fmt);
bfc6f7
+    cmd = sqlite3_vmprintf(fmt, ap);
bfc6f7
+    va_end(ap);
bfc6f7
+
bfc6f7
+    /* sqlite3_exec() doesn't seeem to honor sqlite3_busy_timeout() */
bfc6f7
+    while ((rc = sqlite3_exec(sdb, cmd, NULL, NULL, &err)) == SQLITE_BUSY) {
bfc6f7
+	usleep(sleep_ms);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    if (rc)
bfc6f7
+	rpmlog(RPMLOG_ERR, "sqlite failure: %s: %s\n", cmd, err);
bfc6f7
+    else
bfc6f7
+	rpmlog(RPMLOG_DEBUG, "%s: %d\n", cmd, rc);
bfc6f7
+
bfc6f7
+    sqlite3_free(cmd);
bfc6f7
+
bfc6f7
+    return rc ? RPMRC_FAIL : RPMRC_OK;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int dbiExists(dbiIndex dbi)
bfc6f7
+{
bfc6f7
+    const char *col = (dbi->dbi_type == DBI_PRIMARY) ? "hnum" : "key";
bfc6f7
+    const char *tbl = dbi->dbi_file;
bfc6f7
+    int rc = sqlite3_table_column_metadata(dbi->dbi_db, NULL, tbl, col,
bfc6f7
+					   NULL, NULL, NULL, NULL, NULL);
bfc6f7
+    return (rc == 0);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int init_table(dbiIndex dbi, rpmTagVal tag)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+
bfc6f7
+    if (dbiExists(dbi))
bfc6f7
+	return 0;
bfc6f7
+
bfc6f7
+    if (dbi->dbi_type == DBI_PRIMARY) {
bfc6f7
+	rc = sqlexec(dbi->dbi_db,
bfc6f7
+			"CREATE TABLE IF NOT EXISTS '%q' ("
bfc6f7
+			    "hnum INTEGER PRIMARY KEY AUTOINCREMENT,"
bfc6f7
+			    "blob BLOB NOT NULL"
bfc6f7
+			")",
bfc6f7
+			dbi->dbi_file);
bfc6f7
+    } else {
bfc6f7
+	const char *keytype = (rpmTagGetClass(tag) == RPM_STRING_CLASS) ?
bfc6f7
+				"TEXT" : "BLOB";
bfc6f7
+	rc = sqlexec(dbi->dbi_db,
bfc6f7
+			"CREATE TABLE IF NOT EXISTS '%q' ("
bfc6f7
+			    "key '%q' NOT NULL, "
bfc6f7
+			    "hnum INTEGER NOT NULL, "
bfc6f7
+			    "idx INTEGER NOT NULL, "
bfc6f7
+			    "FOREIGN KEY (hnum) REFERENCES 'Packages'(hnum)"
bfc6f7
+			")",
bfc6f7
+			dbi->dbi_file, keytype);
bfc6f7
+    }
bfc6f7
+    if (!rc)
bfc6f7
+	dbi->dbi_flags |= DBI_CREATED;
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int create_index(sqlite3 *sdb, const char *table, const char *col)
bfc6f7
+{
bfc6f7
+    return sqlexec(sdb,
bfc6f7
+		"CREATE INDEX IF NOT EXISTS '%s_%s_idx' ON '%q'(%s ASC)",
bfc6f7
+		table, col, table, col);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int init_index(dbiIndex dbi, rpmTagVal tag)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+
bfc6f7
+    /* Can't create on readonly database, but things will still work */
bfc6f7
+    if (sqlite3_db_readonly(dbi->dbi_db, NULL) == 1)
bfc6f7
+	return 0;
bfc6f7
+
bfc6f7
+    if (dbi->dbi_type == DBI_SECONDARY) {
bfc6f7
+	int string = (rpmTagGetClass(tag) == RPM_STRING_CLASS);
bfc6f7
+	int array = (rpmTagGetReturnType(tag) == RPM_ARRAY_RETURN_TYPE);
bfc6f7
+	if (!rc && string)
bfc6f7
+	    rc = create_index(dbi->dbi_db, dbi->dbi_file, "key");
bfc6f7
+	if (!rc && array)
bfc6f7
+	    rc = create_index(dbi->dbi_db, dbi->dbi_file, "hnum");
bfc6f7
+    }
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlite_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
bfc6f7
+{
bfc6f7
+    int rc = sqlite_init(rdb, rpmdbHome(rdb));
bfc6f7
+
bfc6f7
+    if (!rc) {
bfc6f7
+	dbiIndex dbi = dbiNew(rdb, rpmtag);
bfc6f7
+	dbi->dbi_db = rdb->db_dbenv;
bfc6f7
+
bfc6f7
+	rc = init_table(dbi, rpmtag);
bfc6f7
+
bfc6f7
+	if (!rc && !(rdb->db_flags & RPMDB_FLAG_REBUILD))
bfc6f7
+	    rc = init_index(dbi, rpmtag);
bfc6f7
+
bfc6f7
+	if (!rc && dbip)
bfc6f7
+	    *dbip = dbi;
bfc6f7
+	else
bfc6f7
+	    dbiFree(dbi);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlite_Close(dbiIndex dbi, unsigned int flags)
bfc6f7
+{
bfc6f7
+    rpmdb rdb = dbi->dbi_rpmdb;
bfc6f7
+    int rc = 0;
bfc6f7
+    if (rdb->db_flags & RPMDB_FLAG_REBUILD)
bfc6f7
+	rc = init_index(dbi, rpmTagGetValue(dbi->dbi_file));
bfc6f7
+    sqlite_fini(dbi->dbi_rpmdb);
bfc6f7
+    dbiFree(dbi);
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlite_Verify(dbiIndex dbi, unsigned int flags)
bfc6f7
+{
bfc6f7
+    int errors = -1;
bfc6f7
+    int key_errors = -1;
bfc6f7
+    sqlite3_stmt *s = NULL;
bfc6f7
+    const char *cmd = "PRAGMA integrity_check";
bfc6f7
+
bfc6f7
+    if (dbi->dbi_type == DBI_SECONDARY)
bfc6f7
+	return RPMRC_OK;
bfc6f7
+
bfc6f7
+    if (sqlite3_prepare_v2(dbi->dbi_db, cmd, -1, &s, NULL) == SQLITE_OK) {
bfc6f7
+	errors = 0;
bfc6f7
+	while (sqlite3_step(s) == SQLITE_ROW) {
bfc6f7
+	    const char *txt = (const char *)sqlite3_column_text(s, 0);
bfc6f7
+	    if (!rstreq(txt, "ok")) {
bfc6f7
+		errors++;
bfc6f7
+		rpmlog(RPMLOG_ERR, "verify: %s\n", txt);
bfc6f7
+	    }
bfc6f7
+	}
bfc6f7
+	sqlite3_finalize(s);
bfc6f7
+    } else {
bfc6f7
+	rpmlog(RPMLOG_ERR, "%s: %s\n", cmd, sqlite3_errmsg(dbi->dbi_db));
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    /* No point checking higher-level errors if low-level errors exist */
bfc6f7
+    if (errors)
bfc6f7
+	goto exit;
bfc6f7
+
bfc6f7
+    cmd = "PRAGMA foreign_key_check";
bfc6f7
+    if (sqlite3_prepare_v2(dbi->dbi_db, cmd, -1, &s, NULL) == SQLITE_OK) {
bfc6f7
+	key_errors = 0;
bfc6f7
+	while (sqlite3_step(s) == SQLITE_ROW) {
bfc6f7
+	    key_errors++;
bfc6f7
+	    rpmlog(RPMLOG_ERR, "verify key: %s[%lld]\n",
bfc6f7
+				sqlite3_column_text(s, 0),
bfc6f7
+				sqlite3_column_int64(s, 1));
bfc6f7
+	}
bfc6f7
+	sqlite3_finalize(s);
bfc6f7
+    } else {
bfc6f7
+	rpmlog(RPMLOG_ERR, "%s: %s\n", cmd, sqlite3_errmsg(dbi->dbi_db));
bfc6f7
+    }
bfc6f7
+
bfc6f7
+exit:
bfc6f7
+
bfc6f7
+    return (errors == 0 && key_errors == 0) ? RPMRC_OK : RPMRC_FAIL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static void sqlite_SetFSync(rpmdb rdb, int enable)
bfc6f7
+{
bfc6f7
+    if (rdb->db_dbenv) {
bfc6f7
+	sqlexec(rdb->db_dbenv,
bfc6f7
+	    "PRAGMA synchronous = %s", enable ? "FULL" : "OFF");
bfc6f7
+    }
bfc6f7
+}
bfc6f7
+
bfc6f7
+static int sqlite_Ctrl(rpmdb rdb, dbCtrlOp ctrl)
bfc6f7
+{
bfc6f7
+    int rc = 0;
bfc6f7
+
bfc6f7
+    switch (ctrl) {
bfc6f7
+    case DB_CTRL_LOCK_RW:
bfc6f7
+	rc = sqlexec(rdb->db_dbenv, "SAVEPOINT 'rwlock'");
bfc6f7
+	break;
bfc6f7
+    case DB_CTRL_UNLOCK_RW:
bfc6f7
+	rc = sqlexec(rdb->db_dbenv, "RELEASE 'rwlock'");
bfc6f7
+	break;
bfc6f7
+    default:
bfc6f7
+	break;
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static dbiCursor sqlite_CursorInit(dbiIndex dbi, unsigned int flags)
bfc6f7
+{
bfc6f7
+    dbiCursor dbc = xcalloc(1, sizeof(*dbc));
bfc6f7
+    dbc->sdb = dbi->dbi_db;
bfc6f7
+    dbc->flags = flags;
bfc6f7
+    dbc->tag = rpmTagGetValue(dbi->dbi_file);
bfc6f7
+    if (rpmTagGetClass(dbc->tag) == RPM_STRING_CLASS) {
bfc6f7
+	dbc->ctype = SQLITE_TEXT;
bfc6f7
+    } else {
bfc6f7
+	dbc->ctype = SQLITE_BLOB;
bfc6f7
+    }
bfc6f7
+    if (dbc->flags & DBC_WRITE)
bfc6f7
+	sqlexec(dbc->sdb, "SAVEPOINT '%s'", dbi->dbi_file);
bfc6f7
+    return dbc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static dbiCursor sqlite_CursorFree(dbiIndex dbi, dbiCursor dbc)
bfc6f7
+{
bfc6f7
+    if (dbc) {
bfc6f7
+	sqlite3_finalize(dbc->stmt);
bfc6f7
+	if (dbc->subc)
bfc6f7
+	    dbiCursorFree(dbi, dbc->subc);
bfc6f7
+	if (dbc->flags & DBC_WRITE)
bfc6f7
+	    sqlexec(dbc->sdb, "RELEASE '%s'", dbi->dbi_file);
bfc6f7
+	free(dbc);
bfc6f7
+    }
bfc6f7
+    return NULL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_pkgdbNew(dbiIndex dbi, dbiCursor dbc, unsigned int *hdrNum)
bfc6f7
+{
bfc6f7
+    return RPMRC_FAIL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_pkgdbPut(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum, unsigned char *hdrBlob, unsigned int hdrLen)
bfc6f7
+{
bfc6f7
+    return RPMRC_FAIL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_pkgdbDel(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum)
bfc6f7
+{
bfc6f7
+    return RPMRC_FAIL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_stepPkg(dbiCursor dbc, unsigned char **hdrBlob, unsigned int *hdrLen)
bfc6f7
+{
bfc6f7
+    int rc = sqlite3_step(dbc->stmt);
bfc6f7
+
bfc6f7
+    if (rc == SQLITE_ROW) {
bfc6f7
+	if (hdrLen)
bfc6f7
+	    *hdrLen = sqlite3_column_bytes(dbc->stmt, 1);
bfc6f7
+	if (hdrBlob)
bfc6f7
+	    *hdrBlob = (void *) sqlite3_column_blob(dbc->stmt, 1);
bfc6f7
+    }
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_pkgdbByKey(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum, unsigned char **hdrBlob, unsigned int *hdrLen)
bfc6f7
+{
bfc6f7
+    int rc = dbiCursorPrep(dbc, "SELECT hnum, blob FROM '%q' WHERE hnum=?",
bfc6f7
+				dbi->dbi_file);
bfc6f7
+
bfc6f7
+    if (!rc)
bfc6f7
+	rc = dbiCursorBindPkg(dbc, hdrNum, NULL, 0);
bfc6f7
+
bfc6f7
+    if (!rc)
bfc6f7
+	rc = sqlite_stepPkg(dbc, hdrBlob, hdrLen);
bfc6f7
+
bfc6f7
+    return dbiCursorResult(dbc);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_pkgdbIter(dbiIndex dbi, dbiCursor dbc,
bfc6f7
+				unsigned char **hdrBlob, unsigned int *hdrLen)
bfc6f7
+{
bfc6f7
+    int rc = RPMRC_OK;
bfc6f7
+    if (dbc->stmt == NULL) {
bfc6f7
+	rc = dbiCursorPrep(dbc, "SELECT hnum, blob FROM '%q'", dbi->dbi_file);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    if (!rc)
bfc6f7
+	rc = sqlite_stepPkg(dbc, hdrBlob, hdrLen);
bfc6f7
+
bfc6f7
+    if (rc == SQLITE_DONE) {
bfc6f7
+	rc = RPMRC_NOTFOUND;
bfc6f7
+    } else {
bfc6f7
+	rc = dbiCursorResult(dbc);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_pkgdbGet(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum, unsigned char **hdrBlob, unsigned int *hdrLen)
bfc6f7
+{
bfc6f7
+    int rc;
bfc6f7
+
bfc6f7
+    if (hdrNum) {
bfc6f7
+	rc = sqlite_pkgdbByKey(dbi, dbc, hdrNum, hdrBlob, hdrLen);
bfc6f7
+    } else {
bfc6f7
+	rc = sqlite_pkgdbIter(dbi, dbc, hdrBlob, hdrLen);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static unsigned int sqlite_pkgdbKey(dbiIndex dbi, dbiCursor dbc)
bfc6f7
+{
bfc6f7
+    return sqlite3_column_int(dbc->stmt, 0);
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_idxdbByKey(dbiIndex dbi, dbiCursor dbc,
bfc6f7
+			    const char *keyp, size_t keylen, int searchType,
bfc6f7
+			    dbiIndexSet *set)
bfc6f7
+{
bfc6f7
+    int rc = RPMRC_NOTFOUND;
bfc6f7
+
bfc6f7
+    if (searchType == DBC_PREFIX_SEARCH) {
bfc6f7
+	rc = dbiCursorPrep(dbc, "SELECT hnum, idx FROM '%q' "
bfc6f7
+				"WHERE MATCH(key,'%q',%d) "
bfc6f7
+				"ORDER BY key",
bfc6f7
+				dbi->dbi_file, keyp, keylen);
bfc6f7
+    } else {
bfc6f7
+	rc = dbiCursorPrep(dbc, "SELECT hnum, idx FROM '%q' WHERE key=?",
bfc6f7
+			dbi->dbi_file);
bfc6f7
+	if (!rc)
bfc6f7
+	    rc = dbiCursorBindIdx(dbc, keyp, keylen, NULL);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+
bfc6f7
+    if (!rc) {
bfc6f7
+	while ((rc = sqlite3_step(dbc->stmt)) == SQLITE_ROW) {
bfc6f7
+	    unsigned int hnum = sqlite3_column_int(dbc->stmt, 0);
bfc6f7
+	    unsigned int tnum = sqlite3_column_int(dbc->stmt, 1);
bfc6f7
+
bfc6f7
+	    if (*set == NULL)
bfc6f7
+		*set = dbiIndexSetNew(5);
bfc6f7
+	    dbiIndexSetAppendOne(*set, hnum, tnum, 0);
bfc6f7
+	}
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    if (rc == SQLITE_DONE) {
bfc6f7
+	rc = (*set) ? RPMRC_OK : RPMRC_NOTFOUND;
bfc6f7
+    } else {
bfc6f7
+	rc = dbiCursorResult(dbc);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_idxdbIter(dbiIndex dbi, dbiCursor dbc, dbiIndexSet *set)
bfc6f7
+{
bfc6f7
+    int rc = RPMRC_OK;
bfc6f7
+
bfc6f7
+    if (dbc->stmt == NULL) {
bfc6f7
+	rc = dbiCursorPrep(dbc, "SELECT DISTINCT key FROM '%q' ORDER BY key",
bfc6f7
+				dbi->dbi_file);
bfc6f7
+	if (set)
bfc6f7
+	    dbc->subc = dbiCursorInit(dbi, 0);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    if (!rc)
bfc6f7
+	rc = sqlite3_step(dbc->stmt);
bfc6f7
+
bfc6f7
+    if (rc == SQLITE_ROW) {
bfc6f7
+	if (dbc->ctype == SQLITE_TEXT) {
bfc6f7
+	    dbc->key = sqlite3_column_text(dbc->stmt, 0);
bfc6f7
+	} else {
bfc6f7
+	    dbc->key = sqlite3_column_blob(dbc->stmt, 0);
bfc6f7
+	}
bfc6f7
+	dbc->keylen = sqlite3_column_bytes(dbc->stmt, 0);
bfc6f7
+	if (dbc->subc) {
bfc6f7
+	    rc = sqlite_idxdbByKey(dbi, dbc->subc, dbc->key, dbc->keylen,
bfc6f7
+				    DBC_NORMAL_SEARCH, set);
bfc6f7
+	} else {
bfc6f7
+	    rc = RPMRC_OK;
bfc6f7
+	}
bfc6f7
+    } else if (rc == SQLITE_DONE) {
bfc6f7
+	rc = RPMRC_NOTFOUND;
bfc6f7
+    } else {
bfc6f7
+	rc = dbiCursorResult(dbc);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_idxdbGet(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexSet *set, int searchType)
bfc6f7
+{
bfc6f7
+    int rc;
bfc6f7
+    if (keyp) {
bfc6f7
+	rc = sqlite_idxdbByKey(dbi, dbc, keyp, keylen, searchType, set);
bfc6f7
+    } else {
bfc6f7
+	rc = sqlite_idxdbIter(dbi, dbc, set);
bfc6f7
+    }
bfc6f7
+
bfc6f7
+    return rc;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_idxdbPut(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexItem rec)
bfc6f7
+{
bfc6f7
+    return RPMRC_FAIL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static rpmRC sqlite_idxdbDel(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexItem rec)
bfc6f7
+{
bfc6f7
+    return RPMRC_FAIL;
bfc6f7
+}
bfc6f7
+
bfc6f7
+static const void * sqlite_idxdbKey(dbiIndex dbi, dbiCursor dbc, unsigned int *keylen)
bfc6f7
+{
bfc6f7
+    const void *key = NULL;
bfc6f7
+    if (dbc) {
bfc6f7
+	key = dbc->key;
bfc6f7
+	if (key && keylen)
bfc6f7
+	    *keylen = dbc->keylen;
bfc6f7
+    }
bfc6f7
+    return key;
bfc6f7
+}
bfc6f7
+
bfc6f7
+struct rpmdbOps_s sqlite_dbops = {
bfc6f7
+    .open	= sqlite_Open,
bfc6f7
+    .close	= sqlite_Close,
bfc6f7
+    .verify	= sqlite_Verify,
bfc6f7
+    .setFSync	= sqlite_SetFSync,
bfc6f7
+    .ctrl	= sqlite_Ctrl,
bfc6f7
+
bfc6f7
+    .cursorInit	= sqlite_CursorInit,
bfc6f7
+    .cursorFree	= sqlite_CursorFree,
bfc6f7
+
bfc6f7
+    .pkgdbPut	= sqlite_pkgdbPut,
bfc6f7
+    .pkgdbDel	= sqlite_pkgdbDel,
bfc6f7
+    .pkgdbGet	= sqlite_pkgdbGet,
bfc6f7
+    .pkgdbKey	= sqlite_pkgdbKey,
bfc6f7
+    .pkgdbNew	= sqlite_pkgdbNew,
bfc6f7
+
bfc6f7
+    .idxdbGet	= sqlite_idxdbGet,
bfc6f7
+    .idxdbPut	= sqlite_idxdbPut,
bfc6f7
+    .idxdbDel	= sqlite_idxdbDel,
bfc6f7
+    .idxdbKey	= sqlite_idxdbKey
bfc6f7
+};
bfc6f7
+
bfc6f7
diff --git a/macros.in b/macros.in
bfc6f7
index a6069ee4d..9ad3d60ef 100644
bfc6f7
--- a/macros.in
bfc6f7
+++ b/macros.in
bfc6f7
@@ -620,6 +620,7 @@ package or when debugging this package.\
bfc6f7
 # bdb Berkeley DB
bfc6f7
 # lmdb Lightning Memory-mapped Database
bfc6f7
 # ndb new data base format
bfc6f7
+# sqlite Sqlite database (read-only in this version!)
bfc6f7
 #
bfc6f7
 %_db_backend	      bdb
bfc6f7
 
bfc6f7
-- 
bfc6f7
2.31.1
bfc6f7