From a13e0755f01df37712c241a996de10a8e3eb10ac Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 12 Jan 2009 21:59:53 +0000 Subject: [PATCH] (trunk libT) close open files immediately after they're done downloading --- libtransmission/completion.c | 20 ++++++++++++++++++++ libtransmission/completion.h | 4 +++- libtransmission/peer-mgr.c | 25 ++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/libtransmission/completion.c b/libtransmission/completion.c index 220f4c129..60297dc6d 100644 --- a/libtransmission/completion.c +++ b/libtransmission/completion.c @@ -315,3 +315,23 @@ tr_cpPieceIsComplete( const tr_completion * cp, tr_piece_index_t piece ) { return cp->completeBlocks[piece] == tr_torPieceCountBlocks( cp->tor, piece ); } + +tr_bool +tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t fileIndex ) +{ + tr_block_index_t block; + + const tr_torrent * tor = cp->tor; + const tr_file * file = &tor->info.files[fileIndex]; + const tr_block_index_t firstBlock = file->offset / tor->blockSize; + const tr_block_index_t lastBlock = ( file->offset + file->length - 1 ) / tor->blockSize; + + assert( tr_torBlockPiece( tor, firstBlock ) == file->firstPiece ); + assert( tr_torBlockPiece( tor, lastBlock ) == file->lastPiece ); + + for( block=firstBlock; block<=lastBlock; ++block ) + if( !tr_cpBlockIsComplete( cp, block ) ) + return FALSE; + + return TRUE; +} diff --git a/libtransmission/completion.h b/libtransmission/completion.h index 70637acfa..c0ff82c5f 100644 --- a/libtransmission/completion.h +++ b/libtransmission/completion.h @@ -128,11 +128,13 @@ void tr_cpPieceAdd( tr_completion * completion, void tr_cpPieceRem( tr_completion * completion, tr_piece_index_t piece ); +tr_bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t ); + /** *** Blocks **/ -static TR_INLINE int tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block ) { +static TR_INLINE tr_bool tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block ) { return tr_bitfieldHas( &cp->blockBitfield, block ); } diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 21271d444..19b7968d3 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -26,6 +26,7 @@ #include "clients.h" #include "completion.h" #include "crypto.h" +#include "fdlimit.h" #include "handshake.h" #include "inout.h" /* tr_ioTestPiece */ #include "net.h" @@ -1062,7 +1063,7 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt ) case TR_PEER_CLIENT_GOT_BLOCK: { - tr_torrent * tor = t->tor; + tr_torrent * tor = t->tor; tr_block_index_t block = _tr_block( tor, e->pieceIndex, e->offset ); @@ -1087,14 +1088,32 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt ) tr_peerMgrSetBlame( tor->session->peerMgr, tor->info.hash, p, ok ); if( !ok ) + { gotBadPiece( t, p ); + } else { int i; - int peerCount = tr_ptrArraySize( &t->peers ); - tr_peer ** peers = (tr_peer**) tr_ptrArrayBase( &t->peers ); + int peerCount; + tr_peer ** peers; + tr_file_index_t fileIndex; + + peerCount = tr_ptrArraySize( &t->peers ); + peers = (tr_peer**) tr_ptrArrayBase( &t->peers ); for( i=0; imsgs, p ); + + for( fileIndex=0; fileIndexinfo.fileCount; ++fileIndex ) + { + const tr_file * file = &tor->info.files[fileIndex]; + if( ( file->firstPiece <= p ) && ( p <= file->lastPiece ) && tr_cpFileIsComplete( &tor->completion, fileIndex ) ) + { + char * path = tr_buildPath( tor->downloadDir, file->name, NULL ); + tordbg( t, "closing recently-completed file \"%s\"", path ); + tr_fdFileClose( path ); + tr_free( path ); + } + } } tr_torrentRecheckCompleteness( tor ); -- 2.40.0