archive_write_set_format_cpio_binary.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. #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_entry_locale.h"
  40. #include "archive_private.h"
  41. #include "archive_write_private.h"
  42. #include "archive_write_set_format_private.h"
  43. static ssize_t archive_write_binary_data(struct archive_write *,
  44. const void *buff, size_t s);
  45. static int archive_write_binary_close(struct archive_write *);
  46. static int archive_write_binary_free(struct archive_write *);
  47. static int archive_write_binary_finish_entry(struct archive_write *);
  48. static int archive_write_binary_header(struct archive_write *,
  49. struct archive_entry *);
  50. static int archive_write_binary_options(struct archive_write *,
  51. const char *, const char *);
  52. static int write_header(struct archive_write *, struct archive_entry *);
  53. struct cpio {
  54. uint64_t entry_bytes_remaining;
  55. int64_t ino_next;
  56. struct { int64_t old; int new;} *ino_list;
  57. size_t ino_list_size;
  58. size_t ino_list_next;
  59. struct archive_string_conv *opt_sconv;
  60. struct archive_string_conv *sconv_default;
  61. int init_default_conversion;
  62. };
  63. /* This struct needs to be packed to get the header right */
  64. #if defined(__GNUC__)
  65. #define PACKED(x) x __attribute__((packed))
  66. #elif defined(_MSC_VER)
  67. #define PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
  68. #else
  69. #define PACKED(x) x
  70. #endif
  71. #define HSIZE 26
  72. PACKED(struct cpio_binary_header {
  73. uint16_t h_magic;
  74. uint16_t h_dev;
  75. uint16_t h_ino;
  76. uint16_t h_mode;
  77. uint16_t h_uid;
  78. uint16_t h_gid;
  79. uint16_t h_nlink;
  80. uint16_t h_majmin;
  81. uint32_t h_mtime;
  82. uint16_t h_namesize;
  83. uint32_t h_filesize;
  84. });
  85. /* Back in the day, the 7th Edition cpio.c had this, to
  86. * adapt to, as the comment said, "VAX, Interdata, ...":
  87. *
  88. * union { long l; short s[2]; char c[4]; } U;
  89. * #define MKSHORT(v,lv) {U.l=1L;if(U.c[0]) U.l=lv,v[0]=U.s[1],v[1]=U.s[0]; else U.l=lv,v[0]=U.s[0],v[1]=U.s[1];}
  90. * long mklong(v)
  91. * short v[];
  92. * {
  93. * U.l = 1;
  94. * if(U.c[0])
  95. * U.s[0] = v[1], U.s[1] = v[0];
  96. * else
  97. * U.s[0] = v[0], U.s[1] = v[1];
  98. * return U.l;
  99. * }
  100. *
  101. * Of course, that assumes that all machines have little-endian shorts,
  102. * and just adapts the others to the special endianness of the PDP-11.
  103. *
  104. * Now, we could do this:
  105. *
  106. * union { uint32_t l; uint16_t s[2]; uint8_t c[4]; } U;
  107. * #define PUTI16(v,sv) {U.s[0]=1;if(U.c[0]) v=sv; else U.s[0]=sv,U.c[2]=U.c[1],U.c[3]=U.c[0],v=U.s[1];}
  108. * #define PUTI32(v,lv) {char_t Ut;U.l=1;if(U.c[0]) U.l=lv,v[0]=U.s[1],v[1]=U.s[0]; else U.l=lv,Ut=U.c[0],U.c[0]=U.c[1],U.c[1]=Ut,Ut=U.c[2],U.c[2]=U.c[3],U.c[3]=Ut,v[0]=U.s[0],v[1]=U.s[1];}
  109. *
  110. * ...but it feels a little better to do it like this:
  111. */
  112. static uint16_t la_swap16(uint16_t in) {
  113. union {
  114. uint16_t s[2];
  115. uint8_t c[4];
  116. } U;
  117. U.s[0] = 1;
  118. if (U.c[0])
  119. return in;
  120. else {
  121. U.s[0] = in;
  122. U.c[2] = U.c[1];
  123. U.c[3] = U.c[0];
  124. return U.s[1];
  125. }
  126. /* NOTREACHED */
  127. }
  128. static uint32_t la_swap32(uint32_t in) {
  129. union {
  130. uint32_t l;
  131. uint16_t s[2];
  132. uint8_t c[4];
  133. } U;
  134. U.l = 1;
  135. if (U.c[0]) { /* Little-endian */
  136. uint16_t t;
  137. U.l = in;
  138. t = U.s[0];
  139. U.s[0] = U.s[1];
  140. U.s[1] = t;
  141. } else if (U.c[3]) { /* Big-endian */
  142. U.l = in;
  143. U.s[0] = la_swap16(U.s[0]);
  144. U.s[1] = la_swap16(U.s[1]);
  145. } else { /* PDP-endian */
  146. U.l = in;
  147. }
  148. return U.l;
  149. }
  150. /*
  151. * Set output format to the selected binary variant
  152. */
  153. static int
  154. archive_write_set_format_cpio_binary(struct archive *_a, int format)
  155. {
  156. struct archive_write *a = (struct archive_write *)_a;
  157. struct cpio *cpio;
  158. if (sizeof(struct cpio_binary_header) != HSIZE) {
  159. archive_set_error(&a->archive, EINVAL,
  160. "Binary cpio format not supported on this platform");
  161. return (ARCHIVE_FATAL);
  162. }
  163. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  164. ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_binary");
  165. /* If someone else was already registered, unregister them. */
  166. if (a->format_free != NULL)
  167. (a->format_free)(a);
  168. cpio = calloc(1, sizeof(*cpio));
  169. if (cpio == NULL) {
  170. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  171. return (ARCHIVE_FATAL);
  172. }
  173. a->format_data = cpio;
  174. a->format_name = "cpio";
  175. a->format_options = archive_write_binary_options;
  176. a->format_write_header = archive_write_binary_header;
  177. a->format_write_data = archive_write_binary_data;
  178. a->format_finish_entry = archive_write_binary_finish_entry;
  179. a->format_close = archive_write_binary_close;
  180. a->format_free = archive_write_binary_free;
  181. a->archive.archive_format = format;
  182. switch (format) {
  183. case ARCHIVE_FORMAT_CPIO_PWB:
  184. a->archive.archive_format_name = "PWB cpio";
  185. break;
  186. case ARCHIVE_FORMAT_CPIO_BIN_LE:
  187. a->archive.archive_format_name = "7th Edition cpio";
  188. break;
  189. default:
  190. archive_set_error(&a->archive, EINVAL, "binary format must be 'pwb' or 'bin'");
  191. return (ARCHIVE_FATAL);
  192. }
  193. return (ARCHIVE_OK);
  194. }
  195. /*
  196. * Set output format to PWB (6th Edition) binary format
  197. */
  198. int
  199. archive_write_set_format_cpio_pwb(struct archive *_a)
  200. {
  201. return archive_write_set_format_cpio_binary(_a, ARCHIVE_FORMAT_CPIO_PWB);
  202. }
  203. /*
  204. * Set output format to 7th Edition binary format
  205. */
  206. int
  207. archive_write_set_format_cpio_bin(struct archive *_a)
  208. {
  209. return archive_write_set_format_cpio_binary(_a, ARCHIVE_FORMAT_CPIO_BIN_LE);
  210. }
  211. static int
  212. archive_write_binary_options(struct archive_write *a, const char *key,
  213. const char *val)
  214. {
  215. struct cpio *cpio = (struct cpio *)a->format_data;
  216. int ret = ARCHIVE_FAILED;
  217. if (strcmp(key, "hdrcharset") == 0) {
  218. if (val == NULL || val[0] == 0)
  219. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  220. "%s: hdrcharset option needs a character-set name",
  221. a->format_name);
  222. else {
  223. cpio->opt_sconv = archive_string_conversion_to_charset(
  224. &a->archive, val, 0);
  225. if (cpio->opt_sconv != NULL)
  226. ret = ARCHIVE_OK;
  227. else
  228. ret = ARCHIVE_FATAL;
  229. }
  230. return (ret);
  231. }
  232. /* Note: The "warn" return is just to inform the options
  233. * supervisor that we didn't handle it. It will generate
  234. * a suitable error if no one used this option. */
  235. return (ARCHIVE_WARN);
  236. }
  237. /*
  238. * Ino values are as long as 64 bits on some systems; cpio format
  239. * only allows 16 bits and relies on the ino values to identify hardlinked
  240. * files. So, we can't merely "hash" the ino numbers since collisions
  241. * would corrupt the archive. Instead, we generate synthetic ino values
  242. * to store in the archive and maintain a map of original ino values to
  243. * synthetic ones so we can preserve hardlink information.
  244. *
  245. * TODO: Make this more efficient. It's not as bad as it looks (most
  246. * files don't have any hardlinks and we don't do any work here for those),
  247. * but it wouldn't be hard to do better.
  248. *
  249. * TODO: Work with dev/ino pairs here instead of just ino values.
  250. */
  251. static int
  252. synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
  253. {
  254. int64_t ino = archive_entry_ino64(entry);
  255. int ino_new;
  256. size_t i;
  257. /*
  258. * If no index number was given, don't assign one. In
  259. * particular, this handles the end-of-archive marker
  260. * correctly by giving it a zero index value. (This is also
  261. * why we start our synthetic index numbers with one below.)
  262. */
  263. if (ino == 0)
  264. return (0);
  265. /* Don't store a mapping if we don't need to. */
  266. if (archive_entry_nlink(entry) < 2) {
  267. return (int)(++cpio->ino_next);
  268. }
  269. /* Look up old ino; if we have it, this is a hardlink
  270. * and we reuse the same value. */
  271. for (i = 0; i < cpio->ino_list_next; ++i) {
  272. if (cpio->ino_list[i].old == ino)
  273. return (cpio->ino_list[i].new);
  274. }
  275. /* Assign a new index number. */
  276. ino_new = (int)(++cpio->ino_next);
  277. /* Ensure space for the new mapping. */
  278. if (cpio->ino_list_size <= cpio->ino_list_next) {
  279. size_t newsize = cpio->ino_list_size < 512
  280. ? 512 : cpio->ino_list_size * 2;
  281. void *newlist = realloc(cpio->ino_list,
  282. sizeof(cpio->ino_list[0]) * newsize);
  283. if (newlist == NULL)
  284. return (-1);
  285. cpio->ino_list_size = newsize;
  286. cpio->ino_list = newlist;
  287. }
  288. /* Record and return the new value. */
  289. cpio->ino_list[cpio->ino_list_next].old = ino;
  290. cpio->ino_list[cpio->ino_list_next].new = ino_new;
  291. ++cpio->ino_list_next;
  292. return (ino_new);
  293. }
  294. static struct archive_string_conv *
  295. get_sconv(struct archive_write *a)
  296. {
  297. struct cpio *cpio;
  298. struct archive_string_conv *sconv;
  299. cpio = (struct cpio *)a->format_data;
  300. sconv = cpio->opt_sconv;
  301. if (sconv == NULL) {
  302. if (!cpio->init_default_conversion) {
  303. cpio->sconv_default =
  304. archive_string_default_conversion_for_write(
  305. &(a->archive));
  306. cpio->init_default_conversion = 1;
  307. }
  308. sconv = cpio->sconv_default;
  309. }
  310. return (sconv);
  311. }
  312. static int
  313. archive_write_binary_header(struct archive_write *a, struct archive_entry *entry)
  314. {
  315. const char *path;
  316. size_t len;
  317. if (archive_entry_filetype(entry) == 0 && archive_entry_hardlink(entry) == NULL) {
  318. archive_set_error(&a->archive, -1, "Filetype required");
  319. return (ARCHIVE_FAILED);
  320. }
  321. if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
  322. && errno == ENOMEM) {
  323. archive_set_error(&a->archive, ENOMEM,
  324. "Can't allocate memory for Pathname");
  325. return (ARCHIVE_FATAL);
  326. }
  327. if (len == 0 || path == NULL || path[0] == '\0') {
  328. archive_set_error(&a->archive, -1, "Pathname required");
  329. return (ARCHIVE_FAILED);
  330. }
  331. if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
  332. archive_set_error(&a->archive, -1, "Size required");
  333. return (ARCHIVE_FAILED);
  334. }
  335. return write_header(a, entry);
  336. }
  337. static int
  338. write_header(struct archive_write *a, struct archive_entry *entry)
  339. {
  340. struct cpio *cpio;
  341. const char *p, *path;
  342. int pathlength, ret, ret_final;
  343. int64_t ino;
  344. struct cpio_binary_header h;
  345. struct archive_string_conv *sconv;
  346. struct archive_entry *entry_main;
  347. size_t len;
  348. cpio = (struct cpio *)a->format_data;
  349. ret_final = ARCHIVE_OK;
  350. sconv = get_sconv(a);
  351. #if defined(_WIN32) && !defined(__CYGWIN__)
  352. /* Make sure the path separators in pathname, hardlink and symlink
  353. * are all slash '/', not the Windows path separator '\'. */
  354. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  355. if (entry_main == NULL) {
  356. archive_set_error(&a->archive, ENOMEM,
  357. "Can't allocate ustar data");
  358. return(ARCHIVE_FATAL);
  359. }
  360. if (entry != entry_main)
  361. entry = entry_main;
  362. else
  363. entry_main = NULL;
  364. #else
  365. entry_main = NULL;
  366. #endif
  367. ret = archive_entry_pathname_l(entry, &path, &len, sconv);
  368. if (ret != 0) {
  369. if (errno == ENOMEM) {
  370. archive_set_error(&a->archive, ENOMEM,
  371. "Can't allocate memory for Pathname");
  372. ret_final = ARCHIVE_FATAL;
  373. goto exit_write_header;
  374. }
  375. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  376. "Can't translate pathname '%s' to %s",
  377. archive_entry_pathname(entry),
  378. archive_string_conversion_charset_name(sconv));
  379. ret_final = ARCHIVE_WARN;
  380. }
  381. /* Include trailing null */
  382. pathlength = (int)len + 1;
  383. h.h_magic = la_swap16(070707);
  384. h.h_dev = la_swap16(archive_entry_dev(entry));
  385. ino = synthesize_ino_value(cpio, entry);
  386. if (ino < 0) {
  387. archive_set_error(&a->archive, ENOMEM,
  388. "No memory for ino translation table");
  389. ret_final = ARCHIVE_FATAL;
  390. goto exit_write_header;
  391. } else if (ino > 077777) {
  392. archive_set_error(&a->archive, ERANGE,
  393. "Too many files for this cpio format");
  394. ret_final = ARCHIVE_FATAL;
  395. goto exit_write_header;
  396. }
  397. h.h_ino = la_swap16((uint16_t)ino);
  398. h.h_mode = archive_entry_mode(entry);
  399. if (((h.h_mode & AE_IFMT) == AE_IFSOCK) || ((h.h_mode & AE_IFMT) == AE_IFIFO)) {
  400. archive_set_error(&a->archive, EINVAL,
  401. "sockets and fifos cannot be represented in the binary cpio formats");
  402. ret_final = ARCHIVE_FATAL;
  403. goto exit_write_header;
  404. }
  405. if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_PWB) {
  406. if ((h.h_mode & AE_IFMT) == AE_IFLNK) {
  407. archive_set_error(&a->archive, EINVAL,
  408. "symbolic links cannot be represented in the PWB cpio format");
  409. ret_final = ARCHIVE_FATAL;
  410. goto exit_write_header;
  411. }
  412. /* we could turn off AE_IFREG here, but it does no harm, */
  413. /* and allows v7 cpio to read the entry without confusion */
  414. }
  415. h.h_mode = la_swap16(h.h_mode);
  416. h.h_uid = la_swap16((uint16_t)archive_entry_uid(entry));
  417. h.h_gid = la_swap16((uint16_t)archive_entry_gid(entry));
  418. h.h_nlink = la_swap16((uint16_t)archive_entry_nlink(entry));
  419. if (archive_entry_filetype(entry) == AE_IFBLK
  420. || archive_entry_filetype(entry) == AE_IFCHR)
  421. h.h_majmin = la_swap16(archive_entry_rdev(entry));
  422. else
  423. h.h_majmin = 0;
  424. h.h_mtime = la_swap32((uint32_t)archive_entry_mtime(entry));
  425. h.h_namesize = la_swap16(pathlength);
  426. /* Non-regular files don't store bodies. */
  427. if (archive_entry_filetype(entry) != AE_IFREG)
  428. archive_entry_set_size(entry, 0);
  429. /* Symlinks get the link written as the body of the entry. */
  430. ret = archive_entry_symlink_l(entry, &p, &len, sconv);
  431. if (ret != 0) {
  432. if (errno == ENOMEM) {
  433. archive_set_error(&a->archive, ENOMEM,
  434. "Can't allocate memory for Linkname");
  435. ret_final = ARCHIVE_FATAL;
  436. goto exit_write_header;
  437. }
  438. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  439. "Can't translate linkname '%s' to %s",
  440. archive_entry_symlink(entry),
  441. archive_string_conversion_charset_name(sconv));
  442. ret_final = ARCHIVE_WARN;
  443. }
  444. if (len > 0 && p != NULL && *p != '\0') {
  445. if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_PWB) {
  446. archive_set_error(&a->archive, EINVAL,
  447. "symlinks are not supported by UNIX V6 or by PWB cpio");
  448. ret_final = ARCHIVE_FATAL;
  449. goto exit_write_header;
  450. }
  451. h.h_filesize = la_swap32((uint32_t)strlen(p)); /* symlink */
  452. } else {
  453. if ((a->archive.archive_format == ARCHIVE_FORMAT_CPIO_PWB) &&
  454. (archive_entry_size(entry) > 256*256*256-1)) {
  455. archive_set_error(&a->archive, ERANGE,
  456. "File is too large for PWB binary cpio format.");
  457. ret_final = ARCHIVE_FAILED;
  458. goto exit_write_header;
  459. } else if (archive_entry_size(entry) > INT32_MAX) {
  460. archive_set_error(&a->archive, ERANGE,
  461. "File is too large for binary cpio format.");
  462. ret_final = ARCHIVE_FAILED;
  463. goto exit_write_header;
  464. }
  465. h.h_filesize = la_swap32((uint32_t)archive_entry_size(entry)); /* file */
  466. }
  467. ret = __archive_write_output(a, &h, HSIZE);
  468. if (ret != ARCHIVE_OK) {
  469. ret_final = ARCHIVE_FATAL;
  470. goto exit_write_header;
  471. }
  472. ret = __archive_write_output(a, path, pathlength);
  473. if ((ret == ARCHIVE_OK) && ((pathlength % 2) != 0))
  474. ret = __archive_write_nulls(a, 1);
  475. if (ret != ARCHIVE_OK) {
  476. ret_final = ARCHIVE_FATAL;
  477. goto exit_write_header;
  478. }
  479. cpio->entry_bytes_remaining = archive_entry_size(entry);
  480. if ((cpio->entry_bytes_remaining % 2) != 0)
  481. cpio->entry_bytes_remaining++;
  482. /* Write the symlink now. */
  483. if (p != NULL && *p != '\0') {
  484. ret = __archive_write_output(a, p, strlen(p));
  485. if ((ret == ARCHIVE_OK) && ((strlen(p) % 2) != 0))
  486. ret = __archive_write_nulls(a, 1);
  487. if (ret != ARCHIVE_OK) {
  488. ret_final = ARCHIVE_FATAL;
  489. goto exit_write_header;
  490. }
  491. }
  492. exit_write_header:
  493. archive_entry_free(entry_main);
  494. return (ret_final);
  495. }
  496. static ssize_t
  497. archive_write_binary_data(struct archive_write *a, const void *buff, size_t s)
  498. {
  499. struct cpio *cpio;
  500. int ret;
  501. cpio = (struct cpio *)a->format_data;
  502. if (s > cpio->entry_bytes_remaining)
  503. s = (size_t)cpio->entry_bytes_remaining;
  504. ret = __archive_write_output(a, buff, s);
  505. cpio->entry_bytes_remaining -= s;
  506. if (ret >= 0)
  507. return (s);
  508. else
  509. return (ret);
  510. }
  511. static int
  512. archive_write_binary_close(struct archive_write *a)
  513. {
  514. int er;
  515. struct archive_entry *trailer;
  516. trailer = archive_entry_new2(NULL);
  517. if (trailer == NULL) {
  518. return ARCHIVE_FATAL;
  519. }
  520. /* nlink = 1 here for GNU cpio compat. */
  521. archive_entry_set_nlink(trailer, 1);
  522. archive_entry_set_size(trailer, 0);
  523. archive_entry_set_pathname(trailer, "TRAILER!!!");
  524. er = write_header(a, trailer);
  525. archive_entry_free(trailer);
  526. return (er);
  527. }
  528. static int
  529. archive_write_binary_free(struct archive_write *a)
  530. {
  531. struct cpio *cpio;
  532. cpio = (struct cpio *)a->format_data;
  533. free(cpio->ino_list);
  534. free(cpio);
  535. a->format_data = NULL;
  536. return (ARCHIVE_OK);
  537. }
  538. static int
  539. archive_write_binary_finish_entry(struct archive_write *a)
  540. {
  541. struct cpio *cpio;
  542. cpio = (struct cpio *)a->format_data;
  543. return (__archive_write_nulls(a,
  544. (size_t)cpio->entry_bytes_remaining));
  545. }