mirror of https://github.com/octoleo/lsyncd.git
This commit is contained in:
parent
65a1f9efb8
commit
fd718d1d49
2
lsyncd.c
2
lsyncd.c
|
@ -878,7 +878,7 @@ masterloop(lua_State *L)
|
||||||
if (have_alarm && time_before(alarm_time, now)) {
|
if (have_alarm && time_before(alarm_time, now)) {
|
||||||
/* there is a delay that wants to be handled already
|
/* there is a delay that wants to be handled already
|
||||||
* thus do not read from inotify_fd and jump directly to its handling */
|
* thus do not read from inotify_fd and jump directly to its handling */
|
||||||
logstring(DEBUG, "immediately handling delayed entries.");
|
logstring(DEBUG, "immediately handling delays.");
|
||||||
do_read = 0;
|
do_read = 0;
|
||||||
} else {
|
} else {
|
||||||
/* use select() to determine what happens next
|
/* use select() to determine what happens next
|
||||||
|
|
358
lsyncd.lua
358
lsyncd.lua
|
@ -1,4 +1,4 @@
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
-- lsyncd.lua Live (Mirror) Syncing Demon
|
-- lsyncd.lua Live (Mirror) Syncing Demon
|
||||||
--
|
--
|
||||||
-- License: GPLv2 (see COPYING) or any later version
|
-- License: GPLv2 (see COPYING) or any later version
|
||||||
|
@ -8,7 +8,7 @@
|
||||||
-- This is the "runner" part of lsyncd. It containts all its high-level logic.
|
-- This is the "runner" part of lsyncd. It containts all its high-level logic.
|
||||||
-- It works closely together with the lsyncd core in lsyncd.c. This means it
|
-- It works closely together with the lsyncd core in lsyncd.c. This means it
|
||||||
-- cannot be runned directly from the standard lua interpreter.
|
-- cannot be runned directly from the standard lua interpreter.
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
|
|
||||||
----
|
----
|
||||||
-- A security measurement.
|
-- A security measurement.
|
||||||
|
@ -27,134 +27,122 @@ lsyncd_version = "2.0b1"
|
||||||
log = lsyncd.log
|
log = lsyncd.log
|
||||||
exec = lsyncd.exec
|
exec = lsyncd.exec
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
-- Lsyncd globals
|
-- Coding checks, ensure termination on some easy to do coding errors.
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
|
|
||||||
----
|
-----
|
||||||
-- TODO
|
-- Metatable to limit keys to numerics.
|
||||||
--
|
--
|
||||||
local function set_valid_keys(t, numeric, keylist)
|
local meta_check_array = {
|
||||||
local mt = getmetatable(t) or {}
|
__index = function(t, k)
|
||||||
mt.keylist = keylist
|
if type(k) ~= "number" then
|
||||||
for k, v in pairs(t) do
|
error("This table is an array and must have numeric keys", 2)
|
||||||
if not keylist[k] then
|
|
||||||
error("There is a non-valid key '"..k.."' in this table")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
mt.__index = function(t, k)
|
|
||||||
if type(k) == "number" then
|
|
||||||
if not numeric then
|
|
||||||
error("This table does not have numerics", 2)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if not keylist[k] then
|
|
||||||
error("Key '"..k.."' not valid for this table", 2)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return rawget(t, k)
|
return rawget(t, k)
|
||||||
end
|
end,
|
||||||
|
__newindex = function(t, k, v)
|
||||||
mt.__newindex = function(t, k, v)
|
if type(k) ~= "number" then
|
||||||
if type(k) == "number" then
|
error("This table is an array and must have numeric keys", 2)
|
||||||
if not numeric then
|
|
||||||
error("This table does not have numerics", 2)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if not keylist[k] then
|
|
||||||
error("Key '"..k.."' not valid for this table", 2)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
rawset(t, k, v)
|
rawset(t, k, v)
|
||||||
end
|
end
|
||||||
setmetatable(t, mt)
|
}
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- Metatable to limit keys to numerics and count the number of entries.
|
||||||
|
-- Lua's # operator does not work on tables which key values are not
|
||||||
|
-- strictly linear.
|
||||||
|
--
|
||||||
|
local meta_check_count_array = {
|
||||||
|
__index = function(t, k)
|
||||||
|
if k == size then
|
||||||
|
return rawget(t, "size")
|
||||||
|
end
|
||||||
|
if type(k) ~= "number" then
|
||||||
|
error("This table is an array and must have numeric keys", 2)
|
||||||
|
end
|
||||||
|
return rawget(t, "_t")[k]
|
||||||
|
end,
|
||||||
|
|
||||||
|
__newindex = function(t, k, v)
|
||||||
|
if type(k) ~= "number" then
|
||||||
|
error("This table is an array and must have numeric keys", 2)
|
||||||
|
end
|
||||||
|
local _t = rawget(t, "_t")
|
||||||
|
local vb = _t[k]
|
||||||
|
if v and not vb then
|
||||||
|
rawset(t, "size", rawget(t, "size") + 1)
|
||||||
|
elseif not v and vb then
|
||||||
|
rawset(t, "size", rawget(t, "size") - 1)
|
||||||
|
end
|
||||||
|
_t[k] = v
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- Metatable to limit keys to those only presented in their prototype
|
||||||
|
--
|
||||||
|
local meta_check_prototype = {
|
||||||
|
__index = function(t, k)
|
||||||
|
if not t.prototype[k] then
|
||||||
|
error("This table does not have key '"..k.."' in its prototype.", 2)
|
||||||
|
end
|
||||||
|
return rawget(t, k)
|
||||||
|
end,
|
||||||
|
__newindex = function(t, k, v)
|
||||||
|
if not t.prototype[k] then
|
||||||
|
error("This table does not have key '"..k.."' in its prototype.", 2)
|
||||||
|
end
|
||||||
|
rawset(t, k, v)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- Limits the keys of table to numbers.
|
||||||
|
--
|
||||||
|
local function set_array(t)
|
||||||
|
for k, _ in pairs(t) do
|
||||||
|
if type(k) ~= "number" then
|
||||||
|
error("This table cannot be set as array, it has non-numberic key '"..k.."'", 2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
setmetatable(t, meta_check_array)
|
||||||
end
|
end
|
||||||
|
|
||||||
----
|
-----
|
||||||
-- Table of all root directories to sync,
|
-- Creates a table with keys limited to numbers.
|
||||||
-- filled during initialization.
|
|
||||||
--
|
--
|
||||||
-- [#] {
|
local function new_array()
|
||||||
-- actions = actions,
|
local t = {}
|
||||||
-- source = source_dir,
|
setmetatable(t, meta_ckeck_array)
|
||||||
-- targetident = the identifier of target (like string "host:dir")
|
return t
|
||||||
-- for lsyncd this passed competly opaquely to the
|
end
|
||||||
-- action handlers
|
|
||||||
--
|
|
||||||
-- .processes = [pid] .. a sublist of processes[] for this target
|
|
||||||
-- .delays = [#) { .. the delays stack
|
|
||||||
-- .atype .. enum, kind of action
|
|
||||||
-- .alarm .. when it should fire
|
|
||||||
-- .wd .. watch descriptor id this origins from TODO needed?
|
|
||||||
-- .sync .. link to sync that raised this delay.
|
|
||||||
-- .filename .. filename or nil (=dir itself)
|
|
||||||
-- (.movepeer) .. for MOVEFROM/MOVETO link to other delay
|
|
||||||
-- }
|
|
||||||
-- }
|
|
||||||
--
|
|
||||||
local origins = {}
|
|
||||||
set_valid_keys(origins, true, {})
|
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- all watches
|
-- Creates a table with keys limited to numbers and
|
||||||
|
-- which counts the number of entries
|
||||||
--
|
--
|
||||||
-- structure:
|
local function new_count_array()
|
||||||
-- [wd] = {
|
local t = { size = 0, _t = {} }
|
||||||
-- .wd .. the watch descriptor (TODO needed?)
|
setmetatable(t, meta_check_count_array)
|
||||||
-- .syncs = [#] { list of stuff to sync to
|
return t
|
||||||
-- .origin .. link to origin
|
end
|
||||||
-- .path .. relative path of dir
|
|
||||||
-- .parent .. link to parent directory in watches
|
|
||||||
-- or nil for origin
|
|
||||||
-- }
|
|
||||||
-- }
|
|
||||||
--
|
|
||||||
local watches = {}
|
|
||||||
set_valid_keys(watches, true, {})
|
|
||||||
|
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- a dictionary of all processes lsyncd spawned.
|
-- Sets the prototype of a table limiting its keys to a defined list.
|
||||||
--
|
--
|
||||||
-- structure
|
local function set_prototype(t, prototype)
|
||||||
-- [pid] = {
|
t.prototype = prototype
|
||||||
-- target ..
|
for k, _ in pairs(t) do
|
||||||
-- atype ..
|
if not t.prototype[k] and k ~= "prototype" then
|
||||||
-- wd ..
|
error("Cannot set prototype of table, conflicting key: '"..k.."'.", 2)
|
||||||
-- sync ..
|
end
|
||||||
-- filename ..
|
end
|
||||||
--
|
setmetatable(t, proto_check_table)
|
||||||
local processes = {}
|
end
|
||||||
|
|
||||||
|
|
||||||
------
|
|
||||||
-- TODO
|
|
||||||
--
|
|
||||||
local collapse_table = {
|
|
||||||
[ATTRIB] = { [ATTRIB] = ATTRIB, [MODIFY] = MODIFY, [CREATE] = CREATE, [DELETE] = DELETE },
|
|
||||||
[MODIFY] = { [ATTRIB] = MODIFY, [MODIFY] = MODIFY, [CREATE] = CREATE, [DELETE] = DELETE },
|
|
||||||
[CREATE] = { [ATTRIB] = CREATE, [MODIFY] = CREATE, [CREATE] = CREATE, [DELETE] = DELETE },
|
|
||||||
[DELETE] = { [ATTRIB] = DELETE, [MODIFY] = DELETE, [CREATE] = MODIFY, [DELETE] = DELETE },
|
|
||||||
}
|
|
||||||
|
|
||||||
-----
|
|
||||||
-- A list of names of the event types the core sends.
|
|
||||||
--
|
|
||||||
local event_names = {
|
|
||||||
[ATTRIB ] = "Attrib",
|
|
||||||
[MODIFY ] = "Modify",
|
|
||||||
[CREATE ] = "Create",
|
|
||||||
[DELETE ] = "Delete",
|
|
||||||
[MOVE ] = "Move",
|
|
||||||
[MOVEFROM ] = "MoveFrom",
|
|
||||||
[MOVETO ] = "MoveTo",
|
|
||||||
}
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
-- Coding checks. Hinders creation of global variables after init.
|
|
||||||
-- (inspired by /Niklas Frykholm)
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
----
|
----
|
||||||
-- Locks a table
|
-- Locks a table
|
||||||
|
@ -191,24 +179,98 @@ local function unlock_new_index(t, k, v)
|
||||||
rawset(t, k, v)
|
rawset(t, k, v)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
|
||||||
-- Tests if a value is nil,
|
--============================================================================
|
||||||
function is_nil(t, k)
|
-- Lsyncd globals
|
||||||
return rawget(t, k)
|
--============================================================================
|
||||||
end
|
|
||||||
|
|
||||||
|
----
|
||||||
|
-- Table of all root directories to sync,
|
||||||
|
-- filled during initialization.
|
||||||
|
--
|
||||||
|
-- [#] {
|
||||||
|
-- actions = actions,
|
||||||
|
-- source = source_dir,
|
||||||
|
-- targetident = the identifier of target (like string "host:dir")
|
||||||
|
-- for lsyncd this passed competly opaquely to the
|
||||||
|
-- action handlers
|
||||||
|
--
|
||||||
|
-- .processes = [pid] .. a sublist of processes[] for this target
|
||||||
|
-- .delays = [#) { .. the delays stack
|
||||||
|
-- .atype .. enum, kind of action
|
||||||
|
-- .alarm .. when it should fire
|
||||||
|
-- .wd .. watch descriptor id this origins from TODO needed?
|
||||||
|
-- .sync .. link to sync that raised this delay.
|
||||||
|
-- .filename .. filename or nil (=dir itself)
|
||||||
|
-- (.movepeer) .. for MOVEFROM/MOVETO link to other delay
|
||||||
|
-- }
|
||||||
|
-- }
|
||||||
|
--
|
||||||
|
local origins = new_array()
|
||||||
|
local proto_delay = {atype=true, alarm=true, wd=true, sync=true, filename=true, movepeer=true}
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Tests if a value is nil,
|
-- all watches
|
||||||
function set_new(t, k, v)
|
--
|
||||||
return rawset(t, k, v)
|
-- structure:
|
||||||
end
|
-- [wd] = {
|
||||||
|
-- .wd .. the watch descriptor (TODO needed?)
|
||||||
|
-- .syncs = [#] { list of stuff to sync to
|
||||||
|
-- .origin .. link to origin
|
||||||
|
-- .path .. relative path of dir
|
||||||
|
-- .parent .. link to parent directory in watches
|
||||||
|
-- or nil for origin
|
||||||
|
-- }
|
||||||
|
-- }
|
||||||
|
--
|
||||||
|
local watches = new_count_array()
|
||||||
|
local proto_watch = {wd=true, syncs=true}
|
||||||
|
local proto_sync = {origin=true, path=true, parent=true}
|
||||||
|
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- a dictionary of all processes lsyncd spawned.
|
||||||
|
--
|
||||||
|
-- structure
|
||||||
|
-- [pid] = {
|
||||||
|
-- target ..
|
||||||
|
-- atype ..
|
||||||
|
-- wd ..
|
||||||
|
-- sync ..
|
||||||
|
-- filename ..
|
||||||
|
--
|
||||||
|
local processes = new_count_array()
|
||||||
|
local proto_process = {pid=true, atype=true, wd=true, sync=true, filename=true}
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------
|
||||||
|
-- TODO
|
||||||
|
--
|
||||||
|
local collapse_table = {
|
||||||
|
[ATTRIB] = { [ATTRIB] = ATTRIB, [MODIFY] = MODIFY, [CREATE] = CREATE, [DELETE] = DELETE },
|
||||||
|
[MODIFY] = { [ATTRIB] = MODIFY, [MODIFY] = MODIFY, [CREATE] = CREATE, [DELETE] = DELETE },
|
||||||
|
[CREATE] = { [ATTRIB] = CREATE, [MODIFY] = CREATE, [CREATE] = CREATE, [DELETE] = DELETE },
|
||||||
|
[DELETE] = { [ATTRIB] = DELETE, [MODIFY] = DELETE, [CREATE] = MODIFY, [DELETE] = DELETE },
|
||||||
|
}
|
||||||
|
set_array(collapse_table)
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- A list of names of the event types the core sends.
|
||||||
|
--
|
||||||
|
local event_names = {
|
||||||
|
[ATTRIB ] = "Attrib",
|
||||||
|
[MODIFY ] = "Modify",
|
||||||
|
[CREATE ] = "Create",
|
||||||
|
[DELETE ] = "Delete",
|
||||||
|
[MOVE ] = "Move",
|
||||||
|
[MOVEFROM ] = "MoveFrom",
|
||||||
|
[MOVETO ] = "MoveTo",
|
||||||
|
}
|
||||||
|
set_array(event_names)
|
||||||
|
|
||||||
|
--============================================================================
|
||||||
-- The lsyncd runner
|
-- The lsyncd runner
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
|
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Puts an action on the delay stack.
|
-- Puts an action on the delay stack.
|
||||||
|
@ -219,8 +281,9 @@ local function delay_action(atype, wd, sync, filename, time)
|
||||||
local delays = origin.delays
|
local delays = origin.delays
|
||||||
local nd = {atype = atype,
|
local nd = {atype = atype,
|
||||||
wd = wd,
|
wd = wd,
|
||||||
sync = sync,
|
sync = sync,
|
||||||
filename = filename }
|
filename = filename }
|
||||||
|
set_prototype(nd, proto_delay)
|
||||||
if time ~= nil and origin.actions.delay ~= nil then
|
if time ~= nil and origin.actions.delay ~= nil then
|
||||||
nd.alarm = lsyncd.addto_clock(time, origin.actions.delay)
|
nd.alarm = lsyncd.addto_clock(time, origin.actions.delay)
|
||||||
else
|
else
|
||||||
|
@ -250,9 +313,11 @@ local function attend_dir(origin, path, parent)
|
||||||
if thiswatch == nil then
|
if thiswatch == nil then
|
||||||
-- new watch
|
-- new watch
|
||||||
thiswatch = {wd = wd, syncs = {} }
|
thiswatch = {wd = wd, syncs = {} }
|
||||||
|
set_prototype(thiswatch, proto_watch)
|
||||||
watches[wd] = thiswatch
|
watches[wd] = thiswatch
|
||||||
end
|
end
|
||||||
local sync = { origin = origin, path = path, parent = parent }
|
local sync = { origin = origin, path = path, parent = parent }
|
||||||
|
set_prototype(sync, proto_sync)
|
||||||
table.insert(thiswatch.syncs, sync)
|
table.insert(thiswatch.syncs, sync)
|
||||||
|
|
||||||
-- warmstart?
|
-- warmstart?
|
||||||
|
@ -278,10 +343,10 @@ function lsyncd_collect_process(pid, exitcode)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local sync = process.sync
|
local sync = process.sync
|
||||||
local origin = sync.origin
|
local o = sync.origin
|
||||||
print("collected ", pid, ": ", event_names[process.atype], origin.source, "/", sync.path , process.filename, " = ", exitcode)
|
print("collected ", pid, ": ", event_names[process.atype], o.source, "/", sync.path , process.filename, " = ", exitcode)
|
||||||
processes[pid] = nil
|
processes[pid] = nil
|
||||||
origin.processes[pid] = nil
|
o.processes[pid] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
|
@ -325,11 +390,12 @@ local function invoke_action(delay)
|
||||||
local pid = func(origin.source, sync.path, delay.filename, origin.targetident)
|
local pid = func(origin.source, sync.path, delay.filename, origin.targetident)
|
||||||
if pid ~= nil and pid > 0 then
|
if pid ~= nil and pid > 0 then
|
||||||
local process = {pid = pid,
|
local process = {pid = pid,
|
||||||
atype = delay.atype,
|
atype = delay.atype,
|
||||||
wd = delay.wd,
|
wd = delay.wd,
|
||||||
sync = delay.sync,
|
sync = delay.sync,
|
||||||
filename = delay.filename
|
filename = delay.filename
|
||||||
}
|
}
|
||||||
|
set_prototype(process, proto_process)
|
||||||
processes[pid] = process
|
processes[pid] = process
|
||||||
origin.processes[pid] = process
|
origin.processes[pid] = process
|
||||||
end
|
end
|
||||||
|
@ -347,7 +413,7 @@ function lsyncd_alarm(now)
|
||||||
-- goes through all targets and spawns more actions
|
-- goes through all targets and spawns more actions
|
||||||
-- if possible
|
-- if possible
|
||||||
for _, o in ipairs(origins) do
|
for _, o in ipairs(origins) do
|
||||||
if #o.processes < o.actions.max_processes then
|
if o.processes.size < o.actions.max_processes then
|
||||||
local delays = o.delays
|
local delays = o.delays
|
||||||
if delays[1] ~= nil and lsyncd.before_eq(delays[1].alarm, now) then
|
if delays[1] ~= nil and lsyncd.before_eq(delays[1].alarm, now) then
|
||||||
invoke_action(o.delays[1])
|
invoke_action(o.delays[1])
|
||||||
|
@ -382,8 +448,9 @@ function lsyncd_initialize()
|
||||||
lsyncd.terminate(-1) -- ERRNO
|
lsyncd.terminate(-1) -- ERRNO
|
||||||
end
|
end
|
||||||
origin.source = asrc
|
origin.source = asrc
|
||||||
origin.delays = {}
|
origin.delays = new_count_array()
|
||||||
origin.processes = {}
|
origin.processes = new_count_array()
|
||||||
|
|
||||||
if actions.max_processes == nil then
|
if actions.max_processes == nil then
|
||||||
actions.max_processes = 1 -- TODO DEFAULT MAXPROCESS
|
actions.max_processes = 1 -- TODO DEFAULT MAXPROCESS
|
||||||
end
|
end
|
||||||
|
@ -406,9 +473,9 @@ function lsyncd_initialize()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
lsyncd.wait_pids(pids, "startup_collector")
|
lsyncd.wait_pids(pids, "startup_collector")
|
||||||
log(NORMAL, "--- Entering normal operation with "..#watches.." monitored directories ---")
|
log(NORMAL, "--- Entering normal operation with "..watches.size.." monitored directories ---")
|
||||||
else
|
else
|
||||||
log(NORMAL, "--- Warmstart into normal operation with "..#watches.." monitored directories ---")
|
log(NORMAL, "--- Warmstart into normal operation with "..watches.size.." monitored directories ---")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -423,7 +490,8 @@ function lsyncd_get_alarm()
|
||||||
local have_alarm = false
|
local have_alarm = false
|
||||||
local alarm = 0
|
local alarm = 0
|
||||||
for _, o in ipairs(origins) do
|
for _, o in ipairs(origins) do
|
||||||
if o.delays[1] ~= nil then
|
if o.delays[1] ~= nil and
|
||||||
|
o.processes.size < o.actions.max_processes then
|
||||||
if have_alarm then
|
if have_alarm then
|
||||||
alarm = lsyncd.earlier(alarm, o.delays[1].alarm)
|
alarm = lsyncd.earlier(alarm, o.delays[1].alarm)
|
||||||
else
|
else
|
||||||
|
@ -432,12 +500,6 @@ function lsyncd_get_alarm()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local hs
|
|
||||||
if have_alarm then
|
|
||||||
hs = "true"
|
|
||||||
else
|
|
||||||
hs = "false"
|
|
||||||
end
|
|
||||||
return have_alarm, alarm
|
return have_alarm, alarm
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -501,9 +563,9 @@ function startup_collector(pid, exitcode)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
-- lsyncd user interface
|
-- lsyncd user interface
|
||||||
------------------------------------------------------------------------------
|
--============================================================================
|
||||||
|
|
||||||
----
|
----
|
||||||
-- Adds one directory (incl. subdir) to be synchronized.
|
-- Adds one directory (incl. subdir) to be synchronized.
|
||||||
|
|
Loading…
Reference in New Issue