richardphibel / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0029-Use-dnf-solv-userdata-to-check-versions-and-checksum.patch

2c1f71
From 465a6a59279bd7fa2680c626ca0f10c059276668 Mon Sep 17 00:00:00 2001
2c1f71
From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= <amatej@redhat.com>
2c1f71
Date: Wed, 9 Feb 2022 13:18:41 +0100
2c1f71
Subject: [PATCH 29/34] Use dnf solv userdata to check versions and checksum
2c1f71
 (RhBug:2027445)
2c1f71
2c1f71
Remove unused functions for checksums
2c1f71
2c1f71
= changelog =
2c1f71
msg: Write and check versions and checksums for solvfile cache by using new dnf solvfile userdata (RhBug:2027445)
2c1f71
     It is not possible to use old cache files, therefore cache regeneration is triggered automatically.
2c1f71
type: bugfix
2c1f71
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2027445
2c1f71
---
2c1f71
 libdnf/dnf-sack.cpp         | 254 ++++++++++++++++++++++--------------
2c1f71
 libdnf/hy-iutil-private.hpp |   2 -
2c1f71
 libdnf/hy-iutil.cpp         |  20 ---
2c1f71
 3 files changed, 156 insertions(+), 120 deletions(-)
2c1f71
2c1f71
diff --git a/libdnf/dnf-sack.cpp b/libdnf/dnf-sack.cpp
2c1f71
index b9baeaef..61f4807c 100644
2c1f71
--- a/libdnf/dnf-sack.cpp
2c1f71
+++ b/libdnf/dnf-sack.cpp
2c1f71
@@ -225,17 +225,39 @@ dnf_sack_new(void)
2c1f71
     return DNF_SACK(g_object_new(DNF_TYPE_SACK, NULL));
2c1f71
 }
2c1f71
 
