richardphibel / rpms / dnf

Forked from rpms/dnf 2 years ago
Clone

Blame SOURCES/0004-Use-rpm.TransactionSet.dbCookie-to-determining-if-rp.patch

2a0a3f
From 087ad3d12ba307355dd66aba54faea97d227a3dd Mon Sep 17 00:00:00 2001
2a0a3f
From: zhanghaolian <65838930+iWhy98@users.noreply.github.com>
2a0a3f
Date: Tue, 25 Jan 2022 15:41:16 +0800
2a0a3f
Subject: [PATCH 1/2] dnf:fix dnf mark error when history sqlite missing
2a0a3f
2a0a3f
---
2a0a3f
 dnf/cli/commands/mark.py | 2 +-
2a0a3f
 1 file changed, 1 insertion(+), 1 deletion(-)
2a0a3f
2a0a3f
diff --git a/dnf/cli/commands/mark.py b/dnf/cli/commands/mark.py
2a0a3f
index ec16b738d..cb1f91c13 100644
2a0a3f
--- a/dnf/cli/commands/mark.py
2a0a3f
+++ b/dnf/cli/commands/mark.py
2a0a3f
@@ -89,7 +89,7 @@ class MarkCommand(commands.Command):
2a0a3f
 
2a0a3f
         old = self.base.history.last()
2a0a3f
         if old is None:
2a0a3f
-            rpmdb_version = self.sack._rpmdb_version()
2a0a3f
+            rpmdb_version = self.base.sack._rpmdb_version()
2a0a3f
         else:
2a0a3f
             rpmdb_version = old.end_rpmdb_version
2a0a3f
 
2a0a3f
-- 
2a0a3f
2.34.1
2a0a3f
2a0a3f
2a0a3f
From bee5b97ad159af019deda4de0d80d0011dba4f7a Mon Sep 17 00:00:00 2001
2a0a3f
From: Jaroslav Rohel <jrohel@redhat.com>
2a0a3f
Date: Fri, 28 Jan 2022 16:53:50 +0100
2a0a3f
Subject: [PATCH 2/2] Use rpm.TransactionSet.dbCookie() to determining if rpmdb
2a0a3f
 has changed
2a0a3f
2a0a3f
DNF was using private method `hawkey.Sack._rpmdb_version()` from libdnf.
2a0a3f
The method computes SHA1 hash from sorted list of hashes stored in
2a0a3f
the headers of the instaled packages. And it adds prefix of the number
2a0a3f
of installed packages to the computed hash. The result was stored
2a0a3f
to the history database and used to detect changes in the rpm database.
2a0a3f
2a0a3f
The patch uses new oficial librpm API function
2a0a3f
`rpm.TransactionSet.dbCookie()`. This is a cleaner solution.
2a0a3f
It is also a step to remove the `._rpmdb_version()` method from libdnf.
2a0a3f
It is an attempt to remove SHA1 calculations from libdnf.
2a0a3f
Troubleshooting FIPS compatibility.
2a0a3f
2a0a3f
= changelog =
2a0a3f
msg: Use rpm.TransactionSet.dbCookie() to determining if rpmdb has changed
2a0a3f
type: bugfix
2a0a3f
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2043476
2a0a3f
---
2a0a3f
 dnf/base.py              |  6 +++---
2a0a3f
 dnf/cli/commands/mark.py |  2 +-
2a0a3f
 dnf/cli/output.py        |  2 +-
2a0a3f
 dnf/rpm/transaction.py   | 16 ++++++++++++++++
2a0a3f
 tests/test_sack.py       |  6 ------
2a0a3f
 5 files changed, 21 insertions(+), 11 deletions(-)
2a0a3f
2a0a3f
diff --git a/dnf/base.py b/dnf/base.py
2a0a3f
index b0a536f7f..574e80f66 100644
2a0a3f
--- a/dnf/base.py
2a0a3f
+++ b/dnf/base.py
2a0a3f
@@ -907,7 +907,7 @@ class Base(object):
2a0a3f
                     cmdline = ' '.join(self.cmds)
2a0a3f
                 old = self.history.last()
2a0a3f
                 if old is None:
2a0a3f
-                    rpmdb_version = self.sack._rpmdb_version()
2a0a3f
+                    rpmdb_version = self._ts.dbCookie()
2a0a3f
                 else:
2a0a3f
                     rpmdb_version = old.end_rpmdb_version
2a0a3f
 
2a0a3f
@@ -1046,7 +1046,7 @@ class Base(object):
2a0a3f
             using_pkgs_pats = list(self.conf.history_record_packages)
2a0a3f
             installed_query = self.sack.query().installed()
2a0a3f
             using_pkgs = installed_query.filter(name=using_pkgs_pats).run()
2a0a3f
-            rpmdbv = self.sack._rpmdb_version()
2a0a3f
+            rpmdbv = self._ts.dbCookie()
2a0a3f
             lastdbv = self.history.last()
2a0a3f
             if lastdbv is not None:
2a0a3f
                 lastdbv = lastdbv.end_rpmdb_version
