From d72a59e70fda62279451482f782f19ed7f470216 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Tue, 27 Feb 2018 10:09:28 +0100 Subject: [PATCH 1/7] do not append a '/' to targets when there it ends with a ':' --- ChangeLog | 18 ++++++++----- default-rsync.lua | 6 +++-- default-rsyncssh.lua | 50 +++++++++++------------------------- lsyncd.h | 2 +- lsyncd.lua | 61 ++++++++++++++------------------------------ 5 files changed, 50 insertions(+), 87 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0de566b..fa1adec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,11 +1,15 @@ +2018-??-??: 2.2.3 + change: if the target/targetdir ands with a ':' do not append + a trailing '/' to it, since that would change it from homedir to rootdir! + 2017-02-16: 2.2.2 - fix: checkgauge 'insist' - fix: no partial path exlusion tests - fix: write pid of forked process in pidfile - fix: crash on not reachable target - workaround: - changed back to filter style rsync calling - until https://bugzilla.samba.org/show_bug.cgi?id=12569 + fix: checkgauge 'insist' + fix: no partial path exlusion tests + fix: write pid of forked process in pidfile + fix: crash on not reachable target + workaround: + changed back to filter style rsync calling + until https://bugzilla.samba.org/show_bug.cgi?id=12569 is fixed and released. 2017-01-05: 2.2.1 diff --git a/default-rsync.lua b/default-rsync.lua index 4c1b4a8..7222faf 100644 --- a/default-rsync.lua +++ b/default-rsync.lua @@ -647,11 +647,13 @@ rsync.prepare = function end -- appends a / to target if not present - if not skipTarget and string.sub(config.target, -1) ~= '/' + -- and not a ':' for home dir. + if not skipTarget + and string.sub( config.target, -1 ) ~= '/' + and string.sub( config.target, -1 ) ~= ':' then config.target = config.target..'/' end - end diff --git a/default-rsyncssh.lua b/default-rsyncssh.lua index 750e3cb..a82fec6 100644 --- a/default-rsyncssh.lua +++ b/default-rsyncssh.lua @@ -91,10 +91,7 @@ local replaceRsyncFilter = ( path ) - if not path - then - return - end + if not path then return end return( path @@ -161,9 +158,7 @@ rsyncssh.action = function -- Replaces what rsync would consider filter rules by literals -- local function sub( p ) - if not p then - return - end + if not p then return end return p: gsub( '%?', '\\?' ): @@ -195,11 +190,7 @@ rsyncssh.action = function -- adds one path to the filter local function addToFilter( path ) - - if filterP[ path ] - then - return - end + if filterP[ path ] then return end filterP[ path ] = true @@ -372,7 +363,7 @@ rsyncssh.collect = function if rc == 'ok' then - log('Normal', 'Startup of "', agent.source, '" finished: ', exitcode) + log( 'Normal', 'Startup of "', agent.source, '" finished: ', exitcode ) elseif rc == 'again' then if settings('insist') @@ -382,16 +373,16 @@ rsyncssh.collect = function log( 'Error', 'Temporary or permanent failure on startup of "', - agent.source, '". Terminating since "insist" is not set.' + agent.source, '". Terminating since "insist" is not set.' ) terminate( -1 ) -- ERRNO end elseif rc == 'die' then - log( 'Error', 'Failure on startup of "',agent.source,'": ', exitcode ) + log( 'Error', 'Failure on startup of "', agent.source, '": ', exitcode ) else - log( 'Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode ) + log( 'Error', 'Unknown exitcode on startup of "', agent.source, ': "', exitcode ) rc = 'die' end @@ -431,7 +422,7 @@ rsyncssh.collect = function then log( 'Normal', 'Failure ', agent.etype, ' ', agent.sourcePath, ': ', exitcode ) else - log( 'Error', 'Unknown exitcode ',agent.etype,' ',agent.sourcePath,': ',exitcode ) + log( 'Error', 'Unknown exitcode ', agent.etype,' ', agent.sourcePath,': ', exitcode ) rc = 'die' end @@ -453,18 +444,12 @@ rsyncssh.prepare = function if not config.host then - error( - 'default.rsyncssh needs "host" configured', - level - ) + error( 'default.rsyncssh needs "host" configured', level ) end if not config.targetdir then - error( - 'default.rsyncssh needs "targetdir" configured', - level - ) + error( 'default.rsyncssh needs "targetdir" configured', level ) end -- @@ -472,18 +457,12 @@ rsyncssh.prepare = function -- if config.ssh._computed then - error( - 'please do not use the internal rsync._computed parameter', - level - ) + error( 'please do not use the internal rsync._computed parameter', level ) end if config.maxProcesses ~= 1 then - error( - 'default.rsyncssh must have maxProcesses set to 1.', - level - ) + error( 'default.rsyncssh must have maxProcesses set to 1.', level ) end local cssh = config.ssh; @@ -578,10 +557,11 @@ rsyncssh.prepare = function end -- appends a slash to the targetdir if missing + -- and is not ':' for home dir if string.sub( config.targetdir, -1 ) ~= '/' + and string.sub( config.targetdir, -1 ) ~= ':' then - config.targetdir = - config.targetdir .. '/' + config.targetdir = config.targetdir .. '/' end end diff --git a/lsyncd.h b/lsyncd.h index 6950cbe..9300a09 100644 --- a/lsyncd.h +++ b/lsyncd.h @@ -12,7 +12,7 @@ #define LSYNCD_H // some older machines need this to see pselect -#define _BSD_SOURCE 1 +#define _DEFAULT_SOURCE 1 #define _XOPEN_SOURCE 700 #define _DARWIN_C_SOURCE 1 diff --git a/lsyncd.lua b/lsyncd.lua index 949d3d9..e050451 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -349,7 +349,7 @@ Queue = ( function return nt[ nt.first ] end - + -- -- Returns the last item of the Queue. -- @@ -361,7 +361,7 @@ Queue = ( function return nt[ nt.last ] end - + -- -- Returns the size of the queue. -- @@ -549,7 +549,6 @@ Queue = ( function ) return iterReverse, self, self[ k_nt ].last + 1 end - -- -- Creates a new queue. @@ -1582,10 +1581,7 @@ local InletFactory = ( function if delay.etype ~= 'Move' then - if eu - then - return eu - end + if eu then return eu end local event = { } @@ -1598,10 +1594,7 @@ local InletFactory = ( function return event else -- moves have 2 events - origin and destination - if eu - then - return eu[1], eu[2] - end + if eu then return eu[1], eu[2] end local event = { move = 'Fr' } local event2 = { move = 'To' } @@ -1628,10 +1621,7 @@ local InletFactory = ( function ) local eu = e2d2[ dlist ] - if eu - then - return eu - end + if eu then return eu end local elist = { } @@ -1773,10 +1763,7 @@ local InletFactory = ( function if not f then - error( - 'inlet does not have function "'..func..'"', - 2 - ) + error( 'inlet does not have function "'..func..'"', 2 ) end return function( ... ) @@ -1837,7 +1824,7 @@ end )( ) -- --- A set of exclude patterns +-- A set of exclude patterns. -- local Excludes = ( function( ) @@ -1850,6 +1837,7 @@ local Excludes = ( function( ) p -- the rsync like pattern ) local o = p + p = string.gsub( p, '%%', '%%%%' ) p = string.gsub( p, '%^', '%%^' ) p = string.gsub( p, '%$', '%%$' ) @@ -1873,11 +1861,7 @@ local Excludes = ( function( ) p = '/' .. p; end - log( - 'Exclude', - 'toLuaPattern "', - o, '" = "', p, '"' - ) + log( 'Exclude', 'toLuaPattern "', o, '" = "', p, '"' ) return p end @@ -1908,7 +1892,6 @@ local Excludes = ( function( ) self, -- self pattern -- the pattern to remove ) - if not self.list[ pattern ] then -- already in the list? log( @@ -1930,9 +1913,9 @@ local Excludes = ( function( ) self, plist ) - for _, v in ipairs(plist) + for _, v in ipairs( plist ) do - add(self, v) + add( self, v ) end end @@ -1948,11 +1931,7 @@ local Excludes = ( function( ) if not f then - log( - 'Error', - 'Cannot open exclude file "', file,'": ', - err - ) + log( 'Error', 'Cannot open exclude file "', file,'": ', err ) terminate( -1 ) end @@ -2281,10 +2260,7 @@ local Sync = ( function do local pd = path .. dirname - if isdir - then - pd = pd..'/' - end + if isdir then pd = pd..'/' end log( 'Delay', @@ -3082,13 +3058,16 @@ local Syncs = ( function config.prepare( config, 4 ) end - if not config[ 'source' ] then + if not config[ 'source' ] + then local info = debug.getinfo( 3, 'Sl' ) + log( 'Error', info.short_src,':', info.currentline,': source missing from sync.' ) + terminate( -1 ) end @@ -3104,6 +3083,7 @@ local Syncs = ( function 'Cannot access source directory: ', config.source ) + terminate( -1 ) end @@ -3118,6 +3098,7 @@ local Syncs = ( function and not config.onMove then local info = debug.getinfo( 3, 'Sl' ) + log( 'Error', info.short_src, ':', @@ -3553,14 +3534,12 @@ end)( ) -- local Fsevents = ( function ( ) - -- -- A list indexed by syncs yielding -- the root path the sync is interested in. -- local syncRoots = { } - -- -- Adds a Sync to receive events. -- @@ -4036,8 +4015,6 @@ end )( ) -- local StatusFile = ( function ( ) - - -- -- Timestamp when the status file has been written. -- From c50aa7c9c10278166a5fc8929636102ce3a4229d Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Tue, 27 Feb 2018 10:14:57 +0100 Subject: [PATCH 2/7] Changelog --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index fa1adec..39d3ce6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 2018-??-??: 2.2.3 change: if the target/targetdir ands with a ':' do not append a trailing '/' to it, since that would change it from homedir to rootdir! + add: example for Amazon S3 Bucket (Daniel Miranda) 2017-02-16: 2.2.2 fix: checkgauge 'insist' From 1e0d867f8007a78f270b725003bd16f6a04bc592 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Tue, 27 Feb 2018 17:14:36 +0100 Subject: [PATCH 3/7] adding inclusion filters --- ChangeLog | 1 + default-rsync.lua | 105 ++++++++------ lsyncd.lua | 343 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 348 insertions(+), 101 deletions(-) diff --git a/ChangeLog b/ChangeLog index 39d3ce6..64138c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ 2018-??-??: 2.2.3 + enhaencement: supporting includes with new filter and filterFrom options change: if the target/targetdir ands with a ':' do not append a trailing '/' to it, since that would change it from homedir to rootdir! add: example for Amazon S3 Bucket (Daniel Miranda) diff --git a/default-rsync.lua b/default-rsync.lua index 7222faf..5f04796 100644 --- a/default-rsync.lua +++ b/default-rsync.lua @@ -49,6 +49,8 @@ rsync.checkgauge = { delete = true, exclude = true, excludeFrom = true, + filter = true, + filterFrom = true, target = true, rsync = { @@ -117,28 +119,6 @@ local eventNotInitBlank = end --- --- Replaces what rsync would consider filter rules by literals. --- -local replaceRsyncFilter = - function -( - path -) - if not path - then - return - end - - return( - path - :gsub( '%?', '\\?' ) - :gsub( '%*', '\\*' ) - :gsub( '%[', '\\[' ) - ) -end - - -- -- Spawns rsync for a list of events -- @@ -161,10 +141,11 @@ rsync.action = function -- -- Replaces what rsync would consider filter rules by literals -- - local function sub( p ) - if not p then - return - end + local function sub + ( + p -- pattern + ) + if not p then return end return p: gsub( '%?', '\\?' ): @@ -179,8 +160,14 @@ rsync.action = function -- Deletes create multi match patterns -- local paths = elist.getPaths( - function( etype, path1, path2 ) - if string.byte( path1, -1 ) == 47 and etype == 'Delete' then + function + ( + etype, -- event type + path1, -- path + path2 -- path to for move events + ) + if string.byte( path1, -1 ) == 47 and etype == 'Delete' + then return sub( path1 )..'***', sub( path2 ) else return sub( path1 ), sub( path2 ) @@ -195,11 +182,12 @@ rsync.action = function local filterP = { } -- adds one path to the filter - local function addToFilter( path ) + local function addToFilter + ( + path + ) - if filterP[ path ] then - return - end + if filterP[ path ] then return end filterP[ path ] = true @@ -211,21 +199,21 @@ rsync.action = function -- rsync needs to have entries for all steps in the path, -- so the file for example d1/d2/d3/f1 needs following filters: -- 'd1/', 'd1/d2/', 'd1/d2/d3/' and 'd1/d2/d3/f1' - for _, path in ipairs( paths ) do - - if path and path ~= '' then - - addToFilter(path) + for _, path in ipairs( paths ) + do + if path and path ~= '' + then + addToFilter( path ) local pp = string.match( path, '^(.*/)[^/]+/?' ) - while pp do - addToFilter(pp) + while pp + do + addToFilter( pp ) + pp = string.match( pp, '^(.*/)[^/]+/?' ) end - end - end log( @@ -334,6 +322,8 @@ rsync.init = function local excludes = inlet.getExcludes( ) + local filters = inlet.hasFilters( ) and inlet.getFilters( ) + local delete = nil local target = config.target @@ -354,9 +344,9 @@ rsync.init = function delete = { '--delete', '--ignore-errors' } end - if #excludes == 0 + if not filters and #excludes == 0 then - -- starts rsync without any excludes + -- starts rsync without any filters or excludes log( 'Normal', 'recursive startup rsync: ', @@ -375,7 +365,8 @@ rsync.init = function target ) - else + elseif not filters + then -- starts rsync providing an exclusion list -- on stdin local exS = table.concat( excludes, '\n' ) @@ -401,6 +392,32 @@ rsync.init = function config.source, target ) + else + -- starts rsync providing a filter list + -- on stdin + local fS = table.concat( filters, '\n' ) + + log( + 'Normal', + 'recursive startup rsync: ', + config.source, + ' -> ', + target, + ' filtering\n', + fS + ) + + spawn( + event, + config.rsync.binary, + '<', fS, + '--filter=. -', + delete, + config.rsync._computed, + '-r', + config.source, + target + ) end end diff --git a/lsyncd.lua b/lsyncd.lua index e050451..1ee1670 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -1650,6 +1650,19 @@ local InletFactory = ( function sync:addExclude( pattern ) end, + + -- + -- Appens a filter. + -- + appendFilter = function + ( + sync, -- the sync of the inlet + rule, -- '+' or '-' + pattern -- exlusion pattern to add + ) + sync:appendFilter( rule, pattern ) + end, + -- -- Removes an exclude. -- @@ -1663,7 +1676,7 @@ local InletFactory = ( function -- -- Gets the list of excludes in their - -- rsynlike patterns form. + -- rsync-like patterns form. -- getExcludes = function ( @@ -1682,6 +1695,48 @@ local InletFactory = ( function return e; end, + -- + -- Gets the list of filters and excldues + -- as rsync-like filter/patterns form. + -- + getFilters = function + ( + sync -- the sync of the inlet + ) + -- creates a copy + local e = { } + local en = 1; + + -- first takes the filters + if sync.filters + then + for _, entry in ipairs( sync.filters.list ) + do + e[ en ] = entry.rule .. ' ' .. entry.pattern; + en = en + 1; + end + end + + -- then the excludes + for k, _ in pairs( sync.excludes.list ) + do + e[ en ] = '- ' .. k; + en = en + 1; + end + + return e; + end, + + -- + -- Returns true if the sync has filters + -- + hasFilters = function + ( + sync -- the sync of the inlet + ) + return not not sync.filters + end, + -- -- Creates a blanketEvent that blocks everything -- and is blocked by everything. @@ -1892,8 +1947,9 @@ local Excludes = ( function( ) self, -- self pattern -- the pattern to remove ) + -- already in the list? if not self.list[ pattern ] - then -- already in the list? + then log( 'Normal', 'Removing not excluded exclude "' .. pattern .. '"' @@ -2016,6 +2072,180 @@ local Excludes = ( function( ) end )( ) +-- +-- A set of filter patterns. +-- +-- Filters allow excludes and includes +-- +local Filters = ( function( ) + + -- + -- Turns a rsync like file pattern to a lua pattern. + -- ( at best it can ) + -- + local function toLuaPattern + ( + p -- the rsync like pattern + ) + local o = p + + p = string.gsub( p, '%%', '%%%%' ) + p = string.gsub( p, '%^', '%%^' ) + p = string.gsub( p, '%$', '%%$' ) + p = string.gsub( p, '%(', '%%(' ) + p = string.gsub( p, '%)', '%%)' ) + p = string.gsub( p, '%.', '%%.' ) + p = string.gsub( p, '%[', '%%[' ) + p = string.gsub( p, '%]', '%%]' ) + p = string.gsub( p, '%+', '%%+' ) + p = string.gsub( p, '%-', '%%-' ) + p = string.gsub( p, '%?', '[^/]' ) + p = string.gsub( p, '%*', '[^/]*' ) + -- this was a ** before + p = string.gsub( p, '%[%^/%]%*%[%^/%]%*', '.*' ) + p = string.gsub( p, '^/', '^/' ) + + if p:sub( 1, 2 ) ~= '^/' + then + -- if does not begin with '^/' + -- then all matches should begin with '/'. + p = '/' .. p; + end + + log( 'Filter', 'toLuaPattern "', o, '" = "', p, '"' ) + + return p + end + + -- + -- Appends a filter pattern + -- + local function append + ( + self, -- the filters object + line -- filter line + ) + local rule, pattern = string.match( line, '%s*([+|-])%s*(.*)' ) + + if not rule or not pattern + then + log( 'Error', 'Unknown filter rule: "', line, '"' ) + terminate( -1 ) + end + + local lp = toLuaPattern( pattern ) + + table.insert( self. list, { rule = rule, pattern = pattern, lp = lp } ) + end + + -- + -- Adds a list of patterns to exclude. + -- + local function appendList + ( + self, + plist + ) + for _, v in ipairs( plist ) + do + append( self, v ) + end + end + + -- + -- Loads the filters from a file. + -- + local function loadFile + ( + self, -- self + file -- filename to load from + ) + f, err = io.open( file ) + + if not f + then + log( 'Error', 'Cannot open filter file "', file, '": ', err ) + + terminate( -1 ) + end + + for line in f:lines( ) + do + if string.match( line, '^%s*#' ) + or string.match( line, '^%s*$' ) + then + -- a comment or empty line: ignore + else + append( self, line ) + end + end + + f:close( ) + end + + -- + -- Tests if 'path' is excluded. + -- + local function test + ( + self, -- self + path -- the path to test + ) + if path:byte( 1 ) ~= 47 + then + error( 'Paths for exlusion tests must start with \'/\'' ) + end + + for _, entry in ipairs( self.list ) + do + local rule = entry.rule + local lp = entry.lp -- lua pattern + + if lp:byte( -1 ) == 36 + then + -- ends with $ + if path:match( lp ) + then + return rule == '-' + end + else + -- ends either end with / or $ + if path:match( lp .. '/' ) + or path:match( lp .. '$' ) + then + return rule == '-' + end + end + end + + return true + end + + -- + -- Cretes a new filter set. + -- + local function new + ( ) + return { + list = { }, + -- functions + append = append, + appendList = appendList, + loadFile = loadFile, + test = test, + } + end + + + -- + -- Public interface. + -- + return { new = new } + +end )( ) + + + -- -- Holds information about one observed directory including subdirs. -- @@ -2038,6 +2268,17 @@ local Sync = ( function return self.excludes:add( pattern ) end + local function appendFilter + ( + self, + rule, + pattern + ) + if not self.filters then self.filters = Filters.new( ) end + + return self.filters:append( rule, pattern ) + end + -- -- Removes an exclude. -- @@ -2280,14 +2521,8 @@ local Sync = ( function -- simple test for single path events if self.excludes:test( path ) then - log( - 'Exclude', - 'excluded ', - etype, - ' on "', - path, - '"' - ) + log( 'Exclude', 'excluded ', etype, ' on "', path, '"' ) + return end else @@ -2298,16 +2533,7 @@ local Sync = ( function if ex1 and ex2 then - log( - 'Exclude', - 'excluded "', - etype, - ' on "', - path, - '" -> "', - path2, - '"' - ) + log( 'Exclude', 'excluded "', etype, ' on "', path, '" -> "', path2, '"' ) return elseif not ex1 and ex2 @@ -2321,13 +2547,7 @@ local Sync = ( function path ) - delay( - self, - 'Delete', - time, - path, - nil - ) + delay( self, 'Delete', time, path, nil ) return elseif ex1 and not ex2 @@ -2341,13 +2561,7 @@ local Sync = ( function path2 ) - delay( - self, - 'Create', - time, - path2, - nil - ) + delay( self, 'Create', time, path2, nil ) return end @@ -2379,13 +2593,7 @@ local Sync = ( function end -- new delay - local nd = Delay.new( - etype, - self, - alarm, - path, - path2 - ) + local nd = Delay.new( etype, self, alarm, path, path2 ) if nd.etype == 'Init' or nd.etype == 'Blanket' then @@ -2562,10 +2770,7 @@ local Sync = ( function tr = test( InletFactory.d2e( d ) ) end - if tr == 'break' - then - break - end + if tr == 'break' then break end if d.status == 'active' or not tr then @@ -2767,12 +2972,14 @@ local Sync = ( function source = config.source, processes = CountArray.new( ), excludes = Excludes.new( ), + filters = nil, -- functions addBlanketDelay = addBlanketDelay, addExclude = addExclude, addInitDelay = addInitDelay, + appendFilter = appendFilter, collect = collect, concerns = concerns, delay = delay, @@ -2797,6 +3004,25 @@ local Sync = ( function -- so Sync{n} will be the n-th call to sync{} nextDefaultName = nextDefaultName + 1 + -- 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 + -- loads exclusions if config.exclude then @@ -2814,16 +3040,19 @@ local Sync = ( function end - if - config.delay ~= nil and - ( - type( config.delay ) ~= 'number' - or config.delay < 0 - ) + if config.delay ~= nil + and ( type( config.delay ) ~= 'number' or config.delay < 0 ) then error( 'delay must be a number and >= 0', 2 ) end + if config.filterFrom + then + if not s.filters then s.filters = Filters.new( ) end + + s.filters:loadFile( config.filterFrom ) + end + if config.excludeFrom then s.excludes:loadFile( config.excludeFrom ) @@ -3847,13 +4076,12 @@ local functionWriter = ( function( ) local as = '' local first = true - for _, v in ipairs( a ) do + for _, v in ipairs( a ) + do + if not first then as = as..' .. ' end - if not first then - as = as..' .. ' - end - - if v[ 1 ] then + if v[ 1 ] + then as = as .. '"' .. v[ 2 ] .. '"' else as = as .. v[ 2 ] @@ -3866,6 +4094,7 @@ local functionWriter = ( function( ) end local ft + if not haveEvent2 then ft = 'function( event )\n' From 465e173983b87c70cf373addac915ee832738233 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 1 Mar 2018 11:26:12 +0100 Subject: [PATCH 4/7] cleanups --- default-rsync.lua | 12 ++------ default.lua | 17 +++--------- lsyncd.lua | 70 ++++++++++++++++------------------------------- 3 files changed, 29 insertions(+), 70 deletions(-) diff --git a/default-rsync.lua b/default-rsync.lua index 5f04796..106c58d 100644 --- a/default-rsync.lua +++ b/default-rsync.lua @@ -16,16 +16,9 @@ --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -if not default -then - error( 'default not loaded' ) -end +if not default then error( 'default not loaded' ) end - -if default.rsync -then - error( 'default-rsync already loaded' ) -end +if default.rsync then error( 'default-rsync already loaded' ) end local rsync = { } @@ -186,7 +179,6 @@ rsync.action = function ( path ) - if filterP[ path ] then return end filterP[ path ] = true diff --git a/default.lua b/default.lua index f2dcbff..d689260 100644 --- a/default.lua +++ b/default.lua @@ -388,13 +388,10 @@ local function check ) for k, v in pairs( config ) do - if not gauge[k] + if not gauge[ k ] then error( - 'Parameter "' - .. subtable - .. k - .. '" unknown.' + 'Parameter "' .. subtable .. k .. '" unknown.' .. ' ( if this is not a typo add it to checkgauge )', level ); @@ -405,10 +402,7 @@ local function check if type( v ) ~= 'table' then error( - 'Parameter "' - .. subtable - .. k - .. '" must be a table.', + 'Parameter "' .. subtable .. k .. '" must be a table.', level ) end @@ -432,10 +426,7 @@ default.prepare = function local gauge = config.checkgauge - if not gauge - then - return - end + if not gauge then return end check( config, gauge, '', level + 1 ) end diff --git a/lsyncd.lua b/lsyncd.lua index 1ee1670..0f5c8f4 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -58,11 +58,9 @@ readdir = lsyncd.readdir -- Coping globals to ensure userscripts cannot change this. -- local log = log - local terminate = terminate - local now = now - +local readdir = readdir -- -- Predeclarations. @@ -123,8 +121,8 @@ local settingsSafe -- -- Array tables error if accessed with a non-number. -- -local Array = ( function( ) - +local Array = ( function +( ) -- -- Metatable. -- @@ -1881,8 +1879,8 @@ end )( ) -- -- A set of exclude patterns. -- -local Excludes = ( function( ) - +local Excludes = ( function +( ) -- -- Turns a rsync like file pattern to a lua pattern. -- ( at best it can ) @@ -2068,7 +2066,6 @@ local Excludes = ( function( ) -- Public interface. -- return { new = new } - end )( ) @@ -2077,8 +2074,8 @@ end )( ) -- -- Filters allow excludes and includes -- -local Filters = ( function( ) - +local Filters = ( function +( ) -- -- Turns a rsync like file pattern to a lua pattern. -- ( at best it can ) @@ -2395,18 +2392,12 @@ local Sync = ( function local alarm = self.config.delay -- delays at least 1 second - if alarm < 1 - then - alarm = 1 - end + if alarm < 1 then alarm = 1 end delay:wait( now( ) + alarm ) end else - log( - 'Delay', - 'collected a list' - ) + log( 'Delay', 'collected a list' ) local rc = self.config.collect( InletFactory.dl2el( delay ), @@ -2503,11 +2494,7 @@ local Sync = ( function if isdir then pd = pd..'/' end - log( - 'Delay', - 'Create creates Create on ', - pd - ) + log( 'Delay', 'Create creates Create on ', pd ) delay( self, 'Create', time, pd, nil ) end @@ -2876,7 +2863,6 @@ local Sync = ( function return d end end - end -- @@ -2975,7 +2961,6 @@ local Sync = ( function filters = nil, -- functions - addBlanketDelay = addBlanketDelay, addExclude = addExclude, addInitDelay = addInitDelay, @@ -3065,7 +3050,6 @@ local Sync = ( function -- Public interface -- return { new = new } - end )( ) @@ -3148,14 +3132,14 @@ local Syncs = ( function do if ( - type( k ) ~= 'number' or - verbatim or - cs._verbatim == true + type( k ) ~= 'number' + or verbatim + or cs._verbatim == true ) and ( - type( cs._merge ) ~= 'table' or - cs._merge[ k ] == true + type( cs._merge ) ~= 'table' + or cs._merge[ k ] == true ) then inheritKV( cd, k, v ) @@ -3166,11 +3150,8 @@ local Syncs = ( function -- ( for non-verbatim tables ) if cs._verbatim ~= true then - local n = nil - for k, v in ipairs( cs ) do - n = k if type( v ) == 'table' then inherit( cd, v ) @@ -3193,10 +3174,7 @@ local Syncs = ( function ) -- don't merge inheritance controls - if k == '_merge' or k == '_verbatim' - then - return - end + if k == '_merge' or k == '_verbatim' then return end local dtype = type( cd [ k ] ) @@ -3212,7 +3190,6 @@ local Syncs = ( function then inherit( cd[ k ], v, k == 'exitcodes' ) end - elseif dtype == 'nil' then cd[ k ] = v @@ -5259,17 +5236,16 @@ end -- -- Returns an Inlet to that sync. -- -function sync( opts ) - - if lsyncdStatus ~= 'init' then - error( - 'Sync can only be created during initialization.', - 2 - ) +function sync +( + opts +) + if lsyncdStatus ~= 'init' + then + error( 'Sync can only be created during initialization.', 2 ) end return Syncs.add( opts ).inlet - end From a78f239fa2c8df9398a2d7139db7ba3b6f3b3230 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 1 Mar 2018 14:14:28 +0100 Subject: [PATCH 5/7] apply filters before inotify watching dirs, actually filter events --- lsyncd.lua | 113 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 39 deletions(-) diff --git a/lsyncd.lua b/lsyncd.lua index 0f5c8f4..5a50b88 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -13,10 +13,6 @@ --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- require('profiler') --- profiler.start() - - -- -- A security measurement. -- The core will exit if version ids mismatch. @@ -2181,7 +2177,7 @@ local Filters = ( function end -- - -- Tests if 'path' is excluded. + -- Tests if 'path' is filtered. -- local function test ( @@ -2190,7 +2186,7 @@ local Filters = ( function ) if path:byte( 1 ) ~= 47 then - error( 'Paths for exlusion tests must start with \'/\'' ) + error( 'Paths for filter tests must start with \'/\'' ) end for _, entry in ipairs( self.list ) @@ -2215,7 +2211,10 @@ local Filters = ( function end end - return true + -- nil means neither a positivie + -- or negative hit, thus excludes have to + -- be queried + return nil end -- @@ -2311,14 +2310,35 @@ local Sync = ( function end end end + + + -- + -- Returns true if the relative path is excluded or filtered + -- + local function testFilter + ( + self, -- the Sync + path -- the relative path + ) + -- never filter the relative root itself + -- ( that would make zero sense ) + if path == '/' then return false end + + local filter = self.filters and self.filters:test( path ) + + if filter ~= nil then return filter end + + -- otherwise check excludes if concerned + return self.excludes:test( path ) + end -- -- Returns true if this Sync concerns about 'path'. -- local function concerns ( - self, - path + self, -- the Sync + path -- the absolute path ) -- not concerned if watch rootdir doesn't match if not path:starts( self.source ) @@ -2333,8 +2353,7 @@ local Sync = ( function return false end - -- concerned if not excluded - return not self.excludes:test( path:sub( #self.source ) ) + return not testFilter( self, path:sub( #self.source ) ) end -- @@ -2348,11 +2367,8 @@ local Sync = ( function ) local delay = self.processes[ pid ] - if not delay - then - -- not a child of this sync. - return - end + -- not a child of this sync? + if not delay then return end if delay.status then @@ -2414,11 +2430,8 @@ local Sync = ( function -- sets the delay on wait again local alarm = self.config.delay - -- delays at least 1 second - if alarm < 1 - then - alarm = 1 - end + -- delays are at least 1 second + if alarm < 1 then alarm = 1 end alarm = now() + alarm @@ -2506,29 +2519,33 @@ local Sync = ( function if not path2 then -- simple test for single path events - if self.excludes:test( path ) + if testFilter( self, path ) then - log( 'Exclude', 'excluded ', etype, ' on "', path, '"' ) + log( 'Filter', 'filtered ', etype, ' on "', path, '"' ) return end else -- for double paths ( move ) it might result into a split - local ex1 = self.excludes:test( path ) + local ex1 = testFilter( self, path ) - local ex2 = self.excludes:test( path2 ) + local ex2 = testFilter( self, path2 ) if ex1 and ex2 then - log( 'Exclude', 'excluded "', etype, ' on "', path, '" -> "', path2, '"' ) + log( + 'Filter', + 'filtered "', etype, ' on "', path, + '" -> "', path2, '"' + ) return elseif not ex1 and ex2 then -- splits the move if only partly excluded log( - 'Exclude', - 'excluded destination transformed ', + 'Filter', + 'filtered destination transformed ', etype, ' to Delete ', path @@ -2541,8 +2558,8 @@ local Sync = ( function then -- splits the move if only partly excluded log( - 'Exclude', - 'excluded origin transformed ', + 'Filter', + 'filtered origin transformed ', etype, ' to Create.', path2 @@ -2554,7 +2571,8 @@ local Sync = ( function end end - if etype == 'Move' and not self.config.onMove + if etype == 'Move' + and not self.config.onMove then -- if there is no move action defined, -- split a move as delete/create @@ -2926,14 +2944,30 @@ local Sync = ( function end - f:write( 'Excluding:\n' ) + f:write( 'Filtering:\n' ) local nothing = true - for t, p in pairs( self.excludes.list ) - do - nothing = false - f:write( t,'\n' ) + if self.filters + then + for _, e in pairs( self.filters.list ) + do + nothing = false + + f:write( e.rule, ' ', e.pattern,'\n' ) + end + end + + if #self.excludes.list > 0 + then + f:write( 'From excludes:\n' ) + + for t, p in pairs( self.excludes.list ) + do + nothing = false + + f:write( '- ', t,'\n' ) + end end if nothing @@ -2947,12 +2981,13 @@ local Sync = ( function -- -- Creates a new Sync. -- - local function new( config ) - + local function new + ( + config + ) local s = { -- fields - config = config, delays = Queue.new( ), source = config.source, From ba52ee1a6fecdd650889b8df57c4964d44f63530 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 1 Mar 2018 15:08:26 +0100 Subject: [PATCH 6/7] cleanups --- lsyncd.lua | 502 ++++++++++++++++++++--------------------------------- 1 file changed, 192 insertions(+), 310 deletions(-) diff --git a/lsyncd.lua b/lsyncd.lua index 5a50b88..50d8411 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -20,10 +20,7 @@ if lsyncd_version then -- ensures the runner is not being loaded twice - lsyncd.log( - 'Error', - 'You cannot use the lsyncd runner as configuration file!' - ) + lsyncd.log( 'Error', 'You cannot use the lsyncd runner as configuration file!' ) lsyncd.terminate( -1 ) end @@ -3813,20 +3810,10 @@ local Fsevents = ( function then path = path .. '/' - if path2 - then - path2 = path2 .. '/' - end + if path2 then path2 = path2 .. '/' end end - log( - 'Fsevents', - etype, ',', - isdir, ',', - time, ',', - path, ',', - path2 - ) + log( 'Fsevents', etype, ',', isdir, ',', time, ',', path, ',', path2 ) for _, sync in Syncs.iwalk() do repeat @@ -3858,11 +3845,7 @@ local Fsevents = ( function then if not relative2 then - log( - 'Normal', - 'Transformed Move to Delete for ', - sync.config.name - ) + log( 'Normal', 'Transformed Move to Delete for ', sync.config.name ) etyped = 'Delete' @@ -3872,11 +3855,7 @@ local Fsevents = ( function relative2 = nil - log( - 'Normal', - 'Transformed Move to Create for ', - sync.config.name - ) + log( 'Normal', 'Transformed Move to Create for ', sync.config.name ) etyped = 'Create' end @@ -3925,7 +3904,8 @@ Monitors = ( function -- -- The default event monitor. -- - local function default( ) + local function default + ( ) return list[ 1 ] end @@ -4049,33 +4029,39 @@ local functionWriter = ( function( ) -- true if there is a second event 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 local a = { { true, iv } } -- goes through all translates - for _, v in ipairs( transVars ) do + for _, v in ipairs( transVars ) + do local ai = 1 - while ai <= #a do - if a[ ai ][ 1 ] then + while ai <= #a + do + if a[ ai ][ 1 ] + then local pre, post = string.match( a[ ai ][ 2 ], '(.*)'..v[1]..'(.*)' ) - if pre then - - if v[3] > 1 then + if pre + then + if v[3] > 1 + then haveEvent2 = true end - if pre ~= '' then + if pre ~= '' + then table.insert( a, ai, { true, pre } ) ai = ai + 1 end a[ ai ] = { false, v[ 2 ] } - if post ~= '' then + if post ~= '' + then table.insert( a, ai + 1, { true, post } ) end end @@ -4155,17 +4141,14 @@ local functionWriter = ( function( ) cmd = string.gsub( cmd, v[ 1 ], - function( ) + function + ( ) occur = true return '"$' .. argn .. '"' end ) - lc = string.gsub( - lc, - v[1], - ']]..' .. v[2] .. '..[[' - ) + lc = string.gsub( lc, v[1], ']]..' .. v[2] .. '..[[' ) if occur then @@ -4218,6 +4201,7 @@ local functionWriter = ( function( ) str = string.match( str, '^%s*(.-)%s*$' ) local ft + if string.byte( str, 1, 1 ) == 47 then -- starts with / @@ -4230,13 +4214,7 @@ local functionWriter = ( function( ) ft = translateShell( str ) end - log( - 'FWrite', - 'translated "', - str, - '" to \n', - ft - ) + log( 'FWrite', 'translated "', str, '" to \n', ft ) return ft end @@ -4294,14 +4272,7 @@ local StatusFile = ( function -- already waiting? if alarm and timestamp < alarm then - log( - 'Statusfile', - 'waiting(', - timestamp, - ' < ', - alarm, - ')' - ) + log( 'Statusfile', 'waiting(', timestamp, ' < ', alarm, ')' ) return end @@ -4309,17 +4280,11 @@ local StatusFile = ( function -- determines when a next write will be possible if not alarm then - local nextWrite = - lastWritten and timestamp - + uSettings.statusInterval + local nextWrite = lastWritten and timestamp + uSettings.statusInterval if nextWrite and timestamp < nextWrite then - log( - 'Statusfile', - 'setting alarm: ', - nextWrite - ) + log( 'Statusfile', 'setting alarm: ', nextWrite ) alarm = nextWrite return @@ -4400,7 +4365,8 @@ local UserAlarms = ( function end end - local a = { + local a = + { timestamp = timestamp, func = func, extra = extra @@ -4432,10 +4398,12 @@ local UserAlarms = ( function -- -- Calls user alarms. -- - local function invoke( timestamp ) - while - #alarms > 0 and - alarms[ 1 ].timestamp <= timestamp + local function invoke + ( + timestamp + ) + while #alarms > 0 + and alarms[ 1 ].timestamp <= timestamp do alarms[ 1 ].func( alarms[ 1 ].timestamp, alarms[ 1 ].extra ) table.remove( alarms, 1 ) @@ -4486,7 +4454,7 @@ function runner.callError ( message ) - log('Error', 'in Lua: ', message ) + log( 'Error', 'in Lua: ', message ) -- prints backtrace local level = 2 @@ -4517,18 +4485,22 @@ end -- Called from core whenever a child process has finished and -- the zombie process was collected by core. -- -function runner.collectProcess( pid, exitcode ) - +function runner.collectProcess +( + pid, -- process id + exitcode -- exitcode +) processCount = processCount - 1 - if processCount < 0 then + if processCount < 0 + then error( 'negative number of processes!' ) end - for _, s in Syncs.iwalk() do - if s:collect(pid, exitcode) then return end + for _, s in Syncs.iwalk( ) + do + if s:collect( pid, exitcode ) then return end end - end -- @@ -4683,119 +4655,119 @@ function runner.configure( args, monitors ) -- -- second paramter is the function to call -- - local options = { - + local options = + { -- log is handled by core already. delay = - { - 1, - function( secs ) - clSettings.delay = secs + 0 - end - }, + { + 1, + function( secs ) + clSettings.delay = secs + 0 + end + }, insist = - { - 0, - function( ) - clSettings.insist = true - end - }, + { + 0, + function( ) + clSettings.insist = true + end + }, log = - { - 1, - nil - }, + { + 1, + nil + }, logfile = - { - 1, - function( file ) - clSettings.logfile = file - end - }, + { + 1, + function( file ) + clSettings.logfile = file + end + }, monitor = - { - -1, - function( monitor ) - if not monitor then - io.stdout:write( 'This Lsyncd supports these monitors:\n' ) - for _, v in ipairs(Monitors.list) do - io.stdout:write(' ',v,'\n') - end - - io.stdout:write('\n') - - lsyncd.terminate(-1) - else - clSettings.monitor = monitor + { + -1, + function( monitor ) + if not monitor then + io.stdout:write( 'This Lsyncd supports these monitors:\n' ) + for _, v in ipairs(Monitors.list) do + io.stdout:write(' ',v,'\n') end + + io.stdout:write('\n') + + lsyncd.terminate(-1) + else + clSettings.monitor = monitor end - }, + end + }, nodaemon = - { - 0, - function( ) - clSettings.nodaemon = true - end - }, + { + 0, + function( ) + clSettings.nodaemon = true + end + }, pidfile = - { - 1, - function( file ) - clSettings.pidfile=file - end - }, + { + 1, + function( file ) + clSettings.pidfile=file + end + }, rsync = - { - 2, - function( src, trg ) - clSettings.syncs = clSettings.syncs or { } - table.insert( - clSettings.syncs, - { 'rsync', src, trg } - ) - end - }, + { + 2, + function( src, trg ) + clSettings.syncs = clSettings.syncs or { } + table.insert( + clSettings.syncs, + { 'rsync', src, trg } + ) + end + }, rsyncssh = - { - 3, - function( src, host, tdir ) - clSettings.syncs = clSettings.syncs or { } - table.insert( - clSettings.syncs, - { 'rsyncssh', src, host, tdir } - ) - end - }, + { + 3, + function( src, host, tdir ) + clSettings.syncs = clSettings.syncs or { } + table.insert( + clSettings.syncs, + { 'rsyncssh', src, host, tdir } + ) + end + }, direct = - { - 2, - function( src, trg ) - clSettings.syncs = clSettings.syncs or { } - table.insert( - clSettings.syncs, - { 'direct', src, trg } - ) - end - }, + { + 2, + function( src, trg ) + clSettings.syncs = clSettings.syncs or { } + table.insert( + clSettings.syncs, + { 'direct', src, trg } + ) + end + }, version = - { - 0, - function( ) - io.stdout:write( 'Version: ', lsyncd_version, '\n' ) - os.exit( 0 ) - end - } + { + 0, + function( ) + io.stdout:write( 'Version: ', lsyncd_version, '\n' ) + os.exit( 0 ) + end + } } -- 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 i = 1 - while i <= #args do + while i <= #args + do local a = args[ i ] - if a:sub( 1, 1 ) ~= '-' then + if a:sub( 1, 1 ) ~= '-' + then table.insert( nonopts, args[ i ] ) else - if a:sub( 1, 2 ) == '--' then + if a:sub( 1, 2 ) == '--' + then a = a:sub( 3 ) else a = a:sub( 2 ) @@ -4820,11 +4795,8 @@ function runner.configure( args, monitors ) if not o then - log( - 'Error', - 'unknown option command line option ', - args[i] - ) + log( 'Error', 'unknown option command line option ', args[ i ] ) + os.exit( -1 ) end @@ -4865,10 +4837,8 @@ function runner.configure( args, monitors ) then if #nonopts ~= 0 then - log( - 'Error', - 'There cannot be command line syncs and a config file together.' - ) + log( 'Error', 'There cannot be command line syncs and a config file together.' ) + os.exit( -1 ) end @@ -4882,10 +4852,7 @@ function runner.configure( args, monitors ) return nonopts[ 1 ] else -- TODO make this possible - log( - 'Error', - 'There can only be one config file in the command line.' - ) + log( 'Error', 'There can only be one config file in the command line.' ) os.exit( -1 ) end @@ -4910,7 +4877,7 @@ function runner.initialize( firstTime ) log( 'Error', 'Do not use settings = { ... }\n'.. - ' please use settings{ ... } (without the equal sign)' + ' please use settings{ ... } ( without the equal sign )' ) os.exit( -1 ) @@ -4923,26 +4890,6 @@ function runner.initialize( firstTime ) -- 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 -- @@ -5031,10 +4978,7 @@ function runner.initialize( firstTime ) -- makes sure the user gave Lsyncd anything to do if Syncs.size() == 0 then - log( - 'Error', - 'Nothing to watch!' - ) + log( 'Error', 'Nothing to watch!' ) os.exit( -1 ) end @@ -5044,7 +4988,8 @@ function runner.initialize( firstTime ) lsyncd.configure( 'running' ); - local ufuncs = { + local ufuncs = + { 'onAttrib', 'onCreate', 'onDelete', @@ -5094,7 +5039,6 @@ function runner.initialize( firstTime ) s:addInitDelay( ) end end - end -- @@ -5106,13 +5050,9 @@ end -- function runner.getAlarm ( ) - log( 'Function', 'getAlarm( )' ) - if lsyncdStatus ~= 'run' - then - return false - end + if lsyncdStatus ~= 'run' then return false end local alarm = false @@ -5121,12 +5061,9 @@ function runner.getAlarm -- local function checkAlarm ( - a + a -- alarm time ) - if a == nil - then - error('got nil alarm') - end + if a == nil then error( 'got nil alarm' ) end if alarm == true or not a then @@ -5166,14 +5103,9 @@ function runner.getAlarm -- checks for an userAlarm checkAlarm( UserAlarms.getAlarm( ) ) - log( - 'Alarm', - 'runner.getAlarm returns: ', - alarm - ) + log( 'Alarm', 'runner.getAlarm returns: ', alarm ) return alarm - end @@ -5193,12 +5125,7 @@ function runner.collector ) if exitcode ~= 0 then - log( - 'Error', - 'Startup process', - pid, - ' failed' - ) + log( 'Error', 'Startup process', pid, ' failed' ) terminate( -1 ) end @@ -5209,54 +5136,41 @@ end -- -- Called by core when an overflow happened. -- -function runner.overflow( ) - - log( - 'Normal', - '--- OVERFLOW in event queue ---' - ) +function runner.overflow +( ) + log( 'Normal', '--- OVERFLOW in event queue ---' ) lsyncdStatus = 'fade' - end -- -- Called by core on a hup signal. -- -function runner.hup( ) - - log( - 'Normal', - '--- HUP signal, resetting ---' - ) +function runner.hup +( ) + log( 'Normal', '--- HUP signal, resetting ---' ) lsyncdStatus = 'fade' - end -- -- Called by core on a term signal. -- -function runner.term( sigcode ) - - local sigtexts = { - [ 2 ] = - 'INT', - - [ 15 ] = - 'TERM' +function runner.term +( + sigcode -- signal code +) + local sigtexts = + { + [ 2 ] = 'INT', + [ 15 ] = 'TERM' }; local sigtext = sigtexts[ sigcode ]; - if not sigtext then - sigtext = 'UNKNOWN' - end + if not sigtext then sigtext = 'UNKNOWN' end - log( - 'Normal', - '--- ', sigtext, ' signal, fading ---' - ) + log( 'Normal', '--- ', sigtext, ' signal, fading ---' ) lsyncdStatus = 'fade' @@ -5294,38 +5208,28 @@ function spawn( binary, -- binary to call ... -- arguments ) - if - agent == nil or - type( agent ) ~= 'table' + if agent == nil + or type( agent ) ~= 'table' then - error( - 'spawning with an invalid agent', - 2 - ) + error( 'spawning with an invalid agent', 2 ) end - if lsyncdStatus == 'fade' then - log( - 'Normal', - 'ignored process spawning while fading' - ) + if lsyncdStatus == 'fade' + then + log( 'Normal', 'ignored process spawning while fading' ) return end - if type( binary ) ~= 'string' then - error( - 'calling spawn(agent, binary, ...): binary is not a string', - 2 - ) + if type( binary ) ~= 'string' + then + error( 'calling spawn(agent, binary, ...): binary is not a string', 2 ) end local dol = InletFactory.getDelayOrList( agent ) - if not dol then - error( - 'spawning with an unknown agent', - 2 - ) + if not dol + then + error( 'spawning with an unknown agent', 2 ) end -- @@ -5360,9 +5264,8 @@ function spawn( then processCount = processCount + 1 - if - uSettings.maxProcesses and - processCount > uSettings.maxProcesses + if uSettings.maxProcesses + and processCount > uSettings.maxProcesses then error( 'Spawned too much processes!' ) end @@ -5397,14 +5300,7 @@ function spawnShell command, -- the shell command ... -- additonal arguments ) - return spawn( - agent, - '/bin/sh', - '-c', - command, - '/bin/sh', - ... - ) + return spawn( agent, '/bin/sh', '-c', command, '/bin/sh', ... ) end @@ -5417,11 +5313,7 @@ function observefd ready, -- called when fd is ready to be read writey -- called when fd is ready to be written ) - return lsyncd.observe_fd( - fd, - ready, - writey - ) + return lsyncd.observe_fd( fd, ready, writey ) end @@ -5493,24 +5385,14 @@ function settings then if not settingsCheckgauge[ k ] then - error( - 'setting "' - ..k - ..'" unknown.', - 2 - ) + error( 'setting "'..k..'" unknown.', 2 ) end uSettings[ k ] = v else if not settingsCheckgauge[ v ] then - error( - 'setting "' - ..v - ..'" unknown.', - 2 - ) + error( 'setting "'..v..'" unknown.', 2 ) end uSettings[ v ] = true From faa582e2591cea09369f73aa4941d10ce1ea8606 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 1 Mar 2018 15:19:30 +0100 Subject: [PATCH 7/7] cleanups --- lsyncd.lua | 81 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/lsyncd.lua b/lsyncd.lua index 50d8411..0c4cda9 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -4662,7 +4662,10 @@ function runner.configure( args, monitors ) delay = { 1, - function( secs ) + function + ( + secs + ) clSettings.delay = secs + 0 end }, @@ -4670,7 +4673,8 @@ function runner.configure( args, monitors ) insist = { 0, - function( ) + function + ( ) clSettings.insist = true end }, @@ -4684,7 +4688,10 @@ function runner.configure( args, monitors ) logfile = { 1, - function( file ) + function + ( + file + ) clSettings.logfile = file end }, @@ -4692,16 +4699,21 @@ function runner.configure( args, monitors ) monitor = { -1, - function( monitor ) - if not monitor then + function + ( + monitor + ) + if not monitor + then io.stdout:write( 'This Lsyncd supports these monitors:\n' ) - for _, v in ipairs(Monitors.list) do - io.stdout:write(' ',v,'\n') + for _, v in ipairs( Monitors.list ) + do + io.stdout:write( ' ', v, '\n' ) end io.stdout:write('\n') - lsyncd.terminate(-1) + lsyncd.terminate( -1 ) else clSettings.monitor = monitor end @@ -4711,7 +4723,8 @@ function runner.configure( args, monitors ) nodaemon = { 0, - function( ) + function + ( ) clSettings.nodaemon = true end }, @@ -4719,52 +4732,63 @@ function runner.configure( args, monitors ) pidfile = { 1, - function( file ) + function + ( + file + ) clSettings.pidfile=file end }, - rsync = + rsync = { 2, - function( src, trg ) + function + ( + src, + trg + ) clSettings.syncs = clSettings.syncs or { } - table.insert( - clSettings.syncs, - { 'rsync', src, trg } - ) + table.insert( clSettings.syncs, { 'rsync', src, trg } ) end }, rsyncssh = { 3, - function( src, host, tdir ) + function + ( + src, + host, + tdir + ) clSettings.syncs = clSettings.syncs or { } - table.insert( - clSettings.syncs, - { 'rsyncssh', src, host, tdir } - ) + + table.insert( clSettings.syncs, { 'rsyncssh', src, host, tdir } ) end }, direct = { 2, - function( src, trg ) + function + ( + src, + trg + ) clSettings.syncs = clSettings.syncs or { } - table.insert( - clSettings.syncs, - { 'direct', src, trg } - ) + + table.insert( clSettings.syncs, { 'direct', src, trg } ) end }, version = { 0, - function( ) + function + ( ) io.stdout:write( 'Version: ', lsyncd_version, '\n' ) + os.exit( 0 ) end } @@ -4841,9 +4865,7 @@ function runner.configure( args, monitors ) os.exit( -1 ) end - else - if #nonopts == 0 then runner.help( args[ 0 ] ) @@ -4856,7 +4878,6 @@ function runner.configure( args, monitors ) os.exit( -1 ) end - end end