mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-12-13 14:43:09 +00:00
This commit is contained in:
parent
6e779475b4
commit
610a77b1ec
20
lsyncd.c
20
lsyncd.c
@ -104,18 +104,10 @@ static struct settings {
|
|||||||
* LOG_ERROR errors only
|
* LOG_ERROR errors only
|
||||||
*/
|
*/
|
||||||
int log_level;
|
int log_level;
|
||||||
|
|
||||||
/**
|
|
||||||
* lsyncd will periodically write its status in this
|
|
||||||
* file if configured so. (for special observing only)
|
|
||||||
*/
|
|
||||||
char * statusfile;
|
|
||||||
|
|
||||||
} settings = {
|
} settings = {
|
||||||
.log_file = NULL,
|
.log_file = NULL,
|
||||||
.log_syslog = false,
|
.log_syslog = false,
|
||||||
.log_level = 0,
|
.log_level = 0,
|
||||||
.statusfile = NULL,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -496,7 +488,7 @@ l_log(lua_State *L)
|
|||||||
* @return the true if time1 <= time2
|
* @return the true if time1 <= time2
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
l_before_eq(lua_State *L)
|
l_is_before_eq(lua_State *L)
|
||||||
{
|
{
|
||||||
clock_t t1 = (clock_t) luaL_checkinteger(L, 1);
|
clock_t t1 = (clock_t) luaL_checkinteger(L, 1);
|
||||||
clock_t t2 = (clock_t) luaL_checkinteger(L, 2);
|
clock_t t2 = (clock_t) luaL_checkinteger(L, 2);
|
||||||
@ -892,13 +884,7 @@ static int
|
|||||||
l_configure(lua_State *L)
|
l_configure(lua_State *L)
|
||||||
{
|
{
|
||||||
const char * command = luaL_checkstring(L, 1);
|
const char * command = luaL_checkstring(L, 1);
|
||||||
if (!strcmp(command, "statusfile")) {
|
if (!strcmp(command, "running")) {
|
||||||
/* configures the status file lsyncd will dump its status to */
|
|
||||||
if (settings.statusfile) {
|
|
||||||
free(settings.statusfile);
|
|
||||||
}
|
|
||||||
settings.statusfile = s_strdup(luaL_checkstring(L, 2));
|
|
||||||
} else if (!strcmp(command, "running")) {
|
|
||||||
/* set by runner after first initialize
|
/* set by runner after first initialize
|
||||||
* from this on log to configurated log end instead of
|
* from this on log to configurated log end instead of
|
||||||
* stdout/stderr */
|
* stdout/stderr */
|
||||||
@ -916,7 +902,7 @@ l_configure(lua_State *L)
|
|||||||
static const luaL_reg lsyncdlib[] = {
|
static const luaL_reg lsyncdlib[] = {
|
||||||
{"add_watch", l_add_watch },
|
{"add_watch", l_add_watch },
|
||||||
{"addto_clock", l_addto_clock },
|
{"addto_clock", l_addto_clock },
|
||||||
{"before_eq", l_before_eq },
|
{"is_before_eq", l_is_before_eq },
|
||||||
{"configure", l_configure },
|
{"configure", l_configure },
|
||||||
{"earlier", l_earlier },
|
{"earlier", l_earlier },
|
||||||
{"exec", l_exec },
|
{"exec", l_exec },
|
||||||
|
113
lsyncd.lua
113
lsyncd.lua
@ -174,7 +174,7 @@ end
|
|||||||
-- Locks globals,
|
-- Locks globals,
|
||||||
-- no more globals can be created
|
-- no more globals can be created
|
||||||
--
|
--
|
||||||
local function globals_lock()
|
local function globalsLock()
|
||||||
local t = _G
|
local t = _G
|
||||||
local mt = getmetatable(t) or {}
|
local mt = getmetatable(t) or {}
|
||||||
mt.__index = function(t, k)
|
mt.__index = function(t, k)
|
||||||
@ -196,7 +196,7 @@ local function globals_lock()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Holds information about a delayed event for one origin/target.
|
-- Holds information about a delayed event for one Sync.
|
||||||
--
|
--
|
||||||
-- valid stati are:
|
-- valid stati are:
|
||||||
-- delay
|
-- delay
|
||||||
@ -226,7 +226,7 @@ end)()
|
|||||||
--
|
--
|
||||||
local Sync = (function()
|
local Sync = (function()
|
||||||
----
|
----
|
||||||
-- Creates a new origin
|
-- Creates a new Sync
|
||||||
--
|
--
|
||||||
local function new(config)
|
local function new(config)
|
||||||
local o = {
|
local o = {
|
||||||
@ -280,7 +280,7 @@ function Sync.delay(self, ename, time, pathname, pathname2)
|
|||||||
-- TODO stackinfo
|
-- TODO stackinfo
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local col = self.config.collapse_table[oldd.ename][newd.ename]
|
local col = self.config.collapseTable[oldd.ename][newd.ename]
|
||||||
if col == -1 then
|
if col == -1 then
|
||||||
-- events cancel each other
|
-- events cancel each other
|
||||||
log("Normal", "Nullfication: ", newd.ename, " after ",
|
log("Normal", "Nullfication: ", newd.ename, " after ",
|
||||||
@ -312,7 +312,7 @@ end
|
|||||||
-- It maintains all configured directories to be synced.
|
-- It maintains all configured directories to be synced.
|
||||||
--
|
--
|
||||||
local Syncs = (function()
|
local Syncs = (function()
|
||||||
-- the list of all origins
|
-- the list of all syncs
|
||||||
local list = Array.new()
|
local list = Array.new()
|
||||||
|
|
||||||
-----
|
-----
|
||||||
@ -395,12 +395,12 @@ local Syncs = (function()
|
|||||||
table.insert(list, s)
|
table.insert(list, s)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- allows to walk through all origins
|
-- allows to walk through all syncs
|
||||||
local function iwalk()
|
local function iwalk()
|
||||||
return ipairs(list)
|
return ipairs(list)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns the number of origins
|
-- returns the number of syncs
|
||||||
local size = function()
|
local size = function()
|
||||||
return #list
|
return #list
|
||||||
end
|
end
|
||||||
@ -420,7 +420,7 @@ end)()
|
|||||||
local Inotifies = (function()
|
local Inotifies = (function()
|
||||||
-----
|
-----
|
||||||
-- A list indexed by inotifies watch descriptor.
|
-- A list indexed by inotifies watch descriptor.
|
||||||
-- Contains a list of all origins observing this directory
|
-- Contains a list of all syncs observing this directory
|
||||||
-- (directly or by recurse)
|
-- (directly or by recurse)
|
||||||
local wdlist = CountArray.new()
|
local wdlist = CountArray.new()
|
||||||
|
|
||||||
@ -430,11 +430,11 @@ local Inotifies = (function()
|
|||||||
-- @param root+path directory to observe
|
-- @param root+path directory to observe
|
||||||
-- @param recurse true if recursing into subdirs or
|
-- @param recurse true if recursing into subdirs or
|
||||||
-- the relative path to root for recursed inotifies
|
-- the relative path to root for recursed inotifies
|
||||||
-- @param origin link to the observer to be notified.
|
-- @param sync link to the observer to be notified.
|
||||||
-- Note: Inotifies should handle this opaquely
|
-- Note: Inotifies should handle this opaquely
|
||||||
local function add(root, path, recurse, origin)
|
local function add(root, path, recurse, sync)
|
||||||
log("Function",
|
log("Function",
|
||||||
"Inotifies.add(",root,", ",path,", ",recurse,", ",origin,")")
|
"Inotifies.add(",root,", ",path,", ",recurse,", ",sync,")")
|
||||||
-- registers watch
|
-- registers watch
|
||||||
local wd = lsyncd.add_watch(root .. path);
|
local wd = lsyncd.add_watch(root .. path);
|
||||||
if wd < 0 then
|
if wd < 0 then
|
||||||
@ -449,14 +449,14 @@ local Inotifies = (function()
|
|||||||
root = root,
|
root = root,
|
||||||
path = path,
|
path = path,
|
||||||
recurse = recurse,
|
recurse = recurse,
|
||||||
origin = origin
|
sync = sync
|
||||||
})
|
})
|
||||||
|
|
||||||
-- registers and adds watches for all subdirectories
|
-- registers and adds watches for all subdirectories
|
||||||
if recurse then
|
if recurse then
|
||||||
local subdirs = lsyncd.sub_dirs(root .. path)
|
local subdirs = lsyncd.sub_dirs(root .. path)
|
||||||
for _, dirname in ipairs(subdirs) do
|
for _, dirname in ipairs(subdirs) do
|
||||||
add(root, path..dirname.."/", true, origin)
|
add(root, path..dirname.."/", true, sync)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -502,11 +502,11 @@ local Inotifies = (function()
|
|||||||
if filename2 then
|
if filename2 then
|
||||||
pathname2 = inotify.path..filename2
|
pathname2 = inotify.path..filename2
|
||||||
end
|
end
|
||||||
Sync.delay(inotify.origin, ename, time, pathname, pathname2)
|
Sync.delay(inotify.sync, ename, time, pathname, pathname2)
|
||||||
-- 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, pathname, true, inotify.origin)
|
add(inotify.root, pathname, true, inotify.sync)
|
||||||
elseif ename == "Delete" then
|
elseif ename == "Delete" then
|
||||||
-- TODO
|
-- TODO
|
||||||
end
|
end
|
||||||
@ -578,11 +578,11 @@ end
|
|||||||
--
|
--
|
||||||
function lsyncd_collect_process(pid, exitcode)
|
function lsyncd_collect_process(pid, exitcode)
|
||||||
local delay = nil
|
local delay = nil
|
||||||
local origin = nil
|
local sync = nil
|
||||||
for _, s in Syncs.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
delay = s.processes[pid]
|
delay = s.processes[pid]
|
||||||
if delay then
|
if delay then
|
||||||
origin = s
|
sync = s
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -590,8 +590,8 @@ function lsyncd_collect_process(pid, exitcode)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
log("Debug", "collected ",pid, ": ",delay.ename," of ",
|
log("Debug", "collected ",pid, ": ",delay.ename," of ",
|
||||||
origin.source,delay.pathname," = ",exitcode)
|
sync.source,delay.pathname," = ",exitcode)
|
||||||
origin.processes[pid] = nil
|
sync.processes[pid] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
@ -602,8 +602,8 @@ end
|
|||||||
--
|
--
|
||||||
local Inlet, inlet_control = (function()
|
local Inlet, inlet_control = (function()
|
||||||
-- lua runner controlled variables
|
-- lua runner controlled variables
|
||||||
local origin = true
|
local sync = true
|
||||||
local delay = true
|
local delay = true
|
||||||
|
|
||||||
-- event to be passed to the user
|
-- event to be passed to the user
|
||||||
local event = {}
|
local event = {}
|
||||||
@ -613,7 +613,7 @@ local Inlet, inlet_control = (function()
|
|||||||
--
|
--
|
||||||
local event_fields = {
|
local event_fields = {
|
||||||
config = function()
|
config = function()
|
||||||
return origin.config
|
return sync.config
|
||||||
end,
|
end,
|
||||||
|
|
||||||
etype = function()
|
etype = function()
|
||||||
@ -641,7 +641,7 @@ local Inlet, inlet_control = (function()
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
root = function()
|
root = function()
|
||||||
return origin.source
|
return sync.source
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
local event_meta = {
|
local event_meta = {
|
||||||
@ -659,9 +659,9 @@ local Inlet, inlet_control = (function()
|
|||||||
-- Interface for lsyncd runner to control what
|
-- Interface for lsyncd runner to control what
|
||||||
-- the inlet will present the user.
|
-- the inlet will present the user.
|
||||||
--
|
--
|
||||||
local function control(set_origin, set_delay)
|
local function control(set_sync, set_delay)
|
||||||
origin = set_origin
|
sync = set_sync
|
||||||
delay = set_delay
|
delay = set_delay
|
||||||
end
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
@ -677,7 +677,7 @@ local Inlet, inlet_control = (function()
|
|||||||
--
|
--
|
||||||
local function get_config()
|
local function get_config()
|
||||||
-- TODO give a readonly handler only.
|
-- TODO give a readonly handler only.
|
||||||
return origin.config
|
return sync.config
|
||||||
end
|
end
|
||||||
|
|
||||||
------
|
------
|
||||||
@ -689,17 +689,17 @@ end)()
|
|||||||
-- TODO
|
-- TODO
|
||||||
--
|
--
|
||||||
--
|
--
|
||||||
local function invoke_action(origin, delay)
|
local function invoke_action(sync, delay)
|
||||||
if delay.ename == "None" then
|
if delay.ename == "None" then
|
||||||
-- a removed action
|
-- a removed action
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
inlet_control(origin, delay)
|
inlet_control(sync, delay)
|
||||||
local pid = origin.config.action(Inlet)
|
local pid = sync.config.action(Inlet)
|
||||||
if pid and pid > 0 then
|
if pid and pid > 0 then
|
||||||
delay.status = "active"
|
delay.status = "active"
|
||||||
origin.processes[pid] = delay
|
sync.processes[pid] = delay
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -733,16 +733,16 @@ local StatusFile = (function()
|
|||||||
-- some logic to not write too often
|
-- some logic to not write too often
|
||||||
if settings.statusIntervall > 0 then
|
if settings.statusIntervall > 0 then
|
||||||
-- already waiting
|
-- already waiting
|
||||||
if alarm and lsyncd.earlier(now, alarm) then
|
if alarm and lsyncd.is_before_eq(now, alarm) then
|
||||||
log("Statusfile", "waiting")
|
log("Statusfile", "waiting(",now," < ",alarm,")")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- when a next Write will be possible
|
-- determines when a next write will be possible
|
||||||
if not alarm then
|
if not alarm then
|
||||||
local nextWrite = lastWritten and
|
local nextWrite = lastWritten and
|
||||||
lsyncd.addto_clock(now, settings.statusIntervall)
|
lsyncd.addto_clock(now, settings.statusIntervall)
|
||||||
if nextWrite and lsyncd.earlier(now, nextWrite) then
|
if nextWrite and lsyncd.is_before_eq(now, nextWrite) then
|
||||||
log("Statusfile", "setting alarm", nextWrite)
|
log("Statusfile", "setting alarm: ", nextWrite)
|
||||||
alarm = nextWrite
|
alarm = nextWrite
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -784,10 +784,10 @@ function lsyncd_cycle(now)
|
|||||||
StatusFile.write(now)
|
StatusFile.write(now)
|
||||||
end
|
end
|
||||||
for _, s in Syncs.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
if s.processes:size() < s.config.max_processes then
|
if s.processes:size() < s.config.maxProcesses then
|
||||||
local delays = s.delays
|
local delays = s.delays
|
||||||
local d = delays[1]
|
local d = delays[1]
|
||||||
if d and lsyncd.before_eq(d.alarm, now) then
|
if d and lsyncd.is_before_eq(d.alarm, now) then
|
||||||
invoke_action(s, d)
|
invoke_action(s, d)
|
||||||
table.remove(delays, 1)
|
table.remove(delays, 1)
|
||||||
s.delayname[d.pathname] = nil -- TODO grab from stack
|
s.delayname[d.pathname] = nil -- TODO grab from stack
|
||||||
@ -883,28 +883,11 @@ function lsyncd_initialize()
|
|||||||
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
|
||||||
globals_lock()
|
globalsLock()
|
||||||
|
|
||||||
-- all valid settings, first value is 1 if it needs a parameter
|
-- TODO
|
||||||
local configure_settings = {
|
if settings.statusIntervall == nil then
|
||||||
statusfile = {1, nil},
|
settings.statusIntervall = default.statusIntervall
|
||||||
}
|
|
||||||
|
|
||||||
-- check all entries in the settings table
|
|
||||||
for c, p in pairs(settings) do
|
|
||||||
local cs = configure_settings[c]
|
|
||||||
if not cs then
|
|
||||||
log("Error", "unknown setting '", c, "'")
|
|
||||||
terminate(-1) -- ERRNO
|
|
||||||
end
|
|
||||||
if cs[1] == 1 and not p then
|
|
||||||
log("Error", "setting '", c, "' needs a parameter")
|
|
||||||
end
|
|
||||||
-- calls the check function if its not nil
|
|
||||||
if cs[2] then
|
|
||||||
cs[2](p)
|
|
||||||
end
|
|
||||||
lsyncd.configure(c, p)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- makes sure the user gave lsyncd anything to do
|
-- makes sure the user gave lsyncd anything to do
|
||||||
@ -914,15 +897,15 @@ function lsyncd_initialize()
|
|||||||
terminate(-1) -- ERRNO
|
terminate(-1) -- ERRNO
|
||||||
end
|
end
|
||||||
|
|
||||||
-- set to true if at least one origin has a startup function
|
-- set to true if at least one sync has a startup function
|
||||||
local have_startup = false
|
local have_startup = false
|
||||||
-- runs through the origins table filled by user calling directory()
|
-- runs through the syncs table filled by user calling directory()
|
||||||
for _, s in Syncs.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
if s.config.onStartup then
|
if s.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
|
||||||
Inotifies.add(s.source, "", true, o)
|
Inotifies.add(s.source, "", true, s)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- from now on use logging as configured instead of stdout/err.
|
-- from now on use logging as configured instead of stdout/err.
|
||||||
@ -959,7 +942,7 @@ function lsyncd_get_alarm()
|
|||||||
for _, s in Syncs.iwalk() do
|
for _, s in Syncs.iwalk() do
|
||||||
-- TODO better handling of stati.
|
-- TODO better handling of stati.
|
||||||
if s.delays[1] and
|
if s.delays[1] and
|
||||||
s.processes:size() < s.config.max_processes then
|
s.processes:size() < s.config.maxProcesses then
|
||||||
if alarm then
|
if alarm then
|
||||||
alarm = lsyncd.earlier(alarm, s.delays[1].alarm)
|
alarm = lsyncd.earlier(alarm, s.delays[1].alarm)
|
||||||
else
|
else
|
||||||
@ -969,7 +952,7 @@ function lsyncd_get_alarm()
|
|||||||
end
|
end
|
||||||
local sa = StatusFile.getAlarm()
|
local sa = StatusFile.getAlarm()
|
||||||
if sa then
|
if sa then
|
||||||
alarm = lsyncd.earlier(sa, alarm)
|
alarm = (alarm and lsyncd.earlier(sa, alarm)) or sa
|
||||||
end
|
end
|
||||||
|
|
||||||
log("Debug", "lysncd_get_alarm returns: ",alarm)
|
log("Debug", "lysncd_get_alarm returns: ",alarm)
|
||||||
@ -1081,7 +1064,7 @@ default = {
|
|||||||
------
|
------
|
||||||
-- Minimum seconds between two writes of a status file.
|
-- Minimum seconds between two writes of a status file.
|
||||||
--
|
--
|
||||||
statusIntervall = 60,
|
statusIntervall = 10,
|
||||||
|
|
||||||
------
|
------
|
||||||
-- TODO
|
-- TODO
|
||||||
|
Loading…
Reference in New Issue
Block a user