code beautification

This commit is contained in:
Axel Kittenberger 2012-01-27 12:08:10 +01:00
parent 27c211eaa4
commit e869bbc1ca
3 changed files with 231 additions and 242 deletions

View File

@ -24,7 +24,7 @@
* in them. Since there is a single buffer, a slow subscriber can cause it to * in them. Since there is a single buffer, a slow subscriber can cause it to
* overflow. If this happens, events will be dropped for all subscribers, * overflow. If this happens, events will be dropped for all subscribers,
* including Spotlight. Consequently, Spotlight may need to look at the entire * including Spotlight. Consequently, Spotlight may need to look at the entire
* volume to determine "what changed". * volume to determine "what changed".
*/ */
#include "lsyncd.h" #include "lsyncd.h"
@ -58,11 +58,11 @@
/* an event argument */ /* an event argument */
struct kfs_event_arg { struct kfs_event_arg {
/* argument type */ /* argument type */
u_int16_t type; u_int16_t type;
/* size of argument data that follows this field */ /* size of argument data that follows this field */
u_int16_t len; u_int16_t len;
union { union {
struct vnode *vp; struct vnode *vp;
char *str; char *str;
@ -88,7 +88,7 @@ struct kfs_event {
pid_t pid; pid_t pid;
/* event arguments */ /* event arguments */
struct kfs_event_arg* args[FSE_MAX_ARGS]; struct kfs_event_arg* args[FSE_MAX_ARGS];
}; };
/** /**
@ -121,15 +121,15 @@ static int fsevents_fd = -1;
"INT64", "INT64",
"RAW", "RAW",
"INO", "INO",
"UID", "UID",
"DEV", "DEV",
"MODE", "MODE",
"GID", "GID",
"FINFO", "FINFO",
};*/ };*/
/** /**
* The read buffer * The read buffer
*/ */
static size_t const readbuf_size = 131072; static size_t const readbuf_size = 131072;
static char * readbuf = NULL; static char * readbuf = NULL;
@ -194,19 +194,17 @@ handle_event(lua_State *L, struct kfs_event *event, ssize_t mlen)
switch(atype) { switch(atype) {
case FSE_RENAME : case FSE_RENAME :
if (path) { if (path) {
/* for move events second string is target */ // for move events second string is target
trg = (char *) &arg->data.str; trg = (char *) &arg->data.str;
} }
/* fallthrough */ // fallthrough
case FSE_CHOWN : case FSE_CHOWN :
case FSE_CONTENT_MODIFIED : case FSE_CONTENT_MODIFIED :
case FSE_CREATE_FILE : case FSE_CREATE_FILE :
case FSE_CREATE_DIR : case FSE_CREATE_DIR :
case FSE_DELETE : case FSE_DELETE :
case FSE_STAT_CHANGED : case FSE_STAT_CHANGED :
if (!path) { if (!path) path = (char *)&arg->data.str;
path = (char *)&arg->data.str;
}
break; break;
} }
break; break;
@ -249,13 +247,11 @@ handle_event(lua_State *L, struct kfs_event *event, ssize_t mlen)
if (etype) { if (etype) {
if (!path) { if (!path) {
printlogf(L, "Error", printlogf(L, "Error", "Internal fail, fsevents, no path.");
"Internal fail, fsevents, no path.");
exit(-1); exit(-1);
} }
if (isdir < 0) { if (isdir < 0) {
printlogf(L, "Error", printlogf(L, "Error", "Internal fail, fsevents, neither dir nor file.");
"Internal fail, fsevents, neither dir nor file.");
exit(-1); exit(-1);
} }
load_runner_func(L, "fsEventsEvent"); load_runner_func(L, "fsEventsEvent");
@ -268,7 +264,7 @@ handle_event(lua_State *L, struct kfs_event *event, ssize_t mlen)
} else { } else {
lua_pushnil(L); lua_pushnil(L);
} }
if (lua_pcall(L, 5, 0, -7)) { if (lua_pcall(L, 5, 0, -7)) {
exit(-1); // ERRNO exit(-1); // ERRNO
} }
@ -277,7 +273,7 @@ handle_event(lua_State *L, struct kfs_event *event, ssize_t mlen)
} }
/** /**
* Called when fsevents has something to read * Called when fsevents has something to read
*/ */
static void static void
fsevents_ready(lua_State *L, struct observance *obs) fsevents_ready(lua_State *L, struct observance *obs)
@ -286,78 +282,77 @@ fsevents_ready(lua_State *L, struct observance *obs)
logstring("Error", "Internal, fsevents_fd != ob->fd"); logstring("Error", "Internal, fsevents_fd != ob->fd");
exit(-1); // ERRNO exit(-1); // ERRNO
} }
{
ptrdiff_t len = read (fsevents_fd, readbuf, readbuf_size); ptrdiff_t len = read (fsevents_fd, readbuf, readbuf_size);
int err = errno; int err = errno;
if (len == 0) { if (len == 0) {
return;
}
if (len < 0) {
if (err == EAGAIN) {
/* nothing more */
return; return;
} else {
printlogf(L, "Error", "Read fail on fsevents");
exit(-1); // ERRNO
} }
if (len < 0) { }
if (err == EAGAIN) { {
/* nothing more */ int off = 0;
return; while (off < len && !hup && !term) {
} else { /* deals with alignment issues on 64 bit by copying data bit by bit */
printlogf(L, "Error", "Read fail on fsevents"); struct kfs_event* event = (struct kfs_event *) eventbuf;
exit(-1); // ERRNO event->type = *(int32_t*)(readbuf+off);
off += sizeof(int32_t);
event->pid = *(pid_t*)(readbuf+off);
off += sizeof(pid_t);
/* arguments */
int whichArg = 0;
int eventbufOff = sizeof(struct kfs_event);
size_t ptrSize = sizeof(void*);
if ((eventbufOff % ptrSize) != 0) {
eventbufOff += ptrSize-(eventbufOff%ptrSize);
} }
} while (off < len && whichArg < FSE_MAX_ARGS) {
{ /* assign argument pointer to eventbuf based on
int off = 0; known current offset into eventbuf */
while (off < len && !hup && !term) { uint16_t argLen = 0;
/* deals with alignment issues on 64 bit by copying data bit by bit */ event->args[whichArg] = (struct kfs_event_arg *) (eventbuf + eventbufOff);
struct kfs_event* event = (struct kfs_event *) eventbuf; /* copy type */
event->type = *(int32_t*)(readbuf+off); uint16_t argType = *(uint16_t*)(readbuf + off);
off += sizeof(int32_t); event->args[whichArg]->type = argType;
event->pid = *(pid_t*)(readbuf+off); off += sizeof(uint16_t);
off += sizeof(pid_t); if (argType == FSE_ARG_DONE) {
/* arguments */ /* done */
int whichArg = 0; break;
int eventbufOff = sizeof(struct kfs_event); } else {
size_t ptrSize = sizeof(void*); /* copy data length */
if ((eventbufOff % ptrSize) != 0) { argLen = *(uint16_t *)(readbuf + off);
eventbufOff += ptrSize-(eventbufOff%ptrSize); event->args[whichArg]->len = argLen;
}
while (off < len && whichArg < FSE_MAX_ARGS) {
/* assign argument pointer to eventbuf based on
known current offset into eventbuf */
uint16_t argLen = 0;
event->args[whichArg] = (struct kfs_event_arg *) (eventbuf + eventbufOff);
/* copy type */
uint16_t argType = *(uint16_t*)(readbuf + off);
event->args[whichArg]->type = argType;
off += sizeof(uint16_t); off += sizeof(uint16_t);
if (argType == FSE_ARG_DONE) { /* copy data */
/* done */ memcpy(&(event->args[whichArg]->data), readbuf + off, argLen);
break; off += argLen;
} else {
/* copy data length */
argLen = *(uint16_t *)(readbuf + off);
event->args[whichArg]->len = argLen;
off += sizeof(uint16_t);
/* copy data */
memcpy(&(event->args[whichArg]->data), readbuf + off, argLen);
off += argLen;
}
/* makes sure alignment is correct for 64 bit systems */
size_t argStructLen = sizeof(uint16_t) + sizeof(uint16_t);
if ((argStructLen % ptrSize) != 0) {
argStructLen += ptrSize-(argStructLen % ptrSize);
}
argStructLen += argLen;
if ((argStructLen % ptrSize) != 0) {
argStructLen += ptrSize-(argStructLen % ptrSize);
}
eventbufOff += argStructLen;
whichArg++;
} }
handle_event(L, event, len); /* makes sure alignment is correct for 64 bit systems */
size_t argStructLen = sizeof(uint16_t) + sizeof(uint16_t);
if ((argStructLen % ptrSize) != 0) {
argStructLen += ptrSize-(argStructLen % ptrSize);
}
argStructLen += argLen;
if ((argStructLen % ptrSize) != 0) {
argStructLen += ptrSize-(argStructLen % ptrSize);
}
eventbufOff += argStructLen;
whichArg++;
} }
handle_event(L, event, len);
} }
} }
} }
/** /**
* Called to close/tidy fsevents * Called to close/tidy fsevents
*/ */
static void static void
fsevents_tidy(struct observance *obs) fsevents_tidy(struct observance *obs)
@ -373,24 +368,24 @@ fsevents_tidy(struct observance *obs)
eventbuf = NULL; eventbuf = NULL;
} }
/** /**
* opens and initalizes fsevents. * opens and initalizes fsevents.
*/ */
extern void extern void
open_fsevents(lua_State *L) open_fsevents(lua_State *L)
{ {
int8_t event_list[] = { // action to take for each event int8_t event_list[] = { // action to take for each event
FSE_REPORT, /* FSE_CREATE_FILE */ FSE_REPORT, // FSE_CREATE_FILE
FSE_REPORT, /* FSE_DELETE */ FSE_REPORT, // FSE_DELETE
FSE_REPORT, /* FSE_STAT_CHANGED */ FSE_REPORT, // FSE_STAT_CHANGED
FSE_REPORT, /* FSE_RENAME */ FSE_REPORT, // FSE_RENAME
FSE_REPORT, /* FSE_CONTENT_MODIFIED */ FSE_REPORT, // FSE_CONTENT_MODIFIED
FSE_REPORT, /* FSE_EXCHANGE */ FSE_REPORT, // FSE_EXCHANGE
FSE_REPORT, /* FSE_FINDER_INFO_CHANGED */ FSE_REPORT, // FSE_FINDER_INFO_CHANGED
FSE_REPORT, /* FSE_CREATE_DIR */ FSE_REPORT, // FSE_CREATE_DIR
FSE_REPORT, /* FSE_CHOWN */ FSE_REPORT, // FSE_CHOWN
FSE_REPORT, /* FSE_XATTR_MODIFIED */ FSE_REPORT, // FSE_XATTR_MODIFIED
FSE_REPORT, /* FSE_XATTR_REMOVED */ FSE_REPORT, // FSE_XATTR_REMOVED
}; };
struct fsevent_clone_args fca = { struct fsevent_clone_args fca = {
.event_list = (int8_t *) event_list, .event_list = (int8_t *) event_list,
@ -400,35 +395,35 @@ open_fsevents(lua_State *L)
}; };
int fd = open(DEV_FSEVENTS, O_RDONLY); int fd = open(DEV_FSEVENTS, O_RDONLY);
int err = errno; int err = errno;
printlogf(L, "Warn", printlogf(L, "Warn",
"Using /dev/fsevents which is considered an OSX internal interface."); "Using /dev/fsevents which is considered an OSX internal interface.");
printlogf(L, "Warn", printlogf(L, "Warn",
"Functionality might break across OSX versions (This is for 10.5.X)"); "Functionality might break across OSX versions (This is for 10.5.X)");
printlogf(L, "Warn", printlogf(L, "Warn",
"A hanging Lsyncd might cause Spotlight/Timemachine doing extra work."); "A hanging Lsyncd might cause Spotlight/Timemachine doing extra work.");
if (fd < 0) { if (fd < 0) {
printlogf(L, "Error", printlogf(L, "Error",
"Cannot access %s monitor! (%d:%s)", "Cannot access %s monitor! (%d:%s)",
DEV_FSEVENTS, err, strerror(err)); DEV_FSEVENTS, err, strerror(err));
exit(-1); // ERRNO exit(-1); // ERRNO
} }
if (ioctl(fd, FSEVENTS_CLONE, (char *)&fca) < 0) { if (ioctl(fd, FSEVENTS_CLONE, (char *)&fca) < 0) {
printlogf(L, "Error", printlogf(L, "Error",
"Cannot control %s monitor! (%d:%s)", "Cannot control %s monitor! (%d:%s)",
DEV_FSEVENTS, errno, strerror(errno)); DEV_FSEVENTS, errno, strerror(errno));
exit(-1); // ERRNO exit(-1); // ERRNO
} }
if (readbuf) { if (readbuf) {
logstring("Error", logstring("Error",
"internal fail, inotify readbuf!=NULL in open_inotify()") "internal fail, inotify readbuf!=NULL in open_inotify()")
exit(-1); // ERRNO exit(-1); // ERRNO
} }
readbuf = s_malloc(readbuf_size); readbuf = s_malloc(readbuf_size);
eventbuf = s_malloc(eventbuf_size); eventbuf = s_malloc(eventbuf_size);
/* fd has been cloned, closes access fd */ // fd has been cloned, closes access fd
close(fd); close(fd);
close_exec_fd(fsevents_fd); close_exec_fd(fsevents_fd);
non_block_fd(fsevents_fd); non_block_fd(fsevents_fd);

View File

@ -87,11 +87,11 @@ l_addwatch(lua_State *L)
mask |= IN_MODIFY; mask |= IN_MODIFY;
} else if (!strcmp(imode, "CloseWrite after Modify")) { } else if (!strcmp(imode, "CloseWrite after Modify")) {
/* might be done in future */ /* might be done in future */
printlogf(L, "Error", printlogf(L, "Error",
"'CloseWrite after Modify' not implemented."); "'CloseWrite after Modify' not implemented.");
exit(-1); // ERRNO exit(-1); // ERRNO
} else { } else {
printlogf(L, "Error", printlogf(L, "Error",
"'%s' not a valid inotfiyMode.", imode); "'%s' not a valid inotfiyMode.", imode);
exit(-1); // ERRNO exit(-1); // ERRNO
} }
@ -101,9 +101,9 @@ l_addwatch(lua_State *L)
int wd = inotify_add_watch(inotify_fd, path, mask); int wd = inotify_add_watch(inotify_fd, path, mask);
if (wd < 0) { if (wd < 0) {
if (errno == ENOSPC) { if (errno == ENOSPC) {
printlogf(L, "Error", printlogf(L, "Error",
"Terminating since out of inotify watches."); "Terminating since out of inotify watches.");
printlogf(L, "Error", printlogf(L, "Error",
"Consider increasing /proc/sys/fs/inotify/max_user_watches"); "Consider increasing /proc/sys/fs/inotify/max_user_watches");
exit(-1); // ERRNO. exit(-1); // ERRNO.
} }
@ -118,7 +118,7 @@ l_addwatch(lua_State *L)
/** /**
* Removes an inotify watch * Removes an inotify watch
* *
* @param dir (Lua stack) numeric watch descriptor * @param dir (Lua stack) numeric watch descriptor
* @return nil * @return nil
*/ */

View File

@ -666,13 +666,13 @@ local InletFactory = (function()
sourcePath = function(event) sourcePath = function(event)
return e2s[event].source .. getPath(event) return e2s[event].source .. getPath(event)
end, end,
------ ------
-- Returns the absolute dir of the file/dir. -- Returns the absolute dir of the file/dir.
-- Includes a trailing slash. -- Includes a trailing slash.
-- --
sourcePathdir = function(event) sourcePathdir = function(event)
return e2s[event].source .. return e2s[event].source ..
(string.match(getPath(event), "^(.*/)[^/]+/?") or "") (string.match(getPath(event), "^(.*/)[^/]+/?") or "")
end, end,
@ -686,10 +686,10 @@ local InletFactory = (function()
------ ------
-- Returns the target. -- Returns the target.
-- Just for user comfort, for most case -- Just for user comfort
-- (Actually except of here, the lsyncd.runner itself --
-- does not care event about the existance of "target", -- (except here, the lsyncd.runner does not care event about the
-- this is completly up to the action scripts.) -- existance of "target", this is up to the scripts.)
-- --
target = function(event) target = function(event)
return e2s[event].config.target return e2s[event].config.target
@ -1689,7 +1689,7 @@ local Syncs = (function()
if type(config.prepare) == "function" then if type(config.prepare) == "function" then
-- explicitly gives a writeable copy of config. -- explicitly gives a writeable copy of config.
config.prepare(config) config.prepare(config)
end end
if not config["source"] then if not config["source"] then
local info = debug.getinfo(3, "Sl") local info = debug.getinfo(3, "Sl")
@ -1722,11 +1722,11 @@ local Syncs = (function()
settings = {} settings = {}
end end
local defaultValues = { local defaultValues = {
'action', 'action',
'collect', 'collect',
'init', 'init',
'maxDelays', 'maxDelays',
'maxProcesses', 'maxProcesses',
} }
for _, dn in pairs(defaultValues) do for _, dn in pairs(defaultValues) do
if config[dn] == nil then if config[dn] == nil then
@ -1735,11 +1735,9 @@ local Syncs = (function()
end end
-- the monitor to use -- the monitor to use
config.monitor = config.monitor =
settings.monitor or config.monitor or Monitors.default() settings.monitor or config.monitor or Monitors.default()
if config.monitor ~= "inotify" if config.monitor ~= "inotify" and config.monitor ~= "fsevents" then
and config.monitor ~= "fsevents"
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,
": event monitor '",config.monitor,"' unknown.") ": event monitor '",config.monitor,"' unknown.")
@ -1784,7 +1782,7 @@ local Syncs = (function()
get = get, get = get,
getRound = getRound, getRound = getRound,
concerns = concerns, concerns = concerns,
iwalk = iwalk, iwalk = iwalk,
nextRound = nextRound, nextRound = nextRound,
size = size size = size
} }
@ -1792,22 +1790,22 @@ end)()
----- -----
-- Utility function, returns the relative part of absolute path if it -- Utility function, returns the relative part of absolute path if it
-- begins with root -- begins with root
-- --
local function splitPath(path, root) local function splitPath(path, root)
local rl = #root local rlen = #root
local sp = string.sub(path, 1, rl) local sp = string.sub(path, 1, rlen)
if sp == root then if sp == root then
return string.sub(path, rl, -1) return string.sub(path, rlen, -1)
else else
return nil return nil
end end
end end
----- -----
-- Interface to inotify, watches recursively subdirs and -- Interface to inotify, watches recursively subdirs and
-- sends events. -- sends events.
-- --
-- All inotify specific implementation should be enclosed here. -- All inotify specific implementation should be enclosed here.
@ -1815,7 +1813,7 @@ end
local Inotify = (function() local Inotify = (function()
----- -----
-- A list indexed by inotifies watch descriptor yielding the -- A list indexed by inotifies watch descriptor yielding the
-- directories absolute paths. -- directories absolute paths.
-- --
local wdpaths = CountArray.new() local wdpaths = CountArray.new()
@ -1831,7 +1829,7 @@ local Inotify = (function()
-- sync is interested in. -- sync is interested in.
-- --
local syncRoots = {} local syncRoots = {}
----- -----
-- Stops watching a directory -- Stops watching a directory
-- --
@ -1842,7 +1840,7 @@ local Inotify = (function()
local function removeWatch(path, core) local function removeWatch(path, core)
local wd = pathwds[path] local wd = pathwds[path]
if not wd then if not wd then
return return
end end
if core then if core then
lsyncd.inotify.rmwatch(wd) lsyncd.inotify.rmwatch(wd)
@ -1855,13 +1853,13 @@ local Inotify = (function()
-- Adds watches for a directory (optionally) including all subdirectories. -- Adds watches for a directory (optionally) including all subdirectories.
-- --
-- @param path absolute path of directory to observe -- @param path absolute path of directory to observe
-- @param recurse true if recursing into subdirs -- @param recurse true if recursing into subdirs
-- @param raiseSync --X -- -- @param raiseSync --X --
-- raiseTime if not nil sends create Events for all files/dirs -- raiseTime if not nil sends create Events for all files/dirs
-- to this sync. -- to this sync.
-- --
local function addWatch(path, recurse, raiseSync, raiseTime) local function addWatch(path, recurse, raiseSync, raiseTime)
log("Function", log("Function",
"Inotify.addWatch(",path,", ",recurse,", ", "Inotify.addWatch(",path,", ",recurse,", ",
raiseSync,", ",raiseTime,")") raiseSync,", ",raiseTime,")")
@ -1871,7 +1869,7 @@ local Inotify = (function()
end end
-- lets the core registers watch with the kernel -- lets the core registers watch with the kernel
local wd = lsyncd.inotify.addwatch(path, local wd = lsyncd.inotify.addwatch(path,
(settings and settings.inotifyMode) or ""); (settings and settings.inotifyMode) or "");
if wd < 0 then if wd < 0 then
log("Inotify","Unable to add watch '",path,"'") log("Inotify","Unable to add watch '",path,"'")
@ -1880,7 +1878,7 @@ local Inotify = (function()
do do
-- If this wd is registered already the kernel -- If this wd is registered already the kernel
-- reused it for a new dir for a reason - old -- reused it for a new dir for a reason - old
-- dir is gone. -- dir is gone.
local op = wdpaths[wd] local op = wdpaths[wd]
if op and op ~= path then if op and op ~= path then
@ -1890,9 +1888,9 @@ local Inotify = (function()
pathwds[path] = wd pathwds[path] = wd
wdpaths[wd] = path wdpaths[wd] = path
-- registers and adds watches for all subdirectories -- registers and adds watches for all subdirectories
-- and/or raises create events for all entries -- and/or raises create events for all entries
if not recurse and not raise then if not recurse and not raise then
return return
end end
@ -1905,7 +1903,7 @@ local Inotify = (function()
if isdir then if isdir then
pd = pd .. "/" pd = pd .. "/"
end end
-- creates a Create event for entry. -- creates a Create event for entry.
if raiseSync then if raiseSync then
local relative = splitPath(pd, syncRoots[raiseSync]) local relative = splitPath(pd, syncRoots[raiseSync])
@ -1942,7 +1940,7 @@ local Inotify = (function()
-- @param isdir true if filename is a directory -- @param isdir true if filename is a directory
-- @param time time of event -- @param time time of event
-- @param filename string filename without path -- @param filename string filename without path
-- @param filename2 -- @param filename2
-- --
local function event(etype, wd, isdir, time, filename, wd2, filename2) local function event(etype, wd, isdir, time, filename, wd2, filename2)
if isdir then if isdir then
@ -1953,9 +1951,9 @@ local Inotify = (function()
end end
if filename2 then if filename2 then
log("Inotify", "got event ",etype," ",filename, log("Inotify", "got event ",etype," ",filename,
"(",wd,") to ",filename2,"(",wd2,")") "(",wd,") to ",filename2,"(",wd2,")")
else else
log("Inotify","got event ",etype," ",filename,"(",wd,")") log("Inotify","got event ",etype," ",filename,"(",wd,")")
end end
@ -1964,12 +1962,12 @@ local Inotify = (function()
if path then if path then
path = path..filename path = path..filename
end end
local path2 = wd2 and wdpaths[wd2] local path2 = wd2 and wdpaths[wd2]
if path2 and filename2 then if path2 and filename2 then
path2 = path2..filename2 path2 = path2..filename2
end end
if not path and path2 and etype =="Move" then if not path and path2 and etype =="Move" then
log("Inotify", "Move from deleted directory ",path2, log("Inotify", "Move from deleted directory ",path2,
" becomes Create.") " becomes Create.")
@ -1986,7 +1984,7 @@ local Inotify = (function()
for sync, root in pairs(syncRoots) do repeat for sync, root in pairs(syncRoots) do repeat
local relative = splitPath(path, root) local relative = splitPath(path, root)
local relative2 local relative2
if path2 then if path2 then
relative2 = splitPath(path2, root) relative2 = splitPath(path2, root)
end end
@ -1994,9 +1992,9 @@ local Inotify = (function()
-- sync is not interested in this dir -- sync is not interested in this dir
break -- continue break -- continue
end end
-- makes a copy of etype to possibly change it -- makes a copy of etype to possibly change it
local etyped = etype local etyped = etype
if etyped == 'Move' then if etyped == 'Move' then
if not relative2 then if not relative2 then
log("Normal", "Transformed Move to Create for ", log("Normal", "Transformed Move to Create for ",
@ -2011,7 +2009,7 @@ local Inotify = (function()
end end
end end
sync:delay(etyped, time, relative, relative2) sync:delay(etyped, time, relative, relative2)
if isdir then if isdir then
if etyped == "Create" then if etyped == "Create" then
addWatch(path, true, sync, time) addWatch(path, true, sync, time)
@ -2036,10 +2034,10 @@ local Inotify = (function()
end end
-- public interface -- public interface
return { return {
addSync = addSync, addSync = addSync,
event = event, event = event,
statusReport = statusReport statusReport = statusReport
} }
end)() end)()
@ -2071,47 +2069,42 @@ local Fsevents = (function()
----- -----
-- Called when any event has occured. -- Called when any event has occured.
-- --
-- @param etype "Attrib", "Mofify", "Create", "Delete", "Move") -- etype: 'Attrib', 'Mofify', 'Create', 'Delete', 'Move')
-- @param wd watch descriptor (matches lsyncd.inotifyadd()) -- isdir: true if filename is a directory
-- @param isdir true if filename is a directory -- time: time of event
-- @param time time of event -- path: path of file
-- @param filename string filename without path -- path2: path of target in case of 'Move'
-- @param filename2
-- --
local function event(etype, isdir, time, path, path2) local function event(etype, isdir, time, path, path2)
if isdir then if isdir then
path = path .. '/' path = path..'/'
if path2 then if path2 then path2 = path2..'/' end
path2 = path2 .. '/'
end
end end
log("Fsevents",etype,",",isdir,",",time,",",path,",",path2) log('Fsevents',etype,',',isdir,',',time,',',path,',',path2)
for _, s in Syncs.iwalk() do repeat for _, s in Syncs.iwalk() do repeat
local root = s.source local root = s.source
if not path:starts(root) then if not path:starts(root) then
break -- continue break -- continue
end end
local relative = splitPath(path, root) local relative = splitPath(path, root)
local relative2 local relative2
if path2 then if path2 then
relative2 = splitPath(path2, root) relative2 = splitPath(path2, root)
end end
-- makes a copy of etype to possibly change it -- possibly change etype for this iteration only
local etyped = etype local etyped = etype
if etyped == 'Move' then if etyped == 'Move' then
if not relative2 then if not relative2 then
log("Normal", "Transformed Move to Create for ", log('Normal', 'Transformed Move to Create for ', sync.config.name)
sync.config.name)
etyped = 'Create' etyped = 'Create'
elseif not relative then elseif not relative then
relative = relative2 relative = relative2
relative2 = nil relative2 = nil
log("Normal", "Transformed Move to Delete for ", log('Normal', 'Transformed Move to Delete for ', sync.config.name)
sync.config.name)
etyped = 'Delete' etyped = 'Delete'
end end
end end
@ -2127,10 +2120,10 @@ local Fsevents = (function()
end end
-- public interface -- public interface
return { return {
addSync = addSync, addSync = addSync,
event = event, event = event,
statusReport = statusReport statusReport = statusReport
} }
end)() end)()
@ -2164,7 +2157,7 @@ Monitors = (function()
-- public interface -- public interface
return { default = default, return { default = default,
list = list, list = list,
initialize = initialize initialize = initialize
} }
end)() end)()
@ -2207,7 +2200,7 @@ local functionWriter = (function()
----- -----
-- Splits a user string into its arguments -- Splits a user string into its arguments
-- --
-- @param a string where parameters are seperated by spaces. -- @param a string where parameters are seperated by spaces.
-- --
-- @return a table of arguments -- @return a table of arguments
@ -2246,19 +2239,19 @@ local functionWriter = (function()
local function translateBinary(str) local function translateBinary(str)
-- splits the string -- splits the string
local args = splitStr(str) local args = splitStr(str)
-- true if there is a second event -- true if there is a second event
local haveEvent2 = false local haveEvent2 = false
for ia, iv in ipairs(args) do for ia, iv in ipairs(args) do
-- a list of arguments this arg is being split into -- a list of arguments this arg is being split into
local a = {{true, iv}} local a = {{true, iv}}
-- goes through all translates -- goes through all translates
for _, v in ipairs(transVars) do for _, v in ipairs(transVars) do
local ai = 1 local ai = 1
while ai <= #a do while ai <= #a do
if a[ai][1] then if a[ai][1] then
local pre, post = local pre, post =
string.match(a[ai][2], "(.*)"..v[1].."(.*)") string.match(a[ai][2], "(.*)"..v[1].."(.*)")
if pre then if pre then
if v[3] > 1 then if v[3] > 1 then
@ -2287,7 +2280,7 @@ local functionWriter = (function()
end end
if v[1] then if v[1] then
as = as..'"'..v[2]..'"' as = as..'"'..v[2]..'"'
else else
as = as..v[2] as = as..v[2]
end end
first = false first = false
@ -2305,9 +2298,9 @@ local functionWriter = (function()
ft = ft .. " [[ spawns action '" .. str .. '\']])\n' ft = ft .. " [[ spawns action '" .. str .. '\']])\n'
ft = ft .. " spawn(event" ft = ft .. " spawn(event"
for _, v in ipairs(args) do for _, v in ipairs(args) do
ft = ft .. ",\n " .. v ft = ft .. ",\n " .. v
end end
ft = ft .. ")\nend" ft = ft .. ")\nend"
return ft return ft
end end
@ -2324,10 +2317,10 @@ local functionWriter = (function()
for _, v in ipairs(transVars) do for _, v in ipairs(transVars) do
local occur = false local occur = false
cmd = string.gsub(cmd, v[1], cmd = string.gsub(cmd, v[1],
function() function()
occur = true occur = true
return '"$'..argn..'"' return '"$'..argn..'"'
end) end)
lc = string.gsub(lc, v[1], ']]..'..v[2]..'..[[') lc = string.gsub(lc, v[1], ']]..'..v[2]..'..[[')
if occur then if occur then
@ -2344,11 +2337,12 @@ local functionWriter = (function()
else else
ft = "function(event, event2)\n" ft = "function(event, event2)\n"
end end
ft = ft .. ' log("Normal", "Event " .. event.etype ..\n' -- TODO do array joining instead
ft = ft .. " [[ spawns shell '" .. lc .. '\']])\n' ft = ft..' log("Normal", "Event " .. event.etype ..\n'
ft = ft .. " spawnShell(event, [[" .. cmd .. "]]" ft = ft.." [[ spawns shell '"..lc..'\']])\n'
ft = ft.." spawnShell(event, [["..cmd.. "]]"
for _, v in ipairs(args) do for _, v in ipairs(args) do
ft = ft .. ",\n " .. v ft = ft..",\n "..v
end end
ft = ft .. ")\nend" ft = ft .. ")\nend"
return ft return ft
@ -2357,13 +2351,13 @@ local functionWriter = (function()
----- -----
-- writes a lua function for a layer 3 user script. -- writes a lua function for a layer 3 user script.
local function translate(str) local function translate(str)
-- trim spaces -- trim spaces
str = string.match(str, "^%s*(.-)%s*$") str = string.match(str, "^%s*(.-)%s*$")
local ft local ft
if string.byte(str, 1, 1) == 47 then if string.byte(str, 1, 1) == 47 then
-- starts with / -- starts with /
ft = translateBinary(str) ft = translateBinary(str)
elseif string.byte(str, 1, 1) == 94 then elseif string.byte(str, 1, 1) == 94 then
-- starts with ^ -- starts with ^
ft = translateShell(str:sub(2, -1)) ft = translateShell(str:sub(2, -1))
@ -2385,7 +2379,7 @@ end)()
-- Writes a status report file at most every [statusintervall] seconds. -- Writes a status report file at most every [statusintervall] seconds.
-- --
-- --
local StatusFile = (function() local StatusFile = (function()
----- -----
-- Timestamp when the status file has been written. -- Timestamp when the status file has been written.
@ -2417,7 +2411,7 @@ local StatusFile = (function()
end end
-- determines 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 = local nextWrite =
lastWritten and timestamp + settings.statusInterval lastWritten and timestamp + settings.statusInterval
if nextWrite and timestamp < nextWrite then if nextWrite and timestamp < nextWrite then
log("Statusfile", "setting alarm: ", nextWrite) log("Statusfile", "setting alarm: ", nextWrite)
@ -2441,7 +2435,7 @@ local StatusFile = (function()
s:statusReport(f) s:statusReport(f)
f:write("\n") f:write("\n")
end end
Inotify.statusReport(f) Inotify.statusReport(f)
f:close() f:close()
end end
@ -2453,22 +2447,22 @@ end)()
------ ------
-- Lets the userscript make its own alarms. -- Lets the userscript make its own alarms.
-- --
local UserAlarms = (function() local UserAlarms = (function()
local alarms = {} local alarms = {}
----- -----
-- Calls the user function at timestamp. -- Calls the user function at timestamp.
-- --
local function alarm(timestamp, func, extra) local function alarm(timestamp, func, extra)
local idx local idx
for k, v in ipairs(alarms) do for k, v in ipairs(alarms) do
if timestamp < v.timestamp then if timestamp < v.timestamp then
idx = k idx = k
break break
end end
end end
local a = {timestamp = timestamp, local a = {timestamp = timestamp,
func = func, func = func,
extra = extra} extra = extra}
if idx then if idx then
table.insert(alarms, idx, a) table.insert(alarms, idx, a)
@ -2482,7 +2476,7 @@ local UserAlarms = (function()
-- --
local function getAlarm() local function getAlarm()
if #alarms == 0 then if #alarms == 0 then
return false return false
else else
return alarms[1].timestamp return alarms[1].timestamp
end end
@ -2505,7 +2499,7 @@ local UserAlarms = (function()
end)() end)()
--============================================================================ --============================================================================
-- lsyncd runner plugs. These functions will be called from core. -- lsyncd runner plugs. These functions will be called from core.
--============================================================================ --============================================================================
----- -----
@ -2535,17 +2529,17 @@ function runner.callError(message)
if not info then if not info then
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
log("Error", "Backtrace ", level - 1, " :", log("Error", "Backtrace ", level - 1, " :",
info.short_src, ":", info.currentline) info.short_src, ":", info.currentline)
level = level + 1 level = level + 1
end end
end end
----- -----
-- Called from code whenever a child process finished and -- Called from code whenever a child process finished and
-- zombie process was collected by core. -- zombie process was collected by core.
-- --
function runner.collectProcess(pid, exitcode) function runner.collectProcess(pid, exitcode)
processCount = processCount - 1 processCount = processCount - 1
if processCount < 0 then if processCount < 0 then
error("negative number of processes!") error("negative number of processes!")
@ -2560,7 +2554,7 @@ end
----- -----
-- Called from core everytime a masterloop cycle runs through. -- Called from core everytime a masterloop cycle runs through.
-- This happens in case of -- This happens in case of
-- * an expired alarm. -- * an expired alarm.
-- * a returned child process. -- * a returned child process.
-- * received filesystem events. -- * received filesystem events.
@ -2615,16 +2609,16 @@ function runner.help()
io.stdout:write( io.stdout:write(
[[ [[
USAGE: USAGE:
runs a config file: runs a config file:
lsyncd [OPTIONS] [CONFIG-FILE] lsyncd [OPTIONS] [CONFIG-FILE]
default rsync behaviour: default rsync behaviour:
lsyncd [OPTIONS] -rsync [SOURCE] [TARGET] lsyncd [OPTIONS] -rsync [SOURCE] [TARGET]
default rsync with mv's through ssh: default rsync with mv's through ssh:
lsyncd [OPTIONS] -rsyncssh [SOURCE] [HOST] [TARGETDIR] lsyncd [OPTIONS] -rsyncssh [SOURCE] [HOST] [TARGETDIR]
default local copying mechanisms (cp|mv|rm): default local copying mechanisms (cp|mv|rm):
lsyncd [OPTIONS] -direct [SOURCE] [TARGETDIR] lsyncd [OPTIONS] -direct [SOURCE] [TARGETDIR]
@ -2638,7 +2632,7 @@ OPTIONS:
-logfile FILE Writes log to FILE (DEFAULT: uses syslog) -logfile FILE Writes log to FILE (DEFAULT: uses syslog)
-nodaemon Does not detach and logs to stdout/stderr -nodaemon Does not detach and logs to stdout/stderr
-pidfile FILE Writes Lsyncds PID into FILE -pidfile FILE Writes Lsyncds PID into FILE
-runner FILE Loads Lsyncds lua part from FILE -runner FILE Loads Lsyncds lua part from FILE
-version Prints versions and exits -version Prints versions and exits
LICENSE: LICENSE:
@ -2650,7 +2644,7 @@ SEE:
]]) ]])
-- --
-- -monitor NAME Uses operating systems event montior NAME -- -monitor NAME Uses operating systems event montior NAME
-- (inotify/fanotify/fsevents) -- (inotify/fanotify/fsevents)
os.exit(-1) -- ERRNO os.exit(-1) -- ERRNO
@ -2674,11 +2668,11 @@ function runner.configure(args, monitors)
-- a list of all valid --options -- a list of all valid --options
-- first paramter is number of options -- first paramter is number of options
-- if < 0 the function checks existance -- if < 0 the function checks existance
-- second paramter is function to call when in args -- second paramter is function to call when in args
-- --
local options = { local options = {
-- log is handled by core already. -- log is handled by core already.
delay = delay =
{1, function(secs) {1, function(secs)
clSettings.delay = secs clSettings.delay = secs
end}, end},
@ -2686,13 +2680,13 @@ function runner.configure(args, monitors)
{0, function() {0, function()
clSettings.insist = true clSettings.insist = true
end}, end},
log = log =
{1, nil}, {1, nil},
logfile = logfile =
{1, function(file) {1, function(file)
clSettings.logfile = file clSettings.logfile = file
end}, end},
monitor = monitor =
{-1, function(monitor) {-1, function(monitor)
if not monitor then if not monitor then
io.stdout:write("This Lsyncd supports these monitors:\n") io.stdout:write("This Lsyncd supports these monitors:\n")
@ -2705,26 +2699,26 @@ function runner.configure(args, monitors)
clSettings.monitor=monitor clSettings.monitor=monitor
end end
end}, end},
nodaemon = nodaemon =
{0, function() {0, function()
clSettings.nodaemon = true clSettings.nodaemon = true
end}, end},
pidfile = pidfile =
{1, function(file) {1, function(file)
clSettings.pidfile=file clSettings.pidfile=file
end}, end},
rsync = rsync =
{2, function(src, trg) {2, function(src, trg)
clSettings.syncs = clSettings.syncs or {} clSettings.syncs = clSettings.syncs or {}
table.insert(clSettings.syncs, {"rsync", src, trg}) table.insert(clSettings.syncs, {"rsync", src, trg})
end}, end},
rsyncssh = rsyncssh =
{3, function(src, host, tdir) {3, function(src, host, tdir)
clSettings.syncs = clSettings.syncs or {} clSettings.syncs = clSettings.syncs or {}
table.insert(clSettings.syncs, {"rsyncssh", src, host, tdir}) table.insert(clSettings.syncs, {"rsyncssh", src, host, tdir})
end}, end},
direct = direct =
{2, function(src, trg) {2, function(src, trg)
clSettings.syncs = clSettings.syncs or {} clSettings.syncs = clSettings.syncs or {}
table.insert(clSettings.syncs, {"direct", src, trg}) table.insert(clSettings.syncs, {"direct", src, trg})
end}, end},
@ -2776,7 +2770,7 @@ function runner.configure(args, monitors)
if clSettings.syncs then if clSettings.syncs then
if #nonopts ~= 0 then if #nonopts ~= 0 then
log("Error", log("Error",
"There cannot be command line default syncs with a config file.") "There cannot be command line default syncs with a config file.")
os.exit(-1) -- ERRNO os.exit(-1) -- ERRNO
end end
@ -2785,7 +2779,7 @@ function runner.configure(args, monitors)
runner.help(args[0]) runner.help(args[0])
elseif #nonopts == 1 then elseif #nonopts == 1 then
return nonopts[1] return nonopts[1]
else else
log("Error", "There can only be one config file in command line.") log("Error", "There can only be one config file in command line.")
os.exit(-1) -- ERRNO os.exit(-1) -- ERRNO
end end
@ -2798,11 +2792,11 @@ end
-- --
-- @firstTime true the first time Lsyncd startup, false on resets -- @firstTime true the first time Lsyncd startup, false on resets
-- due to HUP signal or monitor queue OVERFLOW. -- due to HUP signal or monitor queue OVERFLOW.
-- --
function runner.initialize(firstTime) function runner.initialize(firstTime)
-- creates settings if user didnt -- creates settings if user didnt
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
lockGlobals() lockGlobals()
@ -2864,12 +2858,12 @@ function runner.initialize(firstTime)
end end
----- -----
-- transfers some defaults to settings -- transfers some defaults to settings
if settings.statusInterval == nil then if settings.statusInterval == nil then
settings.statusInterval = default.statusInterval settings.statusInterval = default.statusInterval
end end
-- makes sure the user gave Lsyncd anything to do -- makes sure the user gave Lsyncd anything to do
if Syncs.size() == 0 then if Syncs.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.");
@ -2879,12 +2873,12 @@ function runner.initialize(firstTime)
-- from now on use logging as configured instead of stdout/err. -- from now on use logging as configured instead of stdout/err.
lsyncdStatus = "run"; lsyncdStatus = "run";
lsyncd.configure("running"); lsyncd.configure("running");
local ufuncs = { local ufuncs = {
"onAttrib", "onCreate", "onDelete", "onAttrib", "onCreate", "onDelete",
"onModify", "onMove", "onStartup" "onModify", "onMove", "onStartup"
} }
-- translates layer 3 scripts -- translates layer 3 scripts
for _, s in Syncs.iwalk() do for _, s in Syncs.iwalk() do
-- checks if any user functions is a layer 3 string. -- checks if any user functions is a layer 3 string.
@ -2901,7 +2895,7 @@ function runner.initialize(firstTime)
for _, s in Syncs.iwalk() do for _, s in Syncs.iwalk() do
if s.config.monitor == "inotify" then if s.config.monitor == "inotify" then
Inotify.addSync(s, s.source) Inotify.addSync(s, s.source)
elseif s.config.monitor == "fsevents" then elseif s.config.monitor == "fsevents" then
Fsevents.addSync(s, s.source) Fsevents.addSync(s, s.source)
else else
error("sync "..s.config.name.. error("sync "..s.config.name..