diff options
author | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2007-06-22 04:35:12 +0000 |
---|---|---|
committer | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2007-06-22 04:35:12 +0000 |
commit | ed2b193e6c6d0cce882796969369e305075103e0 (patch) | |
tree | 45f02eef0ac120e193459b71f73a7b62b7db8784 /main/utils.c | |
parent | de3fbdf9699a307db497cd68d3d9d044936d7b4f (diff) |
Issue 9990 - New API ast_mkdir, which creates parent directories as necessary (and is faster than an outcall to mkdir -p)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@71040 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/utils.c')
-rw-r--r-- | main/utils.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/main/utils.c b/main/utils.c index 7570ca608..65139356d 100644 --- a/main/utils.c +++ b/main/utils.c @@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include <errno.h> #include <stdarg.h> #include <stdio.h> +#include <sys/stat.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> @@ -1058,3 +1059,39 @@ void ast_enable_packet_fragmentation(int sock) ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n"); #endif /* HAVE_IP_MTU_DISCOVER */ } + +int ast_mkdir(const char *path, int mode) +{ + char *ptr; + int len = strlen(path), count = 0, x, piececount = 0; + char *tmp = ast_strdupa(path); + char **pieces; + char *fullpath = alloca(len + 1); + int res = 0; + + for (ptr = tmp; *ptr; ptr++) { + if (*ptr == '/') + count++; + } + + /* Count the components to the directory path */ + pieces = alloca(count * sizeof(*pieces)); + for (ptr = tmp; *ptr; ptr++) { + if (*ptr == '/') { + *ptr = '\0'; + pieces[piececount++] = ptr + 1; + } + } + + *fullpath = '\0'; + for (x = 0; x < piececount; x++) { + /* This looks funky, but the buffer is always ideally-sized, so it's fine. */ + strcat(fullpath, "/"); + strcat(fullpath, pieces[x]); + res = mkdir(fullpath, mode); + if (res && errno != EEXIST) + return errno; + } + return 0; +} + |