fixing again setting with new delay objects, cleanups

This commit is contained in:
Axel Kittenberger 2017-02-06 17:00:39 +01:00
parent 544f6066b7
commit 785a7dd503
3 changed files with 63 additions and 46 deletions

View File

@ -124,9 +124,9 @@ l_addwatch( lua_State *L )
// kernel call to create the inotify watch // kernel call to create the inotify watch
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( printlogf(
L, "Error", L, "Error",
@ -154,18 +154,18 @@ 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
*/ */
static int static int
l_rmwatch(lua_State *L) l_rmwatch( lua_State *L )
{ {
int wd = luaL_checkinteger(L, 1); int wd = luaL_checkinteger( L, 1 );
inotify_rm_watch(inotify_fd, wd); inotify_rm_watch( inotify_fd, wd );
printlogf(L, "Inotify", "rmwatch()<-%d", wd); printlogf( L, "Inotify", "rmwatch()<-%d", wd );
return 0; return 0;
} }
@ -173,7 +173,8 @@ l_rmwatch(lua_State *L)
/* /*
| Lsyncd's core's inotify functions. | Lsyncd's core's inotify functions.
*/ */
static const luaL_Reg linotfylib[] = { static const luaL_Reg linotfylib[ ] =
{
{ "addwatch", l_addwatch }, { "addwatch", l_addwatch },
{ "rmwatch", l_rmwatch }, { "rmwatch", l_rmwatch },
{ NULL, NULL} { NULL, NULL}
@ -215,6 +216,7 @@ handle_event(
// used to execute two events in case of unmatched MOVE_FROM buffer // used to execute two events in case of unmatched MOVE_FROM buffer
struct inotify_event *after_buf = NULL; struct inotify_event *after_buf = NULL;
if( event && ( IN_Q_OVERFLOW & event->mask ) ) if( event && ( IN_Q_OVERFLOW & event->mask ) )
{ {
// and overflow happened, tells the runner // and overflow happened, tells the runner
@ -224,8 +226,11 @@ handle_event(
{ {
exit( -1 ); exit( -1 );
} }
lua_pop( L, 1 ); lua_pop( L, 1 );
hup = 1; hup = 1;
return; return;
} }
@ -251,10 +256,10 @@ handle_event(
move_event = false; move_event = false;
} }
else if( else if(
move_event && move_event
( && (
!( IN_MOVED_TO & event->mask ) || !( IN_MOVED_TO & event->mask )
event->cookie != move_event_buf->cookie || event->cookie != move_event_buf->cookie
) )
) )
{ {
@ -263,18 +268,21 @@ handle_event(
logstring( logstring(
"Inotify", "Inotify",
"icore, changing unary MOVE_FROM into DELETE" "icore, changing unary MOVE_FROM into DELETE"
) );
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( else if(
move_event && move_event
( && ( IN_MOVED_TO & event->mask )
IN_MOVED_TO & event->mask ) && && event->cookie == move_event_buf->cookie
event->cookie == move_event_buf->cookie )
)
{ {
// this is indeed a matched move */ // this is indeed a matched move */
event_type = "Move"; event_type = "Move";
@ -290,10 +298,14 @@ handle_event(
if( move_event_buf_size < el ) if( move_event_buf_size < el )
{ {
move_event_buf_size = el; move_event_buf_size = el;
move_event_buf = s_realloc( move_event_buf, el ); move_event_buf = s_realloc( move_event_buf, el );
} }
memcpy( move_event_buf, event, el ); memcpy( move_event_buf, event, el );
move_event = true; move_event = true;
return; return;
} }
@ -346,6 +358,7 @@ handle_event(
} }
lua_pushstring( L, event_type ); lua_pushstring( L, event_type );
if( event_type != MOVE ) if( event_type != MOVE )
{ {
lua_pushnumber( L, event->wd ); lua_pushnumber( L, event->wd );
@ -390,6 +403,7 @@ handle_event(
| buffer to read inotify events into | buffer to read inotify events into
*/ */
static size_t readbuf_size = 2048; static size_t readbuf_size = 2048;
static char * readbuf = NULL; static char * readbuf = NULL;
@ -511,11 +525,14 @@ inotify_tidy( struct observance *obs )
"Error", "Error",
"internal failure: inotify_fd != ob->fd" "internal failure: inotify_fd != ob->fd"
); );
exit( -1 ); exit( -1 );
} }
close( inotify_fd ); close( inotify_fd );
free( readbuf ); free( readbuf );
readbuf = NULL; readbuf = NULL;
} }

View File

@ -2081,7 +2081,6 @@ daemonize(
write_pidfile( L, pidfile ); write_pidfile( L, pidfile );
} }
// detaches the new process from the parent process // detaches the new process from the parent process
sid = setsid( ); sid = setsid( );

View File

@ -723,6 +723,19 @@ local Delay = ( function
self[ k_nt ].status = 'active' self[ k_nt ].status = 'active'
end end
--
-- Sets the delay status to 'wait'
--
local function wait
(
self, -- this delay
alarm -- alarm for the delay
)
self[ k_nt ].status = 'wait'
self[ k_nt ].alarm = alarm
end
-- --
-- Creates a new delay. -- Creates a new delay.
-- --
@ -741,6 +754,7 @@ local Delay = ( function
{ {
blockedBy = blockedBy, blockedBy = blockedBy,
setActive = setActive, setActive = setActive,
wait = wait,
[ k_nt ] = [ k_nt ] =
{ {
etype = etype, etype = etype,
@ -2129,7 +2143,7 @@ local Sync = ( function
if delay.status ~= 'active' if delay.status ~= 'active'
then then
error('collecting a non-active process') error( 'collecting a non-active process' )
end end
local rc = self.config.collect( local rc = self.config.collect(
@ -2142,12 +2156,11 @@ local Sync = ( function
log( 'Error', 'Critical exitcode.' ) log( 'Error', 'Critical exitcode.' )
terminate( -1 ) terminate( -1 )
end elseif rc ~= 'again'
if rc ~= 'again'
then then
-- if its active again the collecter restarted the event -- if its active again the collecter restarted the event
removeDelay( self, delay ) removeDelay( self, delay )
log( log(
'Delay', 'Delay',
'Finish of ', 'Finish of ',
@ -2159,8 +2172,6 @@ local Sync = ( function
) )
else else
-- sets the delay on wait again -- sets the delay on wait again
delay.status = 'wait'
local alarm = self.config.delay local alarm = self.config.delay
-- delays at least 1 second -- delays at least 1 second
@ -2169,7 +2180,7 @@ local Sync = ( function
alarm = 1 alarm = 1
end end
delay.alarm = now( ) + alarm delay:wait( now( ) + alarm )
end end
else else
log( log(
@ -2187,13 +2198,9 @@ local Sync = ( function
log( 'Error', 'Critical exitcode.' ); log( 'Error', 'Critical exitcode.' );
terminate( -1 ) terminate( -1 )
end elseif rc == 'again'
if rc == 'again'
then then
-- sets the delay on wait again -- sets the delay on wait again
delay.status = 'wait'
local alarm = self.config.delay local alarm = self.config.delay
-- delays at least 1 second -- delays at least 1 second
@ -2206,18 +2213,12 @@ local Sync = ( function
for _, d in ipairs( delay ) for _, d in ipairs( delay )
do do
d.alarm = alarm d:wait( alarm )
d.status = 'wait'
end end
end else
for _, d in ipairs( delay )
for _, d in ipairs( delay ) do
do
if rc ~= 'again'
then
removeDelay( self, d ) removeDelay( self, d )
else
d.status = 'wait'
end end
end end