From: Nathan Wagner Date: Thu, 25 Oct 2018 16:30:33 +0000 (+0000) Subject: change uncompresslzma to take a file descriptor X-Git-Tag: v0.2.16~58 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=343423780e984fd9188b55b688190decda19f94c change uncompresslzma to take a file descriptor --- diff --git a/lib/uncompress.c b/lib/uncompress.c index 9053900..cb8fed2 100644 --- a/lib/uncompress.c +++ b/lib/uncompress.c @@ -12,9 +12,10 @@ #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; } } } diff --git a/lib/zpm.c b/lib/zpm.c index 9bd9732..644dc97 100644 --- 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 c714464..05b8fe9 100644 --- a/zpm.h +++ b/zpm.h @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -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);