Blame SOURCES/0001-Preserve-changed-API-for-cr_compress_file_with_stat-RhBug1973588.patch

07a6c3
From 6bcfaac228236ac3c609d014cbd23c3bd645bf18 Mon Sep 17 00:00:00 2001
07a6c3
From: Aleš Matěj <amatej@redhat.com>
07a6c3
Date: Thu, 9 Sep 2021 08:31:03 +0200
07a6c3
Subject: [PATCH] Preserve changed API for cr_compress_file_with_stat (RhBug:1973588)
07a6c3
07a6c3
In order to be compatible in rhel8 we want to preserve the old API and
07a6c3
behavior.
07a6c3
07a6c3
Keep the fixed version as cr_compress_file_with_stat_v2 only for rhel8
07a6c3
07a6c3
https://bugzilla.redhat.com/show_bug.cgi?id=1973588
07a6c3
07a6c3
With fixed memory leak of `tmp_err`, reported here:
07a6c3
https://bugzilla.redhat.com/show_bug.cgi?id=2005781
07a6c3
---
07a6c3
 src/misc.c              | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
07a6c3
 src/misc.h              |  42 ++++++++++++++++++++++++++++++++++++++++--
07a6c3
 src/modifyrepo_shared.c |   4 ++--
07a6c3
 src/python/misc-py.c    |   2 +-
07a6c3
 src/threads.c           |  14 +++++++-------
07a6c3
 tests/test_misc.c       |  34 +++++++++++++++++-----------------
07a6c3
 6 files changed, 205 insertions(+), 30 deletions(-)
07a6c3
07a6c3
diff --git a/src/misc.c b/src/misc.c
07a6c3
index 4bd9f4c..c4b2cb3 100644
07a6c3
--- a/src/misc.c
07a6c3
+++ b/src/misc.c
07a6c3
@@ -446,7 +446,7 @@ cr_copy_file(const char *src, const char *in_dst, GError **err)
07a6c3
 
07a6c3
 int
