mirror of https://github.com/octoleo/lsyncd.git
structering the core some more
This commit is contained in:
parent
5b43bbe58e
commit
10ecd66c60
18
core/core.c
18
core/core.c
|
@ -41,6 +41,7 @@
|
|||
#include "mem.h"
|
||||
#include "util.h"
|
||||
#include "pipe.h"
|
||||
#include "observe.h"
|
||||
|
||||
#ifdef WITH_INOTIFY
|
||||
#include "inotify.h"
|
||||
|
@ -195,11 +196,10 @@ static int callError;
|
|||
static void
|
||||
user_obs_ready(
|
||||
lua_State * L,
|
||||
struct observance * obs
|
||||
int fd,
|
||||
void * extra
|
||||
)
|
||||
{
|
||||
int fd = obs->fd;
|
||||
|
||||
// pushes the ready table on table
|
||||
lua_pushlightuserdata( L, ( void * ) user_obs_ready );
|
||||
lua_gettable( L, LUA_REGISTRYINDEX );
|
||||
|
@ -228,11 +228,10 @@ user_obs_ready(
|
|||
static void
|
||||
user_obs_writey(
|
||||
lua_State * L,
|
||||
struct observance * obs
|
||||
int fd,
|
||||
void * extra
|
||||
)
|
||||
{
|
||||
int fd = obs->fd;
|
||||
|
||||
// pushes the writey table on table
|
||||
lua_pushlightuserdata( L, (void *) user_obs_writey );
|
||||
lua_gettable( L, LUA_REGISTRYINDEX );
|
||||
|
@ -259,9 +258,12 @@ user_obs_writey(
|
|||
| FIXME - give the user a chance to do something in that case!
|
||||
*/
|
||||
static void
|
||||
user_obs_tidy( struct observance *obs )
|
||||
user_obs_tidy(
|
||||
int fd,
|
||||
void * extra
|
||||
)
|
||||
{
|
||||
close( obs->fd );
|
||||
close( fd );
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "mem.h"
|
||||
#include "log.h"
|
||||
#include "inotify.h"
|
||||
#include "observe.h"
|
||||
|
||||
|
||||
/*
|
||||
|
@ -390,11 +391,12 @@ static char * readbuf = NULL;
|
|||
static void
|
||||
inotify_ready(
|
||||
lua_State *L,
|
||||
struct observance *obs
|
||||
int fd,
|
||||
void * extra
|
||||
)
|
||||
{
|
||||
// sanity check
|
||||
if( obs->fd != inotify_fd )
|
||||
if( fd != inotify_fd )
|
||||
{
|
||||
logstring( "Error", "internal failure, inotify_fd != obs->fd" );
|
||||
|
||||
|
@ -483,9 +485,9 @@ register_inotify( lua_State *L )
|
|||
| Cleans up the inotify handling.
|
||||
*/
|
||||
static void
|
||||
inotify_tidy( struct observance *obs )
|
||||
inotify_tidy( int fd, void * extra )
|
||||
{
|
||||
if( obs->fd != inotify_fd )
|
||||
if( fd != inotify_fd )
|
||||
{
|
||||
logstring( "Error", "internal failure: inotify_fd != ob->fd" );
|
||||
exit( -1 );
|
||||
|
|
|
@ -17,6 +17,5 @@
|
|||
extern void register_inotify(lua_State *L);
|
||||
extern void open_inotify(lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
#ifndef LSYNCD_LOG_H
|
||||
#define LSYNCD_LOG_H
|
||||
|
||||
|
||||
// Adds a logging category
|
||||
extern bool add_logcat( const char *name, int priority );
|
||||
|
||||
|
@ -43,3 +42,4 @@ printlogf0(
|
|||
extern void log_free( );
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
* Authors: Axel Kittenberger <axkibe@gmail.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LSYNCD_H
|
||||
#define LSYNCD_H
|
||||
|
||||
|
@ -78,56 +77,6 @@ extern volatile sig_atomic_t hup;
|
|||
extern volatile sig_atomic_t term;
|
||||
|
||||
|
||||
/*
|
||||
| File-descriptor helpers
|
||||
*/
|
||||
|
||||
// Sets the non-blocking flag for a file descriptor.
|
||||
extern void non_block_fd(int fd);
|
||||
|
||||
// Sets the close-on-exit flag for a file descriptor.
|
||||
extern void close_exec_fd(int fd);
|
||||
|
||||
|
||||
/**
|
||||
* An observance to be called when a file descritor becomes
|
||||
* read-ready or write-ready.
|
||||
*/
|
||||
struct observance {
|
||||
// The file descriptor to observe.
|
||||
int fd;
|
||||
|
||||
// Function to call when read becomes ready.
|
||||
void (*ready)(lua_State *, struct observance *);
|
||||
|
||||
// Function to call when write becomes ready.
|
||||
void (*writey)(lua_State *, struct observance *);
|
||||
|
||||
// Function to call to clean up
|
||||
void (*tidy)(struct observance *);
|
||||
|
||||
// Extra tokens to pass to the functions.
|
||||
void *extra;
|
||||
};
|
||||
|
||||
// makes the core observe a file descriptor
|
||||
extern void observe_fd(
|
||||
int fd,
|
||||
void (*ready) (lua_State *, struct observance *),
|
||||
void (*writey)(lua_State *, struct observance *),
|
||||
void (*tidy) (struct observance *),
|
||||
void *extra
|
||||
);
|
||||
|
||||
// makes the big select for all observed fds
|
||||
extern void observe_select( lua_State * L, struct timespec const * timeout );
|
||||
|
||||
// tidies up all observances
|
||||
extern void observe_tidy_all( );
|
||||
|
||||
// stops the core to observe a file descriptor
|
||||
extern void nonobserve_fd(int fd);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
| License: GPLv2 (see COPYING) or any later version
|
||||
| Authors: Axel Kittenberger <axkibe@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef LSYNCD_MEM_H
|
||||
#define LSYNCD_MEM_H
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
| observe.c from Lsyncd - Live (Mirror) Syncing Demon
|
||||
|
|
||||
|
|
||||
| Handles observing fd's and the big select.
|
||||
| Handles observing file descriptors and the big select.
|
||||
|
|
||||
|
|
||||
| License: GPLv2 (see COPYING) or any later version
|
||||
|
@ -22,7 +22,26 @@
|
|||
#include "mem.h"
|
||||
|
||||
|
||||
// FIXME make "struct observance" a local structure
|
||||
/**
|
||||
* An observance to be called when a file descritor becomes
|
||||
* read-ready or write-ready.
|
||||
*/
|
||||
struct observance {
|
||||
// The file descriptor to observe.
|
||||
int fd;
|
||||
|
||||
// Function to call when read becomes ready.
|
||||
void (*ready)( lua_State *, int fd, void * extra );
|
||||
|
||||
// Function to call when write becomes ready.
|
||||
void (*writey)( lua_State *, int fd, void * extra );
|
||||
|
||||
// Function to call to clean up
|
||||
void (*tidy)( int fd, void * extra );
|
||||
|
||||
// Extra tokens to pass to the functions.
|
||||
void *extra;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
|
@ -58,9 +77,9 @@ static bool observance_action = false;
|
|||
extern void
|
||||
observe_fd(
|
||||
int fd,
|
||||
void ( * ready ) ( lua_State *, struct observance * ),
|
||||
void ( * writey ) ( lua_State *, struct observance * ),
|
||||
void ( * tidy ) ( struct observance * ),
|
||||
void ( * ready ) ( lua_State *, int fd, void * extra ),
|
||||
void ( * writey ) ( lua_State *, int fd, void * extra ),
|
||||
void ( * tidy ) ( int fd, void * extra ),
|
||||
void * extra
|
||||
)
|
||||
{
|
||||
|
@ -165,7 +184,7 @@ nonobserve_fd( int fd )
|
|||
}
|
||||
|
||||
// tidies up the observance
|
||||
observances[ pos ].tidy( observances + pos );
|
||||
observances[ pos ].tidy( observances[ pos ].fd, observances[ pos ].extra );
|
||||
|
||||
// and moves the list down
|
||||
memmove(
|
||||
|
@ -238,12 +257,13 @@ observe_select
|
|||
for( pi = 0; pi < observances_len; pi++ )
|
||||
{
|
||||
struct observance *obs = observances + pi;
|
||||
int fd = obs->fd;
|
||||
|
||||
// checks for signals
|
||||
if( hup || term ) break;
|
||||
|
||||
// a file descriptor became read-ready
|
||||
if( obs->ready && FD_ISSET( obs->fd, &rfds ) ) obs->ready( L, obs );
|
||||
if( obs->ready && FD_ISSET( fd, &rfds ) ) obs->ready( L, fd, obs->extra );
|
||||
|
||||
// Checks for signals, again, better safe than sorry
|
||||
if ( hup || term ) break;
|
||||
|
@ -251,14 +271,11 @@ observe_select
|
|||
// FIXME breaks on multiple nonobservances in one beat
|
||||
if(
|
||||
nonobservances_len > 0 &&
|
||||
nonobservances[ nonobservances_len - 1 ] == obs->fd
|
||||
nonobservances[ nonobservances_len - 1 ] == fd
|
||||
) continue;
|
||||
|
||||
// a file descriptor became write-ready
|
||||
if( obs->writey && FD_ISSET( obs->fd, &wfds ) )
|
||||
{
|
||||
obs->writey( L, obs );
|
||||
}
|
||||
if( obs->writey && FD_ISSET( fd, &wfds ) ) obs->writey( L, fd, obs->extra );
|
||||
}
|
||||
|
||||
observance_action = false;
|
||||
|
@ -284,7 +301,7 @@ void observe_tidy_all( )
|
|||
for( i = 0; i < observances_len; i++ )
|
||||
{
|
||||
struct observance *obs = observances + i;
|
||||
obs->tidy( obs );
|
||||
obs->tidy( obs->fd, obs->extra );
|
||||
}
|
||||
|
||||
observances_len = 0;
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
| observe.h from Lsyncd - Live (Mirror) Syncing Demon
|
||||
|
|
||||
|
|
||||
| Handles observing file descriptors and the big select.
|
||||
|
|
||||
|
|
||||
| License: GPLv2 (see COPYING) or any later version
|
||||
| Authors: Axel Kittenberger <axkibe@gmail.com>
|
||||
*/
|
||||
#ifndef LSYNCD_OBSERVE_H
|
||||
#define LSYNCD_OBSERVE_H
|
||||
|
||||
// Sets the non-blocking flag for a file descriptor.
|
||||
extern void non_block_fd(int fd);
|
||||
|
||||
// Sets the close-on-exit flag for a file descriptor.
|
||||
extern void close_exec_fd(int fd);
|
||||
|
||||
// makes the core observe a file descriptor
|
||||
extern void observe_fd(
|
||||
int fd,
|
||||
void (*ready) ( lua_State *, int fd, void * extra ),
|
||||
void (*writey)( lua_State *, int fd, void * extra ),
|
||||
void (*tidy) ( int fd, void * extra ),
|
||||
void *extra
|
||||
);
|
||||
|
||||
// makes the big select for all observed fds
|
||||
extern void observe_select( lua_State * L, struct timespec const * timeout );
|
||||
|
||||
// tidies up all observances
|
||||
extern void observe_tidy_all( );
|
||||
|
||||
// stops the core to observe a file descriptor
|
||||
extern void nonobserve_fd( int fd );
|
||||
|
||||
#endif
|
||||
|
19
core/pipe.c
19
core/pipe.c
|
@ -14,14 +14,10 @@
|
|||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_USE_APICHECK 1
|
||||
#include <lua.h>
|
||||
//#include <lualib.h>
|
||||
//#include <lauxlib.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "mem.h"
|
||||
#include "pipe.h"
|
||||
#include "observe.h"
|
||||
|
||||
|
||||
/*
|
||||
|
@ -42,12 +38,11 @@ struct pipemsg
|
|||
static void
|
||||
pipe_writey(
|
||||
lua_State * L,
|
||||
struct observance * observance
|
||||
int fd,
|
||||
void * extra
|
||||
)
|
||||
{
|
||||
int fd = observance->fd;
|
||||
|
||||
struct pipemsg *pm = (struct pipemsg * ) observance->extra;
|
||||
struct pipemsg * pm = (struct pipemsg *) extra;
|
||||
|
||||
int len = write( fd, pm->text + pm->pos, pm->tlen - pm->pos );
|
||||
|
||||
|
@ -75,11 +70,11 @@ pipe_writey(
|
|||
| Called when cleaning up a pipe.
|
||||
*/
|
||||
static void
|
||||
pipe_tidy( struct observance * observance )
|
||||
pipe_tidy( int fd, void * extra )
|
||||
{
|
||||
struct pipemsg *pm = ( struct pipemsg * ) observance->extra;
|
||||
struct pipemsg * pm = (struct pipemsg *) extra;
|
||||
|
||||
close( observance->fd );
|
||||
close( fd );
|
||||
free( pm->text );
|
||||
free( pm );
|
||||
}
|
||||
|
|
11
core/pipe.h
11
core/pipe.h
|
@ -8,27 +8,21 @@
|
|||
| License: GPLv2 (see COPYING) or any later version
|
||||
| Authors: Axel Kittenberger <axkibe@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef LSYNCD_PIPE_H
|
||||
#define LSYNCD_PIPE_H
|
||||
|
||||
|
||||
/*
|
||||
| Creates a pipe.
|
||||
|
|
||||
| Sets the write end non blocking and close on exec.
|
||||
*/
|
||||
extern void pipe_create(
|
||||
int pipefd[ 2 ]
|
||||
);
|
||||
|
||||
|
||||
extern void pipe_create( int pipefd[ 2 ] );
|
||||
|
||||
/*
|
||||
| Writes to a pipe and handles observing for further writing
|
||||
| if it's buffer is fully filled on first try.
|
||||
|
|
||||
| This may be used only once for every pipe manged by Lsyncd.
|
||||
| This may be used only once for every pipe managed by Lsyncd!
|
||||
*/
|
||||
extern void pipe_write(
|
||||
int pipedf[ 2 ], // the pipe file descriptors
|
||||
|
@ -36,6 +30,5 @@ extern void pipe_write(
|
|||
size_t pipe_len // the pipe's text length
|
||||
);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
#ifndef LSYNCD_UTIL_H
|
||||
#define LSYNCD_UTIL_H
|
||||
|
||||
|
||||
/*
|
||||
| Returns the absolute path of a path.
|
||||
|
|
||||
|
@ -31,5 +30,5 @@ extern void non_block_fd( int fd );
|
|||
*/
|
||||
extern void close_exec_fd( int fd );
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue