+ fprintf (stdlis, _("Renaming %s back to %s\n"),
+ quote_n (0, after_backup_name),
+ quote_n (1, before_backup_name));
+ assign_string (&after_backup_name, 0);
+ }
+}
+
+/* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
+int
+deref_stat (bool deref, char const *name, struct stat *buf)
+{
+ return deref ? stat (name, buf) : lstat (name, buf);
+}
+
+/* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
+ access time to ATIME. ATFLAG controls symbolic-link following, in
+ the style of openat. */
+int
+set_file_atime (int fd, int parentfd, char const *file, struct timespec atime,
+ int atflag)
+{
+ struct timespec ts[2];
+ ts[0] = atime;
+ ts[1].tv_nsec = UTIME_OMIT;
+ return fdutimensat (fd, parentfd, file, ts, atflag);
+}
+
+/* A description of a working directory. */
+struct wd
+{
+ /* The directory's name. */
+ char const *name;
+
+ /* A negative value if no attempt has been made to save the
+ directory, 0 if it was saved successfully, and a positive errno
+ value if it was not saved successfully. */
+ int err;
+
+ /* The saved version of the directory, if ERR == 0. */
+ struct saved_cwd saved_cwd;
+};
+
+/* A vector of chdir targets. wd[0] is the initial working directory. */
+static struct wd *wd;
+
+/* The number of working directories in the vector. */
+static size_t wd_count;
+
+/* The allocated size of the vector. */
+static size_t wd_alloc;
+
+int
+chdir_count ()
+{
+ if (wd_count == 0)
+ return wd_count;
+ return wd_count - 1;
+}
+
+/* DIR is the operand of a -C option; add it to vector of chdir targets,
+ and return the index of its location. */
+int
+chdir_arg (char const *dir)
+{
+ if (wd_count == wd_alloc)
+ {
+ if (wd_alloc == 0)
+ {
+ wd_alloc = 2;
+ wd = xmalloc (sizeof *wd * wd_alloc);
+ }
+ else
+ wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
+
+ if (! wd_count)
+ {
+ wd[wd_count].name = ".";
+ wd[wd_count].err = -1;
+ wd_count++;
+ }
+ }
+
+ /* Optimize the common special case of the working directory,
+ or the working directory as a prefix. */
+ if (dir[0])
+ {
+ while (dir[0] == '.' && ISSLASH (dir[1]))
+ for (dir += 2; ISSLASH (*dir); dir++)
+ continue;
+ if (! dir[dir[0] == '.'])
+ return wd_count - 1;
+ }
+
+ wd[wd_count].name = dir;
+ wd[wd_count].err = -1;
+ return wd_count++;
+}
+
+/* Index of current directory. */
+int chdir_current;
+
+/* Change to directory I. If I is 0, change to the initial working
+ directory; otherwise, I must be a value returned by chdir_arg. */
+void
+chdir_do (int i)
+{
+ if (chdir_current != i)
+ {
+ struct wd *prev = &wd[chdir_current];
+ struct wd *curr = &wd[i];
+
+ if (prev->err < 0)
+ {
+ prev->err = 0;
+ if (save_cwd (&prev->saved_cwd) != 0)
+ prev->err = errno;
+ else if (0 <= prev->saved_cwd.desc)
+ {
+ /* Make sure we still have at least one descriptor available. */
+ int fd1 = prev->saved_cwd.desc;
+ int fd2 = dup (fd1);
+ if (0 <= fd2)
+ close (fd2);
+ else if (errno == EMFILE)
+ {
+ /* Force restore_cwd to use chdir_long. */
+ close (fd1);
+ prev->saved_cwd.desc = -1;
+ prev->saved_cwd.name = xgetcwd ();
+ if (! prev->saved_cwd.name)
+ prev->err = errno;
+ }
+ else
+ prev->err = errno;
+ }
+ }
+
+ if (0 <= curr->err)
+ {
+ int err = curr->err;
+ if (err == 0 && restore_cwd (&curr->saved_cwd) != 0)
+ err = errno;
+ if (err)
+ FATAL_ERROR ((0, err, _("Cannot restore working directory")));
+ }
+ else
+ {
+ if (i && ! ISSLASH (curr->name[0]))
+ chdir_do (i - 1);
+ if (chdir (curr->name) != 0)
+ chdir_fatal (curr->name);
+ }
+
+ chdir_current = i;
+ }
+}
+\f
+void
+close_diag (char const *name)
+{
+ if (ignore_failed_read_option)
+ close_warn (name);
+ else
+ close_error (name);
+}
+
+void
+open_diag (char const *name)
+{
+ if (ignore_failed_read_option)
+ open_warn (name);
+ else
+ open_error (name);
+}
+
+void
+read_diag_details (char const *name, off_t offset, size_t size)
+{
+ if (ignore_failed_read_option)
+ read_warn_details (name, offset, size);
+ else
+ read_error_details (name, offset, size);
+}
+
+void
+readlink_diag (char const *name)
+{
+ if (ignore_failed_read_option)
+ readlink_warn (name);
+ else
+ readlink_error (name);
+}
+
+void
+savedir_diag (char const *name)
+{
+ if (ignore_failed_read_option)
+ savedir_warn (name);
+ else
+ savedir_error (name);
+}
+
+void
+seek_diag_details (char const *name, off_t offset)
+{
+ if (ignore_failed_read_option)
+ seek_warn_details (name, offset);
+ else
+ seek_error_details (name, offset);
+}
+
+void
+stat_diag (char const *name)
+{
+ if (ignore_failed_read_option)
+ stat_warn (name);
+ else
+ stat_error (name);
+}
+
+void
+file_removed_diag (const char *name, bool top_level,
+ void (*diagfn) (char const *name))
+{
+ if (!top_level && errno == ENOENT)
+ {
+ WARNOPT (WARN_FILE_REMOVED,
+ (0, 0, _("%s: File removed before we read it"),
+ quotearg_colon (name)));
+ set_exit_status (TAREXIT_DIFFERS);