summaryrefslogtreecommitdiffstats
path: root/Controler-afb/ctl-lua.c
diff options
context:
space:
mode:
authorfulup <fulup.arfoll@iot.bzh>2017-08-11 08:18:05 +0200
committerfulup <fulup.arfoll@iot.bzh>2017-08-11 08:24:20 +0200
commitc13952824e2cc110b9405e1bc9017801dd938ac0 (patch)
treec85896cd8453a103e30eb0d47b8d8448ea6dab4b /Controler-afb/ctl-lua.c
parenteb6a78be524aaee70fca55b86a7b065763591178 (diff)
Update to new Tempate
Diffstat (limited to 'Controler-afb/ctl-lua.c')
-rw-r--r--Controler-afb/ctl-lua.c106
1 files changed, 54 insertions, 52 deletions
diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c
index 8b40e38..38a3d5f 100644
--- a/Controler-afb/ctl-lua.c
+++ b/Controler-afb/ctl-lua.c
@@ -70,66 +70,35 @@ typedef enum {
* https://stackoverflow.com/questions/45596493/lua-using-lua-newuserdata-from-lua-pcall
*/
-STATIC LuaAfbContextT *LuaCtxCheck (lua_State *luaState, int index) {
- LuaAfbContextT *afbContext;
- //luaL_checktype(luaState, index, LUA_TUSERDATA);
- //afbContext = (LuaAfbContextT *)luaL_checkudata(luaState, index, CTX_TOKEN);
- luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA);
- afbContext = (LuaAfbContextT *) lua_touserdata(luaState, index);
- if (afbContext == NULL && afbContext->ctxMagic != CTX_MAGIC) {
- luaL_error(luaState, "Fail to retrieve user data context=%s", CTX_TOKEN);
- AFB_ERROR ("afbContextCheck error retrieving afbContext");
- return NULL;
- }
- return afbContext;
-}
-
-STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, const char* info) {
- // LuaAfbContextT *afbContext = (LuaAfbContextT *)lua_newuserdata(luaState, sizeof(LuaAfbContextT));
- // luaL_setmetatable(luaState, CTX_TOKEN);
- LuaAfbContextT *afbContext = (LuaAfbContextT *)calloc(1, sizeof(LuaAfbContextT));
- lua_pushlightuserdata(luaState, afbContext);
- if (!afbContext) {
- AFB_ERROR ("LuaCtxPush fail to allocate user data context");
- return NULL;
- }
- afbContext->ctxMagic=CTX_MAGIC;
- afbContext->info=strdup(info);
- afbContext->request= request;
- return afbContext;
-}
-
-STATIC void LuaCtxFree (LuaAfbContextT *afbContext) {
- free (afbContext->info);
-}
-
// List Avaliable Configuration Files
-PUBLIC json_object* ScanForConfig (char* searchPath, char *pre, char *ext) {
+PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, char *pre, char *ext) {
json_object *responseJ;
- DIR *dirHandle;
char *dirPath;
char* dirList= strdup(searchPath);
size_t extLen=0;
- void ScanDir (char *dirpath) {
+ void ScanDir (char *searchPath) {
+ DIR *dirHandle;
struct dirent *dirEnt;
- dirHandle = opendir (dirPath);
+ dirHandle = opendir (searchPath);
if (!dirHandle) {
- AFB_NOTICE ("CONFIG-SCANNING dir=%s not readable", dirPath);
+ AFB_NOTICE ("CONFIG-SCANNING dir=%s not readable", searchPath);
return;
}
- AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", dirPath);
+ //AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", searchPath);
while ((dirEnt = readdir(dirHandle)) != NULL) {
// recursively search embedded directories ignoring any directory starting by '.' or '_'
- if (dirEnt->d_type == DT_DIR) {
+ if (dirEnt->d_type == DT_DIR && mode == CTL_SCAN_RECURSIVE) {
char newpath[CONTROL_MAXPATH_LEN];
if (dirEnt->d_name[0]=='.' || dirEnt->d_name[0]=='_') continue;
- strncpy(newpath, dirpath, sizeof(newpath));
+ strncpy(newpath, searchPath, sizeof(newpath));
strncat(newpath, "/", sizeof(newpath));
strncat(newpath, dirEnt->d_name, sizeof(newpath));
+ ScanDir(newpath);
+ continue;
}
// Unknown type is accepted to support dump filesystems
@@ -142,12 +111,12 @@ PUBLIC json_object* ScanForConfig (char* searchPath, char *pre, char *ext) {
if (ext && strcasecmp (ext, &dirEnt->d_name[extIdx])) continue;
struct json_object *pathJ = json_object_new_object();
- json_object_object_add(pathJ, "dirpath", json_object_new_string(dirPath));
+ json_object_object_add(pathJ, "fullpath", json_object_new_string(searchPath));
json_object_object_add(pathJ, "filename", json_object_new_string(dirEnt->d_name));
json_object_array_add(responseJ, pathJ);
}
- closedir(dirHandle);
}
+ closedir(dirHandle);
}
if (ext) extLen=strlen(ext);
@@ -162,6 +131,39 @@ PUBLIC json_object* ScanForConfig (char* searchPath, char *pre, char *ext) {
return (responseJ);
}
+STATIC LuaAfbContextT *LuaCtxCheck (lua_State *luaState, int index) {
+ LuaAfbContextT *afbContext;
+ //luaL_checktype(luaState, index, LUA_TUSERDATA);
+ //afbContext = (LuaAfbContextT *)luaL_checkudata(luaState, index, CTX_TOKEN);
+ luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA);
+ afbContext = (LuaAfbContextT *) lua_touserdata(luaState, index);
+ if (afbContext == NULL && afbContext->ctxMagic != CTX_MAGIC) {
+ luaL_error(luaState, "Fail to retrieve user data context=%s", CTX_TOKEN);
+ AFB_ERROR ("afbContextCheck error retrieving afbContext");
+ return NULL;
+ }
+ return afbContext;
+}
+
+STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, const char* info) {
+ // LuaAfbContextT *afbContext = (LuaAfbContextT *)lua_newuserdata(luaState, sizeof(LuaAfbContextT));
+ // luaL_setmetatable(luaState, CTX_TOKEN);
+ LuaAfbContextT *afbContext = (LuaAfbContextT *)calloc(1, sizeof(LuaAfbContextT));
+ lua_pushlightuserdata(luaState, afbContext);
+ if (!afbContext) {
+ AFB_ERROR ("LuaCtxPush fail to allocate user data context");
+ return NULL;
+ }
+ afbContext->ctxMagic=CTX_MAGIC;
+ afbContext->info=strdup(info);
+ afbContext->request= request;
+ return afbContext;
+}
+
+STATIC void LuaCtxFree (LuaAfbContextT *afbContext) {
+ free (afbContext->info);
+}
+
STATIC int LuaPushArgument (json_object *arg) {
switch (json_object_get_type(arg)) {
@@ -511,7 +513,7 @@ PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) {
// scan luascript search path once
static json_object *luaScriptPathJ =NULL;
- if (!luaScriptPathJ) luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , NULL, "lua");
+ if (!luaScriptPathJ) luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_FLAT, NULL, "lua");
err= wrap_json_unpack (queryJ, "{s:s, s?o s?o !}", "script", &script,"args", &args, "arg", &args);
if (err) {
@@ -537,10 +539,10 @@ PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) {
}
for (index=0; index < json_object_array_length(luaScriptPathJ); index++) {
- char *filename; char*dirpath;
+ char *filename; char*fullpath;
json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index);
- err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "dirpath", &dirpath,"filename", &filename);
+ err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename);
if (err) {
AFB_ERROR ("LUA-DOSCRIPT HOOPs invalid LUA script path = %s", json_object_get_string(entryJ));
goto OnErrorExit;
@@ -548,8 +550,8 @@ PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) {
if (strcmp(filename, script)) continue;
- char filepath[255];
- strncpy(filepath, dirpath, sizeof(filepath));
+ char filepath[CONTROL_MAXPATH_LEN];
+ strncpy(filepath, fullpath, sizeof(filepath));
strncat(filepath, "/", sizeof(filepath));
strncat(filepath, filename, sizeof(filepath));
err= luaL_loadfile(luaState, filepath);
@@ -616,7 +618,7 @@ PUBLIC int LuaLibInit () {
int err, index;
// search for default policy config file
- json_object *luaScriptPathJ = ScanForConfig(CONTROL_LUA_PATH , "onload", "lua");
+ json_object *luaScriptPathJ = ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_RECURSIVE, "onload", "lua");
// open a new LUA interpretor
luaState = luaL_newstate();
@@ -636,15 +638,15 @@ PUBLIC int LuaLibInit () {
for (index=0; index < json_object_array_length(luaScriptPathJ); index++) {
json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index);
- char *filename; char*dirpath;
- err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "dirpath", &dirpath,"filename", &filename);
+ char *filename; char*fullpath;
+ err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename);
if (err) {
AFB_ERROR ("LUA-INIT HOOPs invalid config file path = %s", json_object_get_string(entryJ));
goto OnErrorExit;
}
char filepath[CONTROL_MAXPATH_LEN];
- strncpy(filepath, dirpath, sizeof(filepath));
+ strncpy(filepath, fullpath, sizeof(filepath));
strncat(filepath, "/", sizeof(filepath));
strncat(filepath, filename, sizeof(filepath));
err= luaL_loadfile(luaState, filepath);