common: Allow building of mkdir_p.c for Windows.

* common/mkdir_p.c: Change license and comment debug statements.
(amkdir_p, mkdir_p): Fail on malloc error and use default_errsource to
build an error code.  Change return value to gpg_error_t.
(amkdir_p): Use gnupg_mkdir.

* common/membuf.c: Include util.h first to avoid redefined macro
warnings.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-10-08 16:42:14 +02:00
parent d7b8e76f99
commit 4c29852590
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 94 additions and 64 deletions

View File

@ -33,9 +33,8 @@
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include "membuf.h"
#include "util.h" #include "util.h"
#include "membuf.h"
/* A simple implementation of a dynamic buffer. Use init_membuf() to /* A simple implementation of a dynamic buffer. Use init_membuf() to

View File

@ -3,12 +3,22 @@
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
* GnuPG is free software; you can redistribute it and/or modify * This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of either
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* *
* GnuPG is distributed in the hope that it will be useful, * - 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
@ -23,28 +33,30 @@
#include <assert.h> #include <assert.h>
#include <stdarg.h> #include <stdarg.h>
#include "mkdir_p.h" #include "util.h"
#include "stringhelp.h" #include "stringhelp.h"
#include "logging.h" #include "logging.h"
#include "util.h" #include "sysutils.h"
#include "mkdir_p.h"
#define DEBUG 0
int gpg_error_t
amkdir_p (char **directory_components) amkdir_p (char **directory_components)
{ {
gpg_error_t err = 0;
int count; int count;
char **dirs; char **dirs;
int i; int i;
int rc = 0;
for (count = 0; directory_components[count]; count ++) for (count = 0; directory_components[count]; count ++)
; ;
if (DEBUG) /* log_debug ("%s: %d directory components.\n", __func__, count); */
log_debug ("%s: %d directory components.\n", __func__, count);
dirs = xtrycalloc (count, sizeof (char *));
if (!dirs)
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
dirs = xcalloc (count, sizeof (char *));
for (i = 0; directory_components[i]; i ++) for (i = 0; directory_components[i]; i ++)
{ {
if (i == 0) if (i == 0)
@ -52,41 +64,39 @@ amkdir_p (char **directory_components)
else else
dirs[i] = make_filename (dirs[i - 1], directory_components[i], NULL); dirs[i] = make_filename (dirs[i - 1], directory_components[i], NULL);
if (DEBUG) /* log_debug ("%s: Directory %d: `%s'.\n", __func__, i, dirs[i]); */
log_debug ("%s: Directory %d: `%s'.\n", __func__, i, dirs[i]);
} }
for (i = count - 1; i >= 0; i --) for (i = count - 1; i >= 0; i --)
{ {
struct stat s; struct stat s;
if (DEBUG) /* log_debug ("%s: stat(%s)\n", __func__, dirs[i]); */
log_debug ("%s: stat(%s)\n", __func__, dirs[i]);
rc = stat (dirs[i], &s); if (!stat (dirs[i], &s))
if (rc == 0 && ! S_ISDIR (s.st_mode)) {
{ if ( ! S_ISDIR (s.st_mode))
if (DEBUG) {
log_debug ("%s: %s exists, but is not a directory!\n", /* log_debug ("%s: %s exists, but is not a directory!\n", */
__func__, dirs[i]); /* __func__, dirs[i]); */
rc = gpg_error (GPG_ERR_ENOTDIR); err = gpg_err_make (default_errsource, GPG_ERR_ENOTDIR);
goto out; goto out;
} }
else if (rc == 0) else
{ {
/* Got a directory. */ /* Got a directory. */
if (DEBUG) /* log_debug ("%s: %s exists and is a directory!\n", */
log_debug ("%s: %s exists and is a directory!\n", /* __func__, dirs[i]); */
__func__, dirs[i]); err = 0;
break; break;
} }
}
else if (errno == ENOENT) else if (errno == ENOENT)
/* This directory does not exist yet. Continue walking up the /* This directory does not exist yet. Continue walking up the
hierarchy. */ hierarchy. */
{ {
if (DEBUG) /* log_debug ("%s: %s does not exist!\n", */
log_debug ("%s: %s does not exist!\n", /* __func__, dirs[i]); */
__func__, dirs[i]);
continue; continue;
} }
else else
@ -94,10 +104,9 @@ amkdir_p (char **directory_components)
(we return this above), which means that a component of the (we return this above), which means that a component of the
path prefix is not a directory. */ path prefix is not a directory. */
{ {
if (DEBUG) /* log_debug ("%s: stat(%s) => %s!\n", */
log_debug ("%s: stat(%s) => %s!\n", /* __func__, dirs[i], strerror (errno)); */
__func__, dirs[i], strerror (errno)); err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
rc = gpg_error_from_syserror ();
goto out; goto out;
} }
} }
@ -108,13 +117,11 @@ amkdir_p (char **directory_components)
for (; i < count; i ++) for (; i < count; i ++)
{ {
if (DEBUG) /* log_debug ("Creating directory: %s\n", dirs[i]); */
log_debug ("Creating directory: %s\n", dirs[i]);
rc = mkdir (dirs[i], S_IRUSR | S_IWUSR | S_IXUSR); if (gnupg_mkdir (dirs[i], "-rwx"))
if (rc)
{ {
rc = gpg_error_from_syserror (); err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
goto out; goto out;
} }
} }
@ -124,20 +131,24 @@ amkdir_p (char **directory_components)
xfree (dirs[i]); xfree (dirs[i]);
xfree (dirs); xfree (dirs);
if (DEBUG) /* log_debug ("%s: Returning %s\n", __func__, gpg_strerror (rc)); */
log_debug ("%s: Returning %s\n", __func__, gpg_strerror (rc));
return rc; return err;
} }
int
gpg_error_t
mkdir_p (char *directory_component, ...) mkdir_p (char *directory_component, ...)
{ {
va_list ap; va_list ap;
gpg_error_t err = 0;
int i; int i;
int space = 1; int space = 1;
char **dirs = xmalloc (space * sizeof (char *)); char **dirs;
int rc;
dirs = xtrymalloc (space * sizeof (char *));
if (!dirs)
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
dirs[0] = directory_component; dirs[0] = directory_component;
@ -146,16 +157,26 @@ mkdir_p (char *directory_component, ...)
{ {
if (i == space) if (i == space)
{ {
char **tmp_dirs;
space = 2 * space; space = 2 * space;
dirs = xrealloc (dirs, space * sizeof (char *)); 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;
} }
dirs[i] = va_arg (ap, char *); dirs[i] = va_arg (ap, char *);
} }
va_end (ap); va_end (ap);
rc = amkdir_p (dirs); if (!err)
err = amkdir_p (dirs);
xfree (dirs); xfree (dirs);
return rc; return err;
} }

