PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libarchive/archive_read_open_fd.c

http://www.minix3.org/
C | 186 lines | 119 code | 18 blank | 49 comment | 15 complexity | d4f436bf76e8c87e3dd4f963ea642f46 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  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 can_skip;
  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. #if ARCHIVE_API_VERSION < 2
  58. static ssize_t file_skip(struct archive *, void *, size_t request);
  59. #else
  60. static off_t file_skip(struct archive *, void *, off_t request);
  61. #endif
  62. int
  63. archive_read_open_fd(struct archive *a, int fd, size_t block_size)
  64. {
  65. struct stat st;
  66. struct read_fd_data *mine;
  67. void *b;
  68. archive_clear_error(a);
  69. if (fstat(fd, &st) != 0) {
  70. archive_set_error(a, errno, "Can't stat fd %d", fd);
  71. return (ARCHIVE_FATAL);
  72. }
  73. mine = (struct read_fd_data *)malloc(sizeof(*mine));
  74. b = malloc(block_size);
  75. if (mine == NULL || b == NULL) {
  76. archive_set_error(a, ENOMEM, "No memory");
  77. free(mine);
  78. free(b);
  79. return (ARCHIVE_FATAL);
  80. }
  81. mine->block_size = block_size;
  82. mine->buffer = b;
  83. mine->fd = fd;
  84. /*
  85. * Skip support is a performance optimization for anything
  86. * that supports lseek(). On FreeBSD, only regular files and
  87. * raw disk devices support lseek() and there's no portable
  88. * way to determine if a device is a raw disk device, so we
  89. * only enable this optimization for regular files.
  90. */
  91. if (S_ISREG(st.st_mode)) {
  92. archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
  93. mine->can_skip = 1;
  94. } else
  95. mine->can_skip = 0;
  96. #if defined(__CYGWIN__) || defined(_WIN32)
  97. setmode(mine->fd, O_BINARY);
  98. #endif
  99. return (archive_read_open2(a, mine,
  100. NULL, file_read, file_skip, file_close));
  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. bytes_read = read(mine->fd, mine->buffer, mine->block_size);
  109. if (bytes_read < 0) {
  110. archive_set_error(a, errno, "Error reading fd %d", mine->fd);
  111. }
  112. return (bytes_read);
  113. }
  114. #if ARCHIVE_API_VERSION < 2
  115. static ssize_t
  116. file_skip(struct archive *a, void *client_data, size_t request)
  117. #else
  118. static off_t
  119. file_skip(struct archive *a, void *client_data, off_t request)
  120. #endif
  121. {
  122. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  123. off_t old_offset, new_offset;
  124. if (!mine->can_skip)
  125. return (0);
  126. /* Reduce request to the next smallest multiple of block_size */
  127. request = (request / mine->block_size) * mine->block_size;
  128. if (request == 0)
  129. return (0);
  130. /*
  131. * Hurray for lazy evaluation: if the first lseek fails, the second
  132. * one will not be executed.
  133. */
  134. if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
  135. ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
  136. {
  137. /* If seek failed once, it will probably fail again. */
  138. mine->can_skip = 0;
  139. if (errno == ESPIPE)
  140. {
  141. /*
  142. * Failure to lseek() can be caused by the file
  143. * descriptor pointing to a pipe, socket or FIFO.
  144. * Return 0 here, so the compression layer will use
  145. * read()s instead to advance the file descriptor.
  146. * It's slower of course, but works as well.
  147. */
  148. return (0);
  149. }
  150. /*
  151. * There's been an error other than ESPIPE. This is most
  152. * likely caused by a programmer error (too large request)
  153. * or a corrupted archive file.
  154. */
  155. archive_set_error(a, errno, "Error seeking");
  156. return (-1);
  157. }
  158. return (new_offset - old_offset);
  159. }
  160. static int
  161. file_close(struct archive *a, void *client_data)
  162. {
  163. struct read_fd_data *mine = (struct read_fd_data *)client_data;
  164. (void)a; /* UNUSED */
  165. free(mine->buffer);
  166. free(mine);
  167. return (ARCHIVE_OK);
  168. }