better configure-time checking for inotify & kqueue.

This commit is contained in:
Charles Kerr 2009-02-28 14:43:37 +00:00
parent b5327c36ee
commit 09161120eb
10 changed files with 165 additions and 108 deletions

View File

@ -101,6 +101,41 @@ AC_PATH_ZLIB
AC_SYS_LARGEFILE
dnl ----------------------------------------------------------------------------
dnl
dnl third_party/FilewWatcher tests: look for inotify and kqueue
AC_CHECK_HEADER([sys/inotify.h],
[AC_CHECK_FUNC([inotify_init],[have_inotify="yes"],[have_inotify="no"])],
[have_inotify="no"])
AC_ARG_WITH([inotify],
[AS_HELP_STRING([--with-inotify],[Enable inotify support (default=auto)])],
[want_inotify=${enableval}],
[want_inotify=${have_inotify}])
if test "x$want_inotify" = "xyes" ; then
if test "x$have_inotify" = "xyes"; then
AC_DEFINE([WITH_INOTIFY],[1])
else
AC_MSG_ERROR("inotify not found!")
fi
fi
AC_CHECK_HEADER([sys/event.h],
[AC_CHECK_FUNC([kqueue],[have_kqueue="yes"],[have_kqueue="no"])],
[have_kqueue="no"])
AC_ARG_WITH([kqueue],
[AS_HELP_STRING([--with-kqueue],[Enable kqueue support (default=auto)])],
[want_kqueue=${enableval}],
[want_kqueue=${have_kqueue}])
if test "x$want_kqueue" = "xyes" ; then
if test "x$have_kqueue" = "xyes"; then
AC_DEFINE([WITH_KQUEUE],[1])
else
AC_MSG_ERROR("kqueue not found!")
fi
fi
dnl ----------------------------------------------------------------------------
dnl
dnl posix_fadvise

View File

@ -181,16 +181,23 @@ getConfigDir( int argc, const char ** argv )
}
static void
dirChangedCB( CFW_Watch * watch UNUSED, const char * directory, const char * filename, CFW_Action action, void * userData )
dirChangedCB( CFW_Watch * watch UNUSED,
const char * directory,
const char * filename,
CFW_Action action,
void * userData )
{
if( action & ( CFW_ACTION_ADD | CFW_ACTION_DELETE ) )
if( ( action & CFW_ACTION_ADD ) && ( strstr( filename, ".torrent" ) != NULL ) )
{
int err;
char * path = tr_buildPath( directory, filename, NULL );
tr_session * session = userData;
tr_ctor * ctor = tr_ctorNew( session );
tr_ctorSetMetainfoFromFile( ctor, path );
tr_torrentNew( session, ctor, &err );
err = tr_ctorSetMetainfoFromFile( ctor, path );
if( !err )
tr_torrentNew( session, ctor, &err );
tr_free( path );
}
}
@ -325,9 +332,11 @@ main( int argc, char ** argv )
tr_ctorFree( ctor );
}
while( !closing ) {
while( !closing )
{
tr_wait( 1000 ); /* sleep one second */
if( watch )
if( watch != NULL ) /* maybe look for new .torrent files */
cfw_update( watch );
}

View File

@ -11,9 +11,9 @@
#if defined(_WIN32)
# include "FileWatcherWin32.h"
#elif defined(__APPLE_CC__)
#elif defined(WITH_KQUEUE) || defined(__APPLE_CC__)
# include "FileWatcherOSX.h"
#elif defined(__linux__)
#elif defined(WITH_INOTIFY) || defined(__linux__)
# include "FileWatcherLinux.h"
#else
# error FIXME

View File

@ -7,7 +7,7 @@
@date 4/15/2009
*/
#ifdef __linux__
#if defined(WITH_INOTIFY)
#include "FileWatcherLinux.h"
@ -163,4 +163,4 @@ namespace FW
};//namespace FW
#endif//__linux__
#endif // WITH_INOTIFY || __linux__

View File

@ -1,66 +1,66 @@
/**
Released under a free dont-bother-me license. I don't claim this
software won't destroy everything that you hold dear, but I really
doubt it will. And please try not to take credit for others' work.
@author James Wynn
@date 4/15/2009
*/
/**
Implementation header file for Linux based on inotify.
*/
#ifndef _FW_FILEWATCHERLINUX_H_
#define _FW_FILEWATCHERLINUX_H_
#pragma once
#include "FileWatcher.h"
/**
Released under a free dont-bother-me license. I don't claim this
software won't destroy everything that you hold dear, but I really
doubt it will. And please try not to take credit for others' work.
@author James Wynn
@date 4/15/2009
*/
/**
Implementation header file for Linux based on inotify.
*/
#ifndef _FW_FILEWATCHERLINUX_H_
#define _FW_FILEWATCHERLINUX_H_
#pragma once
#include "FileWatcher.h"
#include <map>
#include <sys/types.h>
namespace FW
{
// forward decl
struct WatchStruct;
///
/// @class FileWatcherLinux
class FileWatcherLinux
{
public:
/// type for a map from WatchID to WatchStruct pointer
typedef std::map<WatchID, WatchStruct*> WatchMap;
public:
///
///
FileWatcherLinux();
///
///
virtual ~FileWatcherLinux();
/// Add a directory watch
WatchID addWatch(const String& directory, FileWatchListener* watcher);
/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch(const String& directory);
/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch(WatchID watchid);
/// Updates the watcher. Must be called often.
void update();
/// Handles the action
void handleAction(WatchStruct* watch, const String& filename, unsigned long action);
private:
/// Map of WatchID to WatchStruct pointers
WatchMap mWatches;
/// The last watchid
namespace FW
{
// forward decl
struct WatchStruct;
///
/// @class FileWatcherLinux
class FileWatcherLinux
{
public:
/// type for a map from WatchID to WatchStruct pointer
typedef std::map<WatchID, WatchStruct*> WatchMap;
public:
///
///
FileWatcherLinux();
///
///
virtual ~FileWatcherLinux();
/// Add a directory watch
WatchID addWatch(const String& directory, FileWatchListener* watcher);
/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch(const String& directory);
/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch(WatchID watchid);
/// Updates the watcher. Must be called often.
void update();
/// Handles the action
void handleAction(WatchStruct* watch, const String& filename, unsigned long action);
private:
/// Map of WatchID to WatchStruct pointers
WatchMap mWatches;
/// The last watchid
WatchID mLastWatchID;
/// inotify file descriptor
int mFD;
@ -68,9 +68,9 @@ namespace FW
struct timeval mTimeOut;
/// File descriptor set
fd_set mDescriptorSet;
};//end FileWatcherLinux
};//namespace FW
#endif//_FW_FILEWATCHERLINUX_H_
};//end FileWatcherLinux
};//namespace FW
#endif//_FW_FILEWATCHERLINUX_H_

