diff --git a/lsyncd-conf.lua b/lsyncd-conf.lua index 7357639..3374123 100644 --- a/lsyncd-conf.lua +++ b/lsyncd-conf.lua @@ -4,24 +4,65 @@ -- TODO documentation- -- settings = { - logfile = "/tmp/lsyncd", +-- logfile = "/tmp/lsyncd", nodaemon, + loglevel = DEBUG, } + +------ +-- for testing purposes +-- +slower = "sleep 10" +slowbash = { + startup = function(source, target) + log(NORMAL, "cp -r from "..source.." -> "..target) + return 0; + end, + + create = function(source, path, name, target) + local src = source..path..name + log(NORMAL, "create from "..source..path..name.." -> "..target..path..name) + return exec("/bin/bash", "-c", slower.."&& cp '$1' '$2'", "/bin/bash", + source..path..name, target..path..name) + end, + + attrib = function(source, path, name, target) + -- ignore attribs + return 0 + end, + + delete = function(source, path, name, target) + log(NORMAL, "delete "..target..path..name) + return exec("/bin/bash", "-c", slower.." && rm $1", "/bin/bash", + target..path..name) + end, + + move = function(source, path, name, destpath, destname, target) + log(NORMAL, "move from " .. destination .. "/" .. path) + return exec("/bin/bash", "-c", "sleep " .. slowsec .. " && rm $1 $2", "/bin/bash", + source .. "/" .. path, target .. "/" .. path) + end, +} + +----- +-- lsyncd classic - sync with rsync +-- +-- All functions return the pid of a spawned process +-- or 0 if they didn't exec something. rsync = { - default = function(source, path, target) + ---- + -- Called for every sync/target pair on startup + startup = function(source, target) + log(NORMAL, "startup recursive rsync: " .. source .. " -> " .. target) + return exec("/usr/bin/rsync", "-ltrs", source, target) + end, + + default = function(source, target, path) return exec("/usr/bin/rsync", "--delete", "-ltds", source .. "/" .. path, target .. "/" .. path) end } -directory("s", "d", rsync) +sync(slowbash, "s", "d") ----- --- Called for every source .. target pair on startup --- Returns the pid of a spawned process --- Return 0 if you dont exec something. -function startup_action(source, target) - log(NORMAL, "startup recursive rsync: " .. source .. " -> " .. target) - return exec("/usr/bin/rsync", "-ltrs", source, target) -end diff --git a/lsyncd.c b/lsyncd.c index c6e7a82..951f9e5 100644 --- a/lsyncd.c +++ b/lsyncd.c @@ -1017,10 +1017,6 @@ main(int argc, char *argv[]) lua_getglobal(L, "lsyncd_initialize"); lua_call(L, 0, 0); - /* let lua code will perform startup calls like recursive rsync */ - lua_getglobal(L, "startup"); - lua_call(L, 0, 0); - masterloop(L); /* cleanup */ diff --git a/lsyncd.lua b/lsyncd.lua index 8b4918c..5c82672 100644 --- a/lsyncd.lua +++ b/lsyncd.lua @@ -22,7 +22,8 @@ lsyncd_version = "2.0b1" ---- -- Shortcuts (which user is supposed to be able to use them as well) -log = lsyncd.log +log = lsyncd.log +exec = lsyncd.exec ---- -- Table of all directories to watch, @@ -147,6 +148,18 @@ function lsyncd_initialize() -- and add the dir watch inclusively all subdirs attend_dir(asrc, "", target, nil) end + + log(NORMAL, "--- startup ---") + local pids = { } + local pid + for i, o in ipairs(origins) do + if (o.actions.startup ~= nil) then + pid = o.actions.startup(o.source, o.targetident) + end + table.insert(pids, pid) + end + lsyncd.wait_pids(pids, "startup_collector") + log(NORMAL, "--- Entering normal operation with " .. #watches .. " monitored directories ---") end ---- @@ -239,15 +252,18 @@ end ------------------------------------------------------------------------------ ---- --- Adds one directory to be watched. +-- Adds one directory (incl. subdir) to be synchronized. -- Users primary configuration device. -- -- @param TODO -- -function directory(source_dir, target_identifier, actions) - local o = { source = source_dir, targetident = target_identifier, actions = actions} +function sync(actions, source_dir, target_identifier) + local o = { actions = actions, + source = source_dir, + targetident = target_identifier, + } table.insert(origins, o) - return o + return end ---- @@ -259,29 +275,5 @@ function default_overflow() end overflow = default_overflow ------ --- Called by core after initialization. --- --- Default function will start an simultanous action for every --- source -> destination pair. And waits for these processes to finish --- --- The user can override this function by specifing his/her own --- "startup". (and yet may still call default startup) --- -function default_startup() - log(NORMAL, "--- startup ---") - local pids = { } - for i, o in ipairs(origins) do - startup_action(o.source, o.targetident) - table.insert(pids, pid) - end - lsyncd.wait_pids(pids, "startup_collector") - log(NORMAL, "--- Entering normal operation with " .. #watches .. " monitored directories ---") -end -startup = default_startup ----- --- other functions the user might want to use -exec = lsyncd.exec -