-
Notifications
You must be signed in to change notification settings - Fork 18
/
mkpath.c
49 lines (43 loc) · 1.04 KB
/
mkpath.c
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
/* Function with behaviour like `mkdir -p' */
/* From: http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/
* with some tweaks
* libglib'i'fied by Miek Gieben
*/
#include "rdup-up.h"
int mkpath(const char *s, mode_t mode)
{
char *q, *r = NULL, *path = NULL, *up = NULL;
int rv = -1;
if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
return 0;
#ifdef DEBUG
msgd(__func__, __LINE__, _("Path ele '%s\'"), s);
#endif /* DEBUG */
if ((path = g_strdup(s)) == NULL)
return -1;
if ((q = g_strdup(s)) == NULL)
return -1;
if ((r = dirname(q)) == NULL)
goto out;
if ((up = g_strdup(r)) == NULL)
return -1;
if ((mkpath(up, mode) == -1) && (errno != EEXIST)) {
msgd(__func__, __LINE__, _("Failed or exists '%s\': %s"), up,
strerror(errno));
goto out;
}
if ((mkdir(path, mode) == -1) && (errno != EEXIST))
msgd(__func__, __LINE__,
_("Failed to create directory '%s\': %s"), path,
strerror(errno));
else
rv = 0;
out:
if (up)
g_free(up);
if (q)
g_free(q);
if (path)
g_free(path);
return rv;
}