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_entry_locale.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
45 
46 static ssize_t	archive_write_cpio_data(struct archive_write *,
47 		    const void *buff, size_t s);
48 static int	archive_write_cpio_close(struct archive_write *);
49 static int	archive_write_cpio_free(struct archive_write *);
50 static int	archive_write_cpio_finish_entry(struct archive_write *);
51 static int	archive_write_cpio_header(struct archive_write *,
52 		    struct archive_entry *);
53 static int	archive_write_cpio_options(struct archive_write *,
54 		    const char *, const char *);
55 static int	format_octal(int64_t, void *, int);
56 static int64_t	format_octal_recursive(int64_t, char *, int);
57 static int	write_header(struct archive_write *, struct archive_entry *);
58 
59 struct cpio {
60 	uint64_t	  entry_bytes_remaining;
61 
62 	int64_t		  ino_next;
63 
64 	struct		 { int64_t old; int new;} *ino_list;
65 	size_t		  ino_list_size;
66 	size_t		  ino_list_next;
67 
68 	struct archive_string_conv *opt_sconv;
69 	struct archive_string_conv *sconv_default;
70 	int		  init_default_conversion;
71 };
72 
73 #define	c_magic_offset 0
74 #define	c_magic_size 6
75 #define	c_dev_offset 6
76 #define	c_dev_size 6
77 #define	c_ino_offset 12
78 #define	c_ino_size 6
79 #define	c_mode_offset 18
80 #define	c_mode_size 6
81 #define	c_uid_offset 24
82 #define	c_uid_size 6
83 #define	c_gid_offset 30
84 #define	c_gid_size 6
85 #define	c_nlink_offset 36
86 #define	c_nlink_size 6
87 #define	c_rdev_offset 42
88 #define	c_rdev_size 6
89 #define	c_mtime_offset 48
90 #define	c_mtime_size 11
91 #define	c_namesize_offset 59
92 #define	c_namesize_size 6
93 #define	c_filesize_offset 65
94 #define	c_filesize_size 11
95 
96 /*
97  * Set output format to 'cpio' format.
98  */
99 int
100 archive_write_set_format_cpio(struct archive *_a)
101 {
102 	struct archive_write *a = (struct archive_write *)_a;
103 	struct cpio *cpio;
104 
105 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
106 	    ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
107 
108 	/* If someone else was already registered, unregister them. */
109 	if (a->format_free != NULL)
110 		(a->format_free)(a);
111 
112 	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
113 	if (cpio == NULL) {
114 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
115 		return (ARCHIVE_FATAL);
116 	}
117 	a->format_data = cpio;
118 	a->format_name = "cpio";
119 	a->format_options = archive_write_cpio_options;
120 	a->format_write_header = archive_write_cpio_header;
121 	a->format_write_data = archive_write_cpio_data;
122 	a->format_finish_entry = archive_write_cpio_finish_entry;
123 	a->format_close = archive_write_cpio_close;
124 	a->format_free = archive_write_cpio_free;
125 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
126 	a->archive.archive_format_name = "POSIX cpio";
127 	return (ARCHIVE_OK);
128 }
129 
130 static int
131 archive_write_cpio_options(struct archive_write *a, const char *key,
132     const char *val)
133 {
134 	struct cpio *cpio = (struct cpio *)a->format_data;
135 	int ret = ARCHIVE_FAILED;
136 
137 	if (strcmp(key, "hdrcharset")  == 0) {
138 		if (val == NULL || val[0] == 0)
139 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
140 			    "%s: hdrcharset option needs a character-set name",
141 			    a->format_name);
142 		else {
143 			cpio->opt_sconv = archive_string_conversion_to_charset(
144 			    &a->archive, val, 0);
145 			if (cpio->opt_sconv != NULL)
146 				ret = ARCHIVE_OK;
147 			else
148 				ret = ARCHIVE_FATAL;
149 		}
150 	} else
151 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
152 		    "%s: unknown keyword ``%s''", a->format_name, key);
153 
154 	return (ret);
155 }
156 
157 /*
158  * Ino values are as long as 64 bits on some systems; cpio format
159  * only allows 18 bits and relies on the ino values to identify hardlinked
160  * files.  So, we can't merely "hash" the ino numbers since collisions
161  * would corrupt the archive.  Instead, we generate synthetic ino values
162  * to store in the archive and maintain a map of original ino values to
163  * synthetic ones so we can preserve hardlink information.
164  *
165  * TODO: Make this more efficient.  It's not as bad as it looks (most
166  * files don't have any hardlinks and we don't do any work here for those),
167  * but it wouldn't be hard to do better.
168  *
169  * TODO: Work with dev/ino pairs here instead of just ino values.
170  */
171 static int
172 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
173 {
174 	int64_t ino = archive_entry_ino64(entry);
175 	int ino_new;
176 	size_t i;
177 
178 	/*
179 	 * If no index number was given, don't assign one.  In
180 	 * particular, this handles the end-of-archive marker
181 	 * correctly by giving it a zero index value.  (This is also
182 	 * why we start our synthetic index numbers with one below.)
183 	 */
184 	if (ino == 0)
185 		return (0);
186 
187 	/* Don't store a mapping if we don't need to. */
188 	if (archive_entry_nlink(entry) < 2) {
189 		return ++cpio->ino_next;
190 	}
191 
192 	/* Look up old ino; if we have it, this is a hardlink
193 	 * and we reuse the same value. */
194 	for (i = 0; i < cpio->ino_list_next; ++i) {
195 		if (cpio->ino_list[i].old == ino)
196 			return (cpio->ino_list[i].new);
197 	}
198 
199 	/* Assign a new index number. */
200 	ino_new = ++cpio->ino_next;
201 
202 	/* Ensure space for the new mapping. */
203 	if (cpio->ino_list_size <= cpio->ino_list_next) {
204 		size_t newsize = cpio->ino_list_size < 512
205 		    ? 512 : cpio->ino_list_size * 2;
206 		void *newlist = realloc(cpio->ino_list,
207 		    sizeof(cpio->ino_list[0]) * newsize);
208 		if (newlist == NULL)
209 			return (-1);
210 
211 		cpio->ino_list_size = newsize;
212 		cpio->ino_list = newlist;
213 	}
214 
215 	/* Record and return the new value. */
216 	cpio->ino_list[cpio->ino_list_next].old = ino;
217 	cpio->ino_list[cpio->ino_list_next].new = ino_new;
218 	++cpio->ino_list_next;
219 	return (ino_new);
220 }
221 
222 
223 static struct archive_string_conv *
224 get_sconv(struct archive_write *a)
225 {
226 	struct cpio *cpio;
227 	struct archive_string_conv *sconv;
228 
229 	cpio = (struct cpio *)a->format_data;
230 	sconv = cpio->opt_sconv;
231 	if (sconv == NULL) {
232 		if (!cpio->init_default_conversion) {
233 			cpio->sconv_default =
234 			    archive_string_default_conversion_for_write(
235 			      &(a->archive));
236 			cpio->init_default_conversion = 1;
237 		}
238 		sconv = cpio->sconv_default;
239 	}
240 	return (sconv);
241 }
242 
243 static int
244 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
245 {
246 	const char *path;
247 	size_t len;
248 
249 	if (archive_entry_filetype(entry) == 0) {
250 		archive_set_error(&a->archive, -1, "Filetype required");
251 		return (ARCHIVE_FAILED);
252 	}
253 
254 	if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
255 	    && errno == ENOMEM) {
256 		archive_set_error(&a->archive, ENOMEM,
257 		    "Can't allocate memory for Pathname");
258 		return (ARCHIVE_FATAL);
259 	}
260 	if (len == 0 || path == NULL || path[0] == '\0') {
261 		archive_set_error(&a->archive, -1, "Pathname required");
262 		return (ARCHIVE_FAILED);
263 	}
264 
265 	if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
266 		archive_set_error(&a->archive, -1, "Size required");
267 		return (ARCHIVE_FAILED);
268 	}
269 	return write_header(a, entry);
270 }
271 
272 static int
273 write_header(struct archive_write *a, struct archive_entry *entry)
274 {
275 	struct cpio *cpio;
276 	const char *p, *path;
277 	int pathlength, ret, ret_final;
278 	int64_t	ino;
279 	char h[76];
280 	struct archive_string_conv *sconv;
281 	size_t len;
282 
283 	cpio = (struct cpio *)a->format_data;
284 	ret_final = ARCHIVE_OK;
285 	sconv = get_sconv(a);
286 
287 	ret = archive_entry_pathname_l(entry, &path, &len, sconv);
288 	if (ret != 0) {
289 		if (errno == ENOMEM) {
290 			archive_set_error(&a->archive, ENOMEM,
291 			    "Can't allocate memory for Pathname");
292 			return (ARCHIVE_FATAL);
293 		}
294 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
295 		    "Can't translate pathname '%s' to %s",
296 		    archive_entry_pathname(entry),
297 		    archive_string_conversion_charset_name(sconv));
298 		ret_final = ARCHIVE_WARN;
299 	}
300 	/* Include trailing null. */
301 	pathlength = (int)len + 1;
302 
303 	memset(h, 0, sizeof(h));
304 	format_octal(070707, h + c_magic_offset, c_magic_size);
305 	format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
306 
307 	ino = synthesize_ino_value(cpio, entry);
308 	if (ino < 0) {
309 		archive_set_error(&a->archive, ENOMEM,
310 		    "No memory for ino translation table");
311 		return (ARCHIVE_FATAL);
312 	} else if (ino > 0777777) {
313 		archive_set_error(&a->archive, ERANGE,
314 		    "Too many files for this cpio format");
315 		return (ARCHIVE_FATAL);
316 	}
317 	format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
318 
319 	/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
320 	format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
321 	format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
322 	format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
323 	format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
324 	if (archive_entry_filetype(entry) == AE_IFBLK
325 	    || archive_entry_filetype(entry) == AE_IFCHR)
326 	    format_octal(archive_entry_dev(entry), h + c_rdev_offset, c_rdev_size);
327 	else
328 	    format_octal(0, h + c_rdev_offset, c_rdev_size);
329 	format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
330 	format_octal(pathlength, h + c_namesize_offset, c_namesize_size);
331 
332 	/* Non-regular files don't store bodies. */
333 	if (archive_entry_filetype(entry) != AE_IFREG)
334 		archive_entry_set_size(entry, 0);
335 
336 	/* Symlinks get the link written as the body of the entry. */
337 	ret = archive_entry_symlink_l(entry, &p, &len, sconv);
338 	if (ret != 0) {
339 		if (errno == ENOMEM) {
340 			archive_set_error(&a->archive, ENOMEM,
341 			    "Can't allocate memory for Linkname");
342 			return (ARCHIVE_FATAL);
343 		}
344 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
345 		    "Can't translate linkname '%s' to %s",
346 		    archive_entry_symlink(entry),
347 		    archive_string_conversion_charset_name(sconv));
348 		ret_final = ARCHIVE_WARN;
349 	}
350 	if (len > 0 && p != NULL  &&  *p != '\0')
351 		ret = format_octal(strlen(p), h + c_filesize_offset,
352 		    c_filesize_size);
353 	else
354 		ret = format_octal(archive_entry_size(entry),
355 		    h + c_filesize_offset, c_filesize_size);
356 	if (ret) {
357 		archive_set_error(&a->archive, ERANGE,
358 		    "File is too large for cpio format.");
359 		return (ARCHIVE_FAILED);
360 	}
361 
362 	ret = __archive_write_output(a, h, sizeof(h));
363 	if (ret != ARCHIVE_OK)
364 		return (ARCHIVE_FATAL);
365 
366 	ret = __archive_write_output(a, path, pathlength);
367 	if (ret != ARCHIVE_OK)
368 		return (ARCHIVE_FATAL);
369 
370 	cpio->entry_bytes_remaining = archive_entry_size(entry);
371 
372 	/* Write the symlink now. */
373 	if (p != NULL  &&  *p != '\0') {
374 		ret = __archive_write_output(a, p, strlen(p));
375 		if (ret != ARCHIVE_OK)
376 			return (ARCHIVE_FATAL);
377 	}
378 	return (ret_final);
379 }
380 
381 static ssize_t
382 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
383 {
384 	struct cpio *cpio;
385 	int ret;
386 
387 	cpio = (struct cpio *)a->format_data;
388 	if (s > cpio->entry_bytes_remaining)
389 		s = cpio->entry_bytes_remaining;
390 
391 	ret = __archive_write_output(a, buff, s);
392 	cpio->entry_bytes_remaining -= s;
393 	if (ret >= 0)
394 		return (s);
395 	else
396 		return (ret);
397 }
398 
399 /*
400  * Format a number into the specified field.
401  */
402 static int
403 format_octal(int64_t v, void *p, int digits)
404 {
405 	int64_t	max;
406 	int	ret;
407 
408 	max = (((int64_t)1) << (digits * 3)) - 1;
409 	if (v >= 0  &&  v <= max) {
410 	    format_octal_recursive(v, (char *)p, digits);
411 	    ret = 0;
412 	} else {
413 	    format_octal_recursive(max, (char *)p, digits);
414 	    ret = -1;
415 	}
416 	return (ret);
417 }
418 
419 static int64_t
420 format_octal_recursive(int64_t v, char *p, int s)
421 {
422 	if (s == 0)
423 		return (v);
424 	v = format_octal_recursive(v, p+1, s-1);
425 	*p = '0' + (v & 7);
426 	return (v >> 3);
427 }
428 
429 static int
430 archive_write_cpio_close(struct archive_write *a)
431 {
432 	int er;
433 	struct archive_entry *trailer;
434 
435 	trailer = archive_entry_new2(NULL);
436 	/* nlink = 1 here for GNU cpio compat. */
437 	archive_entry_set_nlink(trailer, 1);
438 	archive_entry_set_size(trailer, 0);
439 	archive_entry_set_pathname(trailer, "TRAILER!!!");
440 	er = write_header(a, trailer);
441 	archive_entry_free(trailer);
442 	return (er);
443 }
444 
445 static int
446 archive_write_cpio_free(struct archive_write *a)
447 {
448 	struct cpio *cpio;
449 
450 	cpio = (struct cpio *)a->format_data;
451 	free(cpio->ino_list);
452 	free(cpio);
453 	a->format_data = NULL;
454 	return (ARCHIVE_OK);
455 }
456 
457 static int
458 archive_write_cpio_finish_entry(struct archive_write *a)
459 {
460 	struct cpio *cpio;
461 
462 	cpio = (struct cpio *)a->format_data;
463 	return (__archive_write_nulls(a, cpio->entry_bytes_remaining));
464 }
465