2c1f71
-static int
2c1f71
-can_use_repomd_cache(FILE *fp_solv, unsigned char cs_repomd[CHKSUM_BYTES])
2c1f71
-{
2c1f71
-    unsigned char cs_cache[CHKSUM_BYTES];
2c1f71
-
2c1f71
-    if (fp_solv &&
2c1f71
-        !checksum_read(cs_cache, fp_solv) &&
2c1f71
-        !checksum_cmp(cs_cache, cs_repomd))
2c1f71
-        return 1;
2c1f71
+// Try to load cached solv file into repo otherwise return FALSE
2c1f71
+static gboolean
2c1f71
+try_to_use_cached_solvfile(const char *path, Repo *repo, int flags, const unsigned char *checksum, GError **err){
2c1f71
+    FILE *fp_cache = fopen(path, "r");
2c1f71
+    if (!fp_cache) {
2c1f71
+        // Missing cache files (ENOENT) are not an error and can even be expected in some cases
2c1f71
+        // (such as when repo doesn't have updateinfo/prestodelta metadata).
2c1f71
+        // Use g_debug in order not to pollute the log by default with such entries.
2c1f71
+        if (errno == ENOENT) {
2c1f71
+            g_debug("Failed to open solvfile cache: %s: %s", path, strerror(errno));
2c1f71
+        } else {
2c1f71
+            g_warning("Failed to open solvfile cache: %s: %s", path, strerror(errno));
2c1f71
+        }
2c1f71
+        return FALSE;
2c1f71
+    }
2c1f71
+    std::unique_ptr<SolvUserdata> solv_userdata = solv_userdata_read(fp_cache);
2c1f71
+    gboolean ret = TRUE;
2c1f71
+    if (solv_userdata && solv_userdata_verify(solv_userdata.get(), checksum)) {
2c1f71
+        // after reading the header rewind to the begining
2c1f71
+        fseek(fp_cache, 0, SEEK_SET);
2c1f71
+        if (repo_add_solv(repo, fp_cache, flags)) {
2c1f71
+            g_set_error (err,
2c1f71
+                         DNF_ERROR,
2c1f71
+                         DNF_ERROR_INTERNAL_ERROR,
2c1f71
+                         _("repo_add_solv() has failed."));
2c1f71
+            ret = FALSE;
2c1f71
+        }
2c1f71
+    } else {
2c1f71
+        ret = FALSE;
2c1f71
+    }
2c1f71
 
2c1f71
-    return 0;
2c1f71
+    fclose(fp_cache);
2c1f71
+    return ret;
2c1f71
 }
2c1f71
 
2c1f71
 void
2c1f71
@@ -375,33 +397,27 @@ load_ext(DnfSack *sack, HyRepo hrepo, _hy_repo_repodata which_repodata,
2c1f71
     gboolean done = FALSE;
2c1f71
 
2c1f71
     char *fn_cache =  dnf_sack_give_cache_fn(sack, name, suffix);
2c1f71
-    fp = fopen(fn_cache, "r");
2c1f71
     assert(libdnf::repoGetImpl(hrepo)->checksum);
2c1f71
-    if (can_use_repomd_cache(fp, libdnf::repoGetImpl(hrepo)->checksum)) {
2c1f71
-        int flags = 0;
2c1f71
-        /* the updateinfo is not a real extension */
2c1f71
-        if (which_repodata != _HY_REPODATA_UPDATEINFO)
2c1f71
-            flags |= REPO_EXTEND_SOLVABLES;
2c1f71
-        /* do not pollute the main pool with directory component ids */
2c1f71
-        if (which_repodata == _HY_REPODATA_FILENAMES || which_repodata == _HY_REPODATA_OTHER)
2c1f71
-            flags |= REPO_LOCALPOOL;
2c1f71
-        done = TRUE;
2c1f71
+
2c1f71
+    int flags = 0;
2c1f71
+    /* the updateinfo is not a real extension */
2c1f71
+    if (which_repodata != _HY_REPODATA_UPDATEINFO)
2c1f71
+        flags |= REPO_EXTEND_SOLVABLES;
2c1f71
+    /* do not pollute the main pool with directory component ids */
2c1f71
+    if (which_repodata == _HY_REPODATA_FILENAMES || which_repodata == _HY_REPODATA_OTHER)
2c1f71
+        flags |= REPO_LOCALPOOL;
2c1f71
+    if (try_to_use_cached_solvfile(fn_cache, repo, flags, libdnf::repoGetImpl(hrepo)->checksum, error)) {
2c1f71
         g_debug("%s: using cache file: %s", __func__, fn_cache);
2c1f71
-        ret = repo_add_solv(repo, fp, flags);
2c1f71
-        if (ret) {
2c1f71
-            g_set_error_literal (error,
2c1f71
-                                 DNF_ERROR,
2c1f71
-                                 DNF_ERROR_INTERNAL_ERROR,
2c1f71
-                                 _("failed to add solv"));
2c1f71
-            return FALSE;
2c1f71
-        } else {
2c1f71
-            repo_update_state(hrepo, which_repodata, _HY_LOADED_CACHE);
2c1f71
-            repo_set_repodata(hrepo, which_repodata, repo->nrepodata - 1);
2c1f71
-        }
2c1f71
+        done = TRUE;
2c1f71
+        repo_update_state(hrepo, which_repodata, _HY_LOADED_CACHE);
2c1f71
+        repo_set_repodata(hrepo, which_repodata, repo->nrepodata - 1);
2c1f71
     }
2c1f71
+    if (error && *error) {
2c1f71
+        g_prefix_error(error, _("Loading extension cache %s (%d) failed: "), fn_cache, which_repodata);
2c1f71
+        return FALSE;
2c1f71
+    }
2c1f71
+
2c1f71
     g_free(fn_cache);
2c1f71
-    if (fp)
2c1f71
-        fclose(fp);
2c1f71
     if (done)
2c1f71
         return TRUE;
2c1f71
 
2c1f71
@@ -514,35 +530,53 @@ write_main(DnfSack *sack, HyRepo hrepo, int switchtosolv, GError **error)
2c1f71
                         strerror(errno));
2c1f71
             goto done;
2c1f71
         }
2c1f71
-        rc = repo_write(repo, fp);
2c1f71
-        rc |= checksum_write(repoImpl->checksum, fp);
2c1f71
-        rc |= fclose(fp);
2c1f71
+
2c1f71
+        SolvUserdata solv_userdata;
2c1f71
+        if (solv_userdata_fill(&solv_userdata, repoImpl->checksum, error)) {
2c1f71
+            ret = FALSE;
2c1f71
+            fclose(fp);
2c1f71
+            goto done;
2c1f71
+        }
2c1f71
+
2c1f71
+        Repowriter *writer = repowriter_create(repo);
2c1f71
+        repowriter_set_userdata(writer, &solv_userdata, solv_userdata_size);
2c1f71
+        rc = repowriter_write(writer, fp);
2c1f71
+        repowriter_free(writer);
2c1f71
         if (rc) {
2c1f71
+            ret = FALSE;
2c1f71
+            fclose(fp);
2c1f71
+            g_set_error(error,
2c1f71
+                       DNF_ERROR,
2c1f71
+                       DNF_ERROR_INTERNAL_ERROR,
2c1f71
+                       _("While writing primary cache %s repowriter write failed: %i, error: %s"),
2c1f71
+                       tmp_fn_templ, rc, pool_errstr(repo->pool));
2c1f71
+            goto done;
2c1f71
+        }
2c1f71
+
2c1f71
+        if (fclose(fp)) {
2c1f71
             ret = FALSE;
2c1f71
             g_set_error (error,
2c1f71
                         DNF_ERROR,
2c1f71
                         DNF_ERROR_FILE_INVALID,
2c1f71
-                        _("write_main() failed writing data: %i"), rc);
2c1f71
+                        _("Failed closing tmp file %s: %s"),
2c1f71
+                        tmp_fn_templ, strerror(errno));
2c1f71
             goto done;
2c1f71
         }
