]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lua: implement dirname in C rather than depend on external executable
authorNatanael Copa <ncopa@alpinelinux.org>
Thu, 5 Sep 2013 13:29:20 +0000 (15:29 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Thu, 5 Sep 2013 23:58:28 +0000 (19:58 -0400)
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@alpinelinux.org>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lua-lxc/core.c
src/lua-lxc/lxc.lua

index 504e14785a6d865a8b0e555d72fd693b3f65daed..002e8bf6666bed247f2dae05000a27dbc6ed87c7 100644 (file)
@@ -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}
 };
 
index ae47c0dd670443c7606125abd36b4e2f238bd77f..3f41d6a8a29c6cc854c760817ea81fda4ebfb5b5 100755 (executable)
@@ -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