zip_close.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. zip_close.c -- close zip archive and update changes
  3. Copyright (C) 1999-2013 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <[email protected]>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "zipint.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef HAVE_STRINGS_H
  35. #include <strings.h>
  36. #endif
  37. #include <errno.h>
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #ifdef _WIN32
  44. #include <io.h>
  45. #include <fcntl.h>
  46. #endif
  47. /* max deflate size increase: size + ceil(size/16k)*5+6 */
  48. #define MAX_DEFLATE_SIZE_32 4293656963u
  49. static int add_data(struct zip *, struct zip_source *, struct zip_dirent *, FILE *);
  50. static int copy_data(FILE *, zip_uint64_t, FILE *, struct zip_error *);
  51. static int copy_source(struct zip *, struct zip_source *, FILE *);
  52. static int write_cdir(struct zip *, const struct zip_filelist *, zip_uint64_t, FILE *);
  53. static char *_zip_create_temp_output(struct zip *, FILE **);
  54. static int _zip_torrentzip_cmp(const void *, const void *);
  55. ZIP_EXTERN int
  56. zip_close(struct zip *za)
  57. {
  58. zip_uint64_t i, j, survivors;
  59. int error;
  60. char *temp;
  61. FILE *out;
  62. #ifndef _WIN32
  63. mode_t mask;
  64. #endif
  65. struct zip_filelist *filelist;
  66. int reopen_on_error;
  67. int new_torrentzip;
  68. int changed;
  69. reopen_on_error = 0;
  70. if (za == NULL)
  71. return -1;
  72. changed = _zip_changed(za, &survivors);
  73. /* don't create zip files with no entries */
  74. if (survivors == 0) {
  75. if (za->zn && ((za->open_flags & ZIP_TRUNCATE) || (changed && za->zp))) {
  76. if (remove(za->zn) != 0) {
  77. _zip_error_set(&za->error, ZIP_ER_REMOVE, errno);
  78. return -1;
  79. }
  80. }
  81. zip_discard(za);
  82. return 0;
  83. }
  84. if (!changed) {
  85. zip_discard(za);
  86. return 0;
  87. }
  88. if (survivors > za->nentry) {
  89. _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  90. return -1;
  91. }
  92. if ((filelist=(struct zip_filelist *)malloc(sizeof(filelist[0])*(size_t)survivors)) == NULL)
  93. return -1;
  94. /* archive comment is special for torrentzip */
  95. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) {
  96. /* TODO: use internal function when zip_set_archive_comment clears TORRENT flag */
  97. if (zip_set_archive_comment(za, TORRENT_SIG "XXXXXXXX", TORRENT_SIG_LEN + TORRENT_CRC_LEN) < 0) {
  98. free(filelist);
  99. return -1;
  100. }
  101. }
  102. /* TODO: if no longer torrentzip and archive comment not changed by user, delete it */
  103. /* create list of files with index into original archive */
  104. for (i=j=0; i<za->nentry; i++) {
  105. if (za->entry[i].deleted)
  106. continue;
  107. if (j >= survivors) {
  108. free(filelist);
  109. _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  110. return -1;
  111. }
  112. filelist[j].idx = i;
  113. filelist[j].name = zip_get_name(za, i, 0);
  114. j++;
  115. }
  116. if (j < survivors) {
  117. free(filelist);
  118. _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  119. return -1;
  120. }
  121. if ((temp=_zip_create_temp_output(za, &out)) == NULL) {
  122. free(filelist);
  123. return -1;
  124. }
  125. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  126. qsort(filelist, (size_t)survivors, sizeof(filelist[0]),
  127. _zip_torrentzip_cmp);
  128. new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1
  129. && zip_get_archive_flag(za, ZIP_AFL_TORRENT,
  130. ZIP_FL_UNCHANGED) == 0);
  131. error = 0;
  132. for (j=0; j<survivors; j++) {
  133. int new_data;
  134. struct zip_entry *entry;
  135. struct zip_dirent *de;
  136. i = filelist[j].idx;
  137. entry = za->entry+i;
  138. new_data = (ZIP_ENTRY_DATA_CHANGED(entry) || new_torrentzip || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_COMP_METHOD));
  139. /* create new local directory entry */
  140. if (entry->changes == NULL) {
  141. if ((entry->changes=_zip_dirent_clone(entry->orig)) == NULL) {
  142. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  143. error = 1;
  144. break;
  145. }
  146. }
  147. de = entry->changes;
  148. if (_zip_read_local_ef(za, i) < 0) {
  149. error = 1;
  150. break;
  151. }
  152. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  153. _zip_dirent_torrent_normalize(entry->changes);
  154. de->offset = (zip_uint64_t)ftello(out); /* TODO: check for errors */
  155. if (new_data) {
  156. struct zip_source *zs;
  157. zs = NULL;
  158. if (!ZIP_ENTRY_DATA_CHANGED(entry)) {
  159. if ((zs=_zip_source_zip_new(za, za, i, ZIP_FL_UNCHANGED, 0, 0, NULL)) == NULL) {
  160. error = 1;
  161. break;
  162. }
  163. }
  164. /* add_data writes dirent */
  165. if (add_data(za, zs ? zs : entry->source, de, out) < 0) {
  166. error = 1;
  167. if (zs)
  168. zip_source_free(zs);
  169. break;
  170. }
  171. if (zs)
  172. zip_source_free(zs);
  173. }
  174. else {
  175. zip_uint64_t offset;
  176. /* when copying data, all sizes are known -> no data descriptor needed */
  177. de->bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
  178. if (_zip_dirent_write(de, out, ZIP_FL_LOCAL, &za->error) < 0) {
  179. error = 1;
  180. break;
  181. }
  182. if ((offset=_zip_file_get_offset(za, i, &za->error)) == 0) {
  183. error = 1;
  184. break;
  185. }
  186. if ((fseeko(za->zp, (off_t)offset, SEEK_SET) < 0)) {
  187. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  188. error = 1;
  189. break;
  190. }
  191. if (copy_data(za->zp, de->comp_size, out, &za->error) < 0) {
  192. error = 1;
  193. break;
  194. }
  195. }
  196. }
  197. if (!error) {
  198. if (write_cdir(za, filelist, survivors, out) < 0)
  199. error = 1;
  200. }
  201. free(filelist);
  202. if (error) {
  203. fclose(out);
  204. (void)remove(temp);
  205. free(temp);
  206. return -1;
  207. }
  208. if (fclose(out) != 0) {
  209. _zip_error_set(&za->error, ZIP_ER_CLOSE, errno);
  210. (void)remove(temp);
  211. free(temp);
  212. return -1;
  213. }
  214. if (za->zp) {
  215. fclose(za->zp);
  216. za->zp = NULL;
  217. reopen_on_error = 1;
  218. }
  219. if (_zip_rename(temp, za->zn) != 0) {
  220. _zip_error_set(&za->error, ZIP_ER_RENAME, errno);
  221. (void)remove(temp);
  222. free(temp);
  223. if (reopen_on_error) {
  224. /* ignore errors, since we're already in an error case */
  225. za->zp = fopen(za->zn, "rb");
  226. }
  227. return -1;
  228. }
  229. #ifndef _WIN32
  230. mask = umask(0);
  231. umask(mask);
  232. chmod(za->zn, 0666&~mask);
  233. #endif
  234. zip_discard(za);
  235. free(temp);
  236. return 0;
  237. }
  238. static int
  239. add_data(struct zip *za, struct zip_source *src, struct zip_dirent *de, FILE *ft)
  240. {
  241. off_t offstart, offdata, offend;
  242. struct zip_stat st;
  243. struct zip_source *s2;
  244. int ret;
  245. int is_zip64;
  246. zip_flags_t flags;
  247. if (zip_source_stat(src, &st) < 0) {
  248. _zip_error_set_from_source(&za->error, src);
  249. return -1;
  250. }
  251. if ((st.valid & ZIP_STAT_COMP_METHOD) == 0) {
  252. st.valid |= ZIP_STAT_COMP_METHOD;
  253. st.comp_method = ZIP_CM_STORE;
  254. }
  255. if (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != ZIP_CM_STORE)
  256. de->comp_method = st.comp_method;
  257. else if (de->comp_method == ZIP_CM_STORE && (st.valid & ZIP_STAT_SIZE)) {
  258. st.valid |= ZIP_STAT_COMP_SIZE;
  259. st.comp_size = st.size;
  260. }
  261. else {
  262. /* we'll recompress */
  263. st.valid &= ~ZIP_STAT_COMP_SIZE;
  264. }
  265. flags = ZIP_EF_LOCAL;
  266. if ((st.valid & ZIP_STAT_SIZE) == 0)
  267. flags |= ZIP_FL_FORCE_ZIP64;
  268. else {
  269. de->uncomp_size = st.size;
  270. if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) {
  271. if (( ((de->comp_method == ZIP_CM_DEFLATE || ZIP_CM_IS_DEFAULT(de->comp_method)) && st.size > MAX_DEFLATE_SIZE_32)
  272. || (de->comp_method != ZIP_CM_STORE && de->comp_method != ZIP_CM_DEFLATE && !ZIP_CM_IS_DEFAULT(de->comp_method))))
  273. flags |= ZIP_FL_FORCE_ZIP64;
  274. }
  275. else
  276. de->comp_size = st.comp_size;
  277. }
  278. offstart = ftello(ft);
  279. /* as long as we don't support non-seekable output, clear data descriptor bit */
  280. de->bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
  281. if ((is_zip64=_zip_dirent_write(de, ft, flags, &za->error)) < 0)
  282. return -1;
  283. if (st.comp_method == ZIP_CM_STORE || (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != de->comp_method)) {
  284. struct zip_source *s_store, *s_crc;
  285. zip_compression_implementation comp_impl;
  286. if (st.comp_method != ZIP_CM_STORE) {
  287. if ((comp_impl=_zip_get_compression_implementation(st.comp_method)) == NULL) {
  288. _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
  289. return -1;
  290. }
  291. if ((s_store=comp_impl(za, src, st.comp_method, ZIP_CODEC_DECODE)) == NULL) {
  292. /* error set by comp_impl */
  293. return -1;
  294. }
  295. }
  296. else
  297. s_store = src;
  298. if ((s_crc=zip_source_crc(za, s_store, 0)) == NULL) {
  299. if (s_store != src)
  300. zip_source_pop(s_store);
  301. return -1;
  302. }
  303. /* TODO: deflate 0-byte files for torrentzip? */
  304. if (de->comp_method != ZIP_CM_STORE && ((st.valid & ZIP_STAT_SIZE) == 0 || st.size != 0)) {
  305. if ((comp_impl=_zip_get_compression_implementation(de->comp_method)) == NULL) {
  306. _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
  307. zip_source_pop(s_crc);
  308. if (s_store != src)
  309. zip_source_pop(s_store);
  310. return -1;
  311. }
  312. if ((s2=comp_impl(za, s_crc, de->comp_method, ZIP_CODEC_ENCODE)) == NULL) {
  313. zip_source_pop(s_crc);
  314. if (s_store != src)
  315. zip_source_pop(s_store);
  316. return -1;
  317. }
  318. }
  319. else
  320. s2 = s_crc;
  321. }
  322. else
  323. s2 = src;
  324. offdata = ftello(ft);
  325. ret = copy_source(za, s2, ft);
  326. if (zip_source_stat(s2, &st) < 0)
  327. ret = -1;
  328. while (s2 != src) {
  329. if ((s2=zip_source_pop(s2)) == NULL) {
  330. /* TODO: set erorr */
  331. ret = -1;
  332. break;
  333. }
  334. }
  335. if (ret < 0)
  336. return -1;
  337. offend = ftello(ft);
  338. if (fseeko(ft, offstart, SEEK_SET) < 0) {
  339. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  340. return -1;
  341. }
  342. if ((st.valid & (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) {
  343. _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  344. return -1;
  345. }
  346. if (st.valid & ZIP_STAT_MTIME)
  347. de->last_mod = st.mtime;
  348. else
  349. time(&de->last_mod);
  350. de->comp_method = st.comp_method;
  351. de->crc = st.crc;
  352. de->uncomp_size = st.size;
  353. de->comp_size = (zip_uint64_t)(offend - offdata);
  354. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  355. _zip_dirent_torrent_normalize(de);
  356. if ((ret=_zip_dirent_write(de, ft, flags, &za->error)) < 0)
  357. return -1;
  358. if (is_zip64 != ret) {
  359. /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */
  360. _zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  361. return -1;
  362. }
  363. if (fseeko(ft, offend, SEEK_SET) < 0) {
  364. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. static int
  370. copy_data(FILE *fs, zip_uint64_t len, FILE *ft, struct zip_error *error)
  371. {
  372. char buf[BUFSIZE];
  373. size_t n, nn;
  374. if (len == 0)
  375. return 0;
  376. while (len > 0) {
  377. nn = len > sizeof(buf) ? sizeof(buf) : len > SIZE_MAX ? SIZE_MAX : (size_t)len;
  378. if ((n=fread(buf, 1, nn, fs)) == 0) {
  379. if (ferror(fs)) {
  380. _zip_error_set(error, ZIP_ER_READ, errno);
  381. return -1;
  382. }
  383. else {
  384. _zip_error_set(error, ZIP_ER_EOF, 0);
  385. return -1;
  386. }
  387. }
  388. if (fwrite(buf, 1, n, ft) != (size_t)n) {
  389. _zip_error_set(error, ZIP_ER_WRITE, errno);
  390. return -1;
  391. }
  392. len -= n;
  393. }
  394. return 0;
  395. }
  396. static int
  397. copy_source(struct zip *za, struct zip_source *src, FILE *ft)
  398. {
  399. char buf[BUFSIZE];
  400. zip_int64_t n;
  401. int ret;
  402. if (zip_source_open(src) < 0) {
  403. _zip_error_set_from_source(&za->error, src);
  404. return -1;
  405. }
  406. ret = 0;
  407. while ((n=zip_source_read(src, buf, sizeof(buf))) > 0) {
  408. if (fwrite(buf, 1, (size_t)n, ft) != (size_t)n) {
  409. _zip_error_set(&za->error, ZIP_ER_WRITE, errno);
  410. ret = -1;
  411. break;
  412. }
  413. }
  414. if (n < 0) {
  415. if (ret == 0)
  416. _zip_error_set_from_source(&za->error, src);
  417. ret = -1;
  418. }
  419. zip_source_close(src);
  420. return ret;
  421. }
  422. static int
  423. write_cdir(struct zip *za, const struct zip_filelist *filelist, zip_uint64_t survivors, FILE *out)
  424. {
  425. off_t cd_start, end;
  426. zip_int64_t size;
  427. uLong crc;
  428. char buf[TORRENT_CRC_LEN+1];
  429. cd_start = ftello(out);
  430. if ((size=_zip_cdir_write(za, filelist, survivors, out)) < 0)
  431. return -1;
  432. end = ftello(out);
  433. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0)
  434. return 0;
  435. /* fix up torrentzip comment */
  436. if (_zip_filerange_crc(out, cd_start, (off_t)size, &crc, &za->error) < 0)
  437. return -1;
  438. snprintf(buf, sizeof(buf), "%08lX", (long)crc);
  439. if (fseeko(out, end-TORRENT_CRC_LEN, SEEK_SET) < 0) {
  440. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  441. return -1;
  442. }
  443. if (fwrite(buf, TORRENT_CRC_LEN, 1, out) != 1) {
  444. _zip_error_set(&za->error, ZIP_ER_WRITE, errno);
  445. return -1;
  446. }
  447. return 0;
  448. }
  449. int
  450. _zip_changed(const struct zip *za, zip_uint64_t *survivorsp)
  451. {
  452. int changed;
  453. zip_uint64_t i, survivors;
  454. changed = 0;
  455. survivors = 0;
  456. if (za->comment_changed || za->ch_flags != za->flags)
  457. changed = 1;
  458. for (i=0; i<za->nentry; i++) {
  459. if (za->entry[i].deleted || za->entry[i].source || (za->entry[i].changes && za->entry[i].changes->changed != 0))
  460. changed = 1;
  461. if (!za->entry[i].deleted)
  462. survivors++;
  463. }
  464. if (survivorsp)
  465. *survivorsp = survivors;
  466. return changed;
  467. }
  468. static char *
  469. _zip_create_temp_output(struct zip *za, FILE **outp)
  470. {
  471. char *temp;
  472. int tfd;
  473. FILE *tfp;
  474. if (za->tempdir) {
  475. if ((temp=(char *)malloc(strlen(za->tempdir)+13)) == NULL) {
  476. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  477. return NULL;
  478. }
  479. sprintf(temp, "%s/.zip.XXXXXX", za->tempdir);
  480. }
  481. else {
  482. if ((temp=(char *)malloc(strlen(za->zn)+8)) == NULL) {
  483. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  484. return NULL;
  485. }
  486. sprintf(temp, "%s.XXXXXX", za->zn);
  487. }
  488. if ((tfd=mkstemp(temp)) == -1) {
  489. _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
  490. free(temp);
  491. return NULL;
  492. }
  493. if ((tfp=fdopen(tfd, "r+b")) == NULL) {
  494. _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
  495. close(tfd);
  496. (void)remove(temp);
  497. free(temp);
  498. return NULL;
  499. }
  500. #ifdef _WIN32
  501. /*
  502. According to Pierre Joye, Windows in some environments per
  503. default creates text files, so force binary mode.
  504. */
  505. _setmode(_fileno(tfp), _O_BINARY );
  506. #endif
  507. *outp = tfp;
  508. return temp;
  509. }
  510. static int
  511. _zip_torrentzip_cmp(const void *a, const void *b)
  512. {
  513. const char *aname = ((const struct zip_filelist *)a)->name;
  514. const char *bname = ((const struct zip_filelist *)b)->name;
  515. if (aname == NULL)
  516. return (bname != NULL) * -1;
  517. else if (bname == NULL)
  518. return 1;
  519. return strcasecmp(aname, bname);
  520. }