pdb_samba_dsdb: implement PDB_CAP_TRUSTED_DOMAINS_EX related functions
[Samba.git] / lib / util / mkdir_p.c
blob290a1f395206436269901477ac8840a25886f32d
1 /*
2 mkdir -p
4 Copyright (C) Amitay Isaacs 2014
5 Copyright (C) Martin Schwenke 2014
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.
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.
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/>.
20 #include "replace.h"
21 #include <sys/stat.h>
22 #include <libgen.h>
23 #include "mkdir_p.h"
25 int mkdir_p(const char *dir, int mode)
27 char t[PATH_MAX];
28 ssize_t len;
29 int ret;
31 if (strcmp(dir, "/") == 0) {
32 return 0;
35 if (strcmp(dir, ".") == 0) {
36 return 0;
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;
45 /* Fail on anything else except ENOENT */
46 if (errno != ENOENT) {
47 return ret;
50 /* Create ancestors */
51 len = strlen(dir);
52 if (len >= PATH_MAX) {
53 errno = ENAMETOOLONG;
54 return -1;
56 strncpy(t, dir, len+1);
58 ret = mkdir_p(dirname(t), mode);
59 if (ret != 0) {
60 return ret;
63 /* Create directory */
64 ret = mkdir(dir, mode);
65 if ((ret == -1) && (errno == EEXIST)) {
66 ret = 0;
69 return ret;