diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index 021642687..70100a1a6 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -205,6 +205,42 @@ preallocateFileFull( const char * filename, uint64_t length ) return success; } +FILE* +tr_open_file_for_reading( const char * filename, tr_bool sequential ) +{ + int fd; + int flags; + + /* build the flags */ + flags = O_RDONLY; +#ifdef O_SEQUENTIAL + if( sequential ) flags |= O_SEQUENTIAL; +#endif +#ifdef O_RANDOM + if( !sequential ) flags |= O_RANDOM +#endif +#ifdef O_BINARY + flags |= O_BINARY; +#endif +#ifdef O_LARGEFILE + flags |= O_LARGEFILE; +#endif + + /* open the file */ + fd = open( filename, flags, 0666 ); + if( fd < 0 ) + return NULL; + +#if defined( SYS_DARWIN ) + fcntl( fd, F_NOCACHE, 1 ); + fcntl( fd, F_RDAHEAD, sequential ); +#elif defined( HAVE_POSIX_FADVISE ) + posix_fadvise( fd, 0, 0, sequential ? POSIX_FADV_SEQUENTIAL : POSIX_FADV_RANDOM ); +#endif + + return fdopen( fd, "r" ); +} + /** * returns 0 on success, or an errno value on failure. * errno values include ENOENT if the parent folder doesn't exist, diff --git a/libtransmission/fdlimit.h b/libtransmission/fdlimit.h index 8a72cfda4..09a1915d6 100644 --- a/libtransmission/fdlimit.h +++ b/libtransmission/fdlimit.h @@ -32,6 +32,8 @@ void tr_fdInit( size_t openFileLimit, size_t globalPeerLimit ); +FILE* tr_open_file_for_reading( const char * filename, tr_bool sequential ); + /** * Returns an fd to the specified filename. * diff --git a/libtransmission/verify.c b/libtransmission/verify.c index a72ca634c..f0beca003 100644 --- a/libtransmission/verify.c +++ b/libtransmission/verify.c @@ -22,6 +22,7 @@ #include "transmission.h" #include "completion.h" +#include "fdlimit.h" #include "resume.h" /* tr_torrentSaveResume() */ #include "inout.h" #include "list.h" @@ -74,11 +75,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) if( !filePos && !fp ) { char * filename = tr_buildPath( tor->downloadDir, file->name, NULL ); - fp = fopen( filename, "rb" ); -#ifdef HAVE_POSIX_FADVISE - if( fp != NULL ) - posix_fadvise( fileno( fp ), 0, 0, POSIX_FADV_SEQUENTIAL ); -#endif + fp = tr_open_file_for_reading( filename, TRUE ); /* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */ tr_free( filename ); }