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