2015-04-20 13:02:11 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
int
|
2016-12-14 22:40:06 -05:00
|
|
|
mkdirp(const char *path, mode_t mode, mode_t pmode)
|
2015-04-20 13:02:11 -04:00
|
|
|
{
|
|
|
|
char tmp[PATH_MAX], *p;
|
2016-12-14 22:40:05 -05:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
return 0;
|
|
|
|
errno = ENOTDIR;
|
|
|
|
weprintf("%s:", path);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-20 13:02:11 -04:00
|
|
|
|
|
|
|
estrlcpy(tmp, path, sizeof(tmp));
|
|
|
|
for (p = tmp + (tmp[0] == '/'); *p; p++) {
|
|
|
|
if (*p != '/')
|
|
|
|
continue;
|
|
|
|
*p = '\0';
|
2016-12-14 22:40:06 -05:00
|
|
|
if (mkdir(tmp, pmode) < 0 && errno != EEXIST) {
|
2015-04-20 13:02:11 -04:00
|
|
|
weprintf("mkdir %s:", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = '/';
|
|
|
|
}
|
2016-12-14 22:40:06 -05:00
|
|
|
if (mkdir(tmp, mode) < 0 && errno != EEXIST) {
|
2015-04-20 13:02:11 -04:00
|
|
|
weprintf("mkdir %s:", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|