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: head/lib/libarchive/archive_write_set_format_cpio.c 201170 2009-12-29 06:34:23Z kientzle $");
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 
45 static ssize_t	archive_write_cpio_data(struct archive_write *,
46 		    const void *buff, size_t s);
47 static int	archive_write_cpio_finish(struct archive_write *);
48 static int	archive_write_cpio_destroy(struct archive_write *);
49 static int	archive_write_cpio_finish_entry(struct archive_write *);
50 static int	archive_write_cpio_header(struct archive_write *,
51 		    struct archive_entry *);
52 static int	format_octal(int64_t, void *, int);
53 static int64_t	format_octal_recursive(int64_t, char *, int);
54 
55 struct 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 struct cpio_header {
66 	char	c_magic[6];
67 	char	c_dev[6];
68 	char	c_ino[6];
69 	char	c_mode[6];
70 	char	c_uid[6];
71 	char	c_gid[6];
72 	char	c_nlink[6];
73 	char	c_rdev[6];
74 	char	c_mtime[11];
75 	char	c_namesize[6];
76 	char	c_filesize[11];
77 };
78 
79 /*
80  * Set output format to 'cpio' format.
81  */
82 int
83 archive_write_set_format_cpio(struct archive *_a)
84 {
85 	struct archive_write *a = (struct archive_write *)_a;
86 	struct cpio *cpio;
87 
88 	/* If someone else was already registered, unregister them. */
89 	if (a->format_destroy != NULL)
90 		(a->format_destroy)(a);
91 
92 	cpio = (struct cpio *)malloc(sizeof(*cpio));
93 	if (cpio == NULL) {
94 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
95 		return (ARCHIVE_FATAL);
96 	}
97 	memset(cpio, 0, sizeof(*cpio));
98 	a->format_data = cpio;
99 
100 	a->pad_uncompressed = 1;
101 	a->format_name = "cpio";
102 	a->format_write_header = archive_write_cpio_header;
103 	a->format_write_data = archive_write_cpio_data;
104 	a->format_finish_entry = archive_write_cpio_finish_entry;
105 	a->format_finish = archive_write_cpio_finish;
106 	a->format_destroy = archive_write_cpio_destroy;
107 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
108 	a->archive.archive_format_name = "POSIX cpio";
109 	return (ARCHIVE_OK);
110 }
111 
112 /*
113  * Ino values are as long as 64 bits on some systems; cpio format
114  * only allows 18 bits and relies on the ino values to identify hardlinked
115  * files.  So, we can't merely "hash" the ino numbers since collisions
116  * would corrupt the archive.  Instead, we generate synthetic ino values
117  * to store in the archive and maintain a map of original ino values to
118  * synthetic ones so we can preserve hardlink information.
119  *
120  * TODO: Make this more efficient.  It's not as bad as it looks (most
121  * files don't have any hardlinks and we don't do any work here for those),
122  * but it wouldn't be hard to do better.
123  *
124  * TODO: Work with dev/ino pairs here instead of just ino values.
125  */
126 static int
127 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
128 {
129 	int64_t ino = archive_entry_ino64(entry);
130 	int ino_new;
131 	size_t i;
132 
133 	/*
134 	 * If no index number was given, don't assign one.  In
135 	 * particular, this handles the end-of-archive marker
136 	 * correctly by giving it a zero index value.  (This is also
137 	 * why we start our synthetic index numbers with one below.)
138 	 */
139 	if (ino == 0)
140 		return (0);
141 
142 	/* Don't store a mapping if we don't need to. */
143 	if (archive_entry_nlink(entry) < 2) {
144 		return ++cpio->ino_next;
145 	}
146 
147 	/* Look up old ino; if we have it, this is a hardlink
148 	 * and we reuse the same value. */
149 	for (i = 0; i < cpio->ino_list_next; ++i) {
150 		if (cpio->ino_list[i].old == ino)
151 			return (cpio->ino_list[i].new);
152 	}
153 
154 	/* Assign a new index number. */
155 	ino_new = ++cpio->ino_next;
156 
157 	/* Ensure space for the new mapping. */
158 	if (cpio->ino_list_size <= cpio->ino_list_next) {
159 		size_t newsize = cpio->ino_list_size < 512
160 		    ? 512 : cpio->ino_list_size * 2;
161 		void *newlist = realloc(cpio->ino_list,
162 		    sizeof(cpio->ino_list[0]) * newsize);
163 		if (newlist == NULL)
164 			return (-1);
165 
166 		cpio->ino_list_size = newsize;
167 		cpio->ino_list = newlist;
168 	}
169 
170 	/* Record and return the new value. */
171 	cpio->ino_list[cpio->ino_list_next].old = ino;
172 	cpio->ino_list[cpio->ino_list_next].new = ino_new;
173 	++cpio->ino_list_next;
174 	return (ino_new);
175 }
176 
177 static int
178 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
179 {
180 	struct cpio *cpio;
181 	const char *p, *path;
182 	int pathlength, ret, ret2;
183 	int64_t	ino;
184 	struct cpio_header	 h;
185 
186 	cpio = (struct cpio *)a->format_data;
187 	ret2 = ARCHIVE_OK;
188 
189 	path = archive_entry_pathname(entry);
190 	pathlength = (int)strlen(path) + 1; /* Include trailing null. */
191 
192 	memset(&h, 0, sizeof(h));
193 	format_octal(070707, &h.c_magic, sizeof(h.c_magic));
194 	format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
195 
196 	ino = synthesize_ino_value(cpio, entry);
197 	if (ino < 0) {
198 		archive_set_error(&a->archive, ENOMEM,
199 		    "No memory for ino translation table");
200 		return (ARCHIVE_FATAL);
201 	} else if (ino > 0777777) {
202 		archive_set_error(&a->archive, ERANGE,
203 		    "Too many files for this cpio format");
204 		return (ARCHIVE_FATAL);
205 	}
206 	format_octal(ino & 0777777, &h.c_ino, sizeof(h.c_ino));
207 
208 	format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
209 	format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
210 	format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
211 	format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
212 	if (archive_entry_filetype(entry) == AE_IFBLK
213 	    || archive_entry_filetype(entry) == AE_IFCHR)
214 	    format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
215 	else
216 	    format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
217 	format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
218 	format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
219 
220 	/* Non-regular files don't store bodies. */
221 	if (archive_entry_filetype(entry) != AE_IFREG)
222 		archive_entry_set_size(entry, 0);
223 
224 	/* Symlinks get the link written as the body of the entry. */
225 	p = archive_entry_symlink(entry);
226 	if (p != NULL  &&  *p != '\0')
227 		format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
228 	else
229 		format_octal(archive_entry_size(entry),
230 		    &h.c_filesize, sizeof(h.c_filesize));
231 
232 	ret = (a->compressor.write)(a, &h, sizeof(h));
233 	if (ret != ARCHIVE_OK)
234 		return (ARCHIVE_FATAL);
235 
236 	ret = (a->compressor.write)(a, path, pathlength);
237 	if (ret != ARCHIVE_OK)
238 		return (ARCHIVE_FATAL);
239 
240 	cpio->entry_bytes_remaining = archive_entry_size(entry);
241 
242 	/* Write the symlink now. */
243 	if (p != NULL  &&  *p != '\0')
244 		ret = (a->compressor.write)(a, p, strlen(p));
245 
246 	if (ret == ARCHIVE_OK)
247 		ret = ret2;
248 	return (ret);
249 }
250 
251 static ssize_t
252 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
253 {
254 	struct cpio *cpio;
255 	int ret;
256 
257 	cpio = (struct cpio *)a->format_data;
258 	if (s > cpio->entry_bytes_remaining)
259 		s = cpio->entry_bytes_remaining;
260 
261 	ret = (a->compressor.write)(a, buff, s);
262 	cpio->entry_bytes_remaining -= s;
263 	if (ret >= 0)
264 		return (s);
265 	else
266 		return (ret);
267 }
268 
269 /*
270  * Format a number into the specified field.
271  */
272 static int
273 format_octal(int64_t v, void *p, int digits)
274 {
275 	int64_t	max;
276 	int	ret;
277 
278 	max = (((int64_t)1) << (digits * 3)) - 1;
279 	if (v >= 0  &&  v <= max) {
280 	    format_octal_recursive(v, (char *)p, digits);
281 	    ret = 0;
282 	} else {
283 	    format_octal_recursive(max, (char *)p, digits);
284 	    ret = -1;
285 	}
286 	return (ret);
287 }
288 
289 static int64_t
290 format_octal_recursive(int64_t v, char *p, int s)
291 {
292 	if (s == 0)
293 		return (v);
294 	v = format_octal_recursive(v, p+1, s-1);
295 	*p = '0' + (v & 7);
296 	return (v >> 3);
297 }
298 
299 static int
300 archive_write_cpio_finish(struct archive_write *a)
301 {
302 	int er;
303 	struct archive_entry *trailer;
304 
305 	trailer = archive_entry_new();
306 	/* nlink = 1 here for GNU cpio compat. */
307 	archive_entry_set_nlink(trailer, 1);
308 	archive_entry_set_pathname(trailer, "TRAILER!!!");
309 	er = archive_write_cpio_header(a, trailer);
310 	archive_entry_free(trailer);
311 	return (er);
312 }
313 
314 static int
315 archive_write_cpio_destroy(struct archive_write *a)
316 {
317 	struct cpio *cpio;
318 
319 	cpio = (struct cpio *)a->format_data;
320 	free(cpio->ino_list);
321 	free(cpio);
322 	a->format_data = NULL;
323 	return (ARCHIVE_OK);
324 }
325 
326 static int
327 archive_write_cpio_finish_entry(struct archive_write *a)
328 {
329 	struct cpio *cpio;
330 	size_t to_write;
331 	int ret;
332 
333 	cpio = (struct cpio *)a->format_data;
334 	ret = ARCHIVE_OK;
335 	while (cpio->entry_bytes_remaining > 0) {
336 		to_write = cpio->entry_bytes_remaining < a->null_length ?
337 		    cpio->entry_bytes_remaining : a->null_length;
338 		ret = (a->compressor.write)(a, a->nulls, to_write);
339 		if (ret != ARCHIVE_OK)
340 			return (ret);
341 		cpio->entry_bytes_remaining -= to_write;
342 	}
343 	return (ret);
344 }
345