compression_wrapper: Little change in use of cr_ContentStat object.
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 6 Jun 2013 10:54:50 +0000 (12:54 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 6 Jun 2013 10:54:50 +0000 (12:54 +0200)
src/compression_wrapper.c
src/compression_wrapper.h
src/misc.h
tests/test_compression_wrapper.c
tests/test_misc.c

index 3f6c567..bf105b3 100644 (file)
@@ -88,46 +88,25 @@ cr_ContentStat *
 cr_contentstat_new(cr_ChecksumType type, GError **err)
 {
     cr_ContentStat *cstat;
-    cr_ChecksumCtx *checksum;
-    GError *tmp_err = NULL;
 
     assert(!err || *err == NULL);
 
-    if (type == CR_CHECKSUM_UNKNOWN) {
-        checksum = NULL;
-    } else {
-        checksum = cr_checksum_new(type, &tmp_err);
-        if (tmp_err) {
-            g_propagate_error(err, tmp_err);
-            return NULL;
-        }
-    }
-
     cstat = g_malloc0(sizeof(cr_ContentStat));
-    cstat->checksum = checksum;
+    cstat->checksum_type = type;
 
     return cstat;
 }
 
-char *
+void
 cr_contentstat_free(cr_ContentStat *cstat, GError **err)
 {
-    char *checksum_str = NULL;
-    GError *tmp_err = NULL;
-
     assert(!err || *err == NULL);
 
     if (!cstat)
-        return NULL;
-
-    if (cstat->checksum) {
-        checksum_str = cr_checksum_final(cstat->checksum, &tmp_err);
-        if (tmp_err)
-            g_propagate_error(err, tmp_err);
-    }
+        return;
 
+    g_free(cstat->checksum);
     g_free(cstat);
-    return checksum_str;
 }
 
 typedef struct {
@@ -504,7 +483,21 @@ cr_open_with_stat(const char *filename,
         return NULL;
     }
 
-    file->stat = stat;
+    if (stat) {
+        file->stat = stat;
+
+        if (stat->checksum_type == CR_CHECKSUM_UNKNOWN) {
+            file->checksum_ctx = NULL;
+        } else {
+            file->checksum_ctx = cr_checksum_new(stat->checksum_type,
+                                                 &tmp_err);
+            if (tmp_err) {
+                g_propagate_error(err, tmp_err);
+                cr_close(file, NULL);
+                return NULL;
+            }
+        }
+    }
 
     assert(!err || (!file && *err != NULL) || (file && *err == NULL));
 
@@ -675,6 +668,10 @@ cr_close(CR_FILE *cr_file, GError **err)
             break;
     }
 
+    if (cr_file->stat)
+        cr_file->stat->checksum = cr_checksum_final(cr_file->checksum_ctx,
+                                                    NULL);
+
     g_free(cr_file);
 
     assert(!err || (ret != CRE_OK && *err != NULL)
@@ -891,18 +888,14 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len, GError **err)
     }
 
     if (cr_file->stat) {
-        // Do open content stat
-
-        cr_ContentStat *stat = cr_file->stat;
-        GError *tmp_err = NULL;
-
-        stat->size += len;
-        if (stat->checksum)
-            cr_checksum_update(stat->checksum, buffer, len, &tmp_err);
-
-        if (tmp_err) {
-            g_propagate_error(err, tmp_err);
-            return CR_CW_ERR;
+        cr_file->stat->size += len;
+        if (cr_file->checksum_ctx) {
+            GError *tmp_err = NULL;
+            cr_checksum_update(cr_file->checksum_ctx, buffer, len, &tmp_err);
+            if (tmp_err) {
+                g_propagate_error(err, tmp_err);
+                return CR_CW_ERR;
+            }
         }
     }
 
index 632ae4f..c956924 100644 (file)
@@ -54,8 +54,9 @@ typedef enum {
 /** Stat build about open content during compression (writting).
  */
 typedef struct {
-    gint64          size;       /*!< Size of content */
-    cr_ChecksumCtx  *checksum;  /*!< Checksum context */
+    gint64          size;           /*!< Size of content */
+    cr_ChecksumType checksum_type;  /*!< Checksum type */
+    char            *checksum;      /*!< Checksum */
 } cr_ContentStat;
 
 /** Creates new cr_ContentStat object
@@ -69,18 +70,17 @@ cr_ContentStat *cr_contentstat_new(cr_ChecksumType type, GError **err);
 /** Frees cr_ContentStat object.
  * @param cstat     cr_ContentStat object
  * @param err       GError **
- * @return          checksum or NULL on error or if checksum calculation
- *                  was disabled (CR_CHECKSUM_UNKNOWN was used)
  */
-char *cr_contentstat_free(cr_ContentStat *cstat, GError **err);
+void cr_contentstat_free(cr_ContentStat *cstat, GError **err);
 
 /** Structure represents a compressed file.
  */
 typedef struct {
-    cr_CompressionType  type;   /*!< Type of compression */
-    void                *FILE;  /*!< Pointer to gzFile, BZFILE or plain FILE */
-    cr_OpenMode         mode;   /*!< Mode */
-    cr_ContentStat      *stat;  /*!< Content stats */
+    cr_CompressionType  type;           /*!< Type of compression */
+    void                *FILE;          /*!< Pointer to gzFile, BZFILE, ... */
+    cr_OpenMode         mode;           /*!< Mode */
+    cr_ContentStat      *stat;          /*!< Content stats */
+    cr_ChecksumCtx      *checksum_ctx;  /*!< Checksum contenxt */
 } CR_FILE;
 
 #define CR_CW_ERR       -1      /*!< Return value - Error */
@@ -114,7 +114,7 @@ cr_CompressionType cr_detect_compression(const char* filename, GError **err);
  * @param filename      filename
  * @param mode          open mode
  * @param comtype       type of compression
- * @param stat          cr_ContentStat object or NULL
+ * @param stat          pointer to cr_ContentStat or NULL
  * @param err           GError **
  * @return              pointer to a CR_FILE or NULL
  */
index f381823..46d771d 100644 (file)
@@ -165,7 +165,7 @@ int cr_copy_file(const char *src,
  *                      compression suffix is used.
  *                      If dst is NULL, src + compression suffix is used)
  * @param comtype       type of compression
- * @param stat          cr_ContentStat object or NULL
+ * @param stat          pointer to cr_ContentStat or NULL
  * @param err           GError **
  * @return              cr_Error return code
  */
index 58bf445..3879c05 100644 (file)
 static void
 test_cr_contentstat(void)
 {
-    char *checksum;
     cr_ContentStat *cs = NULL;
     GError *tmp_err = NULL;
 
     cs = cr_contentstat_new(CR_CHECKSUM_UNKNOWN, NULL);
     g_assert(cs);
-    checksum = cr_contentstat_free(cs, NULL);
-    g_assert(!checksum);
+    g_assert(!cs->checksum);
+    cr_contentstat_free(cs, NULL);
     cs = NULL;
 
     cs = cr_contentstat_new(CR_CHECKSUM_UNKNOWN, &tmp_err);
     g_assert(cs);
     g_assert(!tmp_err);
-    checksum = cr_contentstat_free(cs, &tmp_err);
-    g_assert(!checksum);
+    g_assert(!cs->checksum);
+    cr_contentstat_free(cs, &tmp_err);
     g_assert(!tmp_err);
     cs = NULL;
-
-    cs = cr_contentstat_new(CR_CHECKSUM_SHA256, NULL);
-    g_assert(cs);
-    checksum = cr_contentstat_free(cs, NULL);
-    g_assert(checksum);
-    g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649"
-                                  "b934ca495991b7852b855");
-    g_free(checksum);
-    cs = NULL;
-    checksum = NULL;
-
-    cs = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
-    g_assert(cs);
-    g_assert(!tmp_err);
-    checksum = cr_contentstat_free(cs, &tmp_err);
-    g_assert(!tmp_err);
-    g_assert(checksum);
-    g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649"
-                                  "b934ca495991b7852b855");
-    g_free(checksum);
-    cs = NULL;
-    checksum = NULL;
 }
 
 static void
