This commit is contained in:
Axel Kittenberger 2010-10-22 08:37:15 +00:00
parent 42e8f69b05
commit e575ab8d76
2 changed files with 66 additions and 50 deletions

View File

@ -60,6 +60,12 @@ enum event_type {
CREATE = 3, CREATE = 3,
DELETE = 4, DELETE = 4,
MOVE = 5, MOVE = 5,
/* MOVEFROM/TO never get passed to the runner, but it uses these
* enums to split events again. The core will only send complete
* move events to the runner. Moves into or out of the watch tree
* are replaced with CREATE/DELETE events. */
MOVEFROM = 6,
MOVETO = 7,
}; };
/** /**
@ -714,36 +720,42 @@ void handle_event(lua_State *L, struct inotify_event *event) {
} }
if (event == NULL) { if (event == NULL) {
/* a buffered MOVE_FROM is not followed by anything, thus it is unary */ /* a buffered MOVE_FROM is not followed by anything,
thus it is unary */
event = move_event_buf; event = move_event_buf;
event_type = DELETE; event_type = DELETE;
move_event = false; move_event = false;
} else if (move_event && (!(IN_MOVED_TO & event->mask)|| event->cookie != move_event_buf->cookie)) { } else if (move_event &&
/* there is a MOVE_FROM event in the buffer and this is not the match */ ( !(IN_MOVED_TO & event->mask) ||
/* continue in this function iteration to handler the buffer instead */ event->cookie != move_event_buf->cookie) ) {
/* there is a MOVE_FROM event in the buffer and this is not the match
* continue in this function iteration to handler the buffer instead */
after_buf = event; after_buf = event;
event = move_event_buf; event = move_event_buf;
event_type = DELETE; event_type = DELETE;
move_event = false; move_event = false;
} else if (move_event && (IN_MOVED_TO & event->mask) && event->cookie == move_event_buf->cookie) { } else if ( move_event &&
(IN_MOVED_TO & event->mask) &&
event->cookie == move_event_buf->cookie ) {
/* this is indeed a matched move */ /* this is indeed a matched move */
event_type = MOVE; event_type = MOVE;
move_event = false; move_event = false;
} else if (IN_MOVED_FROM & event->mask) { } else if (IN_MOVED_FROM & event->mask) {
/* just the MOVE_FROM, buffers this event, and wait if next event is a matching /* just the MOVE_FROM, buffers this event, and wait if next event is a matching
* MOVED_TO of this was an unary move out of the watched tree. */ * MOVED_TO of this was an unary move out of the watched tree. */
if (move_event_buf_size < sizeof(struct inotify_event) + event->len) { size_t el = sizeof(struct inotify_event) + event->len;
move_event_buf_size = sizeof(struct inotify_event) + event->len; if (move_event_buf_size < el) {
move_event_buf = s_realloc(move_event_buf, move_event_buf_size); move_event_buf_size = el;
move_event_buf = s_realloc(move_event_buf, el);
} }
memcpy(move_event_buf, event, sizeof(struct inotify_event) + event->len); memcpy(move_event_buf, event, el);
move_event = true; move_event = true;
return; return;
} else if (IN_MOVED_TO & event->mask) { } else if (IN_MOVED_TO & event->mask) {
/* must be an unary moveto */ /* must be an unary move-to */
event_type = CREATE; event_type = CREATE;
} else if (IN_MOVED_FROM & event->mask) { } else if (IN_MOVED_FROM & event->mask) {
/* must be an unary movefrom */ /* must be an unary move-from */
event_type = DELETE; event_type = DELETE;
} else if (IN_ATTRIB & event->mask) { } else if (IN_ATTRIB & event->mask) {
/* just attrib change */ /* just attrib change */
@ -762,6 +774,7 @@ void handle_event(lua_State *L, struct inotify_event *event) {
return; return;
} }
/* and hands over to runner */
lua_getglobal(L, "lsyncd_event"); lua_getglobal(L, "lsyncd_event");
lua_pushnumber(L, event_type); lua_pushnumber(L, event_type);
lua_pushnumber(L, event->wd); lua_pushnumber(L, event->wd);
@ -775,6 +788,7 @@ void handle_event(lua_State *L, struct inotify_event *event) {
} }
lua_call(L, 5, 0); lua_call(L, 5, 0);
/* if there is a buffered event executes it */
if (after_buf) { if (after_buf) {
handle_event(L, after_buf); handle_event(L, after_buf);
} }
@ -842,9 +856,8 @@ masterloop(lua_State *L)
} }
/* reads possible events from inotify stream */ /* reads possible events from inotify stream */
do { while(do_read) {
int i = 0; int i = 0;
if (do_read) {
do { do {
len = read (inotify_fd, readbuf, readbuf_size); len = read (inotify_fd, readbuf, readbuf_size);
if (len < 0 && errno == EINVAL) { if (len < 0 && errno == EINVAL) {
@ -858,16 +871,13 @@ masterloop(lua_State *L)
continue; continue;
} }
} while(0); } while(0);
} else {
len = 0;
}
while (i < len && !reset) { while (i < len && !reset) {
struct inotify_event *event = (struct inotify_event *) &readbuf[i]; struct inotify_event *event = (struct inotify_event *) &readbuf[i];
handle_event(L, event); handle_event(L, event);
i += sizeof(struct inotify_event) + event->len; i += sizeof(struct inotify_event) + event->len;
} }
/* check if there is more data */ /* check if there is more data */
if (do_read) { {
struct timeval tv = {.tv_sec = 0, .tv_usec = 0}; struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
fd_set readfds; fd_set readfds;
FD_ZERO(&readfds); FD_ZERO(&readfds);
@ -877,9 +887,9 @@ masterloop(lua_State *L)
logstring(DEBUG, "there is more data on inotify."); logstring(DEBUG, "there is more data on inotify.");
} }
} }
} while (do_read); }
/* checks if there is an unary MOVE_FROM in the buffer */ /* checks if there is an unary MOVE_FROM left in the buffer */
if (move_event) { if (move_event) {
handle_event(L, NULL); handle_event(L, NULL);
} }

