Blob Blame History Raw
From a535b3f25e4391f23d1cee46028827285e221de3 Mon Sep 17 00:00:00 2001
From: Aleš Matěj <amatej@redhat.com>
Date: Tue, 18 Jun 2019 13:49:27 +0200
Subject: [PATCH] modifyrepo_c: Prevent doubling of compression (test.gz.gz) (RhBug:1639287)

---
 src/compression_wrapper.c |  3 ++-
 src/misc.c                | 22 +++++++---------------
 src/modifyrepo_shared.c   | 22 +++++++++++++++++++++-
 3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/src/compression_wrapper.c b/src/compression_wrapper.c
index adc2f39..efb075c 100644
--- a/src/compression_wrapper.c
+++ b/src/compression_wrapper.c
@@ -148,7 +148,8 @@ cr_detect_compression(const char *filename, GError **err)
     } else if (g_str_has_suffix(filename, ".xz"))
     {
         return CR_CW_XZ_COMPRESSION;
-    } else if (g_str_has_suffix(filename, ".xml"))
+    } else if (g_str_has_suffix(filename, ".xml") ||
+               g_str_has_suffix(filename, ".sqlite"))
     {
         return CR_CW_NO_COMPRESSION;
     }
diff --git a/src/misc.c b/src/misc.c
index 9937480..c5ccd12 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -437,7 +437,7 @@ cr_compress_file_with_stat(const char *src,
     int ret = CRE_OK;
     int readed;
     char buf[BUFFER_SIZE];
-    FILE *orig = NULL;
+    CR_FILE *orig = NULL;
     CR_FILE *new = NULL;
     gchar *dst = (gchar *) in_dst;
     GError *tmp_err = NULL;
@@ -466,7 +466,7 @@ cr_compress_file_with_stat(const char *src,
                           NULL);
     }
 
-    orig = fopen(src, "rb");
+    orig = cr_open(src, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err);
     if (orig == NULL) {
         g_debug("%s: Cannot open source file %s (%s)", __func__, src,
                 g_strerror(errno));
@@ -484,21 +484,13 @@ cr_compress_file_with_stat(const char *src,
         goto compress_file_cleanup;
     }
 
-    while ((readed = fread(buf, 1, BUFFER_SIZE, orig)) > 0) {
-        if (readed != BUFFER_SIZE && ferror(orig)) {
-            g_debug("%s: Error while copy %s -> %s (%s)", __func__, src,
-                    dst, g_strerror(errno));
-            g_set_error(err, ERR_DOMAIN, CRE_IO,
-                        "Error while read %s: %s", src, g_strerror(errno));
-            ret = CRE_IO;
-            goto compress_file_cleanup;
-        }
-
-        cr_write(new, buf, readed, &tmp_err);
+    while ((readed = cr_read(orig, buf, BUFFER_SIZE, &tmp_err)) > 0) {
+        if (!tmp_err)
+            cr_write(new, buf, readed, &tmp_err);
         if (tmp_err) {
             g_debug("%s: Error while copy %s -> %s", __func__, src, dst);
             g_propagate_prefixed_error(err, tmp_err,
-                    "Error while read %s: ", dst);
+                    "Error while copy to %s: ", dst);
             ret = CRE_IO;
             goto compress_file_cleanup;
         }
@@ -510,7 +502,7 @@ compress_file_cleanup:
         g_free(dst);
 
     if (orig)
-        fclose(orig);
+        cr_close(orig, NULL);
 
     if (new)
         cr_close(new, NULL);
diff --git a/src/modifyrepo_shared.c b/src/modifyrepo_shared.c
index 805c894..91e56e8 100644
--- a/src/modifyrepo_shared.c
+++ b/src/modifyrepo_shared.c
@@ -50,6 +50,23 @@ cr_modifyrepotask_free(cr_ModifyRepoTask *task)
     g_free(task);
 }
 
+gchar *
+remove_compression_suffix_if_present(gchar* name, GError **err)
+{
+    cr_CompressionType src_fn_com_type = cr_detect_compression(name, err);
+    if (src_fn_com_type != CR_CW_NO_COMPRESSION && src_fn_com_type != CR_CW_UNKNOWN_COMPRESSION){
+        const gchar *src_suffix = cr_compression_suffix(src_fn_com_type);
+        if (src_suffix){
+            if (g_str_has_suffix(name, src_suffix)){
+                int name_len = strlen(name);
+                int suffix_len = strlen(src_suffix);
+                return g_strndup(name, name_len - suffix_len);
+            }
+        }
+    }
+    return g_strdup(name);
+}
+
 gboolean
 cr_modifyrepo(GSList *modifyrepotasks, gchar *repopath, GError **err)
 {
@@ -192,12 +209,15 @@ cr_modifyrepo(GSList *modifyrepotasks, gchar *repopath, GError **err)
             suffix = cr_compression_suffix(compress_type);
         }
 
+        char* sufixless_src_fn = remove_compression_suffix_if_present(task->path, err);
+
         // Prepare dst filename - Get basename
         _cleanup_free_ gchar *filename = NULL;
         if (task->new_name)
             filename = g_path_get_basename(task->new_name);
         else
-            filename = g_path_get_basename(src_fn);
+            filename = g_path_get_basename(sufixless_src_fn);
+        g_free(sufixless_src_fn);
 
         // Prepare dst filename - Add suffix
         if (suffix) {
--
libgit2 0.27.8