+ char *caname = normalize_filename (name);
+ struct directory *dir = make_directory (name, caname);
+ struct directory *ret = hash_lookup (directory_table, dir);
+ free_directory (dir);
+ return ret;
+ }
+}
+
+#if 0
+/* Remove directory entry for the given CANAME */
+void
+remove_directory (const char *caname)
+{
+ struct directory *dir = make_directory (caname, xstrdup (caname));
+ struct directory *ret = hash_delete (directory_table, dir);
+ if (ret)
+ free_directory (ret);
+ free_directory (dir);
+}
+#endif
+
+/* If first OLD_PREFIX_LEN bytes of DIR->NAME name match OLD_PREFIX,
+ replace them with NEW_PREFIX. */
+void
+rebase_directory (struct directory *dir,
+ const char *old_prefix, size_t old_prefix_len,
+ const char *new_prefix, size_t new_prefix_len)
+{
+ replace_prefix (&dir->name, old_prefix, old_prefix_len,
+ new_prefix, new_prefix_len);
+}
+
+/* Return a directory entry for a given combination of device and inode
+ numbers, or zero if none found. */
+static struct directory *
+find_directory_meta (dev_t dev, ino_t ino)
+{
+ if (! directory_meta_table)
+ return 0;
+ else
+ {
+ struct directory *dir = make_directory ("", NULL);
+ struct directory *ret;
+ dir->device_number = dev;
+ dir->inode_number = ino;
+ ret = hash_lookup (directory_meta_table, dir);
+ free_directory (dir);
+ return ret;
+ }
+}
+
+void
+update_parent_directory (const char *name)
+{
+ struct directory *directory;
+ char *p;
+
+ p = dir_name (name);
+ directory = find_directory (p);
+ if (directory)
+ {
+ struct stat st;
+ if (deref_stat (dereference_option, p, &st) != 0)
+ {
+ 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);