From c25c2970a6aabc45ee6375cc127ed45efea2f9bf Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 5 Sep 2013 15:29:20 +0200 Subject: [PATCH] lua: implement dirname in C rather than depend on external executable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Acked-by: Stéphane Graber --- 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 504e14785..002e8bf66 100644 --- a/src/lua-lxc/core.c +++ b/src/lua-lxc/core.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #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 ae47c0dd6..3f41d6a8a 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