mirror of
https://github.com/octoleo/lsyncd.git
synced 2025-01-06 00:30:46 +00:00
This commit is contained in:
parent
d37006c8eb
commit
2ec192cdbc
@ -11,7 +11,7 @@ settings = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
------
|
------
|
||||||
-- for testing purposes
|
-- for testing purposes. uses bash command to hold local dirs in sync.
|
||||||
--
|
--
|
||||||
prefix = "sleep 1 && "
|
prefix = "sleep 1 && "
|
||||||
slowbash = {
|
slowbash = {
|
||||||
@ -22,25 +22,25 @@ slowbash = {
|
|||||||
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(self, unit)
|
create = function(self, events)
|
||||||
local event = unit:nextevent()
|
local event = events:nextevent()
|
||||||
log(NORMAL, "create from "..event.spath.." -> "..event.tpath)
|
log(NORMAL, "create from "..event.spath.." -> "..event.tpath)
|
||||||
return shell(prefix..[[cp "$1" "$2"]], event.spath, event.tpath)
|
return shell(prefix..[[cp "$1" "$2"]], event.spath, event.tpath)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
modify = function(self, unit)
|
modify = function(self, events)
|
||||||
local event = unit:nextevent()
|
local event = events:nextevent()
|
||||||
log(NORMAL, "modify from "..event.spath.." -> "..event.tpath)
|
log(NORMAL, "modify from "..event.spath.." -> "..event.tpath)
|
||||||
return shell(prefix..[[cp "$1" "$2"]], event.spath, event.tpath)
|
return shell(prefix..[[cp "$1" "$2"]], event.spath, event.tpath)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
attrib = function(self, unit)
|
attrib = function(self, events)
|
||||||
-- ignore attribs
|
-- ignore attribs
|
||||||
return 0
|
return 0
|
||||||
end,
|
end,
|
||||||
|
|
||||||
delete = function(self, unit)
|
delete = function(self, events)
|
||||||
local event = unit:nextevent()
|
local event = events:nextevent()
|
||||||
log(NORMAL, "delete "..event.tpath)
|
log(NORMAL, "delete "..event.tpath)
|
||||||
return exec(prefix..[[rm "$1"]], event.tpath)
|
return exec(prefix..[[rm "$1"]], event.tpath)
|
||||||
end,
|
end,
|
||||||
|
96
lsyncd.lua
96
lsyncd.lua
@ -58,27 +58,23 @@ local meta_check_array = {
|
|||||||
--
|
--
|
||||||
local meta_check_count_array = {
|
local meta_check_count_array = {
|
||||||
__index = function(t, k)
|
__index = function(t, k)
|
||||||
--TODO if k == size then
|
|
||||||
-- return rawget(t, "size")
|
|
||||||
-- end
|
|
||||||
if type(k) ~= "number" then
|
if type(k) ~= "number" then
|
||||||
error("This table is an array and must have numeric keys", 2)
|
error("This table is an array and must have numeric keys", 2)
|
||||||
end
|
end
|
||||||
return rawget(t, "mt")[k]
|
return t.nt[k]
|
||||||
end,
|
end,
|
||||||
|
|
||||||
__newindex = function(t, k, v)
|
__newindex = function(t, k, v)
|
||||||
if type(k) ~= "number" then
|
if type(k) ~= "number" then
|
||||||
error("This table is an array and must have numeric keys", 2)
|
error("This table is an array and must have numeric keys", 2)
|
||||||
end
|
end
|
||||||
local mt = rawget(t, "mt")
|
local vb = t.nt[k]
|
||||||
local vb = mt[k]
|
|
||||||
if v and not vb then
|
if v and not vb then
|
||||||
rawset(t, "size", rawget(t, "size") + 1)
|
t.size = t.size + 1
|
||||||
elseif not v and vb then
|
elseif not v and vb then
|
||||||
rawset(t, "size", rawget(t, "size") - 1)
|
t.size = t.size - 1
|
||||||
end
|
end
|
||||||
mt[k] = v
|
t.nt[k] = v
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +123,7 @@ end
|
|||||||
-- which counts the number of entries
|
-- which counts the number of entries
|
||||||
--
|
--
|
||||||
local function new_count_array()
|
local function new_count_array()
|
||||||
local t = { size = 0, mt = {} }
|
local t = { size = 0, nt = {} }
|
||||||
setmetatable(t, meta_check_count_array)
|
setmetatable(t, meta_check_count_array)
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
@ -146,42 +142,24 @@ local function set_prototype(t, prototype)
|
|||||||
setmetatable(t, meta_check_prototype)
|
setmetatable(t, meta_check_prototype)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
----
|
||||||
-- ?
|
-- Locks globals,
|
||||||
local function lock_new_index(t, k, v)
|
-- no more globals can be created
|
||||||
if (k~="_" and string.sub(k,1,2) ~= "__") then
|
--
|
||||||
GLOBAL_unlock(_G)
|
local function global_lock()
|
||||||
error("Lsyncd does not allow GLOBALS to be created on the fly." ..
|
local t = _G
|
||||||
"Declare '" ..k.."' local or declare global on load.", 2)
|
local mt = getmetatable(t) or {}
|
||||||
else
|
mt.__newindex = function(t, k, v)
|
||||||
rawset(t, k, v)
|
if (k~="_" and string.sub(k, 1, 2) ~= "__") then
|
||||||
|
error("Lsyncd does not allow GLOBALS to be created on the fly." ..
|
||||||
|
"Declare '" ..k.."' local or declare global on load.", 2)
|
||||||
|
else
|
||||||
|
rawset(t, k, v)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
----
|
|
||||||
-- Locks a table
|
|
||||||
local function GLOBAL_lock(t)
|
|
||||||
local mt = getmetatable(t) or {}
|
|
||||||
mt.__newindex = lock_new_index
|
|
||||||
setmetatable(t, mt)
|
setmetatable(t, mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
|
||||||
-- ?
|
|
||||||
local function unlock_new_index(t, k, v)
|
|
||||||
rawset(t, k, v)
|
|
||||||
end
|
|
||||||
|
|
||||||
----
|
|
||||||
-- Unlocks a table
|
|
||||||
---
|
|
||||||
local function GLOBAL_unlock(t)
|
|
||||||
local mt = getmetatable(t) or {}
|
|
||||||
mt.__newindex = unlock_new_index
|
|
||||||
setmetatable(t, mt)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--============================================================================
|
--============================================================================
|
||||||
-- Lsyncd globals
|
-- Lsyncd globals
|
||||||
@ -403,16 +381,24 @@ function lsyncd_collect_process(pid, exitcode)
|
|||||||
end
|
end
|
||||||
|
|
||||||
------
|
------
|
||||||
-- TODO
|
-- Hidden key for lsyncd.lua internal variables not ment for
|
||||||
|
-- the user to see
|
||||||
--
|
--
|
||||||
local unit = {
|
local hk = {}
|
||||||
lsyncd_origin = true,
|
|
||||||
lsyncd_delay = true,
|
|
||||||
|
|
||||||
nextevent = function(self)
|
------
|
||||||
|
--- TODO
|
||||||
|
local events = {
|
||||||
|
[hk] = {
|
||||||
|
origin = true,
|
||||||
|
delay = true,
|
||||||
|
},
|
||||||
|
|
||||||
|
nextevent = function(self)
|
||||||
|
local h = self[hk]
|
||||||
return {
|
return {
|
||||||
spath = self.lsyncd_origin.source..self.lsyncd_delay.pathname,
|
spath = h.origin.source .. h.delay.pathname,
|
||||||
tpath = self.lsyncd_origin.targetident..self.lsyncd_delay.pathname,
|
tpath = h.origin.targetident .. h.delay.pathname,
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
@ -443,9 +429,9 @@ local function invoke_action(origin, delay)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if func then
|
if func then
|
||||||
unit.lsyncd_origin = origin
|
events[hk].origin = origin
|
||||||
unit.lsyncd_delay = delay
|
events[hk].delay = delay
|
||||||
local pid = func(actions, unit)
|
local pid = func(actions, events)
|
||||||
if pid and pid > 0 then
|
if pid and pid > 0 then
|
||||||
local process = {origin = origin,
|
local process = {origin = origin,
|
||||||
delay = delay
|
delay = delay
|
||||||
@ -465,7 +451,7 @@ function lsyncd_status_report(fd)
|
|||||||
local w = lsyncd.writefd
|
local w = lsyncd.writefd
|
||||||
w(fd, "Lsyncd status report at "..os.date().."\n\n")
|
w(fd, "Lsyncd status report at "..os.date().."\n\n")
|
||||||
w(fd, "Watching "..watches.size.." directories\n")
|
w(fd, "Watching "..watches.size.." directories\n")
|
||||||
for i, v in pairs(watches.mt) do
|
for i, v in pairs(watches.nt) do
|
||||||
w(fd, " "..i..": ")
|
w(fd, " "..i..": ")
|
||||||
if i ~= v.wd then
|
if i ~= v.wd then
|
||||||
w(fd, "[Error: wd/v.wd "..i.."~="..v.wd.."]")
|
w(fd, "[Error: wd/v.wd "..i.."~="..v.wd.."]")
|
||||||
@ -522,7 +508,7 @@ function lsyncd_initialize(args)
|
|||||||
settings = settings or {}
|
settings = settings or {}
|
||||||
|
|
||||||
-- From this point on, no globals may be created anymore
|
-- From this point on, no globals may be created anymore
|
||||||
GLOBAL_lock(_G)
|
global_lock()
|
||||||
|
|
||||||
-- parses all arguments
|
-- parses all arguments
|
||||||
for i = 1, #args do
|
for i = 1, #args do
|
||||||
@ -537,7 +523,7 @@ function lsyncd_initialize(args)
|
|||||||
else
|
else
|
||||||
a = a:sub(2)
|
a = a:sub(2)
|
||||||
end
|
end
|
||||||
--TOTO
|
--TODO
|
||||||
end
|
end
|
||||||
|
|
||||||
-- all valid settings, first value is 1 if it needs a parameter
|
-- all valid settings, first value is 1 if it needs a parameter
|
||||||
|
Loading…
Reference in New Issue
Block a user