gpo: move mkdir_p to lib/util
[gd/samba-autobuild/.git] / lib / util / mkdir_p.c
1 /*
2    mkdir -p
3
4    Copyright (C) Amitay Isaacs  2014
5    Copyright (C) Martin Schwenke  2014
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20 #include "replace.h"
21 #include <sys/stat.h>
22 #include <libgen.h>
23 #include "mkdir_p.h"
24
25 int mkdir_p(const char *dir, int mode)
26 {
27         char t[PATH_MAX];
28         ssize_t len;
29         int ret;
30
31         if (strcmp(dir, "/") == 0) {
32                 return 0;
33         }
34
35         if (strcmp(dir, ".") == 0) {
36                 return 0;
37         }
38
39         /* Try to create directory */
40         ret = mkdir(dir, mode);
41         /* Succeed if that worked or if it already existed */
42         if (ret == 0 || errno == EEXIST) {
43                 return 0;
44         }
45         /* Fail on anything else except ENOENT */
46         if (errno != ENOENT) {
47                 return ret;
48         }
49
50         /* Create ancestors */
51         len = strlen(dir);
52         if (len >= PATH_MAX) {
53                 errno = ENAMETOOLONG;
54                 return -1;
55         }
56         strncpy(t, dir, len+1);
57
58         ret = mkdir_p(dirname(t), mode);
59         if (ret != 0) {
60                 return ret;
61         }
62
63         /* Create directory */
64         ret = mkdir(dir, mode);
65         if ((ret == -1) && (errno == EEXIST)) {
66                 ret = 0;
67         }
68
69         return ret;
70 }