Blame SOURCES/0002-modifyrepo_c-Prevent-doubling-of-compression-testgzgz-RhBug1639287.patch

6cec19
From a535b3f25e4391f23d1cee46028827285e221de3 Mon Sep 17 00:00:00 2001
6cec19
From: Aleš Matěj <amatej@redhat.com>
6cec19
Date: Tue, 18 Jun 2019 13:49:27 +0200
6cec19
Subject: [PATCH] modifyrepo_c: Prevent doubling of compression (test.gz.gz) (RhBug:1639287)
6cec19
6cec19
---
6cec19
 src/compression_wrapper.c |  3 ++-
6cec19
 src/misc.c                | 22 +++++++---------------
6cec19
 src/modifyrepo_shared.c   | 22 +++++++++++++++++++++-
6cec19
 3 files changed, 30 insertions(+), 17 deletions(-)
6cec19
6cec19
diff --git a/src/compression_wrapper.c b/src/compression_wrapper.c
6cec19
index adc2f39..efb075c 100644
6cec19
--- a/src/compression_wrapper.c
6cec19
+++ b/src/compression_wrapper.c
6cec19
@@ -148,7 +148,8 @@ cr_detect_compression(const char *filename, GError **err)
6cec19
     } else if (g_str_has_suffix(filename, ".xz"))
6cec19
     {
6cec19
         return CR_CW_XZ_COMPRESSION;
6cec19
-    } else if (g_str_has_suffix(filename, ".xml"))
6cec19
+    } else if (g_str_has_suffix(filename, ".xml") ||
6cec19
+               g_str_has_suffix(filename, ".sqlite"))
6cec19
     {
6cec19
         return CR_CW_NO_COMPRESSION;
6cec19
     }
6cec19
diff --git a/src/misc.c b/src/misc.c
6cec19
index 9937480..c5ccd12 100644
6cec19
--- a/src/misc.c
6cec19
+++ b/src/misc.c
6cec19
@@ -437,7 +437,7 @@ cr_compress_file_with_stat(const char *src,
6cec19
     int ret = CRE_OK;
6cec19
     int readed;
6cec19
     char buf[BUFFER_SIZE];
6cec19
-    FILE *orig = NULL;
6cec19
+    CR_FILE *orig = NULL;
6cec19
     CR_FILE *new = NULL;
6cec19
     gchar *dst = (gchar *) in_dst;
6cec19
     GError *tmp_err = NULL;
6cec19
@@ -466,7 +466,7 @@ cr_compress_file_with_stat(const char *src,
6cec19
                           NULL);
6cec19
     }
6cec19
 
6cec19
-    orig = fopen(src, "rb");
6cec19
+    orig = cr_open(src, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err);
6cec19
     if (orig == NULL) {
6cec19
         g_debug("%s: Cannot open source file %s (%s)", __func__, src,
6cec19
                 g_strerror(errno));
6cec19
@@ -484,21 +484,13 @@ cr_compress_file_with_stat(const char *src,
6cec19
         goto compress_file_cleanup;
6cec19
     }
6cec19
 
6cec19
-    while ((readed = fread(buf, 1, BUFFER_SIZE, orig)) > 0) {
6cec19
-        if (readed != BUFFER_SIZE && ferror(orig)) {
6cec19
-            g_debug("%s: Error while copy %s -> %s (%s)", __func__, src,
6cec19
-                    dst, g_strerror(errno));
6cec19
-            g_set_error(err, ERR_DOMAIN, CRE_IO,
6cec19
-                        "Error while read %s: %s", src, g_strerror(errno));
6cec19
-            ret = CRE_IO;
6cec19
-            goto compress_file_cleanup;
6cec19
-        }
6cec19
-
6cec19
-        cr_write(new, buf, readed, &tmp_err);
6cec19
+    while ((readed = cr_read(orig, buf, BUFFER_SIZE, &tmp_err)) > 0) {
6cec19
+        if (!tmp_err)
6cec19
+            cr_write(new, buf, readed, &tmp_err);
6cec19
         if (tmp_err) {
6cec19
             g_debug("%s: Error while copy %s -> %s", __func__, src, dst);
6cec19
             g_propagate_prefixed_error(err, tmp_err,
6cec19
-                    "Error while read %s: ", dst);
6cec19
+                    "Error while copy to %s: ", dst);
6cec19
             ret = CRE_IO;
6cec19
             goto compress_file_cleanup;
6cec19
         }
6cec19
@@ -510,7 +502,7 @@ compress_file_cleanup:
6cec19
         g_free(dst);
6cec19
 
6cec19
     if (orig)
6cec19
-        fclose(orig);
6cec19
+        cr_close(orig, NULL);
6cec19
 
6cec19
     if (new)
6cec19
         cr_close(new, NULL);
6cec19
diff --git a/src/modifyrepo_shared.c b/src/modifyrepo_shared.c
6cec19
index 805c894..91e56e8 100644
6cec19
--- a/src/modifyrepo_shared.c
6cec19
+++ b/src/modifyrepo_shared.c
6cec19
@@ -50,6 +50,23 @@ cr_modifyrepotask_free(cr_ModifyRepoTask *task)
6cec19
     g_free(task);
6cec19
 }
6cec19
 
6cec19
+gchar *
6cec19
+remove_compression_suffix_if_present(gchar* name, GError **err)
6cec19
+{
6cec19
+    cr_CompressionType src_fn_com_type = cr_detect_compression(name, err);
6cec19
+    if (src_fn_com_type != CR_CW_NO_COMPRESSION && src_fn_com_type != CR_CW_UNKNOWN_COMPRESSION){
6cec19
+        const gchar *src_suffix = cr_compression_suffix(src_fn_com_type);
6cec19
+        if (src_suffix){
6cec19
+            if (g_str_has_suffix(name, src_suffix)){
6cec19
+                int name_len = strlen(name);
6cec19
+                int suffix_len = strlen(src_suffix);
6cec19
+                return g_strndup(name, name_len - suffix_len);
6cec19
+            }
6cec19
+        }
6cec19
+    }
6cec19
+    return g_strdup(name);
6cec19
+}
6cec19
+
6cec19
 gboolean
6cec19
 cr_modifyrepo(GSList *modifyrepotasks, gchar *repopath, GError **err)
6cec19
 {
6cec19
@@ -192,12 +209,15 @@ cr_modifyrepo(GSList *modifyrepotasks, gchar *repopath, GError **err)
6cec19
             suffix = cr_compression_suffix(compress_type);
6cec19
         }
6cec19
 
6cec19
+        char* sufixless_src_fn = remove_compression_suffix_if_present(task->path, err);
6cec19
+
6cec19
         // Prepare dst filename - Get basename
6cec19
         _cleanup_free_ gchar *filename = NULL;
6cec19
         if (task->new_name)
6cec19
             filename = g_path_get_basename(task->new_name);
6cec19
         else
6cec19
-            filename = g_path_get_basename(src_fn);
6cec19
+            filename = g_path_get_basename(sufixless_src_fn);
6cec19
+        g_free(sufixless_src_fn);
6cec19
 
6cec19
         // Prepare dst filename - Add suffix
6cec19
         if (suffix) {
6cec19
--
6cec19
libgit2 0.27.8
6cec19