[asterisk-commits] tilghman: trunk r71040 - in /trunk: apps/ include/asterisk/ main/ pbx/ res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 21 23:35:13 CDT 2007


Author: tilghman
Date: Thu Jun 21 23:35:12 2007
New Revision: 71040

URL: http://svn.digium.com/view/asterisk?view=rev&rev=71040
Log:
Issue 9990 - New API ast_mkdir, which creates parent directories as necessary (and is faster than an outcall to mkdir -p)

Modified:
    trunk/apps/app_dial.c
    trunk/apps/app_dictate.c
    trunk/apps/app_minivm.c
    trunk/apps/app_sms.c
    trunk/apps/app_test.c
    trunk/apps/app_voicemail.c
    trunk/include/asterisk/utils.h
    trunk/main/logger.c
    trunk/main/utils.c
    trunk/pbx/pbx_spool.c
    trunk/res/res_monitor.c

Modified: trunk/apps/app_dial.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_dial.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/apps/app_dial.c (original)
+++ trunk/apps/app_dial.c Thu Jun 21 23:35:12 2007
@@ -1146,13 +1146,13 @@
 
 		/* make sure the priv-callerintros dir actually exists */
 		snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
-		if (mkdir(pa->privintro, 0755) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(errno));
+		if ((res = ast_mkdir(pa->privintro, 0755))) {
+			ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
 			return -1;
 		}
 
-		snprintf(pa->privintro,sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
-		if (ast_fileexists(pa->privintro,NULL,NULL ) > 0 && strncmp(pa->privcid,"NOCALLERID",10) != 0) {
+		snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
+		if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
 			/* the DELUX version of this code would allow this caller the
 			   option to hear and retape their previously recorded intro.
 			*/

Modified: trunk/apps/app_dictate.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_dictate.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/apps/app_dictate.c (original)
+++ trunk/apps/app_dictate.c Thu Jun 21 23:35:12 2007
@@ -139,7 +139,7 @@
 			ast_copy_string(filein, filename, sizeof(filein));
 			filename = "";
 		}
-		mkdir(base, 0755);
+		ast_mkdir(base, 0755);
 		len = strlen(base) + strlen(filein) + 2;
 		if (!path || len > maxlen) {
 			path = alloca(len);

Modified: trunk/apps/app_minivm.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_minivm.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/apps/app_minivm.c (original)
+++ trunk/apps/app_minivm.c Thu Jun 21 23:35:12 2007
@@ -1159,28 +1159,11 @@
  */
 static int create_dirpath(char *dest, int len, char *domain, char *username, char *folder)
 {
-	mode_t	mode = VOICEMAIL_DIR_MODE;
-
-	if(!ast_strlen_zero(domain)) {
-		make_dir(dest, len, domain, "", "");
-		if(mkdir(dest, mode) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno));
-			return -1;
-		}
-	}
-	if(!ast_strlen_zero(username)) {
-		make_dir(dest, len, domain, username, "");
-		if(mkdir(dest, mode) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno));
-			return -1;
-		}
-	}
-	if(!ast_strlen_zero(folder)) {
-		make_dir(dest, len, domain, username, folder);
-		if(mkdir(dest, mode) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno));
-			return -1;
-		}
+	int res;
+	make_dir(dest, len, domain, username, folder);
+	if ((res = ast_mkdir(dest, 0777))) {
+		ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
+		return -1;
 	}
 	ast_debug(2, "Creating directory for %s@%s folder %s : %s\n", username, domain, folder, dest);
 	return 0;

Modified: trunk/apps/app_sms.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_sms.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/apps/app_sms.c (original)
+++ trunk/apps/app_sms.c Thu Jun 21 23:35:12 2007
@@ -916,10 +916,8 @@
 	char buf[30];
 	FILE *o;
 
