This commit is contained in:
Axel Kittenberger 2010-10-18 09:02:51 +00:00
parent 88cb57bc58
commit 2529d36c3f
2 changed files with 59 additions and 50 deletions

View File

@ -73,15 +73,22 @@ s_malloc(size_t size)
return r; return r;
} }
/*****************************************************************************
* Library calls for lsyncd.lua
*
* These are as minimal as possible glues to the operating system needed for
* lsyncd operation.
*
****************************************************************************/
/** /**
* Adds an inotify watch * Adds an inotify watch
* *
* @param dir path to directory * @param dir (Lua stack) path to directory
* @return numeric watch descriptor * @return (Lua stack) numeric watch descriptor
*/ */
static int static int
add_watch(lua_State *L) l_add_watch(lua_State *L)
{ {
const char *path = luaL_checkstring(L, 1); const char *path = luaL_checkstring(L, 1);
lua_Integer wd = inotify_add_watch(inotify_fd, path, standard_event_mask); lua_Integer wd = inotify_add_watch(inotify_fd, path, standard_event_mask);
@ -89,12 +96,15 @@ add_watch(lua_State *L)
return 1; return 1;
} }
/** /**
* Executes a subprocess * Executes a subprocess. Does not wait for it to return.
*
* @param (Lua stack) Path to binary to call
* @params (Lua stack) list of string as arguments
* @return (Lua stack) the pid on success, 0 on failure.
*/ */
static int static int
exec(lua_State *L) l_exec(lua_State *L)
{ {
const char *binary = luaL_checkstring(L, 1); const char *binary = luaL_checkstring(L, 1);
int argc = lua_gettop(L) - 1; int argc = lua_gettop(L) - 1;
@ -103,9 +113,8 @@ exec(lua_State *L)
char const **argv = s_calloc(argc + 2, sizeof(char *)); char const **argv = s_calloc(argc + 2, sizeof(char *));
argv[0] = binary; argv[0] = binary;
for(i = 1; i < argc; i++) { for(i = 1; i <= argc; i++) {
argv[i] = luaL_checkstring(L, i + 1); argv[i] = luaL_checkstring(L, i + 1);
printf("%d.%s\n", i, argv[i]);
} }
argv[i] = NULL; argv[i] = NULL;
@ -127,7 +136,8 @@ exec(lua_State *L)
} }
free(argv); free(argv);
return 0; lua_pushnumber(L, pid);
return 1;
} }
@ -138,7 +148,7 @@ exec(lua_State *L)
* @return absolute path of directory * @return absolute path of directory
*/ */
static int static int
real_dir(lua_State *L) l_real_dir(lua_State *L)
{ {
luaL_Buffer b; luaL_Buffer b;
char *cbuf; char *cbuf;
@ -174,7 +184,7 @@ real_dir(lua_State *L)
* Dumps the LUA stack. For debugging purposes. * Dumps the LUA stack. For debugging purposes.
*/ */
static int static int
stackdump(lua_State* l) l_stackdump(lua_State* l)
{ {
int i; int i;
int top = lua_gettop(l); int top = lua_gettop(l);
@ -204,11 +214,11 @@ stackdump(lua_State* l)
/** /**
* Reads the directories sub directories. * Reads the directories sub directories.
* *
* @param absolute path to directory. * @param (Lua stack) absolute path to directory.
* @return a table of directory names. * @return (Lua stack) a table of directory names.
*/ */
static int static int
sub_dirs (lua_State *L) l_sub_dirs (lua_State *L)
{ {
const char * dirname = luaL_checkstring(L, 1); const char * dirname = luaL_checkstring(L, 1);
DIR *d; DIR *d;
@ -247,7 +257,7 @@ sub_dirs (lua_State *L)
continue; continue;
} }
/* add this to the LUA table */ /* add this to the Lua table */
lua_pushnumber(L, idx++); lua_pushnumber(L, idx++);
lua_pushstring(L, de->d_name); lua_pushstring(L, de->d_name);
lua_settable(L, -3); lua_settable(L, -3);
@ -255,12 +265,16 @@ sub_dirs (lua_State *L)
return 1; return 1;
} }
/*****************************************************************************
* Lsyncd Core
****************************************************************************/
static const luaL_reg lsyncdlib[] = { static const luaL_reg lsyncdlib[] = {
{"add_watch", add_watch}, {"add_watch", l_add_watch},
{"exec", exec}, {"exec", l_exec},
{"real_dir", real_dir}, {"real_dir", l_real_dir},
{"stackdump", stackdump}, {"stackdump", l_stackdump},
{"sub_dirs", sub_dirs}, {"sub_dirs", l_sub_dirs},
{NULL, NULL} {NULL, NULL}
}; };
@ -301,12 +315,15 @@ main(int argc, char *argv[])
} }
/* initialize */ /* initialize */
/* lua code will set configuration and add watches */
lua_getglobal(L, "lsyncd_initialize"); lua_getglobal(L, "lsyncd_initialize");
lua_call(L, 0, 0); lua_call(L, 0, 0);
/* startup */ /* startup */
/* lua code will perform startup calls like recursive rsync */
lua_getglobal(L, "startup"); lua_getglobal(L, "startup");
lua_call(L, 0, 0); lua_call(L, 0, 0);
l_stackdump(L);
/* cleanup */ /* cleanup */
close(inotify_fd); close(inotify_fd);

