2015-09-29 14:12:00 +02:00
|
|
|
/* mkdir_p.c - Create a directory and any missing parents.
|
|
|
|
* Copyright (C) 2015 g10 Code GmbH
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
2015-10-08 16:42:14 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2015-09-29 14:12:00 +02:00
|
|
|
*
|
2015-10-08 16:42:14 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2015-09-29 14:12:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2015-09-29 14:12:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
#include "util.h"
|
2015-09-29 14:12:00 +02:00
|
|
|
#include "stringhelp.h"
|
|
|
|
#include "logging.h"
|
2015-10-08 16:42:14 +02:00
|
|
|
#include "sysutils.h"
|
|
|
|
#include "mkdir_p.h"
|
2015-09-29 14:12:00 +02:00
|
|
|
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
gpg_error_t
|
2015-10-16 16:30:46 +02:00
|
|
|
gnupg_amkdir_p (const char **directory_components)
|
2015-09-29 14:12:00 +02:00
|
|
|
{
|
2015-10-08 16:42:14 +02:00
|
|
|
gpg_error_t err = 0;
|
2015-09-29 14:12:00 +02:00
|
|
|
int count;
|
|
|
|
char **dirs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (count = 0; directory_components[count]; count ++)
|
|
|
|
;
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("%s: %d directory components.\n", __func__, count); */
|
|
|
|
|
2015-10-18 19:37:41 +02:00
|
|
|
dirs = xtrycalloc (count, sizeof *dirs);
|
2015-10-08 16:42:14 +02:00
|
|
|
if (!dirs)
|
|
|
|
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
2015-09-29 14:12:00 +02:00
|
|
|
|
|
|
|
for (i = 0; directory_components[i]; i ++)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
2015-10-20 12:10:03 +02:00
|
|
|
dirs[i] = make_filename_try (directory_components[i], NULL);
|
2015-09-29 14:12:00 +02:00
|
|
|
else
|
2015-10-18 19:37:41 +02:00
|
|
|
dirs[i] = make_filename_try (dirs[i-1], directory_components[i], NULL);
|
|
|
|
if (!dirs[i])
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
|
|
|
goto out;
|
|
|
|
}
|
2015-09-29 14:12:00 +02:00
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("%s: Directory %d: `%s'.\n", __func__, i, dirs[i]); */
|
2015-09-29 14:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = count - 1; i >= 0; i --)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("%s: stat(%s)\n", __func__, dirs[i]); */
|
|
|
|
|
2020-10-20 16:38:06 +02:00
|
|
|
if (!gnupg_stat (dirs[i], &s))
|
2015-10-08 16:42:14 +02:00
|
|
|
{
|
|
|
|
if ( ! S_ISDIR (s.st_mode))
|
|
|
|
{
|
|
|
|
/* log_debug ("%s: %s exists, but is not a directory!\n", */
|
|
|
|
/* __func__, dirs[i]); */
|
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_ENOTDIR);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Got a directory. */
|
|
|
|
/* log_debug ("%s: %s exists and is a directory!\n", */
|
|
|
|
/* __func__, dirs[i]); */
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 14:12:00 +02:00
|
|
|
else if (errno == ENOENT)
|
|
|
|
/* This directory does not exist yet. Continue walking up the
|
|
|
|
hierarchy. */
|
|
|
|
{
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("%s: %s does not exist!\n", */
|
|
|
|
/* __func__, dirs[i]); */
|
2015-09-29 14:12:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Some other error code. Die. Note: this could be ENOTDIR
|
|
|
|
(we return this above), which means that a component of the
|
|
|
|
path prefix is not a directory. */
|
|
|
|
{
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("%s: stat(%s) => %s!\n", */
|
|
|
|
/* __func__, dirs[i], strerror (errno)); */
|
|
|
|
err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
2015-09-29 14:12:00 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (i >= -1);
|
|
|
|
/* DIRS[I] exists. Start with the following entry. */
|
|
|
|
i ++;
|
|
|
|
|
|
|
|
for (; i < count; i ++)
|
|
|
|
{
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("Creating directory: %s\n", dirs[i]); */
|
2015-09-29 14:12:00 +02:00
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
if (gnupg_mkdir (dirs[i], "-rwx"))
|
2015-09-29 14:12:00 +02:00
|
|
|
{
|
2015-10-08 16:42:14 +02:00
|
|
|
err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
2015-09-29 14:12:00 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2015-10-18 19:37:41 +02:00
|
|
|
for (i = 0; i < count; i ++)
|
2015-09-29 14:12:00 +02:00
|
|
|
xfree (dirs[i]);
|
|
|
|
xfree (dirs);
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
/* log_debug ("%s: Returning %s\n", __func__, gpg_strerror (rc)); */
|
2015-09-29 14:12:00 +02:00
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
return err;
|
2015-09-29 14:12:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
|
|
|
|
gpg_error_t
|
2015-10-16 16:30:46 +02:00
|
|
|
gnupg_mkdir_p (const char *directory_component, ...)
|
2015-09-29 14:12:00 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2015-10-08 16:42:14 +02:00
|
|
|
gpg_error_t err = 0;
|
2015-09-29 14:12:00 +02:00
|
|
|
int i;
|
|
|
|
int space = 1;
|
2015-10-18 19:37:41 +02:00
|
|
|
const char **dirs;
|
2015-10-08 16:42:14 +02:00
|
|
|
|
|
|
|
dirs = xtrymalloc (space * sizeof (char *));
|
|
|
|
if (!dirs)
|
|
|
|
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
2015-09-29 14:12:00 +02:00
|
|
|
|
|
|
|
dirs[0] = directory_component;
|
|
|
|
|
|
|
|
va_start (ap, directory_component);
|
|
|
|
for (i = 1; dirs[i - 1]; i ++)
|
|
|
|
{
|
|
|
|
if (i == space)
|
|
|
|
{
|
2015-10-18 19:37:41 +02:00
|
|
|
const char **tmp_dirs;
|
2015-10-08 16:42:14 +02:00
|
|
|
|
2015-09-29 14:12:00 +02:00
|
|
|
space = 2 * space;
|
2015-10-08 16:42:14 +02:00
|
|
|
tmp_dirs = xtryrealloc (dirs, space * sizeof (char *));
|
|
|
|
if (!tmp_dirs)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource,
|
|
|
|
gpg_err_code_from_syserror ());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dirs = tmp_dirs;
|
2015-09-29 14:12:00 +02:00
|
|
|
}
|
|
|
|
dirs[i] = va_arg (ap, char *);
|
|
|
|
}
|
|
|
|
va_end (ap);
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
if (!err)
|
2015-10-16 16:30:46 +02:00
|
|
|
err = gnupg_amkdir_p (dirs);
|
2015-09-29 14:12:00 +02:00
|
|
|
|
|
|
|
xfree (dirs);
|
|
|
|
|
2015-10-08 16:42:14 +02:00
|
|
|
return err;
|
2015-09-29 14:12:00 +02:00
|
|
|
}
|