mirror of
https://github.com/octoleo/lsyncd.git
synced 2025-01-07 17:14:03 +00:00
code beautification
This commit is contained in:
parent
27c211eaa4
commit
e869bbc1ca
157
fsevents.c
157
fsevents.c
@ -194,19 +194,17 @@ handle_event(lua_State *L, struct kfs_event *event, ssize_t mlen)
|
||||
switch(atype) {
|
||||
case FSE_RENAME :
|
||||
if (path) {
|
||||
/* for move events second string is target */
|
||||
// for move events second string is target
|
||||
trg = (char *) &arg->data.str;
|
||||
}
|
||||
/* fallthrough */
|
||||
// fallthrough
|
||||
case FSE_CHOWN :
|
||||
case FSE_CONTENT_MODIFIED :
|
||||
case FSE_CREATE_FILE :
|
||||
case FSE_CREATE_DIR :
|
||||
case FSE_DELETE :
|
||||
case FSE_STAT_CHANGED :
|
||||
if (!path) {
|
||||
path = (char *)&arg->data.str;
|
||||
}
|
||||
if (!path) path = (char *)&arg->data.str;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@ -249,13 +247,11 @@ handle_event(lua_State *L, struct kfs_event *event, ssize_t mlen)
|
||||
|
||||
if (etype) {
|
||||
if (!path) {
|
||||
printlogf(L, "Error",
|
||||
"Internal fail, fsevents, no path.");
|
||||
printlogf(L, "Error", "Internal fail, fsevents, no path.");
|
||||
exit(-1);
|
||||
}
|
||||
if (isdir < 0) {
|
||||
printlogf(L, "Error",
|
||||
"Internal fail, fsevents, neither dir nor file.");
|
||||
printlogf(L, "Error", "Internal fail, fsevents, neither dir nor file.");
|
||||
exit(-1);
|
||||
}
|
||||
load_runner_func(L, "fsEventsEvent");
|
||||
@ -286,72 +282,71 @@ fsevents_ready(lua_State *L, struct observance *obs)
|
||||
logstring("Error", "Internal, fsevents_fd != ob->fd");
|
||||
exit(-1); // ERRNO
|
||||
}
|
||||
{
|
||||
ptrdiff_t len = read (fsevents_fd, readbuf, readbuf_size);
|
||||
int err = errno;
|
||||
if (len == 0) {
|
||||
|
||||
ptrdiff_t len = read (fsevents_fd, readbuf, readbuf_size);
|
||||
int err = errno;
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
if (len < 0) {
|
||||
if (err == EAGAIN) {
|
||||
/* nothing more */
|
||||
return;
|
||||
} else {
|
||||
printlogf(L, "Error", "Read fail on fsevents");
|
||||
exit(-1); // ERRNO
|
||||
}
|
||||
if (len < 0) {
|
||||
if (err == EAGAIN) {
|
||||
/* nothing more */
|
||||
return;
|
||||
} else {
|
||||
printlogf(L, "Error", "Read fail on fsevents");
|
||||
exit(-1); // ERRNO
|
||||
}
|
||||
{
|
||||
int off = 0;
|
||||
while (off < len && !hup && !term) {
|
||||
/* deals with alignment issues on 64 bit by copying data bit by bit */
|
||||
struct kfs_event* event = (struct kfs_event *) eventbuf;
|
||||
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);
|
||||
}
|
||||
}
|
||||
{
|
||||
int off = 0;
|
||||
while (off < len && !hup && !term) {
|
||||
/* deals with alignment issues on 64 bit by copying data bit by bit */
|
||||
struct kfs_event* event = (struct kfs_event *) eventbuf;
|
||||
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
|
||||
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;
|
||||
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);
|
||||
if (argType == FSE_ARG_DONE) {
|
||||
/* done */
|
||||
break;
|
||||
} else {
|
||||
/* copy data length */
|
||||
argLen = *(uint16_t *)(readbuf + off);
|
||||
event->args[whichArg]->len = argLen;
|
||||
off += sizeof(uint16_t);
|
||||
if (argType == FSE_ARG_DONE) {
|
||||
/* done */
|
||||
break;
|
||||
} 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++;
|
||||
/* copy data */
|
||||
memcpy(&(event->args[whichArg]->data), readbuf + off, argLen);
|
||||
off += argLen;
|
||||
}
|
||||
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)
|
||||
{
|
||||
int8_t event_list[] = { // action to take for each event
|
||||
FSE_REPORT, /* FSE_CREATE_FILE */
|
||||
FSE_REPORT, /* FSE_DELETE */
|
||||
FSE_REPORT, /* FSE_STAT_CHANGED */
|
||||
FSE_REPORT, /* FSE_RENAME */
|
||||
FSE_REPORT, /* FSE_CONTENT_MODIFIED */
|
||||
FSE_REPORT, /* FSE_EXCHANGE */
|
||||
FSE_REPORT, /* FSE_FINDER_INFO_CHANGED */
|
||||
FSE_REPORT, /* FSE_CREATE_DIR */
|
||||
FSE_REPORT, /* FSE_CHOWN */
|
||||
FSE_REPORT, /* FSE_XATTR_MODIFIED */
|
||||
FSE_REPORT, /* FSE_XATTR_REMOVED */
|
||||
FSE_REPORT, // FSE_CREATE_FILE
|
||||
FSE_REPORT, // FSE_DELETE
|
||||
FSE_REPORT, // FSE_STAT_CHANGED
|
||||
FSE_REPORT, // FSE_RENAME
|
||||
FSE_REPORT, // FSE_CONTENT_MODIFIED
|
||||
FSE_REPORT, // FSE_EXCHANGE
|
||||
FSE_REPORT, // FSE_FINDER_INFO_CHANGED
|
||||
FSE_REPORT, // FSE_CREATE_DIR
|
||||
FSE_REPORT, // FSE_CHOWN
|
||||
FSE_REPORT, // FSE_XATTR_MODIFIED
|
||||
FSE_REPORT, // FSE_XATTR_REMOVED
|
||||
};
|
||||
struct fsevent_clone_args fca = {
|
||||
.event_list = (int8_t *) event_list,
|
||||
@ -428,7 +423,7 @@ open_fsevents(lua_State *L)
|
||||
}
|
||||
readbuf = s_malloc(readbuf_size);
|
||||
eventbuf = s_malloc(eventbuf_size);
|
||||
/* fd has been cloned, closes access fd */
|
||||
// fd has been cloned, closes access fd
|
||||
close(fd);
|
||||
close_exec_fd(fsevents_fd);
|
||||
non_block_fd(fsevents_fd);
|
||||
|
56
lsyncd.lua
56
lsyncd.lua
@ -686,10 +686,10 @@ local InletFactory = (function()
|
||||
|
||||
------
|
||||
-- Returns the target.
|
||||
-- Just for user comfort, for most case
|
||||
-- (Actually except of here, the lsyncd.runner itself
|
||||
-- does not care event about the existance of "target",
|
||||
-- this is completly up to the action scripts.)
|
||||
-- Just for user comfort
|
||||
--
|
||||
-- (except here, the lsyncd.runner does not care event about the
|
||||
-- existance of "target", this is up to the scripts.)
|
||||
--
|
||||
target = function(event)
|
||||
return e2s[event].config.target
|
||||
@ -1737,9 +1737,7 @@ local Syncs = (function()
|
||||
-- the monitor to use
|
||||
config.monitor =
|
||||
settings.monitor or config.monitor or Monitors.default()
|
||||
if config.monitor ~= "inotify"
|
||||
and config.monitor ~= "fsevents"
|
||||
then
|
||||
if config.monitor ~= "inotify" and config.monitor ~= "fsevents" then
|
||||
local info = debug.getinfo(3, "Sl")
|
||||
log("Error", info.short_src, ":", info.currentline,
|
||||
": event monitor '",config.monitor,"' unknown.")
|
||||
@ -1796,11 +1794,11 @@ end)()
|
||||
-- begins with root
|
||||
--
|
||||
local function splitPath(path, root)
|
||||
local rl = #root
|
||||
local sp = string.sub(path, 1, rl)
|
||||
local rlen = #root
|
||||
local sp = string.sub(path, 1, rlen)
|
||||
|
||||
if sp == root then
|
||||
return string.sub(path, rl, -1)
|
||||
return string.sub(path, rlen, -1)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
@ -2071,23 +2069,20 @@ local Fsevents = (function()
|
||||
-----
|
||||
-- Called when any event has occured.
|
||||
--
|
||||
-- @param etype "Attrib", "Mofify", "Create", "Delete", "Move")
|
||||
-- @param wd watch descriptor (matches lsyncd.inotifyadd())
|
||||
-- @param isdir true if filename is a directory
|
||||
-- @param time time of event
|
||||
-- @param filename string filename without path
|
||||
-- @param filename2
|
||||
-- etype: 'Attrib', 'Mofify', 'Create', 'Delete', 'Move')
|
||||
-- isdir: true if filename is a directory
|
||||
-- time: time of event
|
||||
-- path: path of file
|
||||
-- path2: path of target in case of 'Move'
|
||||
--
|
||||
local function event(etype, isdir, time, path, path2)
|
||||
|
||||
if isdir then
|
||||
path = path .. '/'
|
||||
if path2 then
|
||||
path2 = path2 .. '/'
|
||||
end
|
||||
path = path..'/'
|
||||
if path2 then path2 = path2..'/' end
|
||||
end
|
||||
|
||||
log("Fsevents",etype,",",isdir,",",time,",",path,",",path2)
|
||||
log('Fsevents',etype,',',isdir,',',time,',',path,',',path2)
|
||||
|
||||
for _, s in Syncs.iwalk() do repeat
|
||||
local root = s.source
|
||||
@ -2100,18 +2095,16 @@ local Fsevents = (function()
|
||||
relative2 = splitPath(path2, root)
|
||||
end
|
||||
|
||||
-- makes a copy of etype to possibly change it
|
||||
-- possibly change etype for this iteration only
|
||||
local etyped = etype
|
||||
if etyped == 'Move' then
|
||||
if not relative2 then
|
||||
log("Normal", "Transformed Move to Create for ",
|
||||
sync.config.name)
|
||||
log('Normal', 'Transformed Move to Create for ', sync.config.name)
|
||||
etyped = 'Create'
|
||||
elseif not relative then
|
||||
relative = relative2
|
||||
relative2 = nil
|
||||
log("Normal", "Transformed Move to Delete for ",
|
||||
sync.config.name)
|
||||
log('Normal', 'Transformed Move to Delete for ', sync.config.name)
|
||||
etyped = 'Delete'
|
||||
end
|
||||
end
|
||||
@ -2344,11 +2337,12 @@ local functionWriter = (function()
|
||||
else
|
||||
ft = "function(event, event2)\n"
|
||||
end
|
||||
ft = ft .. ' log("Normal", "Event " .. event.etype ..\n'
|
||||
ft = ft .. " [[ spawns shell '" .. lc .. '\']])\n'
|
||||
ft = ft .. " spawnShell(event, [[" .. cmd .. "]]"
|
||||
-- TODO do array joining instead
|
||||
ft = ft..' log("Normal", "Event " .. event.etype ..\n'
|
||||
ft = ft.." [[ spawns shell '"..lc..'\']])\n'
|
||||
ft = ft.." spawnShell(event, [["..cmd.. "]]"
|
||||
for _, v in ipairs(args) do
|
||||
ft = ft .. ",\n " .. v
|
||||
ft = ft..",\n "..v
|
||||
end
|
||||
ft = ft .. ")\nend"
|
||||
return ft
|
||||
@ -2688,7 +2682,7 @@ function runner.configure(args, monitors)
|
||||
end},
|
||||
log =
|
||||
{1, nil},
|
||||
logfile =
|
||||
logfile =
|
||||
{1, function(file)
|
||||
clSettings.logfile = file
|
||||
end},
|
||||
|
Loading…
Reference in New Issue
Block a user