View File

@ -3,12 +3,22 @@
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
* GnuPG is free software; you can redistribute it and/or modify * This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of either
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* *
* GnuPG is distributed in the hope that it will be useful, * - 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
@ -29,7 +39,7 @@
first try to create the directory "foo/bar" and then the directory first try to create the directory "foo/bar" and then the directory
"foo/bar/xyzzy". On success returns 0, otherwise an error code is "foo/bar/xyzzy". On success returns 0, otherwise an error code is
returned. */ returned. */
int mkdir_p (char *directory_component, ...) GPGRT_ATTR_SENTINEL(0); gpg_error_t mkdir_p (char *directory_component, ...) GPGRT_ATTR_SENTINEL(0);
/* Like mkdir_p, but DIRECTORY_COMPONENTS is a NULL terminated /* Like mkdir_p, but DIRECTORY_COMPONENTS is a NULL terminated
array, e.g.: array, e.g.:
@ -37,6 +47,6 @@ int mkdir_p (char *directory_component, ...) GPGRT_ATTR_SENTINEL(0);
char **dirs = { "foo", "bar", NULL }; char **dirs = { "foo", "bar", NULL };
amkdir_p (dirs); amkdir_p (dirs);
*/ */
int amkdir_p (char **directory_components); gpg_error_t amkdir_p (char **directory_components);
#endif #endif