+ if (do_chdir)
+ {
+ /* If we know that basename_dir is a directory (because we've
+ just created it), then ensure that when we change to it,
+ that final component is not a symlink. Otherwise, we must
+ accept the possibility that basename_dir is a preexisting
+ symlink-to-directory and chdir through the symlink. */
+ if ((dir_known_to_exist
+ ? chdir_no_follow (basename_dir)
+ : chdir (basename_dir)) == 0)
+ dir_known_to_exist = true;
+ else if (dir_known_to_exist)
+ {
+ error (0, errno, _("cannot chdir to directory %s"),
+ quote (dir));
+ retval = false;
+ break;
+ }
+ }
+ else if (!dir_known_to_exist)
+ dir_known_to_exist = (stat (basename_dir, &stats) == 0
+ && S_ISDIR (stats.st_mode));
+
+ if (!dir_known_to_exist)