2a0a3f
@@ -1163,7 +1163,7 @@ class Base(object):
2a0a3f
         for tsi in transaction_items:
2a0a3f
             count = display_banner(tsi.pkg, count)
2a0a3f
 
2a0a3f
-        rpmdbv = rpmdb_sack._rpmdb_version()
2a0a3f
+        rpmdbv = self._ts.dbCookie()
2a0a3f
         self.history.end(rpmdbv)
2a0a3f
 
2a0a3f
         timer()
2a0a3f
diff --git a/dnf/cli/commands/mark.py b/dnf/cli/commands/mark.py
2a0a3f
index cb1f91c13..36bf9d436 100644
2a0a3f
--- a/dnf/cli/commands/mark.py
2a0a3f
+++ b/dnf/cli/commands/mark.py
2a0a3f
@@ -89,7 +89,7 @@ class MarkCommand(commands.Command):
2a0a3f
 
2a0a3f
         old = self.base.history.last()
2a0a3f
         if old is None:
2a0a3f
-            rpmdb_version = self.base.sack._rpmdb_version()
2a0a3f
+            rpmdb_version = self.base._ts.dbCookie()
2a0a3f
         else:
2a0a3f
             rpmdb_version = old.end_rpmdb_version
2a0a3f
 
2a0a3f
diff --git a/dnf/cli/output.py b/dnf/cli/output.py
2a0a3f
index a4e9f6c8e..ecf05c2b0 100644
2a0a3f
--- a/dnf/cli/output.py
2a0a3f
+++ b/dnf/cli/output.py
2a0a3f
@@ -1607,7 +1607,7 @@ Transaction Summary
2a0a3f
             if lastdbv is not None and trans.tid == lasttid:
2a0a3f
                 #  If this is the last transaction, is good and it doesn't
2a0a3f
                 # match the current rpmdb ... then mark it as bad.
2a0a3f
-                rpmdbv = self.sack._rpmdb_version()
2a0a3f
+                rpmdbv = self.base._ts.dbCookie()
2a0a3f
                 trans.compare_rpmdbv(str(rpmdbv))
2a0a3f
             lastdbv = None
2a0a3f
 
2a0a3f
diff --git a/dnf/rpm/transaction.py b/dnf/rpm/transaction.py
2a0a3f
index bcc2a7024..a11f36e7e 100644
2a0a3f
--- a/dnf/rpm/transaction.py
2a0a3f
+++ b/dnf/rpm/transaction.py
2a0a3f
@@ -12,8 +12,10 @@
2a0a3f
 from __future__ import absolute_import
2a0a3f
 from __future__ import unicode_literals
2a0a3f
 from dnf.i18n import _
2a0a3f
+import logging
2a0a3f
 import rpm
2a0a3f
 
2a0a3f
+_logger = logging.getLogger('dnf')
2a0a3f
 read_ts = None
2a0a3f
 ts = None
2a0a3f
 
2a0a3f
@@ -61,6 +63,20 @@ class TransactionWrapper(object):
2a0a3f
             mi.pattern(tag, tp, pat)
2a0a3f
         return mi
2a0a3f
 
2a0a3f
+    def dbCookie(self):
2a0a3f
+        # dbCookie() does not support lazy opening of rpm database.
2a0a3f
+        # The following line opens the database if it is not already open.
2a0a3f
+        if self.ts.openDB() != 0:
2a0a3f
+            _logger.error(_('The openDB() function connot open rpm database.'))
2a0a3f
+            return ''
2a0a3f
+
2a0a3f
+        cookie = self.ts.dbCookie()
2a0a3f
+        if not cookie:
2a0a3f
+            _logger.error(_('The dbCookie() function did not return cookie of rpm database.'))
2a0a3f
+            return ''
2a0a3f
+
2a0a3f
+        return cookie
2a0a3f
+
2a0a3f
     def __getattr__(self, attr):
2a0a3f
         if attr in self._methods:
2a0a3f
             return self.getMethod(attr)
2a0a3f
diff --git a/tests/test_sack.py b/tests/test_sack.py
2a0a3f
index 49a715924..2c6fe8e01 100644
2a0a3f
--- a/tests/test_sack.py
2a0a3f
+++ b/tests/test_sack.py
2a0a3f
@@ -32,12 +32,6 @@ class SackTest(tests.support.DnfBaseTestCase):
2a0a3f
 
2a0a3f
     REPOS = []
2a0a3f
 
2a0a3f
-    def test_rpmdb_version(self):
2a0a3f
-        version = self.sack._rpmdb_version()
2a0a3f
-        self.assertIsNotNone(version)
2a0a3f
-        expected = "%s:%s" % (tests.support.TOTAL_RPMDB_COUNT, tests.support.RPMDB_CHECKSUM)
2a0a3f
-        self.assertEqual(version, expected)
2a0a3f
-
2a0a3f
     def test_excludepkgs(self):
2a0a3f
         self.base.conf.excludepkgs = ['pepper']
2a0a3f
         self.base._setup_excludes_includes()
2a0a3f
-- 
2a0a3f
2.34.1
2a0a3f