From 83d0f90c650585776d9da8d546cd3b466c278b28 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 11 Nov 2010 19:52:20 +0000 Subject: [PATCH] --- lsyncd.c | 43 +++++++++++++++++++++++++++++++------------ lsyncd.lua | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/lsyncd.c b/lsyncd.c index 22b9d24..878640b 100644 --- a/lsyncd.c +++ b/lsyncd.c @@ -546,9 +546,10 @@ l_now(lua_State *L) /** * Returns (on Lua stack) the addition of a clock timer by seconds. * - * @param1 the clock timer - * @param2 seconds to change clock. - * TODO + * @param1 (Lua stack) the clock timer + * @param2 (Lua stack) seconds to change clock. + * + * @return (Lua stack) clock timer + seconds. */ static int l_addtoclock(lua_State *L) @@ -565,6 +566,7 @@ l_addtoclock(lua_State *L) * * @param (Lua stack) Path to binary to call * @params (Lua stack) list of string as arguments + * * @return (Lua stack) the pid on success, 0 on failure. */ static int @@ -893,9 +895,10 @@ bool move_event = false; * Handles an inotify event. */ static void -handle_event(lua_State *L, struct inotify_event *event) { - /* TODO */ - int event_type = NONE; +handle_event(lua_State *L, + struct inotify_event *event) +{ + int event_type; /* used to execute two events in case of unmatched MOVE_FROM buffer */ struct inotify_event *after_buf = NULL; @@ -1159,10 +1162,27 @@ main(int argc, char *argv[]) /* load Lua */ L = lua_open(); + luaL_openlibs(L); + { + /* checks the lua version */ + const char *version; + int major, minor; + lua_getglobal(L, "_VERSION"); + version = luaL_checkstring(L, -1); + if (sscanf(version, "Lua %d.%d", &major, &minor) != 2) { + fprintf(stderr, "cannot parse lua library version!\n"); + return -1; // ERRNO + } + if ((major < 5) || (major == 5 && minor < 1)) { + fprintf(stderr, "lua library is too old. Need 5.1 at least"); + return -1; // ERRNO + } + lua_pop(L, 1); + } { + /* prepares logging early */ int i = 1; - /* Prepares logging early */ add_logcat("Normal", LOG_NOTICE); add_logcat("Error", LOG_ERR); while (i < argc) { @@ -1180,8 +1200,7 @@ main(int argc, char *argv[]) } } - /* TODO check lua version */ - luaL_openlibs(L); + /* registers lsycnd core */ luaL_register(L, "lsyncd", lsyncdlib); lua_setglobal(L, "lysncd"); @@ -1278,7 +1297,7 @@ main(int argc, char *argv[]) } { - /* checks version match between runner/core */ + /* asserts version match between runner and core */ const char *lversion; lua_getglobal(L, "lsyncd_version"); lversion = luaL_checkstring(L, -1); @@ -1308,7 +1327,7 @@ main(int argc, char *argv[]) } { - /* start the option parser in lua script */ + /* starts the option parser in lua script */ int idx = 1; const char *s; /* creates a table with all remaining argv option arguments */ @@ -1326,7 +1345,7 @@ main(int argc, char *argv[]) if (s) { lsyncd_config_file = s_strdup(s); } - lua_pop(L, 2); // TODO + lua_pop(L, 2); } if (lsyncd_config_file) { diff --git a/lsyncd.lua b/lsyncd.lua index aed9119..88625d8 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -176,7 +176,8 @@ local Delay = (function() ----- -- Creates a new delay. -- - -- @param TODO + -- @params see below + -- local function new(etype, alarm, path, path2) local o = { ----- @@ -445,7 +446,7 @@ local Inlet, InletControl = (function() -- local function cancelEvent(event) local delay = event[delayKey] - if delay.status ~= "waiting" then + if delay.status ~= "wait" then log("Error", "Ignored try to cancel a non-waiting event of type ", event.etype) return @@ -655,9 +656,15 @@ local Sync = (function() return nil end - if self.delays[1] then - return self.delays[1].alarm + -- finds the nearest delay waiting to be spawned + for _, d in ipairs(self.delays) do + if d.status == "wait" then + return d.alarm + end end + + -- nothing to spawn. + return nil end ----- @@ -1119,10 +1126,13 @@ end)() --============================================================================ ----- --- true after runner.initalized() --- TODO change to string X2 +-- Current status of lsyncd. -- -local running = false +-- "init" ... on (re)init +-- "run" ... normal operation +-- "fade" ... waits for remaining processes +-- +local lsyncdStatus = "init" ---- -- the cores interface to the runner @@ -1284,7 +1294,7 @@ function runner.initialize() end -- from now on use logging as configured instead of stdout/err. - running = true; + lsyncdStatus = "run"; lsyncd.configure("running"); -- runs through the syncs table filled by user calling directory() @@ -1372,8 +1382,8 @@ end -- Main utility to create new observations. -- function sync(opts) - if running then - error("Cannot add new syncs while running!") + if lsyncdStatus ~= "init" then + error("Sync can only be created on initialization.", 2) end Syncs.add(opts) end @@ -1456,9 +1466,9 @@ local defaultRsync = { -- TODO make readonly -- default = { + ----- - -- Default action - -- TODO desc + -- Default action calls user scripts on**** functions. -- action = function(inlet) -- in case of moves getEvent returns the origin and dest of the move @@ -1471,7 +1481,10 @@ default = { end, ----- - -- Called if to see if two events can be collapsed + -- Called to see if two events can be collapsed. + -- + -- Default function uses the collapseTable. + -- -- @param event1 first event -- @param event2 second event -- @return -1 ... no interconnection @@ -1541,7 +1554,7 @@ default = { if type(config.onStartup) == "function" then local event = inlet.createBlanketEvent() local startup = config.onStartup(event) - if event.status == "waiting" then + if event.status == "wait" then -- user script did not spawn anything -- thus the blanket event is deleted again. inlet.cancelEvent(event)