From 4d2c793b0132cfb233ab9da1252d317c7a7bea20 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Fri, 25 Nov 2016 14:55:59 +0100 Subject: [PATCH] code beautify --- default-rsyncssh.lua | 108 +++++++++++++++++++------------------------ lsyncd.lua | 60 ++++++++++++------------ 2 files changed, 79 insertions(+), 89 deletions(-) diff --git a/default-rsyncssh.lua b/default-rsyncssh.lua index 1e811ac..290f622 100644 --- a/default-rsyncssh.lua +++ b/default-rsyncssh.lua @@ -283,14 +283,16 @@ rsyncssh.prepare = function( config, level ) default.rsync.prepare( config, level + 1, true ) - if not config.host then + if not config.host + then error( 'default.rsyncssh needs "host" configured', level ) end - if not config.targetdir then + if not config.targetdir + then error( 'default.rsyncssh needs "targetdir" configured', level @@ -300,44 +302,37 @@ rsyncssh.prepare = function( config, level ) -- -- computes the ssh options -- - if config.ssh._computed then + if config.ssh._computed + then error( 'please do not use the internal rsync._computed parameter', level ) end - local cssh = - config.ssh; + local cssh = config.ssh; - cssh._computed = - { } + cssh._computed = { } - local computed = - cssh._computed + local computed = cssh._computed - local computedN = - 1 + local computedN = 1 - local rsyncc = - config.rsync._computed + local rsyncc = config.rsync._computed - if cssh.identityFile then - computed[ computedN ] = - '-i' + if cssh.identityFile + then + computed[ computedN ] = '-i' - computed[ computedN + 1 ] = - cssh.identityFile + computed[ computedN + 1 ] = cssh.identityFile - computedN = - computedN + 2 + computedN = computedN + 2 - if not config.rsync._rshIndex then - config.rsync._rshIndex = - #rsyncc + 1 + if not config.rsync._rshIndex + then + config.rsync._rshIndex = #rsyncc + 1 - rsyncc[ config.rsync._rshIndex ] = - '--rsh=ssh' + rsyncc[ config.rsync._rshIndex ] = '--rsh=ssh' end rsyncc[ config.rsync._rshIndex ] = @@ -346,26 +341,21 @@ rsyncssh.prepare = function( config, level ) cssh.identityFile end - if cssh.options then + if cssh.options + then + for k, v in pairs( cssh.options ) + do + computed[ computedN ] = '-o' + computed[ computedN + 1 ] = k .. '=' .. v - for k, v in pairs( cssh.options ) do + computedN = computedN + 2 - computed[ computedN ] = - '-o' + if not config.rsync._rshIndex + then + config.rsync._rshIndex = #rsyncc + 1 - computed[ computedN + 1 ] = - k .. '=' .. v - - computedN = - computedN + 2 - - if not config.rsync._rshIndex then - config.rsync._rshIndex = - #rsyncc + 1 - - rsyncc[ config.rsync._rshIndex ] = - '--rsh=ssh' + rsyncc[ config.rsync._rshIndex ] = '--rsh=ssh' end rsyncc[ config.rsync._rshIndex ] = @@ -382,40 +372,38 @@ rsyncssh.prepare = function( config, level ) end end - if cssh.port then - computed[ computedN ] = - '-p' + if cssh.port + then + computed[ computedN ] = '-p' - computed[ computedN + 1 ] = - cssh.port + computed[ computedN + 1 ] = cssh.port - computedN = - computedN + 2 + computedN = computedN + 2 - if not config.rsync._rshIndex then - config.rsync._rshIndex = - #rsyncc + 1 + if not config.rsync._rshIndex + then + config.rsync._rshIndex = #rsyncc + 1 - rsyncc[ config.rsync._rshIndex ] = - '--rsh=ssh' + rsyncc[ config.rsync._rshIndex ] = '--rsh=ssh' end rsyncc[ config.rsync._rshIndex ] = rsyncc[ config.rsync._rshIndex ] .. ' -p ' .. cssh.port end - if cssh._extra then - for k, v in ipairs( cssh._extra ) do - computed[ computedN ] = - v + if cssh._extra + then + for k, v in ipairs( cssh._extra ) + do + computed[ computedN ] = v - computedN = - computedN + 1 + computedN = computedN + 1 end end -- appends a slash to the targetdir if missing - if string.sub( config.targetdir, -1 ) ~= '/' then + if string.sub( config.targetdir, -1 ) ~= '/' + then config.targetdir = config.targetdir .. '/' end diff --git a/lsyncd.lua b/lsyncd.lua index 0e18cab..383d430 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -3349,16 +3349,18 @@ local UserAlarms = ( function( ) local alarms = { } - -- -- Calls the user function at timestamp. -- local function alarm( timestamp, func, extra ) - local idx - for k, v in ipairs( alarms ) do - if timestamp < v.timestamp then + + for k, v in ipairs( alarms ) + do + if timestamp < v.timestamp + then idx = k + break end end @@ -3369,12 +3371,12 @@ local UserAlarms = ( function( ) extra = extra } - if idx then + if idx + then table.insert( alarms, idx, a ) else table.insert( alarms, a ) end - end @@ -3382,13 +3384,12 @@ local UserAlarms = ( function( ) -- Retrieves the soonest alarm. -- local function getAlarm( ) - - if #alarms == 0 then + if #alarms == 0 + then return false else return alarms[1].timestamp end - end @@ -3415,7 +3416,6 @@ local UserAlarms = ( function( ) invoke = invoke } - end )( ) --============================================================================ @@ -3451,11 +3451,13 @@ function runner.callError( message ) -- prints backtrace local level = 2 - while true do + while true + do local info = debug.getinfo( level, 'Sl' ) - if not info then + if not info + then terminate( -1 ) end @@ -3502,11 +3504,10 @@ end function runner.cycle( timestamp -- the current kernel time (in jiffies) ) - - if lsyncdStatus == 'fade' then - - if processCount > 0 then - + if lsyncdStatus == 'fade' + then + if processCount > 0 + then if lastReportedWaiting == false or timestamp >= lastReportedWaiting + 60 @@ -3523,7 +3524,6 @@ function runner.cycle( return true else - return false end end @@ -3546,13 +3546,12 @@ function runner.cycle( local ir = start repeat - local s = Syncs.get( ir ) s:invokeActions( timestamp ) ir = ir + 1 - if ir > Syncs.size( ) then - + if ir > Syncs.size( ) + then ir = 1 end until ir == start @@ -3562,7 +3561,8 @@ function runner.cycle( UserAlarms.invoke( timestamp ) - if uSettings.statusFile then + if uSettings.statusFile + then StatusFile.write( timestamp ) end @@ -4050,13 +4050,14 @@ end -- -- Called by core to query the soonest alarm. -- --- @return false ... no alarm, core can in untimed sleep, or +-- @return false ... no alarm, core can go in untimed sleep -- true ... immediate action -- times ... the alarm time (only read if number is 1) -- function runner.getAlarm( ) - if lsyncdStatus ~= 'run' then + if lsyncdStatus ~= 'run' + then return false end @@ -4066,22 +4067,23 @@ function runner.getAlarm( ) -- Checks if 'a' is sooner than the 'alarm' up-value. -- local function checkAlarm( a ) - - if a == nil then + if a == nil + then error('got nil alarm') end - if alarm == true or not a then + if alarm == true or not a + then -- 'alarm' is already immediate or -- a not a new alarm return end -- sets 'alarm' to a if a is sooner - if not alarm or a < alarm then + if not alarm or a < alarm + then alarm = a end - end --