alexk / rpms / rpm

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