archive_read_open_file.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_file.c,v 1.20 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_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. struct read_FILE_data {
  47. FILE *f;
  48. size_t block_size;
  49. void *buffer;
  50. char can_skip;
  51. };
  52. static int file_close(struct archive *, void *);
  53. static ssize_t file_read(struct archive *, void *, const void **buff);
  54. #if ARCHIVE_API_VERSION < 2
  55. static ssize_t file_skip(struct archive *, void *, size_t request);
  56. #else
  57. static off_t file_skip(struct archive *, void *, off_t request);
  58. #endif
  59. int
  60. archive_read_open_FILE(struct archive *a, FILE *f)
  61. {
  62. struct stat st;
  63. struct read_FILE_data *mine;
  64. size_t block_size = 128 * 1024;
  65. void *b;
  66. mine = (struct read_FILE_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->f = f;
  77. /*
  78. * If we can't fstat() the file, it may just be that it's not
  79. * a file. (FILE * objects can wrap many kinds of I/O
  80. * streams, some of which don't support fileno()).)
  81. */
  82. if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
  83. archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
  84. /* Enable the seek optimization only for regular files. */
  85. mine->can_skip = 1;
  86. } else
  87. mine->can_skip = 0;
  88. return (archive_read_open2(a, mine, NULL, file_read,
  89. file_skip, file_close));
  90. }
  91. static ssize_t
  92. file_read(struct archive *a, void *client_data, const void **buff)
  93. {
  94. struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
  95. size_t bytes_read;
  96. *buff = mine->buffer;
  97. bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
  98. if (bytes_read < mine->block_size && ferror(mine->f)) {
  99. archive_set_error(a, errno, "Error reading file");
  100. }
  101. return (bytes_read);
  102. }
  103. #if ARCHIVE_API_VERSION < 2
  104. static ssize_t
  105. file_skip(struct archive *a, void *client_data, size_t request)
  106. #else
  107. static off_t
  108. file_skip(struct archive *a, void *client_data, off_t request)
  109. #endif
  110. {
  111. struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
  112. (void)a; /* UNUSED */
  113. /*
  114. * If we can't skip, return 0 as the amount we did step and
  115. * the caller will work around by reading and discarding.
  116. */
  117. if (!mine->can_skip)
  118. return (0);
  119. if (request == 0)
  120. return (0);
  121. #if HAVE_FSEEKO
  122. if (fseeko(mine->f, request, SEEK_CUR) != 0)
  123. #else
  124. if (fseek(mine->f, request, SEEK_CUR) != 0)
  125. #endif
  126. {
  127. mine->can_skip = 0;
  128. return (0);
  129. }
  130. return (request);
  131. }
  132. static int
  133. file_close(struct archive *a, void *client_data)
  134. {
  135. struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
  136. (void)a; /* UNUSED */
  137. if (mine->buffer != NULL)
  138. free(mine->buffer);
  139. free(mine);
  140. return (ARCHIVE_OK);
  141. }