2009-03-01 15:38:58 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2009-2010 Mnemosyne LLC
|
2009-03-01 15:38:58 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2009-06-05 17:52:02 +00:00
|
|
|
* $Id$
|
2009-03-01 15:38:58 +00:00
|
|
|
*/
|
|
|
|
#ifdef WITH_INOTIFY
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <unistd.h> /* close */
|
|
|
|
#else
|
|
|
|
#include <sys/types.h> /* stat */
|
|
|
|
#include <sys/stat.h> /* stat */
|
|
|
|
#include <dirent.h> /* readdir */
|
2009-06-05 17:02:29 +00:00
|
|
|
#include <event.h> /* evbuffer */
|
2009-03-01 15:38:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h> /* strstr */
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2009-06-05 17:02:29 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_buildPath(), tr_inf() */
|
2009-03-01 15:38:58 +00:00
|
|
|
#include "watch.h"
|
|
|
|
|
|
|
|
struct dtr_watchdir
|
|
|
|
{
|
|
|
|
tr_session * session;
|
|
|
|
char * dir;
|
|
|
|
dtr_watchdir_callback * callback;
|
|
|
|
#ifdef WITH_INOTIFY
|
|
|
|
int inotify_fd;
|
|
|
|
#else /* readdir implementation */
|
|
|
|
time_t lastTimeChecked;
|
2009-06-05 17:02:29 +00:00
|
|
|
struct evbuffer * lastFiles;
|
2009-03-01 15:38:58 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-02-21 00:24:01 +00:00
|
|
|
static tr_bool
|
|
|
|
str_has_suffix( const char *str, const char *suffix )
|
|
|
|
{
|
|
|
|
const size_t str_len = strlen( str );
|
|
|
|
const size_t suffix_len = strlen( suffix );
|
|
|
|
|
|
|
|
if( str_len < suffix_len )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return !strncasecmp( str + str_len - suffix_len, suffix, suffix_len );
|
|
|
|
}
|
|
|
|
|
2009-03-01 15:38:58 +00:00
|
|
|
/***
|
|
|
|
**** INOTIFY IMPLEMENTATION
|
|
|
|
***/
|
|
|
|
|
|
|
|
#if defined(WITH_INOTIFY)
|
|
|
|
|
|
|
|
/* how many inotify events to try to batch into a single read */
|
|
|
|
#define EVENT_BATCH_COUNT 50
|
|
|
|
/* size of the event structure, not counting name */
|
|
|
|
#define EVENT_SIZE (sizeof (struct inotify_event))
|
|
|
|
/* reasonable guess as to size of 50 events */
|
|
|
|
#define BUF_LEN (EVENT_BATCH_COUNT * (EVENT_SIZE + 16) + 2048)
|
|
|
|
|
2010-02-25 18:21:19 +00:00
|
|
|
#define DTR_INOTIFY_MASK (IN_CLOSE_WRITE|IN_MOVED_TO)
|
2009-03-01 15:38:58 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
watchdir_new_impl( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
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) );
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
watchdir_free_impl( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
inotify_rm_watch( w->inotify_fd, DTR_INOTIFY_MASK );
|
|
|
|
close( w->inotify_fd );
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
watchdir_update_impl( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval time;
|
|
|
|
const int fd = w->inotify_fd;
|
|
|
|
|
|
|
|
/* timeout after one second */
|
|
|
|
time.tv_sec = 1;
|
|
|
|
time.tv_usec = 0;
|
|
|
|
|
|
|
|
/* make the fd_set hold the inotify fd */
|
|
|
|
FD_ZERO( &rfds );
|
|
|
|
FD_SET( fd, &rfds );
|
|
|
|
|
|
|
|
/* check for added files */
|
|
|
|
ret = select( fd+1, &rfds, NULL, NULL, &time );
|
|
|
|
if( ret < 0 ) {
|
|
|
|
perror( "select" );
|
|
|
|
} else if( !ret ) {
|
|
|
|
/* timed out! */
|
|
|
|
} else if( FD_ISSET( fd, &rfds ) ) {
|
|
|
|
int i = 0;
|
|
|
|
char buf[BUF_LEN];
|
|
|
|
int len = read( fd, buf, sizeof( buf ) );
|
|
|
|
while (i < len) {
|
|
|
|
struct inotify_event * event = (struct inotify_event *) &buf[i];
|
2010-02-21 13:29:23 +00:00
|
|
|
const char * name = event->name;
|
|
|
|
if( str_has_suffix( name, ".torrent" ) )
|
2010-02-12 12:50:36 +00:00
|
|
|
{
|
2010-02-21 13:29:23 +00:00
|
|
|
tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir );
|
|
|
|
w->callback( w->session, w->dir, name );
|
2010-02-12 12:50:36 +00:00
|
|
|
}
|
2009-03-01 15:38:58 +00:00
|
|
|
i += EVENT_SIZE + event->len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* WITH_INOTIFY */
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** READDIR IMPLEMENTATION
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define WATCHDIR_POLL_INTERVAL_SECS 10
|
|
|
|
|
2009-06-05 17:02:29 +00:00
|
|
|
#define FILE_DELIMITER '\0'
|
|
|
|
|
2009-03-01 15:38:58 +00:00
|
|
|
static void
|
|
|
|
watchdir_new_impl( dtr_watchdir * w UNUSED )
|
|
|
|
{
|
|
|
|
tr_inf( "Using readdir to watch directory \"%s\"", w->dir );
|
2009-06-05 17:02:29 +00:00
|
|
|
w->lastFiles = evbuffer_new( );
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
watchdir_free_impl( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
evbuffer_free( w->lastFiles );
|
2009-03-01 15:38:58 +00:00
|
|
|
}
|
|
|
|
static void
|
2009-06-05 17:02:29 +00:00
|
|
|
add_file_to_list( struct evbuffer * buf, const char * filename, size_t len )
|
|
|
|
{
|
|
|
|
const char delimiter = FILE_DELIMITER;
|
|
|
|
evbuffer_add( buf, &delimiter, 1 );
|
|
|
|
evbuffer_add( buf, filename, len );
|
|
|
|
evbuffer_add( buf, &delimiter, 1 );
|
|
|
|
}
|
|
|
|
static tr_bool
|
|
|
|
is_file_in_list( struct evbuffer * buf, const char * filename, size_t len )
|
2009-03-01 15:38:58 +00:00
|
|
|
{
|
2009-06-05 17:02:29 +00:00
|
|
|
tr_bool in_list;
|
|
|
|
struct evbuffer * test = evbuffer_new( );
|
|
|
|
add_file_to_list( test, filename, len );
|
2009-06-17 14:49:23 +00:00
|
|
|
in_list = evbuffer_find( buf, EVBUFFER_DATA( test ), EVBUFFER_LENGTH( test ) ) != NULL;
|
2009-06-05 17:02:29 +00:00
|
|
|
evbuffer_free( test );
|
|
|
|
return in_list;
|
2009-03-01 15:38:58 +00:00
|
|
|
}
|
|
|
|
static void
|
|
|
|
watchdir_update_impl( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
DIR * odir;
|
|
|
|
const time_t oldTime = w->lastTimeChecked;
|
|
|
|
const char * dirname = w->dir;
|
2009-06-05 17:02:29 +00:00
|
|
|
struct evbuffer * curFiles = evbuffer_new( );
|
2009-03-01 15:38:58 +00:00
|
|
|
|
|
|
|
if ( ( oldTime + WATCHDIR_POLL_INTERVAL_SECS < time( NULL ) )
|
|
|
|
&& !stat( dirname, &sb )
|
|
|
|
&& S_ISDIR( sb.st_mode )
|
|
|
|
&& (( odir = opendir( dirname ))) )
|
|
|
|
{
|
|
|
|
struct dirent * d;
|
|
|
|
|
|
|
|
for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
|
|
|
|
{
|
2009-06-05 17:02:29 +00:00
|
|
|
size_t len;
|
2010-02-21 13:29:23 +00:00
|
|
|
const char * name = d->d_name;
|
2009-03-01 15:38:58 +00:00
|
|
|
|
2010-02-21 13:29:23 +00:00
|
|
|
if( !name || *name=='.' ) /* skip dotfiles */
|
2009-03-01 15:38:58 +00:00
|
|
|
continue;
|
2010-02-21 13:29:23 +00:00
|
|
|
if( !str_has_suffix( name, ".torrent" ) ) /* skip non-torrents */
|
2009-03-01 15:38:58 +00:00
|
|
|
continue;
|
|
|
|
|
2010-02-21 13:29:23 +00:00
|
|
|
len = strlen( name );
|
|
|
|
add_file_to_list( curFiles, name, len );
|
2009-06-05 17:02:29 +00:00
|
|
|
|
|
|
|
/* if this file wasn't here last time, try adding it */
|
2010-02-21 13:29:23 +00:00
|
|
|
if( !is_file_in_list( w->lastFiles, name, len ) ) {
|
|
|
|
tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir );
|
|
|
|
w->callback( w->session, w->dir, name );
|
2009-06-05 17:02:29 +00:00
|
|
|
}
|
2009-03-01 15:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir( odir );
|
|
|
|
w->lastTimeChecked = time( NULL );
|
2009-06-05 17:02:29 +00:00
|
|
|
evbuffer_free( w->lastFiles );
|
|
|
|
w->lastFiles = curFiles;
|
2009-03-01 15:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
dtr_watchdir*
|
|
|
|
dtr_watchdir_new( tr_session * session, const char * dir, dtr_watchdir_callback * callback )
|
|
|
|
{
|
|
|
|
dtr_watchdir * w = tr_new0( dtr_watchdir, 1 );
|
|
|
|
|
|
|
|
w->session = session;
|
|
|
|
w->dir = tr_strdup( dir );
|
|
|
|
w->callback = callback;
|
|
|
|
|
|
|
|
watchdir_new_impl( w );
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dtr_watchdir_update( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
if( w != NULL )
|
|
|
|
watchdir_update_impl( w );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dtr_watchdir_free( dtr_watchdir * w )
|
|
|
|
{
|
|
|
|
if( w != NULL )
|
|
|
|
{
|
|
|
|
watchdir_free_impl( w );
|
|
|
|
tr_free( w->dir );
|
|
|
|
tr_free( w );
|
|
|
|
}
|
|
|
|
}
|