View File

@ -1,31 +1,10 @@
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- lsyncd library functions implemented in C -- lsyncd runner implemented in LUA
------------------------------------------------------------------------------
----
-- real_dir(dir)
--
-- Converts a relative directory path to an absolute.
--
-- @param dir a relative path to directory
-- @return absolute path of directory
--
----
--
-- sub_dirs(dir)
--
-- Reads the directories sub directories.
--
-- @param dir absolute path to directory.
-- @return a table of directory names.
--
------------------------------------------------------------------------------
-- lsyncd library functions implemented in LUA
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
---- ----
-- Table of all directories to watch. -- Table of all directories to watch.
local origin = {} local origins = {}
---- ----
-- all targets -- all targets
@ -40,7 +19,7 @@ local watches = {}
-- --
-- @param sdir -- @param sdir
-- @param target -- @param target
-- @param ... --
local function attend_dir(origin, path, target) local function attend_dir(origin, path, target)
print("attending dir", origin, "+", path, "->", target.path); print("attending dir", origin, "+", path, "->", target.path);
-- actual dir = origin + path -- actual dir = origin + path
@ -77,11 +56,22 @@ end
function lsyncd_initialize() function lsyncd_initialize()
print("--- INIT ---") print("--- INIT ---")
local i, o local i, o
for i, o in ipairs(origin) do for i, o in ipairs(origins) do
print("Handling ", o.source, "->" , o.targetpath) print("Handling ", o.source, "->" , o.targetpath)
-- resolves source to be an absolute path
local src = lsyncd.real_dir(o.source)
if src == nil then
print("Cannot resovle source path: ", src)
lsyncd.terminate() -- ERRNO
end
o.source = src
-- appends the target on target lists
local target = { path = o.targetpath } local target = { path = o.targetpath }
table.insert(targets, target) table.insert(targets, target)
origin[i].target = target o.target = target
-- and add the dir watch inclusively all subdirs
attend_dir(o.source, "", target) attend_dir(o.source, "", target)
end end
end end
@ -92,10 +82,11 @@ end
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
---- ----
-- Add one directory to be watched. -- Adds one directory to be watched.
--
function add(source_dir, target_path) function add(source_dir, target_path)
local o = { source = lsyncd.real_dir(source_dir), targetpath = target_path } local o = { source = source_dir, targetpath = target_path }
table.insert(origin, o) table.insert(origins, o)
return o return o
end end
@ -107,10 +98,11 @@ end
-- --
-- User can override this function by specifing his/her own -- User can override this function by specifing his/her own
-- "startup". (and yet may still call default startup) -- "startup". (and yet may still call default startup)
--
function default_startup() function default_startup()
print("--- STARTUP ---") print("--- STARTUP ---")
local pids = { } local pids = { }
for i, o in ipairs(origin) do for i, o in ipairs(origins) do
print("/usr/bin/rsync", "-ltrs", o.source, o.targetpath) print("/usr/bin/rsync", "-ltrs", o.source, o.targetpath)
pid = lsyncd.exec("/usr/bin/rsync", "-ltrs", o.source, o.targetpath) pid = lsyncd.exec("/usr/bin/rsync", "-ltrs", o.source, o.targetpath)
print("started ", pid) print("started ", pid)