1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
25 */
26
27 #include "archive_platform.h"
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_entry_locale.h"
43 #include "archive_integer.h"
44 #include "archive_private.h"
45 #include "archive_write_private.h"
46 #include "archive_write_set_format_private.h"
47
48 static ssize_t archive_write_odc_data(struct archive_write *,
49 const void *buff, size_t s);
50 static int archive_write_odc_close(struct archive_write *);
51 static int archive_write_odc_free(struct archive_write *);
52 static int archive_write_odc_finish_entry(struct archive_write *);
53 static int archive_write_odc_header(struct archive_write *,
54 struct archive_entry *);
55 static int archive_write_odc_options(struct archive_write *,
56 const char *, const char *);
57 static int format_octal(int64_t, void *, int);
58 static int64_t format_octal_recursive(int64_t, char *, int);
59 static int write_header(struct archive_write *, struct archive_entry *);
60
61 struct cpio {
62 uint64_t entry_bytes_remaining;
63
64 int64_t ino_next;
65
66 struct { int64_t old; int new;} *ino_list;
67 size_t ino_list_size;
68 size_t ino_list_next;
69
70 struct archive_string_conv *opt_sconv;
71 struct archive_string_conv *sconv_default;
72 int init_default_conversion;
73 };
74
75 #define c_magic_offset 0
76 #define c_magic_size 6
77 #define c_dev_offset 6
78 #define c_dev_size 6
79 #define c_ino_offset 12
80 #define c_ino_size 6
81 #define c_mode_offset 18
82 #define c_mode_size 6
83 #define c_uid_offset 24
84 #define c_uid_size 6
85 #define c_gid_offset 30
86 #define c_gid_size 6
87 #define c_nlink_offset 36
88 #define c_nlink_size 6
89 #define c_rdev_offset 42
90 #define c_rdev_size 6
91 #define c_mtime_offset 48
92 #define c_mtime_size 11
93 #define c_namesize_offset 59
94 #define c_namesize_size 6
95 #define c_filesize_offset 65
96 #define c_filesize_size 11
97
98 /*
99 * Set output format to 'cpio' format.
100 */
101 int
archive_write_set_format_cpio_odc(struct archive * _a)102 archive_write_set_format_cpio_odc(struct archive *_a)
103 {
104 struct archive_write *a = (struct archive_write *)_a;
105 struct cpio *cpio;
106
107 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
108 ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_odc");
109
110 /* If someone else was already registered, unregister them. */
111 (void)__archive_write_unregister_format(a);
112
113 cpio = calloc(1, sizeof(*cpio));
114 if (cpio == NULL) {
115 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
116 return (ARCHIVE_FATAL);
117 }
118 a->format_data = cpio;
119 a->format_name = "cpio";
120 a->format_options = archive_write_odc_options;
121 a->format_write_header = archive_write_odc_header;
122 a->format_write_data = archive_write_odc_data;
123 a->format_finish_entry = archive_write_odc_finish_entry;
124 a->format_close = archive_write_odc_close;
125 a->format_free = archive_write_odc_free;
126 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
127 a->archive.archive_format_name = "POSIX cpio";
128 return (ARCHIVE_OK);
129 }
130
131 static int
archive_write_odc_options(struct archive_write * a,const char * key,const char * val)132 archive_write_odc_options(struct archive_write *a, const char *key,
133 const char *val)
134 {
135 struct cpio *cpio = a->format_data;
136 int ret = ARCHIVE_FAILED;
137
138 if (strcmp(key, "hdrcharset") == 0) {
139 if (val == NULL || val[0] == 0)
140 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
141 "%s: hdrcharset option needs a character-set name",
142 a->format_name);
143 else {
144 cpio->opt_sconv = archive_string_conversion_to_charset(
145 &a->archive, val, 0);
146 if (cpio->opt_sconv != NULL)
147 ret = ARCHIVE_OK;
148 else
149 ret = ARCHIVE_FATAL;
150 }
151 return (ret);
152 }
153
154 /* Note: The "warn" return is just to inform the options
155 * supervisor that we didn't handle it. It will generate
156 * a suitable error if no one used this option. */
157 return (ARCHIVE_WARN);
158 }
159
160 /*
161 * Ino values are as long as 64 bits on some systems; cpio format
162 * only allows 18 bits and relies on the ino values to identify hardlinked
163 * files. So, we can't merely "hash" the ino numbers since collisions
164 * would corrupt the archive. Instead, we generate synthetic ino values
165 * to store in the archive and maintain a map of original ino values to
166 * synthetic ones so we can preserve hardlink information.
167 *
168 * TODO: Make this more efficient. It's not as bad as it looks (most
169 * files don't have any hardlinks and we don't do any work here for those),
170 * but it wouldn't be hard to do better.
171 *
172 * TODO: Work with dev/ino pairs here instead of just ino values.
173 */
174 static int
synthesize_ino_value(struct cpio * cpio,struct archive_entry * entry)175 synthesize_ino_value(struct cpio *cpio, struct archive_entry *entry)
176 {
177 int64_t ino = archive_entry_ino64(entry);
178 int ino_new;
179 size_t i;
180
181 /*
182 * If no index number was given, don't assign one. In
183 * particular, this handles the end-of-archive marker
184 * correctly by giving it a zero index value. (This is also
185 * why we start our synthetic index numbers with one below.)
186 */
187 if (ino == 0)
188 return (0);
189
190 /* Don't store a mapping if we don't need to. */
191 if (archive_entry_nlink(entry) < 2) {
192 return (int)(++cpio->ino_next);
193 }
194
195 /* Look up old ino; if we have it, this is a hardlink
196 * and we reuse the same value. */
197 for (i = 0; i < cpio->ino_list_next; ++i) {
198 if (cpio->ino_list[i].old == ino)
199 return (cpio->ino_list[i].new);
200 }
201
202 /* Assign a new index number. */
203 ino_new = (int)(++cpio->ino_next);
204
205 /* Ensure space for the new mapping. */
206 if (cpio->ino_list_size <= cpio->ino_list_next) {
207 size_t newsize, size;
208 if (cpio->ino_list_size < 512)
209 newsize = 512;
210 else if (archive_ckd_mul_size(&newsize,
211 cpio->ino_list_size, 2))
212 return (-1);
213 if (archive_ckd_mul_size(&size,
214 newsize, sizeof(cpio->ino_list[0])))
215 return (-1);
216 void *newlist = realloc(cpio->ino_list, size);
217 if (newlist == NULL)
218 return (-1);
219
220 cpio->ino_list_size = newsize;
221 cpio->ino_list = newlist;
222 }
223
224 /* Record and return the new value. */
225 cpio->ino_list[cpio->ino_list_next].old = ino;
226 cpio->ino_list[cpio->ino_list_next].new = ino_new;
227 ++cpio->ino_list_next;
228 return (ino_new);
229 }
230
231
232 static struct archive_string_conv *
get_sconv(struct archive_write * a)233 get_sconv(struct archive_write *a)
234 {
235 struct cpio *cpio = a->format_data;
236 struct archive_string_conv *sconv;
237
238 sconv = cpio->opt_sconv;
239 if (sconv == NULL) {
240 if (!cpio->init_default_conversion) {
241 cpio->sconv_default =
242 archive_string_default_conversion_for_write(
243 &(a->archive));
244 cpio->init_default_conversion = 1;
245 }
246 sconv = cpio->sconv_default;
247 }
248 return (sconv);
249 }
250
251 static int
archive_write_odc_header(struct archive_write * a,struct archive_entry * entry)252 archive_write_odc_header(struct archive_write *a, struct archive_entry *entry)
253 {
254 const char *path;
255 size_t len;
256
257 if (archive_entry_filetype(entry) == 0 && archive_entry_hardlink(entry) == NULL) {
258 archive_set_error(&a->archive, -1, "Filetype required");
259 return (ARCHIVE_FAILED);
260 }
261
262 if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
263 && errno == ENOMEM) {
264 archive_set_error(&a->archive, ENOMEM,
265 "Can't allocate memory for Pathname");
266 return (ARCHIVE_FATAL);
267 }
268 if (len == 0 || path == NULL || path[0] == '\0') {
269 archive_set_error(&a->archive, -1, "Pathname required");
270 return (ARCHIVE_FAILED);
271 }
272
273 if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
274 archive_set_error(&a->archive, -1, "Size required");
275 return (ARCHIVE_FAILED);
276 }
277 return write_header(a, entry);
278 }
279
280 static int
write_header(struct archive_write * a,struct archive_entry * entry)281 write_header(struct archive_write *a, struct archive_entry *entry)
282 {
283 struct cpio *cpio = a->format_data;
284 const char *p, *path;
285 int ret, ret_final;
286 int64_t ino;
287 char h[76];
288 struct archive_string_conv *sconv;
289 struct archive_entry *entry_main;
290 size_t len, pathlength;
291
292 ret_final = ARCHIVE_OK;
293 sconv = get_sconv(a);
294
295 #if defined(_WIN32) && !defined(__CYGWIN__)
296 /* Make sure the path separators in pathname, hardlink and symlink
297 * are all slash '/', not the Windows path separator '\'. */
298 entry_main = __la_win_entry_in_posix_pathseparator(entry);
299 if (entry_main == NULL) {
300 archive_set_error(&a->archive, ENOMEM,
301 "Can't allocate ustar data");
302 return(ARCHIVE_FATAL);
303 }
304 if (entry != entry_main)
305 entry = entry_main;
306 else
307 entry_main = NULL;
308 #else
309 entry_main = NULL;
310 #endif
311
312 ret = archive_entry_pathname_l(entry, &path, &len, sconv);
313 if (ret != 0) {
314 if (errno == ENOMEM) {
315 archive_set_error(&a->archive, ENOMEM,
316 "Can't allocate memory for Pathname");
317 ret_final = ARCHIVE_FATAL;
318 goto exit_write_header;
319 }
320 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
321 "Can't translate pathname '%s' to %s",
322 archive_entry_pathname(entry),
323 archive_string_conversion_charset_name(sconv));
324 ret_final = ARCHIVE_WARN;
325 }
326 /* Include trailing null. */
327 pathlength = len + 1;
328
329 memset(h, 0, sizeof(h));
330 format_octal(070707, h + c_magic_offset, c_magic_size);
331 format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
332
333 ino = synthesize_ino_value(cpio, entry);
334 if (ino < 0) {
335 archive_set_error(&a->archive, ENOMEM,
336 "No memory for ino translation table");
337 ret_final = ARCHIVE_FATAL;
338 goto exit_write_header;
339 } else if (ino > 0777777) {
340 archive_set_error(&a->archive, ERANGE,
341 "Too many files for this cpio format");
342 ret_final = ARCHIVE_FATAL;
343 goto exit_write_header;
344 }
345 format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
346
347 /* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
348 format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
349 format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
350 format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
351 format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
352 if (archive_entry_filetype(entry) == AE_IFBLK
353 || archive_entry_filetype(entry) == AE_IFCHR)
354 format_octal(archive_entry_rdev(entry), h + c_rdev_offset, c_rdev_size);
355 else
356 format_octal(0, h + c_rdev_offset, c_rdev_size);
357 format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
358 if (format_octal((int64_t)pathlength, h + c_namesize_offset, c_namesize_size)) {
359 archive_set_error(&a->archive, ERANGE,
360 "Filename is too long for cpio format");
361 ret_final = ARCHIVE_FAILED;
362 goto exit_write_header;
363 }
364
365 /* Non-regular files don't store bodies. */
366 if (archive_entry_filetype(entry) != AE_IFREG)
367 archive_entry_set_size(entry, 0);
368
369 /* Symlinks get the link written as the body of the entry. */
370 ret = archive_entry_symlink_l(entry, &p, &len, sconv);
371 if (ret != 0) {
372 if (errno == ENOMEM) {
373 archive_set_error(&a->archive, ENOMEM,
374 "Can't allocate memory for Linkname");
375 ret_final = ARCHIVE_FATAL;
376 goto exit_write_header;
377 }
378 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
379 "Can't translate linkname '%s' to %s",
380 archive_entry_symlink(entry),
381 archive_string_conversion_charset_name(sconv));
382 ret_final = ARCHIVE_WARN;
383 }
384 if (len > 0 && p != NULL && *p != '\0')
385 ret = format_octal(strlen(p), h + c_filesize_offset,
386 c_filesize_size);
387 else
388 ret = format_octal(archive_entry_size(entry),
389 h + c_filesize_offset, c_filesize_size);
390 if (ret) {
391 archive_set_error(&a->archive, ERANGE,
392 "File is too large for cpio format");
393 ret_final = ARCHIVE_FAILED;
394 goto exit_write_header;
395 }
396
397 ret = __archive_write_output(a, h, sizeof(h));
398 if (ret != ARCHIVE_OK) {
399 ret_final = ARCHIVE_FATAL;
400 goto exit_write_header;
401 }
402
403 ret = __archive_write_output(a, path, pathlength);
404 if (ret != ARCHIVE_OK) {
405 ret_final = ARCHIVE_FATAL;
406 goto exit_write_header;
407 }
408
409 cpio->entry_bytes_remaining = archive_entry_size(entry);
410
411 /* Write the symlink now. */
412 if (p != NULL && *p != '\0') {
413 ret = __archive_write_output(a, p, strlen(p));
414 if (ret != ARCHIVE_OK) {
415 ret_final = ARCHIVE_FATAL;
416 goto exit_write_header;
417 }
418 }
419 exit_write_header:
420 archive_entry_free(entry_main);
421 return (ret_final);
422 }
423
424 static ssize_t
archive_write_odc_data(struct archive_write * a,const void * buff,size_t s)425 archive_write_odc_data(struct archive_write *a, const void *buff, size_t s)
426 {
427 struct cpio *cpio = a->format_data;
428 int ret;
429
430 if (s > cpio->entry_bytes_remaining)
431 s = (size_t)cpio->entry_bytes_remaining;
432
433 ret = __archive_write_output(a, buff, s);
434 cpio->entry_bytes_remaining -= s;
435 if (ret >= 0)
436 return (s);
437 else
438 return (ret);
439 }
440
441 /*
442 * Format a number into the specified field.
443 */
444 static int
format_octal(int64_t v,void * p,int digits)445 format_octal(int64_t v, void *p, int digits)
446 {
447 int64_t max;
448 int ret;
449
450 max = (((int64_t)1) << (digits * 3)) - 1;
451 if (v >= 0 && v <= max) {
452 format_octal_recursive(v, (char *)p, digits);
453 ret = 0;
454 } else {
455 format_octal_recursive(max, (char *)p, digits);
456 ret = -1;
457 }
458 return (ret);
459 }
460
461 static int64_t
format_octal_recursive(int64_t v,char * p,int s)462 format_octal_recursive(int64_t v, char *p, int s)
463 {
464 if (s == 0)
465 return (v);
466 v = format_octal_recursive(v, p+1, s-1);
467 *p = '0' + ((char)v & 7);
468 return (v >> 3);
469 }
470
471 static int
archive_write_odc_close(struct archive_write * a)472 archive_write_odc_close(struct archive_write *a)
473 {
474 int er;
475 struct archive_entry *trailer;
476
477 trailer = archive_entry_new2(NULL);
478 if (trailer == NULL) {
479 return ARCHIVE_FATAL;
480 }
481 /* nlink = 1 here for GNU cpio compat. */
482 archive_entry_set_nlink(trailer, 1);
483 archive_entry_set_size(trailer, 0);
484 archive_entry_set_pathname(trailer, "TRAILER!!!");
485 er = write_header(a, trailer);
486 archive_entry_free(trailer);
487 return (er);
488 }
489
490 static int
archive_write_odc_free(struct archive_write * a)491 archive_write_odc_free(struct archive_write *a)
492 {
493 struct cpio *cpio = a->format_data;
494
495 free(cpio->ino_list);
496 free(cpio);
497 a->format_data = NULL;
498 return (ARCHIVE_OK);
499 }
500
501 static int
archive_write_odc_finish_entry(struct archive_write * a)502 archive_write_odc_finish_entry(struct archive_write *a)
503 {
504 struct cpio *cpio = a->format_data;
505
506 return (__archive_write_nulls(a,
507 cpio->entry_bytes_remaining));
508 }
509