View File

@ -7,7 +7,7 @@
@date 4/15/2009
*/
#ifdef __APPLE_CC__
#if defined(WITH_KQUEUE) || defined(__APPLE_CC__)
#include "FileWatcherOSX.h"
@ -129,4 +129,4 @@ namespace FW
};//namespace FW
#endif//__APPLE_CC__
#endif // WITH_KQUEUE || __APPLE_CC__

View File

@ -15,8 +15,6 @@
#define _FW_FILEWATCHEROSX_H_
#pragma once
#ifdef __APPLE_CC__
#include "FileWatcher.h"
#include <map>
#include <sys/types.h>
@ -71,6 +69,4 @@ namespace FW
};//namespace FW
#endif//__APPLE_CC__
#endif//_FW_FILEWATCHEROSX_H_

View File

@ -16,8 +16,6 @@
#define _FW_FILEWATCHERWIN32_H_
#pragma once
#ifdef _WIN32
#include "FileWatcher.h"
#include <map>
@ -68,6 +66,4 @@ namespace FW
};//namespace FW
#endif//_WIN32
#endif//_FW_FILEWATCHERWIN32_H_

View File

@ -21,7 +21,9 @@ struct CFW_Impl: public FileWatchListener
void * myCallbackData;
public:
CFW_Impl( const char * dir, CFW_ActionCallback * callback, void * callbackData ):
CFW_Impl( const char * dir,
CFW_ActionCallback * callback,
void * callbackData ):
myID( myWatcher.addWatch( dir, this ) ),
myCallback( callback ),
myCallbackData( callbackData )
@ -31,10 +33,18 @@ struct CFW_Impl: public FileWatchListener
{
myWatcher.removeWatch( myID );
}
virtual void handleFileAction( WatchID watchid, const String& dir, const String& filename, FileWatcher::Action action )
public:
virtual void handleFileAction( WatchID watchid,
const String & dir,
const String & filename,
FileWatcher::Action action )
{
std::cerr << __FILE__ << ':' << __LINE__ << " dir is " << dir << " filename is " << filename << std::endl;
(*myCallback)( this, dir.c_str(), filename.c_str(), (CFW_Action)action, myCallbackData );
(*myCallback)( this,
dir.c_str(),
filename.c_str(),
(CFW_Action)action,
myCallbackData );
}
void update( )
{
@ -42,20 +52,25 @@ std::cerr << __FILE__ << ':' << __LINE__ << " dir is " << dir << " filename is "
}
};
extern "C" CFW_Watch*
cfw_addWatch( const char * directory, CFW_ActionCallback * callback, void * callbackData )
extern "C"
{
return new CFW_Impl( directory, callback, callbackData );
}
CFW_Watch*
cfw_addWatch( const char * directory, CFW_ActionCallback * callback, void * callbackData )
{
return new CFW_Impl( directory, callback, callbackData );
}
extern "C" void
cfw_removeWatch( CFW_Watch * watch )
{
delete watch;
}
void
cfw_removeWatch( CFW_Watch * watch )
{
if( watch != 0 )
delete watch;
}
extern "C" void
cfw_update( CFW_Watch * watch )
{
watch->update( );
void
cfw_update( CFW_Watch * watch )
{
if( watch != 0 )
watch->update( );
}
}

View File

@ -28,13 +28,19 @@ CFW_Action;
typedef struct CFW_Impl CFW_Watch;
typedef void ( CFW_ActionCallback )( CFW_Watch*, const char * dir, const char * filename, CFW_Action, void * callbackData );
typedef void ( CFW_ActionCallback )( CFW_Watch * watch,
const char * dir,
const char * filename,
CFW_Action action,
void * callbackData );
CFW_Watch* cfw_addWatch ( const char * directory, CFW_ActionCallback * callback, void * callbackData );
CFW_Watch* cfw_addWatch ( const char * directory,
CFW_ActionCallback * callback,
void * callbackData );
void cfw_removeWatch ( CFW_Watch * );
void cfw_removeWatch ( CFW_Watch * watch );
void cfw_update ( CFW_Watch * );
void cfw_update ( CFW_Watch * watch );
#ifdef __cplusplus