archive_write_set_format_cpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio.c 201170 2009-12-29 06:34:23Z kientzle $");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #include <stdio.h>
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_entry_locale.h"
  41. #include "archive_private.h"
  42. #include "archive_write_private.h"
  43. #include "archive_write_set_format_private.h"
  44. static ssize_t archive_write_cpio_data(struct archive_write *,
  45. const void *buff, size_t s);
  46. static int archive_write_cpio_close(struct archive_write *);
  47. static int archive_write_cpio_free(struct archive_write *);
  48. static int archive_write_cpio_finish_entry(struct archive_write *);
  49. static int archive_write_cpio_header(struct archive_write *,
  50. struct archive_entry *);
  51. static int archive_write_cpio_options(struct archive_write *,
  52. const char *, const char *);
  53. static int format_octal(int64_t, void *, int);
  54. static int64_t format_octal_recursive(int64_t, char *, int);
  55. static int write_header(struct archive_write *, struct archive_entry *);
  56. struct cpio {
  57. uint64_t entry_bytes_remaining;
  58. int64_t ino_next;
  59. struct { int64_t old; int new;} *ino_list;
  60. size_t ino_list_size;
  61. size_t ino_list_next;
  62. struct archive_string_conv *opt_sconv;
  63. struct archive_string_conv *sconv_default;
  64. int init_default_conversion;
  65. };
  66. #define c_magic_offset 0
  67. #define c_magic_size 6
  68. #define c_dev_offset 6
  69. #define c_dev_size 6
  70. #define c_ino_offset 12
  71. #define c_ino_size 6
  72. #define c_mode_offset 18
  73. #define c_mode_size 6
  74. #define c_uid_offset 24
  75. #define c_uid_size 6
  76. #define c_gid_offset 30
  77. #define c_gid_size 6
  78. #define c_nlink_offset 36
  79. #define c_nlink_size 6
  80. #define c_rdev_offset 42
  81. #define c_rdev_size 6
  82. #define c_mtime_offset 48
  83. #define c_mtime_size 11
  84. #define c_namesize_offset 59
  85. #define c_namesize_size 6
  86. #define c_filesize_offset 65
  87. #define c_filesize_size 11
  88. /*
  89. * Set output format to 'cpio' format.
  90. */
  91. int
  92. archive_write_set_format_cpio(struct archive *_a)
  93. {
  94. struct archive_write *a = (struct archive_write *)_a;
  95. struct cpio *cpio;
  96. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  97. ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
  98. /* If someone else was already registered, unregister them. */
  99. if (a->format_free != NULL)
  100. (a->format_free)(a);
  101. cpio = (struct cpio *)calloc(1, sizeof(*cpio));
  102. if (cpio == NULL) {
  103. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  104. return (ARCHIVE_FATAL);
  105. }
  106. a->format_data = cpio;
  107. a->format_name = "cpio";
  108. a->format_options = archive_write_cpio_options;
  109. a->format_write_header = archive_write_cpio_header;
  110. a->format_write_data = archive_write_cpio_data;
  111. a->format_finish_entry = archive_write_cpio_finish_entry;
  112. a->format_close = archive_write_cpio_close;
  113. a->format_free = archive_write_cpio_free;
  114. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  115. a->archive.archive_format_name = "POSIX cpio";
  116. return (ARCHIVE_OK);
  117. }
  118. static int
  119. archive_write_cpio_options(struct archive_write *a, const char *key,
  120. const char *val)
  121. {
  122. struct cpio *cpio = (struct cpio *)a->format_data;
  123. int ret = ARCHIVE_FAILED;
  124. if (strcmp(key, "hdrcharset") == 0) {
  125. if (val == NULL || val[0] == 0)
  126. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  127. "%s: hdrcharset option needs a character-set name",
  128. a->format_name);
  129. else {
  130. cpio->opt_sconv = archive_string_conversion_to_charset(
  131. &a->archive, val, 0);
  132. if (cpio->opt_sconv != NULL)
  133. ret = ARCHIVE_OK;
  134. else
  135. ret = ARCHIVE_FATAL;
  136. }
  137. return (ret);
  138. }
  139. /* Note: The "warn" return is just to inform the options
  140. * supervisor that we didn't handle it. It will generate
  141. * a suitable error if no one used this option. */
  142. return (ARCHIVE_WARN);
  143. }
  144. /*
  145. * Ino values are as long as 64 bits on some systems; cpio format
  146. * only allows 18 bits and relies on the ino values to identify hardlinked
  147. * files. So, we can't merely "hash" the ino numbers since collisions
  148. * would corrupt the archive. Instead, we generate synthetic ino values
  149. * to store in the archive and maintain a map of original ino values to
  150. * synthetic ones so we can preserve hardlink information.
  151. *
  152. * TODO: Make this more efficient. It's not as bad as it looks (most
  153. * files don't have any hardlinks and we don't do any work here for those),
  154. * but it wouldn't be hard to do better.
  155. *
  156. * TODO: Work with dev/ino pairs here instead of just ino values.
  157. */
  158. static int
  159. synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
  160. {
  161. int64_t ino = archive_entry_ino64(entry);
  162. int ino_new;
  163. size_t i;
  164. /*
  165. * If no index number was given, don't assign one. In
  166. * particular, this handles the end-of-archive marker
  167. * correctly by giving it a zero index value. (This is also
  168. * why we start our synthetic index numbers with one below.)
  169. */
  170. if (ino == 0)
  171. return (0);
  172. /* Don't store a mapping if we don't need to. */
  173. if (archive_entry_nlink(entry) < 2) {
  174. return (int)(++cpio->ino_next);
  175. }
  176. /* Look up old ino; if we have it, this is a hardlink
  177. * and we reuse the same value. */
  178. for (i = 0; i < cpio->ino_list_next; ++i) {
  179. if (cpio->ino_list[i].old == ino)
  180. return (cpio->ino_list[i].new);
  181. }
  182. /* Assign a new index number. */
  183. ino_new = (int)(++cpio->ino_next);
  184. /* Ensure space for the new mapping. */
  185. if (cpio->ino_list_size <= cpio->ino_list_next) {
  186. size_t newsize = cpio->ino_list_size < 512
  187. ? 512 : cpio->ino_list_size * 2;
  188. void *newlist = realloc(cpio->ino_list,
  189. sizeof(cpio->ino_list[0]) * newsize);
  190. if (newlist == NULL)
  191. return (-1);
  192. cpio->ino_list_size = newsize;
  193. cpio->ino_list = newlist;
  194. }
  195. /* Record and return the new value. */
  196. cpio->ino_list[cpio->ino_list_next].old = ino;
  197. cpio->ino_list[cpio->ino_list_next].new = ino_new;
  198. ++cpio->ino_list_next;
  199. return (ino_new);
  200. }
  201. static struct archive_string_conv *
  202. get_sconv(struct archive_write *a)
  203. {
  204. struct cpio *cpio;
  205. struct archive_string_conv *sconv;
  206. cpio = (struct cpio *)a->format_data;
  207. sconv = cpio->opt_sconv;
  208. if (sconv == NULL) {
  209. if (!cpio->init_default_conversion) {
  210. cpio->sconv_default =
  211. archive_string_default_conversion_for_write(
  212. &(a->archive));
  213. cpio->init_default_conversion = 1;
  214. }
  215. sconv = cpio->sconv_default;
  216. }
  217. return (sconv);
  218. }
  219. static int
  220. archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
  221. {
  222. const char *path;
  223. size_t len;
  224. if (archive_entry_filetype(entry) == 0) {
  225. archive_set_error(&a->archive, -1, "Filetype required");
  226. return (ARCHIVE_FAILED);
  227. }
  228. if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
  229. && errno == ENOMEM) {
  230. archive_set_error(&a->archive, ENOMEM,
  231. "Can't allocate memory for Pathname");
  232. return (ARCHIVE_FATAL);
  233. }
  234. if (len == 0 || path == NULL || path[0] == '\0') {
  235. archive_set_error(&a->archive, -1, "Pathname required");
  236. return (ARCHIVE_FAILED);
  237. }
  238. if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
  239. archive_set_error(&a->archive, -1, "Size required");
  240. return (ARCHIVE_FAILED);
  241. }
  242. return write_header(a, entry);
  243. }
  244. static int
  245. write_header(struct archive_write *a, struct archive_entry *entry)
  246. {
  247. struct cpio *cpio;
  248. const char *p, *path;
  249. int pathlength, ret, ret_final;
  250. int64_t ino;
  251. char h[76];
  252. struct archive_string_conv *sconv;
  253. struct archive_entry *entry_main;
  254. size_t len;
  255. cpio = (struct cpio *)a->format_data;
  256. ret_final = ARCHIVE_OK;
  257. sconv = get_sconv(a);
  258. #if defined(_WIN32) && !defined(__CYGWIN__)
  259. /* Make sure the path separators in pathname, hardlink and symlink
  260. * are all slash '/', not the Windows path separator '\'. */
  261. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  262. if (entry_main == NULL) {
  263. archive_set_error(&a->archive, ENOMEM,
  264. "Can't allocate ustar data");
  265. return(ARCHIVE_FATAL);
  266. }
  267. if (entry != entry_main)
  268. entry = entry_main;
  269. else
  270. entry_main = NULL;
  271. #else
  272. entry_main = NULL;
  273. #endif
  274. ret = archive_entry_pathname_l(entry, &path, &len, sconv);
  275. if (ret != 0) {
  276. if (errno == ENOMEM) {
  277. archive_set_error(&a->archive, ENOMEM,
  278. "Can't allocate memory for Pathname");
  279. ret_final = ARCHIVE_FATAL;
  280. goto exit_write_header;
  281. }
  282. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  283. "Can't translate pathname '%s' to %s",
  284. archive_entry_pathname(entry),
  285. archive_string_conversion_charset_name(sconv));
  286. ret_final = ARCHIVE_WARN;
  287. }
  288. /* Include trailing null. */
  289. pathlength = (int)len + 1;
  290. memset(h, 0, sizeof(h));
  291. format_octal(070707, h + c_magic_offset, c_magic_size);
  292. format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
  293. ino = synthesize_ino_value(cpio, entry);
  294. if (ino < 0) {
  295. archive_set_error(&a->archive, ENOMEM,
  296. "No memory for ino translation table");
  297. ret_final = ARCHIVE_FATAL;
  298. goto exit_write_header;
  299. } else if (ino > 0777777) {
  300. archive_set_error(&a->archive, ERANGE,
  301. "Too many files for this cpio format");
  302. ret_final = ARCHIVE_FATAL;
  303. goto exit_write_header;
  304. }
  305. format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
  306. /* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
  307. format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
  308. format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
  309. format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
  310. format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
  311. if (archive_entry_filetype(entry) == AE_IFBLK
  312. || archive_entry_filetype(entry) == AE_IFCHR)
  313. format_octal(archive_entry_dev(entry), h + c_rdev_offset, c_rdev_size);
  314. else
  315. format_octal(0, h + c_rdev_offset, c_rdev_size);
  316. format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
  317. format_octal(pathlength, h + c_namesize_offset, c_namesize_size);
  318. /* Non-regular files don't store bodies. */
  319. if (archive_entry_filetype(entry) != AE_IFREG)
  320. archive_entry_set_size(entry, 0);
  321. /* Symlinks get the link written as the body of the entry. */
  322. ret = archive_entry_symlink_l(entry, &p, &len, sconv);
  323. if (ret != 0) {
  324. if (errno == ENOMEM) {
  325. archive_set_error(&a->archive, ENOMEM,
  326. "Can't allocate memory for Linkname");
  327. ret_final = ARCHIVE_FATAL;
  328. goto exit_write_header;
  329. }
  330. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  331. "Can't translate linkname '%s' to %s",
  332. archive_entry_symlink(entry),
  333. archive_string_conversion_charset_name(sconv));
  334. ret_final = ARCHIVE_WARN;
  335. }
  336. if (len > 0 && p != NULL && *p != '\0')
  337. ret = format_octal(strlen(p), h + c_filesize_offset,
  338. c_filesize_size);
  339. else
  340. ret = format_octal(archive_entry_size(entry),
  341. h + c_filesize_offset, c_filesize_size);
  342. if (ret) {
  343. archive_set_error(&a->archive, ERANGE,
  344. "File is too large for cpio format.");
  345. ret_final = ARCHIVE_FAILED;
  346. goto exit_write_header;
  347. }
  348. ret = __archive_write_output(a, h, sizeof(h));
  349. if (ret != ARCHIVE_OK) {
  350. ret_final = ARCHIVE_FATAL;
  351. goto exit_write_header;
  352. }
  353. ret = __archive_write_output(a, path, pathlength);
  354. if (ret != ARCHIVE_OK) {
  355. ret_final = ARCHIVE_FATAL;
  356. goto exit_write_header;
  357. }
  358. cpio->entry_bytes_remaining = archive_entry_size(entry);
  359. /* Write the symlink now. */
  360. if (p != NULL && *p != '\0') {
  361. ret = __archive_write_output(a, p, strlen(p));
  362. if (ret != ARCHIVE_OK) {
  363. ret_final = ARCHIVE_FATAL;
  364. goto exit_write_header;
  365. }
  366. }
  367. exit_write_header:
  368. archive_entry_free(entry_main);
  369. return (ret_final);
  370. }
  371. static ssize_t
  372. archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
  373. {
  374. struct cpio *cpio;
  375. int ret;
  376. cpio = (struct cpio *)a->format_data;
  377. if (s > cpio->entry_bytes_remaining)
  378. s = (size_t)cpio->entry_bytes_remaining;
  379. ret = __archive_write_output(a, buff, s);
  380. cpio->entry_bytes_remaining -= s;
  381. if (ret >= 0)
  382. return (s);
  383. else
  384. return (ret);
  385. }
  386. /*
  387. * Format a number into the specified field.
  388. */
  389. static int
  390. format_octal(int64_t v, void *p, int digits)
  391. {
  392. int64_t max;
  393. int ret;
  394. max = (((int64_t)1) << (digits * 3)) - 1;
  395. if (v >= 0 && v <= max) {
  396. format_octal_recursive(v, (char *)p, digits);
  397. ret = 0;
  398. } else {
  399. format_octal_recursive(max, (char *)p, digits);
  400. ret = -1;
  401. }
  402. return (ret);
  403. }
  404. static int64_t
  405. format_octal_recursive(int64_t v, char *p, int s)
  406. {
  407. if (s == 0)
  408. return (v);
  409. v = format_octal_recursive(v, p+1, s-1);
  410. *p = '0' + ((char)v & 7);
  411. return (v >> 3);
  412. }
  413. static int
  414. archive_write_cpio_close(struct archive_write *a)
  415. {
  416. int er;
  417. struct archive_entry *trailer;
  418. trailer = archive_entry_new2(NULL);
  419. /* nlink = 1 here for GNU cpio compat. */
  420. archive_entry_set_nlink(trailer, 1);
  421. archive_entry_set_size(trailer, 0);
  422. archive_entry_set_pathname(trailer, "TRAILER!!!");
  423. er = write_header(a, trailer);
  424. archive_entry_free(trailer);
  425. return (er);
  426. }
  427. static int
  428. archive_write_cpio_free(struct archive_write *a)
  429. {
  430. struct cpio *cpio;
  431. cpio = (struct cpio *)a->format_data;
  432. free(cpio->ino_list);
  433. free(cpio);
  434. a->format_data = NULL;
  435. return (ARCHIVE_OK);
  436. }
  437. static int
  438. archive_write_cpio_finish_entry(struct archive_write *a)
  439. {
  440. struct cpio *cpio;
  441. cpio = (struct cpio *)a->format_data;
  442. return (__archive_write_nulls(a,
  443. (size_t)cpio->entry_bytes_remaining));
  444. }