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. static ssize_t archive_write_cpio_data(struct archive_write *,
  44. const void *buff, size_t s);
  45. static int archive_write_cpio_close(struct archive_write *);
  46. static int archive_write_cpio_free(struct archive_write *);
  47. static int archive_write_cpio_finish_entry(struct archive_write *);
  48. static int archive_write_cpio_header(struct archive_write *,
  49. struct archive_entry *);
  50. static int archive_write_cpio_options(struct archive_write *,
  51. const char *, const char *);
  52. static int format_octal(int64_t, void *, int);
  53. static int64_t format_octal_recursive(int64_t, char *, int);
  54. static int write_header(struct archive_write *, struct archive_entry *);
  55. struct cpio {
  56. uint64_t entry_bytes_remaining;
  57. int64_t ino_next;
  58. struct { int64_t old; int new;} *ino_list;
  59. size_t ino_list_size;
  60. size_t ino_list_next;
  61. struct archive_string_conv *opt_sconv;
  62. struct archive_string_conv *sconv_default;
  63. int init_default_conversion;
  64. };
  65. #define c_magic_offset 0
  66. #define c_magic_size 6
  67. #define c_dev_offset 6
  68. #define c_dev_size 6
  69. #define c_ino_offset 12
  70. #define c_ino_size 6
  71. #define c_mode_offset 18
  72. #define c_mode_size 6
  73. #define c_uid_offset 24
  74. #define c_uid_size 6
  75. #define c_gid_offset 30
  76. #define c_gid_size 6
  77. #define c_nlink_offset 36
  78. #define c_nlink_size 6
  79. #define c_rdev_offset 42
  80. #define c_rdev_size 6
  81. #define c_mtime_offset 48
  82. #define c_mtime_size 11
  83. #define c_namesize_offset 59
  84. #define c_namesize_size 6
  85. #define c_filesize_offset 65
  86. #define c_filesize_size 11
  87. /*
  88. * Set output format to 'cpio' format.
  89. */
  90. int
  91. archive_write_set_format_cpio(struct archive *_a)
  92. {
  93. struct archive_write *a = (struct archive_write *)_a;
  94. struct cpio *cpio;
  95. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  96. ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
  97. /* If someone else was already registered, unregister them. */
  98. if (a->format_free != NULL)
  99. (a->format_free)(a);
  100. cpio = (struct cpio *)calloc(1, sizeof(*cpio));
  101. if (cpio == NULL) {
  102. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  103. return (ARCHIVE_FATAL);
  104. }
  105. a->format_data = cpio;
  106. a->format_name = "cpio";
  107. a->format_options = archive_write_cpio_options;
  108. a->format_write_header = archive_write_cpio_header;
  109. a->format_write_data = archive_write_cpio_data;
  110. a->format_finish_entry = archive_write_cpio_finish_entry;
  111. a->format_close = archive_write_cpio_close;
  112. a->format_free = archive_write_cpio_free;
  113. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  114. a->archive.archive_format_name = "POSIX cpio";
  115. return (ARCHIVE_OK);
  116. }
  117. static int
  118. archive_write_cpio_options(struct archive_write *a, const char *key,
  119. const char *val)
  120. {
  121. struct cpio *cpio = (struct cpio *)a->format_data;
  122. int ret = ARCHIVE_FAILED;
  123. if (strcmp(key, "hdrcharset") == 0) {
  124. if (val == NULL || val[0] == 0)
  125. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  126. "%s: hdrcharset option needs a character-set name",
  127. a->format_name);
  128. else {
  129. cpio->opt_sconv = archive_string_conversion_to_charset(
  130. &a->archive, val, 0);
  131. if (cpio->opt_sconv != NULL)
  132. ret = ARCHIVE_OK;
  133. else
  134. ret = ARCHIVE_FATAL;
  135. }
  136. return (ret);
  137. }
  138. /* Note: The "warn" return is just to inform the options
  139. * supervisor that we didn't handle it. It will generate
  140. * a suitable error if no one used this option. */
  141. return (ARCHIVE_WARN);
  142. }
  143. /*
  144. * Ino values are as long as 64 bits on some systems; cpio format
  145. * only allows 18 bits and relies on the ino values to identify hardlinked
  146. * files. So, we can't merely "hash" the ino numbers since collisions
  147. * would corrupt the archive. Instead, we generate synthetic ino values
  148. * to store in the archive and maintain a map of original ino values to
  149. * synthetic ones so we can preserve hardlink information.
  150. *
  151. * TODO: Make this more efficient. It's not as bad as it looks (most
  152. * files don't have any hardlinks and we don't do any work here for those),
  153. * but it wouldn't be hard to do better.
  154. *
  155. * TODO: Work with dev/ino pairs here instead of just ino values.
  156. */
  157. static int
  158. synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
  159. {
  160. int64_t ino = archive_entry_ino64(entry);
  161. int ino_new;
  162. size_t i;
  163. /*
  164. * If no index number was given, don't assign one. In
  165. * particular, this handles the end-of-archive marker
  166. * correctly by giving it a zero index value. (This is also
  167. * why we start our synthetic index numbers with one below.)
  168. */
  169. if (ino == 0)
  170. return (0);
  171. /* Don't store a mapping if we don't need to. */
  172. if (archive_entry_nlink(entry) < 2) {
  173. return (int)(++cpio->ino_next);
  174. }
  175. /* Look up old ino; if we have it, this is a hardlink
  176. * and we reuse the same value. */
  177. for (i = 0; i < cpio->ino_list_next; ++i) {
  178. if (cpio->ino_list[i].old == ino)
  179. return (cpio->ino_list[i].new);
  180. }
  181. /* Assign a new index number. */
  182. ino_new = (int)(++cpio->ino_next);
  183. /* Ensure space for the new mapping. */
  184. if (cpio->ino_list_size <= cpio->ino_list_next) {
  185. size_t newsize = cpio->ino_list_size < 512
  186. ? 512 : cpio->ino_list_size * 2;
  187. void *newlist = realloc(cpio->ino_list,
  188. sizeof(cpio->ino_list[0]) * newsize);
  189. if (newlist == NULL)
  190. return (-1);
  191. cpio->ino_list_size = newsize;
  192. cpio->ino_list = newlist;
  193. }
  194. /* Record and return the new value. */
  195. cpio->ino_list[cpio->ino_list_next].old = ino;
  196. cpio->ino_list[cpio->ino_list_next].new = ino_new;
  197. ++cpio->ino_list_next;
  198. return (ino_new);
  199. }
  200. static struct archive_string_conv *
  201. get_sconv(struct archive_write *a)
  202. {
  203. struct cpio *cpio;
  204. struct archive_string_conv *sconv;
  205. cpio = (struct cpio *)a->format_data;
  206. sconv = cpio->opt_sconv;
  207. if (sconv == NULL) {
  208. if (!cpio->init_default_conversion) {
  209. cpio->sconv_default =
  210. archive_string_default_conversion_for_write(
  211. &(a->archive));
  212. cpio->init_default_conversion = 1;
  213. }
  214. sconv = cpio->sconv_default;
  215. }
  216. return (sconv);
  217. }
  218. static int
  219. archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
  220. {
  221. const char *path;
  222. size_t len;
  223. if (archive_entry_filetype(entry) == 0) {
  224. archive_set_error(&a->archive, -1, "Filetype required");
  225. return (ARCHIVE_FAILED);
  226. }
  227. if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
  228. && errno == ENOMEM) {
  229. archive_set_error(&a->archive, ENOMEM,
  230. "Can't allocate memory for Pathname");
  231. return (ARCHIVE_FATAL);
  232. }
  233. if (len == 0 || path == NULL || path[0] == '\0') {
  234. archive_set_error(&a->archive, -1, "Pathname required");
  235. return (ARCHIVE_FAILED);
  236. }
  237. if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
  238. archive_set_error(&a->archive, -1, "Size required");
  239. return (ARCHIVE_FAILED);
  240. }
  241. return write_header(a, entry);
  242. }
  243. static int
  244. write_header(struct archive_write *a, struct archive_entry *entry)
  245. {
  246. struct cpio *cpio;
  247. const char *p, *path;
  248. int pathlength, ret, ret_final;
  249. int64_t ino;
  250. char h[76];
  251. struct archive_string_conv *sconv;
  252. struct archive_entry *entry_main;
  253. size_t len;
  254. cpio = (struct cpio *)a->format_data;
  255. ret_final = ARCHIVE_OK;
  256. sconv = get_sconv(a);
  257. #if defined(_WIN32) && !defined(__CYGWIN__)
  258. /* Make sure the path separators in pahtname, hardlink and symlink
  259. * are all slash '/', not the Windows path separator '\'. */
  260. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  261. if (entry_main == NULL) {
  262. archive_set_error(&a->archive, ENOMEM,
  263. "Can't allocate ustar data");
  264. return(ARCHIVE_FATAL);
  265. }
  266. if (entry != entry_main)
  267. entry = entry_main;
  268. else
  269. entry_main = NULL;
  270. #else
  271. entry_main = NULL;
  272. #endif
  273. ret = archive_entry_pathname_l(entry, &path, &len, sconv);
  274. if (ret != 0) {
  275. if (errno == ENOMEM) {
  276. archive_set_error(&a->archive, ENOMEM,
  277. "Can't allocate memory for Pathname");
  278. ret_final = ARCHIVE_FATAL;
  279. goto exit_write_header;
  280. }
  281. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  282. "Can't translate pathname '%s' to %s",
  283. archive_entry_pathname(entry),
  284. archive_string_conversion_charset_name(sconv));
  285. ret_final = ARCHIVE_WARN;
  286. }
  287. /* Include trailing null. */
  288. pathlength = (int)len + 1;
  289. memset(h, 0, sizeof(h));
  290. format_octal(070707, h + c_magic_offset, c_magic_size);
  291. format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
  292. ino = synthesize_ino_value(cpio, entry);
  293. if (ino < 0) {
  294. archive_set_error(&a->archive, ENOMEM,
  295. "No memory for ino translation table");
  296. ret_final = ARCHIVE_FATAL;
  297. goto exit_write_header;
  298. } else if (ino > 0777777) {
  299. archive_set_error(&a->archive, ERANGE,
  300. "Too many files for this cpio format");
  301. ret_final = ARCHIVE_FATAL;
  302. goto exit_write_header;
  303. }
  304. format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
  305. /* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
  306. format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
  307. format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
  308. format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
  309. format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
  310. if (archive_entry_filetype(entry) == AE_IFBLK
  311. || archive_entry_filetype(entry) == AE_IFCHR)
  312. format_octal(archive_entry_dev(entry), h + c_rdev_offset, c_rdev_size);
  313. else
  314. format_octal(0, h + c_rdev_offset, c_rdev_size);
  315. format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
  316. format_octal(pathlength, h + c_namesize_offset, c_namesize_size);
  317. /* Non-regular files don't store bodies. */
  318. if (archive_entry_filetype(entry) != AE_IFREG)
  319. archive_entry_set_size(entry, 0);
  320. /* Symlinks get the link written as the body of the entry. */
  321. ret = archive_entry_symlink_l(entry, &p, &len, sconv);
  322. if (ret != 0) {
  323. if (errno == ENOMEM) {
  324. archive_set_error(&a->archive, ENOMEM,
  325. "Can't allocate memory for Linkname");
  326. ret_final = ARCHIVE_FATAL;
  327. goto exit_write_header;
  328. }
  329. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  330. "Can't translate linkname '%s' to %s",
  331. archive_entry_symlink(entry),
  332. archive_string_conversion_charset_name(sconv));
  333. ret_final = ARCHIVE_WARN;
  334. }
  335. if (len > 0 && p != NULL && *p != '\0')
  336. ret = format_octal(strlen(p), h + c_filesize_offset,
  337. c_filesize_size);
  338. else
  339. ret = format_octal(archive_entry_size(entry),
  340. h + c_filesize_offset, c_filesize_size);
  341. if (ret) {
  342. archive_set_error(&a->archive, ERANGE,
  343. "File is too large for cpio format.");
  344. ret_final = ARCHIVE_FAILED;
  345. goto exit_write_header;
  346. }
  347. ret = __archive_write_output(a, h, sizeof(h));
  348. if (ret != ARCHIVE_OK) {
  349. ret_final = ARCHIVE_FATAL;
  350. goto exit_write_header;
  351. }
  352. ret = __archive_write_output(a, path, pathlength);
  353. if (ret != ARCHIVE_OK) {
  354. ret_final = ARCHIVE_FATAL;
  355. goto exit_write_header;
  356. }
  357. cpio->entry_bytes_remaining = archive_entry_size(entry);
  358. /* Write the symlink now. */
  359. if (p != NULL && *p != '\0') {
  360. ret = __archive_write_output(a, p, strlen(p));
  361. if (ret != ARCHIVE_OK) {
  362. ret_final = ARCHIVE_FATAL;
  363. goto exit_write_header;
  364. }
  365. }
  366. exit_write_header:
  367. if (entry_main)
  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. }