archive_read_extract2.c 4.6 KB

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