2c1f71
     }
2c1f71
     if (switchtosolv && repo_is_one_piece(repo)) {
2c1f71
+        repo_empty(repo, 1);
2c1f71
         /* switch over to written solv file activate paging */
2c1f71
-        FILE *fp = fopen(tmp_fn_templ, "r");
2c1f71
-        if (fp) {
2c1f71
-            repo_empty(repo, 1);
2c1f71
-            rc = repo_add_solv(repo, fp, 0);
2c1f71
-            fclose(fp);
2c1f71
-            if (rc) {
2c1f71
-                /* this is pretty fatal */
2c1f71
-                ret = FALSE;
2c1f71
-                g_set_error_literal (error,
2c1f71
-                                     DNF_ERROR,
2c1f71
-                                     DNF_ERROR_FILE_INVALID,
2c1f71
-                                     _("write_main() failed to re-load "
2c1f71
-                                       "written solv file"));
2c1f71
-                goto done;
2c1f71
-            }
2c1f71
+        gboolean loaded = try_to_use_cached_solvfile(tmp_fn_templ, repo, 0, repoImpl->checksum, error);
2c1f71
+        if (error && *error) {
2c1f71
+            g_prefix_error(error, _("Failed to use newly written primary cache: %s: "), tmp_fn_templ);
2c1f71
+            ret = FALSE;
2c1f71
+            goto done;
2c1f71
+        }
2c1f71
+        if (!loaded) {
2c1f71
+            g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
2c1f71
+                        _("Failed to use newly written primary cache: %s"), tmp_fn_templ);
2c1f71
+            ret = FALSE;
2c1f71
+            goto done;
2c1f71
         }
2c1f71
     }
2c1f71
 
2c1f71
@@ -569,20 +603,6 @@ write_ext_updateinfo_filter(Repo *repo, Repokey *key, void *kfdata)
2c1f71
     return repo_write_stdkeyfilter(repo, key, 0);
2c1f71
 }
2c1f71
 
2c1f71
-static int
2c1f71
-write_ext_updateinfo(HyRepo hrepo, Repodata *data, FILE *fp)
2c1f71
-{
2c1f71
-    auto repoImpl = libdnf::repoGetImpl(hrepo);
2c1f71
-    Repo *repo = repoImpl->libsolvRepo;
2c1f71
-    int oldstart = repo->start;
2c1f71
-    repo->start = repoImpl->main_end;
2c1f71
-    repo->nsolvables -= repoImpl->main_nsolvables;
2c1f71
-    int res = repo_write_filtered(repo, fp, write_ext_updateinfo_filter, data, 0);
2c1f71
-    repo->start = oldstart;
2c1f71
-    repo->nsolvables += repoImpl->main_nsolvables;
2c1f71
-    return res;
2c1f71
-}
2c1f71
-
2c1f71
 static gboolean
