]> git.ipfire.org Git - pakfire.git/commitdiff
compress: Fix flushing Zstandard output data
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 11:30:21 +0000 (11:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 11:30:21 +0000 (11:30 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/compress.c

index 31151631d40948e8707e542b0415ab7dcf7bae01..afabe206396590956a537910052eadb6efad16f6 100644 (file)
@@ -473,6 +473,7 @@ static ssize_t zstd_write(void* data, const char* buffer, size_t size) {
 
 static int zstd_flush(void* data) {
        struct zstd_cookie* cookie = data;
+       ssize_t bytes_left;
        int r;
 
        // Fail if were given no cookie
@@ -492,18 +493,20 @@ static int zstd_flush(void* data) {
                        cookie->out.size = sizeof(cookie->buffer);
                        cookie->out.pos = 0;
 
-                       r = ZSTD_endStream(cookie->cstream, &cookie->out);
-                       if (ZSTD_isError(r))
+                       bytes_left = ZSTD_endStream(cookie->cstream, &cookie->out);
+                       if (ZSTD_isError(bytes_left))
                                return -1;
 
+                       // Otherwise we write the buffer to the file
+                       if (cookie->out.pos) {
+                               r = fwrite(cookie->buffer, 1, cookie->out.pos, cookie->f);
+                               if (r < (ssize_t)cookie->out.pos)
+                                       return -1;
+                       }
+
                        // If the buffer is empty we are done
-                       if (r == 0)
+                       if (bytes_left == 0)
                                break;
-
-                       // Otherwise we write the buffer to the file
-                       r = fwrite(cookie->buffer, 1, cookie->out.pos, cookie->f);
-                       if (r < (ssize_t)cookie->out.pos)
-                               return -1;
                }
        }