[lxc-devel] [PATCH] lua: implement dirname in C rather than depen on external executable

Natanael Copa ncopa at alpinelinux.org
Thu Sep 5 13:29:20 UTC 2013


Instead of popen and run external executable dirname we implement a
dirname in C in the core module.

We also remove the unused basename function.

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
---
 src/lua-lxc/core.c  |  8 ++++++++
 src/lua-lxc/lxc.lua | 18 +-----------------
 2 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c
index 504e147..002e8bf 100644
--- a/src/lua-lxc/core.c
+++ b/src/lua-lxc/core.c
@@ -28,6 +28,7 @@
 #include <assert.h>
 #include <string.h>
 #include <unistd.h>
+#include <libgen.h>
 #include <lxc/lxccontainer.h>
 
 #if LUA_VERSION_NUM < 502
@@ -387,11 +388,18 @@ static int lxc_util_usleep(lua_State *L) {
     return 0;
 }
 
+static int lxc_util_dirname(lua_State *L) {
+    char *path = strdupa(luaL_checkstring(L, 1));
+    lua_pushstring(L, dirname(path));
+    return 1;
+}
+
 static luaL_Reg lxc_lib_methods[] = {
     {"version_get",		lxc_version_get},
     {"default_config_path_get",	lxc_default_config_path_get},
     {"container_new",		container_new},
     {"usleep",			lxc_util_usleep},
+    {"dirname",			lxc_util_dirname},
     {NULL, NULL}
 };
 
diff --git a/src/lua-lxc/lxc.lua b/src/lua-lxc/lxc.lua
index 4ef1ff9..2f1204c 100755
--- a/src/lua-lxc/lxc.lua
+++ b/src/lua-lxc/lxc.lua
@@ -70,22 +70,6 @@ function string:split(delim, max_cols)
     return cols
 end
 
-function dirname(path)
-    local f,output
-    f = io.popen("dirname " .. path)
-    output = f:read('*all')
-    f:close()
-    return output:sub(1,-2)
-end
-
-function basename(path, suffix)
-    local f,output
-    f = io.popen("basename " .. path .. " " .. (suffix or ""))
-    output = f:read('*all')
-    f:close()
-    return output:sub(1,-2)
-end
-
 function cgroup_path_get()
     local f,line,cgroup_path
 
@@ -99,7 +83,7 @@ function cgroup_path_get()
 	    end
 	    c = line:split(" ", 6)
 	    if (c[1] == "cgroup") then
-		cgroup_path = dirname(c[2])
+		cgroup_path = core.dirname(c[2])
 		break
 	    end
 	end
-- 
1.8.4





More information about the lxc-devel mailing list