archive_read_extract2.c 4.2 KB

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