archive_read_open_fd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_read_open_fd.c 201103 2009-12-28 03:13:49Z 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_IO_H
  37. #include <io.h>
  38. #endif
  39. #ifdef HAVE_STDLIB_H
  40. #include <stdlib.h>
  41. #endif
  42. #ifdef HAVE_STRING_H
  43. #include <string.h>
  44. #endif
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif
  48. #include "archive.h"
  49. struct read_fd_data {
  50. int fd;
  51. size_t block_size;
  52. char use_lseek;
  53. void *buffer;
  54. };
  55. static int file_close(struct archive *, void *);
  56. static ssize_t file_read(struct archive *, void *, const void **buff);
  57. static int64_t file_seek(struct archive *, void *, int64_t request, int);
  58. static int64_t file_skip(struct archive *, void *, int64_t request);
  59. int
  60. archive_read_open_fd(struct archive *a, int fd, size_t block_size)
  61. {
  62. struct stat st;
  63. struct read_fd_data *mine;
  64. void *b;
  65. archive_clear_error(a);
  66. if (fstat(fd, &st) != 0) {
  67. archive_set_error(a, errno, "Can't stat fd %d", fd);
  68. return (ARCHIVE_FATAL);
  69. }
  70. mine = (struct read_fd_data *)calloc(1, sizeof(*mine));
  71. b = malloc(block_size);
  72. if (mine == NULL || b == NULL) {
  73. archive_set_error(a, ENOMEM, "No memory");
  74. free(mine);
  75. free(b);
  76. return (ARCHIVE_FATAL);
  77. }
  78. mine->block_size = block_size;
  79. mine->buffer = b;
  80. mine->fd = fd;
  81. /*
  82. * Skip support is a performance optimization for anything
  83. * that supports lseek(). On FreeBSD, only regular files and
  84. * raw disk devices support lseek() and there's no portable
  85. * way to determine if a device is a raw disk device, so we
  86. * only enable this optimization for regular files.
  87. */
  88. if (S_ISREG(st.st_mode)) {
  89. archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
  90. mine->use_lseek = 1;
  91. }
  92. #if defined(__CYGWIN__) || defined(_WIN32)
  93. setmode(mine->fd, O_BINARY);
  94. #endif
  95. archive_read_set_read_callback(a, file_read);
  96. archive_read_set_skip_callback(a, file_skip);
  97. archive_read_set_seek_callback(a, file_seek);
  98. archive_read_set_close_callback(a, file_close);
  99. archive_read_set_callback_data(a, mine);
  100. return (archive_read_open1(a));
  101. }
  102. static ssize_t
  103. file_read(struct archive *a, void *client_data, const void **buff)
  104. {
  105. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  106. ssize_t bytes_read;
  107. *buff = mine->buffer;
  108. for (;;) {
  109. bytes_read = read(mine->fd, mine->buffer, mine->block_size);
  110. if (bytes_read < 0) {
  111. if (errno == EINTR)
  112. continue;
  113. archive_set_error(a, errno, "Error reading fd %d",
  114. mine->fd);
  115. }
  116. return (bytes_read);
  117. }
  118. }
  119. static int64_t
  120. file_skip(struct archive *a, void *client_data, int64_t request)
  121. {
  122. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  123. int64_t skip = request;
  124. int64_t old_offset, new_offset;
  125. int skip_bits = sizeof(skip) * 8 - 1; /* off_t is a signed type. */
  126. if (!mine->use_lseek)
  127. return (0);
  128. /* Reduce a request that would overflow the 'skip' variable. */
  129. if (sizeof(request) > sizeof(skip)) {
  130. int64_t max_skip =
  131. (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
  132. if (request > max_skip)
  133. skip = max_skip;
  134. }
  135. /* Reduce request to the next smallest multiple of block_size */
  136. request = (request / mine->block_size) * mine->block_size;
  137. if (request == 0)
  138. return (0);
  139. if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) &&
  140. ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0))
  141. return (new_offset - old_offset);
  142. /* If seek failed once, it will probably fail again. */
  143. mine->use_lseek = 0;
  144. /* Let libarchive recover with read+discard. */
  145. if (errno == ESPIPE)
  146. return (0);
  147. /*
  148. * There's been an error other than ESPIPE. This is most
  149. * likely caused by a programmer error (too large request)
  150. * or a corrupted archive file.
  151. */
  152. archive_set_error(a, errno, "Error seeking");
  153. return (-1);
  154. }
  155. /*
  156. * TODO: Store the offset and use it in the read callback.
  157. */
  158. static int64_t
  159. file_seek(struct archive *a, void *client_data, int64_t request, int whence)
  160. {
  161. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  162. int64_t r;
  163. /* We use off_t here because lseek() is declared that way. */
  164. /* See above for notes about when off_t is less than 64 bits. */
  165. r = lseek(mine->fd, request, whence);
  166. if (r >= 0)
  167. return r;
  168. if (errno == ESPIPE) {
  169. archive_set_error(a, errno,
  170. "A file descriptor(%d) is not seekable(PIPE)", mine->fd);
  171. return (ARCHIVE_FAILED);
  172. } else {
  173. /* If the input is corrupted or truncated, fail. */
  174. archive_set_error(a, errno,
  175. "Error seeking in a file descriptor(%d)", mine->fd);
  176. return (ARCHIVE_FATAL);
  177. }
  178. }
  179. static int
  180. file_close(struct archive *a, void *client_data)
  181. {
  182. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  183. (void)a; /* UNUSED */
  184. free(mine->buffer);
  185. free(mine);
  186. return (ARCHIVE_OK);
  187. }