archive_read_extract.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_extract.c,v 1.61 2008/05/26 17:00:22 kientzle Exp $");
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.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. #include "archive.h"
  40. #include "archive_private.h"
  41. #include "archive_read_private.h"
  42. #include "archive_write_disk_private.h"
  43. struct extract {
  44. struct archive *ad; /* archive_write_disk object */
  45. /* Progress function invoked during extract. */
  46. void (*extract_progress)(void *);
  47. void *extract_progress_user_data;
  48. };
  49. static int archive_read_extract_cleanup(struct archive_read *);
  50. static int copy_data(struct archive *ar, struct archive *aw);
  51. static struct extract *get_extract(struct archive_read *);
  52. static struct extract *
  53. get_extract(struct archive_read *a)
  54. {
  55. /* If we haven't initialized, do it now. */
  56. /* This also sets up a lot of global state. */
  57. if (a->extract == NULL) {
  58. a->extract = (struct extract *)malloc(sizeof(*a->extract));
  59. if (a->extract == NULL) {
  60. archive_set_error(&a->archive, ENOMEM, "Can't extract");
  61. return (NULL);
  62. }
  63. memset(a->extract, 0, sizeof(*a->extract));
  64. a->extract->ad = archive_write_disk_new();
  65. if (a->extract->ad == NULL) {
  66. archive_set_error(&a->archive, ENOMEM, "Can't extract");
  67. return (NULL);
  68. }
  69. archive_write_disk_set_standard_lookup(a->extract->ad);
  70. a->cleanup_archive_extract = archive_read_extract_cleanup;
  71. }
  72. return (a->extract);
  73. }
  74. int
  75. archive_read_extract(struct archive *_a, struct archive_entry *entry, int flags)
  76. {
  77. struct extract *extract;
  78. extract = get_extract((struct archive_read *)_a);
  79. if (extract == NULL)
  80. return (ARCHIVE_FATAL);
  81. archive_write_disk_set_options(extract->ad, flags);
  82. return (archive_read_extract2(_a, entry, extract->ad));
  83. }
  84. int
  85. archive_read_extract2(struct archive *_a, struct archive_entry *entry,
  86. struct archive *ad)
  87. {
  88. struct archive_read *a = (struct archive_read *)_a;
  89. int r, r2;
  90. /* Set up for this particular entry. */
  91. archive_write_disk_set_skip_file(ad,
  92. a->skip_file_dev, a->skip_file_ino);
  93. r = archive_write_header(ad, entry);
  94. if (r < ARCHIVE_WARN)
  95. r = ARCHIVE_WARN;
  96. if (r != ARCHIVE_OK)
  97. /* If _write_header failed, copy the error. */
  98. archive_copy_error(&a->archive, ad);
  99. else
  100. /* Otherwise, pour data into the entry. */
  101. r = copy_data(_a, ad);
  102. r2 = archive_write_finish_entry(ad);
  103. if (r2 < ARCHIVE_WARN)
  104. r2 = ARCHIVE_WARN;
  105. /* Use the first message. */
  106. if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
  107. archive_copy_error(&a->archive, ad);
  108. /* Use the worst error return. */
  109. if (r2 < r)
  110. r = r2;
  111. return (r);
  112. }
  113. void
  114. archive_read_extract_set_progress_callback(struct archive *_a,
  115. void (*progress_func)(void *), void *user_data)
  116. {
  117. struct archive_read *a = (struct archive_read *)_a;
  118. struct extract *extract = get_extract(a);
  119. if (extract != NULL) {
  120. extract->extract_progress = progress_func;
  121. extract->extract_progress_user_data = user_data;
  122. }
  123. }
  124. static int
  125. copy_data(struct archive *ar, struct archive *aw)
  126. {
  127. off_t offset;
  128. const void *buff;
  129. struct extract *extract;
  130. size_t size;
  131. int r;
  132. extract = get_extract((struct archive_read *)ar);
  133. for (;;) {
  134. r = archive_read_data_block(ar, &buff, &size, &offset);
  135. if (r == ARCHIVE_EOF)
  136. return (ARCHIVE_OK);
  137. if (r != ARCHIVE_OK)
  138. return (r);
  139. r = archive_write_data_block(aw, buff, size, offset);
  140. if (r < ARCHIVE_WARN)
  141. r = ARCHIVE_WARN;
  142. if (r != ARCHIVE_OK) {
  143. archive_set_error(ar, archive_errno(aw),
  144. "%s", archive_error_string(aw));
  145. return (r);
  146. }
  147. if (extract->extract_progress)
  148. (extract->extract_progress)
  149. (extract->extract_progress_user_data);
  150. }
  151. }
  152. /*
  153. * Cleanup function for archive_extract.
  154. */
  155. static int
  156. archive_read_extract_cleanup(struct archive_read *a)
  157. {
  158. int ret = ARCHIVE_OK;
  159. #if ARCHIVE_API_VERSION > 1
  160. ret =
  161. #endif
  162. archive_write_finish(a->extract->ad);
  163. free(a->extract);
  164. a->extract = NULL;
  165. return (ret);
  166. }