2008-02-15 16:00:46 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "completion.h"
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "resume.h" /* tr_torrentSaveResume() */
|
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"
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
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
|
2008-09-23 19:11:04 +00:00
|
|
|
fireCheckDone( tr_torrent * torrent,
|
|
|
|
tr_verify_done_cb verify_done_cb )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
if( verify_done_cb )
|
|
|
|
verify_done_cb( torrent );
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct verify_node currentNode;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_list * verifyList = NULL;
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_thread * verifyThread = NULL;
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static int 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;
|
|
|
|
}
|
|
|
|
|
2008-05-06 01:43:24 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
checkFile( tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex,
|
|
|
|
int * abortFlag )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
int changed = FALSE;
|
|
|
|
int nofile;
|
|
|
|
struct stat sb;
|
|
|
|
char path[MAX_PATH_LENGTH];
|
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_buildPath ( path, sizeof( path ), tor->downloadDir, file->name, NULL );
|
2008-02-15 16:00:46 +00:00
|
|
|
nofile = stat( path, &sb ) || !S_ISREG( sb.st_mode );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = file->firstPiece;
|
|
|
|
i <= file->lastPiece && i < tor->info.pieceCount && ( !*abortFlag );
|
|
|
|
++i )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
|
|
|
if( nofile )
|
|
|
|
{
|
|
|
|
tr_torrentSetHasPiece( tor, i, 0 );
|
|
|
|
}
|
|
|
|
else if( !tr_torrentIsPieceChecked( tor, i ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int wasComplete = tr_cpPieceIsComplete(
|
|
|
|
tor->completion, i );
|
2008-02-15 18:25:42 +00:00
|
|
|
|
2008-10-02 20:30:29 +00:00
|
|
|
if( tr_ioTestPiece( tor, i ) ) /* yay */
|
2008-02-15 18:25:42 +00:00
|
|
|
{
|
|
|
|
tr_torrentSetHasPiece( tor, i, TRUE );
|
2008-05-06 01:43:24 +00:00
|
|
|
if( !wasComplete )
|
|
|
|
changed = TRUE;
|
2008-02-15 18:25:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if we were wrong about it being complete,
|
|
|
|
* reset and start again. if we were right about
|
|
|
|
* it being incomplete, do nothing -- we don't
|
|
|
|
* want to lose blocks in those incomplete pieces */
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( wasComplete )
|
|
|
|
{
|
2008-02-15 18:25:42 +00:00
|
|
|
tr_torrentSetHasPiece( tor, i, FALSE );
|
2008-05-06 01:43:24 +00:00
|
|
|
changed = TRUE;
|
|
|
|
}
|
2008-02-15 18:25:42 +00:00
|
|
|
}
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
2008-02-16 05:13:56 +00:00
|
|
|
|
|
|
|
tr_torrentSetPieceChecked( tor, i, TRUE );
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
2008-05-06 01:43:24 +00:00
|
|
|
|
|
|
|
return changed;
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
verifyThreadFunc( void * unused UNUSED )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ; ; )
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int changed = 0;
|
|
|
|
tr_file_index_t i;
|
|
|
|
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( ) );
|
|
|
|
|
|
|
|
tor->verifyState = TR_VERIFY_NOW;
|
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
tr_torinf( tor, _( "Verifying torrent" ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < tor->info.fileCount && !stopCurrent; ++i )
|
2008-05-06 01:43:24 +00:00
|
|
|
changed |= checkFile( tor, i, &stopCurrent );
|
2008-02-15 16:00:46 +00:00
|
|
|
|
|
|
|
tor->verifyState = TR_VERIFY_NONE;
|
|
|
|
|
|
|
|
if( !stopCurrent )
|
|
|
|
{
|
2008-05-06 01:43:24 +00:00
|
|
|
if( changed )
|
|
|
|
tr_torrentSaveResume( tor );
|
2008-02-15 16:00:46 +00:00
|
|
|
fireCheckDone( tor, currentNode.verify_done_cb );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyThread = NULL;
|
|
|
|
tr_lockUnlock( getVerifyLock( ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
const int uncheckedCount = tr_torrentCountUncheckedPieces( tor );
|
|
|
|
|
|
|
|
if( !uncheckedCount )
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct verify_node * node;
|
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
tr_torinf( tor, _( "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( ) );
|
|
|
|
tor->verifyState = verifyList ? TR_VERIFY_WAIT : TR_VERIFY_NOW;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_verifyRemove( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_lock * lock = getVerifyLock( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
tr_lockLock( lock );
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
tor->verifyState = TR_VERIFY_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_lockUnlock( lock );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|