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 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 dnl
dnl posix_fadvise dnl posix_fadvise

View File

@ -181,16 +181,23 @@ getConfigDir( int argc, const char ** argv )
} }
static void 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; int err;
char * path = tr_buildPath( directory, filename, NULL ); char * path = tr_buildPath( directory, filename, NULL );
tr_session * session = userData; tr_session * session = userData;
tr_ctor * ctor = tr_ctorNew( session ); 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 ); tr_free( path );
} }
} }
@ -325,9 +332,11 @@ main( int argc, char ** argv )
tr_ctorFree( ctor ); tr_ctorFree( ctor );
} }
while( !closing ) { while( !closing )
{
tr_wait( 1000 ); /* sleep one second */ tr_wait( 1000 ); /* sleep one second */
if( watch )
if( watch != NULL ) /* maybe look for new .torrent files */
cfw_update( watch ); cfw_update( watch );
} }

View File

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

View File

@ -7,7 +7,7 @@
@date 4/15/2009 @date 4/15/2009
*/ */
#ifdef __linux__ #if defined(WITH_INOTIFY)
#include "FileWatcherLinux.h" #include "FileWatcherLinux.h"
@ -163,4 +163,4 @@ namespace FW
};//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 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 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. doubt it will. And please try not to take credit for others' work.
@author James Wynn @author James Wynn
@date 4/15/2009 @date 4/15/2009
*/ */
/** /**
Implementation header file for Linux based on inotify. Implementation header file for Linux based on inotify.
*/ */
#ifndef _FW_FILEWATCHERLINUX_H_ #ifndef _FW_FILEWATCHERLINUX_H_
#define _FW_FILEWATCHERLINUX_H_ #define _FW_FILEWATCHERLINUX_H_
#pragma once #pragma once
#include "FileWatcher.h" #include "FileWatcher.h"
#include <map> #include <map>
#include <sys/types.h> #include <sys/types.h>
namespace FW namespace FW
{ {
// forward decl // forward decl
struct WatchStruct; struct WatchStruct;
/// ///
/// @class FileWatcherLinux /// @class FileWatcherLinux
class FileWatcherLinux class FileWatcherLinux
{ {
public: public:
/// type for a map from WatchID to WatchStruct pointer /// type for a map from WatchID to WatchStruct pointer
typedef std::map<WatchID, WatchStruct*> WatchMap; typedef std::map<WatchID, WatchStruct*> WatchMap;
public: public:
/// ///
/// ///
FileWatcherLinux(); FileWatcherLinux();
/// ///
/// ///
virtual ~FileWatcherLinux(); virtual ~FileWatcherLinux();
/// Add a directory watch /// Add a directory watch
WatchID addWatch(const String& directory, FileWatchListener* watcher); WatchID addWatch(const String& directory, FileWatchListener* watcher);
/// Remove a directory watch. This is a brute force lazy search O(nlogn). /// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch(const String& directory); void removeWatch(const String& directory);
/// Remove a directory watch. This is a map lookup O(logn). /// Remove a directory watch. This is a map lookup O(logn).
void removeWatch(WatchID watchid); void removeWatch(WatchID watchid);
/// Updates the watcher. Must be called often. /// Updates the watcher. Must be called often.
void update(); void update();
/// Handles the action /// Handles the action
void handleAction(WatchStruct* watch, const String& filename, unsigned long action); void handleAction(WatchStruct* watch, const String& filename, unsigned long action);
private: private:
/// Map of WatchID to WatchStruct pointers /// Map of WatchID to WatchStruct pointers
WatchMap mWatches; WatchMap mWatches;
/// The last watchid /// The last watchid
WatchID mLastWatchID; WatchID mLastWatchID;
/// inotify file descriptor /// inotify file descriptor
int mFD; int mFD;
@ -68,9 +68,9 @@ namespace FW
struct timeval mTimeOut; struct timeval mTimeOut;
/// File descriptor set /// File descriptor set
fd_set mDescriptorSet; fd_set mDescriptorSet;
};//end FileWatcherLinux };//end FileWatcherLinux
};//namespace FW };//namespace FW
#endif//_FW_FILEWATCHERLINUX_H_ #endif//_FW_FILEWATCHERLINUX_H_

View File

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

View File

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

View File

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

View File

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

View File

@ -28,13 +28,19 @@ CFW_Action;
typedef struct CFW_Impl CFW_Watch; 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 #ifdef __cplusplus