source: trunk/daemon/watch.c @ 10390

Last change on this file since 10390 was 10390, checked in by charles, 12 years ago

(trunk) one of the least interesting commits ever: remove trailing spaces from source code lines...

  • Property svn:keywords set to Date Rev Author Id
File size: 6.6 KB
Line 
1/*
2 * This file Copyright (C) 2009-2010 Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: watch.c 10390 2010-03-17 17:07:40Z charles $
11 */
12#ifdef WITH_INOTIFY
13  #include <sys/inotify.h>
14  #include <sys/select.h>
15  #include <unistd.h> /* close */
16#else
17  #include <sys/types.h> /* stat */
18  #include <sys/stat.h> /* stat */
19  #include <event.h> /* evbuffer */
20#endif
21
22#include <errno.h>
23#include <string.h> /* strstr */
24
25#include <dirent.h> /* readdir */
26
27#include <libtransmission/transmission.h>
28#include <libtransmission/utils.h> /* tr_buildPath(), tr_inf() */
29#include "watch.h"
30
31struct dtr_watchdir
32{
33    tr_session * session;
34    char * dir;
35    dtr_watchdir_callback * callback;
36#ifdef WITH_INOTIFY
37    int inotify_fd;
38#else /* readdir implementation */
39    time_t lastTimeChecked;
40    struct evbuffer * lastFiles;
41#endif
42};
43
44static tr_bool
45str_has_suffix( const char *str, const char *suffix )
46{
47    const size_t str_len = strlen( str );
48    const size_t suffix_len = strlen( suffix );
49
50    if( str_len < suffix_len )
51        return FALSE;
52
53    return !strncasecmp( str + str_len - suffix_len, suffix, suffix_len );
54}
55
56/***
57****  INOTIFY IMPLEMENTATION
58***/
59
60#if defined(WITH_INOTIFY)
61
62/* how many inotify events to try to batch into a single read */
63#define EVENT_BATCH_COUNT 50
64/* size of the event structure, not counting name */
65#define EVENT_SIZE  (sizeof (struct inotify_event))
66/* reasonable guess as to size of 50 events */
67#define BUF_LEN (EVENT_BATCH_COUNT * (EVENT_SIZE + 16) + 2048)
68
69#define DTR_INOTIFY_MASK (IN_CLOSE_WRITE|IN_MOVED_TO|IN_ONLYDIR)
70
71static void
72watchdir_new_impl( dtr_watchdir * w )
73{
74    int i;
75    DIR * odir;
76    w->inotify_fd = inotify_init( );
77    tr_inf( "Using inotify to watch directory \"%s\"", w->dir );
78    i = inotify_add_watch( w->inotify_fd, w->dir, DTR_INOTIFY_MASK );
79
80    if( i < 0 )
81    {
82        tr_err( "Unable to watch \"%s\": %s", w->dir, strerror (errno) );
83    }
84    else if(( odir = opendir( w->dir )))
85    {
86        struct dirent * d;
87
88        while(( d = readdir( odir )))
89        {
90            const char * name = d->d_name;
91
92            if( !name || *name=='.' ) /* skip dotfiles */
93                continue;
94            if( !str_has_suffix( name, ".torrent" ) ) /* skip non-torrents */
95                continue;
96
97            tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir );
98            w->callback( w->session, w->dir, name );
99        }
100
101        closedir( odir );
102    }
103
104}
105static void
106watchdir_free_impl( dtr_watchdir * w )
107{
108    inotify_rm_watch( w->inotify_fd, DTR_INOTIFY_MASK );
109    close( w->inotify_fd );
110}
111static void
112watchdir_update_impl( dtr_watchdir * w )
113{
114    int ret;
115    fd_set rfds;
116    struct timeval time;
117    const int fd = w->inotify_fd;
118
119    /* timeout after one second */
120    time.tv_sec = 1;
121    time.tv_usec = 0;
122
123    /* make the fd_set hold the inotify fd */
124    FD_ZERO( &rfds );
125    FD_SET( fd, &rfds );
126
127    /* check for added files */
128    ret = select( fd+1, &rfds, NULL, NULL, &time );
129    if( ret < 0 ) {
130        perror( "select" );
131    } else if( !ret ) {
132        /* timed out! */
133    } else if( FD_ISSET( fd, &rfds ) ) {
134        int i = 0;
135        char buf[BUF_LEN];
136        int len = read( fd, buf, sizeof( buf ) );
137        while (i < len) {
138            struct inotify_event * event = (struct inotify_event *) &buf[i];
139            const char * name = event->name;
140            if( str_has_suffix( name, ".torrent" ) )
141            {
142                tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir );
143                w->callback( w->session, w->dir, name );
144            }
145            i += EVENT_SIZE +  event->len;
146        }
147    }
148}
149
150#else /* WITH_INOTIFY */
151
152/***
153****  READDIR IMPLEMENTATION
154***/
155
156#define WATCHDIR_POLL_INTERVAL_SECS 10
157
158#define FILE_DELIMITER '\0'
159
160static void
161watchdir_new_impl( dtr_watchdir * w UNUSED )
162{
163    tr_inf( "Using readdir to watch directory \"%s\"", w->dir );
164    w->lastFiles = evbuffer_new( );
165}
166static void
167watchdir_free_impl( dtr_watchdir * w )
168{
169    evbuffer_free( w->lastFiles );
170}
171static void
172add_file_to_list( struct evbuffer * buf, const char * filename, size_t len )
173{
174    const char delimiter = FILE_DELIMITER;
175    evbuffer_add( buf, &delimiter, 1 );
176    evbuffer_add( buf, filename, len );
177    evbuffer_add( buf, &delimiter, 1 );
178}
179static tr_bool
180is_file_in_list( struct evbuffer * buf, const char * filename, size_t len )
181{
182    tr_bool in_list;
183    struct evbuffer * test = evbuffer_new( );
184    add_file_to_list( test, filename, len );
185    in_list = evbuffer_find( buf, EVBUFFER_DATA( test ), EVBUFFER_LENGTH( test ) ) != NULL;
186    evbuffer_free( test );
187    return in_list;
188}
189static void
190watchdir_update_impl( dtr_watchdir * w )
191{
192    struct stat sb;
193    DIR * odir;
194    const time_t oldTime = w->lastTimeChecked;
195    const char * dirname = w->dir;
196    struct evbuffer * curFiles = evbuffer_new( );
197
198    if ( ( oldTime + WATCHDIR_POLL_INTERVAL_SECS < time( NULL ) )
199         && !stat( dirname, &sb )
200         && S_ISDIR( sb.st_mode )
201         && (( odir = opendir( dirname ))) )
202    {
203        struct dirent * d;
204
205        for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
206        {
207            size_t len;
208            const char * name = d->d_name;
209
210            if( !name || *name=='.' ) /* skip dotfiles */
211                continue;
212            if( !str_has_suffix( name, ".torrent" ) ) /* skip non-torrents */
213                continue;
214
215            len = strlen( name );
216            add_file_to_list( curFiles, name, len );
217
218            /* if this file wasn't here last time, try adding it */
219            if( !is_file_in_list( w->lastFiles, name, len ) ) {
220                tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir );
221                w->callback( w->session, w->dir, name );
222            }
223        }
224
225        closedir( odir );
226        w->lastTimeChecked = time( NULL );
227        evbuffer_free( w->lastFiles );
228        w->lastFiles = curFiles;
229    }
230}
231
232#endif
233
234/***
235****
236***/
237
238dtr_watchdir*
239dtr_watchdir_new( tr_session * session, const char * dir, dtr_watchdir_callback * callback )
240{
241    dtr_watchdir * w = tr_new0( dtr_watchdir, 1 );
242
243    w->session = session;
244    w->dir = tr_strdup( dir );
245    w->callback = callback;
246
247    watchdir_new_impl( w );
248
249    return w;
250}
251
252void
253dtr_watchdir_update( dtr_watchdir * w )
254{
255    if( w != NULL )
256        watchdir_update_impl( w );
257}
258
259void
260dtr_watchdir_free( dtr_watchdir * w )
261{
262    if( w != NULL )
263    {
264        watchdir_free_impl( w );
265        tr_free( w->dir );
266        tr_free( w );
267    }
268}
Note: See TracBrowser for help on using the repository browser.