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

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