From: Nick Bowler Date: Fri, 5 Feb 2010 01:39:29 +0000 (-0500) Subject: liblbx: Move extraction outside of the library. X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/commitdiff_plain/98d285b2abc68892fcf3f0a48a37cfccabd5038c liblbx: Move extraction outside of the library. We now use the much more general approach of supporting basic I/O operations on files in the archives. --- diff --git a/src/lbx.c b/src/lbx.c index ff1914e..44508ab 100644 --- a/src/lbx.c +++ b/src/lbx.c @@ -45,10 +45,18 @@ struct lbx_state { int (*dtor)(void *handle); void *f; + struct lbx_file_state *last_file; + unsigned short nfiles; unsigned long offsets[]; }; +struct lbx_file_state { + unsigned long base, limit, offset; + struct lbx_state *lbx; + int eof; +}; + static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE]) { unsigned short nfiles = unpack_16_le(hdr+0); @@ -137,64 +145,113 @@ int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf) return 0; } -static size_t -_lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of) +int lbx_close(struct lbx_state *lbx) { - unsigned char buf[1024]; - size_t rc, written = 0; + int rc = 0; - assert(lbx->f); + if (lbx && lbx->dtor) + rc = lbx->dtor(lbx->f); + free(lbx); + + return rc; +} - if (lbx->fops->seek(lbx->f, base, SEEK_SET) != 0) { +struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno) +{ + struct lbx_file_state *state; + + if (fileno >= lbx->nfiles) { + lbx_errno = LBX_ERANGE; + return NULL; + } + + lbx->last_file = NULL; + if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) { lbx_errno = -errno; - return 0; + return NULL; } - - while (len) { - size_t amt = MIN(len, sizeof buf); - - rc = lbx->fops->read(buf, amt, lbx->f); - len -= rc; - if (rc < amt) { - if (lbx->fops->eof(lbx->f)) lbx_errno = LBX_EEOF; - else lbx_errno = -errno; - break; - } - rc = fwrite(buf, 1, amt, of); - written += rc; - if (rc < amt) { - lbx_errno = -errno; - break; - } + state = malloc(sizeof *state); + if (!state) { + lbx_errno = -errno; + return NULL; } - return written; + *state = (struct lbx_file_state) { + .base = lbx->offsets[fileno], + .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno], + .lbx = lbx, + }; + + lbx->last_file = state; + return state; } -size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of) +size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n) { - size_t base, len; + const struct lbx_file_ops *fops = f->lbx->fops; + size_t want = MIN(n, f->limit - f->offset); + size_t rc; + + if (f != f->lbx->last_file) { + f->lbx->last_file = NULL; + if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0) + return 0; + f->lbx->last_file = f; + } - if (index >= lbx->nfiles) { - lbx_errno = LBX_ERANGE; - return 0; + rc = fops->read(buf, want, f->lbx->f); + f->offset += rc; + + if (want < n || (rc < want && fops->eof(f->lbx->f))) + f->eof = 1; + return rc; +} + +int lbx_file_seek(struct lbx_file_state *f, long offset, int whence) +{ + const struct lbx_file_ops *fops = f->lbx->fops; + unsigned long pos; + + switch (whence) { + case SEEK_CUR: + pos = f->offset + offset; + break; + case SEEK_SET: + pos = offset; + break; + case SEEK_END: + pos = f->limit + offset; + break; } - - base = lbx->offsets[index]; - len = lbx->offsets[index+1] - lbx->offsets[index]; - return _lbx_fextract(lbx, base, len, of); + + if (pos > f->limit) + return -1; + + f->lbx->last_file = NULL; + if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0) + return -1; + f->lbx->last_file = f; + f->eof = 0; + + return 0; } -int lbx_close(struct lbx_state *lbx) +long lbx_file_tell(struct lbx_file_state *f) { - int rc = 0; + return f->offset; +} - if (lbx && lbx->dtor) - rc = lbx->dtor(lbx->f); - free(lbx); +int lbx_file_eof(struct lbx_file_state *f) +{ + return f->eof; +} - return rc; +void lbx_file_close(struct lbx_file_state *f) +{ + if (f->lbx->last_file == f) + f->lbx->last_file = NULL; + free(f); } const char *lbx_strerror(void) diff --git a/src/lbx.h b/src/lbx.h index a788318..329bf09 100644 --- a/src/lbx.h +++ b/src/lbx.h @@ -33,6 +33,7 @@ extern const struct lbx_file_ops lbx_pipe_fops; /* Opaque */ typedef struct lbx_state LBX; +typedef struct lbx_file_state LBXfile; struct lbx_statbuf { const char *name; @@ -48,8 +49,14 @@ int lbx_close(LBX *); size_t lbx_numfiles(LBX *); /* File operations */ -int lbx_stat(LBX *, size_t, struct lbx_statbuf *); -size_t lbx_extract(LBX *, size_t, FILE *); +int lbx_stat(LBX *, size_t, struct lbx_statbuf *); + +LBXfile *lbx_file_open(LBX *lbx, unsigned fileno); +size_t lbx_file_read(LBXfile *f, void *buf, size_t n); +int lbx_file_seek(LBXfile *f, long offset, int whence); +long lbx_file_tell(LBXfile *f); +int lbx_file_eof(LBXfile *f); +void lbx_file_close(LBXfile *f); /* Misc operations */ const char *lbx_strerror(void); diff --git a/src/lbxtool.c b/src/lbxtool.c index ec0f1b8..9f0097a 100644 --- a/src/lbxtool.c +++ b/src/lbxtool.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -99,6 +100,49 @@ int list(LBX *lbx, const char *name, int verbose, char **argv) { return EXIT_SUCCESS; } +int extract_file(LBXfile *f, const struct lbx_statbuf *stat) +{ + int ret = -1; + size_t rc; + FILE *of; + + of = fopen(stat->name, "wb"); + if (!of) { + errmsg("%s: fopen: %s\n", + stat->name, strerror(errno)); + return -1; + } + + while (1) { + unsigned char buf[1024]; + + rc = lbx_file_read(f, buf, sizeof buf); + if (rc == 0) { + if (lbx_file_eof(f)) + ret = 0; + break; + } + + if (fwrite(buf, rc, 1, of) != 1) { + errmsg("%s: fwrite: %s\n", stat->name, strerror(errno)); + break; + } + + if (rc < sizeof buf) { + if (lbx_file_eof(f)) + ret = 0; + break; + } + } + + if (fclose(of) == EOF) { + errmsg("%s: fclose: %s\n", stat->name, strerror(errno)); + return -1; + } + + return ret; +} + int extract(LBX *lbx, const char *name, int verbose, char **argv) { size_t nfiles; unsigned int i; @@ -110,9 +154,7 @@ int extract(LBX *lbx, const char *name, int verbose, char **argv) { for (i = 0; i < nfiles; i++) { struct lbx_statbuf stat; - size_t rc; - FILE *of; - int j; + LBXfile *file; lbx_stat(lbx, i, &stat); @@ -122,22 +164,16 @@ int extract(LBX *lbx, const char *name, int verbose, char **argv) { default: return EXIT_FAILURE; } - of = fopen(stat.name, "wbx"); - if (!of) { - errmsg("failed to create output file %s: %m.\n", - stat.name); - return EXIT_FAILURE; + file = lbx_file_open(lbx, i); + if (!file) { + errmsg("failed to open archive member %s: %s.\n", + stat.name, lbx_strerror()); + continue; } - if (verbose) printf("extracting %s...\n", stat.name); - rc = lbx_extract(lbx, i, of); - if (rc < stat.size) { - if (rc == 0) remove(stat.name); - errmsg("error extracting %s: %s.\n", - stat.name, lbx_strerror()); + if (extract_file(file, &stat) == -1) return EXIT_FAILURE; - } - if (verbose) printf("wrote %zu bytes.\n", rc); + lbx_file_close(file); } return EXIT_SUCCESS;