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

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