lsyncd/lsyncd.lua

123 lines
3.1 KiB
Lua
Raw Normal View History

2010-10-16 18:21:01 +00:00
------------------------------------------------------------------------------
-- lsyncd library functions implemented in C
------------------------------------------------------------------------------
----
-- real_dir(dir)
--
-- Converts a relative directory path to an absolute.
--
-- @param dir a relative path to directory
-- @return absolute path of directory
--
----
--
-- sub_dirs(dir)
--
-- Reads the directories sub directories.
--
-- @param dir absolute path to directory.
-- @return a table of directory names.
--
------------------------------------------------------------------------------
-- lsyncd library functions implemented in LUA
------------------------------------------------------------------------------
2010-10-17 15:24:55 +00:00
----
-- Table of all directories to watch.
local origin = {}
----
-- 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-16 18:21:01 +00:00
-- @param ...
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
for i, o in ipairs(origin) do
2010-10-17 17:13:53 +00:00
print("Handling ", o.source, "->" , o.targetpath)
local target = { path = o.targetpath }
2010-10-17 15:24:55 +00:00
table.insert(targets, target)
2010-10-17 17:13:53 +00:00
origin[i].target = target
2010-10-17 15:24:55 +00:00
attend_dir(lsyncd.real_dir(o.source), "", target)
end
end
------------------------------------------------------------------------------
-- lsyncd user interface
------------------------------------------------------------------------------
----
-- Add one directory to be watched.
2010-10-17 17:13:53 +00:00
function add(source_dir, target_path)
local o = { source = source_dir, targetpath = target_path }
2010-10-17 15:24:55 +00:00
table.insert(origin, o)
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)
function default_startup()
print("--- STARTUP ---")
local pids = { }
for i, o in ipairs(origin) do
pid = lsyncd.exec("/usr/bin/rsyc", "-ltrs", o.source, o.targetpath)
print("started ", pid)
table.insert(pids, pid)
end
return pids
end
startup = default_startup
2010-10-14 13:52:01 +00:00