mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-06-02 00:10:49 +00:00
This commit is contained in:
parent
35cabeae92
commit
43be580976
155
lsyncd.lua
155
lsyncd.lua
|
@ -142,7 +142,7 @@ end)()
|
||||||
-- Locks globals,
|
-- Locks globals,
|
||||||
-- no more globals can be created
|
-- no more globals can be created
|
||||||
--
|
--
|
||||||
local function globalsLock()
|
local function lockGlobals()
|
||||||
local t = _G
|
local t = _G
|
||||||
local mt = getmetatable(t) or {}
|
local mt = getmetatable(t) or {}
|
||||||
mt.__index = function(t, k)
|
mt.__index = function(t, k)
|
||||||
|
@ -193,86 +193,100 @@ end)()
|
||||||
-- Holds information about one observed directory inclusively subdirs.
|
-- Holds information about one observed directory inclusively subdirs.
|
||||||
--
|
--
|
||||||
local Sync = (function()
|
local Sync = (function()
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- Syncs that have no name specified get an incremental default name
|
||||||
|
--
|
||||||
|
local nextDefaultName = 1
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- Puts an action on the delay stack.
|
||||||
|
--
|
||||||
|
local function delay(self, ename, time, path, path2)
|
||||||
|
log("Function", "delay(", self, ", ", ename, ", ", path, ")")
|
||||||
|
local delays = self.delays
|
||||||
|
local delayname = self.delayname
|
||||||
|
|
||||||
|
if ename == "Move" and not self.config.move then
|
||||||
|
-- if there is no move action defined, split a move as delete/create
|
||||||
|
log("Debug", "splitting Move into Delete & Create")
|
||||||
|
delay(self, "Delete", time, path, nil)
|
||||||
|
delay(self, "Create", time, path2, nil)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- creates the new action
|
||||||
|
local alarm
|
||||||
|
if time and self.config.delay then
|
||||||
|
alarm = lsyncd.addtoclock(time, self.config.delay)
|
||||||
|
else
|
||||||
|
alarm = lsyncd.now()
|
||||||
|
end
|
||||||
|
local newd = Delay.new(ename, path, alarm)
|
||||||
|
|
||||||
|
local oldd = delayname[path]
|
||||||
|
if oldd then
|
||||||
|
-- if there is already a delay on this path.
|
||||||
|
-- decide what should happen with multiple delays.
|
||||||
|
if newd.ename == "MoveFrom" or newd.ename == "MoveTo" or
|
||||||
|
oldd.ename == "MoveFrom" or oldd.ename == "MoveTo" then
|
||||||
|
-- do not collapse moves
|
||||||
|
log("Normal", "Not collapsing events with moves on ", path)
|
||||||
|
-- TODO stackinfo
|
||||||
|
return
|
||||||
|
else
|
||||||
|
local col = self.config.collapseTable[oldd.ename][newd.ename]
|
||||||
|
if col == -1 then
|
||||||
|
-- events cancel each other
|
||||||
|
log("Normal", "Nullfication: ", newd.ename, " after ",
|
||||||
|
oldd.ename, " on ", path)
|
||||||
|
oldd.ename = "None"
|
||||||
|
return
|
||||||
|
elseif col == 0 then
|
||||||
|
-- events tack
|
||||||
|
log("Normal", "Stacking ", newd.ename, " after ",
|
||||||
|
oldd.ename, " on ", path)
|
||||||
|
-- TODO Stack pointer
|
||||||
|
else
|
||||||
|
log("Normal", "Collapsing ", newd.ename, " upon ",
|
||||||
|
oldd.ename, " to ", col, " on ", path)
|
||||||
|
oldd.ename = col
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(delays, newd)
|
||||||
|
else
|
||||||
|
delayname[path] = newd
|
||||||
|
table.insert(delays, newd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Creates a new Sync
|
-- Creates a new Sync
|
||||||
--
|
--
|
||||||
local function new(config)
|
local function new(config)
|
||||||
local o = {
|
local s = {
|
||||||
config = config,
|
config = config,
|
||||||
delays = CountArray.new(),
|
delays = CountArray.new(),
|
||||||
delayname = {},
|
delayname = {},
|
||||||
source = config.source,
|
source = config.source,
|
||||||
processes = CountArray.new(),
|
processes = CountArray.new(),
|
||||||
|
delay = delay,
|
||||||
}
|
}
|
||||||
return o
|
-- provides a default name if needed
|
||||||
|
if not config.name then
|
||||||
|
config.name = "Sync" .. nextDefaultName
|
||||||
|
end
|
||||||
|
-- increment default nevertheless to cause less confusion
|
||||||
|
nextDefaultName = nextDefaultName + 1
|
||||||
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----
|
||||||
-- public interface
|
-- public interface
|
||||||
return {new = new}
|
return {new = new}
|
||||||
end)()
|
end)()
|
||||||
|
|
||||||
-----
|
|
||||||
-- Puts an action on the delay stack.
|
|
||||||
--
|
|
||||||
function Sync.delay(self, ename, time, path, path2)
|
|
||||||
log("Function", "delay(", self, ", ", ename, ", ", path, ")")
|
|
||||||
local delays = self.delays
|
|
||||||
local delayname = self.delayname
|
|
||||||
|
|
||||||
if ename == "Move" and not self.config.move then
|
|
||||||
-- if there is no move action defined, split a move as delete/create
|
|
||||||
log("Debug", "splitting Move into Delete & Create")
|
|
||||||
delay(self, "Delete", time, path, nil)
|
|
||||||
delay(self, "Create", time, path2, nil)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- creates the new action
|
|
||||||
local alarm
|
|
||||||
-- TODO scope
|
|
||||||
if time and self.config.delay then
|
|
||||||
alarm = lsyncd.addtoclock(time, self.config.delay)
|
|
||||||
else
|
|
||||||
alarm = lsyncd.now()
|
|
||||||
end
|
|
||||||
local newd = Delay.new(ename, path, alarm)
|
|
||||||
|
|
||||||
local oldd = delayname[path]
|
|
||||||
if oldd then
|
|
||||||
-- if there is already a delay on this path.
|
|
||||||
-- decide what should happen with multiple delays.
|
|
||||||
if newd.ename == "MoveFrom" or newd.ename == "MoveTo" or
|
|
||||||
oldd.ename == "MoveFrom" or oldd.ename == "MoveTo" then
|
|
||||||
-- do not collapse moves
|
|
||||||
log("Normal", "Not collapsing events with moves on ", path)
|
|
||||||
-- TODO stackinfo
|
|
||||||
return
|
|
||||||
else
|
|
||||||
local col = self.config.collapseTable[oldd.ename][newd.ename]
|
|
||||||
if col == -1 then
|
|
||||||
-- events cancel each other
|
|
||||||
log("Normal", "Nullfication: ", newd.ename, " after ",
|
|
||||||
oldd.ename, " on ", path)
|
|
||||||
oldd.ename = "None"
|
|
||||||
return
|
|
||||||
elseif col == 0 then
|
|
||||||
-- events tack
|
|
||||||
log("Normal", "Stacking ", newd.ename, " after ",
|
|
||||||
oldd.ename, " on ", path)
|
|
||||||
-- TODO Stack pointer
|
|
||||||
else
|
|
||||||
log("Normal", "Collapsing ", newd.ename, " upon ",
|
|
||||||
oldd.ename, " to ", col, " on ", path)
|
|
||||||
oldd.ename = col
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
table.insert(delays, newd)
|
|
||||||
else
|
|
||||||
delayname[path] = newd
|
|
||||||
table.insert(delays, newd)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Syncs - a singleton
|
-- Syncs - a singleton
|
||||||
|
@ -280,7 +294,9 @@ end
|
||||||
-- It maintains all configured directories to be synced.
|
-- It maintains all configured directories to be synced.
|
||||||
--
|
--
|
||||||
local Syncs = (function()
|
local Syncs = (function()
|
||||||
|
-----
|
||||||
-- the list of all syncs
|
-- the list of all syncs
|
||||||
|
--
|
||||||
local list = Array.new()
|
local list = Array.new()
|
||||||
|
|
||||||
-----
|
-----
|
||||||
|
@ -313,6 +329,7 @@ local Syncs = (function()
|
||||||
-----
|
-----
|
||||||
-- Creates a new config table and inherit all keys/values
|
-- Creates a new config table and inherit all keys/values
|
||||||
-- from integer keyed tables
|
-- from integer keyed tables
|
||||||
|
--
|
||||||
local uconfig = config
|
local uconfig = config
|
||||||
config = {}
|
config = {}
|
||||||
inherit(config, uconfig)
|
inherit(config, uconfig)
|
||||||
|
@ -363,12 +380,16 @@ local Syncs = (function()
|
||||||
table.insert(list, s)
|
table.insert(list, s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----
|
||||||
-- allows to walk through all syncs
|
-- allows to walk through all syncs
|
||||||
|
--
|
||||||
local function iwalk()
|
local function iwalk()
|
||||||
return ipairs(list)
|
return ipairs(list)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----
|
||||||
-- returns the number of syncs
|
-- returns the number of syncs
|
||||||
|
--
|
||||||
local size = function()
|
local size = function()
|
||||||
return #list
|
return #list
|
||||||
end
|
end
|
||||||
|
@ -470,7 +491,7 @@ local Inotifies = (function()
|
||||||
if filename2 then
|
if filename2 then
|
||||||
path2 = inotify.path..filename2
|
path2 = inotify.path..filename2
|
||||||
end
|
end
|
||||||
Sync.delay(inotify.sync, ename, time, path, path2)
|
inotify.sync:delay(ename, time, path, path2)
|
||||||
-- adds subdirs for new directories
|
-- adds subdirs for new directories
|
||||||
if inotify.recurse and isdir then
|
if inotify.recurse and isdir then
|
||||||
if ename == "Create" then
|
if ename == "Create" then
|
||||||
|
@ -926,7 +947,7 @@ function lsyncd_initialize()
|
||||||
settings = settings or {}
|
settings = settings or {}
|
||||||
|
|
||||||
-- From this point on, no globals may be created anymore
|
-- From this point on, no globals may be created anymore
|
||||||
globalsLock()
|
lockGlobals()
|
||||||
|
|
||||||
-- TODO
|
-- TODO
|
||||||
if settings.statusIntervall == nil then
|
if settings.statusIntervall == nil then
|
||||||
|
|
Loading…
Reference in New Issue
Block a user