2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
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.
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.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 #ifdef HAVE_SYS_STAT_H
34 #include <sys/mkdev.h>
36 #ifdef MAJOR_IN_SYSMACROS
37 #include <sys/sysmacros.h>
52 #include "archive_entry.h"
53 #include "archive_private.h"
56 uint64_t entry_bytes_remaining;
57 uint64_t entry_padding;
62 * Define structure of POSIX 'ustar' tar header.
64 struct archive_entry_header_ustar {
75 char mtime_padding[1];
79 char magic[6]; /* For POSIX: "ustar\0" */
80 char version[2]; /* For POSIX: "00" */
84 char rdevmajor_padding[2];
86 char rdevminor_padding[2];
92 * A filled-in copy of the header for initialization.
94 static const struct archive_entry_header_ustar template_header = {
96 { '0','0','0','0','0','0' }, { ' ', '\0' }, /* mode, space-null term.*/
97 { '0','0','0','0','0','0' }, { ' ', '\0' }, /* uid, space-null term. */
98 { '0','0','0','0','0','0' }, { ' ', '\0' }, /* gid, space-null term. */
99 { '0','0','0','0','0','0','0','0','0','0','0' }, { ' ' },
100 /* size, space termination. */
101 { '0','0','0','0','0','0','0','0','0','0','0' }, { ' ' },
102 /* mtime, space termination. */
103 { ' ',' ',' ',' ',' ',' ',' ',' ' }, /* Initial checksum value. */
104 { '0' }, /* default: regular file */
105 { "" }, /* linkname */
106 { 'u','s','t','a','r' }, /* magic */
107 { '0', '0' }, /* version */
110 { '0','0','0','0','0','0' }, { ' ', '\0' }, /* rdevmajor, space-null */
111 { '0','0','0','0','0','0' }, { ' ', '\0' }, /* rdevminor, space-null */
116 static ssize_t archive_write_ustar_data(struct archive *a, const void *buff,
118 static int archive_write_ustar_finish(struct archive *);
119 static int archive_write_ustar_finish_entry(struct archive *);
120 static int archive_write_ustar_header(struct archive *,
121 struct archive_entry *entry);
122 static int format_256(int64_t, char *, int);
123 static int format_number(int64_t, char *, int size, int max, int strict);
124 static int format_octal(int64_t, char *, int);
125 static int write_nulls(struct archive *a, size_t);
128 * Set output format to 'ustar' format.
131 archive_write_set_format_ustar(struct archive *a)
135 /* If someone else was already registered, unregister them. */
136 if (a->format_finish != NULL)
137 (a->format_finish)(a);
139 ustar = (struct ustar *)malloc(sizeof(*ustar));
141 archive_set_error(a, ENOMEM, "Can't allocate ustar data");
142 return (ARCHIVE_FATAL);
144 memset(ustar, 0, sizeof(*ustar));
145 a->format_data = ustar;
147 a->pad_uncompressed = 1; /* Mimic gtar in this respect. */
148 a->format_write_header = archive_write_ustar_header;
149 a->format_write_data = archive_write_ustar_data;
150 a->format_finish = archive_write_ustar_finish;
151 a->format_finish_entry = archive_write_ustar_finish_entry;
152 a->archive_format = ARCHIVE_FORMAT_TAR_USTAR;
153 a->archive_format_name = "POSIX ustar";
158 archive_write_ustar_header(struct archive *a, struct archive_entry *entry)
164 ustar = (struct ustar *)a->format_data;
167 /* Only regular files (not hardlinks) have data. */
168 if (archive_entry_hardlink(entry) != NULL ||
169 archive_entry_symlink(entry) != NULL ||
170 !S_ISREG(archive_entry_mode(entry)))
171 archive_entry_set_size(entry, 0);
173 ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1);
174 if (ret != ARCHIVE_OK)
176 ret = (a->compression_write)(a, buff, 512);
177 if (ret != ARCHIVE_OK)
180 ustar->entry_bytes_remaining = archive_entry_size(entry);
181 ustar->entry_padding = 0x1ff & (- ustar->entry_bytes_remaining);
186 * Format a basic 512-byte "ustar" header.
188 * Returns -1 if format failed (due to field overflow).
189 * Note that this always formats as much of the header as possible.
190 * If "strict" is set to zero, it will extend numeric fields as
191 * necessary (overwriting terminators or using base-256 extensions).
193 * This is exported so that other 'tar' formats can use it.
196 __archive_write_format_header_ustar(struct archive *a, char buff[512],
197 struct archive_entry *entry, int tartype, int strict)
199 unsigned int checksum;
200 struct archive_entry_header_ustar *h;
204 const struct stat *st;
210 * The "template header" already includes the "ustar"
211 * signature, various end-of-field markers and other required
214 memcpy(buff, &template_header, 512);
216 h = (struct archive_entry_header_ustar *)buff;
219 * Because the block is already null-filled, and strings
220 * are allowed to exactly fill their destination (without null),
221 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
224 pp = archive_entry_pathname(entry);
225 if (strlen(pp) <= sizeof(h->name))
226 memcpy(h->name, pp, strlen(pp));
228 /* Store in two pieces, splitting at a '/'. */
229 p = strchr(pp + strlen(pp) - sizeof(h->name) - 1, '/');
231 * If there is no path separator, or the prefix or
232 * remaining name are too large, return an error.
235 archive_set_error(a, ENAMETOOLONG,
236 "Pathname too long");
238 } else if (p > pp + sizeof(h->prefix)) {
239 archive_set_error(a, ENAMETOOLONG,
240 "Pathname too long");
243 /* Copy prefix and remainder to appropriate places */
244 memcpy(h->prefix, pp, p - pp);
245 memcpy(h->name, p + 1, pp + strlen(pp) - p - 1);
249 p = archive_entry_hardlink(entry);
253 p = archive_entry_symlink(entry);
254 if (p != NULL && p[0] != '\0') {
255 copy_length = strlen(p);
256 if (copy_length > sizeof(h->linkname)) {
257 archive_set_error(a, ENAMETOOLONG,
258 "Link contents too long");
260 copy_length = sizeof(h->linkname);
262 memcpy(h->linkname, p, copy_length);
265 p = archive_entry_uname(entry);
266 if (p != NULL && p[0] != '\0') {
267 copy_length = strlen(p);
268 if (copy_length > sizeof(h->uname)) {
269 archive_set_error(a, ARCHIVE_ERRNO_MISC,
270 "Username too long");
272 copy_length = sizeof(h->uname);
274 memcpy(h->uname, p, copy_length);
277 p = archive_entry_gname(entry);
278 if (p != NULL && p[0] != '\0') {
279 copy_length = strlen(p);
280 if (strlen(p) > sizeof(h->gname)) {
281 archive_set_error(a, ARCHIVE_ERRNO_MISC,
282 "Group name too long");
284 copy_length = sizeof(h->gname);
286 memcpy(h->gname, p, copy_length);
289 st = archive_entry_stat(entry);
291 if (format_number(st->st_mode & 07777, h->mode, sizeof(h->mode), 8, strict)) {
292 archive_set_error(a, ERANGE, "Numeric mode too large");
296 if (format_number(st->st_uid, h->uid, sizeof(h->uid), 8, strict)) {
297 archive_set_error(a, ERANGE, "Numeric user ID too large");
301 if (format_number(st->st_gid, h->gid, sizeof(h->gid), 8, strict)) {
302 archive_set_error(a, ERANGE, "Numeric group ID too large");
306 if (format_number(st->st_size, h->size, sizeof(h->size), 12, strict)) {
307 archive_set_error(a, ERANGE, "File size out of range");
311 if (format_number(st->st_mtime, h->mtime, sizeof(h->mtime), 12, strict)) {
312 archive_set_error(a, ERANGE,
313 "File modification time too large");
317 if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
318 if (format_number(major(st->st_rdev), h->rdevmajor,
319 sizeof(h->rdevmajor), 8, strict)) {
320 archive_set_error(a, ERANGE,
321 "Major device number too large");
325 if (format_number(minor(st->st_rdev), h->rdevminor,
326 sizeof(h->rdevminor), 8, strict)) {
327 archive_set_error(a, ERANGE,
328 "Minor device number too large");
334 h->typeflag[0] = tartype;
335 } else if (mytartype >= 0) {
336 h->typeflag[0] = mytartype;
338 switch (st->st_mode & S_IFMT) {
339 case S_IFREG: h->typeflag[0] = '0' ; break;
340 case S_IFLNK: h->typeflag[0] = '2' ; break;
341 case S_IFCHR: h->typeflag[0] = '3' ; break;
342 case S_IFBLK: h->typeflag[0] = '4' ; break;
343 case S_IFDIR: h->typeflag[0] = '5' ; break;
344 case S_IFIFO: h->typeflag[0] = '6' ; break;
346 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
347 "tar format cannot archive socket");
351 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
352 "tar format cannot archive this (mode=0%lo)",
353 (unsigned long)st->st_mode);
359 for (i = 0; i < 512; i++)
360 checksum += 255 & (unsigned int)buff[i];
361 h->checksum[6] = '\0'; /* Can't be pre-set in the template. */
362 /* h->checksum[7] = ' '; */ /* This is pre-set in the template. */
363 format_octal(checksum, h->checksum, 6);
368 * Format a number into a field, with some intelligence.
371 format_number(int64_t v, char *p, int s, int maxsize, int strict)
375 limit = ((int64_t)1 << (s*3));
377 /* "Strict" only permits octal values with proper termination. */
379 return (format_octal(v, p, s));
382 * In non-strict mode, we allow the number to overwrite one or
383 * more bytes of the field termination. Even old tar
384 * implementations should be able to handle this with no
388 while (s <= maxsize) {
390 return (format_octal(v, p, s));
396 /* Base-256 can handle any number, positive or negative. */
397 return (format_256(v, p, maxsize));
401 * Format a number into the specified field using base-256.
404 format_256(int64_t v, char *p, int s)
408 *--p = (char)(v & 0xff);
411 *p |= 0x80; /* Set the base-256 marker bit. */
416 * Format a number into the specified field.
419 format_octal(int64_t v, char *p, int s)
425 /* Octal values can't be negative, so use 0. */
432 p += s; /* Start at the end and work backwards. */
434 *--p = '0' + (v & 7);
441 /* If it overflowed, fill field with max value. */
449 archive_write_ustar_finish(struct archive *a)
455 ustar = (struct ustar *)a->format_data;
457 * Suppress end-of-archive if nothing else was ever written.
458 * This fixes a problem where setting one format, then another
459 * ends up writing a gratuitous end-of-archive marker.
461 if (ustar->written && a->compression_write != NULL)
462 r = write_nulls(a, 512*2);
464 a->format_data = NULL;
469 archive_write_ustar_finish_entry(struct archive *a)
474 ustar = (struct ustar *)a->format_data;
476 ustar->entry_bytes_remaining + ustar->entry_padding);
477 ustar->entry_bytes_remaining = ustar->entry_padding = 0;
482 write_nulls(struct archive *a, size_t padding)
486 while (padding > 0) {
487 to_write = padding < a->null_length ? padding : a->null_length;
488 ret = (a->compression_write)(a, a->nulls, to_write);
489 if (ret != ARCHIVE_OK)
497 archive_write_ustar_data(struct archive *a, const void *buff, size_t s)
502 ustar = (struct ustar *)a->format_data;
503 if (s > ustar->entry_bytes_remaining)
504 s = ustar->entry_bytes_remaining;
505 ret = (a->compression_write)(a, buff, s);
506 ustar->entry_bytes_remaining -= s;
507 if (ret != ARCHIVE_OK)