zip_source_file_stdio_named.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. zip_source_file_stdio_named.c -- source for stdio file opened by name
  3. Copyright (C) 1999-2020 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 "zip_source_file.h"
  32. #include "zip_source_file_stdio.h"
  33. #include <stdlib.h>
  34. #include <sys/stat.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #ifdef HAVE_CLONEFILE
  39. #include <sys/attr.h>
  40. #include <sys/clonefile.h>
  41. #define CAN_CLONE
  42. #endif
  43. #ifdef HAVE_FICLONERANGE
  44. #include <linux/fs.h>
  45. #include <sys/ioctl.h>
  46. #define CAN_CLONE
  47. #endif
  48. static zip_int64_t _zip_stdio_op_commit_write(zip_source_file_context_t *ctx);
  49. static zip_int64_t _zip_stdio_op_create_temp_output(zip_source_file_context_t *ctx);
  50. #ifdef CAN_CLONE
  51. static zip_int64_t _zip_stdio_op_create_temp_output_cloning(zip_source_file_context_t *ctx, zip_uint64_t offset);
  52. #endif
  53. static bool _zip_stdio_op_open(zip_source_file_context_t *ctx);
  54. static zip_int64_t _zip_stdio_op_remove(zip_source_file_context_t *ctx);
  55. static void _zip_stdio_op_rollback_write(zip_source_file_context_t *ctx);
  56. static char *_zip_stdio_op_strdup(zip_source_file_context_t *ctx, const char *string);
  57. static zip_int64_t _zip_stdio_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len);
  58. /* clang-format off */
  59. static zip_source_file_operations_t ops_stdio_named = {
  60. _zip_stdio_op_close,
  61. _zip_stdio_op_commit_write,
  62. _zip_stdio_op_create_temp_output,
  63. #ifdef CAN_CLONE
  64. _zip_stdio_op_create_temp_output_cloning,
  65. #else
  66. NULL,
  67. #endif
  68. _zip_stdio_op_open,
  69. _zip_stdio_op_read,
  70. _zip_stdio_op_remove,
  71. _zip_stdio_op_rollback_write,
  72. _zip_stdio_op_seek,
  73. _zip_stdio_op_stat,
  74. _zip_stdio_op_strdup,
  75. _zip_stdio_op_tell,
  76. _zip_stdio_op_write
  77. };
  78. /* clang-format on */
  79. ZIP_EXTERN zip_source_t *
  80. zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) {
  81. if (za == NULL)
  82. return NULL;
  83. return zip_source_file_create(fname, start, len, &za->error);
  84. }
  85. ZIP_EXTERN zip_source_t *
  86. zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) {
  87. if (fname == NULL || length < -1) {
  88. zip_error_set(error, ZIP_ER_INVAL, 0);
  89. return NULL;
  90. }
  91. return zip_source_file_common_new(fname, NULL, start, length, NULL, &ops_stdio_named, NULL, error);
  92. }
  93. static zip_int64_t
  94. _zip_stdio_op_commit_write(zip_source_file_context_t *ctx) {
  95. if (fclose(ctx->fout) < 0) {
  96. zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
  97. return -1;
  98. }
  99. if (rename(ctx->tmpname, ctx->fname) < 0) {
  100. zip_error_set(&ctx->error, ZIP_ER_RENAME, errno);
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. static zip_int64_t
  106. _zip_stdio_op_create_temp_output(zip_source_file_context_t *ctx) {
  107. char *temp;
  108. int tfd;
  109. int mode;
  110. FILE *tfp;
  111. struct stat st;
  112. if ((temp = (char *)malloc(strlen(ctx->fname) + 8)) == NULL) {
  113. zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
  114. return -1;
  115. }
  116. if (stat(ctx->fname, &st) == 0) {
  117. mode = st.st_mode;
  118. }
  119. else {
  120. mode = -1;
  121. }
  122. sprintf(temp, "%s.XXXXXX", ctx->fname);
  123. if ((tfd = _zip_mkstempm(temp, mode)) == -1) {
  124. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  125. free(temp);
  126. return -1;
  127. }
  128. if ((tfp = fdopen(tfd, "r+b")) == NULL) {
  129. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  130. close(tfd);
  131. (void)remove(temp);
  132. free(temp);
  133. return -1;
  134. }
  135. ctx->fout = tfp;
  136. ctx->tmpname = temp;
  137. return 0;
  138. }
  139. #ifdef CAN_CLONE
  140. static zip_int64_t
  141. _zip_stdio_op_create_temp_output_cloning(zip_source_file_context_t *ctx, zip_uint64_t offset) {
  142. char *temp;
  143. FILE *tfp;
  144. if (offset > ZIP_OFF_MAX) {
  145. zip_error_set(&ctx->error, ZIP_ER_SEEK, E2BIG);
  146. return -1;
  147. }
  148. if ((temp = (char *)malloc(strlen(ctx->fname) + 8)) == NULL) {
  149. zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
  150. return -1;
  151. }
  152. sprintf(temp, "%s.XXXXXX", ctx->fname);
  153. #ifdef HAVE_CLONEFILE
  154. #ifndef __clang_analyzer__
  155. /* we can't use mkstemp, since clonefile insists on creating the file */
  156. if (mktemp(temp) == NULL) {
  157. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  158. free(temp);
  159. return -1;
  160. }
  161. #endif
  162. if (clonefile(ctx->fname, temp, 0) < 0) {
  163. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  164. free(temp);
  165. return -1;
  166. }
  167. if ((tfp = _zip_fopen_close_on_exec(temp, true)) == NULL) {
  168. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  169. (void)remove(temp);
  170. free(temp);
  171. return -1;
  172. }
  173. #else
  174. {
  175. int fd;
  176. struct file_clone_range range;
  177. struct stat st;
  178. if (fstat(fileno(ctx->f), &st) < 0) {
  179. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  180. free(temp);
  181. return -1;
  182. }
  183. if ((fd = mkstemp(temp)) < 0) {
  184. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  185. free(temp);
  186. return -1;
  187. }
  188. range.src_fd = fileno(ctx->f);
  189. range.src_offset = 0;
  190. range.src_length = ((offset + st.st_blksize - 1) / st.st_blksize) * st.st_blksize;
  191. if (range.src_length > st.st_size) {
  192. range.src_length = 0;
  193. }
  194. range.dest_offset = 0;
  195. if (ioctl(fd, FICLONERANGE, &range) < 0) {
  196. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  197. (void)close(fd);
  198. (void)remove(temp);
  199. free(temp);
  200. return -1;
  201. }
  202. if ((tfp = fdopen(fd, "r+b")) == NULL) {
  203. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  204. (void)close(fd);
  205. (void)remove(temp);
  206. free(temp);
  207. return -1;
  208. }
  209. }
  210. #endif
  211. if (ftruncate(fileno(tfp), (off_t)offset) < 0) {
  212. (void)fclose(tfp);
  213. (void)remove(temp);
  214. free(temp);
  215. return -1;
  216. }
  217. if (fseeko(tfp, (off_t)offset, SEEK_SET) < 0) {
  218. (void)fclose(tfp);
  219. (void)remove(temp);
  220. free(temp);
  221. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
  222. }
  223. ctx->fout = tfp;
  224. ctx->tmpname = temp;
  225. return 0;
  226. }
  227. #endif
  228. static bool
  229. _zip_stdio_op_open(zip_source_file_context_t *ctx) {
  230. if ((ctx->f = _zip_fopen_close_on_exec(ctx->fname, false)) == NULL) {
  231. zip_error_set(&ctx->error, ZIP_ER_OPEN, errno);
  232. return false;
  233. }
  234. return true;
  235. }
  236. static zip_int64_t
  237. _zip_stdio_op_remove(zip_source_file_context_t *ctx) {
  238. if (remove(ctx->fname) < 0) {
  239. zip_error_set(&ctx->error, ZIP_ER_REMOVE, errno);
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. static void
  245. _zip_stdio_op_rollback_write(zip_source_file_context_t *ctx) {
  246. if (ctx->fout) {
  247. fclose(ctx->fout);
  248. }
  249. (void)remove(ctx->tmpname);
  250. }
  251. static char *
  252. _zip_stdio_op_strdup(zip_source_file_context_t *ctx, const char *string) {
  253. return strdup(string);
  254. }
  255. static zip_int64_t
  256. _zip_stdio_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len) {
  257. size_t ret;
  258. clearerr((FILE *)ctx->fout);
  259. ret = fwrite(data, 1, len, (FILE *)ctx->fout);
  260. if (ret != len || ferror((FILE *)ctx->fout)) {
  261. zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
  262. return -1;
  263. }
  264. return (zip_int64_t)ret;
  265. }