2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio.c 201170 2009-12-29 06:34:23Z kientzle $");
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_write_private.h"
46 #include "archive_write_set_format_private.h"
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 *);
60 uint64_t entry_bytes_remaining;
64 struct { int64_t old; int new;} *ino_list;
68 struct archive_string_conv *opt_sconv;
69 struct archive_string_conv *sconv_default;
70 int init_default_conversion;
73 /* This struct needs to be packed to get the header right */
76 #define PACKED(x) x __attribute__((packed))
77 #elif defined(_MSC_VER)
78 #define PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
85 PACKED(struct cpio_binary_header {
99 /* Back in the day, the 7th Edition cpio.c had this, to
100 * adapt to, as the comment said, "VAX, Interdata, ...":
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];}
109 * U.s[0] = v[1], U.s[1] = v[0];
111 * U.s[0] = v[0], U.s[1] = v[1];
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.
118 * Now, we could do this:
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];}
124 * ...but it feels a little better to do it like this:
127 static uint16_t la_swap16(uint16_t in) {
144 static uint32_t la_swap32(uint32_t in) {
151 if (U.c[0]) { /* Little-endian */
157 } else if (U.c[3]) { /* Big-endian */
159 U.s[0] = la_swap16(U.s[0]);
160 U.s[1] = la_swap16(U.s[1]);
161 } else { /* PDP-endian */
168 * Set output format to the selected binary variant
171 archive_write_set_format_cpio_binary(struct archive *_a, int format)
173 struct archive_write *a = (struct archive_write *)_a;
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);
182 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
183 ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_binary");
185 /* If someone else was already registered, unregister them. */
186 if (a->format_free != NULL)
189 cpio = (struct cpio *)calloc(1, sizeof(*cpio));
191 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
192 return (ARCHIVE_FATAL);
194 a->format_data = cpio;
195 a->format_name = "cpio";
196 a->format_options = archive_write_binary_options;
197 a->format_write_header = archive_write_binary_header;
198 a->format_write_data = archive_write_binary_data;
199 a->format_finish_entry = archive_write_binary_finish_entry;
200 a->format_close = archive_write_binary_close;
201 a->format_free = archive_write_binary_free;
202 a->archive.archive_format = format;
204 case ARCHIVE_FORMAT_CPIO_PWB:
205 a->archive.archive_format_name = "PWB cpio";
207 case ARCHIVE_FORMAT_CPIO_BIN_LE:
208 a->archive.archive_format_name = "7th Edition cpio";
211 archive_set_error(&a->archive, EINVAL, "binary format must be 'pwb' or 'bin'");
212 return (ARCHIVE_FATAL);
218 * Set output format to PWB (6th Edition) binary format
221 archive_write_set_format_cpio_pwb(struct archive *_a)
223 return archive_write_set_format_cpio_binary(_a, ARCHIVE_FORMAT_CPIO_PWB);
227 * Set output format to 7th Edition binary format
230 archive_write_set_format_cpio_bin(struct archive *_a)
232 return archive_write_set_format_cpio_binary(_a, ARCHIVE_FORMAT_CPIO_BIN_LE);
236 archive_write_binary_options(struct archive_write *a, const char *key,
239 struct cpio *cpio = (struct cpio *)a->format_data;
240 int ret = ARCHIVE_FAILED;
242 if (strcmp(key, "hdrcharset") == 0) {
243 if (val == NULL || val[0] == 0)
244 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
245 "%s: hdrcharset option needs a character-set name",
248 cpio->opt_sconv = archive_string_conversion_to_charset(
249 &a->archive, val, 0);
250 if (cpio->opt_sconv != NULL)
258 /* Note: The "warn" return is just to inform the options
259 * supervisor that we didn't handle it. It will generate
260 * a suitable error if no one used this option. */
261 return (ARCHIVE_WARN);
265 * Ino values are as long as 64 bits on some systems; cpio format
266 * only allows 16 bits and relies on the ino values to identify hardlinked
267 * files. So, we can't merely "hash" the ino numbers since collisions
268 * would corrupt the archive. Instead, we generate synthetic ino values
269 * to store in the archive and maintain a map of original ino values to
270 * synthetic ones so we can preserve hardlink information.
272 * TODO: Make this more efficient. It's not as bad as it looks (most
273 * files don't have any hardlinks and we don't do any work here for those),
274 * but it wouldn't be hard to do better.
276 * TODO: Work with dev/ino pairs here instead of just ino values.
279 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
281 int64_t ino = archive_entry_ino64(entry);
286 * If no index number was given, don't assign one. In
287 * particular, this handles the end-of-archive marker
288 * correctly by giving it a zero index value. (This is also
289 * why we start our synthetic index numbers with one below.)
294 /* Don't store a mapping if we don't need to. */
295 if (archive_entry_nlink(entry) < 2) {
296 return (int)(++cpio->ino_next);
299 /* Look up old ino; if we have it, this is a hardlink
300 * and we reuse the same value. */
301 for (i = 0; i < cpio->ino_list_next; ++i) {
302 if (cpio->ino_list[i].old == ino)
303 return (cpio->ino_list[i].new);
306 /* Assign a new index number. */
307 ino_new = (int)(++cpio->ino_next);
309 /* Ensure space for the new mapping. */
310 if (cpio->ino_list_size <= cpio->ino_list_next) {
311 size_t newsize = cpio->ino_list_size < 512
312 ? 512 : cpio->ino_list_size * 2;
313 void *newlist = realloc(cpio->ino_list,
314 sizeof(cpio->ino_list[0]) * newsize);
318 cpio->ino_list_size = newsize;
319 cpio->ino_list = newlist;
322 /* Record and return the new value. */
323 cpio->ino_list[cpio->ino_list_next].old = ino;
324 cpio->ino_list[cpio->ino_list_next].new = ino_new;
325 ++cpio->ino_list_next;
330 static struct archive_string_conv *
331 get_sconv(struct archive_write *a)
334 struct archive_string_conv *sconv;
336 cpio = (struct cpio *)a->format_data;
337 sconv = cpio->opt_sconv;
339 if (!cpio->init_default_conversion) {
340 cpio->sconv_default =
341 archive_string_default_conversion_for_write(
343 cpio->init_default_conversion = 1;
345 sconv = cpio->sconv_default;
351 archive_write_binary_header(struct archive_write *a, struct archive_entry *entry)
356 if (archive_entry_filetype(entry) == 0 && archive_entry_hardlink(entry) == NULL) {
357 archive_set_error(&a->archive, -1, "Filetype required");
358 return (ARCHIVE_FAILED);
361 if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
362 && errno == ENOMEM) {
363 archive_set_error(&a->archive, ENOMEM,
364 "Can't allocate memory for Pathname");
365 return (ARCHIVE_FATAL);
367 if (len == 0 || path == NULL || path[0] == '\0') {
368 archive_set_error(&a->archive, -1, "Pathname required");
369 return (ARCHIVE_FAILED);
372 if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
373 archive_set_error(&a->archive, -1, "Size required");
374 return (ARCHIVE_FAILED);
376 return write_header(a, entry);
380 write_header(struct archive_write *a, struct archive_entry *entry)
383 const char *p, *path;
384 int pathlength, ret, ret_final;
386 struct cpio_binary_header h;
387 struct archive_string_conv *sconv;
388 struct archive_entry *entry_main;
391 cpio = (struct cpio *)a->format_data;
392 ret_final = ARCHIVE_OK;
393 sconv = get_sconv(a);
395 #if defined(_WIN32) && !defined(__CYGWIN__)
396 /* Make sure the path separators in pathname, hardlink and symlink
397 * are all slash '/', not the Windows path separator '\'. */
398 entry_main = __la_win_entry_in_posix_pathseparator(entry);
399 if (entry_main == NULL) {
400 archive_set_error(&a->archive, ENOMEM,
401 "Can't allocate ustar data");
402 return(ARCHIVE_FATAL);
404 if (entry != entry_main)
412 ret = archive_entry_pathname_l(entry, &path, &len, sconv);
414 if (errno == ENOMEM) {
415 archive_set_error(&a->archive, ENOMEM,
416 "Can't allocate memory for Pathname");
417 ret_final = ARCHIVE_FATAL;
418 goto exit_write_header;
420 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
421 "Can't translate pathname '%s' to %s",
422 archive_entry_pathname(entry),
423 archive_string_conversion_charset_name(sconv));
424 ret_final = ARCHIVE_WARN;
426 /* Include trailing null */
427 pathlength = (int)len + 1;
429 h.h_magic = la_swap16(070707);
430 h.h_dev = la_swap16(archive_entry_dev(entry));
432 ino = synthesize_ino_value(cpio, entry);
434 archive_set_error(&a->archive, ENOMEM,
435 "No memory for ino translation table");
436 ret_final = ARCHIVE_FATAL;
437 goto exit_write_header;
438 } else if (ino > 077777) {
439 archive_set_error(&a->archive, ERANGE,
440 "Too many files for this cpio format");
441 ret_final = ARCHIVE_FATAL;
442 goto exit_write_header;
444 h.h_ino = la_swap16((uint16_t)ino);
446 h.h_mode = archive_entry_mode(entry);
447 if (((h.h_mode & AE_IFMT) == AE_IFSOCK) || ((h.h_mode & AE_IFMT) == AE_IFIFO)) {
448 archive_set_error(&a->archive, EINVAL,
449 "sockets and fifos cannot be represented in the binary cpio formats");
450 ret_final = ARCHIVE_FATAL;
451 goto exit_write_header;
453 if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_PWB) {
454 if ((h.h_mode & AE_IFMT) == AE_IFLNK) {
455 archive_set_error(&a->archive, EINVAL,
456 "symbolic links cannot be represented in the PWB cpio format");
457 ret_final = ARCHIVE_FATAL;
458 goto exit_write_header;
460 /* we could turn off AE_IFREG here, but it does no harm, */
461 /* and allows v7 cpio to read the entry without confusion */
463 h.h_mode = la_swap16(h.h_mode);
465 h.h_uid = la_swap16((uint16_t)archive_entry_uid(entry));
466 h.h_gid = la_swap16((uint16_t)archive_entry_gid(entry));
467 h.h_nlink = la_swap16((uint16_t)archive_entry_nlink(entry));
469 if (archive_entry_filetype(entry) == AE_IFBLK
470 || archive_entry_filetype(entry) == AE_IFCHR)
471 h.h_majmin = la_swap16(archive_entry_rdev(entry));
475 h.h_mtime = la_swap32((uint32_t)archive_entry_mtime(entry));
476 h.h_namesize = la_swap16(pathlength);
478 /* Non-regular files don't store bodies. */
479 if (archive_entry_filetype(entry) != AE_IFREG)
480 archive_entry_set_size(entry, 0);
482 /* Symlinks get the link written as the body of the entry. */
483 ret = archive_entry_symlink_l(entry, &p, &len, sconv);
485 if (errno == ENOMEM) {
486 archive_set_error(&a->archive, ENOMEM,
487 "Can't allocate memory for Linkname");
488 ret_final = ARCHIVE_FATAL;
489 goto exit_write_header;
491 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
492 "Can't translate linkname '%s' to %s",
493 archive_entry_symlink(entry),
494 archive_string_conversion_charset_name(sconv));
495 ret_final = ARCHIVE_WARN;
498 if (len > 0 && p != NULL && *p != '\0') {
499 if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_PWB) {
500 archive_set_error(&a->archive, EINVAL,
501 "symlinks are not supported by UNIX V6 or by PWB cpio");
502 ret_final = ARCHIVE_FATAL;
503 goto exit_write_header;
505 h.h_filesize = la_swap32((uint32_t)strlen(p)); /* symlink */
507 if ((a->archive.archive_format == ARCHIVE_FORMAT_CPIO_PWB) &&
508 (archive_entry_size(entry) > 256*256*256-1)) {
509 archive_set_error(&a->archive, ERANGE,
510 "File is too large for PWB binary cpio format.");
511 ret_final = ARCHIVE_FAILED;
512 goto exit_write_header;
513 } else if (archive_entry_size(entry) > INT32_MAX) {
514 archive_set_error(&a->archive, ERANGE,
515 "File is too large for binary cpio format.");
516 ret_final = ARCHIVE_FAILED;
517 goto exit_write_header;
519 h.h_filesize = la_swap32((uint32_t)archive_entry_size(entry)); /* file */
522 ret = __archive_write_output(a, &h, HSIZE);
523 if (ret != ARCHIVE_OK) {
524 ret_final = ARCHIVE_FATAL;
525 goto exit_write_header;
528 ret = __archive_write_output(a, path, pathlength);
529 if ((ret == ARCHIVE_OK) && ((pathlength % 2) != 0))
530 ret = __archive_write_nulls(a, 1);
531 if (ret != ARCHIVE_OK) {
532 ret_final = ARCHIVE_FATAL;
533 goto exit_write_header;
536 cpio->entry_bytes_remaining = archive_entry_size(entry);
537 if ((cpio->entry_bytes_remaining % 2) != 0)
538 cpio->entry_bytes_remaining++;
540 /* Write the symlink now. */
541 if (p != NULL && *p != '\0') {
542 ret = __archive_write_output(a, p, strlen(p));
543 if ((ret == ARCHIVE_OK) && ((strlen(p) % 2) != 0))
544 ret = __archive_write_nulls(a, 1);
545 if (ret != ARCHIVE_OK) {
546 ret_final = ARCHIVE_FATAL;
547 goto exit_write_header;
552 archive_entry_free(entry_main);
557 archive_write_binary_data(struct archive_write *a, const void *buff, size_t s)
562 cpio = (struct cpio *)a->format_data;
563 if (s > cpio->entry_bytes_remaining)
564 s = (size_t)cpio->entry_bytes_remaining;
566 ret = __archive_write_output(a, buff, s);
567 cpio->entry_bytes_remaining -= s;
575 archive_write_binary_close(struct archive_write *a)
578 struct archive_entry *trailer;
580 trailer = archive_entry_new2(NULL);
581 /* nlink = 1 here for GNU cpio compat. */
582 archive_entry_set_nlink(trailer, 1);
583 archive_entry_set_size(trailer, 0);
584 archive_entry_set_pathname(trailer, "TRAILER!!!");
585 er = write_header(a, trailer);
586 archive_entry_free(trailer);
591 archive_write_binary_free(struct archive_write *a)
595 cpio = (struct cpio *)a->format_data;
596 free(cpio->ino_list);
598 a->format_data = NULL;
603 archive_write_binary_finish_entry(struct archive_write *a)
607 cpio = (struct cpio *)a->format_data;
608 return (__archive_write_nulls(a,
609 (size_t)cpio->entry_bytes_remaining));