This commit is contained in:
Axel Kittenberger 2018-03-01 15:08:26 +01:00
parent a78f239fa2
commit ba52ee1a6f
1 changed files with 192 additions and 310 deletions

View File

@ -20,10 +20,7 @@
if lsyncd_version if lsyncd_version
then then
-- ensures the runner is not being loaded twice -- ensures the runner is not being loaded twice
lsyncd.log( lsyncd.log( 'Error', 'You cannot use the lsyncd runner as configuration file!' )
'Error',
'You cannot use the lsyncd runner as configuration file!'
)
lsyncd.terminate( -1 ) lsyncd.terminate( -1 )
end end
@ -3813,20 +3810,10 @@ local Fsevents = ( function
then then
path = path .. '/' path = path .. '/'
if path2 if path2 then path2 = path2 .. '/' end
then
path2 = path2 .. '/'
end
end end
log( log( 'Fsevents', etype, ',', isdir, ',', time, ',', path, ',', path2 )
'Fsevents',
etype, ',',
isdir, ',',
time, ',',
path, ',',
path2
)
for _, sync in Syncs.iwalk() for _, sync in Syncs.iwalk()
do repeat do repeat
@ -3858,11 +3845,7 @@ local Fsevents = ( function
then then
if not relative2 if not relative2
then then
log( log( 'Normal', 'Transformed Move to Delete for ', sync.config.name )
'Normal',
'Transformed Move to Delete for ',
sync.config.name
)
etyped = 'Delete' etyped = 'Delete'
@ -3872,11 +3855,7 @@ local Fsevents = ( function
relative2 = nil relative2 = nil
log( log( 'Normal', 'Transformed Move to Create for ', sync.config.name )
'Normal',
'Transformed Move to Create for ',
sync.config.name
)
etyped = 'Create' etyped = 'Create'
end end
@ -3925,7 +3904,8 @@ Monitors = ( function
-- --
-- The default event monitor. -- The default event monitor.
-- --
local function default( ) local function default
( )
return list[ 1 ] return list[ 1 ]
end end
@ -4049,33 +4029,39 @@ local functionWriter = ( function( )
-- true if there is a second event -- true if there is a second event
local haveEvent2 = false local haveEvent2 = false
for ia, iv in ipairs( args ) do for ia, iv in ipairs( args )
do
-- a list of arguments this arg is being split into -- a list of arguments this arg is being split into
local a = { { true, iv } } local a = { { true, iv } }
-- goes through all translates -- goes through all translates
for _, v in ipairs( transVars ) do for _, v in ipairs( transVars )
do
local ai = 1 local ai = 1
while ai <= #a do while ai <= #a
if a[ ai ][ 1 ] then do
if a[ ai ][ 1 ]
then
local pre, post = local pre, post =
string.match( a[ ai ][ 2 ], '(.*)'..v[1]..'(.*)' ) string.match( a[ ai ][ 2 ], '(.*)'..v[1]..'(.*)' )
if pre then if pre
then
if v[3] > 1 then if v[3] > 1
then
haveEvent2 = true haveEvent2 = true
end end
if pre ~= '' then if pre ~= ''
then
table.insert( a, ai, { true, pre } ) table.insert( a, ai, { true, pre } )
ai = ai + 1 ai = ai + 1
end end
a[ ai ] = { false, v[ 2 ] } a[ ai ] = { false, v[ 2 ] }
if post ~= '' then if post ~= ''
then
table.insert( a, ai + 1, { true, post } ) table.insert( a, ai + 1, { true, post } )
end end
end end
@ -4155,17 +4141,14 @@ local functionWriter = ( function( )
cmd = string.gsub( cmd = string.gsub(
cmd, cmd,
v[ 1 ], v[ 1 ],
function( ) function
( )
occur = true occur = true
return '"$' .. argn .. '"' return '"$' .. argn .. '"'
end end
) )
lc = string.gsub( lc = string.gsub( lc, v[1], ']]..' .. v[2] .. '..[[' )
lc,
v[1],
']]..' .. v[2] .. '..[['
)
if occur if occur
then then
@ -4218,6 +4201,7 @@ local functionWriter = ( function( )
str = string.match( str, '^%s*(.-)%s*$' ) str = string.match( str, '^%s*(.-)%s*$' )
local ft local ft
if string.byte( str, 1, 1 ) == 47 if string.byte( str, 1, 1 ) == 47
then then
-- starts with / -- starts with /
@ -4230,13 +4214,7 @@ local functionWriter = ( function( )
ft = translateShell( str ) ft = translateShell( str )
end end
log( log( 'FWrite', 'translated "', str, '" to \n', ft )
'FWrite',
'translated "',
str,
'" to \n',
ft
)
return ft return ft
end end
@ -4294,14 +4272,7 @@ local StatusFile = ( function
-- already waiting? -- already waiting?
if alarm and timestamp < alarm if alarm and timestamp < alarm
then then
log( log( 'Statusfile', 'waiting(', timestamp, ' < ', alarm, ')' )
'Statusfile',
'waiting(',
timestamp,
' < ',
alarm,
')'
)
return return
end end
@ -4309,17 +4280,11 @@ local StatusFile = ( function
-- determines when a next write will be possible -- determines when a next write will be possible
if not alarm if not alarm
then then
local nextWrite = local nextWrite = lastWritten and timestamp + uSettings.statusInterval
lastWritten and timestamp
+ uSettings.statusInterval
if nextWrite and timestamp < nextWrite if nextWrite and timestamp < nextWrite
then then
log( log( 'Statusfile', 'setting alarm: ', nextWrite )
'Statusfile',
'setting alarm: ',
nextWrite
)
alarm = nextWrite alarm = nextWrite
return return
@ -4400,7 +4365,8 @@ local UserAlarms = ( function
end end
end end
local a = { local a =
{
timestamp = timestamp, timestamp = timestamp,
func = func, func = func,
extra = extra extra = extra
@ -4432,10 +4398,12 @@ local UserAlarms = ( function
-- --
-- Calls user alarms. -- Calls user alarms.
-- --
local function invoke( timestamp ) local function invoke
while (
#alarms > 0 and timestamp
alarms[ 1 ].timestamp <= timestamp )
while #alarms > 0
and alarms[ 1 ].timestamp <= timestamp
do do
alarms[ 1 ].func( alarms[ 1 ].timestamp, alarms[ 1 ].extra ) alarms[ 1 ].func( alarms[ 1 ].timestamp, alarms[ 1 ].extra )
table.remove( alarms, 1 ) table.remove( alarms, 1 )
@ -4486,7 +4454,7 @@ function runner.callError
( (
message message
) )
log('Error', 'in Lua: ', message ) log( 'Error', 'in Lua: ', message )
-- prints backtrace -- prints backtrace
local level = 2 local level = 2
@ -4517,18 +4485,22 @@ end
-- Called from core whenever a child process has finished and -- Called from core whenever a child process has finished and
-- the zombie process was collected by core. -- the zombie process was collected by core.
-- --
function runner.collectProcess( pid, exitcode ) function runner.collectProcess
(
pid, -- process id
exitcode -- exitcode
)
processCount = processCount - 1 processCount = processCount - 1
if processCount < 0 then if processCount < 0
then
error( 'negative number of processes!' ) error( 'negative number of processes!' )
end end
for _, s in Syncs.iwalk() do for _, s in Syncs.iwalk( )
if s:collect(pid, exitcode) then return end do
if s:collect( pid, exitcode ) then return end
end end
end end
-- --
@ -4683,119 +4655,119 @@ function runner.configure( args, monitors )
-- --
-- second paramter is the function to call -- second paramter is the function to call
-- --
local options = { local options =
{
-- log is handled by core already. -- log is handled by core already.
delay = delay =
{ {
1, 1,
function( secs ) function( secs )
clSettings.delay = secs + 0 clSettings.delay = secs + 0
end end
}, },
insist = insist =
{ {
0, 0,
function( ) function( )
clSettings.insist = true clSettings.insist = true
end end
}, },
log = log =
{ {
1, 1,
nil nil
}, },
logfile = logfile =
{ {
1, 1,
function( file ) function( file )
clSettings.logfile = file clSettings.logfile = file
end end
}, },
monitor = monitor =
{ {
-1, -1,
function( monitor ) function( monitor )
if not monitor then if not monitor then
io.stdout:write( 'This Lsyncd supports these monitors:\n' ) io.stdout:write( 'This Lsyncd supports these monitors:\n' )
for _, v in ipairs(Monitors.list) do for _, v in ipairs(Monitors.list) do
io.stdout:write(' ',v,'\n') io.stdout:write(' ',v,'\n')
end
io.stdout:write('\n')
lsyncd.terminate(-1)
else
clSettings.monitor = monitor
end end
io.stdout:write('\n')
lsyncd.terminate(-1)
else
clSettings.monitor = monitor
end end
}, end
},
nodaemon = nodaemon =
{ {
0, 0,
function( ) function( )
clSettings.nodaemon = true clSettings.nodaemon = true
end end
}, },
pidfile = pidfile =
{ {
1, 1,
function( file ) function( file )
clSettings.pidfile=file clSettings.pidfile=file
end end
}, },
rsync = rsync =
{ {
2, 2,
function( src, trg ) function( src, trg )
clSettings.syncs = clSettings.syncs or { } clSettings.syncs = clSettings.syncs or { }
table.insert( table.insert(
clSettings.syncs, clSettings.syncs,
{ 'rsync', src, trg } { 'rsync', src, trg }
) )
end end
}, },
rsyncssh = rsyncssh =
{ {
3, 3,
function( src, host, tdir ) function( src, host, tdir )
clSettings.syncs = clSettings.syncs or { } clSettings.syncs = clSettings.syncs or { }
table.insert( table.insert(
clSettings.syncs, clSettings.syncs,
{ 'rsyncssh', src, host, tdir } { 'rsyncssh', src, host, tdir }
) )
end end
}, },
direct = direct =
{ {
2, 2,
function( src, trg ) function( src, trg )
clSettings.syncs = clSettings.syncs or { } clSettings.syncs = clSettings.syncs or { }
table.insert( table.insert(
clSettings.syncs, clSettings.syncs,
{ 'direct', src, trg } { 'direct', src, trg }
) )
end end
}, },
version = version =
{ {
0, 0,
function( ) function( )
io.stdout:write( 'Version: ', lsyncd_version, '\n' ) io.stdout:write( 'Version: ', lsyncd_version, '\n' )
os.exit( 0 ) os.exit( 0 )
end end
} }
} }
-- non-opts is filled with all args that were no part dash options -- non-opts is filled with all args that were no part dash options
@ -4803,14 +4775,17 @@ function runner.configure( args, monitors )
local nonopts = { } local nonopts = { }
local i = 1 local i = 1
while i <= #args do
while i <= #args
do
local a = args[ i ] local a = args[ i ]
if a:sub( 1, 1 ) ~= '-' then if a:sub( 1, 1 ) ~= '-'
then
table.insert( nonopts, args[ i ] ) table.insert( nonopts, args[ i ] )
else else
if a:sub( 1, 2 ) == '--' then if a:sub( 1, 2 ) == '--'
then
a = a:sub( 3 ) a = a:sub( 3 )
else else
a = a:sub( 2 ) a = a:sub( 2 )
@ -4820,11 +4795,8 @@ function runner.configure( args, monitors )
if not o if not o
then then
log( log( 'Error', 'unknown option command line option ', args[ i ] )
'Error',
'unknown option command line option ',
args[i]
)
os.exit( -1 ) os.exit( -1 )
end end
@ -4865,10 +4837,8 @@ function runner.configure( args, monitors )
then then
if #nonopts ~= 0 if #nonopts ~= 0
then then
log( log( 'Error', 'There cannot be command line syncs and a config file together.' )
'Error',
'There cannot be command line syncs and a config file together.'
)
os.exit( -1 ) os.exit( -1 )
end end
@ -4882,10 +4852,7 @@ function runner.configure( args, monitors )
return nonopts[ 1 ] return nonopts[ 1 ]
else else
-- TODO make this possible -- TODO make this possible
log( log( 'Error', 'There can only be one config file in the command line.' )
'Error',
'There can only be one config file in the command line.'
)
os.exit( -1 ) os.exit( -1 )
end end
@ -4910,7 +4877,7 @@ function runner.initialize( firstTime )
log( log(
'Error', 'Error',
'Do not use settings = { ... }\n'.. 'Do not use settings = { ... }\n'..
' please use settings{ ... } (without the equal sign)' ' please use settings{ ... } ( without the equal sign )'
) )
os.exit( -1 ) os.exit( -1 )
@ -4923,26 +4890,6 @@ function runner.initialize( firstTime )
-- --
lockGlobals( ) lockGlobals( )
--
-- copies simple settings with numeric keys to 'key = true' settings.
--
-- FIXME this can be removed when
-- Lsyncd 2.0.x backwards compatibility is dropped
--
-- for k, v in ipairs( uSettings )
-- do
-- if uSettings[ v ]
-- then
-- log(
-- 'Error',
-- 'Double setting "' .. v.. '"'
-- )
-- os.exit( -1 )
-- end
--
-- uSettings[ v ]= true
-- end
-- --
-- all command line settings overwrite config file settings -- all command line settings overwrite config file settings
-- --
@ -5031,10 +4978,7 @@ function runner.initialize( firstTime )
-- makes sure the user gave Lsyncd anything to do -- makes sure the user gave Lsyncd anything to do
if Syncs.size() == 0 if Syncs.size() == 0
then then
log( log( 'Error', 'Nothing to watch!' )
'Error',
'Nothing to watch!'
)
os.exit( -1 ) os.exit( -1 )
end end
@ -5044,7 +4988,8 @@ function runner.initialize( firstTime )
lsyncd.configure( 'running' ); lsyncd.configure( 'running' );
local ufuncs = { local ufuncs =
{
'onAttrib', 'onAttrib',
'onCreate', 'onCreate',
'onDelete', 'onDelete',
@ -5094,7 +5039,6 @@ function runner.initialize( firstTime )
s:addInitDelay( ) s:addInitDelay( )
end end
end end
end end
-- --
@ -5106,13 +5050,9 @@ end
-- --
function runner.getAlarm function runner.getAlarm
( ) ( )
log( 'Function', 'getAlarm( )' ) log( 'Function', 'getAlarm( )' )
if lsyncdStatus ~= 'run' if lsyncdStatus ~= 'run' then return false end
then
return false
end
local alarm = false local alarm = false
@ -5121,12 +5061,9 @@ function runner.getAlarm
-- --
local function checkAlarm local function checkAlarm
( (
a a -- alarm time
) )
if a == nil if a == nil then error( 'got nil alarm' ) end
then
error('got nil alarm')
end
if alarm == true or not a if alarm == true or not a
then then
@ -5166,14 +5103,9 @@ function runner.getAlarm
-- checks for an userAlarm -- checks for an userAlarm
checkAlarm( UserAlarms.getAlarm( ) ) checkAlarm( UserAlarms.getAlarm( ) )
log( log( 'Alarm', 'runner.getAlarm returns: ', alarm )
'Alarm',
'runner.getAlarm returns: ',
alarm
)
return alarm return alarm
end end
@ -5193,12 +5125,7 @@ function runner.collector
) )
if exitcode ~= 0 if exitcode ~= 0
then then
log( log( 'Error', 'Startup process', pid, ' failed' )
'Error',
'Startup process',
pid,
' failed'
)
terminate( -1 ) terminate( -1 )
end end
@ -5209,54 +5136,41 @@ end
-- --
-- Called by core when an overflow happened. -- Called by core when an overflow happened.
-- --
function runner.overflow( ) function runner.overflow
( )
log( log( 'Normal', '--- OVERFLOW in event queue ---' )
'Normal',
'--- OVERFLOW in event queue ---'
)
lsyncdStatus = 'fade' lsyncdStatus = 'fade'
end end
-- --
-- Called by core on a hup signal. -- Called by core on a hup signal.
-- --
function runner.hup( ) function runner.hup
( )
log( log( 'Normal', '--- HUP signal, resetting ---' )
'Normal',
'--- HUP signal, resetting ---'
)
lsyncdStatus = 'fade' lsyncdStatus = 'fade'
end end
-- --
-- Called by core on a term signal. -- Called by core on a term signal.
-- --
function runner.term( sigcode ) function runner.term
(
local sigtexts = { sigcode -- signal code
[ 2 ] = )
'INT', local sigtexts =
{
[ 15 ] = [ 2 ] = 'INT',
'TERM' [ 15 ] = 'TERM'
}; };
local sigtext = sigtexts[ sigcode ]; local sigtext = sigtexts[ sigcode ];
if not sigtext then if not sigtext then sigtext = 'UNKNOWN' end
sigtext = 'UNKNOWN'
end
log( log( 'Normal', '--- ', sigtext, ' signal, fading ---' )
'Normal',
'--- ', sigtext, ' signal, fading ---'
)
lsyncdStatus = 'fade' lsyncdStatus = 'fade'
@ -5294,38 +5208,28 @@ function spawn(
binary, -- binary to call binary, -- binary to call
... -- arguments ... -- arguments
) )
if if agent == nil
agent == nil or or type( agent ) ~= 'table'
type( agent ) ~= 'table'
then then
error( error( 'spawning with an invalid agent', 2 )
'spawning with an invalid agent',
2
)
end end
if lsyncdStatus == 'fade' then if lsyncdStatus == 'fade'
log( then
'Normal', log( 'Normal', 'ignored process spawning while fading' )
'ignored process spawning while fading'
)
return return
end end
if type( binary ) ~= 'string' then if type( binary ) ~= 'string'
error( then
'calling spawn(agent, binary, ...): binary is not a string', error( 'calling spawn(agent, binary, ...): binary is not a string', 2 )
2
)
end end
local dol = InletFactory.getDelayOrList( agent ) local dol = InletFactory.getDelayOrList( agent )
if not dol then if not dol
error( then
'spawning with an unknown agent', error( 'spawning with an unknown agent', 2 )
2
)
end end
-- --
@ -5360,9 +5264,8 @@ function spawn(
then then
processCount = processCount + 1 processCount = processCount + 1
if if uSettings.maxProcesses
uSettings.maxProcesses and and processCount > uSettings.maxProcesses
processCount > uSettings.maxProcesses
then then
error( 'Spawned too much processes!' ) error( 'Spawned too much processes!' )
end end
@ -5397,14 +5300,7 @@ function spawnShell
command, -- the shell command command, -- the shell command
... -- additonal arguments ... -- additonal arguments
) )
return spawn( return spawn( agent, '/bin/sh', '-c', command, '/bin/sh', ... )
agent,
'/bin/sh',
'-c',
command,
'/bin/sh',
...
)
end end
@ -5417,11 +5313,7 @@ function observefd
ready, -- called when fd is ready to be read ready, -- called when fd is ready to be read
writey -- called when fd is ready to be written writey -- called when fd is ready to be written
) )
return lsyncd.observe_fd( return lsyncd.observe_fd( fd, ready, writey )
fd,
ready,
writey
)
end end
@ -5493,24 +5385,14 @@ function settings
then then
if not settingsCheckgauge[ k ] if not settingsCheckgauge[ k ]
then then
error( error( 'setting "'..k..'" unknown.', 2 )
'setting "'
..k
..'" unknown.',
2
)
end end
uSettings[ k ] = v uSettings[ k ] = v
else else
if not settingsCheckgauge[ v ] if not settingsCheckgauge[ v ]
then then
error( error( 'setting "'..v..'" unknown.', 2 )
'setting "'
..v
..'" unknown.',
2
)
end end
uSettings[ v ] = true uSettings[ v ] = true