+#define TAR_INCREMENTAL_VERSION 2
+
+/* Read incremental snapshot formats 0 and 1 */
+static void
+read_incr_db_01 (int version, const char *initbuf)
+{
+ int n;
+ uintmax_t u;
+ time_t sec;
+ long int nsec;
+ char *buf = NULL;
+ size_t bufsize = 0;
+ char *ebuf;
+ long lineno = 1;
+
+ if (version == 1)
+ {
+ if (getline (&buf, &bufsize, listed_incremental_stream) <= 0)
+ {
+ read_error (listed_incremental_option);
+ free (buf);
+ return;
+ }
+ ++lineno;
+ }
+ else
+ {
+ buf = strdup (initbuf);
+ bufsize = strlen (buf) + 1;
+ }
+
+ sec = TYPE_MINIMUM (time_t);
+ nsec = -1;
+ errno = 0;
+ u = strtoumax (buf, &ebuf, 10);
+ if (!errno && TYPE_MAXIMUM (time_t) < u)
+ errno = ERANGE;
+ if (errno || buf == ebuf)
+ ERROR ((0, errno, "%s:%ld: %s",
+ quotearg_colon (listed_incremental_option),
+ lineno,
+ _("Invalid time stamp")));
+ else
+ {
+ sec = u;
+
+ if (version == 1 && *ebuf)
+ {
+ char const *buf_ns = ebuf + 1;
+ errno = 0;
+ u = strtoumax (buf_ns, &ebuf, 10);
+ if (!errno && BILLION <= u)
+ errno = ERANGE;
+ if (errno || buf_ns == ebuf)
+ {
+ ERROR ((0, errno, "%s:%ld: %s",
+ quotearg_colon (listed_incremental_option),
+ lineno,
+ _("Invalid time stamp")));
+ sec = TYPE_MINIMUM (time_t);
+ }
+ else
+ nsec = u;
+ }
+ else
+ {
+ /* pre-1 incremental format does not contain nanoseconds */
+ nsec = 0;
+ }
+ }
+ newer_mtime_option.tv_sec = sec;
+ newer_mtime_option.tv_nsec = nsec;
+
+
+ while (0 < (n = getline (&buf, &bufsize, listed_incremental_stream)))
+ {
+ dev_t dev;
+ ino_t ino;
+ bool nfs = buf[0] == '+';
+ char *strp = buf + nfs;
+ struct timespec mtime;
+
+ lineno++;
+
+ if (buf[n - 1] == '\n')
+ buf[n - 1] = '\0';
+
+ if (version == 1)
+ {
+ errno = 0;
+ u = strtoumax (strp, &ebuf, 10);
+ if (!errno && TYPE_MAXIMUM (time_t) < u)
+ errno = ERANGE;
+ if (errno || strp == ebuf || *ebuf != ' ')
+ {
+ ERROR ((0, errno, "%s:%ld: %s",
+ quotearg_colon (listed_incremental_option), lineno,
+ _("Invalid modification time (seconds)")));
+ sec = (time_t) -1;
+ }
+ else
+ sec = u;
+ strp = ebuf;
+
+ errno = 0;
+ u = strtoumax (strp, &ebuf, 10);
+ if (!errno && BILLION <= u)
+ errno = ERANGE;
+ if (errno || strp == ebuf || *ebuf != ' ')
+ {
+ ERROR ((0, errno, "%s:%ld: %s",
+ quotearg_colon (listed_incremental_option), lineno,
+ _("Invalid modification time (nanoseconds)")));
+ nsec = -1;
+ }
+ else
+ nsec = u;
+ mtime.tv_sec = sec;
+ mtime.tv_nsec = nsec;
+ strp = ebuf;
+ }
+ else
+ memset (&mtime, 0, sizeof mtime);
+
+ errno = 0;
+ u = strtoumax (strp, &ebuf, 10);
+ if (!errno && TYPE_MAXIMUM (dev_t) < u)
+ errno = ERANGE;
+ if (errno || strp == ebuf || *ebuf != ' ')
+ {
+ ERROR ((0, errno, "%s:%ld: %s",
+ quotearg_colon (listed_incremental_option), lineno,
+ _("Invalid device number")));
+ dev = (dev_t) -1;
+ }
+ else
+ dev = u;
+ strp = ebuf;
+
+ errno = 0;
+ u = strtoumax (strp, &ebuf, 10);
+ if (!errno && TYPE_MAXIMUM (ino_t) < u)
+ errno = ERANGE;
+ if (errno || strp == ebuf || *ebuf != ' ')
+ {
+ ERROR ((0, errno, "%s:%ld: %s",
+ quotearg_colon (listed_incremental_option), lineno,
+ _("Invalid inode number")));
+ ino = (ino_t) -1;
+ }
+ else
+ ino = u;
+ strp = ebuf;
+
+ strp++;
+ unquote_string (strp);
+ note_directory (strp, mtime, dev, ino, nfs, false, NULL);
+ }
+ free (buf);
+}
+
+/* Read a nul-terminated string from FP and store it in STK.
+ Store the number of bytes read (including nul terminator) in PCOUNT.
+
+ Return the last character read or EOF on end of file. */
+static int
+read_obstack (FILE *fp, struct obstack *stk, size_t *pcount)
+{
+ int c;
+ size_t i;
+
+ for (i = 0, c = getc (fp); c != EOF && c != 0; c = getc (fp), i++)
+ obstack_1grow (stk, c);
+ obstack_1grow (stk, 0);
+
+ *pcount = i;
+ return c;
+}
+
+/* Read from file FP a nul-terminated string and convert it to
+ intmax_t. Return the resulting value in PVAL. Assume '-' has
+ already been read.
+
+ Throw a fatal error if the string cannot be converted or if the
+ converted value is less than MIN_VAL. */
+
+static void
+read_negative_num (FILE *fp, intmax_t min_val, intmax_t *pval)
+{
+ int c;
+ size_t i;
+ char buf[INT_BUFSIZE_BOUND (intmax_t)];
+ char *ep;
+ buf[0] = '-';
+
+ for (i = 1; ISDIGIT (c = getc (fp)); i++)
+ {
+ if (i == sizeof buf - 1)
+ FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
+ buf[i] = c;
+ }
+
+ if (c < 0)
+ {
+ if (ferror (fp))
+ FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
+ else
+ FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
+ }
+
+ buf[i] = 0;
+ errno = 0;
+ *pval = strtoimax (buf, &ep, 10);
+ if (c || errno || *pval < min_val)
+ FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
+}
+
+/* Read from file FP a nul-terminated string and convert it to
+ uintmax_t. Return the resulting value in PVAL. Assume C has
+ already been read.
+
+ Throw a fatal error if the string cannot be converted or if the
+ converted value exceeds MAX_VAL.
+
+ Return the last character read or EOF on end of file. */
+
+static int
+read_unsigned_num (int c, FILE *fp, uintmax_t max_val, uintmax_t *pval)
+{
+ size_t i;
+ char buf[UINTMAX_STRSIZE_BOUND], *ep;
+
+ for (i = 0; ISDIGIT (c); i++)
+ {
+ if (i == sizeof buf - 1)
+ FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
+ buf[i] = c;
+ c = getc (fp);
+ }
+
+ if (c < 0)
+ {
+ if (ferror (fp))
+ FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
+ else if (i == 0)
+ return c;
+ else
+ FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
+ }
+
+ buf[i] = 0;
+ errno = 0;
+ *pval = strtoumax (buf, &ep, 10);
+ if (c || errno || max_val < *pval)
+ FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
+ return c;
+}
+
+/* Read from file FP a nul-terminated string and convert it to
+ uintmax_t. Return the resulting value in PVAL.
+
+ Throw a fatal error if the string cannot be converted or if the
+ converted value exceeds MAX_VAL.
+
+ Return the last character read or EOF on end of file. */
+
+static int
+read_num (FILE *fp, uintmax_t max_val, uintmax_t *pval)
+{
+ return read_unsigned_num (getc (fp), fp, max_val, pval);
+}
+
+/* Read from FP two NUL-terminated strings representing a struct
+ timespec. Return the resulting value in PVAL.
+
+ Throw a fatal error if the string cannot be converted. */
+
+static void
+read_timespec (FILE *fp, struct timespec *pval)
+{
+ int c = getc (fp);
+ intmax_t i;
+ uintmax_t u;
+
+ if (c == '-')
+ {
+ read_negative_num (fp, TYPE_MINIMUM (time_t), &i);
+ c = 0;
+ pval->tv_sec = i;
+ }
+ else
+ {
+ c = read_unsigned_num (c, fp, TYPE_MAXIMUM (time_t), &u);
+ pval->tv_sec = u;
+ }
+
+ if (c || read_num (fp, BILLION - 1, &u))
+ FATAL_ERROR ((0, 0, "%s: %s",
+ quotearg_colon (listed_incremental_option),
+ _("Unexpected EOF in snapshot file")));
+ pval->tv_nsec = u;
+}
+
+/* Read incremental snapshot format 2 */
+static void
+read_incr_db_2 (void)
+{
+ uintmax_t u;
+ struct obstack stk;
+
+ obstack_init (&stk);
+
+ read_timespec (listed_incremental_stream, &newer_mtime_option);
+
+ for (;;)
+ {
+ struct timespec mtime;
+ dev_t dev;
+ ino_t ino;
+ bool nfs;
+ char *name;
+ char *content;
+ size_t s;
+
+ if (read_num (listed_incremental_stream, 1, &u))
+ return; /* Normal return */
+
+ nfs = u;
+
+ read_timespec (listed_incremental_stream, &mtime);
+
+ if (read_num (listed_incremental_stream, TYPE_MAXIMUM (dev_t), &u))
+ break;
+ dev = u;
+
+ if (read_num (listed_incremental_stream, TYPE_MAXIMUM (ino_t), &u))
+ break;
+ ino = u;
+
+ if (read_obstack (listed_incremental_stream, &stk, &s))
+ break;
+
+ name = obstack_finish (&stk);
+
+ while (read_obstack (listed_incremental_stream, &stk, &s) == 0 && s > 1)
+ ;
+ if (getc (listed_incremental_stream) != 0)
+ FATAL_ERROR ((0, 0, "%s: %s",
+ quotearg_colon (listed_incremental_option),
+ _("Missing record terminator")));
+
+ content = obstack_finish (&stk);
+ note_directory (name, mtime, dev, ino, nfs, false, content);
+ obstack_free (&stk, content);
+ }
+ FATAL_ERROR ((0, 0, "%s: %s",
+ quotearg_colon (listed_incremental_option),
+ _("Unexpected EOF in snapshot file")));
+}