From f8bc11e094ef3cde89054eb2c571fec72a573a72 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Dec 2007 05:42:33 +0000 Subject: [PATCH] fix a couple of related file priority bugs. --- libtransmission/peer-mgr.c | 16 ++++++++-------- libtransmission/torrent.c | 15 ++++++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 6a9a75fc3..6a8d31cc1 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -590,6 +590,14 @@ compareRefillPiece (const void * aIn, const void * bIn) const struct tr_refill_piece * a = aIn; const struct tr_refill_piece * b = bIn; + /* if one piece has a higher priority, it goes first */ + if (a->priority != b->priority) + return a->priority > b->priority ? -1 : 1; + + /* try to fill partial pieces */ + if( a->percentDone != b->percentDone ) + return a->percentDone > b->percentDone ? -1 : 1; + /* if one *might be* fastallowed to us, get it first... * I'm putting it on top so we prioritize those pieces at * startup, then we'll have them, and we'll be denied access @@ -597,17 +605,9 @@ compareRefillPiece (const void * aIn, const void * bIn) if (a->fastAllowed != b->fastAllowed) return a->fastAllowed < b->fastAllowed ? -1 : 1; - /* if one piece has a higher priority, it goes first */ - if (a->priority != b->priority) - return a->priority > b->priority ? -1 : 1; - /* otherwise if one was suggested to us, get it */ if (a->suggested != b->suggested) return a->suggested < b->suggested ? -1 : 1; - - /* try to fill partial pieces */ - if( a->percentDone != b->percentDone ) - return a->percentDone > b->percentDone ? -1 : 1; /* otherwise if one has fewer peers, it goes first */ if (a->peerCount != b->peerCount) diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index fade63be0..97838e479 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -218,15 +218,20 @@ calculatePiecePriority ( const tr_torrent * tor, int piece ) { int i; - tr_priority_t priority = TR_PRI_NORMAL; + tr_priority_t priority = TR_PRI_LOW; + /* the piece's priority is the max of the priorities + * of all the files in that piece */ for( i=0; iinfo.fileCount; ++i ) { const tr_file * file = &tor->info.files[i]; - if ( file->firstPiece <= piece - && file->lastPiece >= piece - && file->priority > priority) - priority = file->priority; + + if( piece < file->firstPiece ) + continue; + if( piece > file->lastPiece ) + break; + + priority = MAX( priority, file->priority ); /* when dealing with multimedia files, getting the first and last pieces can sometimes allow you to preview it a bit