/* POSIX extended headers for tar.
- Copyright (C) 2003, 2004 Free Software Foundation, Inc.
+ Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
- 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
-#include "system.h"
+#include <system.h>
#include <fnmatch.h>
#include <hash.h>
+#include <inttostr.h>
#include <quotearg.h>
#include <stpcpy.h>
-#include <xstrtol.h>
#include "common.h"
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free free
-#include <obstack.h>
-
#include <fnmatch.h>
+#if !HAVE_DECL_STRTOIMAX && !defined strtoimax
+intmax_t strtoimax ();
+#endif
+#if !HAVE_DECL_STRTOUMAX && !defined strtoumax
+uintmax_t strtoumax ();
+#endif
+
static bool xheader_protected_pattern_p (char const *pattern);
static bool xheader_protected_keyword_p (char const *keyword);
static void xheader_set_single_keyword (char *) __attribute__ ((noreturn));
However it should wait until buffer.c is finally rewritten */
+enum { BILLION = 1000000000, LOG10_BILLION = 9 };
+
\f
/* Keyword options */
if (strcmp (kw, "delete") == 0)
{
if (xheader_protected_pattern_p (p))
- USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), p));
+ USAGE_ERROR ((0, 0, _("Pattern %s cannot be used"), quote (p)));
xheader_list_append (&keyword_pattern_list, p, NULL);
}
else if (strcmp (kw, "exthdr.name") == 0)
}
}
-static void
-to_decimal (uintmax_t value, char *where, size_t size)
-{
- size_t i = 0, j;
-
- where[i++] = 0;
- do
- {
- where[i++] = '0' + value % 10;
- value /= 10;
- }
- while (i < size && value);
- for (j = 0, i--; j < i; j++, i--)
- {
- char c = where[j];
- where[j] = where[i];
- where[i] = c;
- }
-}
-
/*
string Includes: Replaced By:
%d The directory name of the file,
equivalent to the result of the
dirname utility on the translated
- pathname.
+ file name.
%f The filename of the file, equivalent
to the result of the basename
- utility on the translated pathname.
+ utility on the translated file name.
%p The process ID of the pax process.
%% A '%' character. */
const char *p;
char *dir = NULL;
char *base = NULL;
- char pidbuf[64];
- char nbuf[64];
+ char pidbuf[UINTMAX_STRSIZE_BOUND];
+ char const *pptr;
+ char nbuf[UINTMAX_STRSIZE_BOUND];
+ char const *nptr = NULL;
for (p = fmt; *p && (p = strchr (p, '%')); )
{
case 'd':
if (st)
{
- dir = safer_name_suffix (dir_name (st->orig_file_name), false);
+ dir = safer_name_suffix (dir_name (st->orig_file_name),
+ false, absolute_names_option);
len += strlen (dir) - 1;
}
break;
break;
case 'p':
- to_decimal (getpid (), pidbuf, sizeof pidbuf);
- len += strlen (pidbuf) - 1;
+ pptr = umaxtostr (getpid (), pidbuf);
+ len += pidbuf + sizeof pidbuf - 1 - pptr - 1;
break;
case 'n':
if (allow_n)
{
- to_decimal (global_header_count + 1, pidbuf, sizeof pidbuf);
- len += strlen (nbuf) - 1;
+ nptr = umaxtostr (global_header_count + 1, nbuf);
+ len += nbuf + sizeof nbuf - 1 - nptr - 1;
}
break;
}
break;
case 'p':
- q = stpcpy (q, pidbuf);
+ q = stpcpy (q, pptr);
p += 2;
break;
case 'n':
- if (allow_n)
+ if (nptr)
{
- q = stpcpy (q, nbuf);
+ q = stpcpy (q, nptr);
p += 2;
}
/* else fall through */
return false;
}
-/* Decodes a single extended header record. Advances P to the next
- record.
- Returns true on success, false otherwise. */
+/* Decode a single extended header record, advancing *PTR to the next record.
+ Return true on success, false otherwise. */
static bool
-decode_record (char **p,
+decode_record (char **ptr,
void (*handler) (void *, char const *, char const *),
void *data)
{
- size_t len;
+ char *start = *ptr;
+ char *p = start;
+ unsigned long int len;
+ char *len_lim;
char const *keyword;
- char *start = *p;
- char endc;
+ char *nextp;
+ size_t len_max = extended_header.buffer + extended_header.size - start;
- if (**p == 0)
- return false;
+ while (*p == ' ' || *p == '\t')
+ p++;
- len = strtoul (*p, p, 10);
- if (**p != ' ')
+ if (! ISDIGIT (*p))
{
- ERROR ((0, 0,
- _("Malformed extended header: missing whitespace after the length")));
+ if (*p)
+ ERROR ((0, 0, _("Malformed extended header: missing length")));
return false;
}
- keyword = ++*p;
- for (;*p < start + len; ++*p)
- if (**p == '=')
- break;
+ errno = 0;
+ len = strtoul (p, &len_lim, 10);
- if (**p != '=')
+ if (len_max < len)
{
- ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
+ int len_len = len_lim - p;
+ ERROR ((0, 0, _("Extended header length %*s is out of range"),
+ len_len, p));
return false;
}
- **p = 0;
+ nextp = start + len;
- endc = start[len-1];
- start[len-1] = 0;
+ for (p = len_lim; *p == ' ' || *p == '\t'; p++)
+ continue;
+ if (p == len_lim)
+ {
+ ERROR ((0, 0,
+ _("Malformed extended header: missing blank after length")));
+ return false;
+ }
+
+ keyword = p;
+ p = strchr (p, '=');
+ if (! (p && p < nextp))
+ {
+ ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
+ return false;
+ }
- handler (data, keyword, *p + 1);
+ if (nextp[-1] != '\n')
+ {
+ ERROR ((0, 0, _("Malformed extended header: missing newline")));
+ return false;
+ }
- start[len-1] = endc;
- **p = '=';
- *p = &start[len];
+ *p = nextp[-1] = '\0';
+ handler (data, keyword, p + 1);
+ *p = '=';
+ nextp[-1] = '\n';
+ *ptr = nextp;
return true;
}
if (extended_header.size)
{
char *p = extended_header.buffer + BLOCKSIZE;
- char *endp = &extended_header.buffer[extended_header.size-1];
-
- while (p < endp)
- if (!decode_record (&p, decx, st))
- break;
+ while (decode_record (&p, decx, st))
+ continue;
}
run_override_list (keyword_override_list, st);
}
if (extended_header.size)
{
char *p = extended_header.buffer + BLOCKSIZE;
- char *endp = &extended_header.buffer[extended_header.size-1];
xheader_list_destroy (&global_header_override_list);
- while (p < endp)
- if (!decode_record (&p, decg, &global_header_override_list))
- break;
+ while (decode_record (&p, decg, &global_header_override_list))
+ continue;
}
}
extended_header.size = size;
nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE;
extended_header.buffer = xmalloc (size + 1);
+ extended_header.buffer[size] = '\0';
do
{
while (size > 0);
}
-static size_t
-format_uintmax (uintmax_t val, char *buf, size_t s)
-{
- if (!buf)
- {
- s = 0;
- do
- s++;
- while ((val /= 10) != 0);
- }
- else
- {
- char *p = buf + s - 1;
-
- do
- {
- *p-- = val % 10 + '0';
- }
- while ((val /= 10) != 0);
-
- while (p >= buf)
- *p-- = '0';
- }
- return s;
-}
-
static void
xheader_print (struct xheader *xhdr, char const *keyword, char const *value)
{
size_t len = strlen (keyword) + strlen (value) + 3; /* ' ' + '=' + '\n' */
- size_t p, n = 0;
- char nbuf[100];
+ size_t p;
+ size_t n = 0;
+ char nbuf[UINTMAX_STRSIZE_BOUND];
+ char const *np;
do
{
p = n;
- n = format_uintmax (len + p, NULL, 0);
+ np = umaxtostr (len + p, nbuf);
+ n = nbuf + sizeof nbuf - 1 - np;
}
while (n != p);
- format_uintmax (len + n, nbuf, n);
- obstack_grow (xhdr->stk, nbuf, n);
+ obstack_grow (xhdr->stk, np, n);
obstack_1grow (xhdr->stk, ' ');
obstack_grow (xhdr->stk, keyword, strlen (keyword));
obstack_1grow (xhdr->stk, '=');
\f
/* Implementations */
+
+static void
+out_of_range_header (char const *keyword, char const *value,
+ uintmax_t minus_minval, uintmax_t maxval)
+{
+ char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
+ char maxval_buf[UINTMAX_STRSIZE_BOUND];
+ char *minval_string = umaxtostr (minus_minval, minval_buf + 1);
+ char *maxval_string = umaxtostr (maxval, maxval_buf);
+ if (minus_minval)
+ *--minval_string = '-';
+
+ /* TRANSLATORS: The first %s is the pax extended header keyword
+ (atime, gid, etc.). */
+ ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
+ keyword, value, minval_string, maxval_string));
+}
+
static void
code_string (char const *string, char const *keyword, struct xheader *xhdr)
{
}
static void
-code_time (time_t t, unsigned long nano,
- char const *keyword, struct xheader *xhdr)
+code_time (struct timespec t, char const *keyword, struct xheader *xhdr)
{
- char sbuf[200];
- size_t s = format_uintmax (t, NULL, 0);
- if (s + 11 >= sizeof sbuf)
- return;
- format_uintmax (t, sbuf, s);
- sbuf[s++] = '.';
- s += format_uintmax (nano, sbuf + s, 9);
- sbuf[s] = 0;
- xheader_print (xhdr, keyword, sbuf);
+ time_t s = t.tv_sec;
+ int ns = t.tv_nsec;
+ char sbuf[1/*"-"*/ + UINTMAX_STRSIZE_BOUND + 1/*"."*/ + LOG10_BILLION];
+ char *np;
+ bool negative = s < 0;
+
+ if (negative && ns != 0)
+ {
+ s++;
+ ns = BILLION - ns;
+ }
+
+ np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
+ if (negative)
+ *--np = '-';
+ code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
+ xheader_print (xhdr, keyword, np);
}
-static void
-decode_time (char const *arg, time_t *secs, unsigned long *nsecs)
+static bool
+decode_time (struct timespec *ts, char const *arg, char const *keyword)
{
- uintmax_t u;
+ time_t s;
+ unsigned long int ns = 0;
char *p;
- if (xstrtoumax (arg, &p, 10, &u, "") == LONGINT_OK)
+ char *arg_lim;
+ bool negative = *arg == '-';
+
+ errno = 0;
+
+ if (ISDIGIT (arg[negative]))
{
- *secs = u;
- if (*p == '.' && xstrtoumax (p+1, NULL, 10, &u, "") == LONGINT_OK)
- *nsecs = u;
+ if (negative)
+ {
+ intmax_t i = strtoimax (arg, &arg_lim, 10);
+ if (TYPE_SIGNED (time_t) ? i < TYPE_MINIMUM (time_t) : i < 0)
+ goto out_of_range;
+ s = i;
+ }
+ else
+ {
+ uintmax_t i = strtoumax (arg, &arg_lim, 10);
+ if (TYPE_MAXIMUM (time_t) < i)
+ goto out_of_range;
+ s = i;
+ }
+
+ p = arg_lim;
+
+ if (errno == ERANGE)
+ goto out_of_range;
+
+ if (*p == '.')
+ {
+ int digits = 0;
+ bool trailing_nonzero = false;
+
+ while (ISDIGIT (*++p))
+ if (digits < LOG10_BILLION)
+ {
+ ns = 10 * ns + (*p - '0');
+ digits++;
+ }
+ else
+ trailing_nonzero |= *p != '0';
+
+ while (digits++ < LOG10_BILLION)
+ ns *= 10;
+
+ if (negative)
+ {
+ /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
+ I.e., truncate time stamps towards minus infinity while
+ converting them to internal form. */
+ ns += trailing_nonzero;
+ if (ns != 0)
+ {
+ if (s == TYPE_MINIMUM (time_t))
+ goto out_of_range;
+ s--;
+ ns = BILLION - ns;
+ }
+ }
+ }
+
+ if (! *p)
+ {
+ ts->tv_sec = s;
+ ts->tv_nsec = ns;
+ return true;
+ }
}
+
+ ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
+ keyword, arg));
+ return false;
+
+ out_of_range:
+ out_of_range_header (keyword, arg, - (uintmax_t) TYPE_MINIMUM (time_t),
+ TYPE_MAXIMUM (time_t));
+ return false;
}
static void
code_num (uintmax_t value, char const *keyword, struct xheader *xhdr)
{
- char sbuf[100];
- size_t s = format_uintmax (value, NULL, 0);
- format_uintmax (value, sbuf, s);
- sbuf[s] = 0;
- xheader_print (xhdr, keyword, sbuf);
+ char sbuf[UINTMAX_STRSIZE_BOUND];
+ xheader_print (xhdr, keyword, umaxtostr (value, sbuf));
+}
+
+static bool
+decode_num (uintmax_t *num, char const *arg, uintmax_t maxval,
+ char const *keyword)
+{
+ uintmax_t u;
+ char *arg_lim;
+
+ if (! (ISDIGIT (*arg)
+ && (errno = 0, u = strtoumax (arg, &arg_lim, 10), !*arg_lim)))
+ {
+ ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
+ keyword, arg));
+ return false;
+ }
+
+ if (! (u <= maxval && errno != ERANGE))
+ {
+ out_of_range_header (keyword, arg, 0, maxval);
+ return false;
+ }
+
+ *num = u;
+ return true;
}
static void
atime_coder (struct tar_stat_info const *st, char const *keyword,
struct xheader *xhdr, void *data __attribute__ ((unused)))
{
- code_time (st->stat.st_atime, st->atime_nsec, keyword, xhdr);
+ code_time (get_stat_atime (&st->stat), keyword, xhdr);
}
static void
atime_decoder (struct tar_stat_info *st, char const *arg)
{
- decode_time (arg, &st->stat.st_atime, &st->atime_nsec);
+ struct timespec ts;
+ if (decode_time (&ts, arg, "atime"))
+ set_stat_atime (&st->stat, ts);
}
static void
gid_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
+ if (decode_num (&u, arg, TYPE_MAXIMUM (gid_t), "gid"))
st->stat.st_gid = u;
}
ctime_coder (struct tar_stat_info const *st, char const *keyword,
struct xheader *xhdr, void *data __attribute__ ((unused)))
{
- code_time (st->stat.st_ctime, st->ctime_nsec, keyword, xhdr);
+ code_time (get_stat_ctime (&st->stat), keyword, xhdr);
}
static void
ctime_decoder (struct tar_stat_info *st, char const *arg)
{
- decode_time (arg, &st->stat.st_ctime, &st->ctime_nsec);
+ struct timespec ts;
+ if (decode_time (&ts, arg, "ctime"))
+ set_stat_ctime (&st->stat, ts);
}
static void
mtime_coder (struct tar_stat_info const *st, char const *keyword,
struct xheader *xhdr, void *data __attribute__ ((unused)))
{
- code_time (st->stat.st_mtime, st->mtime_nsec, keyword, xhdr);
+ code_time (get_stat_mtime (&st->stat), keyword, xhdr);
}
static void
mtime_decoder (struct tar_stat_info *st, char const *arg)
{
- decode_time (arg, &st->stat.st_mtime, &st->mtime_nsec);
+ struct timespec ts;
+ if (decode_time (&ts, arg, "mtime"))
+ set_stat_mtime (&st->stat, ts);
}
static void
size_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
- st->archive_file_size = st->stat.st_size = u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), "size"))
+ st->stat.st_size = u;
}
static void
uid_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
+ if (decode_num (&u, arg, TYPE_MAXIMUM (uid_t), "uid"))
st->stat.st_uid = u;
}
sparse_size_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
+ if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), "GNU.sparse.size"))
st->stat.st_size = u;
}
sparse_numblocks_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
+ if (decode_num (&u, arg, SIZE_MAX, "GNU.sparse.numblocks"))
{
st->sparse_map_size = u;
- st->sparse_map = calloc(st->sparse_map_size, sizeof(st->sparse_map[0]));
+ st->sparse_map = xcalloc (u, sizeof st->sparse_map[0]);
st->sparse_map_avail = 0;
}
}
sparse_offset_coder (struct tar_stat_info const *st, char const *keyword,
struct xheader *xhdr, void *data)
{
- size_t i = *(size_t*)data;
- code_num (st->sparse_map[i].offset, keyword, xhdr);
+ size_t *pi = data;
+ code_num (st->sparse_map[*pi].offset, keyword, xhdr);
}
static void
sparse_offset_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
- st->sparse_map[st->sparse_map_avail].offset = u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), "GNU.sparse.offset"))
+ {
+ if (st->sparse_map_avail < st->sparse_map_size)
+ st->sparse_map[st->sparse_map_avail].offset = u;
+ else
+ ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
+ "GNU.sparse.offset", arg));
+ }
}
static void
sparse_numbytes_coder (struct tar_stat_info const *st, char const *keyword,
struct xheader *xhdr, void *data)
{
- size_t i = *(size_t*)data;
- code_num (st->sparse_map[i].numbytes, keyword, xhdr);
+ size_t *pi = data;
+ code_num (st->sparse_map[*pi].numbytes, keyword, xhdr);
}
static void
sparse_numbytes_decoder (struct tar_stat_info *st, char const *arg)
{
uintmax_t u;
- if (xstrtoumax (arg, NULL, 10, &u, "") == LONGINT_OK)
+ if (decode_num (&u, arg, SIZE_MAX, "GNU.sparse.numbytes"))
{
- if (st->sparse_map_avail == st->sparse_map_size)
- {
- st->sparse_map_size *= 2;
- st->sparse_map = xrealloc (st->sparse_map,
- st->sparse_map_size
- * sizeof st->sparse_map[0]);
- }
- st->sparse_map[st->sparse_map_avail++].numbytes = u;
+ if (st->sparse_map_avail < st->sparse_map_size)
+ st->sparse_map[st->sparse_map_avail++].numbytes = u;
+ else
+ ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
+ "GNU.sparse.numbytes", arg));
}
}