1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/*
zip_close.c -- close zip archive and update changes
Copyright (C) 1999-2009 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <libzip@nih.at>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "zipint.h"
static int add_data(struct zip *, struct zip_source *, struct zip_dirent *,
FILE *);
static int add_data_comp(zip_source_callback, void *, struct zip_stat *,
FILE *, struct zip_error *);
static int add_data_uncomp(struct zip *, zip_source_callback, void *,
struct zip_stat *, FILE *);
static void ch_set_error(struct zip_error *, zip_source_callback, void *);
static int copy_data(FILE *, off_t, FILE *, struct zip_error *);
static int write_cdir(struct zip *, struct zip_cdir *, FILE *);
static int _zip_cdir_set_comment(struct zip_cdir *, struct zip *);
static int _zip_changed(struct zip *, int *);
static char *_zip_create_temp_output(struct zip *, FILE **);
static int _zip_torrentzip_cmp(const void *, const void *);
struct filelist {
int idx;
const char *name;
};
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 = _zip_memdup(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++;
}
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 = 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;
}
mask = umask(0);
umask(mask);
chmod(za->zn, 0666&~mask);
if (za->ch_comment)
free(za->ch_comment);
_zip_free(za);
free(temp);
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 = st.size;
de->comp_size = 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_comp(zip_source_callback cb, void *ud, struct zip_stat *st,FILE *ft,
struct zip_error *error)
{
char buf[BUFSIZE];
ssize_t n;
st->comp_size = 0;
while ((n=cb(ud, buf, sizeof(buf), ZIP_SOURCE_READ)) > 0) {
if (fwrite(buf, 1, n, ft) != (size_t)n) {
_zip_error_set(error, ZIP_ER_WRITE, errno);
return -1;
}
st->comp_size += n;
}
if (n < 0) {
ch_set_error(error, cb, ud);
return -1;
}
return 0;
}
static int
add_data_uncomp(struct zip *za, zip_source_callback cb, void *ud,
struct zip_stat *st, FILE *ft)
{
char b1[BUFSIZE], b2[BUFSIZE];
int end, flush, ret;
ssize_t n;
size_t n2;
z_stream zstr;
int mem_level;
st->comp_method = ZIP_CM_DEFLATE;
st->comp_size = st->size = 0;
st->crc = crc32(0, NULL, 0);
zstr.zalloc = Z_NULL;
zstr.zfree = Z_NULL;
zstr.opaque = NULL;
zstr.avail_in = 0;
zstr.avail_out = 0;
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
mem_level = TORRENT_MEM_LEVEL;
else
mem_level = MAX_MEM_LEVEL;
/* -MAX_WBITS: undocumented feature of zlib to _not_ write a zlib header */
deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, mem_level,
Z_DEFAULT_STRATEGY);
zstr.next_out = (Bytef *)b2;
zstr.avail_out = sizeof(b2);
zstr.next_in = NULL;
zstr.avail_in = 0;
flush = 0;
end = 0;
while (!end) {
if (zstr.avail_in == 0 && !flush) {
if ((n=cb(ud, b1, sizeof(b1), ZIP_SOURCE_READ)) < 0) {
ch_set_error(&za->error, cb, ud);
deflateEnd(&zstr);
return -1;
}
if (n > 0) {
zstr.avail_in = n;
zstr.next_in = (Bytef *)b1;
st->size += n;
st->crc = crc32(st->crc, (Bytef *)b1, n);
}
else
flush = Z_FINISH;
}
ret = deflate(&zstr, flush);
if (ret != Z_OK && ret != Z_STREAM_END) {
_zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
return -1;
}
if (zstr.avail_out != sizeof(b2)) {
n2 = sizeof(b2) - zstr.avail_out;
if (fwrite(b2, 1, n2, ft) != n2) {
_zip_error_set(&za->error, ZIP_ER_WRITE, errno);
return -1;
}
zstr.next_out = (Bytef *)b2;
zstr.avail_out = sizeof(b2);
st->comp_size += n2;
}
if (ret == Z_STREAM_END) {
deflateEnd(&zstr);
end = 1;
}
}
return 0;
}
static void
ch_set_error(struct zip_error *error, zip_source_callback cb, void *ud)
{
int e[2];
if ((cb(ud, e, sizeof(e), ZIP_SOURCE_ERROR)) < (ssize_t)sizeof(e)) {
error->zip_err = ZIP_ER_INTERNAL;
error->sys_err = 0;
}
else {
error->zip_err = e[0];
error->sys_err = e[1];
}
}
static int
copy_data(FILE *fs, off_t len, FILE *ft, struct zip_error *error)
{
char buf[BUFSIZE];
int n, nn;
if (len == 0)
return 0;
while (len > 0) {
nn = len > sizeof(buf) ? sizeof(buf) : len;
if ((n=fread(buf, 1, nn, fs)) < 0) {
_zip_error_set(error, ZIP_ER_READ, errno);
return -1;
}
else if (n == 0) {
_zip_error_set(error, ZIP_ER_EOF, 0);
return -1;
}
if (fwrite(buf, 1, n, ft) != (size_t)n) {
_zip_error_set(error, ZIP_ER_WRITE, errno);
return -1;
}
len -= n;
}
return 0;
}
static int
write_cdir(struct zip *za, struct zip_cdir *cd, FILE *out)
{
off_t offset;
uLong crc;
char buf[TORRENT_CRC_LEN+1];
if (_zip_cdir_write(cd, out, &za->error) < 0)
return -1;
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0)
return 0;
/* fix up torrentzip comment */
offset = ftello(out);
if (_zip_filerange_crc(out, cd->offset, cd->size, &crc, &za->error) < 0)
return -1;
snprintf(buf, sizeof(buf), "%08lX", (long)crc);
if (fseeko(out, offset-TORRENT_CRC_LEN, SEEK_SET) < 0) {
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
return -1;
}
if (fwrite(buf, TORRENT_CRC_LEN, 1, out) != 1) {
_zip_error_set(&za->error, ZIP_ER_WRITE, errno);
return -1;
}
return 0;
}
static int
_zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
{
if (src->ch_comment_len != -1) {
dest->comment = _zip_memdup(src->ch_comment,
src->ch_comment_len, &src->error);
if (dest->comment == NULL)
return -1;
dest->comment_len = src->ch_comment_len;
} else {
if (src->cdir && src->cdir->comment) {
dest->comment = _zip_memdup(src->cdir->comment,
src->cdir->comment_len, &src->error);
if (dest->comment == NULL)
return -1;
dest->comment_len = src->cdir->comment_len;
}
}
return 0;
}
static int
_zip_changed(struct zip *za, int *survivorsp)
{
int changed, i, survivors;
changed = survivors = 0;
if (za->ch_comment_len != -1
|| za->ch_flags != za->flags)
changed = 1;
for (i=0; i<za->nentry; i++) {
if ((za->entry[i].state != ZIP_ST_UNCHANGED)
|| (za->entry[i].ch_comment_len != -1))
changed = 1;
if (za->entry[i].state != ZIP_ST_DELETED)
survivors++;
}
*survivorsp = survivors;
return changed;
}
static char *
_zip_create_temp_output(struct zip *za, FILE **outp)
{
char *temp;
int tfd;
FILE *tfp;
int len = strlen(za->zn) + 8;
if ((temp=(char *)malloc(len)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
snprintf(temp, len, "%s.XXXXXX", za->zn);
if ((tfd=mkstemp(temp)) == -1) {
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
free(temp);
return NULL;
}
if ((tfp=fdopen(tfd, "r+b")) == NULL) {
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
close(tfd);
remove(temp);
free(temp);
return NULL;
}
#ifdef PHP_WIN32
_setmode(_fileno(tfp), _O_BINARY );
#endif
*outp = tfp;
return temp;
}
static int
_zip_torrentzip_cmp(const void *a, const void *b)
{
return strcasecmp(((const struct filelist *)a)->name,
((const struct filelist *)b)->name);
}
|