Fix compression so it works correctly if there's no dict
authorJonathan Dieter <jdieter@gmail.com>
Mon, 26 Mar 2018 09:29:50 +0000 (12:29 +0300)
committerJonathan Dieter <jdieter@gmail.com>
Mon, 26 Mar 2018 09:29:50 +0000 (12:29 +0300)
Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
src/lib/comp/comp.c
src/lib/index/index_create.c

index 59e1bd5a2e19935430b621d810e0ab0287acee85..a1a465f5cd07d9499190ac7ca0a86253381237e4 100644 (file)
@@ -68,28 +68,32 @@ int zck_comp_init(zckCtx *zck) {
     if(!zck->comp.init(&(zck->comp)))
         return False;
 
-    if(zck->comp.dict && zck->temp_fd) {
-        if(!zck->comp.compress(comp, zck->comp.dict, zck->comp.dict_size, &dst,
-                               &dst_size, 0))
-            return False;
-        if(!zck_write(zck->temp_fd, dst, dst_size)) {
+    if(zck->temp_fd) {
+        if(zck->comp.dict) {
+            if(!zck->comp.compress(comp, zck->comp.dict, zck->comp.dict_size, &dst,
+                                   &dst_size, 0))
+                return False;
+            if(!zck_write(zck->temp_fd, dst, dst_size)) {
+                free(dst);
+                return False;
+            }
+            zck_index_add_to_chunk(zck, dst, dst_size, zck->comp.dict_size);
             free(dst);
-            return False;
-        }
-        zck_index_add_to_chunk(zck, dst, dst_size, zck->comp.dict_size);
-        free(dst);
-        dst = NULL;
-        dst_size = 0;
-
-        if(!zck->comp.end_chunk(comp, &dst, &dst_size, 0))
-            return False;
-        if(!zck_write(zck->temp_fd, dst, dst_size)) {
+            dst = NULL;
+            dst_size = 0;
+
+            if(!zck->comp.end_chunk(comp, &dst, &dst_size, 0))
+                return False;
+            if(!zck_write(zck->temp_fd, dst, dst_size)) {
+                free(dst);
+                return False;
+            }
+            zck_index_add_to_chunk(zck, dst, dst_size, 0);
+            zck_index_finish_chunk(zck);
             free(dst);
-            return False;
+        } else {
+            zck_index_finish_chunk(zck);
         }
-        zck_index_add_to_chunk(zck, dst, dst_size, 0);
-        zck_index_finish_chunk(zck);
-        free(dst);
     }
     zck->comp.dict = NULL;
     zck->comp.dict_size = 0;
@@ -133,11 +137,8 @@ int zck_end_chunk(zckCtx *zck) {
     }
 
     /* No point in compressing empty data */
-    if(zck->comp.data_size == 0) {
-        if(!zck_index_finish_chunk(zck))
-            return False;
+    if(zck->comp.data_size == 0)
         return True;
-    }
 
     char *dst = NULL;
     size_t dst_size = 0;
index d0eeeae27c952dde22ef3e08964849e8ce84e5a2..17b3fca938c444c5387dc82c5155bc6dca131467 100644 (file)
@@ -221,15 +221,24 @@ int zck_index_finish_chunk(zckCtx *zck) {
     if(zck->work_index_item == NULL && !zck_index_create_chunk(zck))
         return False;
 
-    /* Finalize chunk checksum */
-    char *digest = zck_hash_finalize(&(zck->work_index_hash));
-    if(digest == NULL) {
-        zck_log(ZCK_LOG_ERROR,
-                "Unable to calculate %s checksum for new chunk\n",
-                zck_hash_name_from_type(zck->index.hash_type));
-        return False;
+    char *digest = NULL;
+    if(zck->work_index_item->length > 0) {
+        /* Finalize chunk checksum */
+        digest = zck_hash_finalize(&(zck->work_index_hash));
+        if(digest == NULL) {
+            zck_log(ZCK_LOG_ERROR,
+                    "Unable to calculate %s checksum for new chunk\n",
+                    zck_hash_name_from_type(zck->index.hash_type));
+            return False;
+        }
+    } else {
+        digest = zmalloc(zck->chunk_hash_type.digest_size);
+        if(digest == NULL) {
+            zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n",
+                    zck->chunk_hash_type.digest_size);
+            return False;
+        }
     }
-
     if(!finish_chunk(&(zck->index), zck->work_index_item, digest, True))
         return False;