-	ast_copy_string(fn, spool_dir, sizeof (fn));
-	mkdir(fn, 0777);			/* ensure it exists */
-	snprintf(fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
-	mkdir(fn, 0777);			/* ensure it exists */
+	snprintf(fn, sizeof(fn), "%s/%s", spool_dir, h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
+	ast_mkdir(fn, 0777);			/* ensure it exists */
 	ast_copy_string(fn2, fn, sizeof (fn2));
 	snprintf(fn2 + strlen (fn2), sizeof (fn2) - strlen (fn2), "/%s.%s-%d", h->queue, isodate(h->scts, buf, sizeof(buf)), seq++);
 	snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/.%s", fn2 + strlen (fn) + 1);
@@ -1339,11 +1337,9 @@
 	char more = 0;
 
 	*h->da = *h->oa = '\0';			/* clear destinations */
-	ast_copy_string (fn, spool_dir, sizeof (fn));
-	mkdir(fn, 0777);			/* ensure it exists */
 	h->rx = 0;				/* outgoing message */
-	snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", h->smsc ? "mttx" : "motx");
-	mkdir (fn, 0777);			/* ensure it exists */
+	snprintf(fn, sizeof(fn), "%s/%s", spool_dir, h->smsc ? "mttx" : "motx");
+	ast_mkdir(fn, 0777);			/* ensure it exists */
 	d = opendir (fn);
 	if (d) {
 		struct dirent *f = readdirqueue (d, h->queue);

Modified: trunk/apps/app_test.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_test.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/apps/app_test.c (original)
+++ trunk/apps/app_test.c Thu Jun 21 23:35:12 2007
@@ -177,7 +177,7 @@
 	if ((res >=0) && (!ast_strlen_zero(testid))) {
 		/* Make the directory to hold the test results in case it's not there */
 		snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
-		mkdir(fn, 0777);
+		ast_mkdir(fn, 0777);
 		snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
 		if ((f = fopen(fn, "w+"))) {
 			setlinebuf(f);
@@ -338,7 +338,7 @@
 		/* Got a Test ID!  Whoo hoo! */
 		/* Make the directory to hold the test results in case it's not there */
 		snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
-		mkdir(fn, 0777);
+		ast_mkdir(fn, 0777);
 		snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
 		if ((f = fopen(fn, "w+"))) {
 			setlinebuf(f);

Modified: trunk/apps/app_voicemail.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_voicemail.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/apps/app_voicemail.c (original)
+++ trunk/apps/app_voicemail.c Thu Jun 21 23:35:12 2007
@@ -939,8 +939,9 @@
 #ifdef IMAP_STORAGE
 static int make_gsm_file(char *dest, char *imapuser, char *dir, int num)
 {
-	if (mkdir(dir, 01777) && (errno != EEXIST)) {
-		ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dir, strerror(errno));
+	int res;
+	if ((res = ast_mkdir(dir, 01777))) {
+		ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dir, strerror(res));
 		return sprintf(dest, "%s/msg%04d", dir, num);
 	}
 	/* return sprintf(dest, "%s/s/msg%04d", dir, imapuser, num); */
@@ -983,27 +984,12 @@
 static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder)
 {
 	mode_t	mode = VOICEMAIL_DIR_MODE;
-
-	if (!ast_strlen_zero(context)) {
-		make_dir(dest, len, context, "", "");
-		if (mkdir(dest, mode) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno));
-			return -1;
-		}
-	}
-	if (!ast_strlen_zero(ext)) {
-		make_dir(dest, len, context, ext, "");
-		if (mkdir(dest, mode) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno));
-			return -1;
-		}
-	}
-	if (!ast_strlen_zero(folder)) {
-		make_dir(dest, len, context, ext, folder);
-		if (mkdir(dest, mode) && errno != EEXIST) {
-			ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno));
-			return -1;
-		}
+	int res;
+
+	make_dir(dest, len, context, ext, folder);
+	if ((res = ast_mkdir(dest, mode))) {
+		ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
+		return -1;
 	}
 	return 0;
 }

Modified: trunk/include/asterisk/utils.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/utils.h?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/include/asterisk/utils.h (original)
+++ trunk/include/asterisk/utils.h Thu Jun 21 23:35:12 2007
@@ -593,6 +593,16 @@
  */
 void ast_enable_packet_fragmentation(int sock);
 
+/*!
+  \brief Recursively create directory path
+  \param path The directory path to create
+  \param mode The permissions with which to try to create the directory
+  \return 0 on success or an error code otherwise
+
+  Creates a directory path, creating parent directories as needed.
+ */
+int ast_mkdir(const char *path, int mode);
+
 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
 
 #include "asterisk/strings.h"

Modified: trunk/main/logger.c
URL: http://svn.digium.com/view/asterisk/trunk/main/logger.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/main/logger.c (original)
+++ trunk/main/logger.c Thu Jun 21 23:35:12 2007
@@ -417,7 +417,7 @@
 		queue_rotate = 0;
 	qlog = NULL;
 
