-#include "save-cwd.h"
-#include "stat-macros.h"
-
-/* Ensure that the directory ARG exists.
-
- Create any leading directories that don't already exist, with
- permissions PARENT_MODE.
- If the last element of ARG does not exist, create it as
- a new directory with permissions MODE.
- If OWNER and GROUP are non-negative, use them to set the UID and GID of
- any created directories.
- If VERBOSE_FMT_STRING is nonzero, use it as a printf format
- string for printing a message after successfully making a directory,
- with the name of the directory that was just made as an argument.
- If PRESERVE_EXISTING is true and ARG is an existing directory,
- then do not attempt to set its permissions and ownership.
-
- Set *CWD_ERRNO to a (nonzero) error number if this
- function has changed the current working directory and is unable to
- restore it to its initial state. Do not change
- *CWD_ERRNO otherwise.
-
- Return true iff ARG exists as a directory with the proper ownership
- and permissions when done. Note that this function returns true
- even when it fails to return to the initial working directory. */
-
-bool
-make_dir_parents (char const *arg,
- mode_t mode,
- mode_t parent_mode,
- uid_t owner,
- gid_t group,
- bool preserve_existing,
- char const *verbose_fmt_string,
- int *cwd_errno)
-{
- struct stat stats;
- bool retval = true;
- bool do_chdir = false; /* Whether to chdir before each mkdir. */
- struct saved_cwd cwd;
- bool cwd_problem = false;
- char const *fixup_permissions_dir = NULL;
- char const *full_dir = arg;
-
- struct ptr_list
- {
- char *dirname_end;
- struct ptr_list *next;
- };
- struct ptr_list *leading_dirs = NULL;
-
- if (stat (arg, &stats) == 0)
- {
- if (! S_ISDIR (stats.st_mode))
- {
- error (0, 0, _("%s exists but is not a directory"), quote (arg));
- return false;
- }
-
- if (!preserve_existing)
- fixup_permissions_dir = arg;
- }
- else if (errno != ENOENT || !*arg)
- {
- error (0, errno, "%s", quote (arg));
- return false;
- }
- else
- {
- char *slash;
- mode_t tmp_mode; /* Initial perms for leading dirs. */
- bool re_protect; /* Should leading dirs be unwritable? */
- char *basename_dir;
- char *dir;
-
- /* Temporarily relax umask in case it's overly restrictive. */
- mode_t oldmask = umask (0);
-
- /* Make a copy of ARG that we can scribble NULs on. */
- dir = alloca (strlen (arg) + 1);
- strcpy (dir, arg);
- strip_trailing_slashes (dir);
- full_dir = dir;