[svn-commits] mnicholson: branch mnicholson/asttest r183687 - /team/mnicholson/asttest/astt...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sun Mar 22 22:04:26 CDT 2009


Author: mnicholson
Date: Sun Mar 22 22:04:22 2009
New Revision: 183687

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=183687
Log:
partially implemented asterisk:spawn()

Modified:
    team/mnicholson/asttest/asttest/lua/astlib.c

Modified: team/mnicholson/asttest/asttest/lua/astlib.c
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/lua/astlib.c?view=diff&rev=183687&r1=183686&r2=183687
==============================================================================
--- team/mnicholson/asttest/asttest/lua/astlib.c (original)
+++ team/mnicholson/asttest/asttest/lua/astlib.c Sun Mar 22 22:04:22 2009
@@ -22,8 +22,150 @@
 
 #include "astlib_lua.h"
 
+#include <dirent.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/*!
+ * \brief Recursively symlink and copy a directory.
+ * \param src the source directory
+ * \param dst the destination directory
+ *
+ * This function recursively creates symlinks to files in src in the dst
+ * directory.  It does not symlink directories and instead makes new
+ * directories in dst matching the corisponding dir in src.
+ *
+ * \note On error an error message is pushed onto the given lua stack.
+ *
+ * \retval 0 success
+ * \retval -1 error
+ */
+static int symlink_copy_dir(lua_State *L, const char *src, const char *dst) {
+	DIR *src_dir;
+	struct dirent *d;
+	char src_path[PATH_MAX], dst_path[PATH_MAX];
+	struct stat st;
+
+	if (!(src_dir = opendir(src))) {
+		lua_pushstring(L, "error opening dir '");
+		lua_pushstring(L, src);
+		lua_pushstring(L, "': ");
+		lua_pushstring(L, strerror(errno));
+		lua_concat(L, 4);
+		goto e_return;
+	}
+
+	while ((d = readdir(src_dir))) {
+		snprintf(src_path, sizeof(src_path), "%s/%s", src, d->d_name);
+		snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, d->d_name);
+
+		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
+			continue;
+		}
+
+		if (stat(src_path, &st)) {
+			lua_pushstring(L, "error with stat for '");
+			lua_pushstring(L, src_path);
+			lua_pushstring(L, "': ");
+			lua_pushstring(L, strerror(errno));
+			lua_concat(L, 4);
+			goto e_closedir;
+		}
+
+		if (S_ISDIR(st.st_mode)) {
+			if (mkdir(dst_path, st.st_mode)) {
+				lua_pushstring(L, "error creating dir '");
+				lua_pushstring(L, dst_path);
+				lua_pushstring(L, "': ");
+				lua_pushstring(L, strerror(errno));
+				lua_concat(L, 4);
+				goto e_closedir;
+			}
+
+			if (symlink_copy_dir(L, src_path, dst_path)) {
+				goto e_closedir;
+			}
+		} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
+			if (symlink(src_path, dst_path)) {
+				lua_pushstring(L, "error symlink '");
+				lua_pushstring(L, dst_path);
+				lua_pushstring(L, "': ");
+				lua_pushstring(L, strerror(errno));
+				lua_concat(L, 4);
+				goto e_closedir;
+			}
+		} else {
+			/* unsupported file type */
+			lua_pushstring(L, "unsupported file type");
+			return lua_error(L);
+			lua_pushstring(L, "don't know how to symlink '");
+			lua_pushstring(L, src_path);
+			lua_pushstring(L, "'");
+			lua_concat(L, 3);
+			goto e_closedir;
+		}
+	}
+
+	closedir(src_dir);
+	return 0;
+
+e_closedir:
+	closedir(src_dir);
+e_return:
+	return -1;
+}
+
 static int spawn_asterisk(lua_State *L) {
-	return 1;
+	int asterisk_count;
+	const char *asterisk_path;
+	char dir_path[PATH_MAX];
+	mode_t dir_mode = S_IRWXU | S_IRGRP| S_IXGRP| S_IROTH | S_IXOTH;
+
+	/* get the index for this instance */
+	lua_getfield(L, LUA_REGISTRYINDEX, "astlib_count");
+	asterisk_count = lua_tointeger(L, -1);
+
+	/* increment the count */
+	lua_pushinteger(L, asterisk_count + 1);
+	lua_setfield(L, LUA_REGISTRYINDEX, "astlib_count");
+	lua_pop(L, 1);
+
+	lua_getfield(L, LUA_REGISTRYINDEX, "astlib_path");
+	asterisk_path = lua_tostring(L, -1);
+
+	snprintf(dir_path, sizeof(dir_path), "tmp/ast%d", asterisk_count);
+	if (mkdir("tmp", dir_mode) && errno != EEXIST) {
+		lua_pop(L, 1); /* remove the astlib_path */
+		lua_pushstring(L, "error creating tmp directory while spawning asterisk: ");
+		lua_pushstring(L, strerror(errno));
+		lua_concat(L, 2);
+		return lua_error(L);
+	}
+
+	if (mkdir(dir_path, dir_mode)) {
+		lua_pop(L, 1); /* remove the astlib_path */
+		lua_pushstring(L, "error spawning asterisk, unable to create working dir (");
+		lua_pushstring(L, dir_path);
+		lua_pushstring(L, "): ");
+		lua_pushstring(L, strerror(errno));
+		lua_concat(L, 4);
+		return lua_error(L);
+	}
+
+	if (symlink_copy_dir(L, asterisk_path, dir_path)) {
+		lua_remove(L, -2); /* remove the astlib_path */
+		lua_pushstring(L, "\nerror initilizing working environment");
+		lua_concat(L, 2);
+		return lua_error(L);
+	}
+
+	lua_pop(L, 1); /* remove the astlib_path */
+
+	/* XXX now start asterisk */
+	return 0;
 }
 
 static luaL_Reg astlib[] = {
@@ -41,6 +183,9 @@
 	/* set up some registry values */
 	lua_pushstring(L, asterisk_path);
 	lua_setfield(L, LUA_REGISTRYINDEX, "astlib_path");
+
+	lua_pushinteger(L, 1);
+	lua_setfield(L, LUA_REGISTRYINDEX, "astlib_count");
 
 	/* register our functions */
 	luaL_register(L, "ast", astlib);




More information about the svn-commits mailing list