lsyncd/lsyncd.lua

194 lines
5.0 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-19 16:40:49 +00:00
----
-- Called by core when an overflow happened.
function default_overflow()
print("--- OVERFLOW on inotify event queue ---")
lsyncd.terminate(-1) -- TODO reset instead.
end
overflow = default_overflow
-----
-- Called by core on event
--
--
-- @return the pid of a spawned child process or 0 if no spawn.
function default_event()
print("got an event")
return 0
end
on_access = default_event
on_modify = default_event
on_attrib = default_event
on_close_write = default_event
on_close_nowrite = default_event
on_open = default_event
on_moved_from = default_event -- lsyncd only unary moved from
on_moved_to = default_event -- lsyncd only unary moved to
on_move = default_event -- lsyncd Addon TODO
on_create = default_event
on_delete = default_event
on_delete_self = default_event
on_move_self = default_event
2010-10-17 17:13:53 +00:00
-----
-- Called by core after initialization.
--
2010-10-19 20:14:55 +00:00
-- Default function will start an simultanous action for every
-- source -> destination pair. And waits for these processes to finish
2010-10-17 17:13:53 +00:00
--
2010-10-19 20:14:55 +00:00
-- The user can override this function by specifing his/her own
2010-10-17 17:13:53 +00:00
-- "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 20:21:38 +00:00
startup_action(o.source, o.targetpath)
2010-10-17 17:13:53 +00:00
table.insert(pids, pid)
end
2010-10-19 20:14:55 +00:00
lsyncd.wait_pids(pids, "startup_collector")
print("--- Entering normal operation with " .. #watches .. " monitored directories ---")
2010-10-17 17:13:53 +00:00
end
startup = default_startup
2010-10-19 20:21:38 +00:00
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
--
2010-10-19 20:14:55 +00:00
-- Can return either a new pid if one other child process
2010-10-18 12:23:46 +00:00
-- has been spawned as replacement (e.g. retry) or 0 if
-- finished/ok.
--
2010-10-19 20:14:55 +00:00
function default_startup_collector(pid, exitcode)
2010-10-18 12:23:46 +00:00
if exitcode ~= 0 then
print("Startup process", pid, " failed")
lsyncd.terminate(-1) -- ERRNO
end
return 0
end
2010-10-19 20:14:55 +00:00
startup_collector = default_startup_collector
2010-10-18 17:09:59 +00:00
----
-- other functions the user might want to use
exec = lsyncd.exec