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

@ -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");
@ -286,72 +282,71 @@ 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);
} }
} }
} }
@ -380,17 +375,17 @@ 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,
@ -428,7 +423,7 @@ open_fsevents(lua_State *L)
} }
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

@ -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
@ -1737,9 +1737,7 @@ local Syncs = (function()
-- 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.")
@ -1796,11 +1794,11 @@ end)()
-- 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
@ -2071,23 +2069,20 @@ 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
@ -2100,18 +2095,16 @@ local Fsevents = (function()
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
@ -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
@ -2688,7 +2682,7 @@ function runner.configure(args, monitors)
end}, end},
log = log =
{1, nil}, {1, nil},
logfile = logfile =
{1, function(file) {1, function(file)
clSettings.logfile = file clSettings.logfile = file
end}, end},