@@ -561,7 +538,6 @@ test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
     CR_FILE *f;
     int ret;
     cr_ContentStat *stat;
-    char *checksum;
     GError *tmp_err = NULL;
 
     CR_UNUSED(test_data);
@@ -593,12 +569,9 @@ test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
     g_assert(!tmp_err);
 
     g_assert_cmpint(stat->size, ==, content_len);
-    checksum = cr_contentstat_free(stat, &tmp_err);
-    g_assert(checksum);
+    g_assert_cmpstr(stat->checksum, ==, content_sha256);
+    cr_contentstat_free(stat, &tmp_err);
     g_assert(!tmp_err);
-    g_assert_cmpstr(checksum, ==, content_sha256);
-    g_free(checksum);
-    checksum = NULL;
 
     // Gz compression
 
@@ -622,12 +595,9 @@ test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
     g_assert(!tmp_err);
 
     g_assert_cmpint(stat->size, ==, content_len);
-    checksum = cr_contentstat_free(stat, &tmp_err);
-    g_assert(checksum);
+    g_assert_cmpstr(stat->checksum, ==, content_sha256);
+    cr_contentstat_free(stat, &tmp_err);
     g_assert(!tmp_err);
-    g_assert_cmpstr(checksum, ==, content_sha256);
-    g_free(checksum);
-    checksum = NULL;
 
     // Bz2 compression
 
