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: src/lib/libarchive/archive_write_set_format_cpio.c,v 1.14 2008/03/15 11:04:45 kientzle Exp $");
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 
59 struct cpio_header {
60 	char	c_magic[6];
61 	char	c_dev[6];
62 	char	c_ino[6];
63 	char	c_mode[6];
64 	char	c_uid[6];
65 	char	c_gid[6];
66 	char	c_nlink[6];
67 	char	c_rdev[6];
68 	char	c_mtime[11];
69 	char	c_namesize[6];
70 	char	c_filesize[11];
71 };
72 
73 /*
74  * Set output format to 'cpio' format.
75  */
76 int
77 archive_write_set_format_cpio(struct archive *_a)
78 {
79 	struct archive_write *a = (struct archive_write *)_a;
80 	struct cpio *cpio;
81 
82 	/* If someone else was already registered, unregister them. */
83 	if (a->format_destroy != NULL)
84 		(a->format_destroy)(a);
85 
86 	cpio = (struct cpio *)malloc(sizeof(*cpio));
87 	if (cpio == NULL) {
88 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
89 		return (ARCHIVE_FATAL);
90 	}
91 	memset(cpio, 0, sizeof(*cpio));
92 	a->format_data = cpio;
93 
94 	a->pad_uncompressed = 1;
95 	a->format_name = "cpio";
96 	a->format_write_header = archive_write_cpio_header;
97 	a->format_write_data = archive_write_cpio_data;
98 	a->format_finish_entry = archive_write_cpio_finish_entry;
99 	a->format_finish = archive_write_cpio_finish;
100 	a->format_destroy = archive_write_cpio_destroy;
101 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
102 	a->archive.archive_format_name = "POSIX cpio";
103 	return (ARCHIVE_OK);
104 }
105 
106 static int
107 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
108 {
109 	struct cpio *cpio;
110 	const char *p, *path;
111 	int pathlength, ret;
112 	struct cpio_header	 h;
113 
114 	cpio = (struct cpio *)a->format_data;
115 	ret = 0;
116 
117 	path = archive_entry_pathname(entry);
118 	pathlength = strlen(path) + 1; /* Include trailing null. */
119 
120 	memset(&h, 0, sizeof(h));
121 	format_octal(070707, &h.c_magic, sizeof(h.c_magic));
122 	format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
123 	/*
124 	 * TODO: Generate artificial inode numbers rather than just
125 	 * re-using the ones off the disk.  That way, the 18-bit c_ino
126 	 * field only limits the number of files in the archive.
127 	 */
128 	if ((int)archive_entry_ino(entry) > 0777777) {
129 		archive_set_error(&a->archive, ERANGE,
130 		    "large inode number truncated");
131 		ret = ARCHIVE_WARN;
132 	}
133 
134 	format_octal(archive_entry_ino(entry) & 0777777, &h.c_ino, sizeof(h.c_ino));
135 	format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
136 	format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
137 	format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
138 	format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
139 	if (archive_entry_filetype(entry) == AE_IFBLK
140 	    || archive_entry_filetype(entry) == AE_IFCHR)
141 	    format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
142 	else
143 	    format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
144 	format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
145 	format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
146 
147 	/* Non-regular files don't store bodies. */
148 	if (archive_entry_filetype(entry) != AE_IFREG)
149 		archive_entry_set_size(entry, 0);
150 
151 	/* Symlinks get the link written as the body of the entry. */
152 	p = archive_entry_symlink(entry);
153 	if (p != NULL  &&  *p != '\0')
154 		format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
155 	else
156 		format_octal(archive_entry_size(entry),
157 		    &h.c_filesize, sizeof(h.c_filesize));
158 
159 	ret = (a->compressor.write)(a, &h, sizeof(h));
160 	if (ret != ARCHIVE_OK)
161 		return (ARCHIVE_FATAL);
162 
163 	ret = (a->compressor.write)(a, path, pathlength);
164 	if (ret != ARCHIVE_OK)
165 		return (ARCHIVE_FATAL);
166 
167 	cpio->entry_bytes_remaining = archive_entry_size(entry);
168 
169 	/* Write the symlink now. */
170 	if (p != NULL  &&  *p != '\0')
171 		ret = (a->compressor.write)(a, p, strlen(p));
172 
173 	return (ret);
174 }
175 
176 static ssize_t
177 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
178 {
179 	struct cpio *cpio;
180 	int ret;
181 
182 	cpio = (struct cpio *)a->format_data;
183 	if (s > cpio->entry_bytes_remaining)
184 		s = cpio->entry_bytes_remaining;
185 
186 	ret = (a->compressor.write)(a, buff, s);
187 	cpio->entry_bytes_remaining -= s;
188 	if (ret >= 0)
189 		return (s);
190 	else
191 		return (ret);
192 }
193 
194 /*
195  * Format a number into the specified field.
196  */
197 static int
198 format_octal(int64_t v, void *p, int digits)
199 {
200 	int64_t	max;
201 	int	ret;
202 
203 	max = (((int64_t)1) << (digits * 3)) - 1;
204 	if (v >= 0  &&  v <= max) {
205 	    format_octal_recursive(v, (char *)p, digits);
206 	    ret = 0;
207 	} else {
208 	    format_octal_recursive(max, (char *)p, digits);
209 	    ret = -1;
210 	}
211 	return (ret);
212 }
213 
214 static int64_t
215 format_octal_recursive(int64_t v, char *p, int s)
216 {
217 	if (s == 0)
218 		return (v);
219 	v = format_octal_recursive(v, p+1, s-1);
220 	*p = '0' + (v & 7);
221 	return (v >>= 3);
222 }
223 
224 static int
225 archive_write_cpio_finish(struct archive_write *a)
226 {
227 	struct cpio *cpio;
228 	int er;
229 	struct archive_entry *trailer;
230 
231 	cpio = (struct cpio *)a->format_data;
232 	trailer = archive_entry_new();
233 	/* nlink = 1 here for GNU cpio compat. */
234 	archive_entry_set_nlink(trailer, 1);
235 	archive_entry_set_pathname(trailer, "TRAILER!!!");
236 	er = archive_write_cpio_header(a, trailer);
237 	archive_entry_free(trailer);
238 	return (er);
239 }
240 
241 static int
242 archive_write_cpio_destroy(struct archive_write *a)
243 {
244 	struct cpio *cpio;
245 
246 	cpio = (struct cpio *)a->format_data;
247 	free(cpio);
248 	a->format_data = NULL;
249 	return (ARCHIVE_OK);
250 }
251 
252 static int
253 archive_write_cpio_finish_entry(struct archive_write *a)
254 {
255 	struct cpio *cpio;
256 	int to_write, ret;
257 
258 	cpio = (struct cpio *)a->format_data;
259 	ret = ARCHIVE_OK;
260 	while (cpio->entry_bytes_remaining > 0) {
261 		to_write = cpio->entry_bytes_remaining < a->null_length ?
262 		    cpio->entry_bytes_remaining : a->null_length;
263 		ret = (a->compressor.write)(a, a->nulls, to_write);
264 		if (ret != ARCHIVE_OK)
265 			return (ret);
266 		cpio->entry_bytes_remaining -= to_write;
267 	}
268 	return (ret);
269 }
270