1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "archive_platform.h"
27229592Smm__FBSDID("$FreeBSD$");
28228753Smm
29228753Smm#ifdef HAVE_ERRNO_H
30228753Smm#include <errno.h>
31228753Smm#endif
32228753Smm#include <stdio.h>
33228753Smm#ifdef HAVE_STDLIB_H
34228753Smm#include <stdlib.h>
35228753Smm#endif
36228753Smm#ifdef HAVE_STRING_H
37228753Smm#include <string.h>
38228753Smm#endif
39228753Smm
40228753Smm#include "archive.h"
41228753Smm#include "archive_entry.h"
42228753Smm#include "archive_private.h"
43228753Smm#include "archive_write_private.h"
44228753Smm
45228753Smmstatic ssize_t	archive_write_cpio_data(struct archive_write *,
46228753Smm		    const void *buff, size_t s);
47228753Smmstatic int	archive_write_cpio_finish(struct archive_write *);
48228753Smmstatic int	archive_write_cpio_destroy(struct archive_write *);
49228753Smmstatic int	archive_write_cpio_finish_entry(struct archive_write *);
50228753Smmstatic int	archive_write_cpio_header(struct archive_write *,
51228753Smm		    struct archive_entry *);
52228753Smmstatic int	format_octal(int64_t, void *, int);
53228753Smmstatic int64_t	format_octal_recursive(int64_t, char *, int);
54228753Smm
55228753Smmstruct cpio {
56228753Smm	uint64_t	  entry_bytes_remaining;
57228753Smm
58228753Smm	int64_t		  ino_next;
59228753Smm
60228753Smm	struct		 { int64_t old; int new;} *ino_list;
61228753Smm	size_t		  ino_list_size;
62228753Smm	size_t		  ino_list_next;
63228753Smm};
64228753Smm
65229592Smm#ifdef _MSC_VER
66229592Smm#define __packed
67229592Smm#pragma pack(push, 1)
68229592Smm#endif
69229592Smm
70228753Smmstruct cpio_header {
71228753Smm	char	c_magic[6];
72228753Smm	char	c_dev[6];
73228753Smm	char	c_ino[6];
74228753Smm	char	c_mode[6];
75228753Smm	char	c_uid[6];
76228753Smm	char	c_gid[6];
77228753Smm	char	c_nlink[6];
78228753Smm	char	c_rdev[6];
79228753Smm	char	c_mtime[11];
80228753Smm	char	c_namesize[6];
81228753Smm	char	c_filesize[11];
82229592Smm} __packed;
83228753Smm
84229592Smm#ifdef _MSC_VER
85229592Smm#undef __packed
86229592Smm#pragma pack(pop)
87229592Smm#endif
88229592Smm
89228753Smm/*
90228753Smm * Set output format to 'cpio' format.
91228753Smm */
92228753Smmint
93228753Smmarchive_write_set_format_cpio(struct archive *_a)
94228753Smm{
95228753Smm	struct archive_write *a = (struct archive_write *)_a;
96228753Smm	struct cpio *cpio;
97228753Smm
98228753Smm	/* If someone else was already registered, unregister them. */
99228753Smm	if (a->format_destroy != NULL)
100228753Smm		(a->format_destroy)(a);
101228753Smm
102228753Smm	cpio = (struct cpio *)malloc(sizeof(*cpio));
103228753Smm	if (cpio == NULL) {
104228753Smm		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
105228753Smm		return (ARCHIVE_FATAL);
106228753Smm	}
107228753Smm	memset(cpio, 0, sizeof(*cpio));
108228753Smm	a->format_data = cpio;
109228753Smm
110228753Smm	a->pad_uncompressed = 1;
111228753Smm	a->format_name = "cpio";
112228753Smm	a->format_write_header = archive_write_cpio_header;
113228753Smm	a->format_write_data = archive_write_cpio_data;
114228753Smm	a->format_finish_entry = archive_write_cpio_finish_entry;
115228753Smm	a->format_finish = archive_write_cpio_finish;
116228753Smm	a->format_destroy = archive_write_cpio_destroy;
117228753Smm	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
118228753Smm	a->archive.archive_format_name = "POSIX cpio";
119228753Smm	return (ARCHIVE_OK);
120228753Smm}
121228753Smm
122228753Smm/*
123228753Smm * Ino values are as long as 64 bits on some systems; cpio format
124228753Smm * only allows 18 bits and relies on the ino values to identify hardlinked
125228753Smm * files.  So, we can't merely "hash" the ino numbers since collisions
126228753Smm * would corrupt the archive.  Instead, we generate synthetic ino values
127228753Smm * to store in the archive and maintain a map of original ino values to
128228753Smm * synthetic ones so we can preserve hardlink information.
129228753Smm *
130228753Smm * TODO: Make this more efficient.  It's not as bad as it looks (most
131228753Smm * files don't have any hardlinks and we don't do any work here for those),
132228753Smm * but it wouldn't be hard to do better.
133228753Smm *
134228753Smm * TODO: Work with dev/ino pairs here instead of just ino values.
135228753Smm */
136228753Smmstatic int
137228753Smmsynthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
138228753Smm{
139228753Smm	int64_t ino = archive_entry_ino64(entry);
140228753Smm	int ino_new;
141228753Smm	size_t i;
142228753Smm
143228753Smm	/*
144228753Smm	 * If no index number was given, don't assign one.  In
145228753Smm	 * particular, this handles the end-of-archive marker
146228753Smm	 * correctly by giving it a zero index value.  (This is also
147228753Smm	 * why we start our synthetic index numbers with one below.)
148228753Smm	 */
149228753Smm	if (ino == 0)
150228753Smm		return (0);
151228753Smm
152228753Smm	/* Don't store a mapping if we don't need to. */
153228753Smm	if (archive_entry_nlink(entry) < 2) {
154228753Smm		return ++cpio->ino_next;
155228753Smm	}
156228753Smm
157228753Smm	/* Look up old ino; if we have it, this is a hardlink
158228753Smm	 * and we reuse the same value. */
159228753Smm	for (i = 0; i < cpio->ino_list_next; ++i) {
160228753Smm		if (cpio->ino_list[i].old == ino)
161228753Smm			return (cpio->ino_list[i].new);
162228753Smm	}
163228753Smm
164228753Smm	/* Assign a new index number. */
165228753Smm	ino_new = ++cpio->ino_next;
166228753Smm
167228753Smm	/* Ensure space for the new mapping. */
168228753Smm	if (cpio->ino_list_size <= cpio->ino_list_next) {
169228753Smm		size_t newsize = cpio->ino_list_size < 512
170228753Smm		    ? 512 : cpio->ino_list_size * 2;
171228753Smm		void *newlist = realloc(cpio->ino_list,
172228753Smm		    sizeof(cpio->ino_list[0]) * newsize);
173228753Smm		if (newlist == NULL)
174228753Smm			return (-1);
175228753Smm
176228753Smm		cpio->ino_list_size = newsize;
177228753Smm		cpio->ino_list = newlist;
178228753Smm	}
179228753Smm
180228753Smm	/* Record and return the new value. */
181228753Smm	cpio->ino_list[cpio->ino_list_next].old = ino;
182228753Smm	cpio->ino_list[cpio->ino_list_next].new = ino_new;
183228753Smm	++cpio->ino_list_next;
184228753Smm	return (ino_new);
185228753Smm}
186228753Smm
187228753Smmstatic int
188228753Smmarchive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
189228753Smm{
190228753Smm	struct cpio *cpio;
191228753Smm	const char *p, *path;
192228753Smm	int pathlength, ret, ret2;
193228753Smm	int64_t	ino;
194228753Smm	struct cpio_header	 h;
195228753Smm
196228753Smm	cpio = (struct cpio *)a->format_data;
197228753Smm	ret2 = ARCHIVE_OK;
198228753Smm
199228753Smm	path = archive_entry_pathname(entry);
200228753Smm	pathlength = (int)strlen(path) + 1; /* Include trailing null. */
201228753Smm
202228753Smm	memset(&h, 0, sizeof(h));
203228753Smm	format_octal(070707, &h.c_magic, sizeof(h.c_magic));
204228753Smm	format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
205228753Smm
206228753Smm	ino = synthesize_ino_value(cpio, entry);
207228753Smm	if (ino < 0) {
208228753Smm		archive_set_error(&a->archive, ENOMEM,
209228753Smm		    "No memory for ino translation table");
210228753Smm		return (ARCHIVE_FATAL);
211228753Smm	} else if (ino > 0777777) {
212228753Smm		archive_set_error(&a->archive, ERANGE,
213228753Smm		    "Too many files for this cpio format");
214228753Smm		return (ARCHIVE_FATAL);
215228753Smm	}
216228753Smm	format_octal(ino & 0777777, &h.c_ino, sizeof(h.c_ino));
217228753Smm
218228753Smm	format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
219228753Smm	format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
220228753Smm	format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
221228753Smm	format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
222228753Smm	if (archive_entry_filetype(entry) == AE_IFBLK
223228753Smm	    || archive_entry_filetype(entry) == AE_IFCHR)
224228753Smm	    format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
225228753Smm	else
226228753Smm	    format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
227228753Smm	format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
228228753Smm	format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
229228753Smm
230228753Smm	/* Non-regular files don't store bodies. */
231228753Smm	if (archive_entry_filetype(entry) != AE_IFREG)
232228753Smm		archive_entry_set_size(entry, 0);
233228753Smm
234228753Smm	/* Symlinks get the link written as the body of the entry. */
235228753Smm	p = archive_entry_symlink(entry);
236228753Smm	if (p != NULL  &&  *p != '\0')
237228753Smm		format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
238228753Smm	else
239228753Smm		format_octal(archive_entry_size(entry),
240228753Smm		    &h.c_filesize, sizeof(h.c_filesize));
241228753Smm
242228753Smm	ret = (a->compressor.write)(a, &h, sizeof(h));
243228753Smm	if (ret != ARCHIVE_OK)
244228753Smm		return (ARCHIVE_FATAL);
245228753Smm
246228753Smm	ret = (a->compressor.write)(a, path, pathlength);
247228753Smm	if (ret != ARCHIVE_OK)
248228753Smm		return (ARCHIVE_FATAL);
249228753Smm
250228753Smm	cpio->entry_bytes_remaining = archive_entry_size(entry);
251228753Smm
252228753Smm	/* Write the symlink now. */
253228753Smm	if (p != NULL  &&  *p != '\0')
254228753Smm		ret = (a->compressor.write)(a, p, strlen(p));
255228753Smm
256228753Smm	if (ret == ARCHIVE_OK)
257228753Smm		ret = ret2;
258228753Smm	return (ret);
259228753Smm}
260228753Smm
261228753Smmstatic ssize_t
262228753Smmarchive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
263228753Smm{
264228753Smm	struct cpio *cpio;
265228753Smm	int ret;
266228753Smm
267228753Smm	cpio = (struct cpio *)a->format_data;
268228753Smm	if (s > cpio->entry_bytes_remaining)
269228753Smm		s = cpio->entry_bytes_remaining;
270228753Smm
271228753Smm	ret = (a->compressor.write)(a, buff, s);
272228753Smm	cpio->entry_bytes_remaining -= s;
273228753Smm	if (ret >= 0)
274228753Smm		return (s);
275228753Smm	else
276228753Smm		return (ret);
277228753Smm}
278228753Smm
279228753Smm/*
280228753Smm * Format a number into the specified field.
281228753Smm */
282228753Smmstatic int
283228753Smmformat_octal(int64_t v, void *p, int digits)
284228753Smm{
285228753Smm	int64_t	max;
286228753Smm	int	ret;
287228753Smm
288228753Smm	max = (((int64_t)1) << (digits * 3)) - 1;
289228753Smm	if (v >= 0  &&  v <= max) {
290228753Smm	    format_octal_recursive(v, (char *)p, digits);
291228753Smm	    ret = 0;
292228753Smm	} else {
293228753Smm	    format_octal_recursive(max, (char *)p, digits);
294228753Smm	    ret = -1;
295228753Smm	}
296228753Smm	return (ret);
297228753Smm}
298228753Smm
299228753Smmstatic int64_t
300228753Smmformat_octal_recursive(int64_t v, char *p, int s)
301228753Smm{
302228753Smm	if (s == 0)
303228753Smm		return (v);
304228753Smm	v = format_octal_recursive(v, p+1, s-1);
305228753Smm	*p = '0' + (v & 7);
306228753Smm	return (v >> 3);
307228753Smm}
308228753Smm
309228753Smmstatic int
310228753Smmarchive_write_cpio_finish(struct archive_write *a)
311228753Smm{
312228753Smm	int er;
313228753Smm	struct archive_entry *trailer;
314228753Smm
315228753Smm	trailer = archive_entry_new();
316228753Smm	/* nlink = 1 here for GNU cpio compat. */
317228753Smm	archive_entry_set_nlink(trailer, 1);
318228753Smm	archive_entry_set_pathname(trailer, "TRAILER!!!");
319228753Smm	er = archive_write_cpio_header(a, trailer);
320228753Smm	archive_entry_free(trailer);
321228753Smm	return (er);
322228753Smm}
323228753Smm
324228753Smmstatic int
325228753Smmarchive_write_cpio_destroy(struct archive_write *a)
326228753Smm{
327228753Smm	struct cpio *cpio;
328228753Smm
329228753Smm	cpio = (struct cpio *)a->format_data;
330228753Smm	free(cpio->ino_list);
331228753Smm	free(cpio);
332228753Smm	a->format_data = NULL;
333228753Smm	return (ARCHIVE_OK);
334228753Smm}
335228753Smm
336228753Smmstatic int
337228753Smmarchive_write_cpio_finish_entry(struct archive_write *a)
338228753Smm{
339228753Smm	struct cpio *cpio;
340228753Smm	size_t to_write;
341228753Smm	int ret;
342228753Smm
343228753Smm	cpio = (struct cpio *)a->format_data;
344228753Smm	ret = ARCHIVE_OK;
345228753Smm	while (cpio->entry_bytes_remaining > 0) {
346228753Smm		to_write = cpio->entry_bytes_remaining < a->null_length ?
347228753Smm		    cpio->entry_bytes_remaining : a->null_length;
348228753Smm		ret = (a->compressor.write)(a, a->nulls, to_write);
349228753Smm		if (ret != ARCHIVE_OK)
350228753Smm			return (ret);
351228753Smm		cpio->entry_bytes_remaining -= to_write;
352228753Smm	}
353228753Smm	return (ret);
354228753Smm}
355