#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];
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;
// 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;
// that there's no trailing garbage.
assert(strm->avail_in == 0);
//assert(action == LZMA_FINISH);
- return;
+ return bytes;
}
const char *msg;
break;
}
- fprintf(stderr, "xz: %s\n", msg);
- exit(EXIT_FAILURE);
+ fprintf(stderr, "zpmuncompress: %s\n", msg);
+ return -1;
}
}
}
);
}
-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 */
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);
#include <time.h>
#include <limits.h>
#include <stdarg.h>
+#include <sys/types.h>
#include <sqlite3.h>
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);
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);