Blame libarchive/archive_write_set_format_ar.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2007 Kai Wang
Packit Service 1d0348
 * Copyright (c) 2007 Tim Kientzle
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
 *    in this position and unchanged.
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_ar.c 201108 2009-12-28 03:28:21Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
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_private.h"
Packit Service 1d0348
#include "archive_write_private.h"
Packit Service 1d0348
Packit Service 1d0348
struct ar_w {
Packit Service 1d0348
	uint64_t	 entry_bytes_remaining;
Packit Service 1d0348
	uint64_t	 entry_padding;
Packit Service 1d0348
	int		 is_strtab;
Packit Service 1d0348
	int		 has_strtab;
Packit Service 1d0348
	char		 wrote_global_header;
Packit Service 1d0348
	char		*strtab;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Define structure of the "ar" header.
Packit Service 1d0348
 */
Packit Service 1d0348
#define AR_name_offset 0
Packit Service 1d0348
#define AR_name_size 16
Packit Service 1d0348
#define AR_date_offset 16
Packit Service 1d0348
#define AR_date_size 12
Packit Service 1d0348
#define AR_uid_offset 28
Packit Service 1d0348
#define AR_uid_size 6
Packit Service 1d0348
#define AR_gid_offset 34
Packit Service 1d0348
#define AR_gid_size 6
Packit Service 1d0348
#define AR_mode_offset 40
Packit Service 1d0348
#define AR_mode_size 8
Packit Service 1d0348
#define AR_size_offset 48
Packit Service 1d0348
#define AR_size_size 10
Packit Service 1d0348
#define AR_fmag_offset 58
Packit Service 1d0348
#define AR_fmag_size 2
Packit Service 1d0348
Packit Service 1d0348
static int		 archive_write_set_format_ar(struct archive_write *);
Packit Service 1d0348
static int		 archive_write_ar_header(struct archive_write *,
Packit Service 1d0348
			     struct archive_entry *);
Packit Service 1d0348
static ssize_t		 archive_write_ar_data(struct archive_write *,
Packit Service 1d0348
			     const void *buff, size_t s);
Packit Service 1d0348
static int		 archive_write_ar_free(struct archive_write *);
Packit Service 1d0348
static int		 archive_write_ar_close(struct archive_write *);
Packit Service 1d0348
static int		 archive_write_ar_finish_entry(struct archive_write *);
Packit Service 1d0348
static const char	*ar_basename(const char *path);
Packit Service 1d0348
static int		 format_octal(int64_t v, char *p, int s);
Packit Service 1d0348
static int		 format_decimal(int64_t v, char *p, int s);
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_write_set_format_ar_bsd(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_write *a = (struct archive_write *)_a;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_write_set_format_ar_bsd");
Packit Service 1d0348
	r = archive_write_set_format_ar(a);
Packit Service 1d0348
	if (r == ARCHIVE_OK) {
Packit Service 1d0348
		a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
Packit Service 1d0348
		a->archive.archive_format_name = "ar (BSD)";
Packit Service 1d0348
	}
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_write_set_format_ar_svr4(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_write *a = (struct archive_write *)_a;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_write_set_format_ar_svr4");
Packit Service 1d0348
	r = archive_write_set_format_ar(a);
Packit Service 1d0348
	if (r == ARCHIVE_OK) {
Packit Service 1d0348
		a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
Packit Service 1d0348
		a->archive.archive_format_name = "ar (GNU/SVR4)";
Packit Service 1d0348
	}
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Generic initialization.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_set_format_ar(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar_w *ar;
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
	ar = (struct ar_w *)calloc(1, sizeof(*ar));
Packit Service 1d0348
	if (ar == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM, "Can't allocate ar data");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	a->format_data = ar;
Packit Service 1d0348
Packit Service 1d0348
	a->format_name = "ar";
Packit Service 1d0348
	a->format_write_header = archive_write_ar_header;
Packit Service 1d0348
	a->format_write_data = archive_write_ar_data;
Packit Service 1d0348
	a->format_close = archive_write_ar_close;
Packit Service 1d0348
	a->format_free = archive_write_ar_free;
Packit Service 1d0348
	a->format_finish_entry = archive_write_ar_finish_entry;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_ar_header(struct archive_write *a, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	int ret, append_fn;
Packit Service 1d0348
	char buff[60];
Packit Service 1d0348
	char *ss, *se;
Packit Service 1d0348
	struct ar_w *ar;
Packit Service 1d0348
	const char *pathname;
Packit Service 1d0348
	const char *filename;
Packit Service 1d0348
	int64_t size;
Packit Service 1d0348
Packit Service 1d0348
	append_fn = 0;
Packit Service 1d0348
	ar = (struct ar_w *)a->format_data;
Packit Service 1d0348
	ar->is_strtab = 0;
Packit Service 1d0348
	filename = NULL;
Packit Service 1d0348
	size = archive_entry_size(entry);
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Reject files with empty name.
Packit Service 1d0348
	 */
Packit Service 1d0348
	pathname = archive_entry_pathname(entry);
Packit Service 1d0348
	if (pathname == NULL || *pathname == '\0') {
Packit Service 1d0348
		archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
		    "Invalid filename");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * If we are now at the beginning of the archive,
Packit Service 1d0348
	 * we need first write the ar global header.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (!ar->wrote_global_header) {
Packit Service 1d0348
		__archive_write_output(a, "!<arch>\n", 8);
Packit Service 1d0348
		ar->wrote_global_header = 1;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	memset(buff, ' ', 60);
Packit Service 1d0348
	memcpy(&buff[AR_fmag_offset], "`\n", 2);
Packit Service 1d0348
Packit Service 1d0348
	if (strcmp(pathname, "/") == 0 ) {
Packit Service 1d0348
		/* Entry is archive symbol table in GNU format */
Packit Service 1d0348
		buff[AR_name_offset] = '/';
Packit Service 1d0348
		goto stat;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (strcmp(pathname, "__.SYMDEF") == 0) {
Packit Service 1d0348
		/* Entry is archive symbol table in BSD format */
Packit Service 1d0348
		memcpy(buff + AR_name_offset, "__.SYMDEF", 9);
Packit Service 1d0348
		goto stat;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (strcmp(pathname, "//") == 0) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Entry is archive filename table, inform that we should
Packit Service 1d0348
		 * collect strtab in next _data call.
Packit Service 1d0348
		 */
Packit Service 1d0348
		ar->is_strtab = 1;
Packit Service 1d0348
		buff[AR_name_offset] = buff[AR_name_offset + 1] = '/';
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * For archive string table, only ar_size field should
Packit Service 1d0348
		 * be set.
Packit Service 1d0348
		 */
Packit Service 1d0348
		goto size;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Otherwise, entry is a normal archive member.
Packit Service 1d0348
	 * Strip leading paths from filenames, if any.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if ((filename = ar_basename(pathname)) == NULL) {
Packit Service 1d0348
		/* Reject filenames with trailing "/" */
Packit Service 1d0348
		archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
		    "Invalid filename");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * SVR4/GNU variant use a "/" to mark then end of the filename,
Packit Service 1d0348
		 * make it possible to have embedded spaces in the filename.
Packit Service 1d0348
		 * So, the longest filename here (without extension) is
Packit Service 1d0348
		 * actually 15 bytes.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (strlen(filename) <= 15) {
Packit Service 1d0348
			memcpy(&buff[AR_name_offset],
Packit Service 1d0348
			    filename, strlen(filename));
Packit Service 1d0348
			buff[AR_name_offset + strlen(filename)] = '/';
Packit Service 1d0348
		} else {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * For filename longer than 15 bytes, GNU variant
Packit Service 1d0348
			 * makes use of a string table and instead stores the
Packit Service 1d0348
			 * offset of the real filename to in the ar_name field.
Packit Service 1d0348
			 * The string table should have been written before.
Packit Service 1d0348
			 */
Packit Service 1d0348
			if (ar->has_strtab <= 0) {
Packit Service 1d0348
				archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
				    "Can't find string table");
Packit Service 1d0348
				return (ARCHIVE_WARN);
Packit Service 1d0348
			}
Packit Service 1d0348
Packit Service 1d0348
			se = (char *)malloc(strlen(filename) + 3);
Packit Service 1d0348
			if (se == NULL) {
Packit Service 1d0348
				archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
				    "Can't allocate filename buffer");
Packit Service 1d0348
				return (ARCHIVE_FATAL);
Packit Service 1d0348
			}
Packit Service 1d0348
Packit Service 1d0348
			memcpy(se, filename, strlen(filename));
Packit Service 1d0348
			strcpy(se + strlen(filename), "/\n");
Packit Service 1d0348
Packit Service 1d0348
			ss = strstr(ar->strtab, se);
Packit Service 1d0348
			free(se);
Packit Service 1d0348
Packit Service 1d0348
			if (ss == NULL) {
Packit Service 1d0348
				archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
				    "Invalid string table");
Packit Service 1d0348
				return (ARCHIVE_WARN);
Packit Service 1d0348
			}
Packit Service 1d0348
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * GNU variant puts "/" followed by digits into
Packit Service 1d0348
			 * ar_name field. These digits indicates the real
Packit Service 1d0348
			 * filename string's offset to the string table.
Packit Service 1d0348
			 */
Packit Service 1d0348
			buff[AR_name_offset] = '/';
Packit Service 1d0348
			if (format_decimal(ss - ar->strtab,
Packit Service 1d0348
			    buff + AR_name_offset + 1,
Packit Service 1d0348
			    AR_name_size - 1)) {
Packit Service 1d0348
				archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
				    "string table offset too large");
Packit Service 1d0348
				return (ARCHIVE_WARN);
Packit Service 1d0348
			}
Packit Service 1d0348
		}
Packit Service 1d0348
	} else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * BSD variant: for any file name which is more than
Packit Service 1d0348
		 * 16 chars or contains one or more embedded space(s), the
Packit Service 1d0348
		 * string "#1/" followed by the ASCII length of the name is
Packit Service 1d0348
		 * put into the ar_name field. The file size (stored in the
Packit Service 1d0348
		 * ar_size field) is incremented by the length of the name.
Packit Service 1d0348
		 * The name is then written immediately following the
Packit Service 1d0348
		 * archive header.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (strlen(filename) <= 16 && strchr(filename, ' ') == NULL) {
Packit Service 1d0348
			memcpy(&buff[AR_name_offset], filename, strlen(filename));
Packit Service 1d0348
			buff[AR_name_offset + strlen(filename)] = ' ';
Packit Service 1d0348
		}
Packit Service 1d0348
		else {
Packit Service 1d0348
			memcpy(buff + AR_name_offset, "#1/", 3);
Packit Service 1d0348
			if (format_decimal(strlen(filename),
Packit Service 1d0348
			    buff + AR_name_offset + 3,
Packit Service 1d0348
			    AR_name_size - 3)) {
Packit Service 1d0348
				archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
				    "File name too long");
Packit Service 1d0348
				return (ARCHIVE_WARN);
Packit Service 1d0348
			}
Packit Service 1d0348
			append_fn = 1;
Packit Service 1d0348
			size += strlen(filename);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
stat:
Packit Service 1d0348
	if (format_decimal(archive_entry_mtime(entry), buff + AR_date_offset, AR_date_size)) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "File modification time too large");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (format_decimal(archive_entry_uid(entry), buff + AR_uid_offset, AR_uid_size)) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "Numeric user ID too large");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (format_decimal(archive_entry_gid(entry), buff + AR_gid_offset, AR_gid_size)) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "Numeric group ID too large");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (format_octal(archive_entry_mode(entry), buff + AR_mode_offset, AR_mode_size)) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "Numeric mode too large");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Sanity Check: A non-pseudo archive member should always be