07a6c3
 cr_compress_file_with_stat(const char *src,
07a6c3
-                           const char *in_dst,
07a6c3
+                           char **in_dst,
07a6c3
                            cr_CompressionType compression,
07a6c3
                            cr_ContentStat *stat,
07a6c3
                            const char *zck_dict_dir,
07a6c3
@@ -458,6 +458,143 @@ cr_compress_file_with_stat(const char *src,
07a6c3
     char buf[BUFFER_SIZE];
07a6c3
     CR_FILE *orig = NULL;
07a6c3
     CR_FILE *new = NULL;
07a6c3
+    gchar *dst = (gchar *) *in_dst;
07a6c3
+    GError *tmp_err = NULL;
07a6c3
+
07a6c3
+    assert(src);
07a6c3
+    assert(!err || *err == NULL);
07a6c3
+
07a6c3
+    const char *c_suffix = cr_compression_suffix(compression);
07a6c3
+
07a6c3
+    // Src must be a file NOT a directory
07a6c3
+    if (!g_file_test(src, G_FILE_TEST_IS_REGULAR)) {
07a6c3
+        g_debug("%s: Source (%s) must be a regular file!", __func__, src);
07a6c3
+        g_set_error(err, ERR_DOMAIN, CRE_NOFILE,
07a6c3
+                    "Not a regular file: %s", src);
07a6c3
+        return CRE_NOFILE;
07a6c3
+    }
07a6c3
+
07a6c3
+    if (!dst) {
07a6c3
+        // If destination is NULL, use src + compression suffix
07a6c3
+        *in_dst = g_strconcat(src,
07a6c3
+                              c_suffix,
07a6c3
+                              NULL);
07a6c3
+    } else if (g_str_has_suffix(dst, "/")) {
07a6c3
+        // If destination is dir use filename from src + compression suffix
07a6c3
+        *in_dst = g_strconcat(dst,
07a6c3
+                              cr_get_filename(src),
07a6c3
+                              c_suffix,
07a6c3
+                              NULL);
07a6c3
+    } else if (c_suffix && !g_str_has_suffix(dst, c_suffix)) {
07a6c3
+        cr_CompressionType old_type = cr_detect_compression(src, &tmp_err);
07a6c3
+        if (tmp_err) {
07a6c3
+            g_debug("%s: Unable to detect compression type of %s", __func__, src);
07a6c3
+            g_clear_error(&tmp_err);
07a6c3
+        } else if (old_type != CR_CW_NO_COMPRESSION) {
07a6c3
+            _cleanup_free_ gchar *tmp_file = g_strndup(dst, strlen(dst) - strlen(cr_compression_suffix(old_type)));
07a6c3
+            *in_dst = g_strconcat(tmp_file,
07a6c3
+                                  c_suffix,
07a6c3
+                                  NULL);
07a6c3
+        }
07a6c3
+    }
07a6c3
+    if (dst != *in_dst && dst)
07a6c3
+        g_free(dst);
07a6c3
+    dst = (gchar *) *in_dst;
07a6c3
+
07a6c3
+    int mode = CR_CW_AUTO_DETECT_COMPRESSION;
07a6c3
+
07a6c3
+    orig = cr_open(src,
07a6c3
+                   CR_CW_MODE_READ,
07a6c3
+                   mode,
07a6c3
+                   &tmp_err);
07a6c3
+    if (!orig) {
07a6c3
+        ret = tmp_err->code;
07a6c3
+        g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", src);
07a6c3
+        return ret;
07a6c3
+    }
07a6c3
+
07a6c3
+    _cleanup_free_ gchar *dict = NULL;
07a6c3
+    size_t dict_size = 0;
07a6c3
+    if (compression == CR_CW_ZCK_COMPRESSION && zck_dict_dir) {
07a6c3
+        /* Find zdict */
07a6c3
+        _cleanup_free_ gchar *file_basename = NULL;
07a6c3
+        if (dst) {
07a6c3
+            _cleanup_free_ gchar *dict_base = NULL;
07a6c3
+            if (g_str_has_suffix(dst, ".zck"))
07a6c3
+                dict_base = g_strndup(dst, strlen(dst)-4);
07a6c3
+            else
07a6c3
+                dict_base = g_strdup(dst);
07a6c3
+            file_basename = g_path_get_basename(dict_base);
07a6c3
+        } else {
07a6c3
+            file_basename = g_path_get_basename(src);
07a6c3
+        }
07a6c3
+        _cleanup_free_ gchar *dict_file = cr_get_dict_file(zck_dict_dir, file_basename);
07a6c3
+
07a6c3
+        /* Read dictionary from file */
07a6c3
+        if (dict_file && !g_file_get_contents(dict_file, &dict,
07a6c3
+                                             &dict_size, &tmp_err)) {
07a6c3
+            g_set_error(err, ERR_DOMAIN, CRE_IO,
07a6c3
+                        "Error reading zchunk dict %s: %s",
07a6c3
+                        dict_file, tmp_err->message);
07a6c3
+            g_clear_error(&tmp_err);
07a6c3
+            ret = CRE_IO;
07a6c3
+            goto compress_file_cleanup;
07a6c3
+        }
07a6c3
+    }
07a6c3
+
07a6c3
+    new = cr_sopen(dst, CR_CW_MODE_WRITE, compression, stat, &tmp_err);
07a6c3
+    if (tmp_err) {
07a6c3
+        g_debug("%s: Cannot open destination file %s", __func__, dst);
07a6c3
+        g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", dst);
07a6c3
+        ret = CRE_IO;
07a6c3
+        goto compress_file_cleanup;
07a6c3
+    }
07a6c3
+    if (compression == CR_CW_ZCK_COMPRESSION) {
07a6c3
+        if (dict && cr_set_dict(new, dict, dict_size, &tmp_err) != CRE_OK) {
07a6c3
+            ret = tmp_err->code;
07a6c3
+            g_propagate_prefixed_error(err, tmp_err, "Unable to set zdict for %s: ", dst);
07a6c3
+            goto compress_file_cleanup;
07a6c3
+        }
07a6c3
+        if (zck_auto_chunk && cr_set_autochunk(new, TRUE, &tmp_err) != CRE_OK) {
07a6c3
+            ret = tmp_err->code;
07a6c3
+            g_propagate_prefixed_error(err, tmp_err, "Unable to set auto-chunking for %s: ", dst);
07a6c3
+            goto compress_file_cleanup;
07a6c3
+        }
07a6c3
+    }
07a6c3
+    while ((readed = cr_read(orig, buf, BUFFER_SIZE, &tmp_err)) > 0) {
07a6c3
+        cr_write(new, buf, readed, &tmp_err);
07a6c3
+        if (tmp_err) {
07a6c3
+            ret = tmp_err->code;
07a6c3
+            g_propagate_prefixed_error(err, tmp_err, "Unable to write to %s: ", dst);
07a6c3
+            goto compress_file_cleanup;
07a6c3
+        }
07a6c3
+    }
07a6c3
+
07a6c3
+compress_file_cleanup:
07a6c3
+
07a6c3
+    if (orig)
07a6c3
+        cr_close(orig, NULL);
07a6c3
+
07a6c3
+    if (new)
07a6c3
+        cr_close(new, NULL);
07a6c3
+
07a6c3
+    return ret;
07a6c3
+}
07a6c3
+
07a6c3
+int
07a6c3
+cr_compress_file_with_stat_v2(const char *src,
07a6c3
+                              const char *in_dst,
07a6c3
+                              cr_CompressionType compression,
07a6c3
+                              cr_ContentStat *stat,
07a6c3
+                              const char *zck_dict_dir,
07a6c3
+                              gboolean zck_auto_chunk,
07a6c3
+                              GError **err)
07a6c3
+{
07a6c3
+    int ret = CRE_OK;
07a6c3
+    int readed;
07a6c3
+    char buf[BUFFER_SIZE];
07a6c3
+    CR_FILE *orig = NULL;
07a6c3
+    CR_FILE *new = NULL;
07a6c3
     gchar *dst = (gchar *) in_dst;
07a6c3
     GError *tmp_err = NULL;
07a6c3
 
07a6c3
diff --git a/src/misc.h b/src/misc.h
07a6c3
index 60f1a0f..528ccc3 100644
07a6c3
--- a/src/misc.h
07a6c3
+++ b/src/misc.h
07a6c3
@@ -184,9 +184,24 @@ gboolean cr_copy_file(const char *src,
07a6c3
                     cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ZCK_DICT_DIR, \
07a6c3
                                                ZCK_AUTO_CHUNK, ERR)
07a6c3
 
07a6c3
+/** Compress file. This function is temporary and present
07a6c3
+ * only in rhel 8, it will be removed in future versions.
07a6c3
+ * @param SRC           source filename
07a6c3
+ * @param DST           destination (If dst is dir, filename of src +
07a6c3
+ *                      compression suffix is used.
07a6c3
+ *                      If dst is NULL, src + compression suffix is used)
07a6c3
+ * @param COMTYPE       type of compression
07a6c3
+ * @param ZCK_DICT_DIR  Location of zchunk zdicts (if zchunk is enabled)
07a6c3
+ * @param ZCK_AUTO_CHUNK Whether zchunk file should be auto-chunked
07a6c3
+ * @param ERR           GError **
07a6c3
+ * @return              cr_Error return code
07a6c3
+ */
07a6c3
+#define cr_compress_file_v2(SRC, DST, COMTYPE, ZCK_DICT_DIR, ZCK_AUTO_CHUNK, ERR) \
07a6c3
+                    cr_compress_file_with_stat_v2(SRC, DST, COMTYPE, NULL, ZCK_DICT_DIR, \
07a6c3
+                                               ZCK_AUTO_CHUNK, ERR)
07a6c3
 /** Compress file.
07a6c3
  * @param src           source filename
07a6c3
- * @param dst           destination (If dst is dir, filename of src +
07a6c3
+ * @param dst           pointer to destination (If dst is dir, filename of src +
07a6c3
  *                      compression suffix is used.
07a6c3
  *                      If dst is NULL, src + compression suffix is used)
07a6c3
  * @param comtype       type of compression
07a6c3
@@ -197,13 +212,36 @@ gboolean cr_copy_file(const char *src,
07a6c3
  * @return              cr_Error return code
07a6c3
  */
07a6c3
 int cr_compress_file_with_stat(const char *src,
07a6c3
-                               const char *dst,
07a6c3
+                               char **dst,
07a6c3
                                cr_CompressionType comtype,
07a6c3
                                cr_ContentStat *stat,
07a6c3
                                const char *zck_dict_dir,
07a6c3
                                gboolean zck_auto_chunk,
07a6c3
                                GError **err);
07a6c3
 
07a6c3
+/** Compress file with stat versions 2. This function is temporary and present
07a6c3
+ * only in rhel 8, it will be removed in future versions.
07a6c3
+ * It is a compatibility function that preserves the API and behavior of
07a6c3
+ * cr_compress_file_with_stat from createrepo_c-0.12.0.
07a6c3
+ * @param src           source filename
07a6c3
+ * @param dst           destination (If dst is dir, filename of src +
07a6c3
+ *                      compression suffix is used.
07a6c3
+ *                      If dst is NULL, src + compression suffix is used)
07a6c3
+ * @param comtype       type of compression
07a6c3
+ * @param stat          pointer to cr_ContentStat or NULL
07a6c3
+ * @param zck_dict_dir  Location of zchunk zdicts (if zchunk is enabled)
07a6c3
+ * @param zck_auto_chunk Whether zchunk file should be auto-chunked
07a6c3
+ * @param err           GError **
07a6c3
+ * @return              cr_Error return code
07a6c3
+ */
07a6c3
+int cr_compress_file_with_stat_v2(const char *src,
07a6c3
+                                  const char *dst,
07a6c3
+                                  cr_CompressionType comtype,
07a6c3
+                                  cr_ContentStat *stat,
07a6c3
+                                  const char *zck_dict_dir,
07a6c3
+                                  gboolean zck_auto_chunk,
07a6c3
+                                  GError **err);
07a6c3
+
07a6c3
 /** Decompress file.
07a6c3
  * @param SRC           source filename
07a6c3
  * @param DST           destination (If dst is dir, filename of src without
07a6c3
diff --git a/src/modifyrepo_shared.c b/src/modifyrepo_shared.c
07a6c3
index 4e59660..8cf246d 100644
07a6c3
--- a/src/modifyrepo_shared.c
07a6c3
+++ b/src/modifyrepo_shared.c
07a6c3
@@ -120,8 +120,8 @@ cr_write_file(gchar *repopath, cr_ModifyRepoTask *task,
07a6c3
         g_debug("%s: Copy & compress operation %s -> %s",
07a6c3
                  __func__, src_fn, dst_fn);
07a6c3
 
07a6c3
-        if (cr_compress_file(src_fn, dst_fn, compress_type,
07a6c3
-                             task->zck_dict_dir, TRUE, err) != CRE_OK) {
07a6c3
+        if (cr_compress_file_v2(src_fn, dst_fn, compress_type,
07a6c3
+                                task->zck_dict_dir, TRUE, err) != CRE_OK) {
07a6c3
             g_debug("%s: Copy & compress operation failed", __func__);
07a6c3
             return NULL;
07a6c3
         }
07a6c3
diff --git a/src/python/misc-py.c b/src/python/misc-py.c
07a6c3
index 6a7871e..cc28448 100644
07a6c3
--- a/src/python/misc-py.c
07a6c3
+++ b/src/python/misc-py.c
07a6c3
@@ -49,7 +49,7 @@ py_compress_file_with_stat(G_GNUC_UNUSED PyObject *self, PyObject *args)
07a6c3
             return NULL;
07a6c3
     }
07a6c3
 
07a6c3
-    cr_compress_file_with_stat(src, dst, type, contentstat, NULL, FALSE, &tmp_err);
07a6c3
+    cr_compress_file_with_stat_v2(src, dst, type, contentstat, NULL, FALSE, &tmp_err);
07a6c3
     if (tmp_err) {
07a6c3
         nice_exception(&tmp_err, NULL);
07a6c3
         return NULL;
07a6c3
diff --git a/src/threads.c b/src/threads.c
07a6c3
index f0c3f93..b529d55 100644
07a6c3
--- a/src/threads.c
07a6c3
+++ b/src/threads.c
07a6c3
@@ -101,13 +101,13 @@ cr_compressing_thread(gpointer data, G_GNUC_UNUSED gpointer user_data)
07a6c3
                                 cr_compression_suffix(task->type),
07a6c3
                                 NULL);
07a6c3
 
07a6c3
-    cr_compress_file_with_stat(task->src,
07a6c3
-                               task->dst,
07a6c3
-                               task->type,
07a6c3
-                               task->stat,
07a6c3
-                               task->zck_dict_dir,
07a6c3
-                               task->zck_auto_chunk,
07a6c3
-                               &tmp_err);
07a6c3
+    cr_compress_file_with_stat_v2(task->src,
07a6c3
+                                  task->dst,
07a6c3
+                                  task->type,
07a6c3
+                                  task->stat,
07a6c3
+                                  task->zck_dict_dir,
07a6c3
+                                  task->zck_auto_chunk,
07a6c3
+                                  &tmp_err);
07a6c3
 
07a6c3
     if (tmp_err) {
07a6c3
         // Error encountered
07a6c3
diff --git a/tests/test_misc.c b/tests/test_misc.c
07a6c3
index 6614809..1acccb7 100644
07a6c3
--- a/tests/test_misc.c
07a6c3
+++ b/tests/test_misc.c
07a6c3
@@ -548,8 +548,8 @@ compressfile_test_text_file(Copyfiletest *copyfiletest,
07a6c3
     GError *tmp_err = NULL;
07a6c3
 
07a6c3
     g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS));
07a6c3
-    ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file,
07a6c3
-                           CR_CW_GZ_COMPRESSION, NULL, FALSE, &tmp_err);
07a6c3
+    ret = cr_compress_file_v2(TEST_TEXT_FILE, copyfiletest->dst_file,
07a6c3
+                              CR_CW_GZ_COMPRESSION, NULL, FALSE, &tmp_err);
07a6c3
     g_assert(!tmp_err);
07a6c3
     g_assert_cmpint(ret, ==, CRE_OK);
07a6c3
     g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR));
07a6c3
@@ -574,9 +574,9 @@ compressfile_with_stat_test_text_file(Copyfiletest *copyfiletest,
07a6c3
     g_assert(!tmp_err);
07a6c3
 
07a6c3
     g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS));
07a6c3
-    ret = cr_compress_file_with_stat(TEST_TEXT_FILE, copyfiletest->dst_file,
07a6c3
-                                     CR_CW_GZ_COMPRESSION, stat, NULL, FALSE,
07a6c3
-                                     &tmp_err);
07a6c3
+    ret = cr_compress_file_with_stat_v2(TEST_TEXT_FILE, copyfiletest->dst_file,
07a6c3
+                                        CR_CW_GZ_COMPRESSION, stat, NULL, FALSE,
07a6c3
+                                        &tmp_err);
07a6c3
     g_assert(!tmp_err);
07a6c3
     g_assert_cmpint(ret, ==, CRE_OK);
07a6c3
     g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR));
07a6c3
@@ -603,9 +603,9 @@ compressfile_with_stat_test_gz_file_gz_output(Copyfiletest *copyfiletest,
07a6c3
     char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".gz", NULL);
07a6c3
 
07a6c3
     g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS));
07a6c3
-    ret = cr_compress_file_with_stat(TEST_TEXT_FILE_GZ, dst_full_name,
07a6c3
-                                     CR_CW_GZ_COMPRESSION, stat, NULL, FALSE,
07a6c3
-                                     &tmp_err);
07a6c3
+    ret = cr_compress_file_with_stat_v2(TEST_TEXT_FILE_GZ, dst_full_name,
07a6c3
+                                        CR_CW_GZ_COMPRESSION, stat, NULL, FALSE,
07a6c3
+                                        &tmp_err);
07a6c3
     g_assert(!tmp_err);
07a6c3
     g_assert_cmpint(ret, ==, CRE_OK);
07a6c3
     g_assert(g_file_test(dst_full_name, G_FILE_TEST_IS_REGULAR));
07a6c3
@@ -633,9 +633,9 @@ compressfile_test_gz_file_xz_output(Copyfiletest *copyfiletest,
07a6c3
     char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".xz", NULL);
07a6c3
 
07a6c3
     g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS));
07a6c3
-    ret = cr_compress_file(TEST_TEXT_FILE_GZ, dst_full_name,
07a6c3
-                                     CR_CW_XZ_COMPRESSION, NULL, FALSE,
07a6c3
-                                     &tmp_err);
07a6c3
+    ret = cr_compress_file_v2(TEST_TEXT_FILE_GZ, dst_full_name,
07a6c3
+                              CR_CW_XZ_COMPRESSION, NULL, FALSE,
07a6c3
+                              &tmp_err);
07a6c3
     g_assert(!tmp_err);
07a6c3
     g_assert_cmpint(ret, ==, CRE_OK);
07a6c3
     g_assert(g_file_test(dst_full_name, G_FILE_TEST_IS_REGULAR));
07a6c3
@@ -660,9 +660,9 @@ compressfile_test_xz_file_gz_output(Copyfiletest *copyfiletest,
07a6c3
     char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".gz", NULL);
07a6c3
 
07a6c3
     g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS));
07a6c3
-    ret = cr_compress_file(TEST_TEXT_FILE_XZ, dst_full_name,
07a6c3
-                                     CR_CW_GZ_COMPRESSION, NULL, FALSE,
07a6c3
-                                     &tmp_err);
07a6c3
+    ret = cr_compress_file_v2(TEST_TEXT_FILE_XZ, dst_full_name,
07a6c3
+                              CR_CW_GZ_COMPRESSION, NULL, FALSE,
07a6c3
+                              &tmp_err);
07a6c3
     g_assert(!tmp_err);
07a6c3
     g_assert_cmpint(ret, ==, CRE_OK);
07a6c3
     g_assert(g_file_test(dst_full_name, G_FILE_TEST_IS_REGULAR));
07a6c3
@@ -687,9 +687,9 @@ compressfile_test_sqlite_file_gz_output(Copyfiletest *copyfiletest,
07a6c3
     char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".gz", NULL);
07a6c3
 
07a6c3
     g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS));
07a6c3
-    ret = cr_compress_file(TEST_SQLITE_FILE, dst_full_name,
07a6c3
-                                     CR_CW_GZ_COMPRESSION, NULL, FALSE,
07a6c3
-                                     &tmp_err);
07a6c3
+    ret = cr_compress_file_v2(TEST_SQLITE_FILE, dst_full_name,
07a6c3
+                              CR_CW_GZ_COMPRESSION, NULL, FALSE,
07a6c3
+                              &tmp_err);
07a6c3
     g_assert(!tmp_err);
07a6c3
     g_assert_cmpint(ret, ==, CRE_OK);
07a6c3
     g_assert(g_file_test(dst_full_name, G_FILE_TEST_EXISTS));
07a6c3
--
07a6c3
libgit2 1.1.0
07a6c3