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

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