From 499365e2caf045053c363c84399303dd0ae975fd Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Tue, 9 Nov 2010 19:15:41 +0000 Subject: [PATCH] --- lsyncd-conf.lua | 1 + lsyncd.c | 20 ++++++++++-- lsyncd.lua | 87 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 83 insertions(+), 25 deletions(-) diff --git a/lsyncd-conf.lua b/lsyncd-conf.lua index fadb151..abea6b6 100644 --- a/lsyncd-conf.lua +++ b/lsyncd-conf.lua @@ -7,6 +7,7 @@ settings = { -- logfile = "/tmp/lsyncd", -- nodaemon = true, statusfile = "/tmp/lsyncd.stat", + statusintervall = 1, } ---- diff --git a/lsyncd.c b/lsyncd.c index 380cca3..5f1613c 100644 --- a/lsyncd.c +++ b/lsyncd.c @@ -410,7 +410,7 @@ static int l_stackdump(lua_State* L); * @return (Lua stack) numeric watch descriptor */ static int -l_add_watch(lua_State *L) +l_inotifyadd(lua_State *L) { const char *path = luaL_checkstring(L, 1); lua_Integer wd = inotify_add_watch(inotify_fd, path, standard_event_mask); @@ -418,6 +418,19 @@ l_add_watch(lua_State *L) return 1; } +/** + * Removes an inotify watch + * + * @param dir (Lua stack) numeric watch descriptor + * @return nil + */ +static int +l_inotifyrm(lua_State *L) +{ + lua_Integer wd = luaL_checkinteger(L, 1); + inotify_rm_watch(inotify_fd, wd); + return 0; +} /** * Logs a message. @@ -805,20 +818,21 @@ l_configure(lua_State *L) static const luaL_reg lsyncdlib[] = { - {"add_watch", l_add_watch }, {"addtoclock", l_addtoclock }, {"clockbefore", l_clockbefore }, {"clockbeforeq", l_clockbeforeq }, {"configure", l_configure }, {"earlier", l_earlier }, {"exec", l_exec }, + {"inotifyadd", l_inotifyadd }, + {"inotifyrm", l_inotifyrm }, {"log", l_log }, {"now", l_now }, - {"writefd", l_writefd }, {"realdir", l_realdir }, {"stackdump", l_stackdump }, {"subdirs", l_subdirs }, {"terminate", l_terminate }, + {"writefd", l_writefd }, {NULL, NULL} }; diff --git a/lsyncd.lua b/lsyncd.lua index c5e9a4e..648a423 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -114,27 +114,33 @@ local CountArray = (function() t[k_nt][k] = v end - -- TODO - local function iwalk(self) - return ipairs(self[k_nt]) + ----- + -- Walks through all entries in any order. + -- + local function walk(self) + return pairs(self[k_nt]) end ----- -- returns the count + -- local function size(self) return self._size end ----- -- creates a new count array + -- local function new() -- k_nt is native table, private for this object. - local o = {_size = 0, iwalk = iwalk, size = size, [k_nt] = {} } + local o = {_size = 0, walk = walk, size = size, [k_nt] = {} } setmetatable(o, mt) return o end - -- objects public interface + ----- + -- public interface + -- return {new = new} end)() @@ -527,34 +533,24 @@ local Sync = (function() local function invokeActions(self, now) log("Function", "invokeActions('",self.config.name,"',",now,")") if self.processes:size() >= self.config.maxProcesses then - log("Debug", "no new processes") -- no new processes return end for _, d in ipairs(self.delays) do - log("Debug", "iter") if d.alarm ~= true and lsyncd.clockbefore(now, d.alarm) then -- reached point in stack where delays are in future - log("Debug", "waits in future.") return end if d.status == "wait" then -- found a waiting delay - log("Debug", "invoke") InletControl.set(self) - log("Debug", "invoke2") self.config.action(Inlet) - log("Debug", "invoke3") if self.processes:size() >= self.config.maxProcesses then -- no further processes - log("Debug", "no further processes") return end - log("Debug", "finInvoke") end - log("Debug", "next") end - log("Debug", "finInvoke") end @@ -731,6 +727,11 @@ local Inotifies = (function() -- (directly or by recurse) local wdlist = CountArray.new() + ----- + -- A list indexed by sync's containing a list of all paths + -- watches by this sync pointing to the watch descriptor. + local syncpaths = {} + ----- -- Adds watches for a directory including all subdirectories. -- @@ -743,7 +744,7 @@ local Inotifies = (function() log("Function", "Inotifies.add(",root,", ",path,", ",recurse,", ",sync,")") -- registers watch - local wd = lsyncd.add_watch(root .. path); + local wd = lsyncd.inotifyadd(root .. path); if wd < 0 then log("Error","Failure adding watch ",dir," -> ignored ") return @@ -755,9 +756,16 @@ local Inotifies = (function() table.insert(wdlist[wd], { root = root, path = path, - recurse = recurse, + recurse = recurse, sync = sync }) + -- create an entry for receival of with sync/path keys + local sp = syncpaths[sync] + if not sp then + sp = {} + syncpaths[sync] = sp + end + sp[path] = wd -- registers and adds watches for all subdirectories if recurse then @@ -768,6 +776,41 @@ local Inotifies = (function() end end + ----- + -- Removes one event receiver from a directory. + -- + function removeSync(sync, path) + local sp = syncpaths[sync] + if not sp then + error("internal fail, removeSync, nonexisting syncpath-sync.") + end + local wd = sp[path] + if not wd then + error("internal fail, removeSync, nonexisting syncpath-wd.") + end + local ilist = wdlist[wd] + if not ilist then + error("internal fail, removeSync, nonexisting syncpath-ilist.") + end + -- TODO optimize for 1 entry only case + local i, found + for i, v in ipairs(ilist) do + if v.sync == sync then + found = true + break + end + end + if not found then + error("internal fail, removeSync, nonexisiting syncpath-i.") + end + table.remove(ilist, i) + if #ilist == 0 then + wdlist[wd] = nil + lsyncd.inotifyrm(wd) + end + sp[path] = nil + end + ----- -- Called when an event has occured. -- @@ -807,15 +850,15 @@ local Inotifies = (function() local path = inotify.path .. filename local path2 if filename2 then - path2 = inotify.path..filename2 + path2 = inotify.path .. filename2 end inotify.sync:delay(etype, time, path, path2) -- adds subdirs for new directories - if inotify.recurse and isdir then - if etype == "Create" then + if isdir then + if inotify.recurse and etype == "Create" then add(inotify.root, path, true, inotify.sync) elseif etype == "Delete" then - -- TODO + removeSync(inotify.sync, path) end end end @@ -826,7 +869,7 @@ local Inotifies = (function() -- local function status_report(f) f:write("Watching ",wdlist:size()," directories\n") - for wd, v in wdlist:iwalk() do + for wd, v in wdlist:walk() do f:write(" ",wd,": ") local sep = "" for _, v in ipairs(v) do