Changeset 5045


Ignore:
Timestamp:
Feb 15, 2008, 6:25:42 PM (13 years ago)
Author:
charles
Message:

make the code a little easier to read. make tr_ioFileTest()'s return value consistent with other inout funcs.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/inout.c

    r5042 r5045  
    222222****/
    223223
    224 static int
     224static tr_errno
    225225tr_ioRecalculateHash( const tr_torrent  * tor,
    226226                      int                 pieceIndex,
     
    245245    {
    246246        const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) );
    247         int err = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
     247        tr_errno err = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
    248248        if( err )
    249249            return err;
     
    257257}
    258258
    259 int
     259tr_errno
    260260tr_ioTestPiece( const tr_torrent * tor, int pieceIndex )
    261261{
     262    int err;
    262263    uint8_t hash[SHA_DIGEST_LENGTH];
    263     const int ret = tr_ioRecalculateHash( tor, pieceIndex, hash )
    264         || memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
     264
     265    err  = tr_ioRecalculateHash( tor, pieceIndex, hash );
     266
     267    if( !err && memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH ) )
     268        err = TR_ERROR;
     269
    265270    tr_dbg ("torrent [%s] piece %d hash check: %s",
    266             tor->info.name, pieceIndex, ( ret ? "FAILED" : "OK" ));
    267     return ret;
    268 }
    269 
    270 int
    271 tr_ioHash( tr_torrent * tor, int pieceIndex )
    272 {
    273     int ret;
    274     const int success = !tr_ioTestPiece( tor, pieceIndex );
    275 
    276     if( success )
    277     {
    278         tr_dbg( "Piece %d hash OK", pieceIndex );
    279         tr_cpPieceAdd( tor->completion, pieceIndex );
    280         ret = TR_OK;
    281     }
    282     else
    283     {
    284         tr_err( "Piece %d hash FAILED", pieceIndex );
    285         tr_cpPieceRem( tor->completion, pieceIndex );
    286         ret = TR_ERROR;
    287     }
    288 
    289     tr_peerMgrSetBlame( tor->handle->peerMgr, tor->info.hash,
    290                         pieceIndex, success );
    291 
    292     return ret;
    293 }
     271            tor->info.name, pieceIndex, ( err ? "FAILED" : "OK" ));
     272
     273    return err;
     274}
  • trunk/libtransmission/inout.h

    r5042 r5045  
    5151
    5252/**
    53  * returns true if the piece matches its metainfo's SHA1 checksum,
    54  * false otherwise.
     53 * returns 0 if the piece matches its metainfo's SHA1 checksum,
     54 * or TR_ERROR_IO_* if there was a problem reading the piece,
     55 * or TR_ERROR if the checksum didn't match.
    5556 */
    56 int tr_ioTestPiece( const tr_torrent*, int piece );
    57 
    58 
    59 /**
    60  * tests the specified piece and uses the results to
    61  * update the torrent's "completion" and "blame" fields.
    62  */
    63 int tr_ioHash ( tr_torrent*, int piece );
     57tr_errno tr_ioTestPiece( const tr_torrent*, int piece );
    6458
    6559
  • trunk/libtransmission/peer-msgs.c

    r5037 r5045  
    14561456    if( tr_cpPieceIsComplete( tor->completion, req->index ) )
    14571457    {
    1458         if( tr_ioHash( tor, req->index ) )
    1459         {
     1458        const tr_errno err = tr_ioTestPiece( tor, req->index );
     1459
     1460        tr_torrentSetHasPiece( tor, req->index, !err );
     1461        tr_torrentSetPieceChecked( tor, req->index, TRUE );
     1462        tr_peerMgrSetBlame( tor->handle->peerMgr, tor->info.hash, req->index, !err );
     1463
     1464        if( !err )
     1465            fireClientHave( msgs, req->index );
     1466        else
    14601467            gotBadPiece( msgs, req->index );
    1461             return 0;
    1462         }
    1463 
    1464         tr_torrentSetPieceChecked( tor, req->index, TRUE );
    1465         fireClientHave( msgs, req->index );
    14661468    }
    14671469
  • trunk/libtransmission/verify.c

    r5042 r5045  
    9191        else if( !tr_torrentIsPieceChecked( tor, i ) )
    9292        {
    93             const int check = tr_ioTestPiece( tor, i );
    94             tr_torrentSetHasPiece( tor, i, !check );
     93            const tr_errno err = tr_ioTestPiece( tor, i );
     94
     95            if( !err ) /* yay */
     96            {
     97                tr_torrentSetHasPiece( tor, i, TRUE );
     98            }
     99            else
     100            {
     101                /* if we were wrong about it being complete,
     102                 * reset and start again.  if we were right about
     103                 * it being incomplete, do nothing -- we don't
     104                 * want to lose blocks in those incomplete pieces */
     105
     106                if( tr_cpPieceIsComplete( tor->completion, i ) )
     107                    tr_torrentSetHasPiece( tor, i, FALSE );
     108            }
     109               
    95110            tr_torrentSetPieceChecked( tor, i, TRUE );
    96111        }
Note: See TracChangeset for help on using the changeset viewer.