This commit is contained in:
Axel Kittenberger 2010-11-02 21:04:01 +00:00
parent aaf24118d4
commit 5b7bffd303
3 changed files with 67 additions and 52 deletions

View File

@ -5,9 +5,9 @@
-- --
settings = { settings = {
-- logfile = "/tmp/lsyncd", -- logfile = "/tmp/lsyncd",
-- nodaemon, -- nodaemon = true,
statusfile = "/tmp/lsyncd.stat", statusfile = "/tmp/lsyncd.stat",
loglevel = DEBUG, loglevel = "Debug",
} }
------ ------
@ -18,18 +18,17 @@ 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", "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(event) modify = function(event)
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,
@ -39,7 +38,7 @@ slowbash = {
end, end,
delete = function(event) delete = function(event)
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,
@ -54,4 +53,3 @@ slowbash = {
sync{source="s", target="d/", config=slowbash} sync{source="s", target="d/", config=slowbash}

View File

@ -342,6 +342,25 @@ logstring0(enum loglevel level,
return; return;
} }
/**
* Turns a loglevel string into a loglevel enum
*
* @param index the index on lua stack where loglevel is encoded.
*/
static int
get_loglevel(lua_State *L, int index)
{
const char * lstr = luaL_checkstring(L, index);
switch (lstr[0]) {
case 'D' : return DEBUG;
case 'N' : return NORMAL;
case 'V' : return VERBOSE;
case 'E' : return ERROR;
}
printlogf(L, ERROR, "'%s' is not a valid loglevel.", lstr);
exit(-1); // ERRNO
}
/***************************************************************************** /*****************************************************************************
* Library calls for lsyncd.lua * Library calls for lsyncd.lua
@ -366,6 +385,8 @@ l_add_watch(lua_State *L)
return 1; return 1;
} }
/** /**
* Logs a message. * Logs a message.
* *
@ -378,9 +399,9 @@ l_log(lua_State *L)
/* log message */ /* log message */
const char * message; const char * message;
/* log level */ /* log level */
int level = luaL_checkinteger(L, 1); int level = get_loglevel(L, 1);
/* skips filtered messages early */ /* skips filtered messages early */
if ((level & 0x0F) < settings.loglevel) { if (level < settings.loglevel) {
return 0; return 0;
} }
@ -783,7 +804,7 @@ l_configure(lua_State *L)
} }
settings.statusfile = s_strdup(luaL_checkstring(L, 2)); settings.statusfile = s_strdup(luaL_checkstring(L, 2));
} else if (!strcmp(command, "loglevel")) { } else if (!strcmp(command, "loglevel")) {
settings.loglevel = luaL_checkinteger(L, 2); settings.loglevel = get_loglevel(L, 2);
} else if (!strcmp(command, "running")) { } 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
@ -827,20 +848,20 @@ static const luaL_reg lsyncdlib[] = {
*/ */
static void static void
printlogf(lua_State *L, printlogf(lua_State *L,
enum loglevel level, enum loglevel level,
const char *fmt, ...) const char *fmt, ...)
{ {
va_list ap; va_list ap;
/* skips filtered messages early */ /* skips filtered messages early */
if (level < settings.loglevel) { if (level < settings.loglevel) {
return; return;
} }
lua_pushcfunction(L, l_log);
lua_pushinteger(L, level | CORE);
va_start(ap, fmt); va_start(ap, fmt);
lua_pushvfstring(L, fmt, ap); lua_pushvfstring(L, fmt, ap);
va_end(ap); va_end(ap);
lua_call(L, 2, 0); logstring(level, luaL_checkstring(L, -1));
lua_pop(L, -1);
return; return;
} }
@ -1148,12 +1169,6 @@ main(int argc, char *argv[])
luaL_register(L, "lsyncd", lsyncdlib); luaL_register(L, "lsyncd", lsyncdlib);
lua_setglobal(L, "lysncd"); lua_setglobal(L, "lysncd");
/* register log levels */
lua_pushinteger(L, DEBUG); lua_setglobal(L, "DEBUG");
lua_pushinteger(L, VERBOSE); lua_setglobal(L, "VERBOSE");
lua_pushinteger(L, NORMAL); lua_setglobal(L, "NORMAL");
lua_pushinteger(L, ERROR); lua_setglobal(L, "ERROR");
/* checks if the user overrode default runner file */ /* checks if the user overrode default runner file */
if (!strcmp(argv[argp], "--runner")) { if (!strcmp(argv[argp], "--runner")) {
if (argc < 3) { if (argc < 3) {

View File

@ -18,7 +18,7 @@ if lsyncd_version then
-- checks if the runner is being loaded twice -- checks if the runner is being loaded twice
io.stderr:write( io.stderr:write(
"You cannot use the lsyncd runner as configuration file!\n") "You cannot use the lsyncd runner as configuration file!\n")
os.exit(-1) os.exit(-1) -- ERRNO
end end
lsyncd_version = "2.0beta1" lsyncd_version = "2.0beta1"
@ -225,14 +225,14 @@ end)()
-- Puts an action on the delay stack. -- Puts an action on the delay stack.
-- --
function Origin.delay(origin, ename, time, pathname, pathname2) function Origin.delay(origin, ename, time, pathname, pathname2)
log(DEBUG, "delay ", ename, " ", pathname) log("Debug", "delay ", ename, " ", pathname)
local o = origin local o = origin
local delays = o.delays local delays = o.delays
local delayname = o.delayname local delayname = o.delayname
if ename == "Move" and not o.config.move then if ename == "Move" and not o.config.move then
-- if there is no move action defined, split a move as delete/create -- if there is no move action defined, split a move as delete/create
log(DEBUG, "splitting Move into Delete & Create") log("Debug", "splitting Move into Delete & Create")
delay(o, "Delete", time, pathname, nil) delay(o, "Delete", time, pathname, nil)
delay(o, "Create", time, pathname2, nil) delay(o, "Create", time, pathname2, nil)
return return
@ -255,24 +255,24 @@ function Origin.delay(origin, ename, time, pathname, pathname2)
if newd.ename == "MoveFrom" or newd.ename == "MoveTo" or if newd.ename == "MoveFrom" or newd.ename == "MoveTo" or
oldd.ename == "MoveFrom" or oldd.ename == "MoveTo" then oldd.ename == "MoveFrom" or oldd.ename == "MoveTo" then
-- do not collapse moves -- do not collapse moves
log(NORMAL, "Not collapsing events with moves on ", pathname) log("Normal", "Not collapsing events with moves on ", pathname)
-- TODO stackinfo -- TODO stackinfo
return return
else else
local col = o.config.collapse_table[oldd.ename][newd.ename] local col = o.config.collapse_table[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 ",
oldd.ename, " on ", pathname) oldd.ename, " on ", pathname)
oldd.ename = "None" oldd.ename = "None"
return return
elseif col == 0 then elseif col == 0 then
-- events tack -- events tack
log(NORMAL, "Stacking ", newd.ename, " after ", log("Normal", "Stacking ", newd.ename, " after ",
oldd.ename, " on ", pathname) oldd.ename, " on ", pathname)
-- TODO Stack pointer -- TODO Stack pointer
else else
log(NORMAL, "Collapsing ", newd.ename, " upon ", log("Normal", "Collapsing ", newd.ename, " upon ",
oldd.ename, " to ", col, " on ", pathname) oldd.ename, " to ", col, " on ", pathname)
oldd.ename = col oldd.ename = col
return return
@ -321,7 +321,7 @@ local Origins = (function()
local function require_opt(name) local function require_opt(name)
if not config[name] then if not config[name] then
local info = debug.getinfo(3, "Sl") local info = debug.getinfo(3, "Sl")
log(ERROR, info.short_src, ":", info.currentline, log("Error", info.short_src, ":", info.currentline,
": ", name, " missing from sync.") ": ", name, " missing from sync.")
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
@ -342,7 +342,7 @@ local Origins = (function()
not config.delete and not config.move not config.delete and not config.move
then then
local info = debug.getinfo(2, "Sl") local info = debug.getinfo(2, "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
end end
@ -355,8 +355,9 @@ local Origins = (function()
config[name] = settings[name] or default[name] config[name] = settings[name] or default[name]
end end
optional("action")
optional("max_processes") optional("max_processes")
optional("collapse_actions") optional("collapse_table")
local o = Origin.new(config.source, config.target, config) local o = Origin.new(config.source, config.target, config)
table.insert(list, o) table.insert(list, o)
@ -427,7 +428,7 @@ local function inotify_watch_dir(origin, path)
local wd = lsyncd.add_watch(op); local wd = lsyncd.add_watch(op);
if wd < 0 then if wd < 0 then
-- failed adding the watch -- failed adding the watch
log(ERROR, "Failure adding watch ", op, " -> ignored ") log("Error", "Failure adding watch ", op, " -> ignored ")
return return
end end
@ -470,7 +471,7 @@ function lsyncd_collect_process(pid, exitcode)
if not delay then if not delay then
return return
end end
log(DEBUG, "collected ", pid, ": ", log("Debug", "collected ", pid, ": ",
delay.ename, " of ", origin.source, delay.pathname, delay.ename, " of ", origin.source, delay.pathname,
" = ", exitcode) " = ", exitcode)
origin.processes[pid] = nil origin.processes[pid] = nil
@ -592,7 +593,7 @@ function lsyncd_initialize(args)
for i = 1, #args do for i = 1, #args do
local a = args[i] local a = args[i]
if a:sub(1, 1) ~= "-" then if a:sub(1, 1) ~= "-" then
log(ERROR, "Unknown option ", a, log("Error", "Unknown option ", a,
". Options must start with '-' or '--'.") ". Options must start with '-' or '--'.")
os.exit(-1) -- ERRNO os.exit(-1) -- ERRNO
end end
@ -608,12 +609,13 @@ function lsyncd_initialize(args)
local configure_settings = { local configure_settings = {
loglevel = {1, loglevel = {1,
function(param) function(param)
if not (param == DEBUG or param == NORMAL or if not (param == "Debug" or param == "Normal" or
param == VERBOSE or param == ERROR) then param == "Verbose" or param == "Error") then
log(ERROR, "unknown settings.loglevel '", param, "'") log("Error", "unknown settings.loglevel '", param, "'")
terminate(-1); -- ERRNO terminate(-1); -- ERRNO
end end
end}, end
},
statusfile = {1, nil}, statusfile = {1, nil},
} }
@ -621,11 +623,11 @@ function lsyncd_initialize(args)
for c, p in pairs(settings) do for c, p in pairs(settings) do
local cs = configure_settings[c] local cs = configure_settings[c]
if not cs then if not cs then
log(ERROR, "unknown setting '", c, "'") log("Error", "unknown setting '", c, "'")
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
if cs[1] == 1 and not p then if cs[1] == 1 and not p then
log(ERROR, "setting '", c, "' needs a parameter") log("Error", "setting '", c, "' needs a parameter")
end end
-- calls the check function if its not nil -- calls the check function if its not nil
if cs[2] then if cs[2] then
@ -636,8 +638,8 @@ function lsyncd_initialize(args)
-- makes sure the user gave lsyncd anything to do -- makes sure the user gave lsyncd anything to do
if Origins.size() == 0 then if Origins.size() == 0 then
log(ERROR, "Nothing to watch!") log("Error", "Nothing to watch!")
log(ERROR, "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file."); log("Error", "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file.");
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
@ -656,7 +658,7 @@ function lsyncd_initialize(args)
lsyncd.configure("running"); lsyncd.configure("running");
if have_startup then if have_startup then
log(NORMAL, "--- startup ---") log("Normal", "--- startup ---")
local pids = { } local pids = { }
for _, o in Origins.iwalk() do for _, o in Origins.iwalk() do
local pid local pid
@ -666,10 +668,10 @@ function lsyncd_initialize(args)
end end
end end
lsyncd.wait_pids(pids, "startup_collector") lsyncd.wait_pids(pids, "startup_collector")
log(NORMAL, "- Entering normal operation with ", log("Normal", "- Entering normal operation with ",
inotifies.size, " monitored directories -") inotifies.size, " monitored directories -")
else else
log(NORMAL, "- Warmstart into normal operation with ", log("Normal", "- Warmstart into normal operation with ",
inotifies.size, " monitored directories -") inotifies.size, " monitored directories -")
end end
end end
@ -715,16 +717,16 @@ function lsyncd_inotify_event(ename, wd, isdir, time, filename, filename2)
ftype = "file" ftype = "file"
end end
if filename2 then if filename2 then
log(DEBUG, "got event ", ename, " of ", ftype, " ", filename, log("Debug", "got event ", ename, " of ", ftype, " ", filename,
" to ", filename2) " to ", filename2)
else else
log(DEBUG, "got event ", ename, " of ", ftype, " ", filename) log("Debug", "got event ", ename, " of ", ftype, " ", filename)
end end
-- looks up the watch descriptor id -- looks up the watch descriptor id
local ilist = inotifies[wd] local ilist = inotifies[wd]
if not ilist then if not ilist then
log(NORMAL, "event belongs to unknown or deleted watch descriptor.") log("Normal", "event belongs to unknown or deleted watch descriptor.")
return return
end end
@ -757,7 +759,7 @@ end
-- --
function startup_collector(pid, exitcode) function startup_collector(pid, exitcode)
if exitcode ~= 0 then if exitcode ~= 0 then
log(ERROR, "Startup process", pid, " failed") log("Error", "Startup process", pid, " failed")
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
return 0 return 0
@ -774,7 +776,7 @@ sync = Origins.add
-- Called by core when an overflow happened. -- Called by core when an overflow happened.
-- --
function default_overflow() function default_overflow()
log(ERROR, "--- OVERFLOW on inotify event queue ---") log("Error", "--- OVERFLOW on inotify event queue ---")
terminate(-1) -- TODO reset instead. terminate(-1) -- TODO reset instead.
end end
overflow = default_overflow overflow = default_overflow
@ -797,7 +799,7 @@ local default_rsync = {
---- ----
-- Called for every sync/target pair on startup -- Called for every sync/target pair on startup
startup = function(source, target) startup = function(source, target)
log(NORMAL, "startup recursive rsync: ", source, " -> ", target) log("Normal", "startup recursive rsync: ", source, " -> ", target)
return exec("/usr/bin/rsync", "-ltrs", return exec("/usr/bin/rsync", "-ltrs",
source, target) source, target)
end, end,