2c1f71
 write_ext(DnfSack *sack, HyRepo hrepo, _hy_repo_repodata which_repodata,
2c1f71
           const char *suffix, GError **error)
2c1f71
@@ -611,37 +631,78 @@ write_ext(DnfSack *sack, HyRepo hrepo, _hy_repo_repodata which_repodata,
2c1f71
         FILE *fp = fdopen(tmp_fd, "w+");
2c1f71
 
2c1f71
         g_debug("%s: storing %s to: %s", __func__, repo->name, tmp_fn_templ);
2c1f71
-        if (which_repodata != _HY_REPODATA_UPDATEINFO)
2c1f71
-            ret |= repodata_write(data, fp);
2c1f71
-        else
2c1f71
-            ret |= write_ext_updateinfo(hrepo, data, fp);
2c1f71
-        ret |= checksum_write(repoImpl->checksum, fp);
2c1f71
-        ret |= fclose(fp);
2c1f71
+
2c1f71
+        SolvUserdata solv_userdata;
2c1f71
+        if (solv_userdata_fill(&solv_userdata, repoImpl->checksum, error)) {
2c1f71
+            fclose(fp);
2c1f71
+            success = FALSE;
2c1f71
+            goto done;
2c1f71
+        }
2c1f71
+
2c1f71
+        Repowriter *writer = repowriter_create(repo);
2c1f71
+        repowriter_set_userdata(writer, &solv_userdata, solv_userdata_size);
2c1f71
+        if (which_repodata != _HY_REPODATA_UPDATEINFO) {
2c1f71
+            repowriter_set_repodatarange(writer, data->repodataid, data->repodataid + 1);
2c1f71
+            repowriter_set_flags(writer, REPOWRITER_NO_STORAGE_SOLVABLE);
2c1f71
+            ret = repowriter_write(writer, fp);
2c1f71
+        } else {
2c1f71
+            // write only updateinfo repodata
2c1f71
+            int oldstart = repo->start;
2c1f71
+            repo->start = repoImpl->main_end;
2c1f71
+            repo->nsolvables -= repoImpl->main_nsolvables;
2c1f71
+            repowriter_set_flags(writer, REPOWRITER_LEGACY);
2c1f71
+            repowriter_set_keyfilter(writer, write_ext_updateinfo_filter, data);
2c1f71
+            repowriter_set_keyqueue(writer, 0);
2c1f71
+            ret = repowriter_write(writer, fp);
2c1f71
+            repo->start = oldstart;
2c1f71
+            repo->nsolvables += repoImpl->main_nsolvables;
2c1f71
+        }
2c1f71
+        repowriter_free(writer);
2c1f71
         if (ret) {
2c1f71
+            success = FALSE;
2c1f71
+            fclose(fp);
2c1f71
+            g_set_error (error,
2c1f71
+                        DNF_ERROR,
2c1f71
+                        DNF_ERROR_INTERNAL_ERROR,
2c1f71
+                        _("While writing extension cache %s (%d): repowriter write failed: %i, error: %s"),
2c1f71
+                        tmp_fn_templ, which_repodata, ret, pool_errstr(repo->pool));
2c1f71
+            goto done;
2c1f71
+        }
2c1f71
+
2c1f71
+        if (fclose(fp)) {
2c1f71
             success = FALSE;
2c1f71
             g_set_error (error,
2c1f71
                         DNF_ERROR,
2c1f71
-                        DNF_ERROR_FAILED,
2c1f71
-                        _("write_ext(%1$d) has failed: %2$d"),
2c1f71
-                        which_repodata, ret);
2c1f71
+                        DNF_ERROR_FILE_INVALID,
2c1f71
+                        _("While writing extension cache (%d): cannot close temporary file: %s"),
2c1f71
+                        which_repodata, tmp_fn_templ);
2c1f71
             goto done;
2c1f71
         }