-	mkdir(ast_config_AST_LOG_DIR, 0755);
+	ast_mkdir(ast_config_AST_LOG_DIR, 0777);
 
 	AST_LIST_TRAVERSE(&logchannels, f, list) {
 		if (f->disabled) {
@@ -796,14 +796,13 @@
 	/* register the logger cli commands */
 	ast_cli_register_multiple(cli_logger, sizeof(cli_logger) / sizeof(struct ast_cli_entry));
 
-	mkdir(ast_config_AST_LOG_DIR, 0755);
+	ast_mkdir(ast_config_AST_LOG_DIR, 0777);
   
 	/* create log channels */
 	init_logger_chain();
 
 	/* create the eventlog */
 	if (logfiles.event_log) {
-		mkdir(ast_config_AST_LOG_DIR, 0755);
 		snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG);
 		eventlog = fopen(tmp, "a");
 		if (eventlog) {

Modified: trunk/main/utils.c
URL: http://svn.digium.com/view/asterisk/trunk/main/utils.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/main/utils.c (original)
+++ trunk/main/utils.c Thu Jun 21 23:35:12 2007
@@ -34,6 +34,7 @@
 #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 @@
 		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;
+}
+

Modified: trunk/pbx/pbx_spool.c
URL: http://svn.digium.com/view/asterisk/trunk/pbx/pbx_spool.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/pbx/pbx_spool.c (original)
+++ trunk/pbx/pbx_spool.c Thu Jun 21 23:35:12 2007
@@ -294,7 +294,7 @@
 		unlink(o->fn);
 		return 0;
 	}
-	if (mkdir(qdonedir, 0700) && (errno != EEXIST)) {
+	if (ast_mkdir(qdonedir, 0777)) {
 		ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
 		unlink(o->fn);
 		return -1;
@@ -485,7 +485,7 @@
 	pthread_t thread;
 	int ret;
 	snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
-	if (mkdir(qdir, 0700) && (errno != EEXIST)) {
+	if (ast_mkdir(qdir, 0777)) {
 		ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
 		return 0;
 	}

Modified: trunk/res/res_monitor.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_monitor.c?view=diff&rev=71040&r1=71039&r2=71040
==============================================================================
--- trunk/res/res_monitor.c (original)
+++ trunk/res/res_monitor.c Thu Jun 21 23:35:12 2007
@@ -128,7 +128,6 @@
 		const char *fname_base, int need_lock)
 {
 	int res = 0;
-	char tmp[256];
 
 	LOCK_IF_NEEDED(chan, need_lock);
 
@@ -137,12 +136,7 @@
 		char *channel_name, *p;
 
 		/* Create monitoring directory if needed */
-		if (mkdir(ast_config_AST_MONITOR_DIR, 0770) < 0) {
-			if (errno != EEXIST) {
-				ast_log(LOG_WARNING, "Unable to create audio monitor directory: %s\n",
-					strerror(errno));
-			}
-		}
+		ast_mkdir(ast_config_AST_MONITOR_DIR, 0777);
 
 		if (!(monitor = ast_calloc(1, sizeof(*monitor)))) {
 			UNLOCK_IF_NEEDED(chan, need_lock);
@@ -154,10 +148,8 @@
 			int directory = strchr(fname_base, '/') ? 1 : 0;
 			/* try creating the directory just in case it doesn't exist */
 			if (directory) {
-				char *name = ast_strdup(fname_base);
-				snprintf(tmp, sizeof(tmp), "mkdir -p \"%s\"",dirname(name));
-				ast_free(name);
-				ast_safe_system(tmp);
+				char *name = ast_strdupa(fname_base);
+				ast_mkdir(dirname(name), 0777);
 			}
 			snprintf(monitor->read_filename, FILENAME_MAX, "%s/%s-in",
 						directory ? "" : ast_config_AST_MONITOR_DIR, fname_base);
@@ -354,7 +346,6 @@
 /* Change monitoring filename of a channel */
 int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, int need_lock)
 {
-	char tmp[256];
 	if (ast_strlen_zero(fname_base)) {
 		ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to null\n", chan->name);
 		return -1;
@@ -366,10 +357,8 @@
 		int directory = strchr(fname_base, '/') ? 1 : 0;
 		/* try creating the directory just in case it doesn't exist */
 		if (directory) {
-			char *name = ast_strdup(fname_base);
-			snprintf(tmp, sizeof(tmp), "mkdir -p %s",dirname(name));
-			ast_free(name);
-			ast_safe_system(tmp);
+			char *name = ast_strdupa(fname_base);
+			ast_mkdir(dirname(name), 0777);
 		}
 
 		snprintf(chan->monitor->filename_base, FILENAME_MAX, "%s/%s", directory ? "" : ast_config_AST_MONITOR_DIR, fname_base);




More information about the asterisk-commits mailing list