archive_write_set_format_cpio_newc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2006 Rudolf Marek SYSGO s.r.o.
  4. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio_newc.c 201160 2009-12-29 05:41:57Z kientzle $");
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  32. #include <stdio.h>
  33. #ifdef HAVE_STDLIB_H
  34. #include <stdlib.h>
  35. #endif
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #include "archive.h"
  40. #include "archive_entry.h"
  41. #include "archive_entry_locale.h"
  42. #include "archive_private.h"
  43. #include "archive_write_private.h"
  44. static ssize_t archive_write_newc_data(struct archive_write *,
  45. const void *buff, size_t s);
  46. static int archive_write_newc_close(struct archive_write *);
  47. static int archive_write_newc_free(struct archive_write *);
  48. static int archive_write_newc_finish_entry(struct archive_write *);
  49. static int archive_write_newc_header(struct archive_write *,
  50. struct archive_entry *);
  51. static int archive_write_newc_options(struct archive_write *,
  52. const char *, const char *);
  53. static int format_hex(int64_t, void *, int);
  54. static int64_t format_hex_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. int padding;
  59. struct archive_string_conv *opt_sconv;
  60. struct archive_string_conv *sconv_default;
  61. int init_default_conversion;
  62. };
  63. #define c_magic_offset 0
  64. #define c_magic_size 6
  65. #define c_ino_offset 6
  66. #define c_ino_size 8
  67. #define c_mode_offset 14
  68. #define c_mode_size 8
  69. #define c_uid_offset 22
  70. #define c_uid_size 8
  71. #define c_gid_offset 30
  72. #define c_gid_size 8
  73. #define c_nlink_offset 38
  74. #define c_nlink_size 8
  75. #define c_mtime_offset 46
  76. #define c_mtime_size 8
  77. #define c_filesize_offset 54
  78. #define c_filesize_size 8
  79. #define c_devmajor_offset 62
  80. #define c_devmajor_size 8
  81. #define c_devminor_offset 70
  82. #define c_devminor_size 8
  83. #define c_rdevmajor_offset 78
  84. #define c_rdevmajor_size 8
  85. #define c_rdevminor_offset 86
  86. #define c_rdevminor_size 8
  87. #define c_namesize_offset 94
  88. #define c_namesize_size 8
  89. #define c_checksum_offset 102
  90. #define c_checksum_size 8
  91. #define c_header_size 110
  92. /* Logic trick: difference between 'n' and next multiple of 4 */
  93. #define PAD4(n) (3 & (1 + ~(n)))
  94. /*
  95. * Set output format to 'cpio' format.
  96. */
  97. int
  98. archive_write_set_format_cpio_newc(struct archive *_a)
  99. {
  100. struct archive_write *a = (struct archive_write *)_a;
  101. struct cpio *cpio;
  102. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  103. ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_newc");
  104. /* If someone else was already registered, unregister them. */
  105. if (a->format_free != NULL)
  106. (a->format_free)(a);
  107. cpio = (struct cpio *)malloc(sizeof(*cpio));
  108. if (cpio == NULL) {
  109. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  110. return (ARCHIVE_FATAL);
  111. }
  112. memset(cpio, 0, sizeof(*cpio));
  113. a->format_data = cpio;
  114. a->format_name = "cpio";
  115. a->format_options = archive_write_newc_options;
  116. a->format_write_header = archive_write_newc_header;
  117. a->format_write_data = archive_write_newc_data;
  118. a->format_finish_entry = archive_write_newc_finish_entry;
  119. a->format_close = archive_write_newc_close;
  120. a->format_free = archive_write_newc_free;
  121. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  122. a->archive.archive_format_name = "SVR4 cpio nocrc";
  123. return (ARCHIVE_OK);
  124. }
  125. static int
  126. archive_write_newc_options(struct archive_write *a, const char *key,
  127. const char *val)
  128. {
  129. struct cpio *cpio = (struct cpio *)a->format_data;
  130. int ret = ARCHIVE_FAILED;
  131. if (strcmp(key, "hdrcharset") == 0) {
  132. if (val == NULL || val[0] == 0)
  133. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  134. "%s: hdrcharset option needs a character-set name",
  135. a->format_name);
  136. else {
  137. cpio->opt_sconv = archive_string_conversion_to_charset(
  138. &a->archive, val, 0);
  139. if (cpio->opt_sconv != NULL)
  140. ret = ARCHIVE_OK;
  141. else
  142. ret = ARCHIVE_FATAL;
  143. }
  144. return (ret);
  145. }
  146. /* Note: The "warn" return is just to inform the options
  147. * supervisor that we didn't handle it. It will generate
  148. * a suitable error if no one used this option. */
  149. return (ARCHIVE_WARN);
  150. }
  151. static struct archive_string_conv *
  152. get_sconv(struct archive_write *a)
  153. {
  154. struct cpio *cpio;
  155. struct archive_string_conv *sconv;
  156. cpio = (struct cpio *)a->format_data;
  157. sconv = cpio->opt_sconv;
  158. if (sconv == NULL) {
  159. if (!cpio->init_default_conversion) {
  160. cpio->sconv_default =
  161. archive_string_default_conversion_for_write(
  162. &(a->archive));
  163. cpio->init_default_conversion = 1;
  164. }
  165. sconv = cpio->sconv_default;
  166. }
  167. return (sconv);
  168. }
  169. static int
  170. archive_write_newc_header(struct archive_write *a, struct archive_entry *entry)
  171. {
  172. const char *path;
  173. size_t len;
  174. if (archive_entry_filetype(entry) == 0) {
  175. archive_set_error(&a->archive, -1, "Filetype required");
  176. return (ARCHIVE_FAILED);
  177. }
  178. if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
  179. && errno == ENOMEM) {
  180. archive_set_error(&a->archive, ENOMEM,
  181. "Can't allocate memory for Pathname");
  182. return (ARCHIVE_FATAL);
  183. }
  184. if (len == 0 || path == NULL || path[0] == '\0') {
  185. archive_set_error(&a->archive, -1, "Pathname required");
  186. return (ARCHIVE_FAILED);
  187. }
  188. if (archive_entry_hardlink(entry) == NULL
  189. && (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0)) {
  190. archive_set_error(&a->archive, -1, "Size required");
  191. return (ARCHIVE_FAILED);
  192. }
  193. return write_header(a, entry);
  194. }
  195. static int
  196. write_header(struct archive_write *a, struct archive_entry *entry)
  197. {
  198. int64_t ino;
  199. struct cpio *cpio;
  200. const char *p, *path;
  201. int pathlength, ret, ret_final;
  202. char h[c_header_size];
  203. struct archive_string_conv *sconv;
  204. struct archive_entry *entry_main;
  205. size_t len;
  206. int pad;
  207. cpio = (struct cpio *)a->format_data;
  208. ret_final = ARCHIVE_OK;
  209. sconv = get_sconv(a);
  210. #if defined(_WIN32) && !defined(__CYGWIN__)
  211. /* Make sure the path separators in pahtname, hardlink and symlink
  212. * are all slash '/', not the Windows path separator '\'. */
  213. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  214. if (entry_main == NULL) {
  215. archive_set_error(&a->archive, ENOMEM,
  216. "Can't allocate ustar data");
  217. return(ARCHIVE_FATAL);
  218. }
  219. if (entry != entry_main)
  220. entry = entry_main;
  221. else
  222. entry_main = NULL;
  223. #else
  224. entry_main = NULL;
  225. #endif
  226. ret = archive_entry_pathname_l(entry, &path, &len, sconv);
  227. if (ret != 0) {
  228. if (errno == ENOMEM) {
  229. archive_set_error(&a->archive, ENOMEM,
  230. "Can't allocate memory for Pathname");
  231. ret_final = ARCHIVE_FATAL;
  232. goto exit_write_header;
  233. }
  234. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  235. "Can't translate pathname '%s' to %s",
  236. archive_entry_pathname(entry),
  237. archive_string_conversion_charset_name(sconv));
  238. ret_final = ARCHIVE_WARN;
  239. }
  240. pathlength = (int)len + 1; /* Include trailing null. */
  241. memset(h, 0, c_header_size);
  242. format_hex(0x070701, h + c_magic_offset, c_magic_size);
  243. format_hex(archive_entry_devmajor(entry), h + c_devmajor_offset,
  244. c_devmajor_size);
  245. format_hex(archive_entry_devminor(entry), h + c_devminor_offset,
  246. c_devminor_size);
  247. ino = archive_entry_ino64(entry);
  248. if (ino > 0xffffffff) {
  249. archive_set_error(&a->archive, ERANGE,
  250. "large inode number truncated");
  251. ret_final = ARCHIVE_WARN;
  252. }
  253. /* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
  254. format_hex(ino & 0xffffffff, h + c_ino_offset, c_ino_size);
  255. format_hex(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
  256. format_hex(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
  257. format_hex(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
  258. format_hex(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
  259. if (archive_entry_filetype(entry) == AE_IFBLK
  260. || archive_entry_filetype(entry) == AE_IFCHR) {
  261. format_hex(archive_entry_rdevmajor(entry), h + c_rdevmajor_offset, c_rdevmajor_size);
  262. format_hex(archive_entry_rdevminor(entry), h + c_rdevminor_offset, c_rdevminor_size);
  263. } else {
  264. format_hex(0, h + c_rdevmajor_offset, c_rdevmajor_size);
  265. format_hex(0, h + c_rdevminor_offset, c_rdevminor_size);
  266. }
  267. format_hex(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
  268. format_hex(pathlength, h + c_namesize_offset, c_namesize_size);
  269. format_hex(0, h + c_checksum_offset, c_checksum_size);
  270. /* Non-regular files don't store bodies. */
  271. if (archive_entry_filetype(entry) != AE_IFREG)
  272. archive_entry_set_size(entry, 0);
  273. /* Symlinks get the link written as the body of the entry. */
  274. ret = archive_entry_symlink_l(entry, &p, &len, sconv);
  275. if (ret != 0) {
  276. if (errno == ENOMEM) {
  277. archive_set_error(&a->archive, ENOMEM,
  278. "Can't allocate memory for Likname");
  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 linkname '%s' to %s",
  284. archive_entry_symlink(entry),
  285. archive_string_conversion_charset_name(sconv));
  286. ret_final = ARCHIVE_WARN;
  287. }
  288. if (len > 0 && p != NULL && *p != '\0')
  289. ret = format_hex(strlen(p), h + c_filesize_offset,
  290. c_filesize_size);
  291. else
  292. ret = format_hex(archive_entry_size(entry),
  293. h + c_filesize_offset, c_filesize_size);
  294. if (ret) {
  295. archive_set_error(&a->archive, ERANGE,
  296. "File is too large for this format.");
  297. ret_final = ARCHIVE_FAILED;
  298. goto exit_write_header;
  299. }
  300. ret = __archive_write_output(a, h, c_header_size);
  301. if (ret != ARCHIVE_OK) {
  302. ret_final = ARCHIVE_FATAL;
  303. goto exit_write_header;
  304. }
  305. /* Pad pathname to even length. */
  306. ret = __archive_write_output(a, path, pathlength);
  307. if (ret != ARCHIVE_OK) {
  308. ret_final = ARCHIVE_FATAL;
  309. goto exit_write_header;
  310. }
  311. pad = PAD4(pathlength + c_header_size);
  312. if (pad) {
  313. ret = __archive_write_output(a, "\0\0\0", pad);
  314. if (ret != ARCHIVE_OK) {
  315. ret_final = ARCHIVE_FATAL;
  316. goto exit_write_header;
  317. }
  318. }
  319. cpio->entry_bytes_remaining = archive_entry_size(entry);
  320. cpio->padding = (int)PAD4(cpio->entry_bytes_remaining);
  321. /* Write the symlink now. */
  322. if (p != NULL && *p != '\0') {
  323. ret = __archive_write_output(a, p, strlen(p));
  324. if (ret != ARCHIVE_OK) {
  325. ret_final = ARCHIVE_FATAL;
  326. goto exit_write_header;
  327. }
  328. pad = PAD4(strlen(p));
  329. ret = __archive_write_output(a, "\0\0\0", pad);
  330. if (ret != ARCHIVE_OK) {
  331. ret_final = ARCHIVE_FATAL;
  332. goto exit_write_header;
  333. }
  334. }
  335. exit_write_header:
  336. if (entry_main)
  337. archive_entry_free(entry_main);
  338. return (ret_final);
  339. }
  340. static ssize_t
  341. archive_write_newc_data(struct archive_write *a, const void *buff, size_t s)
  342. {
  343. struct cpio *cpio;
  344. int ret;
  345. cpio = (struct cpio *)a->format_data;
  346. if (s > cpio->entry_bytes_remaining)
  347. s = (size_t)cpio->entry_bytes_remaining;
  348. ret = __archive_write_output(a, buff, s);
  349. cpio->entry_bytes_remaining -= s;
  350. if (ret >= 0)
  351. return (s);
  352. else
  353. return (ret);
  354. }
  355. /*
  356. * Format a number into the specified field.
  357. */
  358. static int
  359. format_hex(int64_t v, void *p, int digits)
  360. {
  361. int64_t max;
  362. int ret;
  363. max = (((int64_t)1) << (digits * 4)) - 1;
  364. if (v >= 0 && v <= max) {
  365. format_hex_recursive(v, (char *)p, digits);
  366. ret = 0;
  367. } else {
  368. format_hex_recursive(max, (char *)p, digits);
  369. ret = -1;
  370. }
  371. return (ret);
  372. }
  373. static int64_t
  374. format_hex_recursive(int64_t v, char *p, int s)
  375. {
  376. if (s == 0)
  377. return (v);
  378. v = format_hex_recursive(v, p+1, s-1);
  379. *p = "0123456789abcdef"[v & 0xf];
  380. return (v >> 4);
  381. }
  382. static int
  383. archive_write_newc_close(struct archive_write *a)
  384. {
  385. int er;
  386. struct archive_entry *trailer;
  387. trailer = archive_entry_new();
  388. archive_entry_set_nlink(trailer, 1);
  389. archive_entry_set_size(trailer, 0);
  390. archive_entry_set_pathname(trailer, "TRAILER!!!");
  391. /* Bypass the required data checks. */
  392. er = write_header(a, trailer);
  393. archive_entry_free(trailer);
  394. return (er);
  395. }
  396. static int
  397. archive_write_newc_free(struct archive_write *a)
  398. {
  399. struct cpio *cpio;
  400. cpio = (struct cpio *)a->format_data;
  401. free(cpio);
  402. a->format_data = NULL;
  403. return (ARCHIVE_OK);
  404. }
  405. static int
  406. archive_write_newc_finish_entry(struct archive_write *a)
  407. {
  408. struct cpio *cpio;
  409. cpio = (struct cpio *)a->format_data;
  410. return (__archive_write_nulls(a,
  411. (size_t)cpio->entry_bytes_remaining + cpio->padding));
  412. }