archive_write_open_filename.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_write_open_filename.c,v 1.20 2008/02/19 05:46:58 kientzle Exp $");
  27. #ifdef HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #ifdef HAVE_FCNTL_H
  34. #include <fcntl.h>
  35. #endif
  36. #ifdef HAVE_STDLIB_H
  37. #include <stdlib.h>
  38. #endif
  39. #ifdef HAVE_STRING_H
  40. #include <string.h>
  41. #endif
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #include "archive.h"
  46. #ifndef O_BINARY
  47. #define O_BINARY 0
  48. #endif
  49. struct write_file_data {
  50. int fd;
  51. char filename[1];
  52. };
  53. static int file_close(struct archive *, void *);
  54. static int file_open(struct archive *, void *);
  55. static ssize_t file_write(struct archive *, void *, const void *buff, size_t);
  56. int
  57. archive_write_open_file(struct archive *a, const char *filename)
  58. {
  59. return (archive_write_open_filename(a, filename));
  60. }
  61. int
  62. archive_write_open_filename(struct archive *a, const char *filename)
  63. {
  64. struct write_file_data *mine;
  65. if (filename == NULL || filename[0] == '\0')
  66. return (archive_write_open_fd(a, 1));
  67. mine = (struct write_file_data *)malloc(sizeof(*mine) + strlen(filename));
  68. if (mine == NULL) {
  69. archive_set_error(a, ENOMEM, "No memory");
  70. return (ARCHIVE_FATAL);
  71. }
  72. strcpy(mine->filename, filename);
  73. mine->fd = -1;
  74. return (archive_write_open(a, mine,
  75. file_open, file_write, file_close));
  76. }
  77. static int
  78. file_open(struct archive *a, void *client_data)
  79. {
  80. int flags;
  81. struct write_file_data *mine;
  82. struct stat st;
  83. mine = (struct write_file_data *)client_data;
  84. flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
  85. /*
  86. * Open the file.
  87. */
  88. mine->fd = open(mine->filename, flags, 0666);
  89. if (mine->fd < 0) {
  90. archive_set_error(a, errno, "Failed to open '%s'",
  91. mine->filename);
  92. return (ARCHIVE_FATAL);
  93. }
  94. if (fstat(mine->fd, &st) != 0) {
  95. archive_set_error(a, errno, "Couldn't stat '%s'",
  96. mine->filename);
  97. return (ARCHIVE_FATAL);
  98. }
  99. /*
  100. * Set up default last block handling.
  101. */
  102. if (archive_write_get_bytes_in_last_block(a) < 0) {
  103. if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
  104. S_ISFIFO(st.st_mode))
  105. /* Pad last block when writing to device or FIFO. */
  106. archive_write_set_bytes_in_last_block(a, 0);
  107. else
  108. /* Don't pad last block otherwise. */
  109. archive_write_set_bytes_in_last_block(a, 1);
  110. }
  111. /*
  112. * If the output file is a regular file, don't add it to
  113. * itself. If it's a device file, it's okay to add the device
  114. * entry to the output archive.
  115. */
  116. if (S_ISREG(st.st_mode))
  117. archive_write_set_skip_file(a, st.st_dev, st.st_ino);
  118. return (ARCHIVE_OK);
  119. }
  120. static ssize_t
  121. file_write(struct archive *a, void *client_data, const void *buff, size_t length)
  122. {
  123. struct write_file_data *mine;
  124. ssize_t bytesWritten;
  125. mine = (struct write_file_data *)client_data;
  126. bytesWritten = write(mine->fd, buff, length);
  127. if (bytesWritten <= 0) {
  128. archive_set_error(a, errno, "Write error");
  129. return (-1);
  130. }
  131. return (bytesWritten);
  132. }
  133. static int
  134. file_close(struct archive *a, void *client_data)
  135. {
  136. struct write_file_data *mine = (struct write_file_data *)client_data;
  137. (void)a; /* UNUSED */
  138. close(mine->fd);
  139. free(mine);
  140. return (ARCHIVE_OK);
  141. }