From eac465012ac643586a989f72e79afd72180866b8 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Thu, 21 Sep 2017 09:45:59 -0500 Subject: [PATCH] mkdir now creates root directories needed --- include/libp2p/os/utils.h | 7 +++++++ os/utils.c | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/libp2p/os/utils.h b/include/libp2p/os/utils.h index d2ec91f..c198952 100644 --- a/include/libp2p/os/utils.h +++ b/include/libp2p/os/utils.h @@ -92,4 +92,11 @@ unsigned long long os_utils_gmtime(); */ char *strnstr(const char *haystack, const char *needle, size_t len); +/** + * Make a directory, and any directories before it + * @param path the path to create + * @returns true(1) on success, false(0) otherwise + */ +int os_mkdir(char* path); + #endif /* utils_h */ diff --git a/os/utils.c b/os/utils.c index 5f6649e..4fcc646 100644 --- a/os/utils.c +++ b/os/utils.c @@ -4,6 +4,7 @@ #include #include #include +#include #ifndef __MINGW32__ #include #endif @@ -217,3 +218,42 @@ char *strnstr(const char *haystack, const char *needle, size_t len) } return NULL; } + +/*** + * Make a directory, as well as any directories not already there + * @param path the path to create + * @returns true(1) on success, false(0) otherwise + */ +int os_mkdir(char* dir) { + const mode_t mode = S_IRWXU; + char tmp[256]; + char *p = NULL; + size_t len; + + snprintf(tmp, sizeof(tmp),"%s",dir); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = 0; + for(p = tmp + 1; *p; p++) { + if(*p == '/') { + *p = 0; +#ifdef __MINGW32__ + if (mkdir(tmp) != 0) { +#else + if (mkdir(tmp, mode) != 0 && errno != EEXIST) { +#endif + fprintf(stderr, "Unable to create directory %s.\n", tmp); + return 0; + } + *p = '/'; + } + } +#ifdef __MINGW32__ + if (mkdir(tmp) != 0) { +#else + if (mkdir(tmp, mode) != 0) { +#endif + return 0; + } + return 1; +}