2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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 $");
41 #include "archive_entry.h"
42 #include "archive_entry_locale.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
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 *);
60 uint64_t entry_bytes_remaining;
64 struct { int64_t old; int new;} *ino_list;
68 struct archive_string_conv *opt_sconv;
69 struct archive_string_conv *sconv_default;
70 int init_default_conversion;
73 #define c_magic_offset 0
74 #define c_magic_size 6
75 #define c_dev_offset 6
77 #define c_ino_offset 12
79 #define c_mode_offset 18
81 #define c_uid_offset 24
83 #define c_gid_offset 30
85 #define c_nlink_offset 36
86 #define c_nlink_size 6
87 #define c_rdev_offset 42
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
97 * Set output format to 'cpio' format.
100 archive_write_set_format_cpio(struct archive *_a)
102 struct archive_write *a = (struct archive_write *)_a;
105 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
106 ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
108 /* If someone else was already registered, unregister them. */
109 if (a->format_free != NULL)
112 cpio = (struct cpio *)calloc(1, sizeof(*cpio));
114 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
115 return (ARCHIVE_FATAL);
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";
131 archive_write_cpio_options(struct archive_write *a, const char *key,
134 struct cpio *cpio = (struct cpio *)a->format_data;
135 int ret = ARCHIVE_FAILED;
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",
143 cpio->opt_sconv = archive_string_conversion_to_charset(
144 &a->archive, val, 0);
145 if (cpio->opt_sconv != NULL)
151 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
152 "%s: unknown keyword ``%s''", a->format_name, key);
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.
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.
169 * TODO: Work with dev/ino pairs here instead of just ino values.
172 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
174 int64_t ino = archive_entry_ino64(entry);
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.)
187 /* Don't store a mapping if we don't need to. */
188 if (archive_entry_nlink(entry) < 2) {
189 return ++cpio->ino_next;
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);
199 /* Assign a new index number. */
200 ino_new = ++cpio->ino_next;
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);
211 cpio->ino_list_size = newsize;
212 cpio->ino_list = newlist;
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;
223 static struct archive_string_conv *
224 get_sconv(struct archive_write *a)
227 struct archive_string_conv *sconv;
229 cpio = (struct cpio *)a->format_data;
230 sconv = cpio->opt_sconv;
232 if (!cpio->init_default_conversion) {
233 cpio->sconv_default =
234 archive_string_default_conversion_for_write(
236 cpio->init_default_conversion = 1;
238 sconv = cpio->sconv_default;
244 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
249 if (archive_entry_filetype(entry) == 0) {
250 archive_set_error(&a->archive, -1, "Filetype required");
251 return (ARCHIVE_FAILED);
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);
260 if (len == 0 || path == NULL || path[0] == '\0') {
261 archive_set_error(&a->archive, -1, "Pathname required");
262 return (ARCHIVE_FAILED);
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);
269 return write_header(a, entry);
273 write_header(struct archive_write *a, struct archive_entry *entry)
276 const char *p, *path;
277 int pathlength, ret, ret_final;
280 struct archive_string_conv *sconv;
283 cpio = (struct cpio *)a->format_data;
284 ret_final = ARCHIVE_OK;
285 sconv = get_sconv(a);
287 ret = archive_entry_pathname_l(entry, &path, &len, sconv);
289 if (errno == ENOMEM) {
290 archive_set_error(&a->archive, ENOMEM,
291 "Can't allocate memory for Pathname");
292 return (ARCHIVE_FATAL);
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;
300 /* Include trailing null. */
301 pathlength = (int)len + 1;
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);
307 ino = synthesize_ino_value(cpio, entry);
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);
317 format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
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);
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);
332 /* Non-regular files don't store bodies. */
333 if (archive_entry_filetype(entry) != AE_IFREG)
334 archive_entry_set_size(entry, 0);
336 /* Symlinks get the link written as the body of the entry. */
337 ret = archive_entry_symlink_l(entry, &p, &len, sconv);
339 if (errno == ENOMEM) {
340 archive_set_error(&a->archive, ENOMEM,
341 "Can't allocate memory for Linkname");
342 return (ARCHIVE_FATAL);
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;
350 if (len > 0 && p != NULL && *p != '\0')
351 ret = format_octal(strlen(p), h + c_filesize_offset,
354 ret = format_octal(archive_entry_size(entry),
355 h + c_filesize_offset, c_filesize_size);
357 archive_set_error(&a->archive, ERANGE,
358 "File is too large for cpio format.");
359 return (ARCHIVE_FAILED);
362 ret = __archive_write_output(a, h, sizeof(h));
363 if (ret != ARCHIVE_OK)
364 return (ARCHIVE_FATAL);
366 ret = __archive_write_output(a, path, pathlength);
367 if (ret != ARCHIVE_OK)
368 return (ARCHIVE_FATAL);
370 cpio->entry_bytes_remaining = archive_entry_size(entry);
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);
382 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
387 cpio = (struct cpio *)a->format_data;
388 if (s > cpio->entry_bytes_remaining)
389 s = cpio->entry_bytes_remaining;
391 ret = __archive_write_output(a, buff, s);
392 cpio->entry_bytes_remaining -= s;
400 * Format a number into the specified field.
403 format_octal(int64_t v, void *p, int digits)
408 max = (((int64_t)1) << (digits * 3)) - 1;
409 if (v >= 0 && v <= max) {
410 format_octal_recursive(v, (char *)p, digits);
413 format_octal_recursive(max, (char *)p, digits);
420 format_octal_recursive(int64_t v, char *p, int s)
424 v = format_octal_recursive(v, p+1, s-1);
430 archive_write_cpio_close(struct archive_write *a)
433 struct archive_entry *trailer;
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);
446 archive_write_cpio_free(struct archive_write *a)
450 cpio = (struct cpio *)a->format_data;
451 free(cpio->ino_list);
453 a->format_data = NULL;
458 archive_write_cpio_finish_entry(struct archive_write *a)
462 cpio = (struct cpio *)a->format_data;
463 return (__archive_write_nulls(a, cpio->entry_bytes_remaining));