From 26b4b6597631e6701fc0b29d0909e39fead8cd7b Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Wed, 24 Nov 2010 19:21:43 +0000 Subject: [PATCH] fixed watch descriptor mixups --- lsyncd.lua | 63 +++++++++++++++++++++++++++------------------ tests/randrsync.lua | 6 +++-- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/lsyncd.lua b/lsyncd.lua index 473d415..3d6473c 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -1486,6 +1486,25 @@ local Inotifies = (function() -- sync is interested in. -- local syncRoots = {} + + ----- + -- Stops watching a directory + -- + -- @param path absolute path to unwatch + -- @param core if false not actually send the unwatch to the kernel + -- (used in moves which reuse the watch) + -- + local function removeWatch(path, core) + local wd = pathwds[path] + if not wd then + return + end + if core then + lsyncd.inotify.rmwatch(wd) + end + wdpaths[wd] = nil + pathwds[path] = nil + end ----- -- Adds watches for a directory (optionally) including all subdirectories. @@ -1501,18 +1520,25 @@ local Inotifies = (function() "Inotifies.addWatch(",path,", ",recurse,", ", raiseSync,", ",raiseTime,")") - local wd = pathwds[path] - if not wd then - -- lets the core registers watch with the kernel - local wd = lsyncd.inotify.addwatch(path); - if wd < 0 then - log("Error","Failure adding watch ",path," -> ignored ") - return - end - pathwds[path] = wd - wdpaths[wd] = path + -- lets the core registers watch with the kernel + local wd = lsyncd.inotify.addwatch(path); + if wd < 0 then + log("Error","Failure adding watch ",path," -> ignored ") + return end + do + -- If this wd is registered already the kernel + -- reused it for a new dir for a reason - old + -- dir is gone. + local op = wdpaths[wd] + if op and op ~= path then + pathwds[op] = nil + end + end + pathwds[path] = wd + wdpaths[wd] = path + -- registers and adds watches for all subdirectories -- and/or raises create events for all entries if not recurse and not raise then @@ -1543,19 +1569,6 @@ local Inotifies = (function() end end - ----- - -- Stops watching a directory - -- - local function removeWatch(path) - local wd = pathwds[path] - if not wd then - return - end - lsyncd.inotify.rmwatch(wd) - wdpaths[wd] = nil - pathwds[path] = nil - end - ----- -- adds a Sync to receive events -- @@ -1657,9 +1670,9 @@ local Inotifies = (function() if etyped == "Create" then addWatch(path, true, sync, time) elseif etyped == "Delete" then - removeWatch(path) + removeWatch(path, true) elseif etyped == "Move" then - removeWatch(path) + removeWatch(path, false) addWatch(path2, true, sync, time) end end diff --git a/tests/randrsync.lua b/tests/randrsync.lua index 4941d6c..f3caa2a 100755 --- a/tests/randrsync.lua +++ b/tests/randrsync.lua @@ -16,8 +16,10 @@ local trgdir = tdir.."trg/" posix.mkdir(srcdir) posix.mkdir(trgdir) -local pid = spawn("./lsyncd","-nodaemon","-rsync",srcdir,trgdir, - "-log","Inotify","-log","Exec") + +local logs = {} +--logs = {"-log", "Inotify", "-log", "Exec" } +local pid = spawn("./lsyncd","-nodaemon","-rsync",srcdir,trgdir, unpack(logs)) cwriteln("waiting for Lsyncd to startup") posix.sleep(1)