aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/utils.c
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2008-08-28 07:47:55 +0000
committerTobias Brunner <tobias@strongswan.org>2008-08-28 07:47:55 +0000
commit6c20579a43a73916574f887bf6dc67bc122c0c65 (patch)
treed3d1f513c4596ade3ee3b25816f57e66a62bad76 /src/libstrongswan/utils.c
parent018ae7c1aa68047c49f446c5d26d8433038c74eb (diff)
downloadstrongswan-6c20579a43a73916574f887bf6dc67bc122c0c65.tar.bz2
strongswan-6c20579a43a73916574f887bf6dc67bc122c0c65.tar.xz
mkdir_p: utility function to create a directory and all required parent directories
Diffstat (limited to 'src/libstrongswan/utils.c')
-rw-r--r--src/libstrongswan/utils.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c
index 416c8ef44..88aa1bf79 100644
--- a/src/libstrongswan/utils.c
+++ b/src/libstrongswan/utils.c
@@ -1,4 +1,5 @@
/*
+ * Copyright (C) 2008 Tobias Brunner
* Copyright (C) 2005-2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -17,11 +18,15 @@
#include "utils.h"
+#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>
+#include <unistd.h>
+#include <dirent.h>
#include <enum.h>
+#include <debug.h>
ENUM(status_names, SUCCESS, DESTROY_ME,
"SUCCESS",
@@ -64,6 +69,52 @@ void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
}
/**
+ * Described in header.
+ */
+bool mkdir_p(const char *path, mode_t mode)
+{
+ size_t len;
+ char *pos, full[PATH_MAX];
+ pos = full;
+ if (!path || *path == '\0')
+ {
+ return TRUE;
+ }
+ len = snprintf(full, sizeof(full)-1, "%s", path);
+ if (len < 0 || len >= sizeof(full)-1)
+ {
+ DBG1("path string %s too long", path);
+ return FALSE;
+ }
+ /* ensure that the path ends with a '/' */
+ if (full[len-1] != '/')
+ {
+ full[len++] = '/';
+ full[len] = '\0';
+ }
+ /* skip '/' at the beginning */
+ while (*pos == '/')
+ {
+ pos++;
+ }
+ while ((pos = strchr(pos, '/')))
+ {
+ *pos = '\0';
+ if (access(full, F_OK) < 0)
+ {
+ if (mkdir(full, mode) < 0)
+ {
+ DBG1("failed to create directory %s", full);
+ return FALSE;
+ }
+ }
+ *pos = '/';
+ pos++;
+ }
+ return TRUE;
+}
+
+/**
* return null
*/
void *return_null()