1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "archive_platform.h"
27__FBSDID("$FreeBSD$");
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_private.h"
43#include "archive_write_private.h"
44
45static ssize_t	archive_write_cpio_data(struct archive_write *,
46		    const void *buff, size_t s);
47static int	archive_write_cpio_finish(struct archive_write *);
48static int	archive_write_cpio_destroy(struct archive_write *);
49static int	archive_write_cpio_finish_entry(struct archive_write *);
50static int	archive_write_cpio_header(struct archive_write *,
51		    struct archive_entry *);
52static int	format_octal(int64_t, void *, int);
53static int64_t	format_octal_recursive(int64_t, char *, int);
54
55struct cpio {
56	uint64_t	  entry_bytes_remaining;
57
58	int64_t		  ino_next;
59
60	struct		 { int64_t old; int new;} *ino_list;
61	size_t		  ino_list_size;
62	size_t		  ino_list_next;
63};
64
65#ifdef _MSC_VER
66#define __packed
67#pragma pack(push, 1)
68#endif
69
70struct cpio_header {
71	char	c_magic[6];
72	char	c_dev[6];
73	char	c_ino[6];
74	char	c_mode[6];
75	char	c_uid[6];
76	char	c_gid[6];
77	char	c_nlink[6];
78	char	c_rdev[6];
79	char	c_mtime[11];
80	char	c_namesize[6];
81	char	c_filesize[11];
82} __packed;
83
84#ifdef _MSC_VER
85#undef __packed
86#pragma pack(pop)
87#endif
88
89/*
90 * Set output format to 'cpio' format.
91 */
92int
93archive_write_set_format_cpio(struct archive *_a)
94{
95	struct archive_write *a = (struct archive_write *)_a;
96	struct cpio *cpio;
97
98	/* If someone else was already registered, unregister them. */
99	if (a->format_destroy != NULL)
100		(a->format_destroy)(a);
101
102	cpio = (struct cpio *)malloc(sizeof(*cpio));
103	if (cpio == NULL) {
104		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
105		return (ARCHIVE_FATAL);
106	}
107	memset(cpio, 0, sizeof(*cpio));
108	a->format_data = cpio;
109
110	a->pad_uncompressed = 1;
111	a->format_name = "cpio";
112	a->format_write_header = archive_write_cpio_header;
113	a->format_write_data = archive_write_cpio_data;
114	a->format_finish_entry = archive_write_cpio_finish_entry;
115	a->format_finish = archive_write_cpio_finish;
116	a->format_destroy = archive_write_cpio_destroy;
117	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
118	a->archive.archive_format_name = "POSIX cpio";
119	return (ARCHIVE_OK);
120}
121
122/*
123 * Ino values are as long as 64 bits on some systems; cpio format
124 * only allows 18 bits and relies on the ino values to identify hardlinked
125 * files.  So, we can't merely "hash" the ino numbers since collisions
126 * would corrupt the archive.  Instead, we generate synthetic ino values
127 * to store in the archive and maintain a map of original ino values to
128 * synthetic ones so we can preserve hardlink information.
129 *
130 * TODO: Make this more efficient.  It's not as bad as it looks (most
131 * files don't have any hardlinks and we don't do any work here for those),
132 * but it wouldn't be hard to do better.
133 *
134 * TODO: Work with dev/ino pairs here instead of just ino values.
135 */
136static int
137synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
138{
139	int64_t ino = archive_entry_ino64(entry);
140	int ino_new;
141	size_t i;
142
143	/*
144	 * If no index number was given, don't assign one.  In
145	 * particular, this handles the end-of-archive marker
146	 * correctly by giving it a zero index value.  (This is also
147	 * why we start our synthetic index numbers with one below.)
148	 */
149	if (ino == 0)
150		return (0);
151
152	/* Don't store a mapping if we don't need to. */
153	if (archive_entry_nlink(entry) < 2) {
154		return ++cpio->ino_next;
155	}
156
157	/* Look up old ino; if we have it, this is a hardlink
158	 * and we reuse the same value. */
159	for (i = 0; i < cpio->ino_list_next; ++i) {
160		if (cpio->ino_list[i].old == ino)
161			return (cpio->ino_list[i].new);
162	}
163
164	/* Assign a new index number. */
165	ino_new = ++cpio->ino_next;
166
167	/* Ensure space for the new mapping. */
168	if (cpio->ino_list_size <= cpio->ino_list_next) {
169		size_t newsize = cpio->ino_list_size < 512
170		    ? 512 : cpio->ino_list_size * 2;
171		void *newlist = realloc(cpio->ino_list,
172		    sizeof(cpio->ino_list[0]) * newsize);
173		if (newlist == NULL)
174			return (-1);
175
176		cpio->ino_list_size = newsize;
177		cpio->ino_list = newlist;
178	}
179
180	/* Record and return the new value. */
181	cpio->ino_list[cpio->ino_list_next].old = ino;
182	cpio->ino_list[cpio->ino_list_next].new = ino_new;
183	++cpio->ino_list_next;
184	return (ino_new);
185}
186
187static int
188archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
189{
190	struct cpio *cpio;
191	const char *p, *path;
192	int pathlength, ret, ret2;
193	int64_t	ino;
194	struct cpio_header	 h;
195
196	cpio = (struct cpio *)a->format_data;
197	ret2 = ARCHIVE_OK;
198
199	path = archive_entry_pathname(entry);
200	pathlength = (int)strlen(path) + 1; /* Include trailing null. */
201
202	memset(&h, 0, sizeof(h));
203	format_octal(070707, &h.c_magic, sizeof(h.c_magic));
204	format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
205
206	ino = synthesize_ino_value(cpio, entry);
207	if (ino < 0) {
208		archive_set_error(&a->archive, ENOMEM,
209		    "No memory for ino translation table");
210		return (ARCHIVE_FATAL);
211	} else if (ino > 0777777) {
212		archive_set_error(&a->archive, ERANGE,
213		    "Too many files for this cpio format");
214		return (ARCHIVE_FATAL);
215	}
216	format_octal(ino & 0777777, &h.c_ino, sizeof(h.c_ino));
217
218	format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
219	format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
220	format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
221	format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
222	if (archive_entry_filetype(entry) == AE_IFBLK
223	    || archive_entry_filetype(entry) == AE_IFCHR)
224	    format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
225	else
226	    format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
227	format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
228	format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
229
230	/* Non-regular files don't store bodies. */
231	if (archive_entry_filetype(entry) != AE_IFREG)
232		archive_entry_set_size(entry, 0);
233
234	/* Symlinks get the link written as the body of the entry. */
235	p = archive_entry_symlink(entry);
236	if (p != NULL  &&  *p != '\0')
237		format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
238	else
239		format_octal(archive_entry_size(entry),
240		    &h.c_filesize, sizeof(h.c_filesize));
241
242	ret = (a->compressor.write)(a, &h, sizeof(h));
243	if (ret != ARCHIVE_OK)
244		return (ARCHIVE_FATAL);
245
246	ret = (a->compressor.write)(a, path, pathlength);
247	if (ret != ARCHIVE_OK)
248		return (ARCHIVE_FATAL);
249
250	cpio->entry_bytes_remaining = archive_entry_size(entry);
251
252	/* Write the symlink now. */
253	if (p != NULL  &&  *p != '\0')
254		ret = (a->compressor.write)(a, p, strlen(p));
255
256	if (ret == ARCHIVE_OK)
257		ret = ret2;
258	return (ret);
259}
260
261static ssize_t
262archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
263{
264	struct cpio *cpio;
265	int ret;
266
267	cpio = (struct cpio *)a->format_data;
268	if (s > cpio->entry_bytes_remaining)
269		s = cpio->entry_bytes_remaining;
270
271	ret = (a->compressor.write)(a, buff, s);
272	cpio->entry_bytes_remaining -= s;
273	if (ret >= 0)
274		return (s);
275	else
276		return (ret);
277}
278
279/*
280 * Format a number into the specified field.
281 */
282static int
283format_octal(int64_t v, void *p, int digits)
284{
285	int64_t	max;
286	int	ret;
287
288	max = (((int64_t)1) << (digits * 3)) - 1;
289	if (v >= 0  &&  v <= max) {
290	    format_octal_recursive(v, (char *)p, digits);
291	    ret = 0;
292	} else {
293	    format_octal_recursive(max, (char *)p, digits);
294	    ret = -1;
295	}
296	return (ret);
297}
298
299static int64_t
300format_octal_recursive(int64_t v, char *p, int s)
301{
302	if (s == 0)
303		return (v);
304	v = format_octal_recursive(v, p+1, s-1);
305	*p = '0' + (v & 7);
306	return (v >> 3);
307}
308
309static int
310archive_write_cpio_finish(struct archive_write *a)
311{
312	int er;
313	struct archive_entry *trailer;
314
315	trailer = archive_entry_new();
316	/* nlink = 1 here for GNU cpio compat. */
317	archive_entry_set_nlink(trailer, 1);
318	archive_entry_set_pathname(trailer, "TRAILER!!!");
319	er = archive_write_cpio_header(a, trailer);
320	archive_entry_free(trailer);
321	return (er);
322}
323
324static int
325archive_write_cpio_destroy(struct archive_write *a)
326{
327	struct cpio *cpio;
328
329	cpio = (struct cpio *)a->format_data;
330	free(cpio->ino_list);
331	free(cpio);
332	a->format_data = NULL;
333	return (ARCHIVE_OK);
334}
335
336static int
337archive_write_cpio_finish_entry(struct archive_write *a)
338{
339	struct cpio *cpio;
340	size_t to_write;
341	int ret;
342
343	cpio = (struct cpio *)a->format_data;
344	ret = ARCHIVE_OK;
345	while (cpio->entry_bytes_remaining > 0) {
346		to_write = cpio->entry_bytes_remaining < a->null_length ?
347		    cpio->entry_bytes_remaining : a->null_length;
348		ret = (a->compressor.write)(a, a->nulls, to_write);
349		if (ret != ARCHIVE_OK)
350			return (ret);
351		cpio->entry_bytes_remaining -= to_write;
352	}
353	return (ret);
354}
355