Packit Service 1d0348
	 * a regular file.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (filename != NULL && archive_entry_filetype(entry) != AE_IFREG) {
Packit Service 1d0348
		archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
		    "Regular file required for non-pseudo member");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
size:
Packit Service 1d0348
	if (format_decimal(size, buff + AR_size_offset, AR_size_size)) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "File size out of range");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, buff, 60);
Packit Service 1d0348
	if (ret != ARCHIVE_OK)
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
Packit Service 1d0348
	ar->entry_bytes_remaining = size;
Packit Service 1d0348
	ar->entry_padding = ar->entry_bytes_remaining % 2;
Packit Service 1d0348
Packit Service 1d0348
	if (append_fn > 0) {
Packit Service 1d0348
		ret = __archive_write_output(a, filename, strlen(filename));
Packit Service 1d0348
		if (ret != ARCHIVE_OK)
Packit Service 1d0348
			return (ret);
Packit Service 1d0348
		ar->entry_bytes_remaining -= strlen(filename);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
archive_write_ar_data(struct archive_write *a, const void *buff, size_t s)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar_w *ar;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar_w *)a->format_data;
Packit Service 1d0348
	if (s > ar->entry_bytes_remaining)
Packit Service 1d0348
		s = (size_t)ar->entry_bytes_remaining;
