2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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$");
43 #include "archive_entry.h"
44 #include "archive_entry_locale.h"
45 #include "archive_private.h"
46 #include "archive_write_private.h"
47 #include "archive_write_set_format_private.h"
50 uint64_t entry_bytes_remaining;
51 uint64_t entry_padding;
53 struct archive_string_conv *opt_sconv;
54 struct archive_string_conv *sconv_default;
55 int init_default_conversion;
59 * Define structure of POSIX 'v7tar' tar header.
61 #define V7TAR_name_offset 0
62 #define V7TAR_name_size 100
63 #define V7TAR_mode_offset 100
64 #define V7TAR_mode_size 6
65 #define V7TAR_mode_max_size 8
66 #define V7TAR_uid_offset 108
67 #define V7TAR_uid_size 6
68 #define V7TAR_uid_max_size 8
69 #define V7TAR_gid_offset 116
70 #define V7TAR_gid_size 6
71 #define V7TAR_gid_max_size 8
72 #define V7TAR_size_offset 124
73 #define V7TAR_size_size 11
74 #define V7TAR_size_max_size 12
75 #define V7TAR_mtime_offset 136
76 #define V7TAR_mtime_size 11
77 #define V7TAR_mtime_max_size 12
78 #define V7TAR_checksum_offset 148
79 #define V7TAR_checksum_size 8
80 #define V7TAR_typeflag_offset 156
81 #define V7TAR_typeflag_size 1
82 #define V7TAR_linkname_offset 157
83 #define V7TAR_linkname_size 100
84 #define V7TAR_padding_offset 257
85 #define V7TAR_padding_size 255
88 * A filled-in copy of the header for initialization.
90 static const char template_header[] = {
92 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,
93 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,
94 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,
96 /* Mode, space-null termination: 8 bytes */
97 '0','0','0','0','0','0', ' ','\0',
98 /* uid, space-null termination: 8 bytes */
99 '0','0','0','0','0','0', ' ','\0',
100 /* gid, space-null termination: 8 bytes */
101 '0','0','0','0','0','0', ' ','\0',
102 /* size, space termination: 12 bytes */
103 '0','0','0','0','0','0','0','0','0','0','0', ' ',
104 /* mtime, space termination: 12 bytes */
105 '0','0','0','0','0','0','0','0','0','0','0', ' ',
106 /* Initial checksum value: 8 spaces */
107 ' ',' ',' ',' ',' ',' ',' ',' ',
108 /* Typeflag: 1 byte */
110 /* Linkname: 100 bytes */
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,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,
115 /* Padding: 255 bytes */
116 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,
117 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,
118 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,
119 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,
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,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
126 static ssize_t archive_write_v7tar_data(struct archive_write *a, const void *buff,
128 static int archive_write_v7tar_free(struct archive_write *);
129 static int archive_write_v7tar_close(struct archive_write *);
130 static int archive_write_v7tar_finish_entry(struct archive_write *);
131 static int archive_write_v7tar_header(struct archive_write *,
132 struct archive_entry *entry);
133 static int archive_write_v7tar_options(struct archive_write *,
134 const char *, const char *);
135 static int format_256(int64_t, char *, int);
136 static int format_number(int64_t, char *, int size, int max, int strict);
137 static int format_octal(int64_t, char *, int);
138 static int format_header_v7tar(struct archive_write *, char h[512],
139 struct archive_entry *, int, struct archive_string_conv *);
142 * Set output format to 'v7tar' format.
145 archive_write_set_format_v7tar(struct archive *_a)
147 struct archive_write *a = (struct archive_write *)_a;
150 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
151 ARCHIVE_STATE_NEW, "archive_write_set_format_v7tar");
153 /* If someone else was already registered, unregister them. */
154 if (a->format_free != NULL)
157 /* Basic internal sanity test. */
158 if (sizeof(template_header) != 512) {
159 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
160 "Internal: template_header wrong size: %zu should be 512",
161 sizeof(template_header));
162 return (ARCHIVE_FATAL);
165 v7tar = (struct v7tar *)calloc(1, sizeof(*v7tar));
167 archive_set_error(&a->archive, ENOMEM,
168 "Can't allocate v7tar data");
169 return (ARCHIVE_FATAL);
171 a->format_data = v7tar;
172 a->format_name = "tar (non-POSIX)";
173 a->format_options = archive_write_v7tar_options;
174 a->format_write_header = archive_write_v7tar_header;
175 a->format_write_data = archive_write_v7tar_data;
176 a->format_close = archive_write_v7tar_close;
177 a->format_free = archive_write_v7tar_free;
178 a->format_finish_entry = archive_write_v7tar_finish_entry;
179 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
180 a->archive.archive_format_name = "tar (non-POSIX)";
185 archive_write_v7tar_options(struct archive_write *a, const char *key,
188 struct v7tar *v7tar = (struct v7tar *)a->format_data;
189 int ret = ARCHIVE_FAILED;
191 if (strcmp(key, "hdrcharset") == 0) {
192 if (val == NULL || val[0] == 0)
193 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
194 "%s: hdrcharset option needs a character-set name",
197 v7tar->opt_sconv = archive_string_conversion_to_charset(
198 &a->archive, val, 0);
199 if (v7tar->opt_sconv != NULL)
207 /* Note: The "warn" return is just to inform the options
208 * supervisor that we didn't handle it. It will generate
209 * a suitable error if no one used this option. */
210 return (ARCHIVE_WARN);
214 archive_write_v7tar_header(struct archive_write *a, struct archive_entry *entry)
219 struct archive_entry *entry_main;
220 struct archive_string_conv *sconv;
222 v7tar = (struct v7tar *)a->format_data;
224 /* Setup default string conversion. */
225 if (v7tar->opt_sconv == NULL) {
226 if (!v7tar->init_default_conversion) {
227 v7tar->sconv_default =
228 archive_string_default_conversion_for_write(
230 v7tar->init_default_conversion = 1;
232 sconv = v7tar->sconv_default;
234 sconv = v7tar->opt_sconv;
237 if (archive_entry_pathname(entry) == NULL) {
238 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
239 "Can't record entry in tar file without pathname");
240 return (ARCHIVE_FAILED);
243 /* Only regular files (not hardlinks) have data. */
244 if (archive_entry_hardlink(entry) != NULL ||
245 archive_entry_symlink(entry) != NULL ||
246 !(archive_entry_filetype(entry) == AE_IFREG))
247 archive_entry_set_size(entry, 0);
249 if (AE_IFDIR == archive_entry_filetype(entry)) {
253 * Ensure a trailing '/'. Modify the entry so
254 * the client sees the change.
256 #if defined(_WIN32) && !defined(__CYGWIN__)
259 wp = archive_entry_pathname_w(entry);
260 if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
261 struct archive_wstring ws;
263 archive_string_init(&ws);
264 path_length = wcslen(wp);
265 if (archive_wstring_ensure(&ws,
266 path_length + 2) == NULL) {
267 archive_set_error(&a->archive, ENOMEM,
268 "Can't allocate v7tar data");
269 archive_wstring_free(&ws);
270 return(ARCHIVE_FATAL);
272 /* Should we keep '\' ? */
273 if (wp[path_length -1] == L'\\')
275 archive_wstrncpy(&ws, wp, path_length);
276 archive_wstrappend_wchar(&ws, L'/');
277 archive_entry_copy_pathname_w(entry, ws.s);
278 archive_wstring_free(&ws);
282 p = archive_entry_pathname(entry);
284 * On Windows, this is a backup operation just in
285 * case getting WCS failed. On POSIX, this is a
288 if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
289 struct archive_string as;
291 archive_string_init(&as);
292 path_length = strlen(p);
293 if (archive_string_ensure(&as,
294 path_length + 2) == NULL) {
295 archive_set_error(&a->archive, ENOMEM,
296 "Can't allocate v7tar data");
297 archive_string_free(&as);
298 return(ARCHIVE_FATAL);
300 #if defined(_WIN32) && !defined(__CYGWIN__)
301 /* NOTE: This might break the pathname
302 * if the current code page is CP932 and
303 * the pathname includes a character '\'
304 * as a part of its multibyte pathname. */
305 if (p[strlen(p) -1] == '\\')
309 archive_strncpy(&as, p, path_length);
310 archive_strappend_char(&as, '/');
311 archive_entry_copy_pathname(entry, as.s);
312 archive_string_free(&as);
316 #if defined(_WIN32) && !defined(__CYGWIN__)
317 /* Make sure the path separators in pathname, hardlink and symlink
318 * are all slash '/', not the Windows path separator '\'. */
319 entry_main = __la_win_entry_in_posix_pathseparator(entry);
320 if (entry_main == NULL) {
321 archive_set_error(&a->archive, ENOMEM,
322 "Can't allocate v7tar data");
323 return(ARCHIVE_FATAL);
325 if (entry != entry_main)
332 ret = format_header_v7tar(a, buff, entry, 1, sconv);
333 if (ret < ARCHIVE_WARN) {
334 archive_entry_free(entry_main);
337 ret2 = __archive_write_output(a, buff, 512);
338 if (ret2 < ARCHIVE_WARN) {
339 archive_entry_free(entry_main);
345 v7tar->entry_bytes_remaining = archive_entry_size(entry);
346 v7tar->entry_padding = 0x1ff & (-(int64_t)v7tar->entry_bytes_remaining);
347 archive_entry_free(entry_main);
352 * Format a basic 512-byte "v7tar" header.
354 * Returns -1 if format failed (due to field overflow).
355 * Note that this always formats as much of the header as possible.
356 * If "strict" is set to zero, it will extend numeric fields as
357 * necessary (overwriting terminators or using base-256 extensions).
361 format_header_v7tar(struct archive_write *a, char h[512],
362 struct archive_entry *entry, int strict,
363 struct archive_string_conv *sconv)
365 unsigned int checksum;
374 * The "template header" already includes the "v7tar"
375 * signature, various end-of-field markers and other required
378 memcpy(h, &template_header, 512);
381 * Because the block is already null-filled, and strings
382 * are allowed to exactly fill their destination (without null),
383 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
385 r = archive_entry_pathname_l(entry, &pp, ©_length, sconv);
387 if (errno == ENOMEM) {
388 archive_set_error(&a->archive, ENOMEM,
389 "Can't allocate memory for Pathname");
390 return (ARCHIVE_FATAL);
392 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
393 "Can't translate pathname '%s' to %s",
394 pp, archive_string_conversion_charset_name(sconv));
397 if (strict && copy_length < V7TAR_name_size)
398 memcpy(h + V7TAR_name_offset, pp, copy_length);
399 else if (!strict && copy_length <= V7TAR_name_size)
400 memcpy(h + V7TAR_name_offset, pp, copy_length);
402 /* Prefix is too long. */
403 archive_set_error(&a->archive, ENAMETOOLONG,
404 "Pathname too long");
405 ret = ARCHIVE_FAILED;
408 r = archive_entry_hardlink_l(entry, &p, ©_length, sconv);
410 if (errno == ENOMEM) {
411 archive_set_error(&a->archive, ENOMEM,
412 "Can't allocate memory for Linkname");
413 return (ARCHIVE_FATAL);
415 archive_set_error(&a->archive,
416 ARCHIVE_ERRNO_FILE_FORMAT,
417 "Can't translate linkname '%s' to %s",
418 p, archive_string_conversion_charset_name(sconv));
424 r = archive_entry_symlink_l(entry, &p, ©_length, sconv);
426 if (errno == ENOMEM) {
427 archive_set_error(&a->archive, ENOMEM,
428 "Can't allocate memory for Linkname");
429 return (ARCHIVE_FATAL);
431 archive_set_error(&a->archive,
432 ARCHIVE_ERRNO_FILE_FORMAT,
433 "Can't translate linkname '%s' to %s",
434 p, archive_string_conversion_charset_name(sconv));
438 if (copy_length > 0) {
439 if (copy_length >= V7TAR_linkname_size) {
440 archive_set_error(&a->archive, ENAMETOOLONG,
441 "Link contents too long");
442 ret = ARCHIVE_FAILED;
443 copy_length = V7TAR_linkname_size;
445 memcpy(h + V7TAR_linkname_offset, p, copy_length);
448 if (format_number(archive_entry_mode(entry) & 07777,
449 h + V7TAR_mode_offset, V7TAR_mode_size,
450 V7TAR_mode_max_size, strict)) {
451 archive_set_error(&a->archive, ERANGE,
452 "Numeric mode too large");
453 ret = ARCHIVE_FAILED;
456 if (format_number(archive_entry_uid(entry),
457 h + V7TAR_uid_offset, V7TAR_uid_size, V7TAR_uid_max_size, strict)) {
458 archive_set_error(&a->archive, ERANGE,
459 "Numeric user ID too large");
460 ret = ARCHIVE_FAILED;
463 if (format_number(archive_entry_gid(entry),
464 h + V7TAR_gid_offset, V7TAR_gid_size, V7TAR_gid_max_size, strict)) {
465 archive_set_error(&a->archive, ERANGE,
466 "Numeric group ID too large");
467 ret = ARCHIVE_FAILED;
470 if (format_number(archive_entry_size(entry),
471 h + V7TAR_size_offset, V7TAR_size_size,
472 V7TAR_size_max_size, strict)) {
473 archive_set_error(&a->archive, ERANGE,
474 "File size out of range");
475 ret = ARCHIVE_FAILED;
478 if (format_number(archive_entry_mtime(entry),
479 h + V7TAR_mtime_offset, V7TAR_mtime_size,
480 V7TAR_mtime_max_size, strict)) {
481 archive_set_error(&a->archive, ERANGE,
482 "File modification time too large");
483 ret = ARCHIVE_FAILED;
486 if (mytartype >= 0) {
487 h[V7TAR_typeflag_offset] = mytartype;
489 switch (archive_entry_filetype(entry)) {
490 case AE_IFREG: case AE_IFDIR:
493 h[V7TAR_typeflag_offset] = '2';
496 /* AE_IFBLK, AE_IFCHR, AE_IFIFO, AE_IFSOCK
498 __archive_write_entry_filetype_unsupported(
499 &a->archive, entry, "v7tar");
500 ret = ARCHIVE_FAILED;
505 for (i = 0; i < 512; i++)
506 checksum += 255 & (unsigned int)h[i];
507 format_octal(checksum, h + V7TAR_checksum_offset, 6);
508 /* Can't be pre-set in the template. */
509 h[V7TAR_checksum_offset + 6] = '\0';
514 * Format a number into a field, with some intelligence.
517 format_number(int64_t v, char *p, int s, int maxsize, int strict)
521 limit = ((int64_t)1 << (s*3));
523 /* "Strict" only permits octal values with proper termination. */
525 return (format_octal(v, p, s));
528 * In non-strict mode, we allow the number to overwrite one or
529 * more bytes of the field termination. Even old tar
530 * implementations should be able to handle this with no
534 while (s <= maxsize) {
536 return (format_octal(v, p, s));
542 /* Base-256 can handle any number, positive or negative. */
543 return (format_256(v, p, maxsize));
547 * Format a number into the specified field using base-256.
550 format_256(int64_t v, char *p, int s)
554 *--p = (char)(v & 0xff);
557 *p |= 0x80; /* Set the base-256 marker bit. */
562 * Format a number into the specified field.
565 format_octal(int64_t v, char *p, int s)
571 /* Octal values can't be negative, so use 0. */
578 p += s; /* Start at the end and work backwards. */
580 *--p = (char)('0' + (v & 7));
587 /* If it overflowed, fill field with max value. */
595 archive_write_v7tar_close(struct archive_write *a)
597 return (__archive_write_nulls(a, 512*2));
601 archive_write_v7tar_free(struct archive_write *a)
605 v7tar = (struct v7tar *)a->format_data;
607 a->format_data = NULL;
612 archive_write_v7tar_finish_entry(struct archive_write *a)
617 v7tar = (struct v7tar *)a->format_data;
618 ret = __archive_write_nulls(a,
619 (size_t)(v7tar->entry_bytes_remaining + v7tar->entry_padding));
620 v7tar->entry_bytes_remaining = v7tar->entry_padding = 0;
625 archive_write_v7tar_data(struct archive_write *a, const void *buff, size_t s)
630 v7tar = (struct v7tar *)a->format_data;
631 if (s > v7tar->entry_bytes_remaining)
632 s = (size_t)v7tar->entry_bytes_remaining;
633 ret = __archive_write_output(a, buff, s);
634 v7tar->entry_bytes_remaining -= s;
635 if (ret != ARCHIVE_OK)