2008-02-15 16:00:46 +00:00
|
|
|
/*
|
2009-12-05 02:19:24 +00:00
|
|
|
* This file Copyright (C) 2007-2009 Mnemosyne LLC
|
2008-02-15 16:00:46 +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)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-02-15 16:00:46 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-02-26 21:08:51 +00:00
|
|
|
* $Id$
|
2008-02-15 16:00:46 +00:00
|
|
|
*/
|
|
|
|
|
2008-10-14 20:31:16 +00:00
|
|
|
#include <unistd.h> /* S_ISREG */
|
2008-02-15 16:00:46 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2009-04-06 23:51:55 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
#define _XOPEN_SOURCE 600
|
2009-12-16 07:47:04 +00:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_POSIX_FADVISE) || defined(SYS_DARWIN)
|
|
|
|
#include <fcntl.h> /* posix_fadvise() / fcntl() */
|
2009-04-06 23:51:55 +00:00
|
|
|
#endif
|
|
|
|
|
2009-04-06 04:02:51 +00:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "completion.h"
|
2009-04-07 20:25:32 +00:00
|
|
|
#include "fdlimit.h"
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "inout.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "torrent.h"
|
2008-02-16 05:13:56 +00:00
|
|
|
#include "utils.h" /* tr_buildPath */
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "verify.h"
|
|
|
|
|
2009-04-06 04:02:51 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-09-06 14:05:06 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 200
|
|
|
|
};
|
|
|
|
|
2009-04-06 04:02:51 +00:00
|
|
|
/* #define STOPWATCH */
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
|
|
|
{
|
|
|
|
SHA_CTX sha;
|
2009-04-26 20:44:18 +00:00
|
|
|
int fd = -1;
|
2009-04-26 16:14:47 +00:00
|
|
|
int64_t filePos = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
tr_bool changed = 0;
|
|
|
|
tr_bool hadPiece = 0;
|
2009-09-06 14:05:06 +00:00
|
|
|
time_t lastSleptAt = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
uint32_t piecePos = 0;
|
2009-08-14 20:55:22 +00:00
|
|
|
uint32_t pieceBytesRead = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
tr_file_index_t fileIndex = 0;
|
|
|
|
tr_piece_index_t pieceIndex = 0;
|
|
|
|
const int64_t buflen = tor->info.pieceSize;
|
|
|
|
uint8_t * buffer = tr_new( uint8_t, buflen );
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t begin = tr_time( );
|
2009-09-09 12:44:11 +00:00
|
|
|
time_t end;
|
2009-04-06 04:02:51 +00:00
|
|
|
|
|
|
|
SHA1_Init( &sha );
|
|
|
|
|
|
|
|
while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) )
|
|
|
|
{
|
|
|
|
int64_t leftInPiece;
|
|
|
|
int64_t leftInFile;
|
|
|
|
int64_t bytesThisPass;
|
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
|
|
|
|
|
|
|
/* if we're starting a new piece... */
|
|
|
|
if( piecePos == 0 )
|
|
|
|
{
|
|
|
|
hadPiece = tr_cpPieceIsComplete( &tor->completion, pieceIndex );
|
|
|
|
/* fprintf( stderr, "starting piece %d of %d\n", (int)pieceIndex, (int)tor->info.pieceCount ); */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're starting a new file... */
|
2009-04-26 20:44:18 +00:00
|
|
|
if( !filePos && (fd<0) )
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2009-10-19 05:05:00 +00:00
|
|
|
char * filename = tr_torrentFindFile( tor, fileIndex );
|
|
|
|
fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
|
2009-04-26 20:44:18 +00:00
|
|
|
/* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
|
2009-04-06 04:02:51 +00:00
|
|
|
tr_free( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* figure out how much we can read this pass */
|
|
|
|
leftInPiece = tr_torPieceCountBytes( tor, pieceIndex ) - piecePos;
|
|
|
|
leftInFile = file->length - filePos;
|
|
|
|
bytesThisPass = MIN( leftInFile, leftInPiece );
|
|
|
|
bytesThisPass = MIN( bytesThisPass, buflen );
|
|
|
|
/* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
|
|
|
|
|
|
|
|
/* read a bit */
|
2009-04-26 20:44:18 +00:00
|
|
|
if( (fd>=0) && tr_lseek( fd, filePos, SEEK_SET ) != -1 ) {
|
|
|
|
const int64_t numRead = read( fd, buffer, bytesThisPass );
|
2009-08-14 20:55:22 +00:00
|
|
|
if( numRead > 0 )
|
|
|
|
pieceBytesRead += numRead;
|
2009-04-06 04:02:51 +00:00
|
|
|
if( numRead == bytesThisPass )
|
|
|
|
SHA1_Update( &sha, buffer, numRead );
|
2009-12-25 15:48:41 +00:00
|
|
|
#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
|
2009-12-16 06:34:17 +00:00
|
|
|
posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
|
|
|
|
#endif
|
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
fcntl( fd, F_NOCACHE, 1 );
|
|
|
|
#endif
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* move our offsets */
|
|
|
|
leftInPiece -= bytesThisPass;
|
|
|
|
leftInFile -= bytesThisPass;
|
|
|
|
piecePos += bytesThisPass;
|
|
|
|
filePos += bytesThisPass;
|
|
|
|
|
|
|
|
/* if we're finishing a piece... */
|
|
|
|
if( leftInPiece == 0 )
|
|
|
|
{
|
2009-09-06 14:05:06 +00:00
|
|
|
time_t now;
|
2009-04-06 04:02:51 +00:00
|
|
|
tr_bool hasPiece;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
SHA1_Final( hash, &sha );
|
|
|
|
hasPiece = !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
|
|
|
|
/* fprintf( stderr, "do the hashes match? %s\n", (hasPiece?"yes":"no") ); */
|
|
|
|
|
|
|
|
if( hasPiece ) {
|
|
|
|
tr_torrentSetHasPiece( tor, pieceIndex, TRUE );
|
|
|
|
if( !hadPiece )
|
|
|
|
changed = TRUE;
|
|
|
|
} else if( hadPiece ) {
|
|
|
|
tr_torrentSetHasPiece( tor, pieceIndex, FALSE );
|
|
|
|
changed = TRUE;
|
|
|
|
}
|
|
|
|
tr_torrentSetPieceChecked( tor, pieceIndex, TRUE );
|
2009-11-26 18:47:08 +00:00
|
|
|
now = tr_time( );
|
2009-09-06 14:05:06 +00:00
|
|
|
tor->anyDate = now;
|
|
|
|
|
|
|
|
/* sleeping even just a few msec per second goes a long
|
|
|
|
* way towards reducing IO load... */
|
|
|
|
if( lastSleptAt != now ) {
|
|
|
|
lastSleptAt = now;
|
|
|
|
tr_wait( MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY );
|
|
|
|
}
|
2009-08-14 20:55:22 +00:00
|
|
|
|
2009-04-06 04:02:51 +00:00
|
|
|
SHA1_Init( &sha );
|
|
|
|
++pieceIndex;
|
|
|
|
piecePos = 0;
|
2009-08-14 20:55:22 +00:00
|
|
|
pieceBytesRead = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're finishing a file... */
|
|
|
|
if( leftInFile == 0 )
|
|
|
|
{
|
|
|
|
/* fprintf( stderr, "closing file\n" ); */
|
2009-04-26 20:44:18 +00:00
|
|
|
if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
|
2009-04-06 04:02:51 +00:00
|
|
|
++fileIndex;
|
|
|
|
filePos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2009-04-26 20:44:18 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
tr_close_file( fd );
|
2009-04-06 04:02:51 +00:00
|
|
|
tr_free( buffer );
|
|
|
|
|
2009-09-09 12:44:11 +00:00
|
|
|
/* stopwatch */
|
2009-11-26 18:47:08 +00:00
|
|
|
end = tr_time( );
|
2009-09-09 12:44:11 +00:00
|
|
|
tr_tordbg( tor, "it took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
|
|
|
|
(int)(end-begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize/(1+(end-begin))) );
|
2009-04-06 04:02:51 +00:00
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-02-15 16:00:46 +00:00
|
|
|
|
|
|
|
struct verify_node
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * torrent;
|
|
|
|
tr_verify_done_cb verify_done_cb;
|
2008-02-15 16:00:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2009-01-30 00:41:08 +00:00
|
|
|
fireCheckDone( tr_torrent * tor, tr_verify_done_cb verify_done_cb )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( verify_done_cb )
|
2009-01-30 00:41:08 +00:00
|
|
|
verify_done_cb( tor );
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct verify_node currentNode;
|
2009-01-30 00:41:08 +00:00
|
|
|
static tr_list * verifyList = NULL;
|
|
|
|
static tr_thread * verifyThread = NULL;
|
2009-04-06 04:02:51 +00:00
|
|
|
static tr_bool stopCurrent = FALSE;
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_lock*
|
|
|
|
getVerifyLock( void )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
|
|
|
static tr_lock * lock = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
if( lock == NULL )
|
|
|
|
lock = tr_lockNew( );
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
verifyThreadFunc( void * unused UNUSED )
|
|
|
|
{
|
2008-12-31 18:08:13 +00:00
|
|
|
for( ;; )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int changed = 0;
|
2008-12-31 18:08:13 +00:00
|
|
|
tr_torrent * tor;
|
2008-02-15 16:00:46 +00:00
|
|
|
struct verify_node * node;
|
|
|
|
|
|
|
|
tr_lockLock( getVerifyLock( ) );
|
|
|
|
stopCurrent = FALSE;
|
|
|
|
node = (struct verify_node*) verifyList ? verifyList->data : NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( node == NULL )
|
|
|
|
{
|
2008-02-15 16:00:46 +00:00
|
|
|
currentNode.torrent = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentNode = *node;
|
|
|
|
tor = currentNode.torrent;
|
|
|
|
tr_list_remove_data( &verifyList, node );
|
|
|
|
tr_free( node );
|
|
|
|
tr_lockUnlock( getVerifyLock( ) );
|
|
|
|
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_torinf( tor, "%s", _( "Verifying torrent" ) );
|
2009-03-26 18:06:54 +00:00
|
|
|
tr_torrentSetVerifyState( tor, TR_VERIFY_NOW );
|
2009-04-06 04:02:51 +00:00
|
|
|
changed = verifyTorrent( tor, &stopCurrent );
|
2009-03-26 18:06:54 +00:00
|
|
|
tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2008-02-15 16:00:46 +00:00
|
|
|
|
|
|
|
if( !stopCurrent )
|
|
|
|
{
|
2008-05-06 01:43:24 +00:00
|
|
|
if( changed )
|
2009-08-07 05:29:37 +00:00
|
|
|
tr_torrentSetDirty( tor );
|
2008-02-15 16:00:46 +00:00
|
|
|
fireCheckDone( tor, currentNode.verify_done_cb );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyThread = NULL;
|
|
|
|
tr_lockUnlock( getVerifyLock( ) );
|
|
|
|
}
|
|
|
|
|
2009-09-09 12:44:11 +00:00
|
|
|
static tr_bool
|
|
|
|
torrentHasAnyLocalData( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_file_index_t i;
|
|
|
|
tr_bool hasAny = FALSE;
|
|
|
|
const tr_file_index_t n = tor->info.fileCount;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
for( i=0; i<n && !hasAny; ++i )
|
|
|
|
{
|
|
|
|
struct stat sb;
|
2009-10-19 05:05:00 +00:00
|
|
|
char * path = tr_torrentFindFile( tor, i );
|
|
|
|
if( ( path != NULL ) && !stat( path, &sb ) && ( sb.st_size > 0 ) )
|
2009-09-09 12:44:11 +00:00
|
|
|
hasAny = TRUE;
|
|
|
|
tr_free( path );
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasAny;
|
|
|
|
}
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_verifyAdd( tr_torrent * tor,
|
|
|
|
tr_verify_done_cb verify_done_cb )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2009-09-09 12:44:11 +00:00
|
|
|
if( tr_torrentCountUncheckedPieces( tor ) == 0 )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
|
|
|
/* doesn't need to be checked... */
|
2008-03-19 14:56:47 +00:00
|
|
|
fireCheckDone( tor, verify_done_cb );
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
2009-09-09 12:44:11 +00:00
|
|
|
else if( !torrentHasAnyLocalData( tor ) )
|
|
|
|
{
|
|
|
|
/* we haven't downloaded anything for this torrent yet...
|
|
|
|
* no need to leave it waiting in the back of the queue.
|
|
|
|
* we can mark it as all-missing from here and fire
|
|
|
|
* the "done" callback */
|
|
|
|
const tr_bool hadAny = tr_cpHaveTotal( &tor->completion ) != 0;
|
|
|
|
tr_piece_index_t i;
|
2009-10-11 03:10:17 +00:00
|
|
|
for( i=0; i<tor->info.pieceCount; ++i ) {
|
2009-09-09 12:44:11 +00:00
|
|
|
tr_torrentSetHasPiece( tor, i, FALSE );
|
2009-10-11 03:10:17 +00:00
|
|
|
tr_torrentSetPieceChecked( tor, i, TRUE );
|
|
|
|
}
|
2009-09-09 12:44:11 +00:00
|
|
|
if( hadAny ) /* if we thought we had some, flag as dirty */
|
|
|
|
tr_torrentSetDirty( tor );
|
|
|
|
fireCheckDone( tor, verify_done_cb );
|
|
|
|
}
|
2008-02-15 16:00:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
struct verify_node * node;
|
|
|
|
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_torinf( tor, "%s", _( "Queued for verification" ) );
|
2008-02-15 16:00:46 +00:00
|
|
|
|
|
|
|
node = tr_new( struct verify_node, 1 );
|
|
|
|
node->torrent = tor;
|
|
|
|
node->verify_done_cb = verify_done_cb;
|
|
|
|
|
|
|
|
tr_lockLock( getVerifyLock( ) );
|
2009-05-13 18:37:56 +00:00
|
|
|
tr_torrentSetVerifyState( tor, TR_VERIFY_WAIT );
|
2008-02-15 16:00:46 +00:00
|
|
|
tr_list_append( &verifyList, node );
|
|
|
|
if( verifyThread == NULL )
|
2008-08-12 14:03:03 +00:00
|
|
|
verifyThread = tr_threadNew( verifyThreadFunc, NULL );
|
2008-02-15 16:00:46 +00:00
|
|
|
tr_lockUnlock( getVerifyLock( ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareVerifyByTorrent( const void * va,
|
|
|
|
const void * vb )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
|
|
|
const struct verify_node * a = va;
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_torrent * b = vb;
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
return a->torrent - b;
|
|
|
|
}
|
|
|
|
|
2009-04-16 13:10:25 +00:00
|
|
|
tr_bool
|
2008-10-28 15:13:07 +00:00
|
|
|
tr_verifyInProgress( const tr_torrent * tor )
|
|
|
|
{
|
2009-04-16 13:10:25 +00:00
|
|
|
tr_bool found = FALSE;
|
2008-10-28 15:13:07 +00:00
|
|
|
tr_lock * lock = getVerifyLock( );
|
|
|
|
tr_lockLock( lock );
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-10-28 15:13:07 +00:00
|
|
|
found = ( tor == currentNode.torrent )
|
|
|
|
|| ( tr_list_find( verifyList, tor, compareVerifyByTorrent ) != NULL );
|
|
|
|
|
|
|
|
tr_lockUnlock( lock );
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
void
|
|
|
|
tr_verifyRemove( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_lock * lock = getVerifyLock( );
|
|
|
|
tr_lockLock( lock );
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
if( tor == currentNode.torrent )
|
|
|
|
{
|
|
|
|
stopCurrent = TRUE;
|
|
|
|
while( stopCurrent )
|
|
|
|
{
|
|
|
|
tr_lockUnlock( lock );
|
|
|
|
tr_wait( 100 );
|
|
|
|
tr_lockLock( lock );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_free( tr_list_remove( &verifyList, tor, compareVerifyByTorrent ) );
|
2009-08-03 16:24:44 +00:00
|
|
|
tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_lockUnlock( lock );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-16 13:10:25 +00:00
|
|
|
void
|
|
|
|
tr_verifyClose( tr_session * session UNUSED )
|
|
|
|
{
|
|
|
|
tr_lockLock( getVerifyLock( ) );
|
|
|
|
|
|
|
|
stopCurrent = TRUE;
|
|
|
|
tr_list_free( &verifyList, tr_free );
|
|
|
|
|
|
|
|
tr_lockUnlock( getVerifyLock( ) );
|
|
|
|
}
|