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