mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-12-13 14:43:09 +00:00
This commit is contained in:
parent
88cb57bc58
commit
2529d36c3f
57
lsyncd.c
57
lsyncd.c
@ -73,15 +73,22 @@ s_malloc(size_t size)
|
||||
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
|
||||
*
|
||||
* @param dir path to directory
|
||||
* @return numeric watch descriptor
|
||||
* @param dir (Lua stack) path to directory
|
||||
* @return (Lua stack) numeric watch descriptor
|
||||
*/
|
||||
static int
|
||||
add_watch(lua_State *L)
|
||||
l_add_watch(lua_State *L)
|
||||
{
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
lua_Integer wd = inotify_add_watch(inotify_fd, path, standard_event_mask);
|
||||
@ -89,12 +96,15 @@ add_watch(lua_State *L)
|
||||
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
|
||||
exec(lua_State *L)
|
||||
l_exec(lua_State *L)
|
||||
{
|
||||
const char *binary = luaL_checkstring(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 *));
|
||||
|
||||
argv[0] = binary;
|
||||
for(i = 1; i < argc; i++) {
|
||||
for(i = 1; i <= argc; i++) {
|
||||
argv[i] = luaL_checkstring(L, i + 1);
|
||||
printf("%d.%s\n", i, argv[i]);
|
||||
}
|
||||
argv[i] = NULL;
|
||||
|
||||
@ -127,7 +136,8 @@ exec(lua_State *L)
|
||||
}
|
||||
|
||||
free(argv);
|
||||
return 0;
|
||||
lua_pushnumber(L, pid);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -138,7 +148,7 @@ exec(lua_State *L)
|
||||
* @return absolute path of directory
|
||||
*/
|
||||
static int
|
||||
real_dir(lua_State *L)
|
||||
l_real_dir(lua_State *L)
|
||||
{
|
||||
luaL_Buffer b;
|
||||
char *cbuf;
|
||||
@ -174,7 +184,7 @@ real_dir(lua_State *L)
|
||||
* Dumps the LUA stack. For debugging purposes.
|
||||
*/
|
||||
static int
|
||||
stackdump(lua_State* l)
|
||||
l_stackdump(lua_State* l)
|
||||
{
|
||||
int i;
|
||||
int top = lua_gettop(l);
|
||||
@ -204,11 +214,11 @@ stackdump(lua_State* l)
|
||||
/**
|
||||
* Reads the directories sub directories.
|
||||
*
|
||||
* @param absolute path to directory.
|
||||
* @return a table of directory names.
|
||||
* @param (Lua stack) absolute path to directory.
|
||||
* @return (Lua stack) a table of directory names.
|
||||
*/
|
||||
static int
|
||||
sub_dirs (lua_State *L)
|
||||
l_sub_dirs (lua_State *L)
|
||||
{
|
||||
const char * dirname = luaL_checkstring(L, 1);
|
||||
DIR *d;
|
||||
@ -247,7 +257,7 @@ sub_dirs (lua_State *L)
|
||||
continue;
|
||||
}
|
||||
|
||||
/* add this to the LUA table */
|
||||
/* add this to the Lua table */
|
||||
lua_pushnumber(L, idx++);
|
||||
lua_pushstring(L, de->d_name);
|
||||
lua_settable(L, -3);
|
||||
@ -255,12 +265,16 @@ sub_dirs (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Lsyncd Core
|
||||
****************************************************************************/
|
||||
|
||||
static const luaL_reg lsyncdlib[] = {
|
||||
{"add_watch", add_watch},
|
||||
{"exec", exec},
|
||||
{"real_dir", real_dir},
|
||||
{"stackdump", stackdump},
|
||||
{"sub_dirs", sub_dirs},
|
||||
{"add_watch", l_add_watch},
|
||||
{"exec", l_exec},
|
||||
{"real_dir", l_real_dir},
|
||||
{"stackdump", l_stackdump},
|
||||
{"sub_dirs", l_sub_dirs},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -301,12 +315,15 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
/* initialize */
|
||||
/* lua code will set configuration and add watches */
|
||||
lua_getglobal(L, "lsyncd_initialize");
|
||||
lua_call(L, 0, 0);
|
||||
|
||||
/* startup */
|
||||
/* lua code will perform startup calls like recursive rsync */
|
||||
lua_getglobal(L, "startup");
|
||||
lua_call(L, 0, 0);
|
||||
l_stackdump(L);
|
||||
|
||||
/* cleanup */
|
||||
close(inotify_fd);
|
||||
|
52
lsyncd.lua
52
lsyncd.lua
@ -1,31 +1,10 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- lsyncd library functions implemented in C
|
||||
------------------------------------------------------------------------------
|
||||
----
|
||||
-- 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
|
||||
-- lsyncd runner implemented in LUA
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
----
|
||||
-- Table of all directories to watch.
|
||||
local origin = {}
|
||||
local origins = {}
|
||||
|
||||
----
|
||||
-- all targets
|
||||
@ -40,7 +19,7 @@ local watches = {}
|
||||
--
|
||||
-- @param sdir
|
||||
-- @param target
|
||||
-- @param ...
|
||||
--
|
||||
local function attend_dir(origin, path, target)
|
||||
print("attending dir", origin, "+", path, "->", target.path);
|
||||
-- actual dir = origin + path
|
||||
@ -77,11 +56,22 @@ end
|
||||
function lsyncd_initialize()
|
||||
print("--- INIT ---")
|
||||
local i, o
|
||||
for i, o in ipairs(origin) do
|
||||
for i, o in ipairs(origins) do
|
||||
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 }
|
||||
table.insert(targets, target)
|
||||
origin[i].target = target
|
||||
o.target = target
|
||||
|
||||
-- and add the dir watch inclusively all subdirs
|
||||
attend_dir(o.source, "", target)
|
||||
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)
|
||||
local o = { source = lsyncd.real_dir(source_dir), targetpath = target_path }
|
||||
table.insert(origin, o)
|
||||
local o = { source = source_dir, targetpath = target_path }
|
||||
table.insert(origins, o)
|
||||
return o
|
||||
end
|
||||
|
||||
@ -107,10 +98,11 @@ end
|
||||
--
|
||||
-- User can override this function by specifing his/her own
|
||||
-- "startup". (and yet may still call default startup)
|
||||
--
|
||||
function default_startup()
|
||||
print("--- STARTUP ---")
|
||||
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)
|
||||
pid = lsyncd.exec("/usr/bin/rsync", "-ltrs", o.source, o.targetpath)
|
||||
print("started ", pid)
|
||||
|
Loading…
Reference in New Issue
Block a user