From 86f593d5135b00a9dbf7dc6d5efc8b341002aa08 Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Fri, 16 Apr 2021 14:06:00 +0300
Subject: [PATCH] Ensure database creation on initial installation

Disabling implicit database creation on read-only handles in commit
afbc2b07839c9ffe9f274f3a4bc2395c76d65472 broke number of handy
use-cases such as install to an empty chroot directory, both with
rpm itself and dnf/yum at least, probably others too.

This minimally resurrects the desired part of the behavior: if people are
asking us to install something, creating a missing database is probably
okay to create without requiring an explicit --initdb action first.
It'll still spit some ugly errors from trying to load the keyring but
at least it'll work. The harmless errors we can try to deal with
separately later on.
---
 lib/depends.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lib/depends.c b/lib/depends.c
index 28a4a784d..ed5994290 100644
--- a/lib/depends.c
+++ b/lib/depends.c
@@ -4,6 +4,8 @@
 
 #include "system.h"
 
+#include <fcntl.h>
+
 #include <rpm/rpmlib.h>		/* rpmVersionCompare, rpmlib provides */
 #include <rpm/rpmtag.h>
 #include <rpm/rpmlog.h>
@@ -414,6 +416,10 @@ static int addPackage(rpmts ts, Header h,
     if (isSource)
 	op = RPMTE_INSTALL;
 
+    /* Ensure database creation on initial installs */
+    if (!isSource && rpmtsGetDBMode(ts) == O_RDONLY)
+	rpmtsSetDBMode(ts, (O_RDWR|O_CREAT));
+
     /* Do lazy (readonly?) open of rpm database for upgrades. */
     if (op != RPMTE_INSTALL && rpmtsGetRdb(ts) == NULL && rpmtsGetDBMode(ts) != -1) {
 	if ((ec = rpmtsOpenDB(ts, rpmtsGetDBMode(ts))) != 0)
-- 
2.27.0

