mirror of https://github.com/octoleo/lsyncd.git
This commit is contained in:
parent
c877ec25d9
commit
ebf0e3f0c2
87
lsyncd.lua
87
lsyncd.lua
|
@ -224,7 +224,7 @@ end)()
|
||||||
-----
|
-----
|
||||||
-- Holds information about one observed directory inclusively subdirs.
|
-- Holds information about one observed directory inclusively subdirs.
|
||||||
--
|
--
|
||||||
local Origin = (function()
|
local Sync = (function()
|
||||||
----
|
----
|
||||||
-- Creates a new origin
|
-- Creates a new origin
|
||||||
--
|
--
|
||||||
|
@ -246,25 +246,24 @@ end)()
|
||||||
-----
|
-----
|
||||||
-- Puts an action on the delay stack.
|
-- Puts an action on the delay stack.
|
||||||
--
|
--
|
||||||
function Origin.delay(origin, ename, time, pathname, pathname2)
|
function Sync.delay(self, ename, time, pathname, pathname2)
|
||||||
log("Function", "delay(", origin, ", ", ename, ", ", pathname, ")")
|
log("Function", "delay(", self, ", ", ename, ", ", pathname, ")")
|
||||||
local o = origin
|
local delays = self.delays
|
||||||
local delays = o.delays
|
local delayname = self.delayname
|
||||||
local delayname = o.delayname
|
|
||||||
|
|
||||||
if ename == "Move" and not o.config.move then
|
if ename == "Move" and not self.config.move then
|
||||||
-- if there is no move action defined, split a move as delete/create
|
-- if there is no move action defined, split a move as delete/create
|
||||||
log("Debug", "splitting Move into Delete & Create")
|
log("Debug", "splitting Move into Delete & Create")
|
||||||
delay(o, "Delete", time, pathname, nil)
|
delay(self, "Delete", time, pathname, nil)
|
||||||
delay(o, "Create", time, pathname2, nil)
|
delay(self, "Create", time, pathname2, nil)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- creates the new action
|
-- creates the new action
|
||||||
local alarm
|
local alarm
|
||||||
-- TODO scope
|
-- TODO scope
|
||||||
if time and o.config.delay then
|
if time and self.config.delay then
|
||||||
alarm = lsyncd.addto_clock(time, o.config.delay)
|
alarm = lsyncd.addto_clock(time, self.config.delay)
|
||||||
else
|
else
|
||||||
alarm = lsyncd.now()
|
alarm = lsyncd.now()
|
||||||
end
|
end
|
||||||
|
@ -281,7 +280,7 @@ function Origin.delay(origin, ename, time, pathname, pathname2)
|
||||||
-- TODO stackinfo
|
-- TODO stackinfo
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local col = o.config.collapse_table[oldd.ename][newd.ename]
|
local col = self.config.collapse_table[oldd.ename][newd.ename]
|
||||||
if col == -1 then
|
if col == -1 then
|
||||||
-- events cancel each other
|
-- events cancel each other
|
||||||
log("Normal", "Nullfication: ", newd.ename, " after ",
|
log("Normal", "Nullfication: ", newd.ename, " after ",
|
||||||
|
@ -308,11 +307,11 @@ function Origin.delay(origin, ename, time, pathname, pathname2)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Origins - a singleton
|
-- Syncs - a singleton
|
||||||
--
|
--
|
||||||
-- It maintains all configured directories to be synced.
|
-- It maintains all configured directories to be synced.
|
||||||
--
|
--
|
||||||
local Origins = (function()
|
local Syncs = (function()
|
||||||
-- the list of all origins
|
-- the list of all origins
|
||||||
local list = Array.new()
|
local list = Array.new()
|
||||||
|
|
||||||
|
@ -343,11 +342,15 @@ local Origins = (function()
|
||||||
-- Adds a new directory to observe.
|
-- Adds a new directory to observe.
|
||||||
--
|
--
|
||||||
local function add(config)
|
local function add(config)
|
||||||
|
-----
|
||||||
|
-- Creates a new config table and inherit all keys/values
|
||||||
|
-- from integer keyed tables
|
||||||
local uconfig = config
|
local uconfig = config
|
||||||
config = {}
|
config = {}
|
||||||
inherit(config, uconfig)
|
inherit(config, uconfig)
|
||||||
|
|
||||||
-- raises an error if 'name' isnt in opts
|
------
|
||||||
|
-- raises an error if @param name isnt in opts
|
||||||
local function require_opt(name)
|
local function require_opt(name)
|
||||||
if not config[name] then
|
if not config[name] then
|
||||||
local info = debug.getinfo(3, "Sl")
|
local info = debug.getinfo(3, "Sl")
|
||||||
|
@ -357,7 +360,6 @@ local Origins = (function()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
require_opt("source")
|
require_opt("source")
|
||||||
require_opt("target")
|
|
||||||
|
|
||||||
-- absolute path of source
|
-- absolute path of source
|
||||||
local real_src = lsyncd.real_dir(config.source)
|
local real_src = lsyncd.real_dir(config.source)
|
||||||
|
@ -365,9 +367,10 @@ local Origins = (function()
|
||||||
log(Error, "Cannot access source directory: ", config.source)
|
log(Error, "Cannot access source directory: ", config.source)
|
||||||
terminate(-1) -- ERRNO
|
terminate(-1) -- ERRNO
|
||||||
end
|
end
|
||||||
|
config._source = config.source
|
||||||
config.source = real_src
|
config.source = real_src
|
||||||
|
|
||||||
if not config.onAction and not config.onAttrib and
|
if not config.action and not config.onAttrib and
|
||||||
not config.onCreate and not config.onModify and
|
not config.onCreate and not config.onModify and
|
||||||
not config.onDelete and not config.onMove
|
not config.onDelete and not config.onMove
|
||||||
then
|
then
|
||||||
|
@ -388,8 +391,8 @@ local Origins = (function()
|
||||||
optional("action")
|
optional("action")
|
||||||
optional("max_processes")
|
optional("max_processes")
|
||||||
optional("collapse_table")
|
optional("collapse_table")
|
||||||
local o = Origin.new(config)
|
local s = Sync.new(config)
|
||||||
table.insert(list, o)
|
table.insert(list, s)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- allows to walk through all origins
|
-- allows to walk through all origins
|
||||||
|
@ -499,7 +502,7 @@ local Inotifies = (function()
|
||||||
if filename2 then
|
if filename2 then
|
||||||
pathname2 = inotify.path..filename2
|
pathname2 = inotify.path..filename2
|
||||||
end
|
end
|
||||||
Origin.delay(inotify.origin, ename, time, pathname, pathname2)
|
Sync.delay(inotify.origin, ename, time, pathname, pathname2)
|
||||||
-- adds subdirs for new directories
|
-- adds subdirs for new directories
|
||||||
if inotify.recurse and isdir then
|
if inotify.recurse and isdir then
|
||||||
if ename == "Create" then
|
if ename == "Create" then
|
||||||
|
@ -576,10 +579,10 @@ end
|
||||||
function lsyncd_collect_process(pid, exitcode)
|
function lsyncd_collect_process(pid, exitcode)
|
||||||
local delay = nil
|
local delay = nil
|
||||||
local origin = nil
|
local origin = nil
|
||||||
for _, o in Origins.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
delay = o.processes[pid]
|
delay = s.processes[pid]
|
||||||
if delay then
|
if delay then
|
||||||
origin = o
|
origin = s
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -732,14 +735,14 @@ function lsyncd_cycle(now)
|
||||||
if settings.statusfile then
|
if settings.statusfile then
|
||||||
write_statusfile()
|
write_statusfile()
|
||||||
end
|
end
|
||||||
for _, o in Origins.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
if o.processes:size() < o.config.max_processes then
|
if s.processes:size() < s.config.max_processes then
|
||||||
local delays = o.delays
|
local delays = s.delays
|
||||||
local d = delays[1]
|
local d = delays[1]
|
||||||
if d and lsyncd.before_eq(d.alarm, now) then
|
if d and lsyncd.before_eq(d.alarm, now) then
|
||||||
invoke_action(o, d)
|
invoke_action(s, d)
|
||||||
table.remove(delays, 1)
|
table.remove(delays, 1)
|
||||||
o.delayname[d.pathname] = nil -- TODO grab from stack
|
s.delayname[d.pathname] = nil -- TODO grab from stack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -805,7 +808,7 @@ function lsyncd_configure(args)
|
||||||
a = a:sub(2)
|
a = a:sub(2)
|
||||||
end
|
end
|
||||||
local o = options[a]
|
local o = options[a]
|
||||||
if (o) then
|
if o then
|
||||||
-- TODO --
|
-- TODO --
|
||||||
i = i + o[1]
|
i = i + o[1]
|
||||||
end
|
end
|
||||||
|
@ -857,7 +860,7 @@ function lsyncd_initialize()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- makes sure the user gave lsyncd anything to do
|
-- makes sure the user gave lsyncd anything to do
|
||||||
if Origins.size() == 0 then
|
if Syncs.size() == 0 then
|
||||||
log("Error", "Nothing to watch!")
|
log("Error", "Nothing to watch!")
|
||||||
log("Error", "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file.");
|
log("Error", "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file.");
|
||||||
terminate(-1) -- ERRNO
|
terminate(-1) -- ERRNO
|
||||||
|
@ -866,12 +869,12 @@ function lsyncd_initialize()
|
||||||
-- set to true if at least one origin has a startup function
|
-- set to true if at least one origin has a startup function
|
||||||
local have_startup = false
|
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 _, o in Origins.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
if o.config.onStartup then
|
if s.config.onStartup then
|
||||||
have_startup = true
|
have_startup = true
|
||||||
end
|
end
|
||||||
-- adds the dir watch inclusively all subdirs
|
-- adds the dir watch inclusively all subdirs
|
||||||
Inotifies.add(o.source, "", true, o)
|
Inotifies.add(s.source, "", true, o)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- from now on use logging as configured instead of stdout/err.
|
-- from now on use logging as configured instead of stdout/err.
|
||||||
|
@ -881,10 +884,10 @@ function lsyncd_initialize()
|
||||||
if have_startup then
|
if have_startup then
|
||||||
log("Normal", "--- startup ---")
|
log("Normal", "--- startup ---")
|
||||||
local pids = { }
|
local pids = { }
|
||||||
for _, o in Origins.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
local pid
|
local pid
|
||||||
if o.config.onStartup then
|
if s.config.onStartup then
|
||||||
local pid = o.config.onStartup(o.config)
|
local pid = s.config.onStartup(s.config)
|
||||||
table.insert(pids, pid)
|
table.insert(pids, pid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -905,14 +908,14 @@ end
|
||||||
--
|
--
|
||||||
function lsyncd_get_alarm()
|
function lsyncd_get_alarm()
|
||||||
local alarm = false
|
local alarm = false
|
||||||
for _, o in Origins.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
-- TODO better handling of stati.
|
-- TODO better handling of stati.
|
||||||
if o.delays[1] and
|
if s.delays[1] and
|
||||||
o.processes:size() < o.config.max_processes then
|
s.processes:size() < s.config.max_processes then
|
||||||
if alarm then
|
if alarm then
|
||||||
alarm = lsyncd.earlier(alarm, o.delays[1].alarm)
|
alarm = lsyncd.earlier(alarm, s.delays[1].alarm)
|
||||||
else
|
else
|
||||||
alarm = o.delays[1].alarm
|
alarm = s.delays[1].alarm
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -951,7 +954,7 @@ function sync(opts)
|
||||||
if running then
|
if running then
|
||||||
error("Cannot add syncs while running!")
|
error("Cannot add syncs while running!")
|
||||||
end
|
end
|
||||||
Origins.add(opts)
|
Syncs.add(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
Loading…
Reference in New Issue