Blame libarchive/archive_write_set_format_cpio.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio.c 201170 2009-12-29 06:34:23Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#include <stdio.h>
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_entry.h"
Packit Service 1d0348
#include "archive_entry_locale.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_write_private.h"
Packit Service 1d0348
Packit Service 1d0348
static ssize_t	archive_write_cpio_data(struct archive_write *,
Packit Service 1d0348
		    const void *buff, size_t s);
Packit Service 1d0348
static int	archive_write_cpio_close(struct archive_write *);
Packit Service 1d0348
static int	archive_write_cpio_free(struct archive_write *);
Packit Service 1d0348
static int	archive_write_cpio_finish_entry(struct archive_write *);
Packit Service 1d0348
static int	archive_write_cpio_header(struct archive_write *,
Packit Service 1d0348
		    struct archive_entry *);
Packit Service 1d0348
static int	archive_write_cpio_options(struct archive_write *,
Packit Service 1d0348
		    const char *, const char *);
Packit Service 1d0348
static int	format_octal(int64_t, void *, int);
Packit Service 1d0348
static int64_t	format_octal_recursive(int64_t, char *, int);
Packit Service 1d0348
static int	write_header(struct archive_write *, struct archive_entry *);
Packit Service 1d0348
Packit Service 1d0348
struct cpio {
Packit Service 1d0348
	uint64_t	  entry_bytes_remaining;
Packit Service 1d0348
Packit Service 1d0348
	int64_t		  ino_next;
Packit Service 1d0348
Packit Service 1d0348
	struct		 { int64_t old; int new;} *ino_list;
Packit Service 1d0348
	size_t		  ino_list_size;
Packit Service 1d0348
	size_t		  ino_list_next;
Packit Service 1d0348
Packit Service 1d0348
	struct archive_string_conv *opt_sconv;
Packit Service 1d0348
	struct archive_string_conv *sconv_default;
Packit Service 1d0348
	int		  init_default_conversion;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
#define	c_magic_offset 0
Packit Service 1d0348
#define	c_magic_size 6
Packit Service 1d0348
#define	c_dev_offset 6
Packit Service 1d0348
#define	c_dev_size 6
Packit Service 1d0348
#define	c_ino_offset 12
Packit Service 1d0348
#define	c_ino_size 6
Packit Service 1d0348
#define	c_mode_offset 18
Packit Service 1d0348
#define	c_mode_size 6
Packit Service 1d0348
#define	c_uid_offset 24
Packit Service 1d0348
#define	c_uid_size 6
Packit Service 1d0348
#define	c_gid_offset 30
Packit Service 1d0348
#define	c_gid_size 6
Packit Service 1d0348
#define	c_nlink_offset 36
Packit Service 1d0348
#define	c_nlink_size 6
Packit Service 1d0348
#define	c_rdev_offset 42
Packit Service 1d0348
#define	c_rdev_size 6
Packit Service 1d0348
#define	c_mtime_offset 48
Packit Service 1d0348
#define	c_mtime_size 11
Packit Service 1d0348
#define	c_namesize_offset 59
Packit Service 1d0348
#define	c_namesize_size 6
Packit Service 1d0348
#define	c_filesize_offset 65
Packit Service 1d0348
#define	c_filesize_size 11
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Set output format to 'cpio' format.
Packit Service 1d0348
 */
Packit Service 1d0348
int
Packit Service 1d0348
archive_write_set_format_cpio(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_write *a = (struct archive_write *)_a;
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
Packit Service 1d0348
Packit Service 1d0348
	/* If someone else was already registered, unregister them. */
Packit Service 1d0348
	if (a->format_free != NULL)
Packit Service 1d0348
		(a->format_free)(a);
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
Packit Service 1d0348
	if (cpio == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	a->format_data = cpio;
Packit Service 1d0348
	a->format_name = "cpio";
Packit Service 1d0348
	a->format_options = archive_write_cpio_options;
Packit Service 1d0348
	a->format_write_header = archive_write_cpio_header;
Packit Service 1d0348
	a->format_write_data = archive_write_cpio_data;
Packit Service 1d0348
	a->format_finish_entry = archive_write_cpio_finish_entry;
Packit Service 1d0348
	a->format_close = archive_write_cpio_close;
Packit Service 1d0348
	a->format_free = archive_write_cpio_free;
Packit Service 1d0348
	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
Packit Service 1d0348
	a->archive.archive_format_name = "POSIX cpio";
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_cpio_options(struct archive_write *a, const char *key,
Packit Service 1d0348
    const char *val)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	int ret = ARCHIVE_FAILED;
Packit Service 1d0348
Packit Service 1d0348
	if (strcmp(key, "hdrcharset")  == 0) {
Packit Service 1d0348
		if (val == NULL || val[0] == 0)
Packit Service 1d0348
			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "%s: hdrcharset option needs a character-set name",
Packit Service 1d0348
			    a->format_name);
Packit Service 1d0348
		else {
Packit Service 1d0348
			cpio->opt_sconv = archive_string_conversion_to_charset(
Packit Service 1d0348
			    &a->archive, val, 0);
Packit Service 1d0348
			if (cpio->opt_sconv != NULL)
Packit Service 1d0348
				ret = ARCHIVE_OK;
Packit Service 1d0348
			else
Packit Service 1d0348
				ret = ARCHIVE_FATAL;
Packit Service 1d0348
		}
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Note: The "warn" return is just to inform the options
Packit Service 1d0348
	 * supervisor that we didn't handle it.  It will generate
Packit Service 1d0348
	 * a suitable error if no one used this option. */
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Ino values are as long as 64 bits on some systems; cpio format
Packit Service 1d0348
 * only allows 18 bits and relies on the ino values to identify hardlinked
Packit Service 1d0348
 * files.  So, we can't merely "hash" the ino numbers since collisions
Packit Service 1d0348
 * would corrupt the archive.  Instead, we generate synthetic ino values
Packit Service 1d0348
 * to store in the archive and maintain a map of original ino values to
Packit Service 1d0348
 * synthetic ones so we can preserve hardlink information.
Packit Service 1d0348
 *
Packit Service 1d0348
 * TODO: Make this more efficient.  It's not as bad as it looks (most
Packit Service 1d0348
 * files don't have any hardlinks and we don't do any work here for those),
Packit Service 1d0348
 * but it wouldn't be hard to do better.
Packit Service 1d0348
 *
Packit Service 1d0348
 * TODO: Work with dev/ino pairs here instead of just ino values.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t ino = archive_entry_ino64(entry);
Packit Service 1d0348
	int ino_new;
Packit Service 1d0348
	size_t i;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * If no index number was given, don't assign one.  In
Packit Service 1d0348
	 * particular, this handles the end-of-archive marker
Packit Service 1d0348
	 * correctly by giving it a zero index value.  (This is also
Packit Service 1d0348
	 * why we start our synthetic index numbers with one below.)
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (ino == 0)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	/* Don't store a mapping if we don't need to. */
Packit Service 1d0348
	if (archive_entry_nlink(entry) < 2) {
Packit Service 1d0348
		return (int)(++cpio->ino_next);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Look up old ino; if we have it, this is a hardlink
Packit Service 1d0348
	 * and we reuse the same value. */
Packit Service 1d0348
	for (i = 0; i < cpio->ino_list_next; ++i) {
Packit Service 1d0348
		if (cpio->ino_list[i].old == ino)
Packit Service 1d0348
			return (cpio->ino_list[i].new);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Assign a new index number. */
Packit Service 1d0348
	ino_new = (int)(++cpio->ino_next);
Packit Service 1d0348
Packit Service 1d0348
	/* Ensure space for the new mapping. */
Packit Service 1d0348
	if (cpio->ino_list_size <= cpio->ino_list_next) {
Packit Service 1d0348
		size_t newsize = cpio->ino_list_size < 512
Packit Service 1d0348
		    ? 512 : cpio->ino_list_size * 2;
Packit Service 1d0348
		void *newlist = realloc(cpio->ino_list,
Packit Service 1d0348
		    sizeof(cpio->ino_list[0]) * newsize);
Packit Service 1d0348
		if (newlist == NULL)
Packit Service 1d0348
			return (-1);
Packit Service 1d0348
Packit Service 1d0348
		cpio->ino_list_size = newsize;
Packit Service 1d0348
		cpio->ino_list = newlist;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Record and return the new value. */
Packit Service 1d0348
	cpio->ino_list[cpio->ino_list_next].old = ino;
Packit Service 1d0348
	cpio->ino_list[cpio->ino_list_next].new = ino_new;
Packit Service 1d0348
	++cpio->ino_list_next;
Packit Service 1d0348
	return (ino_new);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
static struct archive_string_conv *
Packit Service 1d0348
get_sconv(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
	struct archive_string_conv *sconv;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	sconv = cpio->opt_sconv;
Packit Service 1d0348
	if (sconv == NULL) {
Packit Service 1d0348
		if (!cpio->init_default_conversion) {
Packit Service 1d0348
			cpio->sconv_default =
Packit Service 1d0348
			    archive_string_default_conversion_for_write(
Packit Service 1d0348
			      &(a->archive));
Packit Service 1d0348
			cpio->init_default_conversion = 1;
Packit Service 1d0348
		}
Packit Service 1d0348
		sconv = cpio->sconv_default;
Packit Service 1d0348
	}
Packit Service 1d0348
	return (sconv);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *path;
Packit Service 1d0348
	size_t len;
Packit Service 1d0348
Packit Service 1d0348
	if (archive_entry_filetype(entry) == 0) {
Packit Service 1d0348
		archive_set_error(&a->archive, -1, "Filetype required");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
Packit Service 1d0348
	    && errno == ENOMEM) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate memory for Pathname");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (len == 0 || path == NULL || path[0] == '\0') {
Packit Service 1d0348
		archive_set_error(&a->archive, -1, "Pathname required");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
Packit Service 1d0348
		archive_set_error(&a->archive, -1, "Size required");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
	return write_header(a, entry);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
write_header(struct archive_write *a, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
	const char *p, *path;
Packit Service 1d0348
	int pathlength, ret, ret_final;
Packit Service 1d0348
	int64_t	ino;
Packit Service 1d0348
	char h[76];
Packit Service 1d0348
	struct archive_string_conv *sconv;
Packit Service 1d0348
	struct archive_entry *entry_main;
Packit Service 1d0348
	size_t len;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	ret_final = ARCHIVE_OK;
Packit Service 1d0348
	sconv = get_sconv(a);
Packit Service 1d0348
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
	/* Make sure the path separators in pathname, hardlink and symlink
Packit Service 1d0348
	 * are all slash '/', not the Windows path separator '\'. */
Packit Service 1d0348
	entry_main = __la_win_entry_in_posix_pathseparator(entry);
Packit Service 1d0348
	if (entry_main == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate ustar data");
Packit Service 1d0348
		return(ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (entry != entry_main)
Packit Service 1d0348
		entry = entry_main;
Packit Service 1d0348
	else
Packit Service 1d0348
		entry_main = NULL;
Packit Service 1d0348
#else
Packit Service 1d0348
	entry_main = NULL;
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
	ret = archive_entry_pathname_l(entry, &path, &len, sconv);
Packit Service 1d0348
	if (ret != 0) {
Packit Service 1d0348
		if (errno == ENOMEM) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate memory for Pathname");
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
Packit Service 1d0348
		    "Can't translate pathname '%s' to %s",
Packit Service 1d0348
		    archive_entry_pathname(entry),
Packit Service 1d0348
		    archive_string_conversion_charset_name(sconv));
Packit Service 1d0348
		ret_final = ARCHIVE_WARN;
Packit Service 1d0348
	}
Packit Service 1d0348
	/* Include trailing null. */
Packit Service 1d0348
	pathlength = (int)len + 1;
Packit Service 1d0348
Packit Service 1d0348
	memset(h, 0, sizeof(h));
Packit Service 1d0348
	format_octal(070707, h + c_magic_offset, c_magic_size);
Packit Service 1d0348
	format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
Packit Service 1d0348
Packit Service 1d0348
	ino = synthesize_ino_value(cpio, entry);
Packit Service 1d0348
	if (ino < 0) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
		    "No memory for ino translation table");
Packit Service 1d0348
		ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	} else if (ino > 0777777) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "Too many files for this cpio format");
Packit Service 1d0348
		ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
	format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
Packit Service 1d0348
Packit Service 1d0348
	/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
Packit Service 1d0348
	format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
Packit Service 1d0348
	format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
Packit Service 1d0348
	format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
Packit Service 1d0348
	format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
Packit Service 1d0348
	if (archive_entry_filetype(entry) == AE_IFBLK
Packit Service 1d0348
	    || archive_entry_filetype(entry) == AE_IFCHR)
Packit Service 1d0348
	    format_octal(archive_entry_dev(entry), h + c_rdev_offset, c_rdev_size);
Packit Service 1d0348
	else
Packit Service 1d0348
	    format_octal(0, h + c_rdev_offset, c_rdev_size);
Packit Service 1d0348
	format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
Packit Service 1d0348
	format_octal(pathlength, h + c_namesize_offset, c_namesize_size);
Packit Service 1d0348
Packit Service 1d0348
	/* Non-regular files don't store bodies. */
Packit Service 1d0348
	if (archive_entry_filetype(entry) != AE_IFREG)
Packit Service 1d0348
		archive_entry_set_size(entry, 0);
Packit Service 1d0348
Packit Service 1d0348
	/* Symlinks get the link written as the body of the entry. */
Packit Service 1d0348
	ret = archive_entry_symlink_l(entry, &p, &len, sconv);
Packit Service 1d0348
	if (ret != 0) {
Packit Service 1d0348
		if (errno == ENOMEM) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate memory for Linkname");
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
Packit Service 1d0348
		    "Can't translate linkname '%s' to %s",
Packit Service 1d0348
		    archive_entry_symlink(entry),
Packit Service 1d0348
		    archive_string_conversion_charset_name(sconv));
Packit Service 1d0348
		ret_final = ARCHIVE_WARN;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (len > 0 && p != NULL  &&  *p != '\0')
Packit Service 1d0348
		ret = format_octal(strlen(p), h + c_filesize_offset,
Packit Service 1d0348
		    c_filesize_size);
Packit Service 1d0348
	else
Packit Service 1d0348
		ret = format_octal(archive_entry_size(entry),
Packit Service 1d0348
		    h + c_filesize_offset, c_filesize_size);
Packit Service 1d0348
	if (ret) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "File is too large for cpio format.");
Packit Service 1d0348
		ret_final = ARCHIVE_FAILED;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, h, sizeof(h));
Packit Service 1d0348
	if (ret != ARCHIVE_OK) {
Packit Service 1d0348
		ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, path, pathlength);
Packit Service 1d0348
	if (ret != ARCHIVE_OK) {
Packit Service 1d0348
		ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	cpio->entry_bytes_remaining = archive_entry_size(entry);
Packit Service 1d0348
Packit Service 1d0348
	/* Write the symlink now. */
Packit Service 1d0348
	if (p != NULL  &&  *p != '\0') {
Packit Service 1d0348
		ret = __archive_write_output(a, p, strlen(p));
Packit Service 1d0348
		if (ret != ARCHIVE_OK) {
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
exit_write_header:
Packit Service 1d0348
	if (entry_main)
Packit Service 1d0348
		archive_entry_free(entry_main);
Packit Service 1d0348
	return (ret_final);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	if (s > cpio->entry_bytes_remaining)
Packit Service 1d0348
		s = (size_t)cpio->entry_bytes_remaining;
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, buff, s);
Packit Service 1d0348
	cpio->entry_bytes_remaining -= s;
Packit Service 1d0348
	if (ret >= 0)
Packit Service 1d0348
		return (s);
Packit Service 1d0348
	else
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Format a number into the specified field.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
format_octal(int64_t v, void *p, int digits)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t	max;
Packit Service 1d0348
	int	ret;
Packit Service 1d0348
Packit Service 1d0348
	max = (((int64_t)1) << (digits * 3)) - 1;
Packit Service 1d0348
	if (v >= 0  &&  v <= max) {
Packit Service 1d0348
	    format_octal_recursive(v, (char *)p, digits);
Packit Service 1d0348
	    ret = 0;
Packit Service 1d0348
	} else {
Packit Service 1d0348
	    format_octal_recursive(max, (char *)p, digits);
Packit Service 1d0348
	    ret = -1;
Packit Service 1d0348
	}
Packit Service 1d0348
	return (ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int64_t
Packit Service 1d0348
format_octal_recursive(int64_t v, char *p, int s)
Packit Service 1d0348
{
Packit Service 1d0348
	if (s == 0)
Packit Service 1d0348
		return (v);
Packit Service 1d0348
	v = format_octal_recursive(v, p+1, s-1);
Packit Service 1d0348
	*p = '0' + ((char)v & 7);
Packit Service 1d0348
	return (v >> 3);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_cpio_close(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	int er;
Packit Service 1d0348
	struct archive_entry *trailer;
Packit Service 1d0348
Packit Service 1d0348
	trailer = archive_entry_new2(NULL);
Packit Service 1d0348
	/* nlink = 1 here for GNU cpio compat. */
Packit Service 1d0348
	archive_entry_set_nlink(trailer, 1);
Packit Service 1d0348
	archive_entry_set_size(trailer, 0);
Packit Service 1d0348
	archive_entry_set_pathname(trailer, "TRAILER!!!");
Packit Service 1d0348
	er = write_header(a, trailer);
Packit Service 1d0348
	archive_entry_free(trailer);
Packit Service 1d0348
	return (er);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_cpio_free(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	free(cpio->ino_list);
Packit Service 1d0348
	free(cpio);
Packit Service 1d0348
	a->format_data = NULL;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_cpio_finish_entry(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	return (__archive_write_nulls(a,
Packit Service 1d0348
		(size_t)cpio->entry_bytes_remaining));
Packit Service 1d0348
}