ZIP_EXTERN int zip_close(zip_t *za) { zip_uint64_t i, j, survivors; zip_int64_t off; int error; zip_filelist_t *filelist; int changed; if (za == NULL) return -1; changed = _zip_changed(za, &survivors); /* don't create zip files with no entries */ if (survivors == 0) { if ((za->open_flags & ZIP_TRUNCATE) || changed) { if (zip_source_remove(za->src) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } } zip_discard(za); return 0; } if (!changed) { zip_discard(za); return 0; } if (survivors > za->nentry) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if ((filelist=(zip_filelist_t *)malloc(sizeof(filelist[0])*(size_t)survivors)) == NULL) return -1; /* create list of files with index into original archive */ for (i=j=0; i<za->nentry; i++) { if (za->entry[i].deleted) continue; if (j >= survivors) { free(filelist); zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } filelist[j].idx = i; j++; } if (j < survivors) { free(filelist); zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if (zip_source_begin_write(za->src) < 0) { _zip_error_set_from_source(&za->error, za->src); free(filelist); return -1; } error = 0; for (j=0; j<survivors; j++) { int new_data; zip_entry_t *entry; zip_dirent_t *de; i = filelist[j].idx; entry = za->entry+i; new_data = (ZIP_ENTRY_DATA_CHANGED(entry) || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_COMP_METHOD)); /* create new local directory entry */ if (entry->changes == NULL) { if ((entry->changes=_zip_dirent_clone(entry->orig)) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); error = 1; break; } } de = entry->changes; if (_zip_read_local_ef(za, i) < 0) { error = 1; break; } if ((off = zip_source_tell_write(za->src)) < 0) { error = 1; break; } de->offset = (zip_uint64_t)off; if (new_data) { zip_source_t *zs; zs = NULL; if (!ZIP_ENTRY_DATA_CHANGED(entry)) { if ((zs=_zip_source_zip_new(za, za, i, ZIP_FL_UNCHANGED, 0, 0, NULL)) == NULL) { error = 1; break; } } /* add_data writes dirent */ if (add_data(za, zs ? zs : entry->source, de) < 0) { error = 1; if (zs) zip_source_free(zs); break; } if (zs) zip_source_free(zs); } else { zip_uint64_t offset; /* when copying data, all sizes are known -> no data descriptor needed */ de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR; if (_zip_dirent_write(za, de, ZIP_FL_LOCAL) < 0) { error = 1; break; } if ((offset=_zip_file_get_offset(za, i, &za->error)) == 0) { error = 1; break; } if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); error = 1; break; } if (copy_data(za, de->comp_size) < 0) { error = 1; break; } } } if (!error) { if (write_cdir(za, filelist, survivors) < 0) error = 1; } free(filelist); if (!error) { if (zip_source_commit_write(za->src) != 0) { _zip_error_set_from_source(&za->error, za->src); error = 1; } } if (error) { zip_source_rollback_write(za->src); return -1; } zip_discard(za); return 0; }
ZIP_EXTERN int zip_close(struct zip *za) { zip_uint64_t i, j, survivors; int error; char *temp; FILE *out; #ifndef _WIN32 mode_t mask; #endif struct zip_filelist *filelist; int reopen_on_error; int new_torrentzip; int changed; reopen_on_error = 0; if (za == NULL) return -1; changed = _zip_changed(za, &survivors); /* don't create zip files with no entries */ if (survivors == 0) { if (za->zn && ((za->open_flags & ZIP_TRUNCATE) || (changed && za->zp))) { if (remove(za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_REMOVE, errno); return -1; } } zip_discard(za); return 0; } if (!changed) { zip_discard(za); return 0; } if (survivors > za->nentry) { _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if ((filelist=(struct zip_filelist *)malloc(sizeof(filelist[0])*(size_t)survivors)) == NULL) return -1; /* archive comment is special for torrentzip */ if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) { /* TODO: use internal function when zip_set_archive_comment clears TORRENT flag */ if (zip_set_archive_comment(za, TORRENT_SIG "XXXXXXXX", TORRENT_SIG_LEN + TORRENT_CRC_LEN) < 0) { free(filelist); return -1; } } /* TODO: if no longer torrentzip and archive comment not changed by user, delete it */ /* create list of files with index into original archive */ for (i=j=0; i<za->nentry; i++) { if (za->entry[i].deleted) continue; if (j >= survivors) { free(filelist); _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } filelist[j].idx = i; filelist[j].name = zip_get_name(za, i, 0); j++; } if (j < survivors) { free(filelist); _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if ((temp=_zip_create_temp_output(za, &out)) == NULL) { free(filelist); return -1; } if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) qsort(filelist, (size_t)survivors, sizeof(filelist[0]), _zip_torrentzip_cmp); new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1 && zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0); error = 0; for (j=0; j<survivors; j++) { int new_data; struct zip_entry *entry; struct zip_dirent *de; i = filelist[j].idx; entry = za->entry+i; new_data = (ZIP_ENTRY_DATA_CHANGED(entry) || new_torrentzip || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_COMP_METHOD)); /* create new local directory entry */ if (entry->changes == NULL) { if ((entry->changes=_zip_dirent_clone(entry->orig)) == NULL) { _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); error = 1; break; } } de = entry->changes; if (_zip_read_local_ef(za, i) < 0) { error = 1; break; } if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) _zip_dirent_torrent_normalize(entry->changes); de->offset = (zip_uint64_t)ftello(out); /* TODO: check for errors */ if (new_data) { struct zip_source *zs; zs = NULL; if (!ZIP_ENTRY_DATA_CHANGED(entry)) { if ((zs=_zip_source_zip_new(za, za, i, ZIP_FL_UNCHANGED, 0, 0, NULL)) == NULL) { error = 1; break; } } /* add_data writes dirent */ if (add_data(za, zs ? zs : entry->source, de, out) < 0) { error = 1; if (zs) zip_source_free(zs); break; } if (zs) zip_source_free(zs); } else { zip_uint64_t offset; /* when copying data, all sizes are known -> no data descriptor needed */ de->bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR; if (_zip_dirent_write(de, out, ZIP_FL_LOCAL, &za->error) < 0) { error = 1; break; } if ((offset=_zip_file_get_offset(za, i, &za->error)) == 0) { error = 1; break; } if ((fseeko(za->zp, (off_t)offset, SEEK_SET) < 0)) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); error = 1; break; } if (copy_data(za->zp, de->comp_size, out, &za->error) < 0) { error = 1; break; } } } if (!error) { if (write_cdir(za, filelist, survivors, out) < 0) error = 1; } free(filelist); if (error) { fclose(out); (void)remove(temp); free(temp); return -1; } if (fclose(out) != 0) { _zip_error_set(&za->error, ZIP_ER_CLOSE, errno); (void)remove(temp); free(temp); return -1; } if (za->zp) { fclose(za->zp); za->zp = NULL; reopen_on_error = 1; } if (_zip_rename(temp, za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_RENAME, errno); (void)remove(temp); free(temp); if (reopen_on_error) { /* ignore errors, since we're already in an error case */ za->zp = fopen(za->zn, "rb"); } return -1; } #ifndef _WIN32 mask = umask(0); umask(mask); chmod(za->zn, 0666&~mask); #endif zip_discard(za); free(temp); return 0; }
static int add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) { zip_int64_t offstart, offdata, offend; struct zip_stat st; zip_source_t *s2; int ret; int is_zip64; zip_flags_t flags; if (zip_source_stat(src, &st) < 0) { _zip_error_set_from_source(&za->error, src); return -1; } if ((st.valid & ZIP_STAT_COMP_METHOD) == 0) { st.valid |= ZIP_STAT_COMP_METHOD; st.comp_method = ZIP_CM_STORE; } if (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != ZIP_CM_STORE) de->comp_method = st.comp_method; else if (de->comp_method == ZIP_CM_STORE && (st.valid & ZIP_STAT_SIZE)) { st.valid |= ZIP_STAT_COMP_SIZE; st.comp_size = st.size; } else { /* we'll recompress */ st.valid &= ~ZIP_STAT_COMP_SIZE; } flags = ZIP_EF_LOCAL; if ((st.valid & ZIP_STAT_SIZE) == 0) flags |= ZIP_FL_FORCE_ZIP64; else { de->uncomp_size = st.size; if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) { if (( ((de->comp_method == ZIP_CM_DEFLATE || ZIP_CM_IS_DEFAULT(de->comp_method)) && st.size > MAX_DEFLATE_SIZE_32) || (de->comp_method != ZIP_CM_STORE && de->comp_method != ZIP_CM_DEFLATE && !ZIP_CM_IS_DEFAULT(de->comp_method)))) flags |= ZIP_FL_FORCE_ZIP64; } else de->comp_size = st.comp_size; } if ((offstart = zip_source_tell_write(za->src)) < 0) { return -1; } /* as long as we don't support non-seekable output, clear data descriptor bit */ de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR; if ((is_zip64=_zip_dirent_write(za, de, flags)) < 0) return -1; if (st.comp_method == ZIP_CM_STORE || (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != de->comp_method)) { zip_source_t *s_store, *s_crc; zip_compression_implementation comp_impl; if (st.comp_method != ZIP_CM_STORE) { if ((comp_impl=_zip_get_compression_implementation(st.comp_method)) == NULL) { zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); return -1; } if ((s_store=comp_impl(za, src, st.comp_method, ZIP_CODEC_DECODE)) == NULL) { /* error set by comp_impl */ return -1; } } else { /* to have the same reference count to src as in the case where it's not stored */ zip_source_keep(src); s_store = src; } s_crc = zip_source_crc(za, s_store, 0); zip_source_free(s_store); if (s_crc == NULL) { return -1; } if (de->comp_method != ZIP_CM_STORE && ((st.valid & ZIP_STAT_SIZE) == 0 || st.size != 0)) { if ((comp_impl=_zip_get_compression_implementation(de->comp_method)) == NULL) { zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); zip_source_free(s_crc); return -1; } s2 = comp_impl(za, s_crc, de->comp_method, ZIP_CODEC_ENCODE); zip_source_free(s_crc); if (s2 == NULL) { return -1; } } else { s2 = s_crc; } } else { zip_source_keep(src); s2 = src; } if ((offdata = zip_source_tell_write(za->src)) < 0) { return -1; } ret = copy_source(za, s2); if (zip_source_stat(s2, &st) < 0) ret = -1; zip_source_free(s2); if (ret < 0) return -1; if ((offend = zip_source_tell_write(za->src)) < 0) { return -1; } if (zip_source_seek_write(za->src, offstart, SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } if ((st.valid & (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if ((de->changed & ZIP_DIRENT_LAST_MOD) == 0) { if (st.valid & ZIP_STAT_MTIME) de->last_mod = st.mtime; else time(&de->last_mod); } de->comp_method = st.comp_method; de->crc = st.crc; de->uncomp_size = st.size; de->comp_size = (zip_uint64_t)(offend - offdata); if ((ret=_zip_dirent_write(za, de, flags)) < 0) return -1; if (is_zip64 != ret) { /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */ zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if (zip_source_seek_write(za->src, offend, SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } return 0; }
ZIP_EXTERN int zip_close(struct zip *za) { int survivors; int i, j, error; char *temp; FILE *out; mode_t mask; struct zip_cdir *cd; struct zip_dirent de; struct filelist *filelist; int reopen_on_error; int new_torrentzip; reopen_on_error = 0; if (za == NULL) return -1; if (!_zip_changed(za, &survivors)) { _zip_free(za); return 0; } /* don't create zip files with no entries */ if (survivors == 0) { if (za->zn && za->zp) { if (remove(za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_REMOVE, errno); return -1; } } _zip_free(za); return 0; } if ((filelist=(struct filelist *)malloc(sizeof(filelist[0])*survivors)) == NULL) return -1; if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) { free(filelist); return -1; } for (i=0; i<survivors; i++) _zip_dirent_init(&cd->entry[i]); /* archive comment is special for torrentzip */ if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) { cd->comment = (char*)_zip_memdup((void*)(TORRENT_SIG "XXXXXXXX"), TORRENT_SIG_LEN + TORRENT_CRC_LEN, &za->error); if (cd->comment == NULL) { _zip_cdir_free(cd); free(filelist); return -1; } cd->comment_len = TORRENT_SIG_LEN + TORRENT_CRC_LEN; } else if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0) { if (_zip_cdir_set_comment(cd, za) == -1) { _zip_cdir_free(cd); free(filelist); return -1; } } if ((temp=_zip_create_temp_output(za, &out)) == NULL) { _zip_cdir_free(cd); free(filelist); return -1; } /* create list of files with index into original archive */ for (i=j=0; i<za->nentry; i++) { if (za->entry[i].state == ZIP_ST_DELETED) continue; filelist[j].idx = i; filelist[j].name = zip_get_name(za, i, 0); j++; } survivors = j; if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) qsort(filelist, survivors, sizeof(filelist[0]), _zip_torrentzip_cmp); new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1 && zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0); error = 0; for (j=0; j<survivors; j++) { i = filelist[j].idx; /* create new local directory entry */ if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) { _zip_dirent_init(&de); if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) _zip_dirent_torrent_normalize(&de); /* use it as central directory entry */ memcpy(cd->entry+j, &de, sizeof(cd->entry[j])); /* set/update file name */ if (za->entry[i].ch_filename == NULL) { if (za->entry[i].state == ZIP_ST_ADDED) { de.filename = strdup("-"); de.filename_len = 1; cd->entry[j].filename = "-"; cd->entry[j].filename_len = 1; } else { de.filename = strdup(za->cdir->entry[i].filename); de.filename_len = strlen(de.filename); cd->entry[j].filename = za->cdir->entry[i].filename; cd->entry[j].filename_len = de.filename_len; } } } else { /* copy existing directory entries */ if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); error = 1; break; } if (_zip_dirent_read(&de, za->zp, NULL, NULL, 1, &za->error) != 0) { error = 1; break; } memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j])); if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) { de.crc = za->cdir->entry[i].crc; de.comp_size = za->cdir->entry[i].comp_size; de.uncomp_size = za->cdir->entry[i].uncomp_size; de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR; cd->entry[j].bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR; } } if (za->entry[i].ch_filename) { free(de.filename); if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) { error = 1; break; } de.filename_len = strlen(de.filename); cd->entry[j].filename = za->entry[i].ch_filename; cd->entry[j].filename_len = de.filename_len; } if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0 && za->entry[i].ch_comment_len != -1) { /* as the rest of cd entries, its malloc/free is done by za */ cd->entry[j].comment = za->entry[i].ch_comment; cd->entry[j].comment_len = za->entry[i].ch_comment_len; } cd->entry[j].offset = (unsigned int)ftello(out); if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) { struct zip_source *zs; zs = NULL; if (!ZIP_ENTRY_DATA_CHANGED(za->entry+i)) { if ((zs=zip_source_zip(za, za, i, ZIP_FL_RECOMPRESS, 0, -1)) == NULL) { error = 1; break; } } if (add_data(za, zs ? zs : za->entry[i].source, &de, out) < 0) { error = 1; break; } cd->entry[j].last_mod = de.last_mod; cd->entry[j].comp_method = de.comp_method; cd->entry[j].comp_size = de.comp_size; cd->entry[j].uncomp_size = de.uncomp_size; cd->entry[j].crc = de.crc; } else { if (_zip_dirent_write(&de, out, 1, &za->error) < 0) { error = 1; break; } /* we just read the local dirent, file is at correct position */ if (copy_data(za->zp, cd->entry[j].comp_size, out, &za->error) < 0) { error = 1; break; } } _zip_dirent_finalize(&de); } free(filelist); if (!error) { if (write_cdir(za, cd, out) < 0) error = 1; } /* pointers in cd entries are owned by za */ cd->nentry = 0; _zip_cdir_free(cd); if (error) { _zip_dirent_finalize(&de); fclose(out); remove(temp); free(temp); return -1; } if (fclose(out) != 0) { _zip_error_set(&za->error, ZIP_ER_CLOSE, errno); remove(temp); free(temp); return -1; } if (za->zp) { fclose(za->zp); za->zp = NULL; reopen_on_error = 1; } if (_zip_rename(temp, za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_RENAME, errno); remove(temp); free(temp); if (reopen_on_error) { /* ignore errors, since we're already in an error case */ za->zp = fopen(za->zn, "rb"); } return -1; } #ifndef _MSC_VER mask = umask(0); umask(mask); chmod(za->zn, 0666&~mask); #endif _zip_free(za); free(temp); return 0; }
static int add_data(struct zip *za, struct zip_source *src, struct zip_dirent *de, FILE *ft) { off_t offstart, offdata, offend; struct zip_stat st; struct zip_source *s2; int ret; int is_zip64; zip_flags_t flags; if (zip_source_stat(src, &st) < 0) { _zip_error_set_from_source(&za->error, src); return -1; } if ((st.valid & ZIP_STAT_COMP_METHOD) == 0) { st.valid |= ZIP_STAT_COMP_METHOD; st.comp_method = ZIP_CM_STORE; } if (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != ZIP_CM_STORE) de->comp_method = st.comp_method; else if (de->comp_method == ZIP_CM_STORE && (st.valid & ZIP_STAT_SIZE)) { st.valid |= ZIP_STAT_COMP_SIZE; st.comp_size = st.size; } else { /* we'll recompress */ st.valid &= ~ZIP_STAT_COMP_SIZE; } flags = ZIP_EF_LOCAL; if ((st.valid & ZIP_STAT_SIZE) == 0) flags |= ZIP_FL_FORCE_ZIP64; else { de->uncomp_size = st.size; if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) { if (( ((de->comp_method == ZIP_CM_DEFLATE || ZIP_CM_IS_DEFAULT(de->comp_method)) && st.size > MAX_DEFLATE_SIZE_32) || (de->comp_method != ZIP_CM_STORE && de->comp_method != ZIP_CM_DEFLATE && !ZIP_CM_IS_DEFAULT(de->comp_method)))) flags |= ZIP_FL_FORCE_ZIP64; } else de->comp_size = st.comp_size; } offstart = ftello(ft); /* as long as we don't support non-seekable output, clear data descriptor bit */ de->bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR; if ((is_zip64=_zip_dirent_write(de, ft, flags, &za->error)) < 0) return -1; if (st.comp_method == ZIP_CM_STORE || (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != de->comp_method)) { struct zip_source *s_store, *s_crc; zip_compression_implementation comp_impl; if (st.comp_method != ZIP_CM_STORE) { if ((comp_impl=_zip_get_compression_implementation(st.comp_method)) == NULL) { _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); return -1; } if ((s_store=comp_impl(za, src, st.comp_method, ZIP_CODEC_DECODE)) == NULL) { /* error set by comp_impl */ return -1; } } else s_store = src; if ((s_crc=zip_source_crc(za, s_store, 0)) == NULL) { if (s_store != src) zip_source_pop(s_store); return -1; } /* TODO: deflate 0-byte files for torrentzip? */ if (de->comp_method != ZIP_CM_STORE && ((st.valid & ZIP_STAT_SIZE) == 0 || st.size != 0)) { if ((comp_impl=_zip_get_compression_implementation(de->comp_method)) == NULL) { _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); zip_source_pop(s_crc); if (s_store != src) zip_source_pop(s_store); return -1; } if ((s2=comp_impl(za, s_crc, de->comp_method, ZIP_CODEC_ENCODE)) == NULL) { zip_source_pop(s_crc); if (s_store != src) zip_source_pop(s_store); return -1; } } else s2 = s_crc; } else s2 = src; offdata = ftello(ft); ret = copy_source(za, s2, ft); if (zip_source_stat(s2, &st) < 0) ret = -1; while (s2 != src) { if ((s2=zip_source_pop(s2)) == NULL) { /* TODO: set erorr */ ret = -1; break; } } if (ret < 0) return -1; offend = ftello(ft); if (fseeko(ft, offstart, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } if ((st.valid & (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) { _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if (st.valid & ZIP_STAT_MTIME) de->last_mod = st.mtime; else time(&de->last_mod); de->comp_method = st.comp_method; de->crc = st.crc; de->uncomp_size = st.size; de->comp_size = (zip_uint64_t)(offend - offdata); if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) _zip_dirent_torrent_normalize(de); if ((ret=_zip_dirent_write(de, ft, flags, &za->error)) < 0) return -1; if (is_zip64 != ret) { /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */ _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if (fseeko(ft, offend, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } return 0; }
static int add_data(struct zip *za, struct zip_source *zs, struct zip_dirent *de, FILE *ft) { off_t offstart, offend; zip_source_callback cb; void *ud; struct zip_stat st; cb = zs->f; ud = zs->ud; if (cb(ud, &st, sizeof(st), ZIP_SOURCE_STAT) < (ssize_t)sizeof(st)) { ch_set_error(&za->error, cb, ud); return -1; } if (cb(ud, NULL, 0, ZIP_SOURCE_OPEN) < 0) { ch_set_error(&za->error, cb, ud); return -1; } offstart = ftello(ft); if (_zip_dirent_write(de, ft, 1, &za->error) < 0) return -1; if (st.comp_method != ZIP_CM_STORE) { if (add_data_comp(cb, ud, &st, ft, &za->error) < 0) return -1; } else { if (add_data_uncomp(za, cb, ud, &st, ft) < 0) return -1; } if (cb(ud, NULL, 0, ZIP_SOURCE_CLOSE) < 0) { ch_set_error(&za->error, cb, ud); return -1; } offend = ftello(ft); if (fseeko(ft, offstart, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } de->last_mod = st.mtime; de->comp_method = st.comp_method; de->crc = st.crc; de->uncomp_size = (unsigned int)st.size; de->comp_size = (unsigned int)st.comp_size; if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) _zip_dirent_torrent_normalize(de); if (_zip_dirent_write(de, ft, 1, &za->error) < 0) return -1; if (fseeko(ft, offend, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } return 0; }
static int add_data(struct zip *za, struct zip_source *src, struct zip_dirent *de, FILE *ft) { off_t offstart, offdata, offend; struct zip_stat st; struct zip_source *s2; zip_compression_implementation comp_impl; int ret; if (zip_source_stat(src, &st) < 0) { _zip_error_set_from_source(&za->error, src); return -1; } offstart = ftello(ft); if (_zip_dirent_write(de, ft, 1, &za->error) < 0) return -1; if ((s2=zip_source_crc(za, src, 0)) == NULL) { zip_source_pop(s2); return -1; } /* XXX: deflate 0-byte files for torrentzip? */ if (((st.valid & ZIP_STAT_COMP_METHOD) == 0 || st.comp_method == ZIP_CM_STORE) && ((st.valid & ZIP_STAT_SIZE) == 0 || st.size != 0)) { comp_impl = NULL; if ((comp_impl=zip_get_compression_implementation(ZIP_CM_DEFLATE)) == NULL) { _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); zip_source_pop(s2); return -1; } if ((s2=comp_impl(za, s2, ZIP_CM_DEFLATE, ZIP_CODEC_ENCODE)) == NULL) { /* XXX: set error? */ zip_source_pop(s2); return -1; } } else s2 = src; offdata = ftello(ft); ret = copy_source(za, s2, ft); if (zip_source_stat(s2, &st) < 0) ret = -1; while (s2 != src) { if ((s2=zip_source_pop(s2)) == NULL) { /* XXX: set erorr */ ret = -1; break; } } if (ret < 0) return -1; offend = ftello(ft); if (fseeko(ft, offstart, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } de->last_mod = st.mtime; de->comp_method = st.comp_method; de->crc = st.crc; de->uncomp_size = st.size; de->comp_size = offend - offdata; if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) _zip_dirent_torrent_normalize(de); if (_zip_dirent_write(de, ft, 1, &za->error) < 0) return -1; if (fseeko(ft, offend, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } return 0; }
int zip_close(struct zip *za) { int changed, survivors; int i, j, tfd, error; char *temp; FILE *tfp; mode_t mask; struct zip_cdir *cd; struct zip_dirent de; changed = survivors = 0; for (i=0; i<za->nentry; i++) { if (za->entry[i].state != ZIP_ST_UNCHANGED) changed = 1; if (za->entry[i].state != ZIP_ST_DELETED) survivors++; } if (!changed) { _zip_free(za); return 0; } /* don't create zip files with no entries */ if (survivors == 0) { if (za->zn) { if (remove(za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_REMOVE, errno); return -1; } } _zip_free(za); return 0; } if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) return -1; if ((temp=malloc(strlen(za->zn)+8)) == NULL) { _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); _zip_cdir_free(cd); return -1; } sprintf(temp, "%s.XXXXXX", za->zn); if ((tfd=mkstemp(temp)) == -1) { _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno); _zip_cdir_free(cd); free(temp); return -1; } if ((tfp=fdopen(tfd, "r+b")) == NULL) { _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno); _zip_cdir_free(cd); close(tfd); remove(temp); free(temp); return -1; } error = 0; for (i=j=0; i<za->nentry; i++) { if (za->entry[i].state == ZIP_ST_DELETED) continue; if (ZIP_ENTRY_DATA_CHANGED(za->entry+i)) { _zip_dirent_init(&de); memcpy(cd->entry+j, &de, sizeof(cd->entry[i])); if (za->entry[i].ch_filename == NULL) { if (za->entry[i].state == ZIP_ST_REPLACED) { de.filename = strdup(za->cdir->entry[i].filename); de.filename_len = strlen(de.filename); cd->entry[j].filename = za->cdir->entry[i].filename; cd->entry[j].filename_len = de.filename_len; } else { de.filename = strdup("-"); de.filename_len = 1; cd->entry[j].filename = "-"; cd->entry[j].filename_len = de.filename_len; } } } else { if (fseek(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); error = 1; break; } if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0) { error = 1; break; } memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[i])); } if (za->entry[i].ch_filename) { free(de.filename); de.filename = strdup(za->entry[i].ch_filename); de.filename_len = strlen(de.filename); cd->entry[j].filename = za->entry[i].ch_filename; cd->entry[j].filename_len = de.filename_len; } cd->entry[j].offset = ftell(tfp); if (ZIP_ENTRY_DATA_CHANGED(za->entry+i)) { if (add_data(za, i, &de, tfp) < 0) { error = -1; break; } cd->entry[j].last_mod = de.last_mod; cd->entry[j].comp_method = de.comp_method; cd->entry[j].comp_size = de.comp_size; cd->entry[j].uncomp_size = de.uncomp_size; cd->entry[j].crc = de.crc; } else { if (_zip_dirent_write(&de, tfp, 1, &za->error) < 0) { error = 1; break; } /* we just read the local dirent, file is at correct position */ if (copy_data(za->zp, de.comp_size, tfp, &za->error) < 0) { error = 1; break; } } j++; _zip_dirent_finalize(&de); } if (!error) { if (_zip_cdir_write(cd, tfp, &za->error) < 0) error = 1; } /* pointers in cd are owned by za */ cd->nentry = 0; _zip_cdir_free(cd); if (error) { _zip_dirent_finalize(&de); fclose(tfp); remove(temp); free(temp); return -1; } if (fclose(tfp) != 0) { _zip_error_set(&za->error, ZIP_ER_CLOSE, errno); remove(temp); free(temp); return -1; } if (rename(temp, za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_RENAME, errno); remove(temp); free(temp); return -1; } if (za->zp) { fclose(za->zp); za->zp = NULL; } mask = umask(0); umask(mask); chmod(za->zn, 0666&~mask); _zip_free(za); return 0; }
static int add_data(struct zip *za, int idx, struct zip_dirent *de, FILE *ft) { off_t offstart, offend; zip_source_callback cb; void *ud; struct zip_stat st; cb = za->entry[idx].source->f; ud = za->entry[idx].source->ud; if (cb(ud, &st, sizeof(st), ZIP_SOURCE_STAT) < sizeof(st)) { ch_set_error(&za->error, cb, ud); return -1; } if (cb(ud, NULL, 0, ZIP_SOURCE_OPEN) < 0) { ch_set_error(&za->error, cb, ud); return -1; } offstart = ftell(ft); if (_zip_dirent_write(de, ft, 1, &za->error) < 0) return -1; if (st.comp_method != ZIP_CM_STORE) { if (add_data_comp(cb, ud, &st, ft, &za->error) < 0) return -1; } else { if (add_data_uncomp(cb, ud, &st, ft, &za->error) < 0) return -1; } if (cb(ud, NULL, 0, ZIP_SOURCE_CLOSE) < 0) { ch_set_error(&za->error, cb, ud); return -1; } offend = ftell(ft); if (fseek(ft, offstart, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } de->comp_method = st.comp_method; de->last_mod = st.mtime; de->crc = st.crc; de->uncomp_size = st.size; de->comp_size = st.comp_size; if (_zip_dirent_write(de, ft, 1, &za->error) < 0) return -1; if (fseek(ft, offend, SEEK_SET) < 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } return 0; }
static int add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) { zip_int64_t offstart, offdata, offend; struct zip_stat st; zip_source_t *src_final, *src_tmp; int ret; int is_zip64; zip_flags_t flags; bool needs_recompress, needs_decompress, needs_crc, needs_compress, needs_reencrypt, needs_decrypt, needs_encrypt; if (zip_source_stat(src, &st) < 0) { _zip_error_set_from_source(&za->error, src); return -1; } if ((st.valid & ZIP_STAT_COMP_METHOD) == 0) { st.valid |= ZIP_STAT_COMP_METHOD; st.comp_method = ZIP_CM_STORE; } if (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != ZIP_CM_STORE) de->comp_method = st.comp_method; else if (de->comp_method == ZIP_CM_STORE && (st.valid & ZIP_STAT_SIZE)) { st.valid |= ZIP_STAT_COMP_SIZE; st.comp_size = st.size; } else { /* we'll recompress */ st.valid &= ~ZIP_STAT_COMP_SIZE; } if ((st.valid & ZIP_STAT_ENCRYPTION_METHOD) == 0) { st.valid |= ZIP_STAT_ENCRYPTION_METHOD; st.encryption_method = ZIP_EM_NONE; } flags = ZIP_EF_LOCAL; if ((st.valid & ZIP_STAT_SIZE) == 0) flags |= ZIP_FL_FORCE_ZIP64; else { de->uncomp_size = st.size; if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) { if (( ((de->comp_method == ZIP_CM_DEFLATE || ZIP_CM_IS_DEFAULT(de->comp_method)) && st.size > MAX_DEFLATE_SIZE_32) || (de->comp_method != ZIP_CM_STORE && de->comp_method != ZIP_CM_DEFLATE && !ZIP_CM_IS_DEFAULT(de->comp_method)))) flags |= ZIP_FL_FORCE_ZIP64; } else de->comp_size = st.comp_size; } if ((offstart = zip_source_tell_write(za->src)) < 0) { return -1; } /* as long as we don't support non-seekable output, clear data descriptor bit */ de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR; if ((is_zip64=_zip_dirent_write(za, de, flags)) < 0) return -1; needs_recompress = !((st.comp_method == de->comp_method) || (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method == ZIP_CM_DEFLATE)); needs_decompress = needs_recompress && (st.comp_method != ZIP_CM_STORE); needs_crc = (st.comp_method == ZIP_CM_STORE) || needs_decompress; needs_compress = needs_recompress && (de->comp_method != ZIP_CM_STORE); needs_reencrypt = needs_recompress || (de->changed & ZIP_DIRENT_PASSWORD) || (de->encryption_method != st.encryption_method); needs_decrypt = needs_reencrypt && (st.encryption_method != ZIP_EM_NONE); needs_encrypt = needs_reencrypt && (de->encryption_method != ZIP_EM_NONE); src_final = src; zip_source_keep(src_final); if (needs_decrypt) { zip_encryption_implementation impl; if ((impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); zip_source_free(src_final); return -1; } if ((src_tmp = impl(za, src_final, st.encryption_method, ZIP_CODEC_DECODE, za->default_password)) == NULL) { /* error set by impl */ zip_source_free(src_final); return -1; } zip_source_free(src_final); src_final = src_tmp; } if (needs_decompress) { zip_compression_implementation comp_impl; if ((comp_impl = _zip_get_compression_implementation(st.comp_method, ZIP_CODEC_DECODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); zip_source_free(src_final); return -1; } if ((src_tmp = comp_impl(za, src_final, st.comp_method, ZIP_CODEC_DECODE)) == NULL) { /* error set by comp_impl */ zip_source_free(src_final); return -1; } zip_source_free(src_final); src_final = src_tmp; } if (needs_crc) { if ((src_tmp = zip_source_crc(za, src_final, 0)) == NULL) { zip_source_free(src_final); return -1; } zip_source_free(src_final); src_final = src_tmp; } if (needs_compress) { zip_compression_implementation comp_impl; if ((comp_impl = _zip_get_compression_implementation(de->comp_method, ZIP_CODEC_ENCODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); zip_source_free(src_final); return -1; } if ((src_tmp = comp_impl(za, src_final, de->comp_method, ZIP_CODEC_ENCODE)) == NULL) { zip_source_free(src_final); return -1; } zip_source_free(src_final); src_final = src_tmp; } if (needs_encrypt) { zip_encryption_implementation impl; const char *password = NULL; if (de->password) { password = de->password; } else if (za->default_password) { password = za->default_password; } if ((impl = _zip_get_encryption_implementation(de->encryption_method, ZIP_CODEC_ENCODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); zip_source_free(src_final); return -1; } if ((src_tmp = impl(za, src_final, de->encryption_method, ZIP_CODEC_ENCODE, password)) == NULL) { /* error set by impl */ zip_source_free(src_final); return -1; } zip_source_free(src_final); src_final = src_tmp; } if ((offdata = zip_source_tell_write(za->src)) < 0) { return -1; } ret = copy_source(za, src_final); if (zip_source_stat(src_final, &st) < 0) { ret = -1; } zip_source_free(src_final); if (ret < 0) { return -1; } if ((offend = zip_source_tell_write(za->src)) < 0) { return -1; } if (zip_source_seek_write(za->src, offstart, SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } if ((st.valid & (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if ((de->changed & ZIP_DIRENT_LAST_MOD) == 0) { if (st.valid & ZIP_STAT_MTIME) de->last_mod = st.mtime; else time(&de->last_mod); } de->comp_method = st.comp_method; de->crc = st.crc; de->uncomp_size = st.size; de->comp_size = (zip_uint64_t)(offend - offdata); if ((ret=_zip_dirent_write(za, de, flags)) < 0) return -1; if (is_zip64 != ret) { /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */ zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if (zip_source_seek_write(za->src, offend, SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } return 0; }