mirror of
https://github.com/octoleo/lsyncd.git
synced 2025-02-02 11:58:25 +00:00
beautifing code
This commit is contained in:
parent
899077ccd7
commit
2e9c103f55
@ -23,31 +23,45 @@ if default.rsync then
|
|||||||
error('default-rsync already loaded')
|
error('default-rsync already loaded')
|
||||||
end
|
end
|
||||||
|
|
||||||
default.rsync = {
|
default.rsync = { }
|
||||||
-----
|
|
||||||
|
--
|
||||||
-- Spawns rsync for a list of events
|
-- Spawns rsync for a list of events
|
||||||
--
|
--
|
||||||
action = function(inlet)
|
-- Exlcusions are already handled by not having
|
||||||
|
-- events for them.
|
||||||
|
--
|
||||||
|
default.rsync.action = function( inlet )
|
||||||
|
|
||||||
|
--
|
||||||
-- gets all events ready for syncing
|
-- gets all events ready for syncing
|
||||||
|
--
|
||||||
local elist = inlet.getEvents(
|
local elist = inlet.getEvents(
|
||||||
function(event)
|
function(event)
|
||||||
return event.etype ~= 'Init' and event.etype ~= 'Blanket'
|
return event.etype ~= 'Init' and event.etype ~= 'Blanket'
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
-----
|
--
|
||||||
-- replaces filter rule by literals
|
-- Replaces what rsync would consider filter rules by literals
|
||||||
--
|
--
|
||||||
local function sub( p )
|
local function sub( p )
|
||||||
if not p then
|
if not p then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
return p:gsub('%?', '\\?'):
|
|
||||||
|
return p:
|
||||||
|
gsub('%?', '\\?'):
|
||||||
gsub('%*', '\\*'):
|
gsub('%*', '\\*'):
|
||||||
gsub('%[', '\\['):
|
gsub('%[', '\\['):
|
||||||
gsub('%]', '\\]')
|
gsub('%]', '\\]')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Gets the list of paths for the event list
|
||||||
|
--
|
||||||
|
-- Deletes create multi match patterns
|
||||||
|
--
|
||||||
local paths = elist.getPaths(
|
local paths = elist.getPaths(
|
||||||
function( etype, path1, path2 )
|
function( etype, path1, path2 )
|
||||||
if string.byte( path1, -1 ) == 47 and etype == 'Delete' then
|
if string.byte( path1, -1 ) == 47 and etype == 'Delete' then
|
||||||
@ -55,43 +69,66 @@ default.rsync = {
|
|||||||
else
|
else
|
||||||
return sub( path1 ), sub( path2 )
|
return sub( path1 ), sub( path2 )
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
-- stores all filters with integer index
|
)
|
||||||
-- local filterI = inlet.getExcludes()
|
|
||||||
|
--
|
||||||
|
-- stores all filters by integer index
|
||||||
|
--
|
||||||
local filterI = { }
|
local filterI = { }
|
||||||
-- stores all filters with path index
|
|
||||||
|
--
|
||||||
|
-- Stores all filters with path index
|
||||||
|
--
|
||||||
local filterP = { }
|
local filterP = { }
|
||||||
|
|
||||||
-- adds one entry into the filter
|
--
|
||||||
-- @param path ... path to add
|
-- Adds one path to the filter
|
||||||
-- @param leaf ... true if this the original path
|
--
|
||||||
-- false if its a parent
|
|
||||||
local function addToFilter( path )
|
local function addToFilter( path )
|
||||||
|
|
||||||
if filterP[ path ] then
|
if filterP[ path ] then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
filterP[ path ] = true
|
filterP[ path ] = true
|
||||||
|
|
||||||
table.insert( filterI, path )
|
table.insert( filterI, path )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- adds a path to the filter, for rsync this needs
|
--
|
||||||
-- to have entries for all steps in the path, so the file
|
-- Adds a path to the filter.
|
||||||
-- d1/d2/d3/f1 needs filters
|
--
|
||||||
|
-- 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'
|
-- 'd1/', 'd1/d2/', 'd1/d2/d3/' and 'd1/d2/d3/f1'
|
||||||
|
--
|
||||||
for _, path in ipairs( paths ) do
|
for _, path in ipairs( paths ) do
|
||||||
|
|
||||||
if path and path ~= '' then
|
if path and path ~= '' then
|
||||||
|
|
||||||
addToFilter(path)
|
addToFilter(path)
|
||||||
|
|
||||||
local pp = string.match( path, '^(.*/)[^/]+/?' )
|
local pp = string.match( path, '^(.*/)[^/]+/?' )
|
||||||
|
|
||||||
while pp do
|
while pp do
|
||||||
addToFilter(pp)
|
addToFilter(pp)
|
||||||
pp = string.match( pp, '^(.*/)[^/]+/?' )
|
pp = string.match( pp, '^(.*/)[^/]+/?' )
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local filterS = table.concat( filterI, '\n' )
|
local filterS = table.concat( filterI, '\n' )
|
||||||
local filter0 = table.concat( filterI, '\000' )
|
local filter0 = table.concat( filterI, '\000' )
|
||||||
log('Normal', 'Calling rsync with filter-list of new/modified files/dirs\n', filterS)
|
|
||||||
|
log(
|
||||||
|
'Normal',
|
||||||
|
'Calling rsync with filter-list of new/modified files/dirs\n',
|
||||||
|
filterS
|
||||||
|
)
|
||||||
|
|
||||||
local config = inlet.getConfig( )
|
local config = inlet.getConfig( )
|
||||||
local delete = nil
|
local delete = nil
|
||||||
|
|
||||||
@ -99,8 +136,9 @@ default.rsync = {
|
|||||||
delete = { '--delete', '--ignore-errors' }
|
delete = { '--delete', '--ignore-errors' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
spawn(
|
||||||
spawn(elist, config.rsync.binary,
|
elist,
|
||||||
|
config.rsync.binary,
|
||||||
'<', filter0,
|
'<', filter0,
|
||||||
config.rsync._computed,
|
config.rsync._computed,
|
||||||
'-r',
|
'-r',
|
||||||
@ -110,13 +148,16 @@ default.rsync = {
|
|||||||
'--include-from=-',
|
'--include-from=-',
|
||||||
'--exclude=*',
|
'--exclude=*',
|
||||||
config.source,
|
config.source,
|
||||||
config.target)
|
config.target
|
||||||
end,
|
)
|
||||||
|
|
||||||
-----
|
end
|
||||||
|
|
||||||
|
--
|
||||||
-- Spawns the recursive startup sync
|
-- Spawns the recursive startup sync
|
||||||
--
|
--
|
||||||
init = function(event)
|
init = function(event)
|
||||||
|
|
||||||
local config = event.config
|
local config = event.config
|
||||||
local inlet = event.inlet
|
local inlet = event.inlet
|
||||||
local excludes = inlet.getExcludes( )
|
local excludes = inlet.getExcludes( )
|
||||||
@ -127,38 +168,71 @@ default.rsync = {
|
|||||||
end
|
end
|
||||||
|
|
||||||
if #excludes == 0 then
|
if #excludes == 0 then
|
||||||
log('Normal', 'recursive startup rsync: ', config.source, ' -> ', config.target)
|
-- start rsync without any excludes
|
||||||
spawn(event, config.rsync.binary,
|
log(
|
||||||
|
'Normal',
|
||||||
|
'recursive startup rsync: ',
|
||||||
|
config.source,
|
||||||
|
' -> ',
|
||||||
|
config.target
|
||||||
|
)
|
||||||
|
|
||||||
|
spawn(
|
||||||
|
event,
|
||||||
|
config.rsync.binary,
|
||||||
delete,
|
delete,
|
||||||
config.rsync._computed,
|
config.rsync._computed,
|
||||||
'-r',
|
'-r',
|
||||||
config.source,
|
config.source,
|
||||||
config.target)
|
config.target
|
||||||
|
)
|
||||||
|
|
||||||
else
|
else
|
||||||
|
-- start rsync providing an exclude list
|
||||||
|
-- on stdin
|
||||||
local exS = table.concat( excludes, '\n' )
|
local exS = table.concat( excludes, '\n' )
|
||||||
log('Normal', 'recursive startup rsync: ',config.source,
|
|
||||||
' -> ',config.target,' excluding\n',exS)
|
log(
|
||||||
spawn(event, config.rsync.binary,
|
'Normal',
|
||||||
|
'recursive startup rsync: ',
|
||||||
|
config.source,
|
||||||
|
' -> ',
|
||||||
|
config.target,
|
||||||
|
' excluding\n',
|
||||||
|
exS
|
||||||
|
)
|
||||||
|
|
||||||
|
spawn(
|
||||||
|
event,
|
||||||
|
config.rsync.binary,
|
||||||
'<', exS,
|
'<', exS,
|
||||||
'--exclude-from=-',
|
'--exclude-from=-',
|
||||||
delete,
|
delete,
|
||||||
config.rsync._computed,
|
config.rsync._computed,
|
||||||
'-r',
|
'-r',
|
||||||
config.source,
|
config.source,
|
||||||
config.target)
|
config.target
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end,
|
|
||||||
|
|
||||||
-----
|
|
||||||
-- Checks the configuration.
|
|
||||||
--
|
--
|
||||||
prepare = function(config)
|
-- Prepares and checks a syncs configuration on startup.
|
||||||
|
--
|
||||||
|
default.rsync.prepare = function( config )
|
||||||
|
|
||||||
if not config.target then
|
if not config.target then
|
||||||
error('default.rsync needs "target" configured', 4)
|
error(
|
||||||
|
'default.rsync needs "target" configured',
|
||||||
|
4
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if config.rsyncOps then
|
if config.rsyncOps then
|
||||||
error('"rsyncOps" is outdated please use the new rsync = { ... } syntax.', 4)
|
error(
|
||||||
|
'"rsyncOps" is outdated please use the new rsync = { ... } syntax.',
|
||||||
|
4
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if config.rsyncOpts and config.rsync._extra then
|
if config.rsyncOpts and config.rsync._extra then
|
||||||
@ -173,12 +247,10 @@ default.rsync = {
|
|||||||
if config.rsyncOpts then
|
if config.rsyncOpts then
|
||||||
log(
|
log(
|
||||||
'Warn',
|
'Warn',
|
||||||
'"rsyncOpts" is outdated. Please use the new rsync = { ... } syntax."',
|
'"rsyncOpts" is outdated. Please use the new rsync = { ... } syntax."'
|
||||||
event.etype, '"'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
config.rsync._extra = config.rsyncOpts
|
config.rsync._extra = config.rsyncOpts
|
||||||
|
|
||||||
config.rsyncOpts = nil
|
config.rsyncOpts = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -194,16 +266,14 @@ default.rsync = {
|
|||||||
if config.rsyncBinary then
|
if config.rsyncBinary then
|
||||||
log(
|
log(
|
||||||
'Warn',
|
'Warn',
|
||||||
'"rsyncBinary" is outdated. Please use the new rsync = { ... } syntax."',
|
'"rsyncBinary" is outdated. Please use the new rsync = { ... } syntax."'
|
||||||
event.etype, '"'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
config.rsync.binary = config.rsyncBinary
|
config.rsync.binary = config.rsyncBinary
|
||||||
|
|
||||||
config.rsyncOpts = nil
|
config.rsyncOpts = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- checks if the _computed argument does not exist already
|
-- checks if the _computed argument exists already
|
||||||
if config.rsync._computed then
|
if config.rsync._computed then
|
||||||
error(
|
error(
|
||||||
'please do not use the internal rsync._computed parameter',
|
'please do not use the internal rsync._computed parameter',
|
||||||
@ -245,35 +315,34 @@ default.rsync = {
|
|||||||
if string.sub(config.target, -1) ~= '/' then
|
if string.sub(config.target, -1) ~= '/' then
|
||||||
config.target = config.target..'/'
|
config.target = config.target..'/'
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
|
|
||||||
-----
|
--
|
||||||
-- rsync uses default collect
|
-- rsync uses default collect
|
||||||
----
|
--
|
||||||
|
|
||||||
-----
|
--
|
||||||
-- By default do deletes.
|
-- By default do deletes.
|
||||||
--
|
--
|
||||||
delete = true,
|
default.rsync.delete = true
|
||||||
|
|
||||||
-----
|
|
||||||
-- Calls rsync with this default short opts.
|
|
||||||
--
|
--
|
||||||
rsync = {
|
-- Calls rsync with this default options
|
||||||
|
--
|
||||||
|
default.rsync.rsync = {
|
||||||
-- The rsync binary to be called.
|
-- The rsync binary to be called.
|
||||||
binary = '/usr/bin/rsync',
|
binary = '/usr/bin/rsync',
|
||||||
links = true,
|
links = true,
|
||||||
times = true,
|
times = true,
|
||||||
protectArgs = true
|
protectArgs = true
|
||||||
},
|
}
|
||||||
|
|
||||||
-----
|
--
|
||||||
-- Exit codes for rsync.
|
-- Exit codes for rsync.
|
||||||
--
|
--
|
||||||
exitcodes = default.rsyncExitCodes,
|
default.rsync.exitcodes = default.rsyncExitCodes
|
||||||
|
|
||||||
-----
|
--
|
||||||
-- Default delay
|
-- Default delay
|
||||||
--
|
--
|
||||||
delay = 15,
|
default.rsync.delay = 15
|
||||||
}
|
|
||||||
|
@ -8,7 +8,9 @@
|
|||||||
-- Authors: Axel Kittenberger <axkibe@gmail.com>
|
-- Authors: Axel Kittenberger <axkibe@gmail.com>
|
||||||
--============================================================================
|
--============================================================================
|
||||||
|
|
||||||
if default then error('default already loaded'); end
|
if default then
|
||||||
|
error('default already loaded')
|
||||||
|
end
|
||||||
|
|
||||||
default = {
|
default = {
|
||||||
-----
|
-----
|
||||||
|
Loading…
x
Reference in New Issue
Block a user