archive_write_set_format_cpio.c revision 311042
134689Sbde/*-
250476Speter * Copyright (c) 2003-2007 Tim Kientzle
31573Srgrimes * Copyright (c) 2011-2012 Michihiro NAKAJIMA
4156813Sru * All rights reserved.
5156813Sru *
634689Sbde * Redistribution and use in source and binary forms, with or without
734689Sbde * modification, are permitted provided that the following conditions
834689Sbde * are met:
938752Sbde * 1. Redistributions of source code must retain the above copyright
10173017Sru *    notice, this list of conditions and the following disclaimer.
11228989Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12204738Simp *    notice, this list of conditions and the following disclaimer in the
13204738Simp *    documentation and/or other materials provided with the distribution.
1481133Stmm *
1559897Sjoe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1679471Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17166131Srafan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18122568Sharti * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1959353Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2041257Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2182355Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2294690Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2341257Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2456081Sbde * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2594690Sdes */
26181344Sdfr
2734689Sbde#include "archive_platform.h"
2834689Sbde__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/archive_write_set_format_cpio.c 311042 2017-01-02 01:43:11Z mm $");
29204738Simp
30204738Simp#ifdef HAVE_ERRNO_H
3134689Sbde#include <errno.h>
32205113Simp#endif
33205113Simp#include <stdio.h>
34205113Simp#ifdef HAVE_STDLIB_H
35205113Simp#include <stdlib.h>
36215127Sed#endif
37205113Simp#ifdef HAVE_STRING_H
38205113Simp#include <string.h>
39219019Sgabor#endif
40205113Simp
41205113Simp#include "archive.h"
42205113Simp#include "archive_entry.h"
43205113Simp#include "archive_entry_locale.h"
44205113Simp#include "archive_private.h"
45205113Simp#include "archive_write_private.h"
46205113Simp
47205113Simpstatic ssize_t	archive_write_cpio_data(struct archive_write *,
48205113Simp		    const void *buff, size_t s);
49205113Simpstatic int	archive_write_cpio_close(struct archive_write *);
50227987Sdimstatic int	archive_write_cpio_free(struct archive_write *);
51227987Sdimstatic int	archive_write_cpio_finish_entry(struct archive_write *);
52227987Sdimstatic int	archive_write_cpio_header(struct archive_write *,
53205113Simp		    struct archive_entry *);
54233337Sstasstatic int	archive_write_cpio_options(struct archive_write *,
55233337Sstas		    const char *, const char *);
56233337Sstasstatic int	format_octal(int64_t, void *, int);
57233337Sstasstatic int64_t	format_octal_recursive(int64_t, char *, int);
58205113Simpstatic int	write_header(struct archive_write *, struct archive_entry *);
59205113Simp
60205113Simpstruct cpio {
61205113Simp	uint64_t	  entry_bytes_remaining;
62205113Simp
63215127Sed	int64_t		  ino_next;
64205113Simp
65205113Simp	struct		 { int64_t old; int new;} *ino_list;
66205113Simp	size_t		  ino_list_size;
67205113Simp	size_t		  ino_list_next;
68205113Simp
69205113Simp	struct archive_string_conv *opt_sconv;
70205113Simp	struct archive_string_conv *sconv_default;
71205113Simp	int		  init_default_conversion;
72205113Simp};
73205113Simp
74205113Simp#define	c_magic_offset 0
75205113Simp#define	c_magic_size 6
76205113Simp#define	c_dev_offset 6
77205113Simp#define	c_dev_size 6
78205113Simp#define	c_ino_offset 12
79205113Simp#define	c_ino_size 6
80205113Simp#define	c_mode_offset 18
81205113Simp#define	c_mode_size 6
82205113Simp#define	c_uid_offset 24
83205113Simp#define	c_uid_size 6
84205113Simp#define	c_gid_offset 30
85205113Simp#define	c_gid_size 6
86207842Smm#define	c_nlink_offset 36
87205113Simp#define	c_nlink_size 6
88241774Suqs#define	c_rdev_offset 42
89205113Simp#define	c_rdev_size 6
90205113Simp#define	c_mtime_offset 48
91205113Simp#define	c_mtime_size 11
92235537Sgber#define	c_namesize_offset 59
93205113Simp#define	c_namesize_size 6
94205113Simp#define	c_filesize_offset 65
95205113Simp#define	c_filesize_size 11
96205113Simp
97205113Simp/*
98210682Srpaulo * Set output format to 'cpio' format.
99221807Sstas */
100205113Simpint
101210690Srpauloarchive_write_set_format_cpio(struct archive *_a)
102205113Simp{
103205113Simp	struct archive_write *a = (struct archive_write *)_a;
104205113Simp	struct cpio *cpio;
105205113Simp
106205113Simp	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
107234772Sjlh	    ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
108228904Sed
109205113Simp	/* If someone else was already registered, unregister them. */
110205113Simp	if (a->format_free != NULL)
111205113Simp		(a->format_free)(a);
112205113Simp
113205113Simp	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
114205113Simp	if (cpio == NULL) {
115205113Simp		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
116205113Simp		return (ARCHIVE_FATAL);
117205113Simp	}
118205113Simp	a->format_data = cpio;
119205113Simp	a->format_name = "cpio";
120205113Simp	a->format_options = archive_write_cpio_options;
121208964Srdivacky	a->format_write_header = archive_write_cpio_header;
122208964Srdivacky	a->format_write_data = archive_write_cpio_data;
12334689Sbde	a->format_finish_entry = archive_write_cpio_finish_entry;
124211759Simp	a->format_close = archive_write_cpio_close;
125211759Simp	a->format_free = archive_write_cpio_free;
126211934Snwhitehorn	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
127211934Snwhitehorn	a->archive.archive_format_name = "POSIX cpio";
128211725Simp	return (ARCHIVE_OK);
129211725Simp}
13072309Sobrien
13172309Sobrienstatic int
1321573Srgrimesarchive_write_cpio_options(struct archive_write *a, const char *key,
1331573Srgrimes    const char *val)
134183242Ssam{
135183242Ssam	struct cpio *cpio = (struct cpio *)a->format_data;
136156813Sru	int ret = ARCHIVE_FAILED;
137121340Sharti
138119508Sphk	if (strcmp(key, "hdrcharset")  == 0) {
139119508Sphk		if (val == NULL || val[0] == 0)
140156813Sru			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
141135549Sdes			    "%s: hdrcharset option needs a character-set name",
14253922Speter			    a->format_name);
14353922Speter		else {
144156813Sru			cpio->opt_sconv = archive_string_conversion_to_charset(
145125123Semax			    &a->archive, val, 0);
146125123Semax			if (cpio->opt_sconv != NULL)
147125123Semax				ret = ARCHIVE_OK;
148131768Semax			else
149183242Ssam				ret = ARCHIVE_FATAL;
150183242Ssam		}
151183242Ssam		return (ret);
152183242Ssam	}
153209400Sed
154208964Srdivacky	/* Note: The "warn" return is just to inform the options
155208964Srdivacky	 * supervisor that we didn't handle it.  It will generate
156208964Srdivacky	 * a suitable error if no one used this option. */
157183242Ssam	return (ARCHIVE_WARN);
158183242Ssam}
159183242Ssam
160183242Ssam/*
161174548Sru * Ino values are as long as 64 bits on some systems; cpio format
162174519Sdougb * only allows 18 bits and relies on the ino values to identify hardlinked
163181344Sdfr * files.  So, we can't merely "hash" the ino numbers since collisions
164174519Sdougb * would corrupt the archive.  Instead, we generate synthetic ino values
165174519Sdougb * to store in the archive and maintain a map of original ino values to
166219019Sgabor * synthetic ones so we can preserve hardlink information.
167219019Sgabor *
168219019Sgabor * TODO: Make this more efficient.  It's not as bad as it looks (most
169219019Sgabor * files don't have any hardlinks and we don't do any work here for those),
170156905Sru * but it wouldn't be hard to do better.
171156905Sru *
172156905Sru * TODO: Work with dev/ino pairs here instead of just ino values.
173156905Sru */
174183242Ssamstatic int
175183242Ssamsynthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
176183242Ssam{
177183242Ssam	int64_t ino = archive_entry_ino64(entry);
178235537Sgber	int ino_new;
179235537Sgber	size_t i;
180235537Sgber
181235537Sgber	/*
182183242Ssam	 * If no index number was given, don't assign one.  In
183183242Ssam	 * particular, this handles the end-of-archive marker
184183242Ssam	 * correctly by giving it a zero index value.  (This is also
185183242Ssam	 * why we start our synthetic index numbers with one below.)
186183242Ssam	 */
187183242Ssam	if (ino == 0)
188183242Ssam		return (0);
189183242Ssam
190211725Simp	/* Don't store a mapping if we don't need to. */
19134689Sbde	if (archive_entry_nlink(entry) < 2) {
192210682Srpaulo		return (int)(++cpio->ino_next);
193210690Srpaulo	}
19436026Sjb
19534689Sbde	/* Look up old ino; if we have it, this is a hardlink
196211725Simp	 * and we reuse the same value. */
197203181Smarcel	for (i = 0; i < cpio->ino_list_next; ++i) {
198161524Smarcel		if (cpio->ino_list[i].old == ino)
199161524Smarcel			return (cpio->ino_list[i].new);
200233413Sgonzo	}
201233413Sgonzo
202233413Sgonzo	/* Assign a new index number. */
203233413Sgonzo	ino_new = (int)(++cpio->ino_next);
204233413Sgonzo
205183242Ssam	/* Ensure space for the new mapping. */
206183242Ssam	if (cpio->ino_list_size <= cpio->ino_list_next) {
207117797Smtm		size_t newsize = cpio->ino_list_size < 512
208117797Smtm		    ? 512 : cpio->ino_list_size * 2;
209227987Sdim		void *newlist = realloc(cpio->ino_list,
210227987Sdim		    sizeof(cpio->ino_list[0]) * newsize);
211227987Sdim		if (newlist == NULL)
212227987Sdim			return (-1);
213227987Sdim
214183242Ssam		cpio->ino_list_size = newsize;
215183242Ssam		cpio->ino_list = newlist;
216129225Scognet	}
217129225Scognet
218183242Ssam	/* Record and return the new value. */
219183242Ssam	cpio->ino_list[cpio->ino_list_next].old = ino;
220183242Ssam	cpio->ino_list[cpio->ino_list_next].new = ino_new;
221183242Ssam	++cpio->ino_list_next;
222183242Ssam	return (ino_new);
223126799Sphk}
224126799Sphk
225183242Ssam
226183242Ssamstatic struct archive_string_conv *
227141403Sphkget_sconv(struct archive_write *a)
228141403Sphk{
229183242Ssam	struct cpio *cpio;
230183242Ssam	struct archive_string_conv *sconv;
231189589Sthompsa
232183242Ssam	cpio = (struct cpio *)a->format_data;
233183242Ssam	sconv = cpio->opt_sconv;
2341573Srgrimes	if (sconv == NULL) {
235		if (!cpio->init_default_conversion) {
236			cpio->sconv_default =
237			    archive_string_default_conversion_for_write(
238			      &(a->archive));
239			cpio->init_default_conversion = 1;
240		}
241		sconv = cpio->sconv_default;
242	}
243	return (sconv);
244}
245
246static int
247archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
248{
249	const char *path;
250	size_t len;
251
252	if (archive_entry_filetype(entry) == 0) {
253		archive_set_error(&a->archive, -1, "Filetype required");
254		return (ARCHIVE_FAILED);
255	}
256
257	if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
258	    && errno == ENOMEM) {
259		archive_set_error(&a->archive, ENOMEM,
260		    "Can't allocate memory for Pathname");
261		return (ARCHIVE_FATAL);
262	}
263	if (len == 0 || path == NULL || path[0] == '\0') {
264		archive_set_error(&a->archive, -1, "Pathname required");
265		return (ARCHIVE_FAILED);
266	}
267
268	if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
269		archive_set_error(&a->archive, -1, "Size required");
270		return (ARCHIVE_FAILED);
271	}
272	return write_header(a, entry);
273}
274
275static int
276write_header(struct archive_write *a, struct archive_entry *entry)
277{
278	struct cpio *cpio;
279	const char *p, *path;
280	int pathlength, ret, ret_final;
281	int64_t	ino;
282	char h[76];
283	struct archive_string_conv *sconv;
284	struct archive_entry *entry_main;
285	size_t len;
286
287	cpio = (struct cpio *)a->format_data;
288	ret_final = ARCHIVE_OK;
289	sconv = get_sconv(a);
290
291#if defined(_WIN32) && !defined(__CYGWIN__)
292	/* Make sure the path separators in pathname, hardlink and symlink
293	 * are all slash '/', not the Windows path separator '\'. */
294	entry_main = __la_win_entry_in_posix_pathseparator(entry);
295	if (entry_main == NULL) {
296		archive_set_error(&a->archive, ENOMEM,
297		    "Can't allocate ustar data");
298		return(ARCHIVE_FATAL);
299	}
300	if (entry != entry_main)
301		entry = entry_main;
302	else
303		entry_main = NULL;
304#else
305	entry_main = NULL;
306#endif
307
308	ret = archive_entry_pathname_l(entry, &path, &len, sconv);
309	if (ret != 0) {
310		if (errno == ENOMEM) {
311			archive_set_error(&a->archive, ENOMEM,
312			    "Can't allocate memory for Pathname");
313			ret_final = ARCHIVE_FATAL;
314			goto exit_write_header;
315		}
316		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
317		    "Can't translate pathname '%s' to %s",
318		    archive_entry_pathname(entry),
319		    archive_string_conversion_charset_name(sconv));
320		ret_final = ARCHIVE_WARN;
321	}
322	/* Include trailing null. */
323	pathlength = (int)len + 1;
324
325	memset(h, 0, sizeof(h));
326	format_octal(070707, h + c_magic_offset, c_magic_size);
327	format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
328
329	ino = synthesize_ino_value(cpio, entry);
330	if (ino < 0) {
331		archive_set_error(&a->archive, ENOMEM,
332		    "No memory for ino translation table");
333		ret_final = ARCHIVE_FATAL;
334		goto exit_write_header;
335	} else if (ino > 0777777) {
336		archive_set_error(&a->archive, ERANGE,
337		    "Too many files for this cpio format");
338		ret_final = ARCHIVE_FATAL;
339		goto exit_write_header;
340	}
341	format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
342
343	/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
344	format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
345	format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
346	format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
347	format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
348	if (archive_entry_filetype(entry) == AE_IFBLK
349	    || archive_entry_filetype(entry) == AE_IFCHR)
350	    format_octal(archive_entry_dev(entry), h + c_rdev_offset, c_rdev_size);
351	else
352	    format_octal(0, h + c_rdev_offset, c_rdev_size);
353	format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
354	format_octal(pathlength, h + c_namesize_offset, c_namesize_size);
355
356	/* Non-regular files don't store bodies. */
357	if (archive_entry_filetype(entry) != AE_IFREG)
358		archive_entry_set_size(entry, 0);
359
360	/* Symlinks get the link written as the body of the entry. */
361	ret = archive_entry_symlink_l(entry, &p, &len, sconv);
362	if (ret != 0) {
363		if (errno == ENOMEM) {
364			archive_set_error(&a->archive, ENOMEM,
365			    "Can't allocate memory for Linkname");
366			ret_final = ARCHIVE_FATAL;
367			goto exit_write_header;
368		}
369		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
370		    "Can't translate linkname '%s' to %s",
371		    archive_entry_symlink(entry),
372		    archive_string_conversion_charset_name(sconv));
373		ret_final = ARCHIVE_WARN;
374	}
375	if (len > 0 && p != NULL  &&  *p != '\0')
376		ret = format_octal(strlen(p), h + c_filesize_offset,
377		    c_filesize_size);
378	else
379		ret = format_octal(archive_entry_size(entry),
380		    h + c_filesize_offset, c_filesize_size);
381	if (ret) {
382		archive_set_error(&a->archive, ERANGE,
383		    "File is too large for cpio format.");
384		ret_final = ARCHIVE_FAILED;
385		goto exit_write_header;
386	}
387
388	ret = __archive_write_output(a, h, sizeof(h));
389	if (ret != ARCHIVE_OK) {
390		ret_final = ARCHIVE_FATAL;
391		goto exit_write_header;
392	}
393
394	ret = __archive_write_output(a, path, pathlength);
395	if (ret != ARCHIVE_OK) {
396		ret_final = ARCHIVE_FATAL;
397		goto exit_write_header;
398	}
399
400	cpio->entry_bytes_remaining = archive_entry_size(entry);
401
402	/* Write the symlink now. */
403	if (p != NULL  &&  *p != '\0') {
404		ret = __archive_write_output(a, p, strlen(p));
405		if (ret != ARCHIVE_OK) {
406			ret_final = ARCHIVE_FATAL;
407			goto exit_write_header;
408		}
409	}
410exit_write_header:
411	if (entry_main)
412		archive_entry_free(entry_main);
413	return (ret_final);
414}
415
416static ssize_t
417archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
418{
419	struct cpio *cpio;
420	int ret;
421
422	cpio = (struct cpio *)a->format_data;
423	if (s > cpio->entry_bytes_remaining)
424		s = (size_t)cpio->entry_bytes_remaining;
425
426	ret = __archive_write_output(a, buff, s);
427	cpio->entry_bytes_remaining -= s;
428	if (ret >= 0)
429		return (s);
430	else
431		return (ret);
432}
433
434/*
435 * Format a number into the specified field.
436 */
437static int
438format_octal(int64_t v, void *p, int digits)
439{
440	int64_t	max;
441	int	ret;
442
443	max = (((int64_t)1) << (digits * 3)) - 1;
444	if (v >= 0  &&  v <= max) {
445	    format_octal_recursive(v, (char *)p, digits);
446	    ret = 0;
447	} else {
448	    format_octal_recursive(max, (char *)p, digits);
449	    ret = -1;
450	}
451	return (ret);
452}
453
454static int64_t
455format_octal_recursive(int64_t v, char *p, int s)
456{
457	if (s == 0)
458		return (v);
459	v = format_octal_recursive(v, p+1, s-1);
460	*p = '0' + ((char)v & 7);
461	return (v >> 3);
462}
463
464static int
465archive_write_cpio_close(struct archive_write *a)
466{
467	int er;
468	struct archive_entry *trailer;
469
470	trailer = archive_entry_new2(NULL);
471	/* nlink = 1 here for GNU cpio compat. */
472	archive_entry_set_nlink(trailer, 1);
473	archive_entry_set_size(trailer, 0);
474	archive_entry_set_pathname(trailer, "TRAILER!!!");
475	er = write_header(a, trailer);
476	archive_entry_free(trailer);
477	return (er);
478}
479
480static int
481archive_write_cpio_free(struct archive_write *a)
482{
483	struct cpio *cpio;
484
485	cpio = (struct cpio *)a->format_data;
486	free(cpio->ino_list);
487	free(cpio);
488	a->format_data = NULL;
489	return (ARCHIVE_OK);
490}
491
492static int
493archive_write_cpio_finish_entry(struct archive_write *a)
494{
495	struct cpio *cpio;
496
497	cpio = (struct cpio *)a->format_data;
498	return (__archive_write_nulls(a,
499		(size_t)cpio->entry_bytes_remaining));
500}
501