Packit Service 1d0348
Packit Service 1d0348
	if (ar->is_strtab > 0) {
Packit Service 1d0348
		if (ar->has_strtab > 0) {
Packit Service 1d0348
			archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
			    "More than one string tables exist");
Packit Service 1d0348
			return (ARCHIVE_WARN);
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		ar->strtab = (char *)malloc(s + 1);
Packit Service 1d0348
		if (ar->strtab == NULL) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate strtab buffer");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		memcpy(ar->strtab, buff, s);
Packit Service 1d0348
		ar->strtab[s] = '\0';
Packit Service 1d0348
		ar->has_strtab = 1;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, buff, s);
Packit Service 1d0348
	if (ret != ARCHIVE_OK)
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
Packit Service 1d0348
	ar->entry_bytes_remaining -= s;
Packit Service 1d0348
	return (s);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_ar_free(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar_w *ar;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar_w *)a->format_data;
Packit Service 1d0348
Packit Service 1d0348
	if (ar == NULL)
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
Packit Service 1d0348
	if (ar->has_strtab > 0) {
Packit Service 1d0348
		free(ar->strtab);
Packit Service 1d0348
		ar->strtab = NULL;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	free(ar);
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_ar_close(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar_w *ar;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * If we haven't written anything yet, we need to write
Packit Service 1d0348
	 * the ar global header now to make it a valid ar archive.
Packit Service 1d0348
	 */
Packit Service 1d0348
	ar = (struct ar_w *)a->format_data;
Packit Service 1d0348
	if (!ar->wrote_global_header) {
Packit Service 1d0348
		ar->wrote_global_header = 1;
Packit Service 1d0348
		ret = __archive_write_output(a, "!<arch>\n", 8);
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_ar_finish_entry(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar_w *ar;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar_w *)a->format_data;
Packit Service 1d0348
Packit Service 1d0348
	if (ar->entry_bytes_remaining != 0) {
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Entry remaining bytes larger than 0");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (ar->entry_padding == 0) {
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (ar->entry_padding != 1) {
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Padding wrong size: %ju should be 1 or 0",
Packit Service 1d0348
		    (uintmax_t)ar->entry_padding);
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, "\n", 1);
Packit Service 1d0348
	return (ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Format a number into the specified field using base-8.
Packit Service 1d0348
 * NB: This version is slightly different from the one in
Packit Service 1d0348
 * _ustar.c
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
format_octal(int64_t v, char *p, int s)
Packit Service 1d0348
{
Packit Service 1d0348
	int len;
Packit Service 1d0348
	char *h;
Packit Service 1d0348
Packit Service 1d0348
	len = s;
Packit Service 1d0348
	h = p;
Packit Service 1d0348
Packit Service 1d0348
	/* Octal values can't be negative, so use 0. */
Packit Service 1d0348
	if (v < 0) {
Packit Service 1d0348
		while (len-- > 0)
Packit Service 1d0348
			*p++ = '0';
Packit Service 1d0348
		return (-1);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	p += s;		/* Start at the end and work backwards. */
Packit Service 1d0348
	do {
Packit Service 1d0348
		*--p = (char)('0' + (v & 7));
Packit Service 1d0348
		v >>= 3;
Packit Service 1d0348
	} while (--s > 0 && v > 0);
Packit Service 1d0348
Packit Service 1d0348
	if (v == 0) {
Packit Service 1d0348
		memmove(h, p, len - s);
Packit Service 1d0348
		p = h + len - s;
Packit Service 1d0348
		while (s-- > 0)
Packit Service 1d0348
			*p++ = ' ';
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	}
Packit Service 1d0348
	/* If it overflowed, fill field with max value. */
Packit Service 1d0348
	while (len-- > 0)
Packit Service 1d0348
		*p++ = '7';
Packit Service 1d0348
Packit Service 1d0348
	return (-1);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Format a number into the specified field using base-10.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
format_decimal(int64_t v, char *p, int s)
Packit Service 1d0348
{
Packit Service 1d0348
	int len;
Packit Service 1d0348
	char *h;
Packit Service 1d0348
Packit Service 1d0348
	len = s;
Packit Service 1d0348
	h = p;
Packit Service 1d0348
Packit Service 1d0348
	/* Negative values in ar header are meaningless, so use 0. */
Packit Service 1d0348
	if (v < 0) {
Packit Service 1d0348
		while (len-- > 0)
Packit Service 1d0348
			*p++ = '0';
Packit Service 1d0348
		return (-1);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	p += s;
Packit Service 1d0348
	do {
Packit Service 1d0348
		*--p = (char)('0' + (v % 10));
Packit Service 1d0348
		v /= 10;
Packit Service 1d0348
	} while (--s > 0 && v > 0);
Packit Service 1d0348
Packit Service 1d0348
	if (v == 0) {
Packit Service 1d0348
		memmove(h, p, len - s);
Packit Service 1d0348
		p = h + len - s;
Packit Service 1d0348
		while (s-- > 0)
Packit Service 1d0348
			*p++ = ' ';
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	}
Packit Service 1d0348
	/* If it overflowed, fill field with max value. */
Packit Service 1d0348
	while (len-- > 0)
Packit Service 1d0348
		*p++ = '9';
Packit Service 1d0348
Packit Service 1d0348
	return (-1);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static const char *
Packit Service 1d0348
ar_basename(const char *path)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *endp, *startp;
Packit Service 1d0348
Packit Service 1d0348
	endp = path + strlen(path) - 1;
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * For filename with trailing slash(es), we return
Packit Service 1d0348
	 * NULL indicating an error.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (*endp == '/')
Packit Service 1d0348
		return (NULL);
Packit Service 1d0348
Packit Service 1d0348
	/* Find the start of the base */
Packit Service 1d0348
	startp = endp;
Packit Service 1d0348
	while (startp > path && *(startp - 1) != '/')
Packit Service 1d0348
		startp--;
Packit Service 1d0348
	
Packit Service 1d0348
	return (startp);
Packit Service 1d0348
}