2c1f71
     }
2c1f71
 
2c1f71
     if (repo_is_one_piece(repo) && which_repodata != _HY_REPODATA_UPDATEINFO) {
2c1f71
         /* switch over to written solv file activate paging */
2c1f71
-        FILE *fp = fopen(tmp_fn_templ, "r");
2c1f71
-        if (fp) {
2c1f71
-            int flags = REPO_USE_LOADING | REPO_EXTEND_SOLVABLES;
2c1f71
-            /* do not pollute the main pool with directory component ids */
2c1f71
-            if (which_repodata == _HY_REPODATA_FILENAMES || which_repodata == _HY_REPODATA_OTHER)
2c1f71
-                flags |= REPO_LOCALPOOL;
2c1f71
-            repodata_extend_block(data, repo->start, repo->end - repo->start);
2c1f71
-            data->state = REPODATA_LOADING;
2c1f71
-            repo_add_solv(repo, fp, flags);
2c1f71
-            data->state = REPODATA_AVAILABLE;
2c1f71
-            fclose(fp);
2c1f71
+        int flags = REPO_USE_LOADING | REPO_EXTEND_SOLVABLES;
2c1f71
+        /* do not pollute the main pool with directory component ids */
2c1f71
+        if (which_repodata == _HY_REPODATA_FILENAMES || which_repodata == _HY_REPODATA_OTHER)
2c1f71
+            flags |= REPO_LOCALPOOL;
2c1f71
+        repodata_extend_block(data, repo->start, repo->end - repo->start);
2c1f71
+        data->state = REPODATA_LOADING;
2c1f71
+        int loaded = try_to_use_cached_solvfile(tmp_fn_templ, repo, flags, repoImpl->checksum, error);
2c1f71
+        if (error && *error) {
2c1f71
+            g_prefix_error(error, _("Failed to use newly written extension cache: %s (%d): "),
2c1f71
+                           tmp_fn_templ, which_repodata);
2c1f71
+            success = FALSE;
2c1f71
+            goto done;
2c1f71
+        }
2c1f71
+        if (!loaded) {
2c1f71
+            g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
2c1f71
+                        _("Failed to use newly written extension cache: %s (%d)"), tmp_fn_templ, which_repodata);
2c1f71
+            success = FALSE;
2c1f71
+            goto done;
2c1f71
         }
2c1f71
+
2c1f71
+        data->state = REPODATA_AVAILABLE;
2c1f71
     }
2c1f71
 
