+ {
+ if (errno != ENOENT)
+ stat_diag (directory->name);
+ /* else: should have been already reported */
+ }
+ else
+ directory->mtime = get_stat_mtime (&st);
+ }
+ free (p);
+}
+
+#define PD_FORCE_CHILDREN 0x10
+#define PD_FORCE_INIT 0x20
+#define PD_CHILDREN(f) ((f) & 3)
+
+static struct directory *
+procdir (const char *name_buffer, struct stat *stat_data,
+ dev_t device,
+ int flag,
+ char *entry)
+{
+ struct directory *directory;
+ bool nfs = NFS_FILE_STAT (*stat_data);
+
+ if ((directory = find_directory (name_buffer)) != NULL)
+ {
+ if (DIR_IS_INITED (directory))
+ {
+ if (flag & PD_FORCE_INIT)
+ {
+ assign_string (&directory->name, name_buffer);
+ }
+ else
+ {
+ *entry = 'N'; /* Avoid duplicating this directory */
+ return directory;
+ }
+ }
+
+ if (strcmp (directory->name, name_buffer))
+ {
+ *entry = 'N';
+ return directory;
+ }
+
+ /* With NFS, the same file can have two different devices
+ if an NFS directory is mounted in multiple locations,
+ which is relatively common when automounting.
+ To avoid spurious incremental redumping of
+ directories, consider all NFS devices as equal,
+ relying on the i-node to establish differences. */
+
+ if (! ((!check_device_option
+ || (DIR_IS_NFS (directory) && nfs)
+ || directory->device_number == stat_data->st_dev)
+ && directory->inode_number == stat_data->st_ino))
+ {
+ /* FIXME: find_directory_meta ignores nfs */
+ struct directory *d = find_directory_meta (stat_data->st_dev,
+ stat_data->st_ino);
+ if (d)
+ {
+ if (strcmp (d->name, name_buffer))
+ {
+ WARNOPT (WARN_RENAME_DIRECTORY,
+ (0, 0,
+ _("%s: Directory has been renamed from %s"),
+ quotearg_colon (name_buffer),
+ quote_n (1, d->name)));
+ directory->orig = d;
+ DIR_SET_FLAG (directory, DIRF_RENAMED);
+ dirlist_replace_prefix (d->name, name_buffer);
+ }
+ directory->children = CHANGED_CHILDREN;
+ }
+ else
+ {
+ WARNOPT (WARN_RENAME_DIRECTORY,
+ (0, 0, _("%s: Directory has been renamed"),
+ quotearg_colon (name_buffer)));
+ directory->children = ALL_CHILDREN;
+ directory->device_number = stat_data->st_dev;
+ directory->inode_number = stat_data->st_ino;
+ }
+ if (nfs)
+ DIR_SET_FLAG (directory, DIRF_NFS);
+ }
+ else
+ directory->children = CHANGED_CHILDREN;
+
+ DIR_SET_FLAG (directory, DIRF_FOUND);
+ }
+ else
+ {
+ struct directory *d = find_directory_meta (stat_data->st_dev,
+ stat_data->st_ino);
+
+ directory = note_directory (name_buffer,
+ get_stat_mtime(stat_data),
+ stat_data->st_dev,
+ stat_data->st_ino,
+ nfs,
+ true,
+ NULL);
+
+ if (d)
+ {
+ if (strcmp (d->name, name_buffer))
+ {
+ WARNOPT (WARN_RENAME_DIRECTORY,
+ (0, 0, _("%s: Directory has been renamed from %s"),
+ quotearg_colon (name_buffer),
+ quote_n (1, d->name)));
+ directory->orig = d;
+ DIR_SET_FLAG (directory, DIRF_RENAMED);
+ dirlist_replace_prefix (d->name, name_buffer);
+ }
+ directory->children = CHANGED_CHILDREN;
+ }
+ else
+ {
+ DIR_SET_FLAG (directory, DIRF_NEW);
+ WARNOPT (WARN_NEW_DIRECTORY,
+ (0, 0, _("%s: Directory is new"),
+ quotearg_colon (name_buffer)));
+ directory->children =
+ (listed_incremental_option
+ || (OLDER_STAT_TIME (*stat_data, m)
+ || (after_date_option
+ && OLDER_STAT_TIME (*stat_data, c))))
+ ? ALL_CHILDREN
+ : CHANGED_CHILDREN;
+ }
+ }
+
+ /* If the directory is on another device and --one-file-system was given,
+ omit it... */
+ if (one_file_system_option && device != stat_data->st_dev
+ /* ... except if it was explicitely given in the command line */
+ && !is_individual_file (name_buffer))
+ /* FIXME:
+ WARNOPT (WARN_XDEV,
+ (0, 0,
+ _("%s: directory is on a different filesystem; not dumped"),
+ quotearg_colon (directory->name)));
+ */
+ directory->children = NO_CHILDREN;
+ else if (flag & PD_FORCE_CHILDREN)
+ {
+ directory->children = PD_CHILDREN(flag);
+ if (directory->children == NO_CHILDREN)
+ *entry = 'N';
+ }
+
+ DIR_SET_FLAG (directory, DIRF_INIT);
+
+ if (directory->children != NO_CHILDREN)
+ {
+ const char *tag_file_name;
+
+ switch (check_exclusion_tags (name_buffer, &tag_file_name))
+ {
+ case exclusion_tag_all:
+ /* This warning can be duplicated by code in dump_file0, but only
+ in case when the topmost directory being archived contains
+ an exclusion tag. */
+ exclusion_tag_warning (name_buffer, tag_file_name,
+ _("directory not dumped"));
+ *entry = 'N';
+ directory->children = NO_CHILDREN;
+ break;
+
+ case exclusion_tag_contents:
+ exclusion_tag_warning (name_buffer, tag_file_name,
+ _("contents not dumped"));
+ directory->children = NO_CHILDREN;
+ break;
+
+ case exclusion_tag_under:
+ exclusion_tag_warning (name_buffer, tag_file_name,
+ _("contents not dumped"));
+ directory->tagfile = tag_file_name;
+ break;
+
+ case exclusion_tag_none:
+ break;
+ }
+ }
+
+ return directory;
+}
+
+/* Compare dumpdir array from DIRECTORY with directory listing DIR and
+ build a new dumpdir template.
+
+ DIR must be returned by a previous call to savedir().
+
+ File names in DIRECTORY->dump->contents must be sorted
+ alphabetically.
+
+ DIRECTORY->dump is replaced with the created template. Each entry is
+ prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
+
+void
+makedumpdir (struct directory *directory, const char *dir)
+{
+ size_t i,
+ dirsize, /* Number of elements in DIR */
+ len; /* Length of DIR, including terminating nul */
+ const char *p;
+ char const **array;
+ char *new_dump, *new_dump_ptr;
+ struct dumpdir *dump;
+
+ if (directory->children == ALL_CHILDREN)
+ dump = NULL;
+ else if (DIR_IS_RENAMED (directory))
+ dump = directory->orig->idump ?
+ directory->orig->idump : directory->orig->dump;
+ else
+ dump = directory->dump;
+
+ /* Count the size of DIR and the number of elements it contains */
+ dirsize = 0;
+ len = 0;
+ for (p = dir; *p; p += strlen (p) + 1, dirsize++)
+ len += strlen (p) + 2;
+ len++;
+
+ /* Create a sorted directory listing */
+ array = xcalloc (dirsize, sizeof array[0]);
+ for (i = 0, p = dir; *p; p += strlen (p) + 1, i++)
+ array[i] = p;
+
+ qsort (array, dirsize, sizeof (array[0]), compare_dirnames);
+
+ /* Prepare space for new dumpdir */
+ new_dump = xmalloc (len);
+ new_dump_ptr = new_dump;
+
+ /* Fill in the dumpdir template */
+ for (i = 0; i < dirsize; i++)
+ {
+ const char *loc = dumpdir_locate (dump, array[i]);
+ if (loc)
+ {
+ if (directory->tagfile)
+ *new_dump_ptr = strcmp (directory->tagfile, array[i]) == 0 ?
+ ' ' : 'I';
+ else
+ *new_dump_ptr = ' ';
+ new_dump_ptr++;
+ }
+ else if (directory->tagfile)
+ *new_dump_ptr++ = strcmp (directory->tagfile, array[i]) == 0 ?
+ ' ' : 'I';
+ else
+ *new_dump_ptr++ = 'Y'; /* New entry */
+
+ /* Copy the file name */
+ for (p = array[i]; (*new_dump_ptr++ = *p++); )
+ ;
+ }
+ *new_dump_ptr = 0;
+ directory->idump = directory->dump;
+ directory->dump = dumpdir_create0 (new_dump, NULL);
+ free (array);
+}
+
+/* Recursively scan the given directory DIR.
+ DEVICE is the device number where DIR resides (for --one-file-system).
+ If CMDLINE is true, the directory name was explicitly listed in the
+ command line.
+ Unless *PDIR is NULL, store there a pointer to the struct directory
+ describing DIR. */
+struct directory *
+scan_directory (char *dir, dev_t device, bool cmdline)
+{
+ char *dirp = savedir (dir); /* for scanning directory */
+ char *name_buffer; /* directory, `/', and directory member */
+ size_t name_buffer_size; /* allocated size of name_buffer, minus 2 */
+ size_t name_length; /* used length in name_buffer */
+ struct stat stat_data;
+ struct directory *directory;
+ char ch;
+
+ if (! dirp)
+ savedir_error (dir);
+
+ name_buffer_size = strlen (dir) + NAME_FIELD_SIZE;
+ name_buffer = xmalloc (name_buffer_size + 2);
+ strcpy (name_buffer, dir);
+ zap_slashes (name_buffer);
+
+ if (deref_stat (dereference_option, name_buffer, &stat_data))
+ {
+ dir_removed_diag (name_buffer, cmdline, stat_diag);
+ free (name_buffer);
+ free (dirp);
+ return NULL;
+ }
+
+ directory = procdir (name_buffer, &stat_data, device,
+ (cmdline ? PD_FORCE_INIT : 0),
+ &ch);
+
+ name_length = strlen (name_buffer);
+ if (! ISSLASH (name_buffer[name_length - 1]))
+ {
+ name_buffer[name_length] = DIRECTORY_SEPARATOR;
+ /* name_buffer has been allocated an extra slot */
+ name_buffer[++name_length] = 0;
+ }
+
+ if (dirp && directory->children != NO_CHILDREN)
+ {
+ char *entry; /* directory entry being scanned */
+ size_t entrylen; /* length of directory entry */
+ dumpdir_iter_t itr;
+
+ makedumpdir (directory, dirp);
+
+ for (entry = dumpdir_first (directory->dump, 1, &itr);
+ entry;
+ entry = dumpdir_next (itr))
+ {
+ entrylen = strlen (entry);
+ if (name_buffer_size <= entrylen - 1 + name_length)
+ {
+ do
+ name_buffer_size += NAME_FIELD_SIZE;
+ while (name_buffer_size <= entrylen - 1 + name_length);
+ name_buffer = xrealloc (name_buffer, name_buffer_size + 2);
+ }
+ strcpy (name_buffer + name_length, entry + 1);
+
+ if (*entry == 'I') /* Ignored entry */
+ *entry = 'N';
+ else if (excluded_name (name_buffer))
+ *entry = 'N';
+ else
+ {
+ if (deref_stat (dereference_option, name_buffer, &stat_data))
+ {
+ file_removed_diag (name_buffer, false, stat_diag);
+ *entry = 'N';
+ continue;
+ }
+
+ if (S_ISDIR (stat_data.st_mode))
+ {
+ int pd_flag = 0;
+ if (!recursion_option)
+ pd_flag |= PD_FORCE_CHILDREN | NO_CHILDREN;
+ else if (directory->children == ALL_CHILDREN)
+ pd_flag |= PD_FORCE_CHILDREN | ALL_CHILDREN;
+ *entry = 'D';
+ procdir (name_buffer, &stat_data, device, pd_flag, entry);
+ }
+
+ else if (one_file_system_option && device != stat_data.st_dev)
+ *entry = 'N';
+
+ else if (*entry == 'Y')
+ /* New entry, skip further checks */;
+
+ /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
+
+ else if (OLDER_STAT_TIME (stat_data, m)
+ && (!after_date_option
+ || OLDER_STAT_TIME (stat_data, c)))
+ *entry = 'N';
+ else
+ *entry = 'Y';
+ }
+ }
+ free (itr);
+ }
+
+ free (name_buffer);
+ if (dirp)
+ free (dirp);
+
+ return directory;
+}
+
+/* Return pointer to the contents of the directory DIR */
+const char *
+directory_contents (struct directory *dir)
+{
+ if (!dir)
+ return NULL;
+ return dir->dump ? dir->dump->contents : NULL;
+}
+
+/* A "safe" version of directory_contents, which never returns NULL. */
+const char *
+safe_directory_contents (struct directory *dir)
+{
+ const char *ret = directory_contents (dir);
+ return ret ? ret : "\0\0\0\0";
+}
+
+void
+name_fill_directory (struct name *name, dev_t device, bool cmdline)
+{
+ name->directory = scan_directory (name->name, device, cmdline);
+}
+
+\f
+static void
+obstack_code_rename (struct obstack *stk, char *from, char *to)
+{
+ char *s;
+
+ s = from[0] == 0 ? from :
+ safer_name_suffix (from, false, absolute_names_option);
+ obstack_1grow (stk, 'R');
+ obstack_grow (stk, s, strlen (s) + 1);
+
+ s = to[0] == 0 ? to:
+ safer_name_suffix (to, false, absolute_names_option);
+ obstack_1grow (stk, 'T');
+ obstack_grow (stk, s, strlen (s) + 1);
+}
+
+static void
+store_rename (struct directory *dir, struct obstack *stk)
+{
+ if (DIR_IS_RENAMED (dir))
+ {
+ struct directory *prev, *p;
+
+ /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
+ are ignored when hit by this function next time.
+ If the chain forms a cycle, prev points to the entry DIR is renamed
+ from. In this case it still retains DIRF_RENAMED flag, which will be
+ cleared in the `else' branch below */
+ for (prev = dir; prev && prev->orig != dir; prev = prev->orig)
+ DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
+
+ if (prev == NULL)
+ {
+ for (p = dir; p && p->orig; p = p->orig)
+ obstack_code_rename (stk, p->orig->name, p->name);
+ }
+ else
+ {
+ char *temp_name;
+
+ DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
+
+ /* Break the cycle by using a temporary name for one of its
+ elements.
+ First, create a temp name stub entry. */
+ temp_name = dir_name (dir->name);
+ obstack_1grow (stk, 'X');
+ obstack_grow (stk, temp_name, strlen (temp_name) + 1);
+
+ obstack_code_rename (stk, dir->name, "");
+
+ for (p = dir; p != prev; p = p->orig)
+ obstack_code_rename (stk, p->orig->name, p->name);
+
+ obstack_code_rename (stk, "", prev->name);
+ }
+ }
+}
+
+void
+append_incremental_renames (struct directory *dir)
+{
+ struct obstack stk;
+ size_t size;
+ struct directory *dp;
+ const char *dump;
+
+ if (dirhead == NULL)
+ return;
+
+ obstack_init (&stk);
+ dump = directory_contents (dir);
+ if (dump)
+ {
+ size = dumpdir_size (dump) - 1;
+ obstack_grow (&stk, dump, size);
+ }
+ else
+ size = 0;
+
+ for (dp = dirhead; dp; dp = dp->next)
+ store_rename (dp, &stk);
+
+ if (obstack_object_size (&stk) != size)
+ {
+ obstack_1grow (&stk, 0);
+ dumpdir_free (dir->dump);
+ dir->dump = dumpdir_create (obstack_finish (&stk));
+ }
+ obstack_free (&stk, NULL);
+}
+
+\f
+
+static FILE *listed_incremental_stream;
+
+/* Version of incremental format snapshots (directory files) used by this
+ tar. Currently it is supposed to be a single decimal number. 0 means
+ incremental snapshots as per tar version before 1.15.2.
+
+ The current tar version supports incremental versions from
+ 0 up to TAR_INCREMENTAL_VERSION, inclusive.
+ It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
+
+#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 = 0;
+ size_t bufsize;
+ 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;
+ }