lsyncd/mantle/lsyncd.lua

2554 lines
44 KiB
Lua
Raw Normal View History

2012-02-15 19:10:50 +00:00
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-19 10:20:27 +00:00
-- lsyncd.lua Live (Mirror) Syncing Demon
--
--
2010-11-17 11:14:36 +00:00
-- This is the "runner" part of Lsyncd. It containts all its high-level logic.
-- It works closely together with the Lsyncd core in lsyncd.c. This means it
2010-10-19 10:20:27 +00:00
-- cannot be runned directly from the standard lua interpreter.
2012-02-15 19:10:50 +00:00
--
--
2012-10-03 15:37:49 +00:00
-- This code assumes your editor is at least 100 chars wide.
--
2012-02-15 19:10:50 +00:00
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if mantle
then
print( 'Error, Lsyncd mantle already loaded' )
os.exit( -1 )
2010-10-19 21:56:00 +00:00
end
2012-10-03 15:37:49 +00:00
--
-- Safes mantle stuff wrapped away from user scripts
2010-11-05 18:20:33 +00:00
--
2018-03-14 08:57:49 +00:00
local core = core
local lockGlobals = lockGlobals
local Inotify = Inotify
local Array = Array
local Queue = Queue
local Combiner = Combiner
local Delay = Delay
local InletFactory = InletFactory
local Filter = Filter
2010-11-05 18:20:33 +00:00
--
2010-10-21 12:37:27 +00:00
-- Shortcuts (which user is supposed to be able to use them as well)
2010-10-22 12:58:57 +00:00
--
2018-03-13 11:29:43 +00:00
log = core.log
terminate = core.terminate
now = core.now
readdir = core.readdir
--
-- Coping globals to ensure userscripts cannot change this.
--
2010-11-29 20:32:54 +00:00
local log = log
local terminate = terminate
local now = now
2018-03-01 10:26:12 +00:00
local readdir = readdir
2010-11-28 10:47:57 +00:00
local Monitors
--
-- Global: total number of processess running.
--
local processCount = 0
--
-- All valid entries in a settings{} call.
--
local settingsCheckgauge =
{
logfile = true,
statusFile = true,
statusInterval = true,
logfacility = true,
logident = true,
inotifyMode = true,
maxProcesses = true,
maxDelays = true,
}
2018-02-27 16:14:36 +00:00
--
-- Settings specified by command line.
--
local clSettings = { }
2018-02-27 16:14:36 +00:00
--
-- Settings specified by config scripts.
--
-- FIXME exported global!
uSettings = { }
2018-02-27 16:14:36 +00:00
--
-- A copy of the settings function to see if the
-- user script replaced the settings() by a table
-- ( pre Lsyncd 2.1 style )
--
local settingsSafe
--============================================================================
-- Lsyncd Prototypes
--============================================================================
2018-02-27 16:14:36 +00:00
2012-10-03 07:23:18 +00:00
--
2014-02-28 09:15:48 +00:00
-- Holds information about one observed directory including subdirs.
2010-11-02 14:11:26 +00:00
--
2016-12-13 13:41:35 +00:00
local Sync = ( function
( )
2012-10-03 07:23:18 +00:00
--
2011-11-23 10:05:42 +00:00
-- Syncs that have no name specified by the user script
2010-11-10 22:03:02 +00:00
-- get an incremental default name 'Sync[X]'
2010-11-06 18:26:59 +00:00
--
local nextDefaultName = 1
2010-11-12 20:43:27 +00:00
2018-02-27 16:14:36 +00:00
local function appendFilter
(
self,
rule,
pattern
)
if not self.filters then self.filters = Filters.new( ) end
return self.filters:append( rule, pattern )
end
2012-10-03 07:23:18 +00:00
--
2010-11-11 15:17:22 +00:00
-- Removes a delay.
2010-11-12 20:43:27 +00:00
--
2016-12-13 13:41:35 +00:00
local function removeDelay
(
self,
delay
)
if self.delays[ delay.dpos ] ~= delay
then
2016-12-14 15:29:33 +00:00
error( 'Queue is broken, delay not at dpos' )
2010-11-11 15:17:22 +00:00
end
2012-10-03 07:23:18 +00:00
2016-12-14 15:29:33 +00:00
self.delays:remove( delay.dpos )
2010-11-12 11:04:45 +00:00
2016-12-14 08:02:51 +00:00
-- frees all delays blocked by this one.
2016-12-06 10:11:48 +00:00
if delay.blocks
then
for _, vd in pairs( delay.blocks )
2016-12-06 10:11:48 +00:00
do
2012-01-27 13:01:00 +00:00
vd.status = 'wait'
2010-11-12 11:04:45 +00:00
end
end
2010-11-11 15:17:22 +00:00
end
2018-03-13 14:07:30 +00:00
--
2018-03-14 08:57:49 +00:00
-- Returns false if the relative path is filtered
--
local function testFilter
(
self, -- the Sync
path -- the relative path
)
-- never filter the relative root itself
-- ( that would make zero sense )
2018-03-14 08:57:49 +00:00
if path == '/'
or not self.filters
then
return true
end
2018-03-14 08:57:49 +00:00
return self.filters:test( path )
end
2010-11-11 15:17:22 +00:00
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
-- Returns true if this Sync concerns about 'path'.
--
2016-12-13 13:41:35 +00:00
local function concerns
(
self, -- the Sync
path -- the absolute path
2016-12-13 13:41:35 +00:00
)
-- not concerned if watch rootdir doesn't match
2016-12-06 10:11:48 +00:00
if not path:starts( self.source )
then
return false
end
-- a sub dir and not concerned about subdirs
2017-01-09 10:14:23 +00:00
if self.config.subdirs == false
and path:sub( #self.source, -1 ):match( '[^/]+/?' )
then
return false
end
2018-03-14 08:57:49 +00:00
return testFilter( self, path:sub( #self.source ) )
end
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
-- Collects a child process.
2010-11-08 12:14:10 +00:00
--
2016-12-13 13:41:35 +00:00
local function collect
(
self, -- the sync
pid, -- process id of collected child process
exitcode -- exitcode of child process
)
2012-10-03 07:23:18 +00:00
local delay = self.processes[ pid ]
-- not a child of this sync?
if not delay then return end
2010-11-10 22:03:02 +00:00
2016-12-06 10:11:48 +00:00
if delay.status
then
2012-10-03 07:23:18 +00:00
log( 'Delay', 'collected an event' )
2016-12-06 10:11:48 +00:00
if delay.status ~= 'active'
then
error( 'collecting a non-active process' )
2010-11-12 18:52:43 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-30 22:56:34 +00:00
local rc = self.config.collect(
2012-10-03 07:23:18 +00:00
InletFactory.d2e( delay ),
exitcode
)
2016-08-29 11:15:29 +00:00
if rc == 'die'
then
log( 'Error', 'Critical exitcode.' )
2012-10-03 15:37:49 +00:00
terminate( -1 )
elseif rc ~= 'again'
2016-08-29 11:15:29 +00:00
then
2010-11-13 20:21:12 +00:00
-- if its active again the collecter restarted the event
2012-10-03 07:23:18 +00:00
removeDelay( self, delay )
2012-10-03 07:23:18 +00:00
log(
'Delay',
'Finish of ',
delay.etype,
' on ',
self.source,delay.path,
' = ',
exitcode
)
2011-11-23 10:05:42 +00:00
else
2010-11-13 20:21:12 +00:00
-- sets the delay on wait again
2011-11-23 10:05:42 +00:00
local alarm = self.config.delay
2012-10-03 07:23:18 +00:00
2010-11-13 20:31:24 +00:00
-- delays at least 1 second
2018-03-01 10:26:12 +00:00
if alarm < 1 then alarm = 1 end
2012-10-03 07:23:18 +00:00
delay:wait( now( ) + alarm )
2010-11-13 20:21:12 +00:00
end
2010-11-12 18:52:43 +00:00
else
2018-03-01 10:26:12 +00:00
log( 'Delay', 'collected a list' )
2012-10-03 07:23:18 +00:00
2010-11-30 22:56:34 +00:00
local rc = self.config.collect(
2012-10-03 07:23:18 +00:00
InletFactory.dl2el( delay ),
exitcode
)
2016-12-06 10:11:48 +00:00
if rc == 'die'
then
2012-10-03 07:23:18 +00:00
log( 'Error', 'Critical exitcode.' );
2016-12-06 10:11:48 +00:00
2012-10-03 15:37:49 +00:00
terminate( -1 )
elseif rc == 'again'
2016-12-06 10:11:48 +00:00
then
2010-11-13 20:31:24 +00:00
-- sets the delay on wait again
2011-11-23 10:05:42 +00:00
local alarm = self.config.delay
2016-12-06 10:11:48 +00:00
-- delays are at least 1 second
if alarm < 1 then alarm = 1 end
2012-10-03 07:23:18 +00:00
2010-11-29 20:32:54 +00:00
alarm = now() + alarm
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
for _, d in ipairs( delay )
do
d:wait( alarm )
2010-11-13 20:31:24 +00:00
end
else
for _, d in ipairs( delay )
do
2012-10-03 07:23:18 +00:00
removeDelay( self, d )
2010-11-12 18:52:43 +00:00
end
end
2012-10-03 07:23:18 +00:00
log( 'Delay','Finished list = ',exitcode )
2010-11-12 18:52:43 +00:00
end
2012-10-03 07:23:18 +00:00
self.processes[ pid ] = nil
2010-11-08 12:14:10 +00:00
end
2010-11-06 18:26:59 +00:00
2012-10-03 07:23:18 +00:00
--
2011-11-23 10:05:42 +00:00
-- Stacks a newDelay on the oldDelay,
2010-11-12 09:45:22 +00:00
-- the oldDelay blocks the new Delay.
--
2011-11-23 10:05:42 +00:00
-- A delay can block 'n' other delays,
2010-11-12 09:45:22 +00:00
-- but is blocked at most by one, the latest delay.
2011-11-23 10:05:42 +00:00
--
2016-12-13 13:41:35 +00:00
local function stack
(
oldDelay,
newDelay
)
2016-12-14 08:02:51 +00:00
newDelay:blockedBy( oldDelay )
2010-11-12 09:45:22 +00:00
end
2012-10-03 07:23:18 +00:00
--
2010-11-06 18:26:59 +00:00
-- Puts an action on the delay stack.
--
2016-12-13 13:41:35 +00:00
local function delay
(
self, -- the sync
etype, -- the event type
time, -- time of the event
path, -- path of the event
path2 -- desitination path of move events
)
2012-10-03 07:23:18 +00:00
log(
'Function',
'delay( ',
2012-10-03 07:23:18 +00:00
self.config.name, ', ',
etype, ', ',
path, ', ',
path2,
' )'
2012-10-03 07:23:18 +00:00
)
2012-02-16 15:49:34 +00:00
--
-- In case new directories were created
-- looks through this directories and makes create events for
-- new stuff found in there.
--
2016-12-13 13:41:35 +00:00
local function recurse
( )
2016-12-06 10:11:48 +00:00
if etype == 'Create' and path:byte( -1 ) == 47
then
2018-03-13 11:29:43 +00:00
local entries = core.readdir( self.source .. path )
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
if entries
then
for dirname, isdir in pairs( entries )
do
local pd = path .. dirname
2012-10-03 07:23:18 +00:00
if isdir then pd = pd..'/' end
2012-10-03 07:23:18 +00:00
2018-03-01 10:26:12 +00:00
log( 'Delay', 'Create creates Create on ', pd )
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
delay( self, 'Create', time, pd, nil )
end
end
end
end
2010-11-20 22:32:25 +00:00
-- exclusion tests
2016-12-06 10:11:48 +00:00
if not path2
then
2011-01-19 15:17:11 +00:00
-- simple test for single path events
2018-03-14 08:57:49 +00:00
if not testFilter( self, path )
2016-12-06 10:11:48 +00:00
then
log( 'Filter', 'filtered ', etype, ' on "', path, '"' )
2018-02-27 16:14:36 +00:00
2010-11-13 16:59:46 +00:00
return
end
else
2016-12-06 10:11:48 +00:00
-- for double paths ( move ) it might result into a split
2018-03-14 08:57:49 +00:00
local ex1 = not testFilter( self, path )
2012-10-03 07:23:18 +00:00
2018-03-14 08:57:49 +00:00
local ex2 = not testFilter( self, path2 )
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
if ex1 and ex2
then
log(
'Filter',
'filtered "', etype, ' on "', path,
'" -> "', path2, '"'
)
2012-10-03 07:23:18 +00:00
2010-11-13 16:59:46 +00:00
return
2016-12-06 10:11:48 +00:00
elseif not ex1 and ex2
then
2018-03-13 14:07:30 +00:00
-- splits the move if only partly filtered
2012-10-03 07:23:18 +00:00
log(
'Filter',
'filtered destination transformed ',
2012-10-03 07:23:18 +00:00
etype,
' to Delete ',
path
)
2018-02-27 16:14:36 +00:00
delay( self, 'Delete', time, path, nil )
2012-10-03 07:23:18 +00:00
2010-11-13 16:59:46 +00:00
return
2016-12-06 10:11:48 +00:00
elseif ex1 and not ex2
then
2018-03-13 14:07:30 +00:00
-- splits the move if only partly filtered
2012-10-03 07:23:18 +00:00
log(
'Filter',
'filtered origin transformed ',
2012-10-03 07:23:18 +00:00
etype,
' to Create.',
path2
)
2018-02-27 16:14:36 +00:00
delay( self, 'Create', time, path2, nil )
2012-10-03 07:23:18 +00:00
2010-11-13 16:59:46 +00:00
return
end
2010-11-10 09:49:44 +00:00
end
2010-11-06 18:26:59 +00:00
if etype == 'Move'
and not self.config.onMove
2016-12-06 10:11:48 +00:00
then
2011-11-23 10:05:42 +00:00
-- if there is no move action defined,
2010-11-12 09:45:22 +00:00
-- split a move as delete/create
2010-11-20 22:32:25 +00:00
-- layer 1 scripts which want moves events have to
2012-01-27 13:01:00 +00:00
-- set onMove simply to 'true'
2012-10-03 07:23:18 +00:00
log( 'Delay', 'splitting Move into Delete & Create' )
2016-12-06 10:11:48 +00:00
2012-10-03 07:23:18 +00:00
delay( self, 'Delete', time, path, nil )
2016-12-06 10:11:48 +00:00
2012-10-03 07:23:18 +00:00
delay( self, 'Create', time, path2, nil )
2016-12-06 10:11:48 +00:00
return
2010-11-06 18:26:59 +00:00
end
-- creates the new action
2011-11-23 10:05:42 +00:00
local alarm
2016-12-06 10:11:48 +00:00
if time and self.config.delay
then
alarm = time + self.config.delay
2010-11-06 18:26:59 +00:00
else
2012-10-03 07:23:18 +00:00
alarm = now( )
2010-11-06 18:26:59 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-10 09:49:44 +00:00
-- new delay
2018-02-27 16:14:36 +00:00
local nd = Delay.new( etype, self, alarm, path, path2 )
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
if nd.etype == 'Init' or nd.etype == 'Blanket'
then
-- always stack init or blanket events on the last event
log(
'Delay',
'Stacking ',
nd.etype,
' event.'
)
if #self.delays > 0
2016-12-06 10:11:48 +00:00
then
2016-12-14 15:29:33 +00:00
stack( self.delays:last( ), nd )
2010-11-12 09:45:22 +00:00
end
2016-12-14 15:29:33 +00:00
nd.dpos = self.delays:push( nd )
2012-10-03 07:23:18 +00:00
recurse( )
2010-11-10 12:59:51 +00:00
return
2010-11-10 09:49:44 +00:00
end
2011-11-23 10:05:42 +00:00
-- detects blocks and combos by working from back until
2010-11-10 09:49:44 +00:00
-- front through the fifo
2016-12-14 15:29:33 +00:00
for il, od in self.delays:qpairsReverse( )
2016-12-06 10:11:48 +00:00
do
2010-12-11 20:00:48 +00:00
-- asks Combiner what to do
2012-10-03 07:23:18 +00:00
local ac = Combiner.combine( od, nd )
2010-11-10 11:23:26 +00:00
2016-12-06 10:11:48 +00:00
if ac
then
Combiner.log( ac, od, nd )
2016-12-06 10:11:48 +00:00
if ac == 'remove'
then
2016-12-14 15:29:33 +00:00
self.delays:remove( il )
2016-12-06 10:11:48 +00:00
elseif ac == 'stack'
then
2012-10-03 07:23:18 +00:00
stack( od, nd )
2016-12-14 15:29:33 +00:00
nd.dpos = self.delays:push( nd )
elseif ac == 'toDelete,stack'
then
2016-12-22 13:59:18 +00:00
if od.status ~= 'active'
then
-- turns olddelay into a delete
local rd = Delay.new( 'Delete', self, od.alarm, od.path )
2016-12-22 13:59:18 +00:00
self.delays:replace( il, rd )
2016-12-22 13:59:18 +00:00
rd.dpos = il
2016-12-22 12:55:59 +00:00
2016-12-22 14:15:17 +00:00
-- and stacks delay2
stack( rd, nd )
else
-- and stacks delay2
stack( od, nd )
end
2016-12-22 12:55:59 +00:00
nd.dpos = self.delays:push( nd )
2016-12-06 10:11:48 +00:00
elseif ac == 'absorb'
then
-- nada
2016-12-06 10:11:48 +00:00
elseif ac == 'replace'
then
2016-12-22 13:59:18 +00:00
if od.status ~= 'active'
then
self.delays:replace( il, nd )
2016-12-22 13:59:18 +00:00
nd.dpos = il
else
stack( od, nd )
nd.dpos = self.delays:push( nd )
end
2016-12-06 10:11:48 +00:00
elseif ac == 'split'
then
2012-10-03 07:23:18 +00:00
delay( self, 'Delete', time, path, nil )
2016-12-06 10:11:48 +00:00
2012-10-03 07:23:18 +00:00
delay( self, 'Create', time, path2, nil )
2011-11-23 10:05:42 +00:00
else
2012-10-03 07:23:18 +00:00
error( 'unknown result of combine()' )
2010-11-06 18:26:59 +00:00
end
2016-12-06 11:57:32 +00:00
2012-10-03 07:23:18 +00:00
recurse( )
2016-12-06 11:57:32 +00:00
return
2010-11-06 18:26:59 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-10 09:49:44 +00:00
il = il - 1
2010-11-06 18:26:59 +00:00
end
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
if nd.path2
then
log( 'Delay', 'New ', nd.etype, ': ', nd.path, ' -> ', nd.path2 )
else
log( 'Delay', 'New ', nd.etype, ': ', nd.path )
end
2012-10-03 07:23:18 +00:00
-- no block or combo
2016-12-14 15:29:33 +00:00
nd.dpos = self.delays:push( nd )
2016-12-06 10:11:48 +00:00
2012-10-03 07:23:18 +00:00
recurse( )
2010-11-06 18:26:59 +00:00
end
2010-11-20 22:32:25 +00:00
2010-11-06 21:29:22 +00:00
--
2012-10-03 07:23:18 +00:00
-- Returns the soonest alarm for this Sync.
--
2016-12-14 13:25:20 +00:00
local function getAlarm
(
self
)
if #self.processes >= self.config.maxProcesses
2016-12-06 10:11:48 +00:00
then
2010-12-01 13:25:05 +00:00
return false
2010-11-30 23:14:17 +00:00
end
2010-11-06 21:29:22 +00:00
2012-02-15 13:45:46 +00:00
-- first checks if more processes could be spawned
-- finds the nearest delay waiting to be spawned
for _, d in self.delays:qpairs( )
do
if d.status == 'wait'
then
return d.alarm
2010-11-11 19:52:20 +00:00
end
2010-11-06 21:29:22 +00:00
end
2010-11-11 19:52:20 +00:00
2010-11-30 23:14:17 +00:00
-- nothing to spawn
2010-12-01 13:25:05 +00:00
return false
2010-11-06 21:29:22 +00:00
end
2011-11-23 10:05:42 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-12 18:52:43 +00:00
-- Gets all delays that are not blocked by active delays.
2010-11-08 12:14:10 +00:00
--
2016-12-13 13:41:35 +00:00
local function getDelays
(
self, -- the sync
test -- function to test each delay
)
2016-12-06 10:11:48 +00:00
local dlist = { sync = self }
2010-12-11 23:00:33 +00:00
local dlistn = 1
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
local blocks = { }
2010-11-12 18:52:43 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-12 18:52:43 +00:00
-- inheritly transfers all blocks from delay
--
2016-12-13 15:37:57 +00:00
local function getBlocks
(
delay
)
2012-10-03 07:23:18 +00:00
blocks[ delay ] = true
2016-12-06 10:11:48 +00:00
if delay.blocks
then
for _, d in ipairs( delay.blocks )
2016-12-06 10:11:48 +00:00
do
2012-10-03 07:23:18 +00:00
getBlocks( d )
2010-11-13 18:04:37 +00:00
end
2010-11-08 12:14:10 +00:00
end
2010-11-12 18:52:43 +00:00
end
2016-12-14 15:29:33 +00:00
for _, d in self.delays:qpairs( )
2016-12-06 10:11:48 +00:00
do
local tr = true
if test
then
tr = test( InletFactory.d2e( d ) )
end
2018-02-27 16:14:36 +00:00
if tr == 'break' then break end
if d.status == 'active' or not tr
2010-11-13 18:04:37 +00:00
then
2012-10-03 07:23:18 +00:00
getBlocks( d )
2016-12-06 10:11:48 +00:00
elseif not blocks[ d ]
then
2012-10-03 07:23:18 +00:00
dlist[ dlistn ] = d
2016-12-06 10:11:48 +00:00
2010-12-11 23:00:33 +00:00
dlistn = dlistn + 1
2010-11-08 12:14:10 +00:00
end
end
2011-11-23 10:05:42 +00:00
2010-11-12 18:52:43 +00:00
return dlist
2010-11-08 12:14:10 +00:00
end
2012-10-03 07:23:18 +00:00
--
2010-11-07 01:06:08 +00:00
-- Creates new actions
--
2016-12-13 13:41:35 +00:00
local function invokeActions
(
self,
timestamp
)
2018-03-14 08:57:49 +00:00
log( 'Function', 'invokeActions( "', self.config.name, '", ', timestamp, ' )' )
2012-10-03 07:23:18 +00:00
if #self.processes >= self.config.maxProcesses
then
2010-11-08 12:14:10 +00:00
-- no new processes
2010-11-07 01:06:08 +00:00
return
end
2012-10-03 07:23:18 +00:00
2016-12-14 15:29:33 +00:00
for _, d in self.delays:qpairs( )
do
-- if reached the global limit return
2016-12-13 13:41:35 +00:00
if uSettings.maxProcesses
and processCount >= uSettings.maxProcesses
2012-10-03 07:23:18 +00:00
then
2018-03-14 08:57:49 +00:00
log( 'Alarm', 'at global process limit.' )
return
end
2012-10-03 07:23:18 +00:00
if #self.delays < self.config.maxDelays
2016-12-06 10:11:48 +00:00
then
2011-11-23 10:05:42 +00:00
-- time constrains are only concerned if not maxed
2010-11-12 20:55:22 +00:00
-- the delay FIFO already.
2016-12-06 10:11:48 +00:00
if d.alarm ~= true and timestamp < d.alarm
then
2010-11-12 20:55:22 +00:00
-- reached point in stack where delays are in future
return
end
2010-11-08 12:14:10 +00:00
end
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
if d.status == 'wait'
then
2010-11-08 12:14:10 +00:00
-- found a waiting delay
2016-12-06 10:11:48 +00:00
if d.etype ~= 'Init'
then
2012-10-03 07:23:18 +00:00
self.config.action( self.inlet )
else
2012-10-03 07:23:18 +00:00
self.config.init( InletFactory.d2e( d ) )
end
if #self.processes >= self.config.maxProcesses
2016-12-06 10:11:48 +00:00
then
2010-11-08 12:14:10 +00:00
-- no further processes
return
end
end
2010-11-07 01:06:08 +00:00
end
end
2011-11-23 10:05:42 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-12 18:52:43 +00:00
-- Gets the next event to be processed.
--
2016-12-13 13:41:35 +00:00
local function getNextDelay
(
self,
timestamp
)
2016-12-14 15:29:33 +00:00
for i, d in self.delays:qpairs( )
2016-12-06 10:11:48 +00:00
do
if #self.delays < self.config.maxDelays
2016-12-06 10:11:48 +00:00
then
2011-11-23 10:05:42 +00:00
-- time constrains are only concerned if not maxed
2010-11-12 20:55:22 +00:00
-- the delay FIFO already.
2016-12-06 10:11:48 +00:00
if d.alarm ~= true and timestamp < d.alarm
then
2010-11-12 20:55:22 +00:00
-- reached point in stack where delays are in future
return nil
end
2010-11-12 18:52:43 +00:00
end
2012-10-03 07:23:18 +00:00
2016-12-06 10:11:48 +00:00
if d.status == 'wait'
then
2010-11-12 18:52:43 +00:00
-- found a waiting delay
return d
end
end
end
2010-11-07 01:06:08 +00:00
--
-- Adds and returns a blanket delay thats blocks all.
-- Used as custom marker.
2010-11-07 09:53:39 +00:00
--
2016-12-13 13:41:35 +00:00
local function addBlanketDelay
(
self
)
2012-10-03 07:23:18 +00:00
local newd = Delay.new( 'Blanket', self, true, '' )
2016-12-06 10:11:48 +00:00
2016-12-14 15:29:33 +00:00
newd.dpos = self.delays:push( newd )
2016-12-06 10:11:48 +00:00
2011-11-23 10:05:42 +00:00
return newd
2010-11-07 09:53:39 +00:00
end
2011-11-23 10:05:42 +00:00
2012-10-03 07:23:18 +00:00
--
-- Adds and returns a blanket delay thats blocks all.
-- Used as startup marker to call init asap.
--
2016-12-13 13:41:35 +00:00
local function addInitDelay
(
self
)
2012-10-03 07:23:18 +00:00
local newd = Delay.new( 'Init', self, true, '' )
2016-12-14 15:29:33 +00:00
newd.dpos = self.delays:push( newd )
2011-11-23 10:05:42 +00:00
return newd
end
2011-11-23 10:05:42 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-12 10:07:58 +00:00
-- Writes a status report about delays in this sync.
--
2016-12-13 13:41:35 +00:00
local function statusReport
(
self,
f
)
2012-01-27 13:01:00 +00:00
local spaces = ' '
2012-10-03 07:23:18 +00:00
f:write( self.config.name, ' source=', self.source, '\n' )
2016-12-06 10:11:48 +00:00
f:write( 'There are ', #self.delays, ' delays\n')
2012-10-03 07:23:18 +00:00
2016-12-14 15:29:33 +00:00
for i, vd in self.delays:qpairs( )
2016-12-06 10:11:48 +00:00
do
2010-11-12 11:04:45 +00:00
local st = vd.status
2016-12-14 15:29:33 +00:00
2012-10-03 07:23:18 +00:00
f:write( st, string.sub( spaces, 1, 7 - #st ) )
f:write( vd.etype, ' ' )
f:write( vd.path )
2016-12-06 10:11:48 +00:00
if vd.path2
then
2012-10-03 07:23:18 +00:00
f:write( ' -> ',vd.path2 )
2010-11-12 10:07:58 +00:00
end
2012-10-03 07:23:18 +00:00
2012-01-27 13:01:00 +00:00
f:write('\n')
2012-10-03 07:23:18 +00:00
2010-11-12 10:07:58 +00:00
end
2012-10-03 07:23:18 +00:00
f:write( 'Filtering:\n' )
2012-10-03 07:23:18 +00:00
2010-11-13 16:59:46 +00:00
local nothing = true
2012-10-03 07:23:18 +00:00
if self.filters
then
for _, e in pairs( self.filters.list )
do
nothing = false
f:write( e.rule, ' ', e.pattern,'\n' )
end
end
2016-12-06 10:11:48 +00:00
if nothing
then
2012-01-27 13:01:00 +00:00
f:write(' nothing.\n')
2010-11-13 16:59:46 +00:00
end
2012-10-03 07:23:18 +00:00
f:write( '\n' )
2010-11-20 22:32:25 +00:00
end
2010-11-13 16:59:46 +00:00
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
-- Creates a new Sync.
2010-11-02 14:11:26 +00:00
--
local function new
(
config
)
2016-12-06 10:11:48 +00:00
local s =
{
2010-11-10 09:49:44 +00:00
-- fields
2010-11-02 14:11:26 +00:00
config = config,
2012-10-03 07:23:18 +00:00
delays = Queue.new( ),
2010-11-05 15:18:01 +00:00
source = config.source,
processes = Counter.new( ),
2018-02-27 16:14:36 +00:00
filters = nil,
2010-11-06 21:29:22 +00:00
-- functions
2010-11-13 19:22:05 +00:00
addBlanketDelay = addBlanketDelay,
addExclude = addExclude,
addInitDelay = addInitDelay,
2018-02-27 16:14:36 +00:00
appendFilter = appendFilter,
2010-11-08 12:14:10 +00:00
collect = collect,
concerns = concerns,
2010-11-08 12:14:10 +00:00
delay = delay,
getAlarm = getAlarm,
2010-11-12 18:52:43 +00:00
getDelays = getDelays,
2010-11-08 12:14:10 +00:00
getNextDelay = getNextDelay,
invokeActions = invokeActions,
2010-11-11 15:17:22 +00:00
removeDelay = removeDelay,
2010-11-13 19:22:05 +00:00
rmExclude = rmExclude,
2010-11-12 10:07:58 +00:00
statusReport = statusReport,
2010-11-02 14:11:26 +00:00
}
2012-10-03 07:23:18 +00:00
s.inlet = InletFactory.newInlet( s )
2010-11-30 22:56:34 +00:00
2010-11-06 18:26:59 +00:00
-- provides a default name if needed
2016-12-06 10:11:48 +00:00
if not config.name
then
2012-10-03 07:23:18 +00:00
config.name = 'Sync' .. nextDefaultName
2010-11-06 18:26:59 +00:00
end
2012-10-03 07:23:18 +00:00
-- increments defaults if a config name was given or not
-- so Sync{n} will be the n-th call to sync{}
2010-11-06 18:26:59 +00:00
nextDefaultName = nextDefaultName + 1
2010-11-13 16:59:46 +00:00
2018-02-27 16:14:36 +00:00
-- loads filters
if config.filter
then
local te = type( config.filter )
s.filters = Filters.new( )
if te == 'table'
then
s.filters:appendList( config.filter )
elseif te == 'string'
then
s.filters:append( config.filter )
else
error( 'type for filter must be table or string', 2 )
end
end
if config.delay ~= nil
and ( type( config.delay ) ~= 'number' or config.delay < 0 )
2013-06-07 12:09:57 +00:00
then
error( 'delay must be a number and >= 0', 2 )
end
2018-02-27 16:14:36 +00:00
if config.filterFrom
then
if not s.filters then s.filters = Filters.new( ) end
s.filters:loadFile( config.filterFrom )
end
2010-11-06 18:26:59 +00:00
return s
2010-11-02 14:11:26 +00:00
end
2010-11-08 12:14:10 +00:00
--
2012-10-03 07:23:18 +00:00
-- Public interface
--
2016-12-13 13:41:35 +00:00
return { new = new }
2012-10-03 07:23:18 +00:00
end )( )
2010-11-02 14:11:26 +00:00
2010-10-25 17:38:57 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:00 +00:00
-- Syncs - a singleton
2011-11-23 10:05:42 +00:00
--
2012-10-03 07:23:18 +00:00
-- Syncs maintains all configured syncs.
2010-11-02 14:11:26 +00:00
--
Syncs = ( function
2016-12-13 13:41:35 +00:00
( )
--
-- Metatable.
--
local mt = { }
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:33:26 +00:00
-- the list of all syncs
2010-11-06 18:26:59 +00:00
--
local syncList = Array.new( )
--
-- Returns the number of syncs.
--
mt.__len = function
( )
return #syncList
end
2012-10-03 07:23:18 +00:00
--
-- The round robin counter. In case of global limited maxProcesses
-- gives every sync equal chances to spawn the next process.
--
local round = 0
2012-10-03 07:23:18 +00:00
--
-- The cycle( ) sheduler goes into the next round of roundrobin.
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
local function nextRound
( )
round = round + 1;
2012-10-03 07:23:18 +00:00
if round >= #syncList then round = 0 end
2012-10-03 07:23:18 +00:00
return round
end
2012-10-03 07:23:18 +00:00
--
-- Returns the round
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
local function getRound
( )
return round
end
2012-10-03 07:23:18 +00:00
--
-- Returns sync at listpos i
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
local function get
( i )
return syncList[ i ];
end
2012-10-03 07:23:18 +00:00
--
-- Helper function for inherit
-- defined below
--
local inheritKV
--
-- Recurvely inherits a source table to a destionation table
-- copying all keys from source.
--
-- All entries with integer keys are inherited as additional
-- sources for non-verbatim tables
2010-11-03 11:37:25 +00:00
--
2016-12-13 13:41:35 +00:00
local function inherit
(
cd, -- table copy destination
cs, -- table copy source
verbatim -- forced verbatim ( for e.g. 'exitcodes' )
2016-08-29 11:15:29 +00:00
)
2016-05-03 08:45:22 +00:00
-- First copies all entries with non-integer keys.
--
2016-05-03 08:45:22 +00:00
-- Tables are merged; already present keys are not
-- overwritten
--
-- For verbatim tables integer keys are treated like
2016-05-03 08:45:22 +00:00
-- non-integer keys
2016-08-29 11:15:29 +00:00
for k, v in pairs( cs )
do
if
(
2018-03-01 10:26:12 +00:00
type( k ) ~= 'number'
or verbatim
or cs._verbatim == true
)
and
(
2018-03-01 10:26:12 +00:00
type( cs._merge ) ~= 'table'
or cs._merge[ k ] == true
)
then
inheritKV( cd, k, v )
2010-11-03 15:53:20 +00:00
end
end
2012-10-03 07:23:18 +00:00
-- recursevely inherits all integer keyed tables
-- ( for non-verbatim tables )
2016-08-29 11:15:29 +00:00
if cs._verbatim ~= true
then
for k, v in ipairs( cs )
do
if type( v ) == 'table'
then
inherit( cd, v )
else
cd[ #cd + 1 ] = v
end
end
end
end
--
-- Helper to inherit. Inherits one key.
--
2016-08-29 11:15:29 +00:00
inheritKV =
function(
cd, -- table copy destination
k, -- key
v -- value
)
-- don't merge inheritance controls
2018-03-01 10:26:12 +00:00
if k == '_merge' or k == '_verbatim' then return end
local dtype = type( cd [ k ] )
2016-08-29 11:15:29 +00:00
if type( v ) == 'table'
then
if dtype == 'nil'
then
cd[ k ] = { }
2016-05-03 08:45:22 +00:00
inherit( cd[ k ], v, k == 'exitcodes' )
elseif
dtype == 'table' and
v._merge ~= false
then
2016-05-03 08:45:22 +00:00
inherit( cd[ k ], v, k == 'exitcodes' )
2010-11-03 11:37:25 +00:00
end
2016-08-29 11:15:29 +00:00
elseif dtype == 'nil'
then
cd[ k ] = v
2010-11-02 20:18:05 +00:00
end
end
2011-11-23 10:05:42 +00:00
2012-10-03 07:23:18 +00:00
--
2016-12-13 13:41:35 +00:00
-- Adds a new sync.
2010-11-03 11:37:25 +00:00
--
2016-12-13 13:41:35 +00:00
local function add
(
config
)
-- Checks if user overwrote the settings function.
-- ( was Lsyncd < 2.1 style )
2016-08-29 11:15:29 +00:00
if settings ~= settingsSafe
then
log(
'Error',
'Do not use settings = { ... }\n'..
' please use settings{ ... } (without the equal sign)'
)
os.exit( -1 )
end
-- Creates a new config table which inherits all keys/values
2010-11-06 10:10:00 +00:00
-- from integer keyed tables
2010-11-03 15:53:20 +00:00
local uconfig = config
2012-10-03 07:23:18 +00:00
config = { }
2011-11-23 10:05:42 +00:00
inherit( config, uconfig )
--
-- last and least defaults are inherited
--
inherit( config, default )
local inheritSettings =
{
'delay',
'maxDelays',
'maxProcesses'
}
-- Lets settings override these values.
2016-08-29 11:15:29 +00:00
for _, v in ipairs( inheritSettings )
do
if uSettings[ v ]
then
2012-10-08 07:10:03 +00:00
config[ v ] = uSettings[ v ]
end
end
-- Lets commandline override these values.
2016-08-29 11:15:29 +00:00
for _, v in ipairs( inheritSettings )
do
if clSettings[ v ]
then
2012-10-08 07:10:03 +00:00
config[ v ] = clSettings[ v ]
end
end
--
-- lets the userscript 'prepare' function
-- check and complete the config
--
2016-08-29 11:15:29 +00:00
if type( config.prepare ) == 'function'
then
-- prepare is given a writeable copy of config
2012-10-06 12:22:08 +00:00
config.prepare( config, 4 )
2012-01-27 11:08:10 +00:00
end
2010-11-11 15:17:22 +00:00
if not config[ 'source' ]
then
2012-10-03 07:23:18 +00:00
local info = debug.getinfo( 3, 'Sl' )
2012-10-03 07:23:18 +00:00
log(
'Error',
info.short_src,':',
info.currentline,': source missing from sync.'
)
2012-10-03 15:37:49 +00:00
terminate( -1 )
2010-11-02 20:18:05 +00:00
end
2011-11-23 10:05:42 +00:00
--
2010-11-02 14:11:26 +00:00
-- absolute path of source
--
2018-03-13 11:29:43 +00:00
local realsrc = core.realdir( config.source )
2012-10-03 07:23:18 +00:00
2016-08-29 11:15:29 +00:00
if not realsrc
then
log( 'Error', 'Cannot access source directory: ', config.source )
2012-10-03 15:37:49 +00:00
terminate( -1 )
2010-11-02 14:11:26 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-06 10:10:00 +00:00
config._source = config.source
2010-11-10 22:03:02 +00:00
config.source = realsrc
2010-11-02 20:18:05 +00:00
2016-12-13 13:41:35 +00:00
if not config.action
and not config.onAttrib
and not config.onCreate
and not config.onModify
and not config.onDelete
and not config.onMove
2010-11-02 20:18:05 +00:00
then
2012-10-03 07:23:18 +00:00
local info = debug.getinfo( 3, 'Sl' )
log( 'Error', info.short_src, ':', info.currentline, ': no actions specified.' )
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
terminate( -1 )
2010-11-02 20:18:05 +00:00
end
2010-11-28 10:47:57 +00:00
-- the monitor to use
2012-01-27 11:08:10 +00:00
config.monitor =
uSettings.monitor
or config.monitor
or Monitors.default( )
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
if config.monitor ~= 'inotify'
2012-10-03 07:23:18 +00:00
then
local info = debug.getinfo( 3, 'Sl' )
log(
'Error',
info.short_src, ':', info.currentline,
': event monitor "', config.monitor, '" unknown.'
2012-10-03 07:23:18 +00:00
)
2012-10-03 15:37:49 +00:00
terminate( -1 )
2010-11-28 10:47:57 +00:00
end
-- creates the new sync
2012-10-03 07:23:18 +00:00
local s = Sync.new( config )
syncList:push( s )
2010-11-30 22:56:34 +00:00
return s
2010-11-02 14:11:26 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- Allows a for-loop to walk through all syncs.
2010-11-06 18:26:59 +00:00
--
2016-12-13 13:41:35 +00:00
local function iwalk
( )
return ipairs( syncList )
2010-11-02 14:11:26 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- Tests if any sync is interested in a path.
--
2016-12-13 13:41:35 +00:00
local function concerns
2017-01-09 10:14:23 +00:00
(
path
)
for _, s in ipairs( syncList )
2016-12-13 13:41:35 +00:00
do
if s:concerns( path )
then
return true
end
end
2012-10-03 07:23:18 +00:00
return false
end
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
local intf =
{
add = add,
get = get,
getRound = getRound,
concerns = concerns,
2012-01-27 11:08:10 +00:00
iwalk = iwalk,
nextRound = nextRound
}
2016-12-05 14:11:00 +00:00
setmetatable( intf, mt )
2010-10-17 15:24:55 +00:00
return intf
end )( )
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-28 09:37:43 +00:00
-- Holds information about the event monitor capabilities
-- of the core.
--
2016-12-13 13:41:35 +00:00
Monitors = ( function
( )
2012-10-03 07:23:18 +00:00
--
2010-11-28 09:37:43 +00:00
-- The cores monitor list
2010-11-28 10:47:57 +00:00
--
2012-10-03 07:23:18 +00:00
local list = { }
2010-11-28 09:37:43 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-28 10:47:57 +00:00
-- The default event monitor.
--
2018-03-01 14:08:26 +00:00
local function default
( )
2012-10-03 07:23:18 +00:00
return list[ 1 ]
2010-11-28 10:47:57 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-28 09:37:43 +00:00
--
2012-10-03 07:23:18 +00:00
-- Initializes with info received from core
--
local function initialize( clist )
2016-12-13 13:41:35 +00:00
for k, v in ipairs( clist )
do
2012-10-03 07:23:18 +00:00
list[ k ] = v
2010-11-28 09:37:43 +00:00
end
end
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
return {
default = default,
list = list,
initialize = initialize
2010-11-28 09:37:43 +00:00
}
2012-10-03 07:23:18 +00:00
end)( )
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
-- Writes functions for the user for layer 3 configurations.
--
local functionWriter = ( function( )
2010-11-13 13:13:51 +00:00
2012-10-03 07:23:18 +00:00
--
-- All variables known to layer 3 configs.
--
2010-11-13 13:13:51 +00:00
transVars = {
2012-01-27 14:19:17 +00:00
{ '%^pathname', 'event.pathname', 1 },
{ '%^pathdir', 'event.pathdir', 1 },
{ '%^path', 'event.path', 1 },
{ '%^sourcePathname', 'event.sourcePathname', 1 },
{ '%^sourcePathdir', 'event.sourcePathdir', 1 },
{ '%^sourcePath', 'event.sourcePath', 1 },
{ '%^source', 'event.source', 1 },
{ '%^targetPathname', 'event.targetPathname', 1 },
{ '%^targetPathdir', 'event.targetPathdir', 1 },
{ '%^targetPath', 'event.targetPath', 1 },
{ '%^target', 'event.target', 1 },
{ '%^o%.pathname', 'event.pathname', 1 },
{ '%^o%.path', 'event.path', 1 },
{ '%^o%.sourcePathname', 'event.sourcePathname', 1 },
{ '%^o%.sourcePathdir', 'event.sourcePathdir', 1 },
{ '%^o%.sourcePath', 'event.sourcePath', 1 },
{ '%^o%.targetPathname', 'event.targetPathname', 1 },
{ '%^o%.targetPathdir', 'event.targetPathdir', 1 },
{ '%^o%.targetPath', 'event.targetPath', 1 },
{ '%^d%.pathname', 'event2.pathname', 2 },
{ '%^d%.path', 'event2.path', 2 },
{ '%^d%.sourcePathname', 'event2.sourcePathname', 2 },
{ '%^d%.sourcePathdir', 'event2.sourcePathdir', 2 },
{ '%^d%.sourcePath', 'event2.sourcePath', 2 },
{ '%^d%.targetPathname', 'event2.targetPathname', 2 },
{ '%^d%.targetPathdir', 'event2.targetPathdir', 2 },
{ '%^d%.targetPath', 'event2.targetPath', 2 },
2010-11-13 13:13:51 +00:00
}
2012-01-27 11:08:10 +00:00
--
2012-10-03 07:23:18 +00:00
-- Splits a user string into its arguments.
-- Returns a table of arguments
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
local function splitStr(
str -- a string where parameters are seperated by spaces.
)
local args = { }
2016-12-13 13:41:35 +00:00
while str ~= ''
do
2010-11-13 13:13:51 +00:00
-- break where argument stops
local bp = #str
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
-- in a quote
local inQuote = false
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
-- tests characters to be space and not within quotes
2016-12-13 13:41:35 +00:00
for i = 1, #str
do
2012-10-03 07:23:18 +00:00
local c = string.sub( str, i, i )
2016-12-13 13:41:35 +00:00
if c == '"'
then
2010-11-13 13:13:51 +00:00
inQuote = not inQuote
2016-12-13 13:41:35 +00:00
elseif c == ' ' and not inQuote
then
2010-11-13 13:13:51 +00:00
bp = i - 1
2016-12-13 13:41:35 +00:00
2010-11-13 13:13:51 +00:00
break
end
end
2012-10-03 07:23:18 +00:00
local arg = string.sub( str, 1, bp )
arg = string.gsub( arg, '"', '\\"' )
table.insert( args, arg )
str = string.sub( str, bp + 1, -1 )
str = string.match( str, '^%s*(.-)%s*$' )
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
return args
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
-- Translates a call to a binary to a lua function.
-- TODO this has a little too blocking.
2010-11-13 13:13:51 +00:00
--
2016-12-13 13:41:35 +00:00
local function translateBinary
(
str
)
2010-11-13 13:13:51 +00:00
-- splits the string
2012-10-03 07:23:18 +00:00
local args = splitStr( str )
2012-01-27 11:08:10 +00:00
2010-11-13 13:13:51 +00:00
-- true if there is a second event
local haveEvent2 = false
2012-01-27 11:08:10 +00:00
2018-03-01 14:08:26 +00:00
for ia, iv in ipairs( args )
do
-- a list of arguments this arg is being split into
2012-10-03 07:23:18 +00:00
local a = { { true, iv } }
2010-11-13 13:13:51 +00:00
-- goes through all translates
2018-03-01 14:08:26 +00:00
for _, v in ipairs( transVars )
do
2012-01-27 11:08:10 +00:00
local ai = 1
2018-03-01 14:08:26 +00:00
while ai <= #a
do
if a[ ai ][ 1 ]
then
2012-01-27 11:08:10 +00:00
local pre, post =
string.match( a[ ai ][ 2 ], '(.*)'..v[1]..'(.*)' )
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if pre
then
if v[3] > 1
then
haveEvent2 = true
end
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if pre ~= ''
then
2012-10-03 07:23:18 +00:00
table.insert( a, ai, { true, pre } )
2010-11-13 13:13:51 +00:00
ai = ai + 1
end
2012-10-03 07:23:18 +00:00
a[ ai ] = { false, v[ 2 ] }
2018-03-01 14:08:26 +00:00
if post ~= ''
then
2012-10-03 07:23:18 +00:00
table.insert( a, ai + 1, { true, post } )
end
2010-11-13 13:13:51 +00:00
end
end
ai = ai + 1
end
end
-- concats the argument pieces into a string.
2012-01-27 14:19:17 +00:00
local as = ''
2010-11-13 13:13:51 +00:00
local first = true
2012-10-03 07:23:18 +00:00
2018-02-27 16:14:36 +00:00
for _, v in ipairs( a )
do
if not first then as = as..' .. ' end
2012-10-03 07:23:18 +00:00
2018-02-27 16:14:36 +00:00
if v[ 1 ]
then
2012-10-03 07:23:18 +00:00
as = as .. '"' .. v[ 2 ] .. '"'
2012-01-27 11:08:10 +00:00
else
2012-10-03 07:23:18 +00:00
as = as .. v[ 2 ]
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
first = false
end
2012-10-03 07:23:18 +00:00
args[ ia ] = as
2010-11-13 13:13:51 +00:00
end
local ft
2018-02-27 16:14:36 +00:00
2016-12-13 13:41:35 +00:00
if not haveEvent2
then
ft = 'function( event )\n'
2010-11-13 13:13:51 +00:00
else
2016-12-13 13:41:35 +00:00
ft = 'function( event, event2 )\n'
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
ft = ft ..
" log('Normal', 'Event ', event.etype, \n" ..
" ' spawns action \"".. str.."\"')\n" ..
2016-12-13 13:41:35 +00:00
" spawn( event"
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
for _, v in ipairs( args )
do
2012-10-03 07:23:18 +00:00
ft = ft .. ',\n ' .. v
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
ft = ft .. ')\nend'
2010-11-13 13:13:51 +00:00
return ft
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
--
2010-11-13 13:13:51 +00:00
-- Translates a call using a shell to a lua function
--
2016-12-13 13:41:35 +00:00
local function translateShell
(
str
)
2010-11-13 13:13:51 +00:00
local argn = 1
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
local args = { }
2016-12-13 13:41:35 +00:00
2010-11-13 13:13:51 +00:00
local cmd = str
2016-12-13 13:41:35 +00:00
2010-11-13 13:44:51 +00:00
local lc = str
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
-- true if there is a second event
local haveEvent2 = false
2016-12-13 13:41:35 +00:00
for _, v in ipairs( transVars )
do
2010-11-13 13:13:51 +00:00
local occur = false
2012-10-03 07:23:18 +00:00
cmd = string.gsub(
cmd,
v[ 1 ],
2018-03-01 14:08:26 +00:00
function
( )
2010-11-13 13:44:51 +00:00
occur = true
2012-10-03 07:23:18 +00:00
return '"$' .. argn .. '"'
end
)
2018-03-01 14:08:26 +00:00
lc = string.gsub( lc, v[1], ']]..' .. v[2] .. '..[[' )
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
if occur
then
2010-11-13 13:13:51 +00:00
argn = argn + 1
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
table.insert( args, v[ 2 ] )
2016-12-13 13:41:35 +00:00
if v[ 3 ] > 1
then
2010-11-13 13:13:51 +00:00
haveEvent2 = true
end
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
local ft
2016-12-13 13:41:35 +00:00
if not haveEvent2
then
ft = 'function( event )\n'
2010-11-13 13:13:51 +00:00
else
2016-12-13 13:41:35 +00:00
ft = 'function( event, event2 )\n'
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2012-01-27 11:08:10 +00:00
-- TODO do array joining instead
2012-01-27 14:19:17 +00:00
ft = ft..
2012-01-27 14:52:42 +00:00
" log('Normal', 'Event ',event.etype,\n"..
2012-03-22 18:52:14 +00:00
" [[ spawns shell \""..lc.."\"]])\n"..
" spawnShell(event, [["..cmd.."]]"
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
for _, v in ipairs( args )
do
2012-01-27 14:19:17 +00:00
ft = ft..',\n '..v
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
ft = ft .. ')\nend'
2010-11-13 13:13:51 +00:00
return ft
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- Writes a lua function for a layer 3 user script.
--
2016-12-13 13:41:35 +00:00
local function translate
(
str
)
-- trims spaces
2012-10-03 07:23:18 +00:00
str = string.match( str, '^%s*(.-)%s*$' )
2010-11-13 13:13:51 +00:00
local ft
2018-03-01 14:08:26 +00:00
2016-12-13 13:41:35 +00:00
if string.byte( str, 1, 1 ) == 47
then
-- starts with /
2012-10-03 07:23:18 +00:00
ft = translateBinary( str )
2016-12-13 13:41:35 +00:00
elseif string.byte( str, 1, 1 ) == 94
then
-- starts with ^
2012-10-03 07:23:18 +00:00
ft = translateShell( str:sub( 2, -1 ) )
2010-11-13 13:13:51 +00:00
else
2012-10-03 07:23:18 +00:00
ft = translateShell( str )
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
log( 'FWrite', 'translated "', str, '" to \n', ft )
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
return ft
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
-- Public interface.
--
return { translate = translate }
end )( )
2010-11-13 13:13:51 +00:00
2010-10-28 17:56:33 +00:00
--
2016-12-13 13:41:35 +00:00
-- Writes a status report file at most every 'statusintervall' seconds.
2010-11-06 10:10:57 +00:00
--
2016-12-13 13:41:35 +00:00
local StatusFile = ( function
( )
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:57 +00:00
-- Timestamp when the status file has been written.
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:57 +00:00
local lastWritten = false
2012-10-03 07:23:18 +00:00
--
-- Timestamp when a status file should be written.
--
2010-11-06 10:10:57 +00:00
local alarm = false
2012-10-03 07:23:18 +00:00
--
-- Returns the alarm when the status file should be written-
2010-11-06 10:10:57 +00:00
--
2016-12-13 13:41:35 +00:00
local function getAlarm
( )
2010-11-06 10:10:57 +00:00
return alarm
end
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:57 +00:00
-- Called to check if to write a status file.
--
2016-12-13 13:41:35 +00:00
local function write
(
timestamp
)
2016-12-14 13:25:20 +00:00
log( 'Function', 'write( ', timestamp, ' )' )
2012-10-03 07:23:18 +00:00
2012-10-08 07:10:03 +00:00
--
-- takes care not to write too often
2012-10-08 07:10:03 +00:00
--
if uSettings.statusInterval > 0
then
2012-10-03 07:23:18 +00:00
-- already waiting?
if alarm and timestamp < alarm
then
2018-03-01 14:08:26 +00:00
log( 'Statusfile', 'waiting(', timestamp, ' < ', alarm, ')' )
2010-11-06 10:10:57 +00:00
return
end
2012-10-03 07:23:18 +00:00
2010-11-06 10:33:26 +00:00
-- determines when a next write will be possible
if not alarm
then
2018-03-01 14:08:26 +00:00
local nextWrite = lastWritten and timestamp + uSettings.statusInterval
2012-10-03 07:23:18 +00:00
if nextWrite and timestamp < nextWrite
then
2018-03-01 14:08:26 +00:00
log( 'Statusfile', 'setting alarm: ', nextWrite )
2010-11-06 10:10:57 +00:00
alarm = nextWrite
2012-10-03 07:23:18 +00:00
2010-11-06 10:10:57 +00:00
return
end
end
2012-10-03 07:23:18 +00:00
2010-11-29 20:32:54 +00:00
lastWritten = timestamp
2010-11-06 10:10:57 +00:00
alarm = false
end
2012-10-03 07:23:18 +00:00
log( 'Statusfile', 'writing now' )
2012-10-08 07:10:03 +00:00
local f, err = io.open( uSettings.statusFile, 'w' )
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
if not f
then
2012-10-03 07:23:18 +00:00
log(
'Error',
'Cannot open status file "' ..
2012-10-08 07:10:03 +00:00
uSettings.statusFile ..
2012-10-03 07:23:18 +00:00
'" :' ..
err
)
2010-11-06 10:10:57 +00:00
return
end
2012-10-03 07:23:18 +00:00
f:write( 'Lsyncd status report at ', os.date( ), '\n\n' )
2016-12-13 13:41:35 +00:00
for i, s in Syncs.iwalk( )
do
2012-10-03 07:23:18 +00:00
s:statusReport( f )
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
f:write( '\n' )
2010-11-12 10:07:58 +00:00
end
2012-01-27 11:08:10 +00:00
2012-10-03 07:23:18 +00:00
Inotify.statusReport( f )
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
f:close( )
2010-11-05 18:04:29 +00:00
end
2010-11-06 10:10:57 +00:00
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
return { write = write, getAlarm = getAlarm }
2012-10-03 07:23:18 +00:00
end )( )
2010-11-30 23:14:17 +00:00
--
2012-10-03 07:23:18 +00:00
-- Lets userscripts make their own alarms.
--
2016-12-13 13:41:35 +00:00
local UserAlarms = ( function
( )
2012-10-03 07:23:18 +00:00
local alarms = { }
--
-- Calls the user function at timestamp.
2010-11-30 23:14:17 +00:00
--
2016-12-13 13:41:35 +00:00
local function alarm
(
timestamp,
func,
extra
)
2012-01-27 11:08:10 +00:00
local idx
2016-11-25 13:55:59 +00:00
for k, v in ipairs( alarms )
do
if timestamp < v.timestamp
then
2010-11-30 23:14:17 +00:00
idx = k
2016-11-25 13:55:59 +00:00
2010-11-30 23:14:17 +00:00
break
end
end
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
local a =
{
2012-10-03 07:23:18 +00:00
timestamp = timestamp,
func = func,
extra = extra
}
2016-11-25 13:55:59 +00:00
if idx
then
2012-10-03 07:23:18 +00:00
table.insert( alarms, idx, a )
2010-11-30 23:14:17 +00:00
else
2012-10-03 07:23:18 +00:00
table.insert( alarms, a )
2010-11-30 23:14:17 +00:00
end
end
2012-10-03 07:23:18 +00:00
2010-11-30 23:14:17 +00:00
--
2012-10-03 07:23:18 +00:00
-- Retrieves the soonest alarm.
--
2016-12-13 13:41:35 +00:00
local function getAlarm
( )
2016-11-25 13:55:59 +00:00
if #alarms == 0
then
2012-01-27 11:08:10 +00:00
return false
2010-11-30 23:14:17 +00:00
else
return alarms[1].timestamp
end
end
2012-10-03 07:23:18 +00:00
--
-- Calls user alarms.
2010-11-30 23:14:17 +00:00
--
2018-03-01 14:08:26 +00:00
local function invoke
(
timestamp
)
while #alarms > 0
and alarms[ 1 ].timestamp <= timestamp
2012-10-03 07:23:18 +00:00
do
alarms[ 1 ].func( alarms[ 1 ].timestamp, alarms[ 1 ].extra )
table.remove( alarms, 1 )
2010-11-30 23:14:17 +00:00
end
end
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
return {
alarm = alarm,
getAlarm = getAlarm,
invoke = invoke
}
end )( )
2010-11-30 23:14:17 +00:00
2010-11-10 15:57:37 +00:00
--============================================================================
2012-10-03 07:23:18 +00:00
-- Lsyncd runner's plugs. These functions are called from core.
2010-11-10 15:57:37 +00:00
--============================================================================
2012-10-03 07:23:18 +00:00
--
-- Current status of Lsyncd.
2010-11-10 15:57:37 +00:00
--
2012-01-30 14:01:18 +00:00
-- 'init' ... on (re)init
-- 'run' ... normal operation
-- 'fade' ... waits for remaining processes
2010-11-11 19:52:20 +00:00
--
2012-01-30 14:01:18 +00:00
local lsyncdStatus = 'init'
2010-11-10 15:57:37 +00:00
2010-11-29 20:32:54 +00:00
--
2012-10-03 07:23:18 +00:00
-- The cores interface to the runner.
--
runner = { }
2018-03-13 11:29:43 +00:00
2010-11-10 15:57:37 +00:00
--
-- Last time said to be waiting for more child processes
--
local lastReportedWaiting = false
2012-10-03 07:23:18 +00:00
--
-- Called from core whenever Lua code failed.
--
2010-11-10 15:57:37 +00:00
-- Logs a backtrace
--
2016-12-14 13:25:20 +00:00
function runner.callError
(
message
)
2018-03-01 14:08:26 +00:00
log( 'Error', 'in Lua: ', message )
2012-10-03 07:23:18 +00:00
2010-11-10 15:57:37 +00:00
-- prints backtrace
local level = 2
2012-10-03 07:23:18 +00:00
2016-11-25 13:55:59 +00:00
while true
do
2012-10-03 07:23:18 +00:00
local info = debug.getinfo( level, 'Sl' )
if not info then terminate( -1 ) end
2012-10-03 07:23:18 +00:00
log(
'Error',
'Backtrace ',
level - 1, ' :',
info.short_src, ':',
info.currentline
)
2010-11-10 15:57:37 +00:00
level = level + 1
end
end
2012-10-03 07:23:18 +00:00
2018-03-14 08:57:49 +00:00
-- Registers the mantle with the core
core.mantle( runner )
2012-10-03 07:23:18 +00:00
--
-- Called from core whenever a child process has finished and
-- the zombie process was collected by core.
2010-11-10 15:57:37 +00:00
--
2018-03-01 14:08:26 +00:00
function runner.collectProcess
(
pid, -- process id
exitcode -- exitcode
)
processCount = processCount - 1
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if processCount < 0
then
2012-10-03 07:23:18 +00:00
error( 'negative number of processes!' )
end
2018-03-01 14:08:26 +00:00
for _, s in Syncs.iwalk( )
do
if s:collect( pid, exitcode ) then return end
2010-11-10 15:57:37 +00:00
end
end
2012-10-03 07:23:18 +00:00
--
2010-11-05 18:04:29 +00:00
-- Called from core everytime a masterloop cycle runs through.
2012-10-03 07:23:18 +00:00
--
2012-01-27 11:08:10 +00:00
-- This happens in case of
2010-11-05 18:04:29 +00:00
-- * an expired alarm.
-- * a returned child process.
-- * received filesystem events.
-- * received a HUP, TERM or INT signal.
2010-10-23 12:36:55 +00:00
--
2012-10-03 07:23:18 +00:00
function runner.cycle(
timestamp -- the current kernel time (in jiffies)
)
2016-12-14 13:25:20 +00:00
log( 'Function', 'cycle( ', timestamp, ' )' )
2016-11-25 13:55:59 +00:00
if lsyncdStatus == 'fade'
then
if processCount > 0
then
2018-03-14 08:57:49 +00:00
if lastReportedWaiting == false
or timestamp >= lastReportedWaiting + 60
then
lastReportedWaiting = timestamp
2018-03-14 08:57:49 +00:00
log( 'Normal', 'waiting for ', processCount, ' more child processes.' )
end
2010-11-14 09:11:09 +00:00
return true
else
return false
end
end
2012-10-03 07:23:18 +00:00
2016-12-14 13:25:20 +00:00
if lsyncdStatus ~= 'run'
then
2012-10-03 07:23:18 +00:00
error( 'runner.cycle() called while not running!' )
2010-11-14 09:11:09 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- goes through all syncs and spawns more actions
-- if possibly. But only let Syncs invoke actions if
-- not at global limit
--
2016-12-14 13:25:20 +00:00
if not uSettings.maxProcesses
or processCount < uSettings.maxProcesses
2012-10-03 07:23:18 +00:00
then
local start = Syncs.getRound( )
local ir = start
2012-10-03 07:23:18 +00:00
repeat
2012-10-03 07:23:18 +00:00
local s = Syncs.get( ir )
2016-12-14 13:25:20 +00:00
2012-10-03 07:23:18 +00:00
s:invokeActions( timestamp )
2016-12-14 13:25:20 +00:00
ir = ir + 1
2012-10-03 07:23:18 +00:00
if ir >= #Syncs then ir = 0 end
until ir == start
2012-10-03 07:23:18 +00:00
Syncs.nextRound( )
2010-11-08 12:14:10 +00:00
end
2010-11-30 23:14:17 +00:00
2012-10-03 07:23:18 +00:00
UserAlarms.invoke( timestamp )
2010-11-30 23:14:17 +00:00
2016-11-25 13:55:59 +00:00
if uSettings.statusFile
then
2012-10-03 07:23:18 +00:00
StatusFile.write( timestamp )
2010-11-05 18:04:29 +00:00
end
2010-11-14 09:11:09 +00:00
return true
2010-10-22 23:14:11 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- Called by core if '-help' or '--help' is in
2010-10-27 09:06:13 +00:00
-- the arguments.
--
2012-10-03 07:23:18 +00:00
function runner.help( )
2010-10-27 19:34:56 +00:00
io.stdout:write(
2010-11-04 13:43:57 +00:00
[[
2010-11-13 21:50:21 +00:00
2012-01-27 11:08:10 +00:00
USAGE:
2018-03-12 12:36:34 +00:00
lsyncd [OPTIONS] [CONFIG-FILE]
2010-11-04 13:43:57 +00:00
OPTIONS:
-delay SECS Overrides default delay times
2010-11-04 13:43:57 +00:00
-help Shows this
2010-11-13 21:50:21 +00:00
-log all Logs everything (debug)
2010-11-04 13:43:57 +00:00
-log scarce Logs errors only
2010-11-04 14:23:34 +00:00
-log [Category] Turns on logging for a debug category
2010-11-13 21:50:21 +00:00
-logfile FILE Writes log to FILE (DEFAULT: uses syslog)
-version Prints versions and exits
2010-11-04 13:43:57 +00:00
LICENSE:
GPLv2 or any later version.
SEE:
2018-03-12 12:36:34 +00:00
`man lsyncd` or visit https://axkibe.github.io/lsyncd/ for further information.
2010-10-27 09:06:13 +00:00
]])
2010-11-28 10:47:57 +00:00
2012-10-03 15:37:49 +00:00
os.exit( -1 )
2010-10-27 09:06:13 +00:00
end
2012-10-03 07:23:18 +00:00
--
2010-11-03 21:02:14 +00:00
-- Called from core to parse the command line arguments
--
2012-10-03 07:23:18 +00:00
-- returns a string as user script to load.
-- or simply 'true' if running with rsync bevaiour
--
-- terminates on invalid arguments.
--
function runner.configure( args, monitors )
2010-11-28 09:37:43 +00:00
Monitors.initialize( monitors )
2012-10-03 07:23:18 +00:00
2012-10-08 07:10:03 +00:00
--
2012-10-03 07:23:18 +00:00
-- a list of all valid options
--
-- first paramter is the number of parameters an option takes
2012-10-03 15:37:49 +00:00
-- if < 0 the called function has to check the presence of
-- optional arguments.
2012-10-03 07:23:18 +00:00
--
-- second paramter is the function to call
2010-11-28 09:37:43 +00:00
--
2018-03-01 14:08:26 +00:00
local options =
{
2010-11-03 21:02:14 +00:00
-- log is handled by core already.
2012-10-03 15:37:49 +00:00
2012-10-03 07:23:18 +00:00
delay =
2018-03-01 14:08:26 +00:00
{
1,
2018-03-01 14:19:30 +00:00
function
(
secs
)
2018-03-01 14:08:26 +00:00
clSettings.delay = secs + 0
end
},
2012-10-03 07:23:18 +00:00
2018-03-12 12:36:34 +00:00
log = { 1, nil },
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
logfile =
2018-03-01 14:08:26 +00:00
{
1,
2018-03-01 14:19:30 +00:00
function
(
file
)
2018-03-01 14:08:26 +00:00
clSettings.logfile = file
end
},
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
version =
2018-03-01 14:08:26 +00:00
{
0,
2018-03-01 14:19:30 +00:00
function
( )
2018-03-01 14:08:26 +00:00
io.stdout:write( 'Version: ', lsyncd_version, '\n' )
2018-03-01 14:19:30 +00:00
2018-03-01 14:08:26 +00:00
os.exit( 0 )
end
}
2010-11-03 21:02:14 +00:00
}
2012-10-03 07:23:18 +00:00
-- non-opts is filled with all args that were no part dash options
local nonopts = { }
2012-10-03 15:37:49 +00:00
local i = 1
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
while i <= #args
do
2012-10-03 15:37:49 +00:00
local a = args[ i ]
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if a:sub( 1, 1 ) ~= '-'
then
2012-10-03 07:23:18 +00:00
table.insert( nonopts, args[ i ] )
2010-11-03 21:02:14 +00:00
else
2018-03-01 14:08:26 +00:00
if a:sub( 1, 2 ) == '--'
then
2012-10-03 07:23:18 +00:00
a = a:sub( 3 )
2010-11-03 21:02:14 +00:00
else
2012-10-03 07:23:18 +00:00
a = a:sub( 2 )
2010-11-03 21:02:14 +00:00
end
2012-10-03 07:23:18 +00:00
local o = options[ a ]
if not o
then
2018-03-01 14:08:26 +00:00
log( 'Error', 'unknown option command line option ', args[ i ] )
2012-10-03 15:37:49 +00:00
os.exit( -1 )
2010-11-03 21:02:14 +00:00
end
2012-10-03 07:23:18 +00:00
if o[ 1 ] >= 0 and i + o[ 1 ] > #args
then
2012-10-03 15:37:49 +00:00
log( 'Error', a ,' needs ', o[ 1 ],' arguments' )
2012-10-03 15:37:49 +00:00
os.exit( -1 )
elseif o[1] < 0
then
2012-10-03 15:37:49 +00:00
o[ 1 ] = -o[ 1 ]
end
if o[ 2 ]
then
if o[ 1 ] == 0
then
2012-10-03 15:37:49 +00:00
o[ 2 ]( )
elseif o[ 1 ] == 1
then
o[ 2 ]( args[ i + 1] )
elseif o[ 1 ] == 2
then
o[ 2 ]( args[ i + 1], args[ i + 2] )
elseif o[ 1 ] == 3
then
o[ 2 ]( args[ i + 1], args[ i + 2], args[ i + 3] )
end
end
i = i + o[1]
2010-11-03 21:02:14 +00:00
end
2012-10-03 15:37:49 +00:00
i = i + 1
2010-11-03 21:02:14 +00:00
end
2018-03-12 12:36:34 +00:00
if #nonopts == 0
then
2018-03-12 12:36:34 +00:00
runner.help( args[ 0 ] )
elseif #nonopts == 1
then
return nonopts[ 1 ]
2010-11-13 20:47:45 +00:00
else
2018-03-12 12:36:34 +00:00
-- TODO make this possible
log( 'Error', 'There can only be one config file in the command line.' )
2012-10-03 15:37:49 +00:00
2018-03-12 12:36:34 +00:00
os.exit( -1 )
2010-11-03 21:02:14 +00:00
end
end
2012-10-03 15:37:49 +00:00
--
2010-10-17 15:24:55 +00:00
-- Called from core on init or restart after user configuration.
2011-08-29 09:21:40 +00:00
--
2012-10-03 15:37:49 +00:00
-- firstTime:
-- true when Lsyncd startups the first time,
-- false on resets, due to HUP signal or monitor queue overflow.
2012-01-27 11:08:10 +00:00
--
2012-10-03 15:37:49 +00:00
function runner.initialize( firstTime )
-- Checks if user overwrote the settings function.
-- ( was Lsyncd <2.1 style )
if settings ~= settingsSafe
then
2012-10-08 07:10:03 +00:00
log(
'Error',
'Do not use settings = { ... }\n'..
2018-03-01 14:08:26 +00:00
' please use settings{ ... } ( without the equal sign )'
2012-10-08 07:10:03 +00:00
)
os.exit( -1 )
2012-10-08 07:10:03 +00:00
end
2012-01-27 11:08:10 +00:00
lastReportedWaiting = false
--
2010-10-25 14:55:40 +00:00
-- From this point on, no globals may be created anymore
--
lockGlobals( )
2010-10-25 14:55:40 +00:00
--
2010-11-17 11:14:36 +00:00
-- all command line settings overwrite config file settings
--
for k, v in pairs( clSettings )
do
if k ~= 'syncs'
then
2012-10-08 07:10:03 +00:00
uSettings[ k ] = v
2010-11-13 21:50:21 +00:00
end
end
2012-02-15 15:47:18 +00:00
if uSettings.logfile
then
2018-03-13 11:29:43 +00:00
core.configure( 'logfile', uSettings.logfile )
2010-11-13 21:50:21 +00:00
end
2012-10-03 15:37:49 +00:00
if uSettings.logident
then
2018-03-13 11:29:43 +00:00
core.configure( 'logident', uSettings.logident )
end
2012-10-03 15:37:49 +00:00
if uSettings.logfacility
then
2018-03-13 11:29:43 +00:00
core.configure( 'logfacility', uSettings.logfacility )
end
2012-10-03 15:37:49 +00:00
--
2012-10-08 07:10:03 +00:00
-- Transfers some defaults to uSettings
2012-10-03 15:37:49 +00:00
--
if uSettings.statusInterval == nil
then
2012-10-08 07:10:03 +00:00
uSettings.statusInterval = default.statusInterval
2010-10-27 09:06:13 +00:00
end
2010-10-25 21:41:45 +00:00
2012-01-27 11:08:10 +00:00
-- makes sure the user gave Lsyncd anything to do
if #Syncs == 0
then
log( 'Error', 'Nothing to watch!' )
2012-10-03 15:37:49 +00:00
os.exit( -1 )
2010-10-21 12:37:27 +00:00
end
2010-11-03 11:37:25 +00:00
-- from now on use logging as configured instead of stdout/err.
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'run';
2018-03-13 11:29:43 +00:00
core.configure( 'running' );
2012-01-27 11:08:10 +00:00
2018-03-01 14:08:26 +00:00
local ufuncs =
{
2012-10-03 15:37:49 +00:00
'onAttrib',
'onCreate',
'onDelete',
'onModify',
'onMove',
'onStartup',
2010-11-13 13:44:51 +00:00
}
2012-01-27 11:08:10 +00:00
2010-11-13 13:44:51 +00:00
-- translates layer 3 scripts
for _, s in Syncs.iwalk()
do
2010-11-13 13:44:51 +00:00
-- checks if any user functions is a layer 3 string.
local config = s.config
for _, fn in ipairs( ufuncs )
do
if type(config[fn]) == 'string'
then
local ft = functionWriter.translate( config[ fn ] )
2018-03-09 09:42:10 +00:00
config[ fn ] = assert( load( 'return '..ft ) )( )
2010-11-13 13:44:51 +00:00
end
end
end
2010-11-07 09:53:39 +00:00
2010-11-17 18:52:55 +00:00
-- runs through the Syncs created by users
for _, s in Syncs.iwalk( )
do
if s.config.monitor == 'inotify'
then
2012-10-03 15:37:49 +00:00
Inotify.addSync( s, s.source )
2010-11-28 10:47:57 +00:00
else
error( 'sync '.. s.config.name..' has unknown event monitor interface.' )
2010-11-28 10:47:57 +00:00
end
2012-10-03 15:37:49 +00:00
-- if the sync has an init function, the init delay
-- is stacked which causes the init function to be called.
if s.config.init
then
2012-10-03 15:37:49 +00:00
s:addInitDelay( )
2010-10-22 10:35:26 +00:00
end
end
2010-10-17 15:24:55 +00:00
end
2012-10-03 15:37:49 +00:00
--
-- Called by core to query the soonest alarm.
2010-10-19 10:12:11 +00:00
--
2016-11-25 13:55:59 +00:00
-- @return false ... no alarm, core can go in untimed sleep
2010-11-08 12:14:10 +00:00
-- true ... immediate action
2010-11-05 18:20:33 +00:00
-- times ... the alarm time (only read if number is 1)
--
2016-12-14 13:25:20 +00:00
function runner.getAlarm
( )
log( 'Function', 'getAlarm( )' )
2012-10-03 15:37:49 +00:00
2018-03-01 14:08:26 +00:00
if lsyncdStatus ~= 'run' then return false end
2012-10-03 15:37:49 +00:00
2010-11-05 18:20:33 +00:00
local alarm = false
2012-10-03 15:37:49 +00:00
--
-- Checks if 'a' is sooner than the 'alarm' up-value.
2010-11-06 21:29:22 +00:00
--
2016-12-14 13:25:20 +00:00
local function checkAlarm
(
2018-03-01 14:08:26 +00:00
a -- alarm time
2016-12-14 13:25:20 +00:00
)
2018-03-01 14:08:26 +00:00
if a == nil then error( 'got nil alarm' ) end
2012-10-03 15:37:49 +00:00
2016-11-25 13:55:59 +00:00
if alarm == true or not a
then
2012-10-03 15:37:49 +00:00
-- 'alarm' is already immediate or
-- a not a new alarm
2010-12-01 13:25:05 +00:00
return
2010-11-06 21:29:22 +00:00
end
2012-10-03 15:37:49 +00:00
-- sets 'alarm' to a if a is sooner
2016-11-25 13:55:59 +00:00
if not alarm or a < alarm
then
2010-12-01 13:25:05 +00:00
alarm = a
2010-10-24 16:41:58 +00:00
end
end
2010-11-06 21:29:22 +00:00
2012-10-03 15:37:49 +00:00
--
-- checks all syncs for their earliest alarm,
-- but only if the global process limit is not yet reached.
2012-10-03 15:37:49 +00:00
--
2016-12-14 13:25:20 +00:00
if not uSettings.maxProcesses
or processCount < uSettings.maxProcesses
2012-10-03 15:37:49 +00:00
then
2016-12-14 13:25:20 +00:00
for _, s in Syncs.iwalk( )
do
checkAlarm( s:getAlarm( ) )
end
else
2012-10-03 15:37:49 +00:00
log(
'Alarm',
'at global process limit.'
)
2010-11-06 10:10:57 +00:00
end
2010-11-06 21:29:22 +00:00
-- checks if a statusfile write has been delayed
2012-10-03 15:37:49 +00:00
checkAlarm( StatusFile.getAlarm( ) )
2010-11-30 23:14:17 +00:00
-- checks for an userAlarm
2012-10-03 15:37:49 +00:00
checkAlarm( UserAlarms.getAlarm( ) )
2018-03-01 14:08:26 +00:00
log( 'Alarm', 'runner.getAlarm returns: ', alarm )
2010-11-06 10:10:57 +00:00
2010-11-05 18:20:33 +00:00
return alarm
2010-10-19 10:12:11 +00:00
end
2010-10-17 15:24:55 +00:00
2010-11-10 15:57:37 +00:00
2012-10-03 15:37:49 +00:00
--
-- Called when an file system monitor events arrive
2010-11-26 16:19:56 +00:00
--
runner.inotifyEvent = Inotify.event
2010-10-21 12:37:27 +00:00
--
2012-10-03 15:37:49 +00:00
-- Collector for every child process that finished in startup phase
2010-10-21 12:37:27 +00:00
--
2017-01-09 12:13:05 +00:00
function runner.collector
(
2012-10-03 15:37:49 +00:00
pid, -- pid of the child process
exitcode -- exitcode of the child process
)
if exitcode ~= 0
then
2018-03-01 14:08:26 +00:00
log( 'Error', 'Startup process', pid, ' failed' )
2012-10-03 15:37:49 +00:00
terminate( -1 )
2010-10-20 13:01:26 +00:00
end
2012-10-03 15:37:49 +00:00
2010-10-21 12:37:27 +00:00
return 0
2010-10-20 10:25:34 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-10 15:57:37 +00:00
-- Called by core when an overflow happened.
--
2018-03-01 14:08:26 +00:00
function runner.overflow
( )
log( 'Normal', '--- OVERFLOW in event queue ---' )
2012-10-03 15:37:49 +00:00
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'fade'
2010-11-14 09:11:09 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-14 09:11:09 +00:00
-- Called by core on a hup signal.
--
2018-03-01 14:08:26 +00:00
function runner.hup
( )
log( 'Normal', '--- HUP signal, resetting ---' )
2012-10-03 15:37:49 +00:00
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'fade'
2010-11-14 09:11:09 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-14 09:11:09 +00:00
-- Called by core on a term signal.
--
2018-03-01 14:08:26 +00:00
function runner.term
(
sigcode -- signal code
)
local sigtexts =
{
[ 2 ] = 'INT',
[ 15 ] = 'TERM'
};
local sigtext = sigtexts[ sigcode ];
2018-03-01 14:08:26 +00:00
if not sigtext then sigtext = 'UNKNOWN' end
2012-10-03 15:37:49 +00:00
2018-03-01 14:08:26 +00:00
log( 'Normal', '--- ', sigtext, ' signal, fading ---' )
2012-10-03 15:37:49 +00:00
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'fade'
2012-10-03 15:37:49 +00:00
2010-11-10 15:57:37 +00:00
end
2010-10-21 12:37:27 +00:00
2010-10-25 17:38:57 +00:00
--============================================================================
2012-10-03 15:37:49 +00:00
-- Lsyncd runner's user interface
2010-10-25 17:38:57 +00:00
--============================================================================
2010-10-17 15:24:55 +00:00
2012-10-03 15:37:49 +00:00
--
2010-11-05 15:18:01 +00:00
-- Main utility to create new observations.
--
2012-10-03 15:37:49 +00:00
-- Returns an Inlet to that sync.
--
2018-03-01 10:26:12 +00:00
function sync
(
opts
)
if lsyncdStatus ~= 'init'
then
error( 'Sync can only be created during initialization.', 2 )
2010-11-05 15:18:01 +00:00
end
2012-10-03 15:37:49 +00:00
return Syncs.add( opts ).inlet
2010-11-05 15:18:01 +00:00
end
2010-10-17 15:24:55 +00:00
2010-10-19 16:40:49 +00:00
2010-10-27 11:31:18 +00:00
--
2012-10-03 15:37:49 +00:00
-- Spawns a new child process.
2010-11-07 01:06:08 +00:00
--
function spawn
(
2012-10-03 15:37:49 +00:00
agent, -- the reason why a process is spawned.
-- a delay or delay list for a sync
-- it will mark the related files as blocked.
binary, -- binary to call
... -- arguments
)
2018-03-01 14:08:26 +00:00
if agent == nil
or type( agent ) ~= 'table'
2012-10-03 15:37:49 +00:00
then
2018-03-01 14:08:26 +00:00
error( 'spawning with an invalid agent', 2 )
2010-11-11 15:17:22 +00:00
end
2018-03-01 14:08:26 +00:00
if lsyncdStatus == 'fade'
then
log( 'Normal', 'ignored process spawning while fading' )
2012-01-30 14:01:18 +00:00
return
2010-11-14 09:11:09 +00:00
end
2018-03-01 14:08:26 +00:00
if type( binary ) ~= 'string'
then
error( 'calling spawn(agent, binary, ...): binary is not a string', 2 )
end
2012-10-03 15:37:49 +00:00
local dol = InletFactory.getDelayOrList( agent )
2018-03-01 14:08:26 +00:00
if not dol
then
error( 'spawning with an unknown agent', 2 )
2012-10-03 15:37:49 +00:00
end
--
-- checks if a spawn is called on an already active event
--
if dol.status
then
2012-10-03 15:37:49 +00:00
-- is an event
if dol.status ~= 'wait'
then
error( 'spawn() called on an non-waiting event', 2 )
end
2012-10-03 15:37:49 +00:00
else
-- is a list
for _, d in ipairs( dol )
do
if d.status ~= 'wait'
and d.status ~= 'block'
then
error( 'spawn() called on an non-waiting event list', 2 )
end
end
end
2012-10-03 15:37:49 +00:00
--
-- tries to spawn the process
--
2018-03-13 11:29:43 +00:00
local pid = core.exec( binary, ... )
if pid and pid > 0
then
processCount = processCount + 1
2018-03-01 14:08:26 +00:00
if uSettings.maxProcesses
and processCount > uSettings.maxProcesses
2012-10-03 15:37:49 +00:00
then
error( 'Spawned too much processes!' )
end
2012-10-03 15:37:49 +00:00
local sync = InletFactory.getSync( agent )
2010-11-30 22:56:34 +00:00
-- delay or list
if dol.status
then
2010-11-30 22:56:34 +00:00
-- is a delay
dol:setActive( )
2012-10-03 15:37:49 +00:00
sync.processes[ pid ] = dol
2012-02-15 15:47:18 +00:00
else
2010-11-30 22:56:34 +00:00
-- is a list
for _, d in ipairs( dol )
do
d:setActive( )
2010-11-12 18:52:43 +00:00
end
2012-10-03 15:37:49 +00:00
sync.processes[ pid ] = dol
2010-11-12 18:52:43 +00:00
end
2010-11-07 01:06:08 +00:00
end
2010-11-05 13:34:02 +00:00
end
2012-10-03 15:37:49 +00:00
--
2011-03-21 08:33:37 +00:00
-- Spawns a child process using the default shell.
2010-11-07 01:06:08 +00:00
--
function spawnShell
(
2012-10-03 15:37:49 +00:00
agent, -- the delay(list) to spawn the command for
command, -- the shell command
... -- additonal arguments
)
2018-03-01 14:08:26 +00:00
return spawn( agent, '/bin/sh', '-c', command, '/bin/sh', ... )
2010-10-27 11:31:18 +00:00
end
--
-- Observes a filedescriptor.
2010-12-01 12:19:17 +00:00
--
function observefd
(
2012-10-03 15:37:49 +00:00
fd, -- file descriptor
ready, -- called when fd is ready to be read
writey -- called when fd is ready to be written
)
2018-03-13 11:29:43 +00:00
return core.observe_fd( fd, ready, writey )
2010-12-01 12:19:17 +00:00
end
2010-12-01 12:19:17 +00:00
--
-- Stops observeing a filedescriptor.
2012-10-03 15:37:49 +00:00
--
function nonobservefd
(
2012-10-03 15:37:49 +00:00
fd -- file descriptor
)
2018-03-13 11:29:43 +00:00
return core.nonobserve_fd( fd )
2010-12-01 12:19:17 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-30 23:14:17 +00:00
-- Calls func at timestamp.
2012-10-03 15:37:49 +00:00
--
2010-11-30 23:14:17 +00:00
-- Use now() to receive current timestamp
2012-10-03 15:37:49 +00:00
-- add seconds with '+' to it
2010-11-30 23:14:17 +00:00
--
alarm = UserAlarms.alarm
2012-10-03 15:37:49 +00:00
--
2010-11-10 11:23:26 +00:00
-- Comfort routine also for user.
-- Returns true if 'String' starts with 'Start'
--
2016-12-13 13:41:35 +00:00
function string.starts
(
String,
Start
)
2012-10-03 15:37:49 +00:00
return string.sub( String, 1, #Start )==Start
2010-11-10 11:23:26 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-10 11:23:26 +00:00
-- Comfort routine also for user.
-- Returns true if 'String' ends with 'End'
--
2016-12-13 13:41:35 +00:00
function string.ends
(
String,
End
)
2012-10-03 15:37:49 +00:00
return End == '' or string.sub( String, -#End ) == End
2010-11-10 11:23:26 +00:00
end
--
2016-12-13 13:41:35 +00:00
-- The settings call
2012-10-03 15:37:49 +00:00
--
2016-12-13 13:41:35 +00:00
function settings
(
a1 -- a string for getting a setting
-- or a table of key/value pairs to set these settings
)
-- if a1 is a string this is a get operation
if type( a1 ) == 'string'
then
return uSettings[ a1 ]
end
-- if its a table it sets all the value of the bale
for k, v in pairs( a1 )
do
if type( k ) ~= 'number'
then
if not settingsCheckgauge[ k ]
then
2018-03-01 14:08:26 +00:00
error( 'setting "'..k..'" unknown.', 2 )
end
2012-10-08 07:10:03 +00:00
uSettings[ k ] = v
else
if not settingsCheckgauge[ v ]
then
2018-03-01 14:08:26 +00:00
error( 'setting "'..v..'" unknown.', 2 )
end
2012-10-08 07:10:03 +00:00
uSettings[ v ] = true
end
end
end
settingsSafe = settings