From 16ba86827fca12e27e2bf535e13d82e7ff36d636 Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Thu, 22 Sep 2016 09:15:55 +1000 Subject: [PATCH] Load script from pkgdatadir if not local If the --test=XXX{.lua} script isn't local, load it from the pkgdatadir where some inbuilt scripts are installed. Autoappend the .lua extension if this wasn't done alrady. Here we set the LUA_PATH so that further lua 'require' directives also pull the source from the pkgdatadir. Signed-off-by: Daniel Black --- sysbench/scripting/Makefile.am | 2 +- sysbench/scripting/script_lua.c | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/sysbench/scripting/Makefile.am b/sysbench/scripting/Makefile.am index d8f1868..331e2d8 100644 --- a/sysbench/scripting/Makefile.am +++ b/sysbench/scripting/Makefile.am @@ -18,7 +18,7 @@ if USE_LUA SUBDIRS = lua . lua_sources = script_lua.c script_lua.h -AM_CPPFLAGS += -I$(srcdir)/lua/src +AM_CPPFLAGS += -I$(srcdir)/lua/src -DDATA_PATH=\"$(pkgdatadir)\" endif noinst_LIBRARIES = libsbscript.a diff --git a/sysbench/scripting/script_lua.c b/sysbench/scripting/script_lua.c index f14c33e..108dd3b 100644 --- a/sysbench/scripting/script_lua.c +++ b/sysbench/scripting/script_lua.c @@ -29,6 +29,8 @@ #include "db_driver.h" +#include + #define EVENT_FUNC "event" #define PREPARE_FUNC "prepare" #define CLEANUP_FUNC "cleanup" @@ -169,6 +171,8 @@ int script_load_lua(const char *testname, sb_test_t *test) { unsigned int i; + setenv("LUA_PATH", DATA_PATH LUA_DIRSEP "?.lua", 0); + /* Initialize global interpreter state */ gstate = sb_lua_new_state(testname, -1); if (gstate == NULL) @@ -488,8 +492,27 @@ lua_State *sb_lua_new_state(const char *scriptname, int thread_id) luaL_newmetatable(state, "sysbench.stmt"); luaL_newmetatable(state, "sysbench.rs"); - - if (luaL_loadfile(state, scriptname) || lua_pcall(state, 0, 0, 0)) + + if (luaL_loadfile(state, scriptname)) + { + /* first location failed - look in DATA_PATH */ + char p[PATH_MAX + 1]; + strncpy(p, DATA_PATH LUA_DIRSEP, sizeof(p)); + strncat(p, scriptname, sizeof(p)); + if (!strrchr(scriptname, '.')) + { + /* add .lua extension if there isn't one */ + strncat(p, ".lua", sizeof(p)); + } + + if (luaL_loadfile(state, p)) + { + lua_error(state); + return NULL; + } + } + + if (lua_pcall(state, 0, 0, 0)) { lua_error(state); return NULL;