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: src/lib/libarchive/archive_write_set_format_ustar.c,v 1.12 2004/11/05 05:26:30 kientzle Exp $");
37 #include "archive_entry.h"
38 #include "archive_private.h"
41 uint64_t entry_bytes_remaining;
42 uint64_t entry_padding;
47 * Define structure of POSIX 'ustar' tar header.
49 struct archive_entry_header_ustar {
60 char mtime_padding[1];
64 char magic[6]; /* For POSIX: "ustar\0" */
65 char version[2]; /* For POSIX: "00" */
69 char rdevmajor_padding[2];
71 char rdevminor_padding[2];
77 * A filled-in copy of the header for initialization.
79 static const struct archive_entry_header_ustar template_header = {
81 { "000000" }, { ' ', '\0' }, /* mode, space-null termination. */
82 { "000000" }, { ' ', '\0' }, /* uid, space-null termination. */
83 { "000000" }, { ' ', '\0' }, /* gid, space-null termination. */
84 { "00000000000" }, { ' ' }, /* size, space termination. */
85 { "00000000000" }, { ' ' }, /* mtime, space termination. */
86 { " " }, /* Initial checksum value. */
87 { '0' }, /* default: regular file */
88 { "" }, /* linkname */
89 { "ustar" }, /* magic */
90 { '0', '0' }, /* version */
93 { "000000" }, { ' ', '\0' }, /* rdevmajor, space-null termination */
94 { "000000" }, { ' ', '\0' }, /* rdevminor, space-null termination */
99 static int archive_write_ustar_data(struct archive *a, const void *buff,
101 static int archive_write_ustar_finish(struct archive *);
102 static int archive_write_ustar_finish_entry(struct archive *);
103 static int archive_write_ustar_header(struct archive *,
104 struct archive_entry *entry);
105 static int format_256(int64_t, char *, int);
106 static int format_number(int64_t, char *, int size, int max, int strict);
107 static int format_octal(int64_t, char *, int);
108 static int write_nulls(struct archive *a, size_t);
111 * Set output format to 'ustar' format.
114 archive_write_set_format_ustar(struct archive *a)
118 /* If someone else was already registered, unregister them. */
119 if (a->format_finish != NULL)
120 (a->format_finish)(a);
122 ustar = malloc(sizeof(*ustar));
124 archive_set_error(a, ENOMEM, "Can't allocate ustar data");
125 return (ARCHIVE_FATAL);
127 memset(ustar, 0, sizeof(*ustar));
128 a->format_data = ustar;
130 a->pad_uncompressed = 1; /* Mimic gtar in this respect. */
131 a->format_write_header = archive_write_ustar_header;
132 a->format_write_data = archive_write_ustar_data;
133 a->format_finish = archive_write_ustar_finish;
134 a->format_finish_entry = archive_write_ustar_finish_entry;
135 a->archive_format = ARCHIVE_FORMAT_TAR_USTAR;
136 a->archive_format_name = "POSIX ustar";
141 archive_write_ustar_header(struct archive *a, struct archive_entry *entry)
147 ustar = a->format_data;
150 /* Only regular files (not hardlinks) have data. */
151 if (archive_entry_hardlink(entry) != NULL ||
152 archive_entry_symlink(entry) != NULL ||
153 !S_ISREG(archive_entry_mode(entry)))
154 archive_entry_set_size(entry, 0);
156 ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1);
157 if (ret != ARCHIVE_OK)
159 ret = (a->compression_write)(a, buff, 512);
160 if (ret != ARCHIVE_OK)
163 ustar->entry_bytes_remaining = archive_entry_size(entry);
164 ustar->entry_padding = 0x1ff & (- ustar->entry_bytes_remaining);
169 * Format a basic 512-byte "ustar" header.
171 * Returns -1 if format failed (due to field overflow).
172 * Note that this always formats as much of the header as possible.
173 * If "strict" is set to zero, it will extend numeric fields as
174 * necessary (overwriting terminators or using base-256 extensions).
176 * This is exported so that other 'tar' formats can use it.
179 __archive_write_format_header_ustar(struct archive *a, char buff[512],
180 struct archive_entry *entry, int tartype, int strict)
182 unsigned int checksum;
183 struct archive_entry_header_ustar *h;
187 const struct stat *st;
193 * The "template header" already includes the "ustar"
194 * signature, various end-of-field markers and other required
197 memcpy(buff, &template_header, 512);
199 h = (struct archive_entry_header_ustar *)buff;
202 * Because the block is already null-filled, and strings
203 * are allowed to exactly fill their destination (without null),
204 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
207 pp = archive_entry_pathname(entry);
208 if (strlen(pp) <= sizeof(h->name))
209 memcpy(h->name, pp, strlen(pp));
211 /* Store in two pieces, splitting at a '/'. */
212 p = strchr(pp + strlen(pp) - sizeof(h->name) - 1, '/');
214 * If there is no path separator, or the prefix or
215 * remaining name are too large, return an error.
218 archive_set_error(a, ENAMETOOLONG,
219 "Pathname too long");
221 } else if (p > pp + sizeof(h->prefix)) {
222 archive_set_error(a, ENAMETOOLONG,
223 "Pathname too long");
226 /* Copy prefix and remainder to appropriate places */
227 memcpy(h->prefix, pp, p - pp);
228 memcpy(h->name, p + 1, pp + strlen(pp) - p - 1);
232 p = archive_entry_hardlink(entry);
236 p = archive_entry_symlink(entry);
237 if (p != NULL && p[0] != '\0') {
238 copy_length = strlen(p);
239 if (copy_length > sizeof(h->linkname)) {
240 archive_set_error(a, ENAMETOOLONG,
241 "Link contents too long");
243 copy_length = sizeof(h->linkname);
245 memcpy(h->linkname, p, copy_length);
248 p = archive_entry_uname(entry);
249 if (p != NULL && p[0] != '\0') {
250 copy_length = strlen(p);
251 if (copy_length > sizeof(h->uname)) {
252 archive_set_error(a, ARCHIVE_ERRNO_MISC,
253 "Username too long");
255 copy_length = sizeof(h->uname);
257 memcpy(h->uname, p, copy_length);
260 p = archive_entry_gname(entry);
261 if (p != NULL && p[0] != '\0') {
262 copy_length = strlen(p);
263 if (strlen(p) > sizeof(h->gname)) {
264 archive_set_error(a, ARCHIVE_ERRNO_MISC,
265 "Group name too long");
267 copy_length = sizeof(h->gname);
269 memcpy(h->gname, p, copy_length);
272 st = archive_entry_stat(entry);
274 if (format_number(st->st_mode & 07777, h->mode, sizeof(h->mode), 8, strict)) {
275 archive_set_error(a, ERANGE, "Numeric mode too large");
279 if (format_number(st->st_uid, h->uid, sizeof(h->uid), 8, strict)) {
280 archive_set_error(a, ERANGE, "Numeric user ID too large");
284 if (format_number(st->st_gid, h->gid, sizeof(h->gid), 8, strict)) {
285 archive_set_error(a, ERANGE, "Numeric group ID too large");
289 if (format_number(st->st_size, h->size, sizeof(h->size), 12, strict)) {
290 archive_set_error(a, ERANGE, "File size out of range");
294 if (format_number(st->st_mtime, h->mtime, sizeof(h->mtime), 12, strict)) {
295 archive_set_error(a, ERANGE,
296 "File modification time too large");
300 if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
301 if (format_number(major(st->st_rdev), h->rdevmajor,
302 sizeof(h->rdevmajor), 8, strict)) {
303 archive_set_error(a, ERANGE,
304 "Major device number too large");
308 if (format_number(minor(st->st_rdev), h->rdevminor,
309 sizeof(h->rdevminor), 8, strict)) {
310 archive_set_error(a, ERANGE,
311 "Minor device number too large");
317 h->typeflag[0] = tartype;
318 } else if (mytartype >= 0) {
319 h->typeflag[0] = mytartype;
321 switch (st->st_mode & S_IFMT) {
322 case S_IFREG: h->typeflag[0] = '0' ; break;
323 case S_IFLNK: h->typeflag[0] = '2' ; break;
324 case S_IFCHR: h->typeflag[0] = '3' ; break;
325 case S_IFBLK: h->typeflag[0] = '4' ; break;
326 case S_IFDIR: h->typeflag[0] = '5' ; break;
327 case S_IFIFO: h->typeflag[0] = '6' ; break;
329 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
330 "tar format cannot archive socket");
334 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
335 "tar format cannot archive this (mode=0%lo)",
336 (unsigned long)st->st_mode);
342 for (i = 0; i < 512; i++)
343 checksum += 255 & (unsigned int)buff[i];
344 h->checksum[6] = '\0'; /* Can't be pre-set in the template. */
345 /* h->checksum[7] = ' '; */ /* This is pre-set in the template. */
346 format_octal(checksum, h->checksum, 6);
351 * Format a number into a field, with some intelligence.
354 format_number(int64_t v, char *p, int s, int maxsize, int strict)
358 limit = ((int64_t)1 << (s*3));
360 /* "Strict" only permits octal values with proper termination. */
362 return (format_octal(v, p, s));
365 * In non-strict mode, we allow the number to overwrite one or
366 * more bytes of the field termination. Even old tar
367 * implementations should be able to handle this with no
371 while (s <= maxsize) {
373 return (format_octal(v, p, s));
379 /* Base-256 can handle any number, positive or negative. */
380 return (format_256(v, p, maxsize));
384 * Format a number into the specified field using base-256.
387 format_256(int64_t v, char *p, int s)
391 *--p = (char)(v & 0xff);
394 *p |= 0x80; /* Set the base-256 marker bit. */
399 * Format a number into the specified field.
402 format_octal(int64_t v, char *p, int s)
408 /* Octal values can't be negative, so use 0. */
415 p += s; /* Start at the end and work backwards. */
417 *--p = '0' + (v & 7);
424 /* If it overflowed, fill field with max value. */
432 archive_write_ustar_finish(struct archive *a)
438 ustar = a->format_data;
440 * Suppress end-of-archive if nothing else was ever written.
441 * This fixes a problem where setting one format, then another
442 * ends up writing a gratuitous end-of-archive marker.
444 if (ustar->written && a->compression_write != NULL)
445 r = write_nulls(a, 512*2);
447 a->format_data = NULL;
452 archive_write_ustar_finish_entry(struct archive *a)
457 ustar = a->format_data;
459 ustar->entry_bytes_remaining + ustar->entry_padding);
460 ustar->entry_bytes_remaining = ustar->entry_padding = 0;
465 write_nulls(struct archive *a, size_t padding)
469 while (padding > 0) {
470 to_write = padding < a->null_length ? padding : a->null_length;
471 ret = (a->compression_write)(a, a->nulls, to_write);
472 if (ret != ARCHIVE_OK)
480 archive_write_ustar_data(struct archive *a, const void *buff, size_t s)
485 ustar = a->format_data;
486 if (s > ustar->entry_bytes_remaining)
487 s = ustar->entry_bytes_remaining;
488 ret = (a->compression_write)(a, buff, s);
489 ustar->entry_bytes_remaining -= s;