]> pd.if.org Git - zpackage/commitdiff
change uncompresslzma to take a file descriptor
authorNathan Wagner <nw@hydaspes.if.org>
Thu, 25 Oct 2018 16:30:33 +0000 (16:30 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 3 Nov 2018 12:39:52 +0000 (12:39 +0000)
lib/uncompress.c
lib/zpm.c
zpm.h

index 90539007a9d9d399e5a18a3482372346aae02f09..cb8fed2b49b4d7a3d28e025f2e2a0f1b48bf1957 100644 (file)
 
 #include "lzma.h"
 
-void uncompresslzma(void *buf, size_t bufsize, FILE *out) {
+ssize_t uncompresslzma(void *buf, size_t bufsize, int out) {
        lzma_stream s = LZMA_STREAM_INIT;
        lzma_stream *strm;
+       ssize_t bytes = 0;
        
        uint8_t outbuf[BUFSIZ];
 
@@ -27,7 +28,7 @@ void uncompresslzma(void *buf, size_t bufsize, FILE *out) {
        if (ret != LZMA_OK) {
                fprintf(stderr, "%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
                                : "Internal error (bug)");
-               exit(EXIT_FAILURE);
+               return -1;
        }
 
        strm->avail_in = bufsize;
@@ -44,15 +45,27 @@ void uncompresslzma(void *buf, size_t bufsize, FILE *out) {
                // This way as much data as possible gets written to output
                // even if decoder detected an error.
                if (strm->avail_out == 0 || ret != LZMA_OK) {
-                       const size_t write_size = BUFSIZ - strm->avail_out;
-
-                       if (fwrite(outbuf, 1, write_size, out) != write_size) {
-                               // Wouldn't be a surprise if writing to stderr
-                               // would fail too but at least try to show an
-                               // error message.
-                               fprintf(stderr, "Cannot write to output file stream: "
-                                               "%s", strerror(errno));
-                               exit(EXIT_FAILURE);
+                       size_t avail = BUFSIZ - strm->avail_out;
+                       ssize_t written = 0;
+                       uint8_t *start;
+
+                       start = outbuf;
+
+                       while (avail > 0) {
+                               written = write(out, outbuf, avail);
+                               if (written == -1) {
+                                       /* Wouldn't be a surprise if writing to
+                                        * stderr would fail too but at least
+                                        * try to show an error message.
+                                        */
+                                       fprintf(stderr, "Cannot write to output"
+                                                       " file stream: %s",
+                                                       strerror(errno));
+                                       return -1;
+                               }
+                               avail -= written;
+                               start += written;
+                               bytes += written;
                        }
 
                        strm->next_out = outbuf;
@@ -65,7 +78,7 @@ void uncompresslzma(void *buf, size_t bufsize, FILE *out) {
                                // that there's no trailing garbage.
                                assert(strm->avail_in == 0);
                                //assert(action == LZMA_FINISH);
-                               return;
+                               return bytes;
                        }
 
                        const char *msg;
@@ -96,8 +109,8 @@ void uncompresslzma(void *buf, size_t bufsize, FILE *out) {
                                        break;
                        }
 
-                       fprintf(stderr, "xz: %s\n", msg);
-                       exit(EXIT_FAILURE);
+                       fprintf(stderr, "zpmuncompress: %s\n", msg);
+                       return -1;
                }
        }
 }
index 9bd973246b9f0f01e2594e211e9d8bc1af9e90d8..644dc9729e05ad28682e43e984101d426dd4af2f 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -447,14 +447,14 @@ int zpm_addvercmp(struct zpm *pkg) {
                        );
 }
 
-int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
+int zpm_extract(struct zpm *pkg, char *hash, char *path, mode_t mode) {
        int rc;
 
        int blobsize;
        //int64_t size;
        void *xzdata;
        int type;
-       FILE *out;
+       int out;
        sqlite3_stmt *ifile;
 
        /* TODO check null */
@@ -507,22 +507,21 @@ int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
        blobsize = sqlite3_column_bytes(ifile, 1);
 
        if (strcmp(path, "-")) {
-               out = fopen(path, "w");
+               out = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0600);
        } else {
-               out = stdout;
+               out = 1;
        }
-       if (!out) {
+       if (out == -1) {
                fprintf(stderr, "can't open output file %s: %s\n", path,
                                strerror(errno));
                sqlite3_finalize(ifile);
                sqlite3_close(db);
                return 0;
        }
-       //fwrite(xzdata, blobsize, 1, stdout);
 
        //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
        uncompresslzma(xzdata, blobsize, out);
-       fclose(out);
+       close(out);
        chmod(path, mode);
 
        sqlite3_finalize(ifile);
diff --git a/zpm.h b/zpm.h
index c7144649940bfd6bbf8008732a480d4f646a85ef..05b8fe9bb7de58140bcf5912f2a9279ec3ab88ba 100644 (file)
--- a/zpm.h
+++ b/zpm.h
@@ -6,6 +6,7 @@
 #include <time.h>
 #include <limits.h>
 #include <stdarg.h>
+#include <sys/types.h>
 
 #include <sqlite3.h>
 
@@ -114,7 +115,7 @@ int zpm_tag(struct zpm *zp, char *path, char *tags);
 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
 
 /* export hash to dest */
-int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
+int zpm_extract(struct zpm *pkg, char *hash, char *path, mode_t mode);
 
 /* export path to dest */
 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
@@ -156,10 +157,8 @@ int zpm_checkinstall(struct zpm *local);
 
 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
 
-#if 1
-void uncompresslzma(void *buf, size_t bufsize, FILE *out);
+ssize_t uncompresslzma(void *buf, size_t bufsize, int outfd);
 void *compresslzma(void *buf, size_t bufsize, size_t *len);
-#endif
 
 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
 int zpm_hash(char *path, char *hash, uint32_t flags);