archive_write_set_format_cpio_newc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 *)calloc(1, sizeof(*cpio));
  108. if (cpio == NULL) {
  109. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  110. return (ARCHIVE_FATAL);
  111. }
  112. a->format_data = cpio;
  113. a->format_name = "cpio";
  114. a->format_options = archive_write_newc_options;
  115. a->format_write_header = archive_write_newc_header;
  116. a->format_write_data = archive_write_newc_data;
  117. a->format_finish_entry = archive_write_newc_finish_entry;
  118. a->format_close = archive_write_newc_close;
  119. a->format_free = archive_write_newc_free;
  120. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  121. a->archive.archive_format_name = "SVR4 cpio nocrc";
  122. return (ARCHIVE_OK);
  123. }
  124. static int
  125. archive_write_newc_options(struct archive_write *a, const char *key,
  126. const char *val)
  127. {
  128. struct cpio *cpio = (struct cpio *)a->format_data;
  129. int ret = ARCHIVE_FAILED;
  130. if (strcmp(key, "hdrcharset") == 0) {
  131. if (val == NULL || val[0] == 0)
  132. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  133. "%s: hdrcharset option needs a character-set name",
  134. a->format_name);
  135. else {
  136. cpio->opt_sconv = archive_string_conversion_to_charset(
  137. &a->archive, val, 0);
  138. if (cpio->opt_sconv != NULL)
  139. ret = ARCHIVE_OK;
  140. else
  141. ret = ARCHIVE_FATAL;
  142. }
  143. return (ret);
  144. }
  145. /* Note: The "warn" return is just to inform the options
  146. * supervisor that we didn't handle it. It will generate
  147. * a suitable error if no one used this option. */
  148. return (ARCHIVE_WARN);
  149. }
  150. static struct archive_string_conv *
  151. get_sconv(struct archive_write *a)
  152. {
  153. struct cpio *cpio;
  154. struct archive_string_conv *sconv;
  155. cpio = (struct cpio *)a->format_data;
  156. sconv = cpio->opt_sconv;
  157. if (sconv == NULL) {
  158. if (!cpio->init_default_conversion) {
  159. cpio->sconv_default =
  160. archive_string_default_conversion_for_write(
  161. &(a->archive));
  162. cpio->init_default_conversion = 1;
  163. }
  164. sconv = cpio->sconv_default;
  165. }
  166. return (sconv);
  167. }
  168. static int
  169. archive_write_newc_header(struct archive_write *a, struct archive_entry *entry)
  170. {
  171. const char *path;
  172. size_t len;
  173. if (archive_entry_filetype(entry) == 0) {
  174. archive_set_error(&a->archive, -1, "Filetype required");
  175. return (ARCHIVE_FAILED);
  176. }
  177. if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
  178. && errno == ENOMEM) {
  179. archive_set_error(&a->archive, ENOMEM,
  180. "Can't allocate memory for Pathname");
  181. return (ARCHIVE_FATAL);
  182. }
  183. if (len == 0 || path == NULL || path[0] == '\0') {
  184. archive_set_error(&a->archive, -1, "Pathname required");
  185. return (ARCHIVE_FAILED);
  186. }
  187. if (archive_entry_hardlink(entry) == NULL
  188. && (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0)) {
  189. archive_set_error(&a->archive, -1, "Size required");
  190. return (ARCHIVE_FAILED);
  191. }
  192. return write_header(a, entry);
  193. }
  194. static int
  195. write_header(struct archive_write *a, struct archive_entry *entry)
  196. {
  197. int64_t ino;
  198. struct cpio *cpio;
  199. const char *p, *path;
  200. int pathlength, ret, ret_final;
  201. char h[c_header_size];
  202. struct archive_string_conv *sconv;
  203. struct archive_entry *entry_main;
  204. size_t len;
  205. int pad;
  206. cpio = (struct cpio *)a->format_data;
  207. ret_final = ARCHIVE_OK;
  208. sconv = get_sconv(a);
  209. #if defined(_WIN32) && !defined(__CYGWIN__)
  210. /* Make sure the path separators in pathname, hardlink and symlink
  211. * are all slash '/', not the Windows path separator '\'. */
  212. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  213. if (entry_main == NULL) {
  214. archive_set_error(&a->archive, ENOMEM,
  215. "Can't allocate ustar data");
  216. return(ARCHIVE_FATAL);
  217. }
  218. if (entry != entry_main)
  219. entry = entry_main;
  220. else
  221. entry_main = NULL;
  222. #else
  223. entry_main = NULL;
  224. #endif
  225. ret = archive_entry_pathname_l(entry, &path, &len, sconv);
  226. if (ret != 0) {
  227. if (errno == ENOMEM) {
  228. archive_set_error(&a->archive, ENOMEM,
  229. "Can't allocate memory for Pathname");
  230. ret_final = ARCHIVE_FATAL;
  231. goto exit_write_header;
  232. }
  233. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  234. "Can't translate pathname '%s' to %s",
  235. archive_entry_pathname(entry),
  236. archive_string_conversion_charset_name(sconv));
  237. ret_final = ARCHIVE_WARN;
  238. }
  239. pathlength = (int)len + 1; /* Include trailing null. */
  240. memset(h, 0, c_header_size);
  241. format_hex(0x070701, h + c_magic_offset, c_magic_size);
  242. format_hex(archive_entry_devmajor(entry), h + c_devmajor_offset,
  243. c_devmajor_size);
  244. format_hex(archive_entry_devminor(entry), h + c_devminor_offset,
  245. c_devminor_size);
  246. ino = archive_entry_ino64(entry);
  247. if (ino > 0xffffffff) {
  248. archive_set_error(&a->archive, ERANGE,
  249. "large inode number truncated");
  250. ret_final = ARCHIVE_WARN;
  251. }
  252. /* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
  253. format_hex(ino & 0xffffffff, h + c_ino_offset, c_ino_size);
  254. format_hex(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
  255. format_hex(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
  256. format_hex(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
  257. format_hex(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
  258. if (archive_entry_filetype(entry) == AE_IFBLK
  259. || archive_entry_filetype(entry) == AE_IFCHR) {
  260. format_hex(archive_entry_rdevmajor(entry), h + c_rdevmajor_offset, c_rdevmajor_size);
  261. format_hex(archive_entry_rdevminor(entry), h + c_rdevminor_offset, c_rdevminor_size);
  262. } else {
  263. format_hex(0, h + c_rdevmajor_offset, c_rdevmajor_size);
  264. format_hex(0, h + c_rdevminor_offset, c_rdevminor_size);
  265. }
  266. format_hex(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
  267. format_hex(pathlength, h + c_namesize_offset, c_namesize_size);
  268. format_hex(0, h + c_checksum_offset, c_checksum_size);
  269. /* Non-regular files don't store bodies. */
  270. if (archive_entry_filetype(entry) != AE_IFREG)
  271. archive_entry_set_size(entry, 0);
  272. /* Symlinks get the link written as the body of the entry. */
  273. ret = archive_entry_symlink_l(entry, &p, &len, sconv);
  274. if (ret != 0) {
  275. if (errno == ENOMEM) {
  276. archive_set_error(&a->archive, ENOMEM,
  277. "Can't allocate memory for Likname");
  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 linkname '%s' to %s",
  283. archive_entry_symlink(entry),
  284. archive_string_conversion_charset_name(sconv));
  285. ret_final = ARCHIVE_WARN;
  286. }
  287. if (len > 0 && p != NULL && *p != '\0')
  288. ret = format_hex(strlen(p), h + c_filesize_offset,
  289. c_filesize_size);
  290. else
  291. ret = format_hex(archive_entry_size(entry),
  292. h + c_filesize_offset, c_filesize_size);
  293. if (ret) {
  294. archive_set_error(&a->archive, ERANGE,
  295. "File is too large for this format.");
  296. ret_final = ARCHIVE_FAILED;
  297. goto exit_write_header;
  298. }
  299. ret = __archive_write_output(a, h, c_header_size);
  300. if (ret != ARCHIVE_OK) {
  301. ret_final = ARCHIVE_FATAL;
  302. goto exit_write_header;
  303. }
  304. /* Pad pathname to even length. */
  305. ret = __archive_write_output(a, path, pathlength);
  306. if (ret != ARCHIVE_OK) {
  307. ret_final = ARCHIVE_FATAL;
  308. goto exit_write_header;
  309. }
  310. pad = PAD4(pathlength + c_header_size);
  311. if (pad) {
  312. ret = __archive_write_output(a, "\0\0\0", pad);
  313. if (ret != ARCHIVE_OK) {
  314. ret_final = ARCHIVE_FATAL;
  315. goto exit_write_header;
  316. }
  317. }
  318. cpio->entry_bytes_remaining = archive_entry_size(entry);
  319. cpio->padding = (int)PAD4(cpio->entry_bytes_remaining);
  320. /* Write the symlink now. */
  321. if (p != NULL && *p != '\0') {
  322. ret = __archive_write_output(a, p, strlen(p));
  323. if (ret != ARCHIVE_OK) {
  324. ret_final = ARCHIVE_FATAL;
  325. goto exit_write_header;
  326. }
  327. pad = PAD4(strlen(p));
  328. ret = __archive_write_output(a, "\0\0\0", pad);
  329. if (ret != ARCHIVE_OK) {
  330. ret_final = ARCHIVE_FATAL;
  331. goto exit_write_header;
  332. }
  333. }
  334. exit_write_header:
  335. if (entry_main)
  336. archive_entry_free(entry_main);
  337. return (ret_final);
  338. }
  339. static ssize_t
  340. archive_write_newc_data(struct archive_write *a, const void *buff, size_t s)
  341. {
  342. struct cpio *cpio;
  343. int ret;
  344. cpio = (struct cpio *)a->format_data;
  345. if (s > cpio->entry_bytes_remaining)
  346. s = (size_t)cpio->entry_bytes_remaining;
  347. ret = __archive_write_output(a, buff, s);
  348. cpio->entry_bytes_remaining -= s;
  349. if (ret >= 0)
  350. return (s);
  351. else
  352. return (ret);
  353. }
  354. /*
  355. * Format a number into the specified field.
  356. */
  357. static int
  358. format_hex(int64_t v, void *p, int digits)
  359. {
  360. int64_t max;
  361. int ret;
  362. max = (((int64_t)1) << (digits * 4)) - 1;
  363. if (v >= 0 && v <= max) {
  364. format_hex_recursive(v, (char *)p, digits);
  365. ret = 0;
  366. } else {
  367. format_hex_recursive(max, (char *)p, digits);
  368. ret = -1;
  369. }
  370. return (ret);
  371. }
  372. static int64_t
  373. format_hex_recursive(int64_t v, char *p, int s)
  374. {
  375. if (s == 0)
  376. return (v);
  377. v = format_hex_recursive(v, p+1, s-1);
  378. *p = "0123456789abcdef"[v & 0xf];
  379. return (v >> 4);
  380. }
  381. static int
  382. archive_write_newc_close(struct archive_write *a)
  383. {
  384. int er;
  385. struct archive_entry *trailer;
  386. trailer = archive_entry_new();
  387. archive_entry_set_nlink(trailer, 1);
  388. archive_entry_set_size(trailer, 0);
  389. archive_entry_set_pathname(trailer, "TRAILER!!!");
  390. /* Bypass the required data checks. */
  391. er = write_header(a, trailer);
  392. archive_entry_free(trailer);
  393. return (er);
  394. }
  395. static int
  396. archive_write_newc_free(struct archive_write *a)
  397. {
  398. struct cpio *cpio;
  399. cpio = (struct cpio *)a->format_data;
  400. free(cpio);
  401. a->format_data = NULL;
  402. return (ARCHIVE_OK);
  403. }
  404. static int
  405. archive_write_newc_finish_entry(struct archive_write *a)
  406. {
  407. struct cpio *cpio;
  408. cpio = (struct cpio *)a->format_data;
  409. return (__archive_write_nulls(a,
  410. (size_t)cpio->entry_bytes_remaining + cpio->padding));
  411. }