View File

@ -14,6 +14,7 @@
-- A security measurement. -- A security measurement.
-- Core will exit if version ids mismatch. -- Core will exit if version ids mismatch.
if lsyncd_version ~= nil then if lsyncd_version ~= nil then
-- checks if the runner is being loaded twice
print("You cannot use the lsyncd runner as configuration file!") print("You cannot use the lsyncd runner as configuration file!")
os.exit(-1) os.exit(-1)
end end
@ -34,10 +35,14 @@ origins = {}
-- structure: -- structure:
-- [#] { -- [#] {
-- .ident .. the identifier of target (like string "host:dir") -- .ident .. the identifier of target (like string "host:dir")
-- for lsyncd this passed competly opaquely to the action handlers -- for lsyncd this passed competly opaquely to the
-- .delays = [#] { -- action handlers
-- wd ... watch descriptor -- .delays = [#) { .. the delays stack
-- atype ... TODO -- .atype .. enum, kind of action
-- .wd .. watch descriptor id this origins from TODO needed?
-- .attend .. link to atttender that raised this.
-- .filename .. filename or nil, means dir itself
-- (.movepeer) .. for MOVEFROM/MOVETO link to other delay
-- } -- }
-- } -- }
targets = {} targets = {}
@ -59,7 +64,6 @@ targets = {}
-- --
watches = {} watches = {}
-- TODO -- TODO
collapse_table = { collapse_table = {
[ATTRIB] = { [ATTRIB] = ATTRIB, [MODIFY] = MODIFY, [CREATE] = CREATE, [DELETE] = DELETE }, [ATTRIB] = { [ATTRIB] = ATTRIB, [MODIFY] = MODIFY, [CREATE] = CREATE, [DELETE] = DELETE },
@ -161,11 +165,13 @@ end
-- A list of names of the event types the core sends. -- A list of names of the event types the core sends.
-- --
local event_names = { local event_names = {
[ATTRIB] = "Attrib", [ATTRIB ] = "Attrib",
[MODIFY] = "Modify", [MODIFY ] = "Modify",
[CREATE] = "Create", [CREATE ] = "Create",
[DELETE] = "Delete", [DELETE ] = "Delete",
[MOVE ] = "Move", [MOVE ] = "Move",
[MOVEFROM ] = "MoveFrom",
[MOVETO ] = "MoveTo",
} }
----- -----