This commit is contained in:
Axel Kittenberger 2010-11-05 13:34:02 +00:00
parent eef4c73bcb
commit e2f58951ad
2 changed files with 114 additions and 33 deletions

View File

@ -17,18 +17,25 @@ slowbash = {
delay = 5, delay = 5,
startup = function(source, target) startup = function(source, 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]], source, target) return shell([[if [ "$(ls -A $1)" ]; then cp -r "$1"* "$2"; fi]],
source, target)
end, end,
create = function(event) create = function(event)
log("Normal", "create from "..event.spath.." -> "..event.tpath) log("Normal",
return shell(prefix..[[cp "$1" "$2"]], event.spath, event.tpath) "create from ", event.sourcebasename,
" -> ", event.targetbasename)
return shell(prefix..[[cp "$1" "$2"]],
event.sourcebasename, event.targetbasename)
end, end,
modify = function(event) modify = function(event)
log("Normal", "modify from "..event.spath.." -> "..event.tpath) log("Normal",
return shell(prefix..[[cp "$1" "$2"]], event.spath, event.tpath) "modify from ", event.sourcename,
" -> ", event.targetname)
return shell(prefix..[[cp "$1" "$2"]],
event.sourcebasename, event.targetbasename)
end, end,
attrib = function(event) attrib = function(event)
@ -37,15 +44,11 @@ slowbash = {
end, end,
delete = function(event) delete = function(event)
log("Normal", "delete "..event.tpath) log("Normal", "delete "..event.targetbasename)
return exec(prefix..[[rm "$1"]], event.tpath) return shell(prefix..[[rm "$1"]], event.targetbasename)
end, end,
-- move = function(source, path, name, destpath, destname, target) -- move = function(event)
-- log(NORMAL, "move from " .. destination .. "/" .. path)
-- return exec("/bin/bash", "-c", "sleep " .. slowsec .. " && rm $1 $2", "/bin/bash",
-- source .. "/" .. path, target .. "/" .. path)
-- return 0
-- end, -- end,
} }

View File

@ -26,7 +26,6 @@ lsyncd_version = "2.0beta1"
-- Shortcuts (which user is supposed to be able to use them as well) -- Shortcuts (which user is supposed to be able to use them as well)
-- --
log = lsyncd.log log = lsyncd.log
exec = lsyncd.exec
terminate = lsyncd.terminate terminate = lsyncd.terminate
--============================================================================ --============================================================================
@ -172,7 +171,7 @@ local function globals_lock()
local mt = getmetatable(t) or {} local mt = getmetatable(t) or {}
mt.__index = function(t, k) mt.__index = function(t, k)
if (k~="_" and string.sub(k, 1, 2) ~= "__") then if (k~="_" and string.sub(k, 1, 2) ~= "__") then
error("Access of non-existing global.", 2) error("Access of non-existing global '"..k.."'", 2)
else else
rawget(t, k) rawget(t, k)
end end
@ -474,21 +473,23 @@ local Inotifies = (function()
local ftype; local ftype;
if isdir then if isdir then
ftype = "directory" ftype = "directory"
else filename = filename .. "/"
ftype = "file" if filename2 then
filename2 = filename2 .. "/"
end
end end
if filename2 then if filename2 then
log("Debug", "got event ", ename, " of ", ftype, " ", filename, log("Inotify", "got event ", ename, " ", filename,
" to ", filename2) " to ", filename2)
else else
log("Debug", "got event ", ename, " of ", ftype, " ", filename) log("Inotify", "got event ", ename, " ", filename)
end end
local ilist = wdlist[wd] local ilist = wdlist[wd]
-- looks up the watch descriptor id -- looks up the watch descriptor id
if not ilist then if not ilist then
-- this is normal in case of deleted subdirs -- this is normal in case of deleted subdirs
log("Normal", "event belongs to unknown/deleted watch descriptor.") log("Inotify", "event belongs to unknown watch descriptor.")
return return
end end
@ -503,9 +504,9 @@ local Inotifies = (function()
-- adds subdirs for new directories -- adds subdirs for new directories
if inotify.recurse and isdir then if inotify.recurse and isdir then
if ename == "Create" then if ename == "Create" then
add(inotify.root, inotify.origin, add(inotify.root, inotify.origin, pathname)
inotify.path.."/"..filename) elseif ename == "Delete" then
-- TODO remove / -- TODO
end end
end end
end end
@ -593,33 +594,106 @@ end
-- hidden from the user. -- hidden from the user.
-- --
local Inlet, inlet_control = (function() local Inlet, inlet_control = (function()
-- lua runner controlled variables
local origin = true local origin = true
local delay = true local delay = true
----- -- event to be passed to the user
local event = {}
-- TODO -- TODO
local event_fields = {
config = function()
return origin.config
end,
etype = function()
return delay.ename
end,
name = function()
error("not implemented")
end,
basename = function()
error("not implemented")
return string.match(delay.pathname, "[^/]+[/]?$")
end,
pathname = function()
return string.match(delay.pathname, "[^/]+[/]?$")
end,
pathbasename = function()
if string.byte(delay.pathname, -1) == 47 then
return string.sub(delay.pathname, 1, -1)
else
return delay.pathname
end
end,
source = function()
return origin.source
end,
sourcename = function()
return origin.source .. delay.pathname
end,
sourcebasename = function()
error("not implemented")
end,
target = function()
return origin.config.target
end,
targetname = function()
return origin.config.target .. delay.pathname
end,
sourcebasename = function()
error("not implemented")
end,
}
local event_meta = {
__index = function(t, k)
local f=event_fields[k]
if not f then
error("event does not have field '"..k.."'", 2)
end
return f()
end
}
setmetatable(event, event_meta)
-----
-- Interface for lsyncd runner to control what
-- the inlet will present the user.
--
local function control(set_origin, set_delay) local function control(set_origin, set_delay)
origin = set_origin origin = set_origin
delay = set_delay delay = set_delay
end end
----- -----
-- TODO -- Gets the next event from queue.
--
local function get_event() local function get_event()
return { -- TODO actually aquire here
spath = origin.source .. delay.pathname, return event
tpath = origin.targetident .. delay.pathname,
ename = delay.ename
}
end end
------ ------
-- TODO -- Returns the configuration table specified by sync{}
--
local function get_config() local function get_config()
-- TODO give a readonly handler only. -- TODO give a readonly handler only.
return origin.config return origin.config
end end
------
-- public interface -- public interface
return {get_event = get_event, get_config = get_config}, control return {get_event = get_event, get_config = get_config}, control
end)() end)()
@ -890,7 +964,11 @@ overflow = default_overflow
-- Spawns a child process using bash. -- Spawns a child process using bash.
-- --
function shell(command, ...) function shell(command, ...)
return exec("/bin/sh", "-c", command, "/bin/sh", ...) return lsyncd.exec("/bin/sh", "-c", command, "/bin/sh", ...)
end
function exec(...)
return lsyncd.exec(...)
end end
--============================================================================ --============================================================================
@ -926,7 +1004,7 @@ 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.ename)] local func = inlet.get_config()[string.lower(event.etype)]
if func then if func then
return func(event) return func(event)
else else