mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-12-13 22:48: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 table of the children pids.
|
||||||
* @param (Lua stack) a function of a collector to be called
|
* @param (Lua stack) a function of a collector to be called
|
||||||
|
105
lsyncd.lua
105
lsyncd.lua
@ -22,12 +22,21 @@ lsyncd_version = "2.0b1"
|
|||||||
|
|
||||||
----
|
----
|
||||||
-- Shortcuts (which user is supposed to be able to use them as well)
|
-- Shortcuts (which user is supposed to be able to use them as well)
|
||||||
|
--
|
||||||
log = lsyncd.log
|
log = lsyncd.log
|
||||||
exec = lsyncd.exec
|
exec = lsyncd.exec
|
||||||
|
|
||||||
----
|
----
|
||||||
-- Table of all directories to watch,
|
-- Table of all root directories to sync,
|
||||||
-- filled and used only during initialization.
|
-- filled during initialization.
|
||||||
|
--
|
||||||
|
-- [#] {
|
||||||
|
-- actions = actions,
|
||||||
|
-- source = source_dir,
|
||||||
|
-- targetident = target_identifier,
|
||||||
|
-- target = link to target directory
|
||||||
|
-- }
|
||||||
|
--
|
||||||
origins = {}
|
origins = {}
|
||||||
|
|
||||||
----
|
----
|
||||||
@ -41,11 +50,12 @@ origins = {}
|
|||||||
-- .delays = [#) { .. the delays stack
|
-- .delays = [#) { .. the delays stack
|
||||||
-- .atype .. enum, kind of action
|
-- .atype .. enum, kind of action
|
||||||
-- .wd .. watch descriptor id this origins from TODO needed?
|
-- .wd .. watch descriptor id this origins from TODO needed?
|
||||||
-- .attend .. link to atttender that raised this.
|
-- .attend .. link to atttender that raised this delay.
|
||||||
-- .filename .. filename or nil, means dir itself
|
-- .filename .. filename or nil (=dir itself)
|
||||||
-- (.movepeer) .. for MOVEFROM/MOVETO link to other delay
|
-- (.movepeer) .. for MOVEFROM/MOVETO link to other delay
|
||||||
-- }
|
-- }
|
||||||
-- }
|
-- }
|
||||||
|
--
|
||||||
targets = {}
|
targets = {}
|
||||||
|
|
||||||
-----
|
-----
|
||||||
@ -54,10 +64,9 @@ targets = {}
|
|||||||
-- structure:
|
-- structure:
|
||||||
-- [wd] = {
|
-- [wd] = {
|
||||||
-- .wd .. the watch descriptor (TODO needed?)
|
-- .wd .. the watch descriptor (TODO needed?)
|
||||||
-- .targets = [#] {
|
-- .syncs = [#] { list of stuff to sync to
|
||||||
-- .odir .. origin source dir
|
-- .origin .. link to origin
|
||||||
-- .path .. path of dir
|
-- .path .. relative path of dir
|
||||||
-- .target .. link to targets[#]
|
|
||||||
-- .parent .. link to parent directory in watches
|
-- .parent .. link to parent directory in watches
|
||||||
-- or nil for origin
|
-- or nil for origin
|
||||||
-- }
|
-- }
|
||||||
@ -83,41 +92,38 @@ end
|
|||||||
----
|
----
|
||||||
-- Adds watches for a directory including all subdirectories.
|
-- Adds watches for a directory including all subdirectories.
|
||||||
--
|
--
|
||||||
-- @param odir origin dir
|
-- @param origin link to origins[] entry
|
||||||
-- @param path path in this dir
|
-- @param path relative path of this dir to origin
|
||||||
-- @param target link to target in [targets]
|
|
||||||
-- @param parent link to parent directory in watches[]
|
-- @param parent link to parent directory in watches[]
|
||||||
-- @param actions TODO
|
|
||||||
--
|
--
|
||||||
local function attend_dir(odir, path, target, parent, actions)
|
local function attend_dir(origin, path, parent)
|
||||||
-- actual dir = origin + path
|
local op = origin.source .. path
|
||||||
local op = odir .. path
|
|
||||||
-- register watch and receive watch descriptor
|
-- register watch and receive watch descriptor
|
||||||
local wd = lsyncd.add_watch(op);
|
local wd = lsyncd.add_watch(op);
|
||||||
if wd < 0 then
|
if wd < 0 then
|
||||||
-- failed adding the watch
|
-- failed adding the watch
|
||||||
log(ERROR, "Failure adding watch " .. op .." -> ignored ")
|
log(ERROR, "Failure adding watch "..op.." -> ignored ")
|
||||||
-- TODO die?
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local thiswatch = watches[wd]
|
local thiswatch = watches[wd]
|
||||||
if thiswatch == nil then
|
if thiswatch == nil then
|
||||||
-- new watch
|
-- new watch
|
||||||
thiswatch = {wd = wd, attends = {} }
|
thiswatch = {wd = wd, syncs = {} }
|
||||||
watches[wd] = thiswatch
|
watches[wd] = thiswatch
|
||||||
end
|
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
|
-- register all subdirectories
|
||||||
local subdirs = lsyncd.sub_dirs(op);
|
local subdirs = lsyncd.sub_dirs(op);
|
||||||
for i, dirname in ipairs(subdirs) do
|
for i, dirname in ipairs(subdirs) do
|
||||||
attend_dir(odir, path .. dirname .. "/", target, thiswatch, actions)
|
attend_dir(origin, path..dirname.."/", thiswatch)
|
||||||
end
|
|
||||||
|
|
||||||
-- TODO
|
|
||||||
if actions ~= nil then
|
|
||||||
delay_action(CREATE, target, nil, nil, wd, odir, path)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -131,35 +137,44 @@ function lsyncd_initialize()
|
|||||||
lsyncd.terminate(-1) -- ERRNO
|
lsyncd.terminate(-1) -- ERRNO
|
||||||
end
|
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()
|
-- 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
|
-- 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
|
if asrc == nil then
|
||||||
print("Cannot resolve source path: ", o.source)
|
print("Cannot resolve source path: ", origin.source)
|
||||||
lsyncd.terminate(-1) -- ERRNO
|
lsyncd.terminate(-1) -- ERRNO
|
||||||
end
|
end
|
||||||
|
origin.source = asrc
|
||||||
|
|
||||||
-- appends the target on target lists
|
-- appends the target on target lists
|
||||||
local target = { ident = o.targetident, delays = {} }
|
local target = { ident = origin.targetident, delays = {} }
|
||||||
table.insert(targets, target)
|
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
|
-- and add the dir watch inclusively all subdirs
|
||||||
attend_dir(asrc, "", target, nil)
|
attend_dir(origin, "", nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if have_startup then
|
||||||
log(NORMAL, "--- startup ---")
|
log(NORMAL, "--- startup ---")
|
||||||
local pids = { }
|
local pids = { }
|
||||||
|
for i, origin in ipairs(origins) do
|
||||||
local pid
|
local pid
|
||||||
for i, o in ipairs(origins) do
|
if origin.actions.startup ~= nil then
|
||||||
if (o.actions.startup ~= nil) then
|
local pid = origin.actions.startup(origin.source, origin.targetident)
|
||||||
pid = o.actions.startup(o.source, o.targetident)
|
|
||||||
end
|
|
||||||
table.insert(pids, pid)
|
table.insert(pids, pid)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
lsyncd.wait_pids(pids, "startup_collector")
|
lsyncd.wait_pids(pids, "startup_collector")
|
||||||
log(NORMAL, "--- Entering normal operation with " .. #watches .. " monitored directories ---")
|
log(NORMAL, "--- Entering normal operation with "..#watches.." monitored directories ---")
|
||||||
|
else
|
||||||
|
log(NORMAL, "--- Warmstart into normal operation with "..#watches.." monitored directories ---")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
----
|
----
|
||||||
@ -204,9 +219,9 @@ function lsyncd_event(etype, wd, isdir, filename, filename2)
|
|||||||
end
|
end
|
||||||
-- TODO comment out to safe performance
|
-- TODO comment out to safe performance
|
||||||
if filename2 == nil then
|
if filename2 == nil then
|
||||||
log(DEBUG, "got event " .. event_names[etype] .. " of " .. ftype .. " " .. filename)
|
log(DEBUG, "got event "..event_names[etype].." of "..ftype.." "..filename)
|
||||||
else
|
else
|
||||||
log(DEBUG, "got event " .. event_names[etype] .. " of " .. ftype .. " " .. filename .. " to " .. filename2)
|
log(DEBUG, "got event "..event_names[etype].." of "..ftype.." "..filename.." to "..filename2)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- looks up the watch descriptor id
|
-- looks up the watch descriptor id
|
||||||
@ -217,15 +232,11 @@ function lsyncd_event(etype, wd, isdir, filename, filename2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- works through all possible source->target pairs
|
-- works through all possible source->target pairs
|
||||||
for i, a in ipairs(w.attends) do
|
for i, sync in ipairs(w.syncs) do
|
||||||
log(DEBUG, "odir = " .. a.odir .. " path = " .. a.path)
|
if isdir and eypte == CREATE then
|
||||||
if (isdir) then
|
attend_dir(sync.origin, sync.path..filename.."/", w)
|
||||||
if (etype == CREATE) then
|
|
||||||
attend_dir(a.odir, a.path .. filename .. "/", w, a.actions)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
@ -275,5 +286,3 @@ function default_overflow()
|
|||||||
end
|
end
|
||||||
overflow = default_overflow
|
overflow = default_overflow
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user