2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2006 Rudolf Marek SYSGO s.r.o.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio_newc.c 201160 2009-12-29 05:41:57Z kientzle $");
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_write_private.h"
47 static ssize_t archive_write_newc_data(struct archive_write *,
48 const void *buff, size_t s);
49 static int archive_write_newc_close(struct archive_write *);
50 static int archive_write_newc_free(struct archive_write *);
51 static int archive_write_newc_finish_entry(struct archive_write *);
52 static int archive_write_newc_header(struct archive_write *,
53 struct archive_entry *);
54 static int archive_write_newc_options(struct archive_write *,
55 const char *, const char *);
56 static int format_hex(int64_t, void *, int);
57 static int64_t format_hex_recursive(int64_t, char *, int);
58 static int write_header(struct archive_write *, struct archive_entry *);
61 uint64_t entry_bytes_remaining;
64 struct archive_string_conv *opt_sconv;
65 struct archive_string_conv *sconv_default;
66 int init_default_conversion;
69 #define c_magic_offset 0
70 #define c_magic_size 6
71 #define c_ino_offset 6
73 #define c_mode_offset 14
75 #define c_uid_offset 22
77 #define c_gid_offset 30
79 #define c_nlink_offset 38
80 #define c_nlink_size 8
81 #define c_mtime_offset 46
82 #define c_mtime_size 8
83 #define c_filesize_offset 54
84 #define c_filesize_size 8
85 #define c_devmajor_offset 62
86 #define c_devmajor_size 8
87 #define c_devminor_offset 70
88 #define c_devminor_size 8
89 #define c_rdevmajor_offset 78
90 #define c_rdevmajor_size 8
91 #define c_rdevminor_offset 86
92 #define c_rdevminor_size 8
93 #define c_namesize_offset 94
94 #define c_namesize_size 8
95 #define c_checksum_offset 102
96 #define c_checksum_size 8
97 #define c_header_size 110
99 /* Logic trick: difference between 'n' and next multiple of 4 */
100 #define PAD4(n) (3 & (1 + ~(n)))
103 * Set output format to 'cpio' format.
106 archive_write_set_format_cpio_newc(struct archive *_a)
108 struct archive_write *a = (struct archive_write *)_a;
111 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
112 ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_newc");
114 /* If someone else was already registered, unregister them. */
115 if (a->format_free != NULL)
118 cpio = (struct cpio *)malloc(sizeof(*cpio));
120 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
121 return (ARCHIVE_FATAL);
123 memset(cpio, 0, sizeof(*cpio));
124 a->format_data = cpio;
125 a->format_name = "cpio";
126 a->format_options = archive_write_newc_options;
127 a->format_write_header = archive_write_newc_header;
128 a->format_write_data = archive_write_newc_data;
129 a->format_finish_entry = archive_write_newc_finish_entry;
130 a->format_close = archive_write_newc_close;
131 a->format_free = archive_write_newc_free;
132 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
133 a->archive.archive_format_name = "SVR4 cpio nocrc";
138 archive_write_newc_options(struct archive_write *a, const char *key,
141 struct cpio *cpio = (struct cpio *)a->format_data;
142 int ret = ARCHIVE_FAILED;
144 if (strcmp(key, "hdrcharset") == 0) {
145 if (val == NULL || val[0] == 0)
146 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
147 "%s: hdrcharset option needs a character-set name",
150 cpio->opt_sconv = archive_string_conversion_to_charset(
151 &a->archive, val, 0);
152 if (cpio->opt_sconv != NULL)
158 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
159 "%s: unknown keyword ``%s''", a->format_name, key);
164 static struct archive_string_conv *
165 get_sconv(struct archive_write *a)
168 struct archive_string_conv *sconv;
170 cpio = (struct cpio *)a->format_data;
171 sconv = cpio->opt_sconv;
173 if (!cpio->init_default_conversion) {
174 cpio->sconv_default =
175 archive_string_default_conversion_for_write(
177 cpio->init_default_conversion = 1;
179 sconv = cpio->sconv_default;
185 archive_write_newc_header(struct archive_write *a, struct archive_entry *entry)
190 if (archive_entry_filetype(entry) == 0) {
191 archive_set_error(&a->archive, -1, "Filetype required");
192 return (ARCHIVE_FAILED);
195 if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
196 && errno == ENOMEM) {
197 archive_set_error(&a->archive, ENOMEM,
198 "Can't allocate memory for Pathname");
199 return (ARCHIVE_FATAL);
201 if (len == 0 || path == NULL || path[0] == '\0') {
202 archive_set_error(&a->archive, -1, "Pathname required");
203 return (ARCHIVE_FAILED);
206 if (archive_entry_hardlink(entry) == NULL
207 && (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0)) {
208 archive_set_error(&a->archive, -1, "Size required");
209 return (ARCHIVE_FAILED);
211 return write_header(a, entry);
215 write_header(struct archive_write *a, struct archive_entry *entry)
219 const char *p, *path;
220 int pathlength, ret, ret_final;
221 char h[c_header_size];
222 struct archive_string_conv *sconv;
226 cpio = (struct cpio *)a->format_data;
227 ret_final = ARCHIVE_OK;
228 sconv = get_sconv(a);
230 ret = archive_entry_pathname_l(entry, &path, &len, sconv);
232 if (errno == ENOMEM) {
233 archive_set_error(&a->archive, ENOMEM,
234 "Can't allocate memory for Pathname");
235 return (ARCHIVE_FATAL);
237 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
238 "Can't translate pathname '%s' to %s",
239 archive_entry_pathname(entry),
240 archive_string_conversion_charset_name(sconv));
241 ret_final = ARCHIVE_WARN;
243 pathlength = (int)len + 1; /* Include trailing null. */
245 memset(h, 0, c_header_size);
246 format_hex(0x070701, h + c_magic_offset, c_magic_size);
247 format_hex(archive_entry_devmajor(entry), h + c_devmajor_offset,
249 format_hex(archive_entry_devminor(entry), h + c_devminor_offset,
252 ino = archive_entry_ino64(entry);
253 if (ino > 0xffffffff) {
254 archive_set_error(&a->archive, ERANGE,
255 "large inode number truncated");
256 ret_final = ARCHIVE_WARN;
259 /* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
260 format_hex(ino & 0xffffffff, h + c_ino_offset, c_ino_size);
261 format_hex(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
262 format_hex(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
263 format_hex(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
264 format_hex(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
265 if (archive_entry_filetype(entry) == AE_IFBLK
266 || archive_entry_filetype(entry) == AE_IFCHR) {
267 format_hex(archive_entry_rdevmajor(entry), h + c_rdevmajor_offset, c_rdevmajor_size);
268 format_hex(archive_entry_rdevminor(entry), h + c_rdevminor_offset, c_rdevminor_size);
270 format_hex(0, h + c_rdevmajor_offset, c_rdevmajor_size);
271 format_hex(0, h + c_rdevminor_offset, c_rdevminor_size);
273 format_hex(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
274 format_hex(pathlength, h + c_namesize_offset, c_namesize_size);
275 format_hex(0, h + c_checksum_offset, c_checksum_size);
277 /* Non-regular files don't store bodies. */
278 if (archive_entry_filetype(entry) != AE_IFREG)
279 archive_entry_set_size(entry, 0);
281 /* Symlinks get the link written as the body of the entry. */
282 ret = archive_entry_symlink_l(entry, &p, &len, sconv);
284 if (errno == ENOMEM) {
285 archive_set_error(&a->archive, ENOMEM,
286 "Can't allocate memory for Likname");
287 return (ARCHIVE_FATAL);
289 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
290 "Can't translate linkname '%s' to %s",
291 archive_entry_symlink(entry),
292 archive_string_conversion_charset_name(sconv));
293 ret_final = ARCHIVE_WARN;
295 if (len > 0 && p != NULL && *p != '\0')
296 ret = format_hex(strlen(p), h + c_filesize_offset,
299 ret = format_hex(archive_entry_size(entry),
300 h + c_filesize_offset, c_filesize_size);
302 archive_set_error(&a->archive, ERANGE,
303 "File is too large for this format.");
304 return (ARCHIVE_FAILED);
307 ret = __archive_write_output(a, h, c_header_size);
308 if (ret != ARCHIVE_OK)
309 return (ARCHIVE_FATAL);
311 /* Pad pathname to even length. */
312 ret = __archive_write_output(a, path, pathlength);
313 if (ret != ARCHIVE_OK)
314 return (ARCHIVE_FATAL);
315 pad = PAD4(pathlength + c_header_size);
317 ret = __archive_write_output(a, "\0\0\0", pad);
318 if (ret != ARCHIVE_OK)
319 return (ARCHIVE_FATAL);
322 cpio->entry_bytes_remaining = archive_entry_size(entry);
323 cpio->padding = PAD4(cpio->entry_bytes_remaining);
325 /* Write the symlink now. */
326 if (p != NULL && *p != '\0') {
327 ret = __archive_write_output(a, p, strlen(p));
328 if (ret != ARCHIVE_OK)
329 return (ARCHIVE_FATAL);
330 pad = PAD4(strlen(p));
331 ret = __archive_write_output(a, "\0\0\0", pad);
332 if (ret != ARCHIVE_OK)
333 return (ARCHIVE_FATAL);
339 archive_write_newc_data(struct archive_write *a, const void *buff, size_t s)
344 cpio = (struct cpio *)a->format_data;
345 if (s > cpio->entry_bytes_remaining)
346 s = cpio->entry_bytes_remaining;
348 ret = __archive_write_output(a, buff, s);
349 cpio->entry_bytes_remaining -= s;
357 * Format a number into the specified field.
360 format_hex(int64_t v, void *p, int digits)
365 max = (((int64_t)1) << (digits * 4)) - 1;
366 if (v >= 0 && v <= max) {
367 format_hex_recursive(v, (char *)p, digits);
370 format_hex_recursive(max, (char *)p, digits);
377 format_hex_recursive(int64_t v, char *p, int s)
381 v = format_hex_recursive(v, p+1, s-1);
382 *p = "0123456789abcdef"[v & 0xf];
387 archive_write_newc_close(struct archive_write *a)
390 struct archive_entry *trailer;
392 trailer = archive_entry_new();
393 archive_entry_set_nlink(trailer, 1);
394 archive_entry_set_size(trailer, 0);
395 archive_entry_set_pathname(trailer, "TRAILER!!!");
396 /* Bypass the required data checks. */
397 er = write_header(a, trailer);
398 archive_entry_free(trailer);
403 archive_write_newc_free(struct archive_write *a)
407 cpio = (struct cpio *)a->format_data;
409 a->format_data = NULL;
414 archive_write_newc_finish_entry(struct archive_write *a)
418 cpio = (struct cpio *)a->format_data;
419 return (__archive_write_nulls(a, cpio->entry_bytes_remaining + cpio->padding));