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
|
/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include "libi3.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifndef HAVE_MKDIRP
/*
* Emulates mkdir -p (creates any missing folders)
*
*/
int mkdirp(const char *path, mode_t mode) {
if (mkdir(path, mode) == 0) {
return 0;
}
if (errno == EEXIST) {
struct stat st;
/* Check that the named file actually is a directory. */
if (stat(path, &st)) {
ELOG("stat(%s) failed: %s\n", path, strerror(errno));
return -1;
}
if (!S_ISDIR(st.st_mode)) {
ELOG("mkdir(%s) failed: %s\n", path, strerror(ENOTDIR));
return -1;
}
return 0;
} else if (errno != ENOENT) {
ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
return -1;
}
char *copy = sstrdup(path);
/* strip trailing slashes, if any */
while (copy[strlen(copy) - 1] == '/') {
copy[strlen(copy) - 1] = '\0';
}
char *sep = strrchr(copy, '/');
if (sep == NULL) {
free(copy);
return -1;
}
*sep = '\0';
int result = -1;
if (mkdirp(copy, mode) == 0) {
result = mkdirp(path, mode);
}
free(copy);
return result;
}
#endif
|