mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-12-14 06:58:29 +00:00
This commit is contained in:
parent
73f48f0aff
commit
e23328f737
2
lsyncd.c
2
lsyncd.c
@ -517,7 +517,7 @@ l_terminate(lua_State *L)
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits after startup for a table of child processes.
|
||||
* Suspends execution until a table of child processes returned.
|
||||
*
|
||||
* @param (Lua stack) a table of the children pids.
|
||||
* @param (Lua stack) a function of a collector to be called
|
||||
|
97
lsyncd.lua
97
lsyncd.lua
@ -22,12 +22,21 @@ lsyncd_version = "2.0b1"
|
||||
|
||||
----
|
||||
-- Shortcuts (which user is supposed to be able to use them as well)
|
||||
--
|
||||
log = lsyncd.log
|
||||
exec = lsyncd.exec
|
||||
|
||||
----
|
||||
-- Table of all directories to watch,
|
||||
-- filled and used only during initialization.
|
||||
-- Table of all root directories to sync,
|
||||
-- filled during initialization.
|
||||
--
|
||||
-- [#] {
|
||||
-- actions = actions,
|
||||
-- source = source_dir,
|
||||
-- targetident = target_identifier,
|
||||
-- target = link to target directory
|
||||
-- }
|
||||
--
|
||||
origins = {}
|
||||
|
||||
----
|
||||
@ -41,11 +50,12 @@ origins = {}
|
||||
-- .delays = [#) { .. the delays stack
|
||||
-- .atype .. enum, kind of action
|
||||
-- .wd .. watch descriptor id this origins from TODO needed?
|
||||
-- .attend .. link to atttender that raised this.
|
||||
-- .filename .. filename or nil, means dir itself
|
||||
-- .attend .. link to atttender that raised this delay.
|
||||
-- .filename .. filename or nil (=dir itself)
|
||||
-- (.movepeer) .. for MOVEFROM/MOVETO link to other delay
|
||||
-- }
|
||||
-- }
|
||||
--
|
||||
targets = {}
|
||||
|
||||
-----
|
||||
@ -54,10 +64,9 @@ targets = {}
|
||||
-- structure:
|
||||
-- [wd] = {
|
||||
-- .wd .. the watch descriptor (TODO needed?)
|
||||
-- .targets = [#] {
|
||||
-- .odir .. origin source dir
|
||||
-- .path .. path of dir
|
||||
-- .target .. link to targets[#]
|
||||
-- .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
|
||||
-- }
|
||||
@ -83,41 +92,38 @@ end
|
||||
----
|
||||
-- Adds watches for a directory including all subdirectories.
|
||||
--
|
||||
-- @param odir origin dir
|
||||
-- @param path path in this dir
|
||||
-- @param target link to target in [targets]
|
||||
-- @param origin link to origins[] entry
|
||||
-- @param path relative path of this dir to origin
|
||||
-- @param parent link to parent directory in watches[]
|
||||
-- @param actions TODO
|
||||
--
|
||||
local function attend_dir(odir, path, target, parent, actions)
|
||||
-- actual dir = origin + path
|
||||
local op = odir .. path
|
||||
local function attend_dir(origin, path, parent)
|
||||
local op = origin.source .. path
|
||||
-- register watch and receive watch descriptor
|
||||
local wd = lsyncd.add_watch(op);
|
||||
if wd < 0 then
|
||||
-- failed adding the watch
|
||||
log(ERROR, "Failure adding watch "..op.." -> ignored ")
|
||||
-- TODO die?
|
||||
return
|
||||
end
|
||||
|
||||
local thiswatch = watches[wd]
|
||||
if thiswatch == nil then
|
||||
-- new watch
|
||||
thiswatch = {wd = wd, attends = {} }
|
||||
thiswatch = {wd = wd, syncs = {} }
|
||||
watches[wd] = thiswatch
|
||||
end
|
||||
table.insert(thiswatch.attends, { odir = odir, path = path, target = target, parent = parent, actions = actions })
|
||||
local sync = { origin = origin, path = path, parent = parent }
|
||||
table.insert(thiswatch.syncs, sync)
|
||||
|
||||
-- warmstart?
|
||||
if origin.actions.startup == nil then
|
||||
delay_action(CREATE, target, nil, wd, origin, path)
|
||||
end
|
||||
|
||||
-- register all subdirectories
|
||||
local subdirs = lsyncd.sub_dirs(op);
|
||||
for i, dirname in ipairs(subdirs) do
|
||||
attend_dir(odir, path .. dirname .. "/", target, thiswatch, actions)
|
||||
end
|
||||
|
||||
-- TODO
|
||||
if actions ~= nil then
|
||||
delay_action(CREATE, target, nil, nil, wd, odir, path)
|
||||
attend_dir(origin, path..dirname.."/", thiswatch)
|
||||
end
|
||||
end
|
||||
|
||||
@ -131,35 +137,44 @@ function lsyncd_initialize()
|
||||
lsyncd.terminate(-1) -- ERRNO
|
||||
end
|
||||
|
||||
-- set to true if at least one origin has a startup function
|
||||
local have_startup = false
|
||||
-- runs through the origins table filled by user calling directory()
|
||||
for i, o in ipairs(origins) do
|
||||
for i, origin in ipairs(origins) do
|
||||
-- resolves source to be an absolute path
|
||||
local asrc = lsyncd.real_dir(o.source)
|
||||
local asrc = lsyncd.real_dir(origin.source)
|
||||
if asrc == nil then
|
||||
print("Cannot resolve source path: ", o.source)
|
||||
print("Cannot resolve source path: ", origin.source)
|
||||
lsyncd.terminate(-1) -- ERRNO
|
||||
end
|
||||
origin.source = asrc
|
||||
|
||||
-- appends the target on target lists
|
||||
local target = { ident = o.targetident, delays = {} }
|
||||
local target = { ident = origin.targetident, delays = {} }
|
||||
table.insert(targets, target)
|
||||
o.target = target -- TODO needed?
|
||||
|
||||
origin.target = target
|
||||
if origin.actions.startup ~= nil then
|
||||
have_startup = true
|
||||
end
|
||||
-- and add the dir watch inclusively all subdirs
|
||||
attend_dir(asrc, "", target, nil)
|
||||
attend_dir(origin, "", nil)
|
||||
end
|
||||
|
||||
if have_startup then
|
||||
log(NORMAL, "--- startup ---")
|
||||
local pids = { }
|
||||
for i, origin in ipairs(origins) do
|
||||
local pid
|
||||
for i, o in ipairs(origins) do
|
||||
if (o.actions.startup ~= nil) then
|
||||
pid = o.actions.startup(o.source, o.targetident)
|
||||
end
|
||||
if origin.actions.startup ~= nil then
|
||||
local pid = origin.actions.startup(origin.source, origin.targetident)
|
||||
table.insert(pids, pid)
|
||||
end
|
||||
end
|
||||
lsyncd.wait_pids(pids, "startup_collector")
|
||||
log(NORMAL, "--- Entering normal operation with "..#watches.." monitored directories ---")
|
||||
else
|
||||
log(NORMAL, "--- Warmstart into normal operation with "..#watches.." monitored directories ---")
|
||||
end
|
||||
end
|
||||
|
||||
----
|
||||
@ -217,17 +232,13 @@ function lsyncd_event(etype, wd, isdir, filename, filename2)
|
||||
end
|
||||
|
||||
-- works through all possible source->target pairs
|
||||
for i, a in ipairs(w.attends) do
|
||||
log(DEBUG, "odir = " .. a.odir .. " path = " .. a.path)
|
||||
if (isdir) then
|
||||
if (etype == CREATE) then
|
||||
attend_dir(a.odir, a.path .. filename .. "/", w, a.actions)
|
||||
for i, sync in ipairs(w.syncs) do
|
||||
if isdir and eypte == CREATE then
|
||||
attend_dir(sync.origin, sync.path..filename.."/", w)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-----
|
||||
-- Called by the core for every child process that
|
||||
-- finished in startup phase
|
||||
@ -275,5 +286,3 @@ function default_overflow()
|
||||
end
|
||||
overflow = default_overflow
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user