zip_close.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. zip_close.c -- close zip archive and update changes
  3. Copyright (C) 1999-2015 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 <libzip@nih.at>
  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. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #ifdef _WIN32
  43. #include <io.h>
  44. #include <fcntl.h>
  45. #endif
  46. /* max deflate size increase: size + ceil(size/16k)*5+6 */
  47. #define MAX_DEFLATE_SIZE_32 4293656963u
  48. static int add_data(zip_t *, zip_source_t *, zip_dirent_t *);
  49. static int copy_data(zip_t *, zip_uint64_t);
  50. static int copy_source(zip_t *, zip_source_t *);
  51. static int write_cdir(zip_t *, const zip_filelist_t *, zip_uint64_t);
  52. ZIP_EXTERN int
  53. zip_close(zip_t *za)
  54. {
  55. zip_uint64_t i, j, survivors;
  56. zip_int64_t off;
  57. int error;
  58. zip_filelist_t *filelist;
  59. int changed;
  60. if (za == NULL)
  61. return -1;
  62. changed = _zip_changed(za, &survivors);
  63. /* don't create zip files with no entries */
  64. if (survivors == 0) {
  65. if ((za->open_flags & ZIP_TRUNCATE) || changed) {
  66. if (zip_source_remove(za->src) < 0) {
  67. _zip_error_set_from_source(&za->error, za->src);
  68. return -1;
  69. }
  70. }
  71. zip_discard(za);
  72. return 0;
  73. }
  74. if (!changed) {
  75. zip_discard(za);
  76. return 0;
  77. }
  78. if (survivors > za->nentry) {
  79. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  80. return -1;
  81. }
  82. if ((filelist=(zip_filelist_t *)malloc(sizeof(filelist[0])*(size_t)survivors)) == NULL)
  83. return -1;
  84. /* create list of files with index into original archive */
  85. for (i=j=0; i<za->nentry; i++) {
  86. if (za->entry[i].deleted)
  87. continue;
  88. if (j >= survivors) {
  89. free(filelist);
  90. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  91. return -1;
  92. }
  93. filelist[j].idx = i;
  94. j++;
  95. }
  96. if (j < survivors) {
  97. free(filelist);
  98. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  99. return -1;
  100. }
  101. if (zip_source_begin_write(za->src) < 0) {
  102. _zip_error_set_from_source(&za->error, za->src);
  103. free(filelist);
  104. return -1;
  105. }
  106. error = 0;
  107. for (j=0; j<survivors; j++) {
  108. int new_data;
  109. zip_entry_t *entry;
  110. zip_dirent_t *de;
  111. i = filelist[j].idx;
  112. entry = za->entry+i;
  113. new_data = (ZIP_ENTRY_DATA_CHANGED(entry) || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_COMP_METHOD));
  114. /* create new local directory entry */
  115. if (entry->changes == NULL) {
  116. if ((entry->changes=_zip_dirent_clone(entry->orig)) == NULL) {
  117. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  118. error = 1;
  119. break;
  120. }
  121. }
  122. de = entry->changes;
  123. if (_zip_read_local_ef(za, i) < 0) {
  124. error = 1;
  125. break;
  126. }
  127. if ((off = zip_source_tell_write(za->src)) < 0) {
  128. error = 1;
  129. break;
  130. }
  131. de->offset = (zip_uint64_t)off;
  132. if (new_data) {
  133. zip_source_t *zs;
  134. zs = NULL;
  135. if (!ZIP_ENTRY_DATA_CHANGED(entry)) {
  136. if ((zs=_zip_source_zip_new(za, za, i, ZIP_FL_UNCHANGED, 0, 0, NULL)) == NULL) {
  137. error = 1;
  138. break;
  139. }
  140. }
  141. /* add_data writes dirent */
  142. if (add_data(za, zs ? zs : entry->source, de) < 0) {
  143. error = 1;
  144. if (zs)
  145. zip_source_free(zs);
  146. break;
  147. }
  148. if (zs)
  149. zip_source_free(zs);
  150. }
  151. else {
  152. zip_uint64_t offset;
  153. /* when copying data, all sizes are known -> no data descriptor needed */
  154. de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR;
  155. if (_zip_dirent_write(za, de, ZIP_FL_LOCAL) < 0) {
  156. error = 1;
  157. break;
  158. }
  159. if ((offset=_zip_file_get_offset(za, i, &za->error)) == 0) {
  160. error = 1;
  161. break;
  162. }
  163. if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) {
  164. _zip_error_set_from_source(&za->error, za->src);
  165. error = 1;
  166. break;
  167. }
  168. if (copy_data(za, de->comp_size) < 0) {
  169. error = 1;
  170. break;
  171. }
  172. }
  173. }
  174. if (!error) {
  175. if (write_cdir(za, filelist, survivors) < 0)
  176. error = 1;
  177. }
  178. free(filelist);
  179. if (!error) {
  180. if (zip_source_commit_write(za->src) != 0) {
  181. _zip_error_set_from_source(&za->error, za->src);
  182. error = 1;
  183. }
  184. }
  185. if (error) {
  186. zip_source_rollback_write(za->src);
  187. return -1;
  188. }
  189. zip_discard(za);
  190. return 0;
  191. }
  192. static int
  193. add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de)
  194. {
  195. zip_int64_t offstart, offdata, offend;
  196. struct zip_stat st;
  197. zip_source_t *s2;
  198. int ret;
  199. int is_zip64;
  200. zip_flags_t flags;
  201. if (zip_source_stat(src, &st) < 0) {
  202. _zip_error_set_from_source(&za->error, src);
  203. return -1;
  204. }
  205. if ((st.valid & ZIP_STAT_COMP_METHOD) == 0) {
  206. st.valid |= ZIP_STAT_COMP_METHOD;
  207. st.comp_method = ZIP_CM_STORE;
  208. }
  209. if (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != ZIP_CM_STORE)
  210. de->comp_method = st.comp_method;
  211. else if (de->comp_method == ZIP_CM_STORE && (st.valid & ZIP_STAT_SIZE)) {
  212. st.valid |= ZIP_STAT_COMP_SIZE;
  213. st.comp_size = st.size;
  214. }
  215. else {
  216. /* we'll recompress */
  217. st.valid &= ~ZIP_STAT_COMP_SIZE;
  218. }
  219. flags = ZIP_EF_LOCAL;
  220. if ((st.valid & ZIP_STAT_SIZE) == 0)
  221. flags |= ZIP_FL_FORCE_ZIP64;
  222. else {
  223. de->uncomp_size = st.size;
  224. if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) {
  225. if (( ((de->comp_method == ZIP_CM_DEFLATE || ZIP_CM_IS_DEFAULT(de->comp_method)) && st.size > MAX_DEFLATE_SIZE_32)
  226. || (de->comp_method != ZIP_CM_STORE && de->comp_method != ZIP_CM_DEFLATE && !ZIP_CM_IS_DEFAULT(de->comp_method))))
  227. flags |= ZIP_FL_FORCE_ZIP64;
  228. }
  229. else
  230. de->comp_size = st.comp_size;
  231. }
  232. if ((offstart = zip_source_tell_write(za->src)) < 0) {
  233. return -1;
  234. }
  235. /* as long as we don't support non-seekable output, clear data descriptor bit */
  236. de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR;
  237. if ((is_zip64=_zip_dirent_write(za, de, flags)) < 0)
  238. return -1;
  239. if (st.comp_method == ZIP_CM_STORE || (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != de->comp_method)) {
  240. zip_source_t *s_store, *s_crc;
  241. zip_compression_implementation comp_impl;
  242. if (st.comp_method != ZIP_CM_STORE) {
  243. if ((comp_impl=_zip_get_compression_implementation(st.comp_method)) == NULL) {
  244. zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
  245. return -1;
  246. }
  247. if ((s_store=comp_impl(za, src, st.comp_method, ZIP_CODEC_DECODE)) == NULL) {
  248. /* error set by comp_impl */
  249. return -1;
  250. }
  251. }
  252. else {
  253. /* to have the same reference count to src as in the case where it's not stored */
  254. zip_source_keep(src);
  255. s_store = src;
  256. }
  257. s_crc = zip_source_crc(za, s_store, 0);
  258. zip_source_free(s_store);
  259. if (s_crc == NULL) {
  260. return -1;
  261. }
  262. if (de->comp_method != ZIP_CM_STORE && ((st.valid & ZIP_STAT_SIZE) == 0 || st.size != 0)) {
  263. if ((comp_impl=_zip_get_compression_implementation(de->comp_method)) == NULL) {
  264. zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
  265. zip_source_free(s_crc);
  266. return -1;
  267. }
  268. s2 = comp_impl(za, s_crc, de->comp_method, ZIP_CODEC_ENCODE);
  269. zip_source_free(s_crc);
  270. if (s2 == NULL) {
  271. return -1;
  272. }
  273. }
  274. else {
  275. s2 = s_crc;
  276. }
  277. }
  278. else {
  279. zip_source_keep(src);
  280. s2 = src;
  281. }
  282. if ((offdata = zip_source_tell_write(za->src)) < 0) {
  283. return -1;
  284. }
  285. ret = copy_source(za, s2);
  286. if (zip_source_stat(s2, &st) < 0)
  287. ret = -1;
  288. zip_source_free(s2);
  289. if (ret < 0)
  290. return -1;
  291. if ((offend = zip_source_tell_write(za->src)) < 0) {
  292. return -1;
  293. }
  294. if (zip_source_seek_write(za->src, offstart, SEEK_SET) < 0) {
  295. _zip_error_set_from_source(&za->error, za->src);
  296. return -1;
  297. }
  298. if ((st.valid & (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) {
  299. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  300. return -1;
  301. }
  302. if ((de->changed & ZIP_DIRENT_LAST_MOD) == 0) {
  303. if (st.valid & ZIP_STAT_MTIME)
  304. de->last_mod = st.mtime;
  305. else
  306. time(&de->last_mod);
  307. }
  308. de->comp_method = st.comp_method;
  309. de->crc = st.crc;
  310. de->uncomp_size = st.size;
  311. de->comp_size = (zip_uint64_t)(offend - offdata);
  312. if ((ret=_zip_dirent_write(za, de, flags)) < 0)
  313. return -1;
  314. if (is_zip64 != ret) {
  315. /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */
  316. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  317. return -1;
  318. }
  319. if (zip_source_seek_write(za->src, offend, SEEK_SET) < 0) {
  320. _zip_error_set_from_source(&za->error, za->src);
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. static int
  326. copy_data(zip_t *za, zip_uint64_t len)
  327. {
  328. zip_uint8_t buf[BUFSIZE];
  329. size_t n;
  330. while (len > 0) {
  331. n = len > sizeof(buf) ? sizeof(buf) : len;
  332. if (_zip_read(za->src, buf, n, &za->error) < 0) {
  333. return -1;
  334. }
  335. if (_zip_write(za, buf, n) < 0) {
  336. return -1;
  337. }
  338. len -= n;
  339. }
  340. return 0;
  341. }
  342. static int
  343. copy_source(zip_t *za, zip_source_t *src)
  344. {
  345. zip_uint8_t buf[BUFSIZE];
  346. zip_int64_t n;
  347. int ret;
  348. if (zip_source_open(src) < 0) {
  349. _zip_error_set_from_source(&za->error, src);
  350. return -1;
  351. }
  352. ret = 0;
  353. while ((n=zip_source_read(src, buf, sizeof(buf))) > 0) {
  354. if (_zip_write(za, buf, (zip_uint64_t)n) < 0) {
  355. ret = -1;
  356. break;
  357. }
  358. }
  359. if (n < 0) {
  360. _zip_error_set_from_source(&za->error, src);
  361. ret = -1;
  362. }
  363. zip_source_close(src);
  364. return ret;
  365. }
  366. static int
  367. write_cdir(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors)
  368. {
  369. zip_int64_t cd_start, end, size;
  370. if ((cd_start = zip_source_tell_write(za->src)) < 0) {
  371. return -1;
  372. }
  373. if ((size=_zip_cdir_write(za, filelist, survivors)) < 0) {
  374. return -1;
  375. }
  376. if ((end = zip_source_tell_write(za->src)) < 0) {
  377. return -1;
  378. }
  379. return 0;
  380. }
  381. int
  382. _zip_changed(const zip_t *za, zip_uint64_t *survivorsp)
  383. {
  384. int changed;
  385. zip_uint64_t i, survivors;
  386. changed = 0;
  387. survivors = 0;
  388. if (za->comment_changed || za->ch_flags != za->flags)
  389. changed = 1;
  390. for (i=0; i<za->nentry; i++) {
  391. if (za->entry[i].deleted || za->entry[i].source || (za->entry[i].changes && za->entry[i].changes->changed != 0))
  392. changed = 1;
  393. if (!za->entry[i].deleted)
  394. survivors++;
  395. }
  396. if (survivorsp)
  397. *survivorsp = survivors;
  398. return changed;
  399. }