2c1f71
     if (!mv(tmp_fn_templ, fn, error)) {
2c1f71
@@ -672,7 +733,7 @@ load_yum_repo(DnfSack *sack, HyRepo hrepo, GError **error)
2c1f71
 
2c1f71
     FILE *fp_primary = NULL;
2c1f71
     FILE *fp_repomd = NULL;
2c1f71
-    FILE *fp_cache = fopen(fn_cache, "r");
2c1f71
+
2c1f71
     if (!fn_repomd) {
2c1f71
         g_set_error (error,
2c1f71
                      DNF_ERROR,
2c1f71
@@ -693,18 +754,17 @@ load_yum_repo(DnfSack *sack, HyRepo hrepo, GError **error)
2c1f71
     }
2c1f71
     checksum_fp(repoImpl->checksum, fp_repomd);
2c1f71
 
2c1f71
-    if (can_use_repomd_cache(fp_cache, repoImpl->checksum)) {
2c1f71
+    if (try_to_use_cached_solvfile(fn_cache, repo, 0, repoImpl->checksum, error)) {
2c1f71
         const char *chksum = pool_checksum_str(pool, repoImpl->checksum);
2c1f71
         g_debug("using cached %s (0x%s)", name, chksum);
2c1f71
-        if (repo_add_solv(repo, fp_cache, 0)) {
2c1f71
-            g_set_error (error,
2c1f71
-                         DNF_ERROR,
2c1f71
-                         DNF_ERROR_INTERNAL_ERROR,
2c1f71
-                         _("repo_add_solv() has failed."));
2c1f71
-            retval = FALSE;
2c1f71
-            goto out;
2c1f71
-        }
2c1f71
         repoImpl->state_main = _HY_LOADED_CACHE;
2c1f71
+        goto out;
2c1f71
+    }
2c1f71
+
2c1f71
+    if (error && *error) {
2c1f71
+        g_prefix_error(error, _("While loading repository failed to use %s: "), fn_cache);
2c1f71
+        retval = FALSE;
2c1f71
+        goto out;
2c1f71
     } else {
2c1f71
         auto primary = hrepo->getMetadataPath(MD_TYPE_PRIMARY);
2c1f71
         if (primary.empty()) {
2c1f71
@@ -733,8 +793,6 @@ load_yum_repo(DnfSack *sack, HyRepo hrepo, GError **error)
2c1f71
         repoImpl->state_main = _HY_LOADED_FETCH;
2c1f71
     }
2c1f71
 out:
2c1f71
-    if (fp_cache)
2c1f71
-        fclose(fp_cache);
2c1f71
     if (fp_repomd)
2c1f71
         fclose(fp_repomd);
2c1f71
     if (fp_primary)
2c1f71
diff --git a/libdnf/hy-iutil-private.hpp b/libdnf/hy-iutil-private.hpp
2c1f71
index d498c032..efc91c63 100644
2c1f71
--- a/libdnf/hy-iutil-private.hpp
2c1f71
+++ b/libdnf/hy-iutil-private.hpp
2c1f71
@@ -52,9 +52,7 @@ int solv_userdata_verify(const SolvUserdata *solv_userdata, const unsigned char
2c1f71
 /* crypto utils */
2c1f71
 int checksum_cmp(const unsigned char *cs1, const unsigned char *cs2);
2c1f71
 int checksum_fp(unsigned char *out, FILE *fp);
2c1f71
-int checksum_read(unsigned char *csout, FILE *fp);
2c1f71
 int checksum_stat(unsigned char *out, FILE *fp);
2c1f71
-int checksum_write(const unsigned char *cs, FILE *fp);
2c1f71
 int checksumt_l2h(int type);
2c1f71
 const char *pool_checksum_str(Pool *pool, const unsigned char *chksum);
2c1f71
 
2c1f71
diff --git a/libdnf/hy-iutil.cpp b/libdnf/hy-iutil.cpp
2c1f71
index f81ca52f..c409a10a 100644
2c1f71
--- a/libdnf/hy-iutil.cpp
2c1f71
+++ b/libdnf/hy-iutil.cpp
2c1f71
@@ -142,17 +142,6 @@ checksum_fp(unsigned char *out, FILE *fp)
2c1f71
     return 0;
2c1f71
 }
2c1f71
 
2c1f71
-/* calls rewind(fp) before returning */
2c1f71
-int
2c1f71
-checksum_read(unsigned char *csout, FILE *fp)
2c1f71
-{
2c1f71
-    if (fseek(fp, -32, SEEK_END) ||
2c1f71
-        fread(csout, CHKSUM_BYTES, 1, fp) != 1)
2c1f71
-        return 1;
2c1f71
-    rewind(fp);
2c1f71
-    return 0;
2c1f71
-}
2c1f71
-
2c1f71
 /* does not move the fp position */
2c1f71
 int
2c1f71
 checksum_stat(unsigned char *out, FILE *fp)
2c1f71
@@ -174,15 +163,6 @@ checksum_stat(unsigned char *out, FILE *fp)
2c1f71
     return 0;
2c1f71
 }
2c1f71
 
2c1f71
-/* moves fp to the end of file */
2c1f71
-int checksum_write(const unsigned char *cs, FILE *fp)
2c1f71
-{
2c1f71
-    if (fseek(fp, 0, SEEK_END) ||
2c1f71
-        fwrite(cs, CHKSUM_BYTES, 1, fp) != 1)
2c1f71
-        return 1;
2c1f71
-    return 0;
2c1f71
-}
2c1f71
-
2c1f71
 static std::array<char, solv_userdata_solv_toolversion_size>
2c1f71
 get_padded_solv_toolversion()
2c1f71
 {
2c1f71
-- 
2c1f71
2.31.1
2c1f71