summaryrefslogtreecommitdiffstats
path: root/common/mkdir_p.c
blob: 6fb98c3709706ccc08115eebaf12ad4208dcfe44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* mkdir_p.c - Create a directory and any missing parents.
 * Copyright (C) 2015 g10 Code GmbH
 *
 * This file is part of GnuPG.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of either
 *
 *   - 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
 * 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
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>

#include "util.h"
#include "stringhelp.h"
#include "logging.h"
#include "sysutils.h"
#include "mkdir_p.h"


gpg_error_t
gnupg_amkdir_p (const char **directory_components)
{
  gpg_error_t err = 0;
  int count;
  char **dirs;
  int i;

  for (count = 0; directory_components[count]; count ++)
    ;

  /* log_debug ("%s: %d directory components.\n", __func__, count); */

  dirs = xtrycalloc (count, sizeof *dirs);
  if (!dirs)
    return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());

  for (i = 0; directory_components[i]; i ++)
    {
      if (i == 0)
	dirs[i] = make_filename_try (directory_components[i], NULL);
      else
	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;
        }

      /* log_debug ("%s: Directory %d: `%s'.\n", __func__, i, dirs[i]); */
    }

  for (i = count - 1; i >= 0; i --)
    {
      struct stat s;

      /* log_debug ("%s: stat(%s)\n", __func__, dirs[i]); */

      if (!gnupg_stat (dirs[i], &s))
        {
          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;
            }
        }
      else if (errno == ENOENT)
	/* This directory does not exist yet.  Continue walking up the
	   hierarchy.  */
	{
          /* log_debug ("%s: %s does not exist!\n", */
          /*            __func__, dirs[i]); */
	  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.  */
	{
          /* log_debug ("%s: stat(%s) => %s!\n", */
          /*            __func__, dirs[i], strerror (errno)); */
	  err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
	  goto out;
	}
    }

  assert (i >= -1);
  /* DIRS[I] exists.  Start with the following entry.  */
  i ++;

  for (; i < count; i ++)
    {
      /* log_debug ("Creating directory: %s\n", dirs[i]); */

      if (gnupg_mkdir (dirs[i], "-rwx"))
	{
	  err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
	  goto out;
	}
    }

 out:
  for (i = 0; i < count; i ++)
    xfree (dirs[i]);
  xfree (dirs);

  /* log_debug ("%s: Returning %s\n", __func__, gpg_strerror (rc)); */

  return err;
}


gpg_error_t
gnupg_mkdir_p (const char *directory_component, ...)
{
  va_list ap;
  gpg_error_t err = 0;
  int i;
  int space = 1;
  const char **dirs;

  dirs = xtrymalloc (space * sizeof (char *));
  if (!dirs)
    return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());

  dirs[0] = directory_component;

  va_start (ap, directory_component);
  for (i = 1; dirs[i - 1]; i ++)
    {
      if (i == space)
	{
          const char **tmp_dirs;

	  space = 2 * space;
	  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 *);
    }
  va_end (ap);

  if (!err)
    err = gnupg_amkdir_p (dirs);

  xfree (dirs);

  return err;
}