Changeset 7979 for trunk/daemon/daemon.c


Ignore:
Timestamp:
Mar 1, 2009, 3:38:58 PM (13 years ago)
Author:
charles
Message:

(trunk daemon) #1882: take KyleK's suggestion of using inotify when possible. As he writes, "the opendir() approach will basically prevent my NAS drives to go to standby."

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r7974 r7979  
    1919#include <sys/types.h> /* umask*/
    2020#include <sys/stat.h> /* umask*/
    21 #include <dirent.h> /* readdir */
    2221
    2322#include <fcntl.h> /* open */
     
    3332#include <libtransmission/version.h>
    3433
     34#include "watch.h"
     35
    3536#define MY_NAME "transmission-daemon"
    3637
     
    3839#define PREF_KEY_DIR_WATCH_ENABLED  "watch-dir-enabled"
    3940
    40 #define WATCHDIR_POLL_INTERVAL_SECS  15
    41 
    42 
    43 static int           closing = FALSE;
    44 static tr_session  * mySession = NULL;
     41
     42static tr_bool closing = FALSE;
     43static tr_session * mySession = NULL;
    4544
    4645/***
     
    182181}
    183182
    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  */
    190183static void
    191 checkForNewFiles( 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 );
     184onFileAdded( tr_session * session, const char * dir, const char * file )
     185{
     186    if( strstr( file, ".torrent" ) != NULL )
     187    {
     188        char * filename = tr_buildPath( dir, file, NULL );
     189        tr_ctor * ctor = tr_ctorNew( session );
     190
     191        int err = tr_ctorSetMetainfoFromFile( ctor, filename );
     192        if( !err )
     193            tr_torrentNew( session, ctor, &err );
     194
     195        tr_ctorFree( ctor );
     196        tr_free( filename );
    224197    }
    225198}
     
    235208    tr_bool dumpSettings = FALSE;
    236209    const char * configDir = NULL;
    237     const char * watchDir = NULL;
    238     time_t lastCheckTime = 0;
     210    dtr_watchdir * watchdir = NULL;
    239211
    240212    signal( SIGINT, gotsig );
     
    336308    {
    337309        int64_t doWatch;
     310        const char * dir;
     311
    338312        if( tr_bencDictFindInt( &settings, PREF_KEY_DIR_WATCH_ENABLED, &doWatch )
    339313            && doWatch
    340             && tr_bencDictFindStr( &settings, PREF_KEY_DIR_WATCH, &watchDir )
    341             && watchDir
    342             && *watchDir )
     314            && tr_bencDictFindStr( &settings, PREF_KEY_DIR_WATCH, &dir )
     315            && dir
     316            && *dir )
    343317        {
    344             tr_ninf( MY_NAME, "watching \"%s\" for added .torrent files", watchDir );
     318            tr_inf( "Watching \"%s\" for new .torrent files", dir );
     319            watchdir = dtr_watchdir_new( mySession, dir, onFileAdded );
    345320        }
    346321    }
     
    357332    {
    358333        tr_wait( 1000 ); /* sleep one second */
    359 
    360         if( watchDir && ( lastCheckTime + WATCHDIR_POLL_INTERVAL_SECS < time( NULL ) ) )
    361         {
    362             checkForNewFiles( mySession, watchDir, lastCheckTime );
    363             lastCheckTime = time( NULL );
    364         }
     334        dtr_watchdir_update( watchdir );
    365335    }
    366336
     
    368338    printf( "Closing transmission session..." );
    369339    tr_sessionSaveSettings( mySession, configDir, &settings );
     340    dtr_watchdir_free( watchdir );
    370341    tr_sessionClose( mySession );
    371342    printf( " done.\n" );
Note: See TracChangeset for help on using the changeset viewer.