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;
90 * Set output format to 'cpio' format.
93 archive_write_set_format_cpio(struct archive *_a)
95 struct archive_write *a = (struct archive_write *)_a;
98 /* If someone else was already registered, unregister them. */
99 if (a->format_destroy != NULL)
100 (a->format_destroy)(a);
102 cpio = (struct cpio *)malloc(sizeof(*cpio));
104 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
105 return (ARCHIVE_FATAL);
107 memset(cpio, 0, sizeof(*cpio));
108 a->format_data = cpio;
110 a->pad_uncompressed = 1;
111 a->format_name = "cpio";
112 a->format_write_header = archive_write_cpio_header;
113 a->format_write_data = archive_write_cpio_data;
114 a->format_finish_entry = archive_write_cpio_finish_entry;
115 a->format_finish = archive_write_cpio_finish;
116 a->format_destroy = archive_write_cpio_destroy;
117 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
118 a->archive.archive_format_name = "POSIX cpio";
123 * Ino values are as long as 64 bits on some systems; cpio format
124 * only allows 18 bits and relies on the ino values to identify hardlinked
125 * files. So, we can't merely "hash" the ino numbers since collisions
126 * would corrupt the archive. Instead, we generate synthetic ino values
127 * to store in the archive and maintain a map of original ino values to
128 * synthetic ones so we can preserve hardlink information.
130 * TODO: Make this more efficient. It's not as bad as it looks (most
131 * files don't have any hardlinks and we don't do any work here for those),
132 * but it wouldn't be hard to do better.
134 * TODO: Work with dev/ino pairs here instead of just ino values.
137 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
139 int64_t ino = archive_entry_ino64(entry);
144 * If no index number was given, don't assign one. In
145 * particular, this handles the end-of-archive marker
146 * correctly by giving it a zero index value. (This is also
147 * why we start our synthetic index numbers with one below.)
152 /* Don't store a mapping if we don't need to. */
153 if (archive_entry_nlink(entry) < 2) {
154 return ++cpio->ino_next;
157 /* Look up old ino; if we have it, this is a hardlink
158 * and we reuse the same value. */
159 for (i = 0; i < cpio->ino_list_next; ++i) {
160 if (cpio->ino_list[i].old == ino)
161 return (cpio->ino_list[i].new);
164 /* Assign a new index number. */
165 ino_new = ++cpio->ino_next;
167 /* Ensure space for the new mapping. */
168 if (cpio->ino_list_size <= cpio->ino_list_next) {
169 size_t newsize = cpio->ino_list_size < 512
170 ? 512 : cpio->ino_list_size * 2;
171 void *newlist = realloc(cpio->ino_list,
172 sizeof(cpio->ino_list[0]) * newsize);
176 cpio->ino_list_size = newsize;
177 cpio->ino_list = newlist;
180 /* Record and return the new value. */
181 cpio->ino_list[cpio->ino_list_next].old = ino;
182 cpio->ino_list[cpio->ino_list_next].new = ino_new;
183 ++cpio->ino_list_next;
188 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
191 const char *p, *path;
192 int pathlength, ret, ret2;
194 struct cpio_header h;
196 cpio = (struct cpio *)a->format_data;
199 path = archive_entry_pathname(entry);
200 pathlength = (int)strlen(path) + 1; /* Include trailing null. */
202 memset(&h, 0, sizeof(h));
203 format_octal(070707, &h.c_magic, sizeof(h.c_magic));
204 format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
206 ino = synthesize_ino_value(cpio, entry);
208 archive_set_error(&a->archive, ENOMEM,
209 "No memory for ino translation table");
210 return (ARCHIVE_FATAL);
211 } else if (ino > 0777777) {
212 archive_set_error(&a->archive, ERANGE,
213 "Too many files for this cpio format");
214 return (ARCHIVE_FATAL);
216 format_octal(ino & 0777777, &h.c_ino, sizeof(h.c_ino));
218 format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
219 format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
220 format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
221 format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
222 if (archive_entry_filetype(entry) == AE_IFBLK
223 || archive_entry_filetype(entry) == AE_IFCHR)
224 format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
226 format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
227 format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
228 format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
230 /* Non-regular files don't store bodies. */
231 if (archive_entry_filetype(entry) != AE_IFREG)
232 archive_entry_set_size(entry, 0);
234 /* Symlinks get the link written as the body of the entry. */
235 p = archive_entry_symlink(entry);
236 if (p != NULL && *p != '\0')
237 format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
239 format_octal(archive_entry_size(entry),
240 &h.c_filesize, sizeof(h.c_filesize));
242 ret = (a->compressor.write)(a, &h, sizeof(h));
243 if (ret != ARCHIVE_OK)
244 return (ARCHIVE_FATAL);
246 ret = (a->compressor.write)(a, path, pathlength);
247 if (ret != ARCHIVE_OK)
248 return (ARCHIVE_FATAL);
250 cpio->entry_bytes_remaining = archive_entry_size(entry);
252 /* Write the symlink now. */
253 if (p != NULL && *p != '\0')
254 ret = (a->compressor.write)(a, p, strlen(p));
256 if (ret == ARCHIVE_OK)
262 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
267 cpio = (struct cpio *)a->format_data;
268 if (s > cpio->entry_bytes_remaining)
269 s = cpio->entry_bytes_remaining;
271 ret = (a->compressor.write)(a, buff, s);
272 cpio->entry_bytes_remaining -= s;
280 * Format a number into the specified field.
283 format_octal(int64_t v, void *p, int digits)
288 max = (((int64_t)1) << (digits * 3)) - 1;
289 if (v >= 0 && v <= max) {
290 format_octal_recursive(v, (char *)p, digits);
293 format_octal_recursive(max, (char *)p, digits);
300 format_octal_recursive(int64_t v, char *p, int s)
304 v = format_octal_recursive(v, p+1, s-1);
310 archive_write_cpio_finish(struct archive_write *a)
313 struct archive_entry *trailer;
315 trailer = archive_entry_new();
316 /* nlink = 1 here for GNU cpio compat. */
317 archive_entry_set_nlink(trailer, 1);
318 archive_entry_set_pathname(trailer, "TRAILER!!!");
319 er = archive_write_cpio_header(a, trailer);
320 archive_entry_free(trailer);
325 archive_write_cpio_destroy(struct archive_write *a)
329 cpio = (struct cpio *)a->format_data;
330 free(cpio->ino_list);
332 a->format_data = NULL;
337 archive_write_cpio_finish_entry(struct archive_write *a)
343 cpio = (struct cpio *)a->format_data;
345 while (cpio->entry_bytes_remaining > 0) {
346 to_write = cpio->entry_bytes_remaining < a->null_length ?
347 cpio->entry_bytes_remaining : a->null_length;
348 ret = (a->compressor.write)(a, a->nulls, to_write);
349 if (ret != ARCHIVE_OK)
351 cpio->entry_bytes_remaining -= to_write;