Changeset 7974


Ignore:
Timestamp:
Feb 28, 2009, 3:27:39 PM (13 years ago)
Author:
charles
Message:

(trunk daemon) another experiment: implement the watchdir with opendir() and readdir(), since those are available on every system we're on, even embedded ones...

Location:
trunk
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r7973 r7974  
    100100
    101101AC_SYS_LARGEFILE
    102 
    103 
    104 dnl ----------------------------------------------------------------------------
    105 dnl
    106 dnl  third_party/FilewWatcher tests: look for inotify and kqueue
    107 
    108 AC_CHECK_HEADER([sys/inotify.h],
    109                 [AC_CHECK_FUNC([inotify_init],[have_inotify="yes"],[have_inotify="no"])],
    110                 [have_inotify="no"])
    111 AC_ARG_WITH([inotify],
    112             [AS_HELP_STRING([--with-inotify],[Enable inotify support (default=auto)])],
    113             [want_inotify=${enableval}],
    114             [want_inotify=${have_inotify}])
    115 if test "x$want_inotify" = "xyes" ; then
    116     if test "x$have_inotify" = "xyes"; then
    117       AC_DEFINE([WITH_INOTIFY],[1])
    118     else
    119       AC_MSG_ERROR("inotify not found!")
    120     fi
    121 fi
    122 
    123 AC_CHECK_HEADER([sys/event.h],
    124                 [AC_CHECK_FUNC([kqueue],[have_kqueue="yes"],[have_kqueue="no"])],
    125                 [have_kqueue="no"])
    126 AC_ARG_WITH([kqueue],
    127             [AS_HELP_STRING([--with-kqueue],[Enable kqueue support (default=auto)])],
    128             [want_kqueue=${enableval}],
    129             [want_kqueue=${have_kqueue}])
    130 if test "x$want_kqueue" = "xyes" ; then
    131     if test "x$have_kqueue" = "xyes"; then
    132       AC_DEFINE([WITH_KQUEUE],[1])
    133     else
    134       AC_MSG_ERROR("kqueue not found!")
    135     fi
    136 fi
    137102
    138103
     
    373338                 libtransmission/Makefile
    374339                 third-party/Makefile
    375                  third-party/filewatcher/Makefile
    376340                 third-party/miniupnp/Makefile
    377341                 third-party/libnatpmp/Makefile
  • trunk/daemon/Makefile.am

    r7971 r7974  
    2020LDADD = \
    2121    $(top_builddir)/libtransmission/libtransmission.a \
    22     $(top_builddir)/third-party/filewatcher/libfilewatcher.a -lstdc++ \
    2322    $(top_builddir)/third-party/miniupnp/libminiupnp.a \
    2423    $(top_builddir)/third-party/libnatpmp/libnatpmp.a \
  • trunk/daemon/daemon.c

    r7973 r7974  
    1919#include <sys/types.h> /* umask*/
    2020#include <sys/stat.h> /* umask*/
     21#include <dirent.h> /* readdir */
    2122
    2223#include <fcntl.h> /* open */
     
    3233#include <libtransmission/version.h>
    3334
    34 #include <filewatcher/file-watcher.h>
    35 
    3635#define MY_NAME "transmission-daemon"
    3736
    3837#define PREF_KEY_DIR_WATCH          "watch-dir"
    3938#define PREF_KEY_DIR_WATCH_ENABLED  "watch-dir-enabled"
     39
     40#define WATCHDIR_POLL_INTERVAL_SECS  15
    4041
    4142
     
    181182}
    182183
     184/**
     185 * This is crude compared to using kqueue/inotify/etc, but has the advantage
     186 * of working portably on whatever random embedded platform we throw at it.
     187 * Since we're only walking a single directory, nonrecursively, a few times
     188 * per minute, let's go with this unless users complain about the load
     189 */
    183190static void
    184 dirChangedCB( CFW_Watch  * watch UNUSED,
    185               const char * directory,
    186               const char * filename,
    187               CFW_Action   action,
    188               void       * userData )
    189 {
    190     if( ( action & CFW_ACTION_ADD ) && ( strstr( filename, ".torrent" ) != NULL ) )
    191     {
    192         int err;
    193         char * path = tr_buildPath( directory, filename, NULL );
    194         tr_session * session = userData;
    195         tr_ctor * ctor = tr_ctorNew( session );
    196 
    197         err = tr_ctorSetMetainfoFromFile( ctor, path );
    198         if( !err )
    199             tr_torrentNew( session, ctor, &err );
    200 
    201         tr_free( path );
     191checkForNewFiles( tr_session * session, const char * dirname, time_t oldTime )
     192{
     193    struct stat sb;
     194    DIR * odir;
     195
     196    if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && (( odir = opendir( dirname ))) )
     197    {
     198        struct dirent * d;
     199
     200        for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
     201        {
     202            char * filename;
     203
     204            if( !d->d_name || *d->d_name=='.' ) /* skip dotfiles */
     205                continue;
     206            if( !strstr( d->d_name, ".torrent" ) ) /* skip non-torrents */
     207                continue;
     208
     209            /* if the file's changed since our last pass, try adding it */
     210            filename = tr_buildPath( dirname, d->d_name, NULL );
     211            if( !stat( filename, &sb ) && sb.st_mtime >= oldTime )
     212            {
     213                tr_ctor * ctor = tr_ctorNew( session );
     214                int err = tr_ctorSetMetainfoFromFile( ctor, filename );
     215                if( !err )
     216                    tr_torrentNew( session, ctor, &err );
     217                tr_ctorFree( ctor );
     218            }
     219
     220            tr_free( filename );
     221        }
     222
     223        closedir( odir );
    202224    }
    203225}
     
    213235    tr_bool dumpSettings = FALSE;
    214236    const char * configDir = NULL;
    215     CFW_Watch * watch = NULL;
     237    const char * watchDir = NULL;
     238    time_t lastCheckTime = 0;
    216239
    217240    signal( SIGINT, gotsig );
     
    313336    {
    314337        int64_t doWatch;
    315         const char * watchDir;
    316338        if( tr_bencDictFindInt( &settings, PREF_KEY_DIR_WATCH_ENABLED, &doWatch )
    317339            && doWatch
     
    321343        {
    322344            tr_ninf( MY_NAME, "watching \"%s\" for added .torrent files", watchDir );
    323             watch = cfw_addWatch( watchDir, dirChangedCB, mySession );
    324345        }
    325346    }
     
    337358        tr_wait( 1000 ); /* sleep one second */
    338359
    339         if( watch != NULL ) /* maybe look for new .torrent files */
    340             cfw_update( watch );
     360        if( watchDir && ( lastCheckTime + WATCHDIR_POLL_INTERVAL_SECS < time( NULL ) ) )
     361        {
     362            checkForNewFiles( mySession, watchDir, lastCheckTime );
     363            lastCheckTime = time( NULL );
     364        }
    341365    }
    342366
    343367    /* shutdown */
    344368    printf( "Closing transmission session..." );
    345     if( watch )
    346         cfw_removeWatch( watch );
    347369    tr_sessionSaveSettings( mySession, configDir, &settings );
    348370    tr_sessionClose( mySession );
  • trunk/third-party/Makefile.am

    r7971 r7974  
    11SUBDIRS = \
    2     filewatcher \
    32    libevent \
    43    libnatpmp \
Note: See TracChangeset for help on using the changeset viewer.