From 2060557f6d19b73b2dde53106d1ade5ff2710f9a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 27 Feb 2010 22:13:29 +0000 Subject: [PATCH] (trunk daemon) #2949 "standardize inotify watchdir implementation" -- fixed in trunk for 1.92. Thanks to Longinus00 for another patch :) --- daemon/watch.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/daemon/watch.c b/daemon/watch.c index 8d176f5ac..c568fc05c 100644 --- a/daemon/watch.c +++ b/daemon/watch.c @@ -16,13 +16,14 @@ #else #include /* stat */ #include /* stat */ - #include /* readdir */ #include /* evbuffer */ #endif #include #include /* strstr */ +#include /* readdir */ + #include #include /* tr_buildPath(), tr_inf() */ #include "watch.h" @@ -65,17 +66,41 @@ str_has_suffix( const char *str, const char *suffix ) /* reasonable guess as to size of 50 events */ #define BUF_LEN (EVENT_BATCH_COUNT * (EVENT_SIZE + 16) + 2048) -#define DTR_INOTIFY_MASK (IN_CLOSE_WRITE|IN_MOVED_TO) +#define DTR_INOTIFY_MASK (IN_CLOSE_WRITE|IN_MOVED_TO|IN_ONLYDIR) static void watchdir_new_impl( dtr_watchdir * w ) { int i; + DIR * odir; w->inotify_fd = inotify_init( ); tr_inf( "Using inotify to watch directory \"%s\"", w->dir ); i = inotify_add_watch( w->inotify_fd, w->dir, DTR_INOTIFY_MASK ); + if( i < 0 ) + { tr_err( "Unable to watch \"%s\": %s", w->dir, strerror (errno) ); + } + else if(( odir = opendir( w->dir ))) + { + struct dirent * d; + + while(( d = readdir( odir ))) + { + const char * name = d->d_name; + + if( !name || *name=='.' ) /* skip dotfiles */ + continue; + if( !str_has_suffix( name, ".torrent" ) ) /* skip non-torrents */ + continue; + + tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir ); + w->callback( w->session, w->dir, name ); + } + + closedir( odir ); + } + } static void watchdir_free_impl( dtr_watchdir * w )