mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-06-07 19:00:51 +00:00
This commit is contained in:
parent
bebc1af090
commit
27b73038eb
|
@ -16,42 +16,39 @@ prefix = "sleep 1 && "
|
||||||
slowbash = {
|
slowbash = {
|
||||||
delay = 5,
|
delay = 5,
|
||||||
|
|
||||||
startup = function(source, target)
|
onStartup = function(config)
|
||||||
|
-- called on startup
|
||||||
|
local source = config.source
|
||||||
|
local target = config.target
|
||||||
log("Normal", "cp -r from ", source, " -> ", target)
|
log("Normal", "cp -r from ", source, " -> ", target)
|
||||||
return shell([[if [ "$(ls -A $1)" ]; then cp -r "$1"* "$2"; fi]],
|
return shell([[if [ "$(ls -A $1)" ]; then cp -r "$1"* "$2"; fi]],
|
||||||
source, target)
|
source, target)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
create = function(event)
|
onCreate = function(config, event)
|
||||||
log("Normal",
|
-- concats the source and the target with the file/dirs path and name
|
||||||
"create from ", event.sourcebasename,
|
-- basename removes the trailing '/' on dirs.
|
||||||
" -> ", event.targetbasename)
|
local source = config.source .. event.pathbasename
|
||||||
return shell(prefix..[[cp "$1" "$2"]],
|
local target = config.target .. event.pathbasename
|
||||||
event.sourcebasename, event.targetbasename)
|
log("Normal", "create from ", source, " -> ", target)
|
||||||
|
return shell(prefix..[[cp "$1" "$2"]], source, target)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
modify = function(event)
|
onModify = function(config, event)
|
||||||
log("Normal",
|
-- same game for modifies
|
||||||
"modify from ", event.sourcename,
|
local source = config.source .. event.pathbasename
|
||||||
" -> ", event.targetname)
|
local target = config.target .. event.pathbasename
|
||||||
return shell(prefix..[[cp "$1" "$2"]],
|
log("Normal", "modify from ", source, " -> ", target)
|
||||||
event.sourcebasename, event.targetbasename)
|
return shell(prefix..[[cp "$1" "$2"]], source, target)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
attrib = function(event)
|
onDelete = function(config, event)
|
||||||
-- ignore attribs
|
-- similar for deletes
|
||||||
return 0
|
local target = config.target .. event.pathbasename
|
||||||
|
log("Normal", "delete ", target)
|
||||||
|
return shell(prefix..[[rm "$1"]], target)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
delete = function(event)
|
|
||||||
log("Normal", "delete "..event.targetbasename)
|
|
||||||
return shell(prefix..[[rm "$1"]], event.targetbasename)
|
|
||||||
end,
|
|
||||||
|
|
||||||
-- move = function(event)
|
|
||||||
-- end,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sync{slowbash, source="s", target="d/"}
|
sync{slowbash, source="s", target="d/"}
|
||||||
|
|
||||||
|
|
79
lsyncd.lua
79
lsyncd.lua
|
@ -218,15 +218,14 @@ end)()
|
||||||
--
|
--
|
||||||
local Origin = (function()
|
local Origin = (function()
|
||||||
----
|
----
|
||||||
-- TODO
|
-- Creates a new origin
|
||||||
--
|
--
|
||||||
local function new(source, targetident, config)
|
local function new(config)
|
||||||
local o = {
|
local o = {
|
||||||
config = config,
|
config = config,
|
||||||
delays = CountArray.new(),
|
delays = CountArray.new(),
|
||||||
delayname = {},
|
delayname = {},
|
||||||
source = source,
|
source = config.source,
|
||||||
targetident = targetident,
|
|
||||||
processes = CountArray.new(),
|
processes = CountArray.new(),
|
||||||
}
|
}
|
||||||
return o
|
return o
|
||||||
|
@ -360,11 +359,11 @@ local Origins = (function()
|
||||||
end
|
end
|
||||||
config.source = real_src
|
config.source = real_src
|
||||||
|
|
||||||
if not config.action and not config.attrib and
|
if not config.onAction and not config.onAttrib and
|
||||||
not config.create and not config.modify and
|
not config.onCreate and not config.onModify and
|
||||||
not config.delete and not config.move
|
not config.onDelete and not config.onMove
|
||||||
then
|
then
|
||||||
local info = debug.getinfo(2, "Sl")
|
local info = debug.getinfo(3, "Sl")
|
||||||
log("Error", info.short_src, ":", info.currentline,
|
log("Error", info.short_src, ":", info.currentline,
|
||||||
": no actions specified, use e.g. 'config=default.rsync'.")
|
": no actions specified, use e.g. 'config=default.rsync'.")
|
||||||
terminate(-1) -- ERRNO
|
terminate(-1) -- ERRNO
|
||||||
|
@ -381,7 +380,7 @@ local Origins = (function()
|
||||||
optional("action")
|
optional("action")
|
||||||
optional("max_processes")
|
optional("max_processes")
|
||||||
optional("collapse_table")
|
optional("collapse_table")
|
||||||
local o = Origin.new(config.source, config.target, config)
|
local o = Origin.new(config)
|
||||||
table.insert(list, o)
|
table.insert(list, o)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -546,6 +545,11 @@ end)()
|
||||||
-- lsyncd runner plugs. These functions will be called from core.
|
-- lsyncd runner plugs. These functions will be called from core.
|
||||||
--============================================================================
|
--============================================================================
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- true after lsyncd_initalized()
|
||||||
|
--
|
||||||
|
local running = false
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Called from core whenever a lua failed.
|
-- Called from core whenever a lua failed.
|
||||||
--
|
--
|
||||||
|
@ -631,41 +635,9 @@ local Inlet, inlet_control = (function()
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
source = function()
|
root = function()
|
||||||
return origin.source
|
return origin.source
|
||||||
end,
|
end,
|
||||||
|
|
||||||
sourcename = function()
|
|
||||||
return origin.source .. delay.pathname
|
|
||||||
end,
|
|
||||||
|
|
||||||
sourcebasename = function()
|
|
||||||
local pn
|
|
||||||
if string.byte(delay.pathname, -1) == 47 then
|
|
||||||
pn = string.sub(delay.pathname, 1, -1)
|
|
||||||
else
|
|
||||||
pn = delay.pathname
|
|
||||||
end
|
|
||||||
return origin.source .. pn
|
|
||||||
end,
|
|
||||||
|
|
||||||
target = function()
|
|
||||||
return origin.config.target
|
|
||||||
end,
|
|
||||||
|
|
||||||
targetname = function()
|
|
||||||
return origin.config.target .. delay.pathname
|
|
||||||
end,
|
|
||||||
|
|
||||||
targetbasename = function()
|
|
||||||
local pn
|
|
||||||
if string.byte(delay.pathname, -1) == 47 then
|
|
||||||
pn = string.sub(delay.pathname, 1, -1)
|
|
||||||
else
|
|
||||||
pn = delay.pathname
|
|
||||||
end
|
|
||||||
return origin.config.target .. pn
|
|
||||||
end,
|
|
||||||
}
|
}
|
||||||
local event_meta = {
|
local event_meta = {
|
||||||
__index = function(t, k)
|
__index = function(t, k)
|
||||||
|
@ -880,7 +852,7 @@ function lsyncd_initialize()
|
||||||
local have_startup = false
|
local have_startup = false
|
||||||
-- runs through the origins table filled by user calling directory()
|
-- runs through the origins table filled by user calling directory()
|
||||||
for _, o in Origins.iwalk() do
|
for _, o in Origins.iwalk() do
|
||||||
if o.config.startup then
|
if o.config.onStartup then
|
||||||
have_startup = true
|
have_startup = true
|
||||||
end
|
end
|
||||||
-- adds the dir watch inclusively all subdirs
|
-- adds the dir watch inclusively all subdirs
|
||||||
|
@ -888,6 +860,7 @@ function lsyncd_initialize()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- from now on use logging as configured instead of stdout/err.
|
-- from now on use logging as configured instead of stdout/err.
|
||||||
|
running = true;
|
||||||
lsyncd.configure("running");
|
lsyncd.configure("running");
|
||||||
|
|
||||||
if have_startup then
|
if have_startup then
|
||||||
|
@ -895,8 +868,8 @@ function lsyncd_initialize()
|
||||||
local pids = { }
|
local pids = { }
|
||||||
for _, o in Origins.iwalk() do
|
for _, o in Origins.iwalk() do
|
||||||
local pid
|
local pid
|
||||||
if o.config.startup then
|
if o.config.onStartup then
|
||||||
local pid = o.config.startup(o.source, o.targetident)
|
local pid = o.config.onStartup(o.config)
|
||||||
table.insert(pids, pid)
|
table.insert(pids, pid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -959,7 +932,15 @@ end
|
||||||
-- lsyncd user interface
|
-- lsyncd user interface
|
||||||
--============================================================================
|
--============================================================================
|
||||||
|
|
||||||
sync = Origins.add
|
-----
|
||||||
|
-- Main utility to create new observations.
|
||||||
|
--
|
||||||
|
function sync(opts)
|
||||||
|
if running then
|
||||||
|
error("Cannot add syncs while running!")
|
||||||
|
end
|
||||||
|
Origins.add(opts)
|
||||||
|
end
|
||||||
|
|
||||||
----
|
----
|
||||||
-- Called by core when an overflow happened.
|
-- Called by core when an overflow happened.
|
||||||
|
@ -1014,9 +995,11 @@ default = {
|
||||||
--
|
--
|
||||||
action = function(inlet)
|
action = function(inlet)
|
||||||
local event = inlet.get_event()
|
local event = inlet.get_event()
|
||||||
local func = inlet.get_config()[string.lower(event.etype)]
|
local config = inlet.get_config()
|
||||||
|
local func = config["on".. event.etype]
|
||||||
if func then
|
if func then
|
||||||
return func(event)
|
-- TODO Moves?
|
||||||
|
return func(config, event)
|
||||||
else
|
else
|
||||||
return -1
|
return -1
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user