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

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