archive_read_data_into_fd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_data_into_fd.c,v 1.16 2008/05/23 05:01:29 cperciva 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_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #include "archive.h"
  37. #include "archive_private.h"
  38. /* Maximum amount of data to write at one time. */
  39. #define MAX_WRITE (1024 * 1024)
  40. /*
  41. * This implementation minimizes copying of data and is sparse-file aware.
  42. */
  43. static int
  44. pad_to(struct archive *a, int fd, int can_lseek,
  45. size_t nulls_size, const char *nulls,
  46. int64_t target_offset, int64_t actual_offset)
  47. {
  48. size_t to_write;
  49. ssize_t bytes_written;
  50. if (can_lseek) {
  51. actual_offset = lseek(fd,
  52. target_offset - actual_offset, SEEK_CUR);
  53. if (actual_offset != target_offset) {
  54. archive_set_error(a, errno, "Seek error");
  55. return (ARCHIVE_FATAL);
  56. }
  57. return (ARCHIVE_OK);
  58. }
  59. while (target_offset > actual_offset) {
  60. to_write = nulls_size;
  61. if (target_offset < actual_offset + (int64_t)nulls_size)
  62. to_write = (size_t)(target_offset - actual_offset);
  63. bytes_written = write(fd, nulls, to_write);
  64. if (bytes_written < 0) {
  65. archive_set_error(a, errno, "Write error");
  66. return (ARCHIVE_FATAL);
  67. }
  68. actual_offset += bytes_written;
  69. }
  70. return (ARCHIVE_OK);
  71. }
  72. int
  73. archive_read_data_into_fd(struct archive *a, int fd)
  74. {
  75. struct stat st;
  76. int r, r2;
  77. const void *buff;
  78. size_t size, bytes_to_write;
  79. ssize_t bytes_written;
  80. int64_t target_offset;
  81. int64_t actual_offset = 0;
  82. int can_lseek;
  83. char *nulls = NULL;
  84. size_t nulls_size = 16384;
  85. archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
  86. "archive_read_data_into_fd");
  87. can_lseek = (fstat(fd, &st) == 0) && S_ISREG(st.st_mode);
  88. if (!can_lseek)
  89. nulls = calloc(1, nulls_size);
  90. while ((r = archive_read_data_block(a, &buff, &size, &target_offset)) ==
  91. ARCHIVE_OK) {
  92. const char *p = buff;
  93. if (target_offset > actual_offset) {
  94. r = pad_to(a, fd, can_lseek, nulls_size, nulls,
  95. target_offset, actual_offset);
  96. if (r != ARCHIVE_OK)
  97. break;
  98. actual_offset = target_offset;
  99. }
  100. while (size > 0) {
  101. bytes_to_write = size;
  102. if (bytes_to_write > MAX_WRITE)
  103. bytes_to_write = MAX_WRITE;
  104. bytes_written = write(fd, p, bytes_to_write);
  105. if (bytes_written < 0) {
  106. archive_set_error(a, errno, "Write error");
  107. r = ARCHIVE_FATAL;
  108. goto cleanup;
  109. }
  110. actual_offset += bytes_written;
  111. p += bytes_written;
  112. size -= bytes_written;
  113. }
  114. }
  115. if (r == ARCHIVE_EOF && target_offset > actual_offset) {
  116. r2 = pad_to(a, fd, can_lseek, nulls_size, nulls,
  117. target_offset, actual_offset);
  118. if (r2 != ARCHIVE_OK)
  119. r = r2;
  120. }
  121. cleanup:
  122. free(nulls);
  123. if (r != ARCHIVE_EOF)
  124. return (r);
  125. return (ARCHIVE_OK);
  126. }