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$");
41 #include "archive_entry.h"
42 #include "archive_private.h"
43 #include "archive_write_private.h"
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);
56 uint64_t entry_bytes_remaining;
60 struct { int64_t old; int new;} *ino_list;
80 * Set output format to 'cpio' format.
83 archive_write_set_format_cpio(struct archive *_a)
85 struct archive_write *a = (struct archive_write *)_a;
88 /* If someone else was already registered, unregister them. */
89 if (a->format_destroy != NULL)
90 (a->format_destroy)(a);
92 cpio = (struct cpio *)malloc(sizeof(*cpio));
94 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
95 return (ARCHIVE_FATAL);
97 memset(cpio, 0, sizeof(*cpio));
98 a->format_data = cpio;
100 a->pad_uncompressed = 1;
101 a->format_name = "cpio";
102 a->format_write_header = archive_write_cpio_header;
103 a->format_write_data = archive_write_cpio_data;
104 a->format_finish_entry = archive_write_cpio_finish_entry;
105 a->format_finish = archive_write_cpio_finish;
106 a->format_destroy = archive_write_cpio_destroy;
107 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
108 a->archive.archive_format_name = "POSIX cpio";
113 * Ino values are as long as 64 bits on some systems; cpio format
114 * only allows 18 bits and relies on the ino values to identify hardlinked
115 * files. So, we can't merely "hash" the ino numbers since collisions
116 * would corrupt the archive. Instead, we generate synthetic ino values
117 * to store in the archive and maintain a map of original ino values to
118 * synthetic ones so we can preserve hardlink information.
120 * TODO: Make this more efficient. It's not as bad as it looks (most
121 * files don't have any hardlinks and we don't do any work here for those),
122 * but it wouldn't be hard to do better.
124 * TODO: Work with dev/ino pairs here instead of just ino values.
127 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
129 int64_t ino = archive_entry_ino64(entry);
134 * If no index number was given, don't assign one. In
135 * particular, this handles the end-of-archive marker
136 * correctly by giving it a zero index value. (This is also
137 * why we start our synthetic index numbers with one below.)
142 /* Don't store a mapping if we don't need to. */
143 if (archive_entry_nlink(entry) < 2) {
144 return ++cpio->ino_next;
147 /* Look up old ino; if we have it, this is a hardlink
148 * and we reuse the same value. */
149 for (i = 0; i < cpio->ino_list_next; ++i) {
150 if (cpio->ino_list[i].old == ino)
151 return (cpio->ino_list[i].new);
154 /* Assign a new index number. */
155 ino_new = ++cpio->ino_next;
157 /* Ensure space for the new mapping. */
158 if (cpio->ino_list_size <= cpio->ino_list_next) {
159 size_t newsize = cpio->ino_list_size < 512
160 ? 512 : cpio->ino_list_size * 2;
161 void *newlist = realloc(cpio->ino_list,
162 sizeof(cpio->ino_list[0]) * newsize);
166 cpio->ino_list_size = newsize;
167 cpio->ino_list = newlist;
170 /* Record and return the new value. */
171 cpio->ino_list[cpio->ino_list_next].old = ino;
172 cpio->ino_list[cpio->ino_list_next].new = ino_new;
173 ++cpio->ino_list_next;
178 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
181 const char *p, *path;
182 int pathlength, ret, ret2;
184 struct cpio_header h;
186 cpio = (struct cpio *)a->format_data;
189 path = archive_entry_pathname(entry);
190 pathlength = (int)strlen(path) + 1; /* Include trailing null. */
192 memset(&h, 0, sizeof(h));
193 format_octal(070707, &h.c_magic, sizeof(h.c_magic));
194 format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
196 ino = synthesize_ino_value(cpio, entry);
198 archive_set_error(&a->archive, ENOMEM,
199 "No memory for ino translation table");
200 return (ARCHIVE_FATAL);
201 } else if (ino > 0777777) {
202 archive_set_error(&a->archive, ERANGE,
203 "Too many files for this cpio format");
204 return (ARCHIVE_FATAL);
206 format_octal(ino & 0777777, &h.c_ino, sizeof(h.c_ino));
208 format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
209 format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
210 format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
211 format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
212 if (archive_entry_filetype(entry) == AE_IFBLK
213 || archive_entry_filetype(entry) == AE_IFCHR)
214 format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
216 format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
217 format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
218 format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
220 /* Non-regular files don't store bodies. */
221 if (archive_entry_filetype(entry) != AE_IFREG)
222 archive_entry_set_size(entry, 0);
224 /* Symlinks get the link written as the body of the entry. */
225 p = archive_entry_symlink(entry);
226 if (p != NULL && *p != '\0')
227 format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
229 format_octal(archive_entry_size(entry),
230 &h.c_filesize, sizeof(h.c_filesize));
232 ret = (a->compressor.write)(a, &h, sizeof(h));
233 if (ret != ARCHIVE_OK)
234 return (ARCHIVE_FATAL);
236 ret = (a->compressor.write)(a, path, pathlength);
237 if (ret != ARCHIVE_OK)
238 return (ARCHIVE_FATAL);
240 cpio->entry_bytes_remaining = archive_entry_size(entry);
242 /* Write the symlink now. */
243 if (p != NULL && *p != '\0')
244 ret = (a->compressor.write)(a, p, strlen(p));
246 if (ret == ARCHIVE_OK)
252 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
257 cpio = (struct cpio *)a->format_data;
258 if (s > cpio->entry_bytes_remaining)
259 s = cpio->entry_bytes_remaining;
261 ret = (a->compressor.write)(a, buff, s);
262 cpio->entry_bytes_remaining -= s;
270 * Format a number into the specified field.
273 format_octal(int64_t v, void *p, int digits)
278 max = (((int64_t)1) << (digits * 3)) - 1;
279 if (v >= 0 && v <= max) {
280 format_octal_recursive(v, (char *)p, digits);
283 format_octal_recursive(max, (char *)p, digits);
290 format_octal_recursive(int64_t v, char *p, int s)
294 v = format_octal_recursive(v, p+1, s-1);
300 archive_write_cpio_finish(struct archive_write *a)
303 struct archive_entry *trailer;
305 trailer = archive_entry_new();
306 /* nlink = 1 here for GNU cpio compat. */
307 archive_entry_set_nlink(trailer, 1);
308 archive_entry_set_pathname(trailer, "TRAILER!!!");
309 er = archive_write_cpio_header(a, trailer);
310 archive_entry_free(trailer);
315 archive_write_cpio_destroy(struct archive_write *a)
319 cpio = (struct cpio *)a->format_data;
320 free(cpio->ino_list);
322 a->format_data = NULL;
327 archive_write_cpio_finish_entry(struct archive_write *a)
333 cpio = (struct cpio *)a->format_data;
335 while (cpio->entry_bytes_remaining > 0) {
336 to_write = cpio->entry_bytes_remaining < a->null_length ?
337 cpio->entry_bytes_remaining : a->null_length;
338 ret = (a->compressor.write)(a, a->nulls, to_write);
339 if (ret != ARCHIVE_OK)
341 cpio->entry_bytes_remaining -= to_write;