API and error handling cleanups --HG-- branch : HEAD
diff --git a/lib/zip_close.c b/lib/zip_close.c index b202288..12fd6f3 100644 --- a/lib/zip_close.c +++ b/lib/zip_close.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_close.c,v 1.34 2003/10/03 07:58:38 dillo Exp $ + $NiH: zip_close.c,v 1.35 2003/10/04 07:39:52 dillo Exp $ zip_close.c -- close zip archive and update changes Copyright (C) 1999 Dieter Baron and Thomas Klausner @@ -76,8 +76,10 @@ struct zip *tzf; mode_t mask; - if (zf->changes == 0) - return _zip_free(zf); + if (zf->changes == 0) { + _zip_free(zf); + return 0; + } /* look if there really are any changes */ count = survivors = 0; @@ -91,23 +93,24 @@ } /* no changes, all has been unchanged */ - if (count == 0) - return _zip_free(zf); + if (count == 0) { + _zip_free(zf); + return 0; + } /* don't create zip files with no entries */ if (survivors == 0) { ret = 0; if (zf->zn) ret = remove(zf->zn); - if (ret == 0) - return _zip_free(zf); _zip_free(zf); + /* XXX: inconsistent: zf freed, returned -1 */ return ret; } temp = (char *)malloc(strlen(zf->zn)+8); if (!temp) { - zip_err = ZERR_MEMORY; + _zip_error_set(&zf->error, ZERR_MEMORY, 0); return -1; } @@ -115,12 +118,13 @@ tfd = mkstemp(temp); if (tfd == -1) { - zip_err = ZERR_TMPOPEN; + _zip_error_set(&zf->error, ZERR_TMPOPEN, errno); + free(temp); return -1; } if ((tfp=fdopen(tfd, "r+b")) == NULL) { - zip_err = ZERR_TMPOPEN; + _zip_error_set(&zf->error, ZERR_TMPOPEN, errno); remove(temp); free(temp); close(tfd); @@ -131,23 +135,13 @@ tzf->zp = tfp; tzf->zn = temp; tzf->nentry = 0; + tzf->entry = NULL; + tzf->nentry_alloc = 0; tzf->comlen = zf->comlen; tzf->cd_size = tzf->cd_offset = 0; tzf->com = (unsigned char *)_zip_memdup(zf->com, zf->comlen); /* XXX: bail out if tzf->com == NULL; */ - /* tzf->entry = (struct zip_entry *)malloc(sizeof(struct - zip_entry)*ALLOC_SIZE); - if (!tzf->entry) { - zip_err = ZERR_MEMORY; - return -1; - } - - tzf->nentry_alloc = ALLOC_SIZE; - */ - tzf->entry = NULL; - tzf->nentry_alloc = 0; - count = 0; if (zf->entry) { for (i=0; i<zf->nentry; i++) { @@ -188,24 +182,22 @@ if (fclose(tzf->zp)==0) { tzf->zp = NULL; + if (rename(tzf->zn, zf->zn) != 0) { + _zip_error_set(&zf->error, ZERR_RENAME, errno); + remove(tzf->zn); + _zip_free(tzf); + return -1; + } if (zf->zp) { fclose(zf->zp); zf->zp = NULL; } - if (rename(tzf->zn, zf->zn) != 0) { - zip_err = ZERR_RENAME; - remove(tzf->zn); - _zip_free(tzf); - zf->zp = fopen(zf->zn, "rb"); - /* XXX: can't handle open error usefully */ - return -1; - } mask = umask(0); umask(mask); chmod(zf->zn, 0666&~mask); } + /* XXX: handle fclose(tzf->fn) error */ - /* no errors possible, since files already closed */ _zip_free(zf); _zip_free(tzf); @@ -227,14 +219,14 @@ return -1; if (_zip_writecdentry(dest->zp, ze, 1) != 0) { - zip_err = ZERR_WRITE; + _zip_error_set(&src->error, ZERR_WRITE, errno); return -1; } if (fseek(src->zp, src->entry[entry_no].meta->local_offset + LENTRYSIZE + src->entry[entry_no].meta->lef_len + src->entry[entry_no].file_fnlen, SEEK_SET) != 0) { - zip_err = ZERR_SEEK; + _zip_error_set(&src->error, ZERR_SEEK, errno); return -1; } @@ -245,11 +237,11 @@ if (len > remainder) len = remainder; if (fread(buf, 1, len, src->zp)!=len) { - zip_err = ZERR_READ; + _zip_error_set(&src->error, ZERR_READ, errno); return -1; } if (fwrite(buf, 1, len, dest->zp)!=len) { - zip_err = ZERR_WRITE; + _zip_error_set(&src->error, ZERR_WRITE, errno); return -1; } remainder -= len;
diff --git a/lib/zip_delete.c b/lib/zip_delete.c index 16a663a..c430788 100644 --- a/lib/zip_delete.c +++ b/lib/zip_delete.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_delete.c,v 1.9 2003/10/02 14:13:29 dillo Exp $ + $NiH: zip_delete.c,v 1.10 2003/10/03 08:34:11 dillo Exp $ zip_delete.c -- delete file from zip archive Copyright (C) 1999 Dieter Baron and Thomas Klausner @@ -44,7 +44,7 @@ zip_delete(struct zip *zf, int idx) { if (idx < 0 || idx >= zf->nentry) { - zip_err = ZERR_INVAL; + _zip_error_set(&zf->error, ZERR_INVAL, 0); return -1; }
diff --git a/lib/zip_error.c b/lib/zip_error.c index 2e28a8c..3d5c3e1 100644 --- a/lib/zip_error.c +++ b/lib/zip_error.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_error.c,v 1.1 2003/10/05 16:05:22 dillo Exp $ + $NiH: zip_error.c,v 1.2 2003/10/06 02:50:05 dillo Exp $ zip_error.c -- struct zip_error helper functions Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner @@ -43,6 +43,15 @@ void +_zip_error_copy(struct zip_error *dst, struct zip_error *src) +{ + dst->zip_err = src->zip_err; + dst->sys_err = src->sys_err; +} + + + +void _zip_error_fini(struct zip_error *err) { free(err->str);
diff --git a/lib/zip_fopen.c b/lib/zip_fopen.c index c46d401..3c31bce 100644 --- a/lib/zip_fopen.c +++ b/lib/zip_fopen.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_fopen.c,v 1.6 2003/10/02 14:13:29 dillo Exp $ + $NiH: zip_fopen.c,v 1.7 2003/10/03 08:34:11 dillo Exp $ zip_fopen.c -- open file in zip archvie for reading Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner @@ -41,14 +41,12 @@ struct zip_file * -zip_fopen(struct zip *zf, const char *fname, int case_sens) +zip_fopen(struct zip *za, const char *fname, int flags) { int idx; - if ((idx=zip_name_locate(zf, fname, case_sens)) < 0) { - zip_err = ZERR_NOENT; + if ((idx=zip_name_locate(za, fname, flags)) < 0) return NULL; - } - return zip_fopen_index(zf, idx); + return zip_fopen_index(za, idx); }
diff --git a/lib/zip_fopen_index.c b/lib/zip_fopen_index.c index b9192c7..51d663a 100644 --- a/lib/zip_fopen_index.c +++ b/lib/zip_fopen_index.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_fopen_index.c,v 1.12 2003/10/02 14:13:29 dillo Exp $ + $NiH: zip_fopen_index.c,v 1.13 2003/10/03 11:20:16 dillo Exp $ zip_fopen_index.c -- open file in zip archive for reading by index Copyright (C) 1999 Dieter Baron and Thomas Klausner @@ -49,25 +49,25 @@ zip_fopen_index(struct zip *zf, int fileno) { unsigned char buf[4]; - int len; + int len, ret; struct zip_file *zff; if ((fileno < 0) || (fileno >= zf->nentry)) { - zip_err = ZERR_INVAL; + _zip_error_set(&zf->error, ZERR_INVAL, 0); return NULL; } #if 0 if (zf->entry[fileno].state != ZIP_ST_UNCHANGED && zf->entry[fileno].state != ZIP_ST_RENAMED) { - zip_err = ZERR_CHANGED; + _zip_error_set(&zf->error, ZERR_CHANGED, 0); return NULL; } #endif if ((zf->entry[fileno].meta->comp_method != 0) && (zf->entry[fileno].meta->comp_method != 8)) { - zip_err = ZERR_COMPNOTSUPP; + _zip_error_set(&zf->error, ZERR_COMPNOTSUPP, 0); return NULL; } @@ -82,13 +82,13 @@ /* go to start of actual data */ if (fseek(zf->zp, zf->entry[fileno].meta->local_offset+LENTRYSIZE-4, SEEK_SET) < 0) { - zip_err = ZERR_SEEK; + _zip_error_set(&zf->error, ZERR_SEEK, errno); zip_fclose(zff); return NULL; } len = fread(buf, 1, 4, zf->zp); if (len != 4) { - zip_err = ZERR_READ; + _zip_error_set(&zf->error, ZERR_READ, errno); zip_fclose(zff); return NULL; } @@ -101,19 +101,20 @@ return zff; if ((zff->buffer=(char *)malloc(BUFSIZE)) == NULL) { - zip_err = ZERR_MEMORY; + _zip_error_set(&zf->error, ZERR_MEMORY, 0); zip_fclose(zff); return NULL; } len = _zip_file_fillbuf (zff->buffer, BUFSIZE, zff); if (len <= 0) { + _zip_error_copy(&zf->error, &zff->error); zip_fclose(zff); return NULL; } if ((zff->zstr = (z_stream *)malloc(sizeof(z_stream))) == NULL) { - zip_err = ZERR_MEMORY; + _zip_error_set(&zf->error, ZERR_MEMORY, 0); zip_fclose(zff); return NULL; } @@ -124,8 +125,8 @@ zff->zstr->avail_in = len; /* negative value to tell zlib that there is no header */ - if (inflateInit2(zff->zstr, -MAX_WBITS) != Z_OK) { - zip_err = ZERR_ZLIB; + if ((ret=inflateInit2(zff->zstr, -MAX_WBITS)) != Z_OK) { + _zip_error_set(&zf->error, ZERR_ZLIB, ret); zip_fclose(zff); return NULL; } @@ -174,23 +175,27 @@ static struct zip_file * _zip_file_new(struct zip *zf) { - struct zip_file *zff; - - if (zf->nfile >= zf->nfile_alloc-1) { - zf->nfile_alloc += 10; - zf->file = (struct zip_file **)realloc(zf->file, zf->nfile_alloc - *sizeof(struct zip_file *)); - if (zf->file == NULL) { - zip_err = ZERR_MEMORY; - return NULL; - } - } + struct zip_file *zff, file; + int n; if ((zff=(struct zip_file *)malloc(sizeof(struct zip_file))) == NULL) { - zip_err = ZERR_MEMORY; + _zip_error_set(&zf->error, ZERR_MEMORY, 0); return NULL; } + if (zf->nfile >= zf->nfile_alloc-1) { + n = zf->nfile_alloc + 10; + file = (struct zip_file **)realloc(zf->file, + n*sizeof(struct zip_file *)); + if (file == NULL) { + _zip_error_set(&zf->error, ZERR_MEMORY, 0); + free(zff); + return NULL; + } + zf->nfile_alloc = n; + zf->file = file; + } + zf->file[zf->nfile++] = zff; zff->zf = zf;
diff --git a/lib/zip_free.c b/lib/zip_free.c index 86eff21..e82a539 100644 --- a/lib/zip_free.c +++ b/lib/zip_free.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_free.c,v 1.5 2003/03/16 10:21:39 wiz Exp $ + $NiH: zip_free.c,v 1.6 2003/10/02 14:13:30 dillo Exp $ zip_free.c -- free struct zip Copyright (C) 1999 Dieter Baron and Thomas Klausner @@ -43,15 +43,12 @@ /* _zip_free: frees the space allocated to a zipfile struct, and closes the - corresponding file. Returns 0 if successful, the error returned - by fclose if not. */ + corresponding file. */ -int +void _zip_free(struct zip *zf) { - int i, ret; - - ret = 0; + int i; if (zf == NULL) return 0; @@ -60,7 +57,7 @@ free(zf->zn); if (zf->zp) - ret = fclose(zf->zp); + fclose(zf->zp); if (zf->com) free(zf->com); @@ -82,8 +79,5 @@ free(zf); - if (ret) - zip_err = ZERR_CLOSE; - - return ret; + return; }
diff --git a/lib/zip_get_name.c b/lib/zip_get_name.c index 1202f47..d9a88d7 100644 --- a/lib/zip_get_name.c +++ b/lib/zip_get_name.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_get_name.c,v 1.4 2003/10/01 09:51:00 dillo Exp $ + $NiH: zip_get_name.c,v 1.5 2003/10/02 14:13:30 dillo Exp $ zip_get_name.c -- get filename for a file in zip file Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner @@ -44,7 +44,7 @@ zip_get_name(struct zip *zf, int idx) { if (idx < 0 || idx >= zf->nentry) { - zip_err = ZERR_INVAL; + _zip_error_set(&zf->error, ZERR_INVAL, 0); return NULL; }
diff --git a/lib/zip_open.c b/lib/zip_open.c index 3323e11..cf336c3 100644 --- a/lib/zip_open.c +++ b/lib/zip_open.c
@@ -1,7 +1,7 @@ /* - $NiH: zip_open.c,v 1.16 2003/10/02 14:13:31 dillo Exp $ + $NiH: zip_open.c,v 1.17 2003/10/06 02:50:06 dillo Exp $ - zip_open.c -- open zip file + zip_open.c -- open zip archive Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives.
diff --git a/lib/zip_rename.c b/lib/zip_rename.c index b94943e..e9c1f9b 100644 --- a/lib/zip_rename.c +++ b/lib/zip_rename.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_rename.c,v 1.8 2003/10/02 14:13:31 dillo Exp $ + $NiH: zip_rename.c,v 1.9 2003/10/03 23:25:31 dillo Exp $ zip_rename.c -- rename file in zip archive Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner @@ -44,7 +44,7 @@ zip_rename(struct zip *zf, int idx, const char *name) { if (idx >= zf->nentry || idx < 0) { - zip_err = ZERR_INVAL; + _zip_error_set(&zf->error, ZERR_INVAL, 0); return -1; }
diff --git a/lib/zip_set_name.c b/lib/zip_set_name.c index 78fd109..6647b01 100644 --- a/lib/zip_set_name.c +++ b/lib/zip_set_name.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_set_name.c,v 1.8 2003/10/01 09:51:01 dillo Exp $ + $NiH: zip_set_name.c,v 1.9 2003/10/02 14:13:32 dillo Exp $ zip_set_name.c -- rename helper function Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner @@ -45,23 +45,24 @@ int _zip_set_name(struct zip *zf, int idx, const char *name) { + char *s; + if (idx < 0 || idx >= zf->nentry) { - zip_err = ZERR_INVAL; + _zip_error_set(&zf->error, ZERR_INVAL, 0); return -1; } if (name != NULL) { + if ((s=strdup(name)) == NULL) { + _zip_error_set(&zf->error, ZERR_MEMORY, 0); + return -1; + } + if (zf->entry[idx].fn_old == NULL) zf->entry[idx].fn_old = zf->entry[idx].fn; else free(zf->entry[idx].fn); - zf->entry[idx].fn = strdup(name); - if (zf->entry[idx].fn == NULL) { - zf->entry[idx].fn = zf->entry[idx].fn_old; - zf->entry[idx].fn_old = NULL; - zip_err = ZERR_MEMORY; - return -1; - } + zf->entry[idx].fn = s; } return 0;
diff --git a/lib/zip_unchange.c b/lib/zip_unchange.c index db42e24..8b8ad25 100644 --- a/lib/zip_unchange.c +++ b/lib/zip_unchange.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_unchange.c,v 1.9 2003/10/02 14:13:32 dillo Exp $ + $NiH: zip_unchange.c,v 1.10 2003/10/03 08:34:11 dillo Exp $ zip_unchange.c -- undo changes to file in zip archive Copyright (C) 1999 Dieter Baron and Thomas Klausner @@ -47,7 +47,7 @@ int ret; if (!zf || idx < 0 || idx >= zf->nentry) { - zip_err = ZERR_INVAL; + _zip_error_set(&zf->error, ZERR_INVAL, 0); return -1; }
diff --git a/lib/zip_unchange_all.c b/lib/zip_unchange_all.c index 157b71a..fba406d 100644 --- a/lib/zip_unchange_all.c +++ b/lib/zip_unchange_all.c
@@ -1,5 +1,5 @@ /* - $NiH: zip_unchange_all.c,v 1.4 2003/10/02 14:13:32 dillo Exp $ + $NiH: zip_unchange_all.c,v 1.5 2003/10/03 08:34:11 dillo Exp $ zip_unchange.c -- undo changes to all files in zip archive Copyright (C) 1999 Dieter Baron and Thomas Klausner @@ -42,18 +42,13 @@ int -zip_unchange_all(struct zip *zf) +zip_unchange_all(struct zip *za) { int ret, i; - - if (!zf) { - zip_err = ZERR_INVAL; - return -1; - } ret = 0; - for (i=0; i<zf->nentry; i++) - ret |= zip_unchange(zf, i); + for (i=0; i<za->nentry; i++) + ret |= zip_unchange(za, i); return ret; }
diff --git a/lib/zipint.h b/lib/zipint.h index 69fb693..fc1ccfe 100644 --- a/lib/zipint.h +++ b/lib/zipint.h
@@ -2,7 +2,7 @@ #define _HAD_ZIPINT_H /* - $NiH: zipint.h,v 1.19 2003/10/05 16:05:22 dillo Exp $ + $NiH: zipint.h,v 1.20 2003/10/06 02:50:07 dillo Exp $ zipint.h -- internal declarations. Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner @@ -115,13 +115,14 @@ void _zip_entry_init(struct zip *, int); -const char *_zip_error_strerror(struct zip_error *); +void _zip_error_copy(struct zip_error *, struct zip_error *); void _zip_error_fini(struct zip_error *); void _zip_error_get(struct zip_error *, int *, int *); void _zip_error_init(struct zip_error *); void _zip_error_set(struct zip_error *, int, int); +const char *_zip_error_strerror(struct zip_error *); int _zip_file_fillbuf(char *, int, struct zip_file *); -int _zip_free(struct zip *); +void _zip_free(struct zip *); int _zip_free_entry(struct zip_entry *); int _zip_local_header_read(struct zip *, int); void *_zip_memdup(const void *, int);