Changeset 5654


Ignore:
Timestamp:
Apr 20, 2008, 9:54:44 PM (14 years ago)
Author:
charles
Message:

clean up the file bsearch that caused the crash reported by John_Clay yesterday

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/inout.c

    r5652 r5654  
    8989}
    9090
     91static int
     92compareOffsetToFile( const void * a, const void * b )
     93{
     94    const uint64_t offset = *(const uint64_t*)a;
     95    const tr_file * file = b;
     96
     97    if( offset < file->offset ) return -1;
     98    if( offset > file->offset + file->length ) return 1;
     99    return 0;
     100}
     101
    91102static void
    92103findFileLocation( const tr_torrent * tor,
     
    96107                  uint64_t         * fileOffset )
    97108{
    98     size_t len;
    99     tr_file_index_t first, last;
    100     const tr_info * inf = &tor->info;
    101     uint64_t offset = tr_pieceOffset( tor, pieceIndex, pieceOffset, 0 );
    102 
    103     assert( pieceIndex < inf->pieceCount );
    104     assert( pieceOffset < tr_torPieceCountBytes( tor, pieceIndex ) );
    105     assert( offset < inf->totalSize );
     109    const uint64_t offset = tr_pieceOffset( tor, pieceIndex, pieceOffset, 0 );
     110    const tr_file * file;
     111
     112    file = bsearch( &offset,
     113                    tor->info.files, tor->info.fileCount, sizeof(tr_file),
     114                    compareOffsetToFile );
    106115   
    107     /* use a lower-bound bsearch to find the file that matches */
    108     first = 0;
    109     last = inf->fileCount;
    110     len = last - first;
    111     while( len > 0 ) {
    112         size_t half = len / 2;
    113         size_t middle = first + half;
    114         if( inf->files[middle].offset + inf->files[middle].length < offset )
    115         {
    116             first = middle;
    117             ++first;
    118             len = len - half - 1;
    119         }
    120         else len = half;
    121     }
    122    
    123     *fileIndex = first;
    124     *fileOffset = offset - inf->files[first].offset;
    125 
    126     assert( inf->files[first].offset <= offset );
    127     if( offset >= inf->files[first].offset + inf->files[first].length ) {
    128         fprintf( stderr, "piece index is %lu\n", (unsigned long)pieceIndex );
    129         fprintf( stderr, "piece offset is %lu\n", (unsigned long)pieceOffset );
    130         fprintf( stderr, "piece size is %lu\n", (unsigned long)inf->pieceSize );
    131         fprintf( stderr, "inf->totalSize is %"PRIu64"\n", inf->totalSize );
    132         fprintf( stderr, "offset is %"PRIu64"\n", offset );
    133         fprintf( stderr, "first is is %"PRIu64"\n", (uint64_t)first );
    134         fprintf( stderr, "inf->fileCount is %"PRIu64"\n", (uint64_t)inf->fileCount );
    135         fprintf( stderr, "inf->fileCount is %d\n", (int)inf->fileCount );
    136         fprintf( stderr, "inf->files[first].offset is %"PRIu64"\n", inf->files[first].offset );
    137         fprintf( stderr, "inf->files[first].length is %"PRIu64"\n", inf->files[first].length );
    138         assert( offset < inf->files[first].offset + inf->files[first].length );
    139     }
    140     assert( *fileIndex < inf->fileCount );
    141     assert( *fileOffset < inf->files[first].length );
     116    *fileIndex = file - tor->info.files;
     117    *fileOffset = offset - file->offset;
     118
     119    assert( *fileIndex < tor->info.fileCount );
     120    assert( *fileOffset < file->length );
     121    assert( tor->info.files[*fileIndex].offset + *fileOffset == offset );
    142122}
    143123
  • trunk/libtransmission/tracker.c

    r5650 r5654  
    10751075    t->manualAnnounceAllowedAt = ~(time_t)0;
    10761076    t->name = tr_strdup( info->name );
    1077     t->randOffset = tr_rand( 60 );
     1077    t->randOffset = tr_rand( 120 );
    10781078    memcpy( t->hash, info->hash, SHA_DIGEST_LENGTH );
    10791079    escape( t->escaped, info->hash, SHA_DIGEST_LENGTH );
Note: See TracChangeset for help on using the changeset viewer.