archive_read_data_into_fd.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. int
  44. archive_read_data_into_fd(struct archive *a, int fd)
  45. {
  46. int r;
  47. const void *buff;
  48. size_t size, bytes_to_write;
  49. ssize_t bytes_written, total_written;
  50. off_t offset;
  51. off_t output_offset;
  52. __archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, "archive_read_data_into_fd");
  53. total_written = 0;
  54. output_offset = 0;
  55. while ((r = archive_read_data_block(a, &buff, &size, &offset)) ==
  56. ARCHIVE_OK) {
  57. const char *p = buff;
  58. if (offset > output_offset) {
  59. output_offset = lseek(fd,
  60. offset - output_offset, SEEK_CUR);
  61. if (output_offset != offset) {
  62. archive_set_error(a, errno, "Seek error");
  63. return (ARCHIVE_FATAL);
  64. }
  65. }
  66. while (size > 0) {
  67. bytes_to_write = size;
  68. if (bytes_to_write > MAX_WRITE)
  69. bytes_to_write = MAX_WRITE;
  70. bytes_written = write(fd, p, bytes_to_write);
  71. if (bytes_written < 0) {
  72. archive_set_error(a, errno, "Write error");
  73. return (ARCHIVE_FATAL);
  74. }
  75. output_offset += bytes_written;
  76. total_written += bytes_written;
  77. p += bytes_written;
  78. size -= bytes_written;
  79. }
  80. }
  81. if (r != ARCHIVE_EOF)
  82. return (r);
  83. return (ARCHIVE_OK);
  84. }