Import libarchive-2.2.5 which fixes a forgotten 'break'. Without this,
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_write_set_format_ustar.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_ustar.c,v 1.21 2007/04/02 00:34:36 kientzle Exp $");
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef MAJOR_IN_MKDEV
33 #include <sys/mkdev.h>
34 #else
35 #ifdef MAJOR_IN_SYSMACROS
36 #include <sys/sysmacros.h>
37 #endif
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #include <stdio.h>
43 #ifdef HAVE_STDLIB_H
44 #include <stdlib.h>
45 #endif
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #endif
49
50 #include "archive.h"
51 #include "archive_entry.h"
52 #include "archive_private.h"
53 #include "archive_write_private.h"
54
55 struct ustar {
56         uint64_t        entry_bytes_remaining;
57         uint64_t        entry_padding;
58 };
59
60 /*
61  * Define structure of POSIX 'ustar' tar header.
62  */
63 #define USTAR_name_offset 0
64 #define USTAR_name_size 100
65 #define USTAR_mode_offset 100
66 #define USTAR_mode_size 6
67 #define USTAR_mode_max_size 8
68 #define USTAR_uid_offset 108
69 #define USTAR_uid_size 6
70 #define USTAR_uid_max_size 8
71 #define USTAR_gid_offset 116
72 #define USTAR_gid_size 6
73 #define USTAR_gid_max_size 8
74 #define USTAR_size_offset 124
75 #define USTAR_size_size 11
76 #define USTAR_size_max_size 12
77 #define USTAR_mtime_offset 136
78 #define USTAR_mtime_size 11
79 #define USTAR_mtime_max_size 11
80 #define USTAR_checksum_offset 148
81 #define USTAR_checksum_size 8
82 #define USTAR_typeflag_offset 156
83 #define USTAR_typeflag_size 1
84 #define USTAR_linkname_offset 157
85 #define USTAR_linkname_size 100
86 #define USTAR_magic_offset 257
87 #define USTAR_magic_size 6
88 #define USTAR_version_offset 263
89 #define USTAR_version_size 2
90 #define USTAR_uname_offset 265
91 #define USTAR_uname_size 32
92 #define USTAR_gname_offset 297
93 #define USTAR_gname_size 32
94 #define USTAR_rdevmajor_offset 329
95 #define USTAR_rdevmajor_size 6
96 #define USTAR_rdevmajor_max_size 8
97 #define USTAR_rdevminor_offset 337
98 #define USTAR_rdevminor_size 6
99 #define USTAR_rdevminor_max_size 8
100 #define USTAR_prefix_offset 345
101 #define USTAR_prefix_size 155
102 #define USTAR_padding_offset 500
103 #define USTAR_padding_size 12
104
105 /*
106  * A filled-in copy of the header for initialization.
107  */
108 static const char template_header[] = {
109         /* name: 100 bytes */
110         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
111         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
112         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
113         0,0,0,0,
114         /* Mode, space-null termination: 8 bytes */
115         '0','0','0','0','0','0', ' ','\0',
116         /* uid, space-null termination: 8 bytes */
117         '0','0','0','0','0','0', ' ','\0',
118         /* gid, space-null termination: 8 bytes */
119         '0','0','0','0','0','0', ' ','\0',
120         /* size, space termation: 12 bytes */
121         '0','0','0','0','0','0','0','0','0','0','0', ' ',
122         /* mtime, space termation: 12 bytes */
123         '0','0','0','0','0','0','0','0','0','0','0', ' ',
124         /* Initial checksum value: 8 spaces */
125         ' ',' ',' ',' ',' ',' ',' ',' ',
126         /* Typeflag: 1 byte */
127         '0',                    /* '0' = regular file */
128         /* Linkname: 100 bytes */
129         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
130         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
131         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
132         0,0,0,0,
133         /* Magic: 6 bytes, Version: 2 bytes */
134         'u','s','t','a','r','\0', '0','0',
135         /* Uname: 32 bytes */
136         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
137         /* Gname: 32 bytes */
138         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
139         /* rdevmajor + space/null padding: 8 bytes */
140         '0','0','0','0','0','0', ' ','\0',
141         /* rdevminor + space/null padding: 8 bytes */
142         '0','0','0','0','0','0', ' ','\0',
143         /* Prefix: 155 bytes */
144         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
145         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
146         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
147         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
148         0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,
149         /* Padding: 12 bytes */
150         0,0,0,0,0,0,0,0, 0,0,0,0
151 };
152
153 static ssize_t  archive_write_ustar_data(struct archive_write *a, const void *buff,
154                     size_t s);
155 static int      archive_write_ustar_destroy(struct archive_write *);
156 static int      archive_write_ustar_finish(struct archive_write *);
157 static int      archive_write_ustar_finish_entry(struct archive_write *);
158 static int      archive_write_ustar_header(struct archive_write *,
159                     struct archive_entry *entry);
160 static int      format_256(int64_t, char *, int);
161 static int      format_number(int64_t, char *, int size, int max, int strict);
162 static int      format_octal(int64_t, char *, int);
163 static int      write_nulls(struct archive_write *a, size_t);
164
165 /*
166  * Set output format to 'ustar' format.
167  */
168 int
169 archive_write_set_format_ustar(struct archive *_a)
170 {
171         struct archive_write *a = (struct archive_write *)_a;
172         struct ustar *ustar;
173
174         /* If someone else was already registered, unregister them. */
175         if (a->format_destroy != NULL)
176                 (a->format_destroy)(a);
177
178         /* Basic internal sanity test. */
179         if (sizeof(template_header) != 512) {
180                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Internal: template_header wrong size: %d should be 512", sizeof(template_header));
181                 return (ARCHIVE_FATAL);
182         }
183
184         ustar = (struct ustar *)malloc(sizeof(*ustar));
185         if (ustar == NULL) {
186                 archive_set_error(&a->archive, ENOMEM, "Can't allocate ustar data");
187                 return (ARCHIVE_FATAL);
188         }
189         memset(ustar, 0, sizeof(*ustar));
190         a->format_data = ustar;
191
192         a->pad_uncompressed = 1;        /* Mimic gtar in this respect. */
193         a->format_write_header = archive_write_ustar_header;
194         a->format_write_data = archive_write_ustar_data;
195         a->format_finish = archive_write_ustar_finish;
196         a->format_destroy = archive_write_ustar_destroy;
197         a->format_finish_entry = archive_write_ustar_finish_entry;
198         a->archive_format = ARCHIVE_FORMAT_TAR_USTAR;
199         a->archive_format_name = "POSIX ustar";
200         return (ARCHIVE_OK);
201 }
202
203 static int
204 archive_write_ustar_header(struct archive_write *a, struct archive_entry *entry)
205 {
206         char buff[512];
207         int ret;
208         struct ustar *ustar;
209
210         ustar = (struct ustar *)a->format_data;
211
212         /* Only regular files (not hardlinks) have data. */
213         if (archive_entry_hardlink(entry) != NULL ||
214             archive_entry_symlink(entry) != NULL ||
215             !S_ISREG(archive_entry_mode(entry)))
216                 archive_entry_set_size(entry, 0);
217
218         ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1);
219         if (ret != ARCHIVE_OK)
220                 return (ret);
221         ret = (a->compression_write)(a, buff, 512);
222         if (ret != ARCHIVE_OK)
223                 return (ret);
224
225         ustar->entry_bytes_remaining = archive_entry_size(entry);
226         ustar->entry_padding = 0x1ff & (-(int64_t)ustar->entry_bytes_remaining);
227         return (ARCHIVE_OK);
228 }
229
230 /*
231  * Format a basic 512-byte "ustar" header.
232  *
233  * Returns -1 if format failed (due to field overflow).
234  * Note that this always formats as much of the header as possible.
235  * If "strict" is set to zero, it will extend numeric fields as
236  * necessary (overwriting terminators or using base-256 extensions).
237  *
238  * This is exported so that other 'tar' formats can use it.
239  */
240 int
241 __archive_write_format_header_ustar(struct archive_write *a, char h[512],
242     struct archive_entry *entry, int tartype, int strict)
243 {
244         unsigned int checksum;
245         int i, ret;
246         size_t copy_length;
247         const char *p, *pp;
248         const struct stat *st;
249         int mytartype;
250
251         ret = 0;
252         mytartype = -1;
253         /*
254          * The "template header" already includes the "ustar"
255          * signature, various end-of-field markers and other required
256          * elements.
257          */
258         memcpy(h, &template_header, 512);
259
260         /*
261          * Because the block is already null-filled, and strings
262          * are allowed to exactly fill their destination (without null),
263          * I use memcpy(dest, src, strlen()) here a lot to copy strings.
264          */
265
266         pp = archive_entry_pathname(entry);
267         if (strlen(pp) <= USTAR_name_size)
268                 memcpy(h + USTAR_name_offset, pp, strlen(pp));
269         else {
270                 /* Store in two pieces, splitting at a '/'. */
271                 p = strchr(pp + strlen(pp) - USTAR_name_size - 1, '/');
272                 /*
273                  * If there is no path separator, or the prefix or
274                  * remaining name are too large, return an error.
275                  */
276                 if (!p) {
277                         archive_set_error(&a->archive, ENAMETOOLONG,
278                             "Pathname too long");
279                         ret = ARCHIVE_WARN;
280                 } else if (p  > pp + USTAR_prefix_size) {
281                         archive_set_error(&a->archive, ENAMETOOLONG,
282                             "Pathname too long");
283                         ret = ARCHIVE_WARN;
284                 } else {
285                         /* Copy prefix and remainder to appropriate places */
286                         memcpy(h + USTAR_prefix_offset, pp, p - pp);
287                         memcpy(h + USTAR_name_offset, p + 1, pp + strlen(pp) - p - 1);
288                 }
289         }
290
291         p = archive_entry_hardlink(entry);
292         if (p != NULL)
293                 mytartype = '1';
294         else
295                 p = archive_entry_symlink(entry);
296         if (p != NULL && p[0] != '\0') {
297                 copy_length = strlen(p);
298                 if (copy_length > USTAR_linkname_size) {
299                         archive_set_error(&a->archive, ENAMETOOLONG,
300                             "Link contents too long");
301                         ret = ARCHIVE_WARN;
302                         copy_length = USTAR_linkname_size;
303                 }
304                 memcpy(h + USTAR_linkname_offset, p, copy_length);
305         }
306
307         p = archive_entry_uname(entry);
308         if (p != NULL && p[0] != '\0') {
309                 copy_length = strlen(p);
310                 if (copy_length > USTAR_uname_size) {
311                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
312                             "Username too long");
313                         ret = ARCHIVE_WARN;
314                         copy_length = USTAR_uname_size;
315                 }
316                 memcpy(h + USTAR_uname_offset, p, copy_length);
317         }
318
319         p = archive_entry_gname(entry);
320         if (p != NULL && p[0] != '\0') {
321                 copy_length = strlen(p);
322                 if (strlen(p) > USTAR_gname_size) {
323                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
324                             "Group name too long");
325                         ret = ARCHIVE_WARN;
326                         copy_length = USTAR_gname_size;
327                 }
328                 memcpy(h + USTAR_gname_offset, p, copy_length);
329         }
330
331         st = archive_entry_stat(entry);
332
333         if (format_number(st->st_mode & 07777, h + USTAR_mode_offset, USTAR_mode_size, USTAR_mode_max_size, strict)) {
334                 archive_set_error(&a->archive, ERANGE, "Numeric mode too large");
335                 ret = ARCHIVE_WARN;
336         }
337
338         if (format_number(st->st_uid, h + USTAR_uid_offset, USTAR_uid_size, USTAR_uid_max_size, strict)) {
339                 archive_set_error(&a->archive, ERANGE, "Numeric user ID too large");
340                 ret = ARCHIVE_WARN;
341         }
342
343         if (format_number(st->st_gid, h + USTAR_gid_offset, USTAR_gid_size, USTAR_gid_max_size, strict)) {
344                 archive_set_error(&a->archive, ERANGE, "Numeric group ID too large");
345                 ret = ARCHIVE_WARN;
346         }
347
348         if (format_number(st->st_size, h + USTAR_size_offset, USTAR_size_size, USTAR_size_max_size, strict)) {
349                 archive_set_error(&a->archive, ERANGE, "File size out of range");
350                 ret = ARCHIVE_WARN;
351         }
352
353         if (format_number(st->st_mtime, h + USTAR_mtime_offset, USTAR_mtime_size, USTAR_mtime_max_size, strict)) {
354                 archive_set_error(&a->archive, ERANGE,
355                     "File modification time too large");
356                 ret = ARCHIVE_WARN;
357         }
358
359         if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
360                 if (format_number(major(st->st_rdev), h + USTAR_rdevmajor_offset,
361                         USTAR_rdevmajor_size, USTAR_rdevmajor_max_size, strict)) {
362                         archive_set_error(&a->archive, ERANGE,
363                             "Major device number too large");
364                         ret = ARCHIVE_WARN;
365                 }
366
367                 if (format_number(minor(st->st_rdev), h + USTAR_rdevminor_offset,
368                         USTAR_rdevminor_size, USTAR_rdevminor_max_size, strict)) {
369                         archive_set_error(&a->archive, ERANGE,
370                             "Minor device number too large");
371                         ret = ARCHIVE_WARN;
372                 }
373         }
374
375         if (tartype >= 0) {
376                 h[USTAR_typeflag_offset] = tartype;
377         } else if (mytartype >= 0) {
378                 h[USTAR_typeflag_offset] = mytartype;
379         } else {
380                 switch (st->st_mode & S_IFMT) {
381                 case S_IFREG: h[USTAR_typeflag_offset] = '0' ; break;
382                 case S_IFLNK: h[USTAR_typeflag_offset] = '2' ; break;
383                 case S_IFCHR: h[USTAR_typeflag_offset] = '3' ; break;
384                 case S_IFBLK: h[USTAR_typeflag_offset] = '4' ; break;
385                 case S_IFDIR: h[USTAR_typeflag_offset] = '5' ; break;
386                 case S_IFIFO: h[USTAR_typeflag_offset] = '6' ; break;
387                 case S_IFSOCK:
388                         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
389                             "tar format cannot archive socket");
390                         ret = ARCHIVE_WARN;
391                         break;
392                 default:
393                         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
394                             "tar format cannot archive this (mode=0%lo)",
395                             (unsigned long)st->st_mode);
396                         ret = ARCHIVE_WARN;
397                 }
398         }
399
400         checksum = 0;
401         for (i = 0; i < 512; i++)
402                 checksum += 255 & (unsigned int)h[i];
403         h[USTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
404         /* h[USTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
405         format_octal(checksum, h + USTAR_checksum_offset, 6);
406         return (ret);
407 }
408
409 /*
410  * Format a number into a field, with some intelligence.
411  */
412 static int
413 format_number(int64_t v, char *p, int s, int maxsize, int strict)
414 {
415         int64_t limit;
416
417         limit = ((int64_t)1 << (s*3));
418
419         /* "Strict" only permits octal values with proper termination. */
420         if (strict)
421                 return (format_octal(v, p, s));
422
423         /*
424          * In non-strict mode, we allow the number to overwrite one or
425          * more bytes of the field termination.  Even old tar
426          * implementations should be able to handle this with no
427          * problem.
428          */
429         if (v >= 0) {
430                 while (s <= maxsize) {
431                         if (v < limit)
432                                 return (format_octal(v, p, s));
433                         s++;
434                         limit <<= 3;
435                 }
436         }
437
438         /* Base-256 can handle any number, positive or negative. */
439         return (format_256(v, p, maxsize));
440 }
441
442 /*
443  * Format a number into the specified field using base-256.
444  */
445 static int
446 format_256(int64_t v, char *p, int s)
447 {
448         p += s;
449         while (s-- > 0) {
450                 *--p = (char)(v & 0xff);
451                 v >>= 8;
452         }
453         *p |= 0x80; /* Set the base-256 marker bit. */
454         return (0);
455 }
456
457 /*
458  * Format a number into the specified field.
459  */
460 static int
461 format_octal(int64_t v, char *p, int s)
462 {
463         int len;
464
465         len = s;
466
467         /* Octal values can't be negative, so use 0. */
468         if (v < 0) {
469                 while (len-- > 0)
470                         *p++ = '0';
471                 return (-1);
472         }
473
474         p += s;         /* Start at the end and work backwards. */
475         while (s-- > 0) {
476                 *--p = (char)('0' + (v & 7));
477                 v >>= 3;
478         }
479
480         if (v == 0)
481                 return (0);
482
483         /* If it overflowed, fill field with max value. */
484         while (len-- > 0)
485                 *p++ = '7';
486
487         return (-1);
488 }
489
490 static int
491 archive_write_ustar_finish(struct archive_write *a)
492 {
493         int r;
494
495         if (a->compression_write == NULL)
496                 return (ARCHIVE_OK);
497
498         r = write_nulls(a, 512*2);
499         return (r);
500 }
501
502 static int
503 archive_write_ustar_destroy(struct archive_write *a)
504 {
505         struct ustar *ustar;
506
507         ustar = (struct ustar *)a->format_data;
508         free(ustar);
509         a->format_data = NULL;
510         return (ARCHIVE_OK);
511 }
512
513 static int
514 archive_write_ustar_finish_entry(struct archive_write *a)
515 {
516         struct ustar *ustar;
517         int ret;
518
519         ustar = (struct ustar *)a->format_data;
520         ret = write_nulls(a,
521             ustar->entry_bytes_remaining + ustar->entry_padding);
522         ustar->entry_bytes_remaining = ustar->entry_padding = 0;
523         return (ret);
524 }
525
526 static int
527 write_nulls(struct archive_write *a, size_t padding)
528 {
529         int ret;
530         size_t to_write;
531
532         while (padding > 0) {
533                 to_write = padding < a->null_length ? padding : a->null_length;
534                 ret = (a->compression_write)(a, a->nulls, to_write);
535                 if (ret != ARCHIVE_OK)
536                         return (ret);
537                 padding -= to_write;
538         }
539         return (ARCHIVE_OK);
540 }
541
542 static ssize_t
543 archive_write_ustar_data(struct archive_write *a, const void *buff, size_t s)
544 {
545         struct ustar *ustar;
546         int ret;
547
548         ustar = (struct ustar *)a->format_data;
549         if (s > ustar->entry_bytes_remaining)
550                 s = ustar->entry_bytes_remaining;
551         ret = (a->compression_write)(a, buff, s);
552         ustar->entry_bytes_remaining -= s;
553         if (ret != ARCHIVE_OK)
554                 return (ret);
555         return (s);
556 }