archive_read_open_fd.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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: src/lib/libarchive/archive_read_open_fd.c,v 1.13 2007/06/26 03:06:48 kientzle Exp $");
  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_STDLIB_H
  34. #include <stdlib.h>
  35. #endif
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. #include "archive.h"
  43. struct read_fd_data {
  44. int fd;
  45. size_t block_size;
  46. char can_skip;
  47. void *buffer;
  48. };
  49. static int file_close(struct archive *, void *);
  50. static ssize_t file_read(struct archive *, void *, const void **buff);
  51. #if ARCHIVE_API_VERSION < 2
  52. static ssize_t file_skip(struct archive *, void *, size_t request);
  53. #else
  54. static off_t file_skip(struct archive *, void *, off_t request);
  55. #endif
  56. int
  57. archive_read_open_fd(struct archive *a, int fd, size_t block_size)
  58. {
  59. struct stat st;
  60. struct read_fd_data *mine;
  61. void *b;
  62. if (fstat(fd, &st) != 0) {
  63. archive_set_error(a, errno, "Can't stat fd %d", fd);
  64. return (ARCHIVE_FATAL);
  65. }
  66. mine = (struct read_fd_data *)malloc(sizeof(*mine));
  67. b = malloc(block_size);
  68. if (mine == NULL || b == NULL) {
  69. archive_set_error(a, ENOMEM, "No memory");
  70. free(mine);
  71. free(b);
  72. return (ARCHIVE_FATAL);
  73. }
  74. mine->block_size = block_size;
  75. mine->buffer = b;
  76. mine->fd = fd;
  77. /*
  78. * Skip support is a performance optimization for anything
  79. * that supports lseek(). On FreeBSD, only regular files and
  80. * raw disk devices support lseek() and there's no portable
  81. * way to determine if a device is a raw disk device, so we
  82. * only enable this optimization for regular files.
  83. */
  84. if (S_ISREG(st.st_mode)) {
  85. archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
  86. mine->can_skip = 1;
  87. } else
  88. mine->can_skip = 0;
  89. return (archive_read_open2(a, mine,
  90. NULL, file_read, file_skip, file_close));
  91. }
  92. static ssize_t
  93. file_read(struct archive *a, void *client_data, const void **buff)
  94. {
  95. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  96. ssize_t bytes_read;
  97. *buff = mine->buffer;
  98. bytes_read = read(mine->fd, mine->buffer, mine->block_size);
  99. if (bytes_read < 0) {
  100. archive_set_error(a, errno, "Error reading fd %d", mine->fd);
  101. }
  102. return (bytes_read);
  103. }
  104. #if ARCHIVE_API_VERSION < 2
  105. static ssize_t
  106. file_skip(struct archive *a, void *client_data, size_t request)
  107. #else
  108. static off_t
  109. file_skip(struct archive *a, void *client_data, off_t request)
  110. #endif
  111. {
  112. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  113. off_t old_offset, new_offset;
  114. if (!mine->can_skip)
  115. return (0);
  116. /* Reduce request to the next smallest multiple of block_size */
  117. request = (request / mine->block_size) * mine->block_size;
  118. if (request == 0)
  119. return (0);
  120. /*
  121. * Hurray for lazy evaluation: if the first lseek fails, the second
  122. * one will not be executed.
  123. */
  124. if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
  125. ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
  126. {
  127. /* If seek failed once, it will probably fail again. */
  128. mine->can_skip = 0;
  129. if (errno == ESPIPE)
  130. {
  131. /*
  132. * Failure to lseek() can be caused by the file
  133. * descriptor pointing to a pipe, socket or FIFO.
  134. * Return 0 here, so the compression layer will use
  135. * read()s instead to advance the file descriptor.
  136. * It's slower of course, but works as well.
  137. */
  138. return (0);
  139. }
  140. /*
  141. * There's been an error other than ESPIPE. This is most
  142. * likely caused by a programmer error (too large request)
  143. * or a corrupted archive file.
  144. */
  145. archive_set_error(a, errno, "Error seeking");
  146. return (-1);
  147. }
  148. return (new_offset - old_offset);
  149. }
  150. static int
  151. file_close(struct archive *a, void *client_data)
  152. {
  153. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  154. (void)a; /* UNUSED */
  155. free(mine->buffer);
  156. free(mine);
  157. return (ARCHIVE_OK);
  158. }