@@ -651,12 +621,9 @@ test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
     g_assert(!tmp_err);
 
     g_assert_cmpint(stat->size, ==, content_len);
-    checksum = cr_contentstat_free(stat, &tmp_err);
-    g_assert(checksum);
+    g_assert_cmpstr(stat->checksum, ==, content_sha256);
+    cr_contentstat_free(stat, &tmp_err);
     g_assert(!tmp_err);
-    g_assert_cmpstr(checksum, ==, content_sha256);
-    g_free(checksum);
-    checksum = NULL;
 
     // Xz compression
 
@@ -680,12 +647,9 @@ test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
     g_assert(!tmp_err);
 
     g_assert_cmpint(stat->size, ==, content_len);
-    checksum = cr_contentstat_free(stat, &tmp_err);
-    g_assert(checksum);
+    g_assert_cmpstr(stat->checksum, ==, content_sha256);
+    cr_contentstat_free(stat, &tmp_err);
     g_assert(!tmp_err);
-    g_assert_cmpstr(checksum, ==, content_sha256);
-    g_free(checksum);
-    checksum = NULL;
 }
 
 static void
@@ -694,7 +658,6 @@ test_contentstating_multiwrite(Outputtest *outputtest, gconstpointer test_data)
     CR_FILE *f;
     int ret;
     cr_ContentStat *stat;
-    char *checksum;
     GError *tmp_err = NULL;
 
     CR_UNUSED(test_data);
@@ -730,12 +693,9 @@ test_contentstating_multiwrite(Outputtest *outputtest, gconstpointer test_data)
     g_assert(!tmp_err);
 
     g_assert_cmpint(stat->size, ==, content_len);
-    checksum = cr_contentstat_free(stat, &tmp_err);
-    g_assert(checksum);
+    g_assert_cmpstr(stat->checksum, ==, content_sha256);
+    cr_contentstat_free(stat, &tmp_err);
     g_assert(!tmp_err);
-    g_assert_cmpstr(checksum, ==, content_sha256);
-    g_free(checksum);
-    checksum = NULL;
 }
 
 
index 7375cba..42a664c 100644 (file)
@@ -566,7 +566,7 @@ compressfile_with_stat_test_text_file(Copyfiletest *copyfiletest, gconstpointer
 {
     CR_UNUSED(test_data);
     int ret;
-    char *checksum, *checksum_;
+    char *checksum;
     cr_ContentStat *stat;
     GError *tmp_err = NULL;
 
@@ -575,18 +575,15 @@ compressfile_with_stat_test_text_file(Copyfiletest *copyfiletest, gconstpointer
     g_assert(!tmp_err);
 
     g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS));
-    ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file,
-                           CR_CW_GZ_COMPRESSION, &tmp_err);
+    ret = cr_compress_file_with_stat(TEST_TEXT_FILE, copyfiletest->dst_file,
+                                     CR_CW_GZ_COMPRESSION, stat, &tmp_err);
     g_assert(!tmp_err);
     g_assert_cmpint(ret, ==, CRE_OK);
     g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR));
-    checksum = cr_contentstat_free(stat, &tmp_err);
-    g_assert(checksum);
+    checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA256, NULL);
+    g_assert_cmpstr(stat->checksum, ==, checksum);
+    cr_contentstat_free(stat, &tmp_err);
     g_assert(!tmp_err);
-    checksum_ = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA256, NULL);
-    g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649"
-                                  "b934ca495991b7852b855");
-    g_free(checksum);
 }
 
 static void