archive_write_set_format_cpio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_set_format_cpio.c,v 1.14 2008/03/15 11:04:45 kientzle Exp $");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include <stdio.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #include "archive.h"
  38. #include "archive_entry.h"
  39. #include "archive_private.h"
  40. #include "archive_write_private.h"
  41. static ssize_t archive_write_cpio_data(struct archive_write *,
  42. const void *buff, size_t s);
  43. static int archive_write_cpio_finish(struct archive_write *);
  44. static int archive_write_cpio_destroy(struct archive_write *);
  45. static int archive_write_cpio_finish_entry(struct archive_write *);
  46. static int archive_write_cpio_header(struct archive_write *,
  47. struct archive_entry *);
  48. static int format_octal(int64_t, void *, int);
  49. static int64_t format_octal_recursive(int64_t, char *, int);
  50. struct cpio {
  51. uint64_t entry_bytes_remaining;
  52. };
  53. struct cpio_header {
  54. char c_magic[6];
  55. char c_dev[6];
  56. char c_ino[6];
  57. char c_mode[6];
  58. char c_uid[6];
  59. char c_gid[6];
  60. char c_nlink[6];
  61. char c_rdev[6];
  62. char c_mtime[11];
  63. char c_namesize[6];
  64. char c_filesize[11];
  65. };
  66. /*
  67. * Set output format to 'cpio' format.
  68. */
  69. int
  70. archive_write_set_format_cpio(struct archive *_a)
  71. {
  72. struct archive_write *a = (struct archive_write *)_a;
  73. struct cpio *cpio;
  74. /* If someone else was already registered, unregister them. */
  75. if (a->format_destroy != NULL)
  76. (a->format_destroy)(a);
  77. cpio = (struct cpio *)malloc(sizeof(*cpio));
  78. if (cpio == NULL) {
  79. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  80. return (ARCHIVE_FATAL);
  81. }
  82. memset(cpio, 0, sizeof(*cpio));
  83. a->format_data = cpio;
  84. a->pad_uncompressed = 1;
  85. a->format_name = "cpio";
  86. a->format_write_header = archive_write_cpio_header;
  87. a->format_write_data = archive_write_cpio_data;
  88. a->format_finish_entry = archive_write_cpio_finish_entry;
  89. a->format_finish = archive_write_cpio_finish;
  90. a->format_destroy = archive_write_cpio_destroy;
  91. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  92. a->archive.archive_format_name = "POSIX cpio";
  93. return (ARCHIVE_OK);
  94. }
  95. static int
  96. archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
  97. {
  98. struct cpio *cpio;
  99. const char *p, *path;
  100. int pathlength, ret;
  101. int64_t ino;
  102. struct cpio_header h;
  103. cpio = (struct cpio *)a->format_data;
  104. ret = 0;
  105. path = archive_entry_pathname(entry);
  106. pathlength = strlen(path) + 1; /* Include trailing null. */
  107. memset(&h, 0, sizeof(h));
  108. format_octal(070707, &h.c_magic, sizeof(h.c_magic));
  109. format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
  110. /*
  111. * TODO: Generate artificial inode numbers rather than just
  112. * re-using the ones off the disk. That way, the 18-bit c_ino
  113. * field only limits the number of files in the archive.
  114. */
  115. ino = archive_entry_ino64(entry);
  116. if (ino < 0 || ino > 0777777) {
  117. archive_set_error(&a->archive, ERANGE,
  118. "large inode number truncated");
  119. ret = ARCHIVE_WARN;
  120. }
  121. format_octal(archive_entry_ino64(entry) & 0777777, &h.c_ino, sizeof(h.c_ino));
  122. format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
  123. format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
  124. format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
  125. format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
  126. if (archive_entry_filetype(entry) == AE_IFBLK
  127. || archive_entry_filetype(entry) == AE_IFCHR)
  128. format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
  129. else
  130. format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
  131. format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
  132. format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
  133. /* Non-regular files don't store bodies. */
  134. if (archive_entry_filetype(entry) != AE_IFREG)
  135. archive_entry_set_size(entry, 0);
  136. /* Symlinks get the link written as the body of the entry. */
  137. p = archive_entry_symlink(entry);
  138. if (p != NULL && *p != '\0')
  139. format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
  140. else
  141. format_octal(archive_entry_size(entry),
  142. &h.c_filesize, sizeof(h.c_filesize));
  143. ret = (a->compressor.write)(a, &h, sizeof(h));
  144. if (ret != ARCHIVE_OK)
  145. return (ARCHIVE_FATAL);
  146. ret = (a->compressor.write)(a, path, pathlength);
  147. if (ret != ARCHIVE_OK)
  148. return (ARCHIVE_FATAL);
  149. cpio->entry_bytes_remaining = archive_entry_size(entry);
  150. /* Write the symlink now. */
  151. if (p != NULL && *p != '\0')
  152. ret = (a->compressor.write)(a, p, strlen(p));
  153. return (ret);
  154. }
  155. static ssize_t
  156. archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
  157. {
  158. struct cpio *cpio;
  159. int ret;
  160. cpio = (struct cpio *)a->format_data;
  161. if (s > cpio->entry_bytes_remaining)
  162. s = cpio->entry_bytes_remaining;
  163. ret = (a->compressor.write)(a, buff, s);
  164. cpio->entry_bytes_remaining -= s;
  165. if (ret >= 0)
  166. return (s);
  167. else
  168. return (ret);
  169. }
  170. /*
  171. * Format a number into the specified field.
  172. */
  173. static int
  174. format_octal(int64_t v, void *p, int digits)
  175. {
  176. int64_t max;
  177. int ret;
  178. max = (((int64_t)1) << (digits * 3)) - 1;
  179. if (v >= 0 && v <= max) {
  180. format_octal_recursive(v, (char *)p, digits);
  181. ret = 0;
  182. } else {
  183. format_octal_recursive(max, (char *)p, digits);
  184. ret = -1;
  185. }
  186. return (ret);
  187. }
  188. static int64_t
  189. format_octal_recursive(int64_t v, char *p, int s)
  190. {
  191. if (s == 0)
  192. return (v);
  193. v = format_octal_recursive(v, p+1, s-1);
  194. *p = '0' + (v & 7);
  195. return (v >>= 3);
  196. }
  197. static int
  198. archive_write_cpio_finish(struct archive_write *a)
  199. {
  200. struct cpio *cpio;
  201. int er;
  202. struct archive_entry *trailer;
  203. cpio = (struct cpio *)a->format_data;
  204. trailer = archive_entry_new();
  205. /* nlink = 1 here for GNU cpio compat. */
  206. archive_entry_set_nlink(trailer, 1);
  207. archive_entry_set_pathname(trailer, "TRAILER!!!");
  208. er = archive_write_cpio_header(a, trailer);
  209. archive_entry_free(trailer);
  210. return (er);
  211. }
  212. static int
  213. archive_write_cpio_destroy(struct archive_write *a)
  214. {
  215. struct cpio *cpio;
  216. cpio = (struct cpio *)a->format_data;
  217. free(cpio);
  218. a->format_data = NULL;
  219. return (ARCHIVE_OK);
  220. }
  221. static int
  222. archive_write_cpio_finish_entry(struct archive_write *a)
  223. {
  224. struct cpio *cpio;
  225. int to_write, ret;
  226. cpio = (struct cpio *)a->format_data;
  227. ret = ARCHIVE_OK;
  228. while (cpio->entry_bytes_remaining > 0) {
  229. to_write = cpio->entry_bytes_remaining < a->null_length ?
  230. cpio->entry_bytes_remaining : a->null_length;
  231. ret = (a->compressor.write)(a, a->nulls, to_write);
  232. if (ret != ARCHIVE_OK)
  233. return (ret);
  234. cpio->entry_bytes_remaining -= to_write;
  235. }
  236. return (ret);
  237. }