archive_read_extract2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_STRING_H
  34. #include <string.h>
  35. #endif
  36. #include "archive.h"
  37. #include "archive_entry.h"
  38. #include "archive_private.h"
  39. #include "archive_read_private.h"
  40. static int copy_data(struct archive *ar, struct archive *aw);
  41. static int archive_read_extract_cleanup(struct archive_read *);
  42. /* Retrieve an extract object without initialising the associated
  43. * archive_write_disk object.
  44. */
  45. struct archive_read_extract *
  46. __archive_read_get_extract(struct archive_read *a)
  47. {
  48. if (a->extract == NULL) {
  49. a->extract = (struct archive_read_extract *)calloc(1, sizeof(*a->extract));
  50. if (a->extract == NULL) {
  51. archive_set_error(&a->archive, ENOMEM, "Can't extract");
  52. return (NULL);
  53. }
  54. a->cleanup_archive_extract = archive_read_extract_cleanup;
  55. }
  56. return (a->extract);
  57. }
  58. /*
  59. * Cleanup function for archive_extract.
  60. */
  61. static int
  62. archive_read_extract_cleanup(struct archive_read *a)
  63. {
  64. int ret = ARCHIVE_OK;
  65. if (a->extract->ad != NULL) {
  66. ret = archive_write_free(a->extract->ad);
  67. }
  68. free(a->extract);
  69. a->extract = NULL;
  70. return (ret);
  71. }
  72. int
  73. archive_read_extract2(struct archive *_a, struct archive_entry *entry,
  74. struct archive *ad)
  75. {
  76. struct archive_read *a = (struct archive_read *)_a;
  77. int r, r2;
  78. /* Set up for this particular entry. */
  79. if (a->skip_file_set)
  80. archive_write_disk_set_skip_file(ad,
  81. a->skip_file_dev, a->skip_file_ino);
  82. r = archive_write_header(ad, entry);
  83. if (r < ARCHIVE_WARN)
  84. r = ARCHIVE_WARN;
  85. if (r != ARCHIVE_OK)
  86. /* If _write_header failed, copy the error. */
  87. archive_copy_error(&a->archive, ad);
  88. else if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) > 0)
  89. /* Otherwise, pour data into the entry. */
  90. r = copy_data(_a, ad);
  91. r2 = archive_write_finish_entry(ad);
  92. if (r2 < ARCHIVE_WARN)
  93. r2 = ARCHIVE_WARN;
  94. /* Use the first message. */
  95. if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
  96. archive_copy_error(&a->archive, ad);
  97. /* Use the worst error return. */
  98. if (r2 < r)
  99. r = r2;
  100. return (r);
  101. }
  102. void
  103. archive_read_extract_set_progress_callback(struct archive *_a,
  104. void (*progress_func)(void *), void *user_data)
  105. {
  106. struct archive_read *a = (struct archive_read *)_a;
  107. struct archive_read_extract *extract = __archive_read_get_extract(a);
  108. if (extract != NULL) {
  109. extract->extract_progress = progress_func;
  110. extract->extract_progress_user_data = user_data;
  111. }
  112. }
  113. static int
  114. copy_data(struct archive *ar, struct archive *aw)
  115. {
  116. int64_t offset;
  117. const void *buff;
  118. struct archive_read_extract *extract;
  119. size_t size;
  120. int r;
  121. extract = __archive_read_get_extract((struct archive_read *)ar);
  122. if (extract == NULL)
  123. return (ARCHIVE_FATAL);
  124. for (;;) {
  125. r = archive_read_data_block(ar, &buff, &size, &offset);
  126. if (r == ARCHIVE_EOF)
  127. return (ARCHIVE_OK);
  128. if (r != ARCHIVE_OK)
  129. return (r);
  130. r = (int)archive_write_data_block(aw, buff, size, offset);
  131. if (r < ARCHIVE_WARN)
  132. r = ARCHIVE_WARN;
  133. if (r < ARCHIVE_OK) {
  134. archive_set_error(ar, archive_errno(aw),
  135. "%s", archive_error_string(aw));
  136. return (r);
  137. }
  138. if (extract->extract_progress)
  139. (extract->extract_progress)
  140. (extract->extract_progress_user_data);
  141. }
  142. }