zip_source_filep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. zip_source_filep.c -- create data source from FILE *
  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 <sys/stat.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "zipint.h"
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #ifdef _WIN32
  39. /* WIN32 needs <fcntl.h> for _O_BINARY */
  40. #include <fcntl.h>
  41. #endif
  42. /* Windows sys/types.h does not provide these */
  43. #ifndef S_ISREG
  44. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  45. #endif
  46. #if defined(S_IXUSR) && defined(S_IRWXG) && defined(S_IRWXO)
  47. #define _SAFE_MASK (S_IXUSR | S_IRWXG | S_IRWXO)
  48. #elif defined(_S_IWRITE)
  49. #define _SAFE_MASK (_S_IWRITE)
  50. #else
  51. #error do not know safe values for umask, please report this
  52. #endif
  53. #ifdef _MSC_VER
  54. /* MSVC doesn't have mode_t */
  55. typedef int mode_t;
  56. #endif
  57. struct read_file {
  58. zip_error_t error; /* last error information */
  59. zip_int64_t supports;
  60. /* reading */
  61. char *fname; /* name of file to read from */
  62. FILE *f; /* file to read from */
  63. struct zip_stat st; /* stat information passed in */
  64. zip_uint64_t start; /* start offset of data to read */
  65. zip_uint64_t end; /* end offset of data to read, 0 for up to EOF */
  66. zip_uint64_t current; /* current offset */
  67. /* writing */
  68. char *tmpname;
  69. FILE *fout;
  70. };
  71. static zip_int64_t read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd);
  72. static int create_temp_output(struct read_file *ctx);
  73. static int _zip_fseek_u(FILE *f, zip_uint64_t offset, int whence, zip_error_t *error);
  74. static int _zip_fseek(FILE *f, zip_int64_t offset, int whence, zip_error_t *error);
  75. ZIP_EXTERN zip_source_t *
  76. zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len)
  77. {
  78. if (za == NULL)
  79. return NULL;
  80. return zip_source_filep_create(file, start, len, &za->error);
  81. }
  82. ZIP_EXTERN zip_source_t *
  83. zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
  84. {
  85. if (file == NULL || length < -1) {
  86. zip_error_set(error, ZIP_ER_INVAL, 0);
  87. return NULL;
  88. }
  89. return _zip_source_file_or_p(NULL, file, start, length, NULL, error);
  90. }
  91. zip_source_t *
  92. _zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_error_t *error)
  93. {
  94. struct read_file *ctx;
  95. zip_source_t *zs;
  96. if (file == NULL && fname == NULL) {
  97. zip_error_set(error, ZIP_ER_INVAL, 0);
  98. return NULL;
  99. }
  100. if ((ctx=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
  101. zip_error_set(error, ZIP_ER_MEMORY, 0);
  102. return NULL;
  103. }
  104. ctx->fname = NULL;
  105. if (fname) {
  106. if ((ctx->fname=strdup(fname)) == NULL) {
  107. zip_error_set(error, ZIP_ER_MEMORY, 0);
  108. free(ctx);
  109. return NULL;
  110. }
  111. }
  112. ctx->f = file;
  113. ctx->start = start;
  114. ctx->end = (len < 0 ? 0 : start+(zip_uint64_t)len);
  115. if (st) {
  116. memcpy(&ctx->st, st, sizeof(ctx->st));
  117. ctx->st.name = NULL;
  118. ctx->st.valid &= ~ZIP_STAT_NAME;
  119. }
  120. else {
  121. zip_stat_init(&ctx->st);
  122. }
  123. ctx->tmpname = NULL;
  124. ctx->fout = NULL;
  125. zip_error_init(&ctx->error);
  126. ctx->supports = ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, -1);
  127. if (ctx->fname) {
  128. struct stat sb;
  129. if (stat(ctx->fname, &sb) < 0 || S_ISREG(sb.st_mode)) {
  130. ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
  131. }
  132. }
  133. else if (fseeko(ctx->f, 0, SEEK_CUR) == 0) {
  134. ctx->supports = ZIP_SOURCE_SUPPORTS_SEEKABLE;
  135. }
  136. if ((zs=zip_source_function_create(read_file, ctx, error)) == NULL) {
  137. free(ctx->fname);
  138. free(ctx);
  139. return NULL;
  140. }
  141. return zs;
  142. }
  143. static int
  144. create_temp_output(struct read_file *ctx)
  145. {
  146. char *temp;
  147. int tfd;
  148. mode_t mask;
  149. FILE *tfp;
  150. if ((temp=(char *)malloc(strlen(ctx->fname)+8)) == NULL) {
  151. zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
  152. return -1;
  153. }
  154. sprintf(temp, "%s.XXXXXX", ctx->fname);
  155. mask = umask(_SAFE_MASK);
  156. if ((tfd=mkstemp(temp)) == -1) {
  157. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  158. umask(mask);
  159. free(temp);
  160. return -1;
  161. }
  162. umask(mask);
  163. if ((tfp=fdopen(tfd, "r+b")) == NULL) {
  164. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  165. close(tfd);
  166. (void)remove(temp);
  167. free(temp);
  168. return -1;
  169. }
  170. #ifdef _WIN32
  171. /*
  172. According to Pierre Joye, Windows in some environments per
  173. default creates text files, so force binary mode.
  174. */
  175. _setmode(_fileno(tfp), _O_BINARY );
  176. #endif
  177. ctx->fout = tfp;
  178. ctx->tmpname = temp;
  179. return 0;
  180. }
  181. static zip_int64_t
  182. read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
  183. {
  184. struct read_file *ctx;
  185. char *buf;
  186. zip_uint64_t n;
  187. size_t i;
  188. ctx = (struct read_file *)state;
  189. buf = (char *)data;
  190. switch (cmd) {
  191. case ZIP_SOURCE_BEGIN_WRITE:
  192. if (ctx->fname == NULL) {
  193. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  194. return -1;
  195. }
  196. return create_temp_output(ctx);
  197. case ZIP_SOURCE_COMMIT_WRITE: {
  198. mode_t mask;
  199. if (fclose(ctx->fout) < 0) {
  200. ctx->fout = NULL;
  201. zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
  202. }
  203. ctx->fout = NULL;
  204. if (rename(ctx->tmpname, ctx->fname) < 0) {
  205. zip_error_set(&ctx->error, ZIP_ER_RENAME, errno);
  206. return -1;
  207. }
  208. mask = umask(022);
  209. umask(mask);
  210. /* not much we can do if chmod fails except make the whole commit fail */
  211. (void)chmod(ctx->fname, 0666&~mask);
  212. free(ctx->tmpname);
  213. ctx->tmpname = NULL;
  214. return 0;
  215. }
  216. case ZIP_SOURCE_CLOSE:
  217. if (ctx->fname) {
  218. fclose(ctx->f);
  219. ctx->f = NULL;
  220. }
  221. return 0;
  222. case ZIP_SOURCE_ERROR:
  223. return zip_error_to_data(&ctx->error, data, len);
  224. case ZIP_SOURCE_FREE:
  225. free(ctx->fname);
  226. free(ctx->tmpname);
  227. if (ctx->f)
  228. fclose(ctx->f);
  229. free(ctx);
  230. return 0;
  231. case ZIP_SOURCE_OPEN:
  232. if (ctx->fname) {
  233. if ((ctx->f=fopen(ctx->fname, "rb")) == NULL) {
  234. zip_error_set(&ctx->error, ZIP_ER_OPEN, errno);
  235. return -1;
  236. }
  237. }
  238. if (ctx->start > 0) {
  239. if (_zip_fseek_u(ctx->f, ctx->start, SEEK_SET, &ctx->error) < 0) {
  240. return -1;
  241. }
  242. }
  243. ctx->current = ctx->start;
  244. return 0;
  245. case ZIP_SOURCE_READ:
  246. if (ctx->end > 0) {
  247. n = ctx->end-ctx->current;
  248. if (n > len) {
  249. n = len;
  250. }
  251. }
  252. else {
  253. n = len;
  254. }
  255. if (n > SIZE_MAX)
  256. n = SIZE_MAX;
  257. if ((i=fread(buf, 1, (size_t)n, ctx->f)) == 0) {
  258. if (ferror(ctx->f)) {
  259. zip_error_set(&ctx->error, ZIP_ER_READ, errno);
  260. return -1;
  261. }
  262. }
  263. ctx->current += i;
  264. return (zip_int64_t)i;
  265. case ZIP_SOURCE_REMOVE:
  266. if (remove(ctx->fname) < 0) {
  267. zip_error_set(&ctx->error, ZIP_ER_REMOVE, errno);
  268. return -1;
  269. }
  270. return 0;
  271. case ZIP_SOURCE_ROLLBACK_WRITE:
  272. if (ctx->fout) {
  273. fclose(ctx->fout);
  274. ctx->fout = NULL;
  275. }
  276. (void)remove(ctx->tmpname);
  277. free(ctx->tmpname);
  278. ctx->tmpname = NULL;
  279. return 0;
  280. case ZIP_SOURCE_SEEK: {
  281. zip_int64_t new_current;
  282. int need_seek;
  283. zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
  284. if (args == NULL)
  285. return -1;
  286. need_seek = 1;
  287. switch (args->whence) {
  288. case SEEK_SET:
  289. new_current = args->offset;
  290. break;
  291. case SEEK_END:
  292. if (ctx->end == 0) {
  293. if (_zip_fseek(ctx->f, args->offset, SEEK_END, &ctx->error) < 0) {
  294. return -1;
  295. }
  296. if ((new_current = ftello(ctx->f)) < 0) {
  297. zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
  298. return -1;
  299. }
  300. need_seek = 0;
  301. }
  302. else {
  303. new_current = (zip_int64_t)ctx->end + args->offset;
  304. }
  305. break;
  306. case SEEK_CUR:
  307. new_current = (zip_int64_t)ctx->current + args->offset;
  308. break;
  309. default:
  310. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  311. return -1;
  312. }
  313. if (new_current < 0 || (zip_uint64_t)new_current < ctx->start || (ctx->end != 0 && (zip_uint64_t)new_current > ctx->end)) {
  314. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  315. return -1;
  316. }
  317. ctx->current = (zip_uint64_t)new_current;
  318. if (need_seek) {
  319. if (_zip_fseek_u(ctx->f, ctx->current, SEEK_SET, &ctx->error) < 0) {
  320. return -1;
  321. }
  322. }
  323. return 0;
  324. }
  325. case ZIP_SOURCE_SEEK_WRITE: {
  326. zip_source_args_seek_t *args;
  327. args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
  328. if (args == NULL) {
  329. return -1;
  330. }
  331. if (_zip_fseek(ctx->fout, args->offset, args->whence, &ctx->error) < 0) {
  332. return -1;
  333. }
  334. return 0;
  335. }
  336. case ZIP_SOURCE_STAT: {
  337. if (len < sizeof(ctx->st))
  338. return -1;
  339. if (ctx->st.valid != 0)
  340. memcpy(data, &ctx->st, sizeof(ctx->st));
  341. else {
  342. zip_stat_t *st;
  343. struct stat fst;
  344. int err;
  345. if (ctx->f)
  346. err = fstat(fileno(ctx->f), &fst);
  347. else
  348. err = stat(ctx->fname, &fst);
  349. if (err != 0) {
  350. zip_error_set(&ctx->error, ZIP_ER_READ, errno);
  351. return -1;
  352. }
  353. st = (zip_stat_t *)data;
  354. zip_stat_init(st);
  355. st->mtime = fst.st_mtime;
  356. st->valid |= ZIP_STAT_MTIME;
  357. if (ctx->end != 0) {
  358. st->size = ctx->end - ctx->start;
  359. st->valid |= ZIP_STAT_SIZE;
  360. }
  361. else if ((fst.st_mode&S_IFMT) == S_IFREG) {
  362. st->size = (zip_uint64_t)fst.st_size;
  363. st->valid |= ZIP_STAT_SIZE;
  364. }
  365. }
  366. return sizeof(ctx->st);
  367. }
  368. case ZIP_SOURCE_SUPPORTS:
  369. return ctx->supports;
  370. case ZIP_SOURCE_TELL:
  371. return (zip_int64_t)ctx->current;
  372. case ZIP_SOURCE_TELL_WRITE:
  373. {
  374. off_t ret = ftello(ctx->fout);
  375. if (ret < 0) {
  376. zip_error_set(&ctx->error, ZIP_ER_TELL, errno);
  377. return -1;
  378. }
  379. return ret;
  380. }
  381. case ZIP_SOURCE_WRITE:
  382. {
  383. size_t ret;
  384. clearerr(ctx->fout);
  385. ret = fwrite(data, 1, len, ctx->fout);
  386. if (ret != len || ferror(ctx->fout)) {
  387. zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
  388. return -1;
  389. }
  390. return (zip_int64_t)ret;
  391. }
  392. default:
  393. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  394. return -1;
  395. }
  396. }
  397. static int
  398. _zip_fseek_u(FILE *f, zip_uint64_t offset, int whence, zip_error_t *error)
  399. {
  400. if (offset > ZIP_INT64_MAX) {
  401. zip_error_set(error, ZIP_ER_SEEK, EOVERFLOW);
  402. return -1;
  403. }
  404. return _zip_fseek(f, (zip_int64_t)offset, whence, error);
  405. }
  406. static int
  407. _zip_fseek(FILE *f, zip_int64_t offset, int whence, zip_error_t *error)
  408. {
  409. if (offset > ZIP_FSEEK_MAX || offset < ZIP_FSEEK_MIN) {
  410. zip_error_set(error, ZIP_ER_SEEK, EOVERFLOW);
  411. return -1;
  412. }
  413. if (fseeko(f, (off_t)offset, whence) < 0) {
  414. zip_error_set(error, ZIP_ER_SEEK, errno);
  415. return -1;
  416. }
  417. return 0;
  418. }