Blob Blame History Raw
From 35ae4edd9a6abef11fbdbfef5e717ba6eee6f8ee Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Wed, 28 Sep 2016 12:09:07 +0000
Subject: [PATCH] Close BZ2 compressed files on cr_close

Per bzip2 documentation: "BZ2_bzReadClose does not call fclose on the underlying file
handle, so you should do that yourself if appropriate.".
This patch adds a INNERFILE element to CR_FILE to keep track of the FILE object so we
can properly close the file on cr_close.

Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
---
 src/compression_wrapper.c | 6 ++++++
 src/compression_wrapper.h | 1 +
 2 files changed, 7 insertions(+)

diff --git a/src/compression_wrapper.c b/src/compression_wrapper.c
index aacaf90..adc2f39 100644
--- a/src/compression_wrapper.c
+++ b/src/compression_wrapper.c
@@ -347,6 +347,7 @@ cr_sopen(const char *filename,
     file = g_malloc0(sizeof(CR_FILE));
     file->mode = mode;
     file->type = type;
+    file->INNERFILE = NULL;
 
     switch (type) {
 
@@ -380,6 +381,7 @@ cr_sopen(const char *filename,
 
         case (CR_CW_BZ2_COMPRESSION): { // ------------------------------------
             FILE *f = fopen(filename, mode_str);
+            file->INNERFILE = f;
             int bzerror;
 
             if (!f) {
@@ -405,6 +407,8 @@ cr_sopen(const char *filename,
             if (bzerror != BZ_OK) {
                 const char *err_msg;
 
+                fclose(f);
+
                 switch (bzerror) {
                     case BZ_CONFIG_ERROR:
                         err_msg = "library has been mis-compiled";
@@ -642,6 +646,8 @@ cr_close(CR_FILE *cr_file, GError **err)
                 BZ2_bzWriteClose(&rc, (BZFILE *) cr_file->FILE,
                                  BZ2_SKIP_FFLUSH, NULL, NULL);
 
+            fclose(cr_file->INNERFILE);
+
             if (rc == BZ_OK) {
                 ret = CRE_OK;
             } else {
diff --git a/src/compression_wrapper.h b/src/compression_wrapper.h
index 910fd45..65022d9 100644
--- a/src/compression_wrapper.h
+++ b/src/compression_wrapper.h
@@ -79,6 +79,7 @@ void cr_contentstat_free(cr_ContentStat *cstat, GError **err);
 typedef struct {
     cr_CompressionType  type;           /*!< Type of compression */
     void                *FILE;          /*!< Pointer to gzFile, BZFILE, ... */
+    void                *INNERFILE;     /*!< Pointer to underlying FILE */
     cr_OpenMode         mode;           /*!< Mode */
     cr_ContentStat      *stat;          /*!< Content stats */
     cr_ChecksumCtx      *checksum_ctx;  /*!< Checksum contenxt */