archive_write_open_filename.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_open_filename.c 191165 2009-04-17 00:39:35Z kientzle $");
  27. #ifdef HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #ifdef HAVE_FCNTL_H
  34. #include <fcntl.h>
  35. #endif
  36. #ifdef HAVE_STDLIB_H
  37. #include <stdlib.h>
  38. #endif
  39. #ifdef HAVE_STRING_H
  40. #include <string.h>
  41. #endif
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #include "archive.h"
  46. #include "archive_private.h"
  47. #include "archive_string.h"
  48. #ifndef O_BINARY
  49. #define O_BINARY 0
  50. #endif
  51. #ifndef O_CLOEXEC
  52. #define O_CLOEXEC 0
  53. #endif
  54. struct write_file_data {
  55. int fd;
  56. struct archive_mstring filename;
  57. };
  58. static int file_close(struct archive *, void *);
  59. static int file_open(struct archive *, void *);
  60. static ssize_t file_write(struct archive *, void *, const void *buff, size_t);
  61. static int open_filename(struct archive *, int, const void *);
  62. int
  63. archive_write_open_file(struct archive *a, const char *filename)
  64. {
  65. return (archive_write_open_filename(a, filename));
  66. }
  67. int
  68. archive_write_open_filename(struct archive *a, const char *filename)
  69. {
  70. if (filename == NULL || filename[0] == '\0')
  71. return (archive_write_open_fd(a, 1));
  72. return (open_filename(a, 1, filename));
  73. }
  74. int
  75. archive_write_open_filename_w(struct archive *a, const wchar_t *filename)
  76. {
  77. if (filename == NULL || filename[0] == L'\0')
  78. return (archive_write_open_fd(a, 1));
  79. return (open_filename(a, 0, filename));
  80. }
  81. static int
  82. open_filename(struct archive *a, int mbs_fn, const void *filename)
  83. {
  84. struct write_file_data *mine;
  85. int r;
  86. mine = (struct write_file_data *)calloc(1, sizeof(*mine));
  87. if (mine == NULL) {
  88. archive_set_error(a, ENOMEM, "No memory");
  89. return (ARCHIVE_FATAL);
  90. }
  91. if (mbs_fn)
  92. r = archive_mstring_copy_mbs(&mine->filename, filename);
  93. else
  94. r = archive_mstring_copy_wcs(&mine->filename, filename);
  95. if (r < 0) {
  96. if (errno == ENOMEM) {
  97. archive_set_error(a, ENOMEM, "No memory");
  98. return (ARCHIVE_FATAL);
  99. }
  100. if (mbs_fn)
  101. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  102. "Can't convert '%s' to WCS",
  103. (const char *)filename);
  104. else
  105. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  106. "Can't convert '%S' to MBS",
  107. (const wchar_t *)filename);
  108. return (ARCHIVE_FAILED);
  109. }
  110. mine->fd = -1;
  111. return (archive_write_open(a, mine,
  112. file_open, file_write, file_close));
  113. }
  114. static int
  115. file_open(struct archive *a, void *client_data)
  116. {
  117. int flags;
  118. struct write_file_data *mine;
  119. struct stat st;
  120. #if defined(_WIN32) && !defined(__CYGWIN__)
  121. wchar_t *fullpath;
  122. #endif
  123. const wchar_t *wcs;
  124. const char *mbs;
  125. mine = (struct write_file_data *)client_data;
  126. flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC;
  127. /*
  128. * Open the file.
  129. */
  130. mbs = NULL; wcs = NULL;
  131. #if defined(_WIN32) && !defined(__CYGWIN__)
  132. if (archive_mstring_get_wcs(a, &mine->filename, &wcs) != 0) {
  133. if (errno == ENOMEM)
  134. archive_set_error(a, errno, "No memory");
  135. else {
  136. archive_mstring_get_mbs(a, &mine->filename, &mbs);
  137. archive_set_error(a, errno,
  138. "Can't convert '%s' to WCS", mbs);
  139. }
  140. return (ARCHIVE_FATAL);
  141. }
  142. fullpath = __la_win_permissive_name_w(wcs);
  143. if (fullpath != NULL) {
  144. mine->fd = _wopen(fullpath, flags, 0666);
  145. free(fullpath);
  146. } else
  147. mine->fd = _wopen(wcs, flags, 0666);
  148. #else
  149. if (archive_mstring_get_mbs(a, &mine->filename, &mbs) != 0) {
  150. if (errno == ENOMEM)
  151. archive_set_error(a, errno, "No memory");
  152. else {
  153. archive_mstring_get_wcs(a, &mine->filename, &wcs);
  154. archive_set_error(a, errno,
  155. "Can't convert '%S' to MBS", wcs);
  156. }
  157. return (ARCHIVE_FATAL);
  158. }
  159. mine->fd = open(mbs, flags, 0666);
  160. __archive_ensure_cloexec_flag(mine->fd);
  161. #endif
  162. if (mine->fd < 0) {
  163. if (mbs != NULL)
  164. archive_set_error(a, errno, "Failed to open '%s'", mbs);
  165. else
  166. archive_set_error(a, errno, "Failed to open '%S'", wcs);
  167. return (ARCHIVE_FATAL);
  168. }
  169. if (fstat(mine->fd, &st) != 0) {
  170. if (mbs != NULL)
  171. archive_set_error(a, errno, "Couldn't stat '%s'", mbs);
  172. else
  173. archive_set_error(a, errno, "Couldn't stat '%S'", wcs);
  174. return (ARCHIVE_FATAL);
  175. }
  176. /*
  177. * Set up default last block handling.
  178. */
  179. if (archive_write_get_bytes_in_last_block(a) < 0) {
  180. if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
  181. S_ISFIFO(st.st_mode))
  182. /* Pad last block when writing to device or FIFO. */
  183. archive_write_set_bytes_in_last_block(a, 0);
  184. else
  185. /* Don't pad last block otherwise. */
  186. archive_write_set_bytes_in_last_block(a, 1);
  187. }
  188. /*
  189. * If the output file is a regular file, don't add it to
  190. * itself. If it's a device file, it's okay to add the device
  191. * entry to the output archive.
  192. */
  193. if (S_ISREG(st.st_mode))
  194. archive_write_set_skip_file(a, st.st_dev, st.st_ino);
  195. return (ARCHIVE_OK);
  196. }
  197. static ssize_t
  198. file_write(struct archive *a, void *client_data, const void *buff,
  199. size_t length)
  200. {
  201. struct write_file_data *mine;
  202. ssize_t bytesWritten;
  203. mine = (struct write_file_data *)client_data;
  204. for (;;) {
  205. bytesWritten = write(mine->fd, buff, length);
  206. if (bytesWritten <= 0) {
  207. if (errno == EINTR)
  208. continue;
  209. archive_set_error(a, errno, "Write error");
  210. return (-1);
  211. }
  212. return (bytesWritten);
  213. }
  214. }
  215. static int
  216. file_close(struct archive *a, void *client_data)
  217. {
  218. struct write_file_data *mine = (struct write_file_data *)client_data;
  219. (void)a; /* UNUSED */
  220. if (mine->fd >= 0)
  221. close(mine->fd);
  222. archive_mstring_clean(&mine->filename);
  223. free(mine);
  224. return (ARCHIVE_OK);
  225. }