From 09161120eba96f41631b5fdb105ab234762540a4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 28 Feb 2009 14:43:37 +0000 Subject: [PATCH] better configure-time checking for inotify & kqueue. --- configure.ac | 35 +++++ daemon/daemon.c | 21 ++- third-party/filewatcher/FileWatcher.cpp | 4 +- third-party/filewatcher/FileWatcherLinux.cpp | 4 +- third-party/filewatcher/FileWatcherLinux.h | 134 +++++++++---------- third-party/filewatcher/FileWatcherOSX.cpp | 4 +- third-party/filewatcher/FileWatcherOSX.h | 4 - third-party/filewatcher/FileWatcherWin32.h | 4 - third-party/filewatcher/file-watcher.cpp | 49 ++++--- third-party/filewatcher/file-watcher.h | 14 +- 10 files changed, 165 insertions(+), 108 deletions(-) diff --git a/configure.ac b/configure.ac index 7389f4706..c0fc996ff 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/daemon/daemon.c b/daemon/daemon.c index 5d1edb674..a907ceda4 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -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 ); } diff --git a/third-party/filewatcher/FileWatcher.cpp b/third-party/filewatcher/FileWatcher.cpp index b380f90e3..b506e9ed3 100644 --- a/third-party/filewatcher/FileWatcher.cpp +++ b/third-party/filewatcher/FileWatcher.cpp @@ -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 diff --git a/third-party/filewatcher/FileWatcherLinux.cpp b/third-party/filewatcher/FileWatcherLinux.cpp index f14af37a3..71c2bb72a 100644 --- a/third-party/filewatcher/FileWatcherLinux.cpp +++ b/third-party/filewatcher/FileWatcherLinux.cpp @@ -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__ diff --git a/third-party/filewatcher/FileWatcherLinux.h b/third-party/filewatcher/FileWatcherLinux.h index 5c446f636..4994d52aa 100644 --- a/third-party/filewatcher/FileWatcherLinux.h +++ b/third-party/filewatcher/FileWatcherLinux.h @@ -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 #include - -namespace FW -{ - - // forward decl - struct WatchStruct; - - /// - /// @class FileWatcherLinux - class FileWatcherLinux - { - public: - /// type for a map from WatchID to WatchStruct pointer - typedef std::map 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 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_ diff --git a/third-party/filewatcher/FileWatcherOSX.cpp b/third-party/filewatcher/FileWatcherOSX.cpp index 4a4c5a04a..6d4362f6e 100644 --- a/third-party/filewatcher/FileWatcherOSX.cpp +++ b/third-party/filewatcher/FileWatcherOSX.cpp @@ -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__ diff --git a/third-party/filewatcher/FileWatcherOSX.h b/third-party/filewatcher/FileWatcherOSX.h index 719ea87c3..0d032358b 100644 --- a/third-party/filewatcher/FileWatcherOSX.h +++ b/third-party/filewatcher/FileWatcherOSX.h @@ -15,8 +15,6 @@ #define _FW_FILEWATCHEROSX_H_ #pragma once -#ifdef __APPLE_CC__ - #include "FileWatcher.h" #include #include @@ -71,6 +69,4 @@ namespace FW };//namespace FW -#endif//__APPLE_CC__ - #endif//_FW_FILEWATCHEROSX_H_ diff --git a/third-party/filewatcher/FileWatcherWin32.h b/third-party/filewatcher/FileWatcherWin32.h index ef8d0c9fa..17470eba2 100644 --- a/third-party/filewatcher/FileWatcherWin32.h +++ b/third-party/filewatcher/FileWatcherWin32.h @@ -16,8 +16,6 @@ #define _FW_FILEWATCHERWIN32_H_ #pragma once -#ifdef _WIN32 - #include "FileWatcher.h" #include @@ -68,6 +66,4 @@ namespace FW };//namespace FW -#endif//_WIN32 - #endif//_FW_FILEWATCHERWIN32_H_ diff --git a/third-party/filewatcher/file-watcher.cpp b/third-party/filewatcher/file-watcher.cpp index 93651c620..d91f1d71f 100644 --- a/third-party/filewatcher/file-watcher.cpp +++ b/third-party/filewatcher/file-watcher.cpp @@ -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( ); + } } diff --git a/third-party/filewatcher/file-watcher.h b/third-party/filewatcher/file-watcher.h index 6e25a1c72..be7981e3a 100644 --- a/third-party/filewatcher/file-watcher.h +++ b/third-party/filewatcher/file-watcher.h @@ -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