Lua挂载文件系统

时间:2020-10-19 21:03:31

标签: lua filesystems mount

我想使用Lua在Linux上挂载文件系统,但是我没有在lua 5.4 manualLuaFileSytem库中找到任何功能。有什么方法可以在Lua或现有库中挂载文件系统?

1 个答案:

答案 0 :(得分:1)

与大多数依赖平台的syscall一样,Lua不会提供此类映射。 因此,您将需要一些可完成此操作的C-API模块。 看来https://github.com/justincormack/ljsyscall是通用的“但”专注于LuaJIT,而https://luaposix.github.io/luaposix/没有提供mount

我最近有类似的需求,我结束了C模块的开发:

static int l_mount(lua_State* L)
{
    int res = 0;
    // TODO add more checks on args!
    const char *source = luaL_checkstring(L, 1);
    const char *target = luaL_checkstring(L, 2);
    const char *type   = luaL_checkstring(L, 3);
    lua_Integer flags  = luaL_checkinteger(L, 4);
    const char *data   = luaL_checkstring(L, 5);

    res = mount(source, target, type, flags, data);
    if ( res != 0)
    {
        int err = errno;
        lua_pushnil(L);
        lua_pushfstring(L, "mount failed: errno[%s]", strerror(err));
        return 2;
    }
    else
    {
        lua_pushfstring(L, "ok");
        return 1;
    }
}

#define register_constant(s)\
    lua_pushinteger(L, s);\
    lua_setfield(L, -2, #s);

// Module functions
static const luaL_Reg R[] =
{
    { "mount", l_mount },
    { NULL, NULL }
};

int luaopen_sysutils(lua_State* L)
{
    luaL_newlib(L, R);

    // do more mount defines mapping, maybe in some table.
    register_constant(MS_RDONLY);
    //...
    
    return 1;
}

将其编译为C Lua模块,不要忘记需要CAP_SYS_ADMIN来调用mount syscall。

相关问题