Ignore:
Timestamp:
Dec 5, 2012, 5:29:46 PM (9 years ago)
Author:
jordan
Message:

Follow more common whitespace style conventions in the C code (libtransmission, daemon, utils, cli, gtk).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/verify.c

    r12204 r13625  
    33 *
    44 * This file is licensed by the GPL version 2. Works owned by the
    5  * Transmission project are granted a special exemption to clause 2(b)
     5 * Transmission project are granted a special exemption to clause 2 (b)
    66 * so that the bulk of its code can remain under the MIT license.
    77 * This exemption does not extend to derived works not owned by
     
    1111 */
    1212
    13 #include <string.h> /* memcmp() */
    14 #include <stdlib.h> /* free() */
     13#include <string.h> /* memcmp () */
     14#include <stdlib.h> /* free () */
    1515
    1616#ifdef HAVE_POSIX_FADVISE
    1717 #define _XOPEN_SOURCE 600
    18  #include <fcntl.h> /* posix_fadvise() */
     18 #include <fcntl.h> /* posix_fadvise () */
    1919#endif
    2020
     
    2525#include "fdlimit.h"
    2626#include "list.h"
    27 #include "platform.h" /* tr_lock() */
     27#include "platform.h" /* tr_lock () */
    2828#include "torrent.h"
    29 #include "utils.h" /* tr_valloc(), tr_free() */
     29#include "utils.h" /* tr_valloc (), tr_free () */
    3030#include "verify.h"
    3131
     
    3636enum
    3737{
    38     MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 100
     38  MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 100
    3939};
    4040
    4141static bool
    42 verifyTorrent( tr_torrent * tor, bool * stopFlag )
    43 {
    44     time_t end;
    45     SHA_CTX sha;
    46     int fd = -1;
    47     int64_t filePos = 0;
    48     bool changed = 0;
    49     bool hadPiece = 0;
    50     time_t lastSleptAt = 0;
    51     uint32_t piecePos = 0;
    52     tr_file_index_t fileIndex = 0;
    53     tr_file_index_t prevFileIndex = !fileIndex;
    54     tr_piece_index_t pieceIndex = 0;
    55     const time_t begin = tr_time( );
    56     const size_t buflen = 1024 * 128; /* 128 KiB buffer */
    57     uint8_t * buffer = tr_valloc( buflen );
    58 
    59     SHA1_Init( &sha );
    60 
    61     tr_tordbg( tor, "%s", "verifying torrent..." );
    62     tr_torrentSetChecked( tor, 0 );
    63     while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) )
     42verifyTorrent (tr_torrent * tor, bool * stopFlag)
     43{
     44  time_t end;
     45  SHA_CTX sha;
     46  int fd = -1;
     47  int64_t filePos = 0;
     48  bool changed = 0;
     49  bool hadPiece = 0;
     50  time_t lastSleptAt = 0;
     51  uint32_t piecePos = 0;
     52  tr_file_index_t fileIndex = 0;
     53  tr_file_index_t prevFileIndex = !fileIndex;
     54  tr_piece_index_t pieceIndex = 0;
     55  const time_t begin = tr_time ();
     56  const size_t buflen = 1024 * 128; /* 128 KiB buffer */
     57  uint8_t * buffer = tr_valloc (buflen);
     58
     59  SHA1_Init (&sha);
     60
     61  tr_tordbg (tor, "%s", "verifying torrent...");
     62  tr_torrentSetChecked (tor, 0);
     63  while (!*stopFlag && (pieceIndex < tor->info.pieceCount))
    6464    {
    65         uint32_t leftInPiece;
    66         uint32_t bytesThisPass;
    67         uint64_t leftInFile;
    68         const tr_file * file = &tor->info.files[fileIndex];
    69 
    70         /* if we're starting a new piece... */
    71         if( piecePos == 0 )
    72             hadPiece = tr_cpPieceIsComplete( &tor->completion, pieceIndex );
    73 
    74         /* if we're starting a new file... */
    75         if( !filePos && (fd<0) && (fileIndex!=prevFileIndex) )
    76         {
    77             char * filename = tr_torrentFindFile( tor, fileIndex );
    78             fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
    79             tr_free( filename );
    80             prevFileIndex = fileIndex;
    81         }
    82 
    83         /* figure out how much we can read this pass */
    84         leftInPiece = tr_torPieceCountBytes( tor, pieceIndex ) - piecePos;
    85         leftInFile = file->length - filePos;
    86         bytesThisPass = MIN( leftInFile, leftInPiece );
    87         bytesThisPass = MIN( bytesThisPass, buflen );
    88 
    89         /* read a bit */
    90         if( fd >= 0 ) {
    91             const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
    92             if( numRead > 0 ) {
    93                 bytesThisPass = (uint32_t)numRead;
    94                 SHA1_Update( &sha, buffer, bytesThisPass );
     65      uint32_t leftInPiece;
     66      uint32_t bytesThisPass;
     67      uint64_t leftInFile;
     68      const tr_file * file = &tor->info.files[fileIndex];
     69
     70      /* if we're starting a new piece... */
     71      if (piecePos == 0)
     72        hadPiece = tr_cpPieceIsComplete (&tor->completion, pieceIndex);
     73
     74      /* if we're starting a new file... */
     75      if (!filePos && (fd<0) && (fileIndex!=prevFileIndex))
     76        {
     77          char * filename = tr_torrentFindFile (tor, fileIndex);
     78          fd = filename == NULL ? -1 : tr_open_file_for_scanning (filename);
     79          tr_free (filename);
     80          prevFileIndex = fileIndex;
     81        }
     82
     83      /* figure out how much we can read this pass */
     84      leftInPiece = tr_torPieceCountBytes (tor, pieceIndex) - piecePos;
     85      leftInFile = file->length - filePos;
     86      bytesThisPass = MIN (leftInFile, leftInPiece);
     87      bytesThisPass = MIN (bytesThisPass, buflen);
     88
     89      /* read a bit */
     90      if (fd >= 0)
     91        {
     92          const ssize_t numRead = tr_pread (fd, buffer, bytesThisPass, filePos);
     93          if (numRead > 0)
     94            {
     95              bytesThisPass = (uint32_t)numRead;
     96              SHA1_Update (&sha, buffer, bytesThisPass);
    9597#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
    96                 posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
     98              posix_fadvise (fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED);
    9799#endif
    98100            }
    99101        }
    100102
    101         /* move our offsets */
    102         leftInPiece -= bytesThisPass;
    103         leftInFile -= bytesThisPass;
    104         piecePos += bytesThisPass;
    105         filePos += bytesThisPass;
    106 
    107         /* if we're finishing a piece... */
    108         if( leftInPiece == 0 )
    109         {
    110             time_t now;
    111             bool hasPiece;
    112             uint8_t hash[SHA_DIGEST_LENGTH];
    113 
    114             SHA1_Final( hash, &sha );
    115             hasPiece = !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
    116 
    117             if( hasPiece || hadPiece ) {
    118                 tr_torrentSetHasPiece( tor, pieceIndex, hasPiece );
    119                 changed |= hasPiece != hadPiece;
     103      /* move our offsets */
     104      leftInPiece -= bytesThisPass;
     105      leftInFile -= bytesThisPass;
     106      piecePos += bytesThisPass;
     107      filePos += bytesThisPass;
     108
     109      /* if we're finishing a piece... */
     110      if (leftInPiece == 0)
     111        {
     112          time_t now;
     113          bool hasPiece;
     114          uint8_t hash[SHA_DIGEST_LENGTH];
     115
     116          SHA1_Final (hash, &sha);
     117          hasPiece = !memcmp (hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH);
     118
     119          if (hasPiece || hadPiece)
     120            {
     121              tr_torrentSetHasPiece (tor, pieceIndex, hasPiece);
     122              changed |= hasPiece != hadPiece;
    120123            }
    121             tr_torrentSetPieceChecked( tor, pieceIndex );
    122             now = tr_time( );
    123             tor->anyDate = now;
    124 
    125             /* sleeping even just a few msec per second goes a long
    126              * way towards reducing IO load... */
    127             if( lastSleptAt != now ) {
    128                 lastSleptAt = now;
    129                 tr_wait_msec( MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY );
     124
     125          tr_torrentSetPieceChecked (tor, pieceIndex);
     126          now = tr_time ();
     127          tor->anyDate = now;
     128
     129          /* sleeping even just a few msec per second goes a long
     130           * way towards reducing IO load... */
     131          if (lastSleptAt != now)
     132            {
     133              lastSleptAt = now;
     134              tr_wait_msec (MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY);
    130135            }
    131136
    132             SHA1_Init( &sha );
    133             ++pieceIndex;
    134             piecePos = 0;
    135         }
    136 
    137         /* if we're finishing a file... */
    138         if( leftInFile == 0 )
    139         {
    140             if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
    141             ++fileIndex;
    142             filePos = 0;
     137          SHA1_Init (&sha);
     138          pieceIndex++;
     139          piecePos = 0;
     140        }
     141
     142      /* if we're finishing a file... */
     143      if (leftInFile == 0)
     144        {
     145          if (fd >= 0)
     146            {
     147              tr_close_file (fd);
     148              fd = -1;
     149            }
     150          fileIndex++;
     151          filePos = 0;
    143152        }
    144153    }
    145154
    146     /* cleanup */
    147     if( fd >= 0 )
    148         tr_close_file( fd );
    149     free( buffer );
    150 
    151     /* stopwatch */
    152     end = tr_time( );
    153     tr_tordbg( tor, "Verification is done. It took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
    154                (int)(end-begin), tor->info.totalSize,
    155                (uint64_t)(tor->info.totalSize/(1+(end-begin))) );
    156 
    157     return changed;
     155  /* cleanup */
     156  if (fd >= 0)
     157    tr_close_file (fd);
     158  free (buffer);
     159
     160  /* stopwatch */
     161  end = tr_time ();
     162  tr_tordbg (tor, "Verification is done. It took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
     163             (int)(end-begin), tor->info.totalSize,
     164             (uint64_t)(tor->info.totalSize/ (1+ (end-begin))));
     165
     166  return changed;
    158167}
    159168
     
    164173struct verify_node
    165174{
    166     tr_torrent *         torrent;
    167     tr_verify_done_cb    verify_done_cb;
    168     uint64_t             current_size;
     175  tr_torrent *         torrent;
     176  tr_verify_done_cb    verify_done_cb;
     177  uint64_t             current_size;
    169178};
    170179
    171180static void
    172 fireCheckDone( tr_torrent * tor, tr_verify_done_cb verify_done_cb )
    173 {
    174     assert( tr_isTorrent( tor ) );
    175 
    176     if( verify_done_cb )
    177         verify_done_cb( tor );
     181fireCheckDone (tr_torrent * tor, tr_verify_done_cb verify_done_cb)
     182{
     183  assert (tr_isTorrent (tor));
     184
     185  if (verify_done_cb)
     186    verify_done_cb (tor);
    178187}
    179188
     
    184193
    185194static tr_lock*
    186 getVerifyLock( void )
    187 {
    188     static tr_lock * lock = NULL;
    189     if( lock == NULL )
    190         lock = tr_lockNew( );
    191     return lock;
     195getVerifyLock (void)
     196{
     197  static tr_lock * lock = NULL;
     198
     199  if (lock == NULL)
     200    lock = tr_lockNew ();
     201
     202  return lock;
    192203}
    193204
    194205static void
    195 verifyThreadFunc( void * unused UNUSED )
    196 {
    197     for( ;; )
     206verifyThreadFunc (void * unused UNUSED)
     207{
     208  for (;;)
    198209    {
    199         int changed = 0;
    200         tr_torrent * tor;
    201         struct verify_node * node;
    202 
    203         tr_lockLock( getVerifyLock( ) );
    204         stopCurrent = false;
    205         node = (struct verify_node*) verifyList ? verifyList->data : NULL;
    206         if( node == NULL )
    207         {
    208             currentNode.torrent = NULL;
    209             break;
    210         }
    211 
    212         currentNode = *node;
    213         tor = currentNode.torrent;
    214         tr_list_remove_data( &verifyList, node );
    215         tr_free( node );
    216         tr_lockUnlock( getVerifyLock( ) );
    217 
    218         tr_torinf( tor, "%s", _( "Verifying torrent" ) );
    219         tr_torrentSetVerifyState( tor, TR_VERIFY_NOW );
    220         changed = verifyTorrent( tor, &stopCurrent );
    221         tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
    222         assert( tr_isTorrent( tor ) );
    223 
    224         if( !stopCurrent )
    225         {
    226             if( changed )
    227                 tr_torrentSetDirty( tor );
    228             fireCheckDone( tor, currentNode.verify_done_cb );
     210      int changed = 0;
     211      tr_torrent * tor;
     212      struct verify_node * node;
     213
     214      tr_lockLock (getVerifyLock ());
     215      stopCurrent = false;
     216      node = (struct verify_node*) verifyList ? verifyList->data : NULL;
     217      if (node == NULL)
     218        {
     219          currentNode.torrent = NULL;
     220          break;
     221        }
     222
     223      currentNode = *node;
     224      tor = currentNode.torrent;
     225      tr_list_remove_data (&verifyList, node);
     226      tr_free (node);
     227      tr_lockUnlock (getVerifyLock ());
     228
     229      tr_torinf (tor, "%s", _ ("Verifying torrent"));
     230      tr_torrentSetVerifyState (tor, TR_VERIFY_NOW);
     231      changed = verifyTorrent (tor, &stopCurrent);
     232      tr_torrentSetVerifyState (tor, TR_VERIFY_NONE);
     233      assert (tr_isTorrent (tor));
     234
     235      if (!stopCurrent)
     236        {
     237          if (changed)
     238            tr_torrentSetDirty (tor);
     239          fireCheckDone (tor, currentNode.verify_done_cb);
    229240        }
    230241    }
    231242
    232     verifyThread = NULL;
    233     tr_lockUnlock( getVerifyLock( ) );
     243  verifyThread = NULL;
     244  tr_lockUnlock (getVerifyLock ());
    234245}
    235246
    236247static int
    237 compareVerifyByPriorityAndSize( const void * va, const void * vb )
    238 {
    239     const struct verify_node * a = va;
    240     const struct verify_node * b = vb;
    241 
    242     /* higher priority comes before lower priority */
    243     const tr_priority_t pa = tr_torrentGetPriority( a->torrent );
    244     const tr_priority_t pb = tr_torrentGetPriority( b->torrent );
    245     if( pa != pb )
    246         return pa > pb ? -1 : 1;
    247 
    248     /* smaller torrents come before larger ones because they verify faster */
    249     if( a->current_size < b->current_size ) return -1;
    250     if( a->current_size > b->current_size ) return  1;
    251     return 0;
     248compareVerifyByPriorityAndSize (const void * va, const void * vb)
     249{
     250  const struct verify_node * a = va;
     251  const struct verify_node * b = vb;
     252
     253  /* higher priority comes before lower priority */
     254  const tr_priority_t pa = tr_torrentGetPriority (a->torrent);
     255  const tr_priority_t pb = tr_torrentGetPriority (b->torrent);
     256  if (pa != pb)
     257    return pa > pb ? -1 : 1;
     258
     259  /* smaller torrents come before larger ones because they verify faster */
     260  if (a->current_size < b->current_size)
     261    return -1;
     262  if (a->current_size > b->current_size)
     263    return  1;
     264  return 0;
    252265}
    253266
    254267void
    255 tr_verifyAdd( tr_torrent * tor, tr_verify_done_cb verify_done_cb )
    256 {
    257     struct verify_node * node;
    258 
    259     assert( tr_isTorrent( tor ) );
    260     tr_torinf( tor, "%s", _( "Queued for verification" ) );
    261 
    262     node = tr_new( struct verify_node, 1 );
    263     node->torrent = tor;
    264     node->verify_done_cb = verify_done_cb;
    265     node->current_size = tr_torrentGetCurrentSizeOnDisk( tor );
    266 
    267     tr_lockLock( getVerifyLock( ) );
    268     tr_torrentSetVerifyState( tor, TR_VERIFY_WAIT );
    269     tr_list_insert_sorted( &verifyList, node, compareVerifyByPriorityAndSize );
    270     if( verifyThread == NULL )
    271         verifyThread = tr_threadNew( verifyThreadFunc, NULL );
    272     tr_lockUnlock( getVerifyLock( ) );
     268tr_verifyAdd (tr_torrent * tor, tr_verify_done_cb verify_done_cb)
     269{
     270  struct verify_node * node;
     271
     272  assert (tr_isTorrent (tor));
     273  tr_torinf (tor, "%s", _ ("Queued for verification"));
     274
     275  node = tr_new (struct verify_node, 1);
     276  node->torrent = tor;
     277  node->verify_done_cb = verify_done_cb;
     278  node->current_size = tr_torrentGetCurrentSizeOnDisk (tor);
     279
     280  tr_lockLock (getVerifyLock ());
     281  tr_torrentSetVerifyState (tor, TR_VERIFY_WAIT);
     282  tr_list_insert_sorted (&verifyList, node, compareVerifyByPriorityAndSize);
     283  if (verifyThread == NULL)
     284    verifyThread = tr_threadNew (verifyThreadFunc, NULL);
     285  tr_lockUnlock (getVerifyLock ());
    273286}
    274287
    275288static int
    276 compareVerifyByTorrent( const void * va, const void * vb )
    277 {
    278     const struct verify_node * a = va;
    279     const tr_torrent * b = vb;
    280     return a->torrent - b;
     289compareVerifyByTorrent (const void * va, const void * vb)
     290{
     291  const struct verify_node * a = va;
     292  const tr_torrent * b = vb;
     293  return a->torrent - b;
    281294}
    282295
    283296void
    284 tr_verifyRemove( tr_torrent * tor )
    285 {
    286     tr_lock * lock = getVerifyLock( );
    287     tr_lockLock( lock );
    288 
    289     assert( tr_isTorrent( tor ) );
    290 
    291     if( tor == currentNode.torrent )
     297tr_verifyRemove (tr_torrent * tor)
     298{
     299  tr_lock * lock = getVerifyLock ();
     300  tr_lockLock (lock);
     301
     302  assert (tr_isTorrent (tor));
     303
     304  if (tor == currentNode.torrent)
    292305    {
    293         stopCurrent = true;
    294         while( stopCurrent )
    295         {
    296             tr_lockUnlock( lock );
    297             tr_wait_msec( 100 );
    298             tr_lockLock( lock );
     306      stopCurrent = true;
     307
     308      while (stopCurrent)
     309        {
     310          tr_lockUnlock (lock);
     311          tr_wait_msec (100);
     312          tr_lockLock (lock);
    299313        }
    300314    }
    301     else
     315  else
    302316    {
    303         tr_free( tr_list_remove( &verifyList, tor, compareVerifyByTorrent ) );
    304         tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
     317      tr_free (tr_list_remove (&verifyList, tor, compareVerifyByTorrent));
     318      tr_torrentSetVerifyState (tor, TR_VERIFY_NONE);
    305319    }
    306320
    307     tr_lockUnlock( lock );
     321  tr_lockUnlock (lock);
    308322}
    309323
    310324void
    311 tr_verifyClose( tr_session * session UNUSED )
    312 {
    313     tr_lockLock( getVerifyLock( ) );
    314 
    315     stopCurrent = true;
    316     tr_list_free( &verifyList, tr_free );
    317 
    318     tr_lockUnlock( getVerifyLock( ) );
    319 }
     325tr_verifyClose (tr_session * session UNUSED)
     326{
     327  tr_lockLock (getVerifyLock ());
     328
     329  stopCurrent = true;
     330  tr_list_free (&verifyList, tr_free);
     331
     332  tr_lockUnlock (getVerifyLock ());
     333}
     334
Note: See TracChangeset for help on using the changeset viewer.