2010-10-16 18:21:01 +00:00
|
|
|
------------------------------------------------------------------------------
|
2010-10-18 09:02:51 +00:00
|
|
|
-- lsyncd runner implemented in LUA
|
2010-10-16 18:21:01 +00:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
2010-10-17 15:24:55 +00:00
|
|
|
----
|
|
|
|
-- Table of all directories to watch.
|
2010-10-18 09:02:51 +00:00
|
|
|
local origins = {}
|
2010-10-17 15:24:55 +00:00
|
|
|
|
|
|
|
----
|
|
|
|
-- all targets
|
|
|
|
local targets = {}
|
|
|
|
|
|
|
|
-----
|
|
|
|
-- all watches
|
|
|
|
local watches = {}
|
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
----
|
|
|
|
-- Adds watches for a directory including all subdirectories.
|
|
|
|
--
|
2010-10-17 15:24:55 +00:00
|
|
|
-- @param sdir
|
|
|
|
-- @param target
|
2010-10-18 09:02:51 +00:00
|
|
|
--
|
2010-10-17 15:24:55 +00:00
|
|
|
local function attend_dir(origin, path, target)
|
2010-10-17 17:13:53 +00:00
|
|
|
print("attending dir", origin, "+", path, "->", target.path);
|
2010-10-17 15:24:55 +00:00
|
|
|
-- actual dir = origin + path
|
|
|
|
local op = origin .. path
|
|
|
|
-- register watch and receive watch descriptor
|
|
|
|
local wd = lsyncd.add_watch(op);
|
|
|
|
if wd < 0 then
|
|
|
|
-- failed adding the watch
|
|
|
|
-- TODO die?
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if watches[wd] ~= nil then
|
|
|
|
-- this directory is already watched, add the target
|
|
|
|
local watch = watches[wd]
|
|
|
|
table.insert(watch.attends, { origin = origin, path = path, target = target })
|
|
|
|
else
|
|
|
|
-- new watch
|
|
|
|
local watch = {wd = wd, attends = { origin = origin, path = path, target = target } }
|
|
|
|
watches[wd] = watch
|
|
|
|
end
|
2010-10-16 18:21:01 +00:00
|
|
|
|
2010-10-17 15:24:55 +00:00
|
|
|
-- register all subdirectories
|
|
|
|
local subd = lsyncd.sub_dirs(op);
|
|
|
|
local i, o
|
|
|
|
for i, v in ipairs(subd) do
|
|
|
|
attend_dir(origin, path .. v .. "/", target)
|
2010-10-16 18:21:01 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-16 10:26:48 +00:00
|
|
|
|
2010-10-17 15:24:55 +00:00
|
|
|
----
|
|
|
|
-- Called from core on init or restart after user configuration.
|
|
|
|
--
|
|
|
|
function lsyncd_initialize()
|
|
|
|
print("--- INIT ---")
|
|
|
|
local i, o
|
2010-10-18 09:02:51 +00:00
|
|
|
for i, o in ipairs(origins) do
|
2010-10-17 17:13:53 +00:00
|
|
|
print("Handling ", o.source, "->" , o.targetpath)
|
2010-10-18 09:02:51 +00:00
|
|
|
-- resolves source to be an absolute path
|
|
|
|
local src = lsyncd.real_dir(o.source)
|
|
|
|
if src == nil then
|
|
|
|
print("Cannot resovle source path: ", src)
|
|
|
|
lsyncd.terminate() -- ERRNO
|
|
|
|
end
|
|
|
|
o.source = src
|
|
|
|
|
|
|
|
-- appends the target on target lists
|
2010-10-17 17:13:53 +00:00
|
|
|
local target = { path = o.targetpath }
|
2010-10-17 15:24:55 +00:00
|
|
|
table.insert(targets, target)
|
2010-10-18 09:02:51 +00:00
|
|
|
o.target = target
|
|
|
|
|
|
|
|
-- and add the dir watch inclusively all subdirs
|
2010-10-17 20:26:37 +00:00
|
|
|
attend_dir(o.source, "", target)
|
2010-10-17 15:24:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- lsyncd user interface
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
----
|
2010-10-18 09:02:51 +00:00
|
|
|
-- Adds one directory to be watched.
|
|
|
|
--
|
2010-10-17 17:13:53 +00:00
|
|
|
function add(source_dir, target_path)
|
2010-10-18 09:02:51 +00:00
|
|
|
local o = { source = source_dir, targetpath = target_path }
|
|
|
|
table.insert(origins, o)
|
2010-10-17 15:24:55 +00:00
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2010-10-17 17:13:53 +00:00
|
|
|
-----
|
|
|
|
-- Called by core after initialization.
|
|
|
|
--
|
|
|
|
-- Returns a table of integers (pid of children) the core will
|
|
|
|
-- wait for before entering normal operation.
|
|
|
|
--
|
|
|
|
-- User can override this function by specifing his/her own
|
|
|
|
-- "startup". (and yet may still call default startup)
|
2010-10-18 09:02:51 +00:00
|
|
|
--
|
2010-10-17 17:13:53 +00:00
|
|
|
function default_startup()
|
|
|
|
print("--- STARTUP ---")
|
|
|
|
local pids = { }
|
2010-10-18 09:02:51 +00:00
|
|
|
for i, o in ipairs(origins) do
|
2010-10-17 20:26:37 +00:00
|
|
|
print("/usr/bin/rsync", "-ltrs", o.source, o.targetpath)
|
|
|
|
pid = lsyncd.exec("/usr/bin/rsync", "-ltrs", o.source, o.targetpath)
|
2010-10-17 17:13:53 +00:00
|
|
|
print("started ", pid)
|
|
|
|
table.insert(pids, pid)
|
|
|
|
end
|
2010-10-18 12:23:46 +00:00
|
|
|
return pids
|
2010-10-17 17:13:53 +00:00
|
|
|
end
|
|
|
|
startup = default_startup
|
|
|
|
|
2010-10-18 12:23:46 +00:00
|
|
|
-----
|
|
|
|
-- Called by the core for every child process that
|
|
|
|
-- finished in startup phase
|
|
|
|
--
|
|
|
|
-- Parameters are pid and exitcode of child process
|
|
|
|
--
|
|
|
|
-- Can returns either a new pid if another child process
|
|
|
|
-- has been spawned as replacement (e.g. retry) or 0 if
|
|
|
|
-- finished/ok.
|
|
|
|
--
|
|
|
|
function default_startup_returned(pid, exitcode)
|
|
|
|
print("startup_returned ", pid, exitcode);
|
|
|
|
if exitcode ~= 0 then
|
|
|
|
print("Startup process", pid, " failed")
|
|
|
|
lsyncd.terminate(-1) -- ERRNO
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
startup_returned = default_startup_returned
|
2010-10-14 13:52:01 +00:00
|
|
|
|