lsyncd/lsyncd.lua

170 lines
4.4 KiB
Lua
Raw Normal View History

2010-10-16 18:21:01 +00:00
------------------------------------------------------------------------------
2010-10-19 10:20:27 +00:00
-- lsyncd.lua Live (Mirror) Syncing Demon
--
-- License: GPLv2 (see COPYING) or any later version
--
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
-- 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
-- cannot be runned directly from the standard lua interpreter.
2010-10-16 18:21:01 +00:00
------------------------------------------------------------------------------
2010-10-18 17:09:59 +00:00
----
2010-10-19 10:20:27 +00:00
-- A security measurement.
2010-10-18 17:09:59 +00:00
-- Core will exit if version ids mismatch.
lsyncd_version = "2.0b1"
2010-10-17 15:24:55 +00:00
----
-- Table of all directories to watch.
2010-10-18 17:09:59 +00:00
origins = {}
2010-10-17 15:24:55 +00:00
----
-- all targets
2010-10-18 17:09:59 +00:00
targets = {}
2010-10-17 15:24:55 +00:00
-----
-- all watches
2010-10-18 17:09:59 +00:00
watches = {}
2010-10-17 15:24:55 +00:00
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)
-- 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()
local i, o
2010-10-18 09:02:51 +00:00
for i, o in ipairs(origins) do
-- 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
2010-10-19 10:12:11 +00:00
----
-- Calle by core to determine soonest alarm.
--
-- @param now ... the current time representation.
--
-- @return two variables.
-- number -1 means ... alarm is in the past.
-- 0 means ... no alarm, core can in untimed sleep
-- 1 means ... alarm time specified.
-- times ... the alarm time (only read if number is 1)
function lsyncd_get_alarm()
return 0, 0
end
2010-10-17 15:24:55 +00:00
------------------------------------------------------------------------------
-- 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()
2010-10-18 17:09:59 +00:00
print("--- startup ---")
2010-10-17 17:13:53 +00:00
local pids = { }
2010-10-18 09:02:51 +00:00
for i, o in ipairs(origins) do
2010-10-19 10:12:11 +00:00
print("startup recursive rsync: " .. o.source .. " -> " .. o.targetpath)
-- TODO userchangeablefunction
2010-10-17 20:26:37 +00:00
pid = lsyncd.exec("/usr/bin/rsync", "-ltrs", o.source, o.targetpath)
2010-10-17 17:13:53 +00:00
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)
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
2010-10-18 17:09:59 +00:00
-----
-- Called by core after startup phase when finished waiting for
-- children spawned at startup.
--
function default_normalop()
print("--- Entering normal operation with " .. #watches .. " monitored directories ---")
end
normalop = default_normalop
----
-- other functions the user might want to use
exec = lsyncd.exec