source: trunk/libtransmission/verify.c @ 8169

Last change on this file since 8169 was 8169, checked in by charles, 13 years ago

(trunk libT) posix_fadvise() tweak to the new verify code.

  • Property svn:keywords set to Date Rev Author Id
File size: 8.1 KB
Line 
1/*
2 * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * 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)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: verify.c 8169 2009-04-06 23:51:55Z charles $
11 */
12
13#include <unistd.h> /* S_ISREG */
14#include <sys/stat.h>
15
16#ifdef HAVE_POSIX_FADVISE
17 #define _XOPEN_SOURCE 600
18 #include <fcntl.h> /* posix_fadvise() */
19#endif
20
21#include <openssl/sha.h>
22
23#include "transmission.h"
24#include "completion.h"
25#include "resume.h" /* tr_torrentSaveResume() */
26#include "inout.h"
27#include "list.h"
28#include "platform.h"
29#include "torrent.h"
30#include "utils.h" /* tr_buildPath */
31#include "verify.h"
32
33
34/***
35****
36***/
37
38/* #define STOPWATCH */
39
40static tr_bool
41verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
42{
43    SHA_CTX sha;
44    FILE * fp = NULL;
45    size_t filePos = 0;
46    tr_bool changed = 0;
47    tr_bool hadPiece = 0;
48    uint32_t piecePos = 0;
49    tr_file_index_t fileIndex = 0;
50    tr_piece_index_t pieceIndex = 0;
51    const int64_t buflen = tor->info.pieceSize;
52    uint8_t * buffer = tr_new( uint8_t, buflen );
53#ifdef STOPWATCH
54    time_t now = time( NULL );
55#endif
56
57    SHA1_Init( &sha );
58
59    while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) )
60    {
61        int64_t leftInPiece;
62        int64_t leftInFile;
63        int64_t bytesThisPass;
64        const tr_file * file = &tor->info.files[fileIndex];
65
66        /* if we're starting a new piece... */
67        if( piecePos == 0 )
68        {
69            hadPiece = tr_cpPieceIsComplete( &tor->completion, pieceIndex );
70            /* fprintf( stderr, "starting piece %d of %d\n", (int)pieceIndex, (int)tor->info.pieceCount ); */
71        }
72
73        /* if we're starting a new file... */
74        if( !filePos && !fp )
75        {
76            char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
77            fp = fopen( filename, "rb" );
78#ifdef HAVE_POSIX_FADVISE
79            posix_fadvise( fileno( fp ), 0, 0, POSIX_FADV_SEQUENTIAL );
80#endif
81            /* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */
82            tr_free( filename );
83        }
84
85        /* figure out how much we can read this pass */
86        leftInPiece = tr_torPieceCountBytes( tor, pieceIndex ) - piecePos;
87        leftInFile = file->length - filePos;
88        bytesThisPass = MIN( leftInFile, leftInPiece );
89        bytesThisPass = MIN( bytesThisPass, buflen );
90        /* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
91
92        /* read a bit */
93        if( fp && !fseek( fp, filePos, SEEK_SET ) ) {
94            const int64_t numRead = fread( buffer, 1, bytesThisPass, fp );
95            if( numRead == bytesThisPass )
96                SHA1_Update( &sha, buffer, numRead );
97        }
98
99        /* move our offsets */
100        leftInPiece -= bytesThisPass;
101        leftInFile -= bytesThisPass;
102        piecePos += bytesThisPass;
103        filePos += bytesThisPass;
104
105        /* if we're finishing a piece... */
106        if( leftInPiece == 0 )
107        {
108            tr_bool hasPiece;
109            uint8_t hash[SHA_DIGEST_LENGTH];
110
111            SHA1_Final( hash, &sha );
112            hasPiece = !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
113            /* fprintf( stderr, "do the hashes match? %s\n", (hasPiece?"yes":"no") ); */
114
115            if( hasPiece ) {
116                tr_torrentSetHasPiece( tor, pieceIndex, TRUE );
117                if( !hadPiece )
118                    changed = TRUE;
119            } else if( hadPiece ) {
120                tr_torrentSetHasPiece( tor, pieceIndex, FALSE );
121                changed = TRUE;
122            }
123            tr_torrentSetPieceChecked( tor, pieceIndex, TRUE );
124
125            SHA1_Init( &sha );
126            ++pieceIndex;
127            piecePos = 0;
128        }
129
130        /* if we're finishing a file... */
131        if( leftInFile == 0 )
132        {
133            /* fprintf( stderr, "closing file\n" ); */
134            if( fp != NULL ) { fclose( fp ); fp = NULL; }
135            ++fileIndex;
136            filePos = 0;
137        }
138    }
139
140    /* cleanup */
141    if( fp != NULL )
142        fclose( fp );
143    tr_free( buffer );
144
145#ifdef STOPWATCH
146{
147    time_t now2 = time( NULL );
148    fprintf( stderr, "it took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)\n",
149             (int)(now2-now), tor->info.totalSize, (uint64_t)(tor->info.totalSize/(1+(now2-now))) );
150}
151#endif
152
153    return changed;
154}
155
156/***
157****
158***/
159
160struct verify_node
161{
162    tr_torrent *         torrent;
163    tr_verify_done_cb    verify_done_cb;
164};
165
166static void
167fireCheckDone( tr_torrent * tor, tr_verify_done_cb verify_done_cb )
168{
169    assert( tr_isTorrent( tor ) );
170
171    if( verify_done_cb )
172        verify_done_cb( tor );
173}
174
175static struct verify_node currentNode;
176static tr_list * verifyList = NULL;
177static tr_thread * verifyThread = NULL;
178static tr_bool stopCurrent = FALSE;
179
180static tr_lock*
181getVerifyLock( void )
182{
183    static tr_lock * lock = NULL;
184
185    if( lock == NULL )
186        lock = tr_lockNew( );
187    return lock;
188}
189
190static void
191verifyThreadFunc( void * unused UNUSED )
192{
193    for( ;; )
194    {
195        int                  changed = 0;
196        tr_torrent         * tor;
197        struct verify_node * node;
198
199        tr_lockLock( getVerifyLock( ) );
200        stopCurrent = FALSE;
201        node = (struct verify_node*) verifyList ? verifyList->data : NULL;
202        if( node == NULL )
203        {
204            currentNode.torrent = NULL;
205            break;
206        }
207
208        currentNode = *node;
209        tor = currentNode.torrent;
210        tr_list_remove_data( &verifyList, node );
211        tr_free( node );
212        tr_lockUnlock( getVerifyLock( ) );
213
214        tr_torinf( tor, _( "Verifying torrent" ) );
215        tr_torrentSetVerifyState( tor, TR_VERIFY_NOW );
216        changed = verifyTorrent( tor, &stopCurrent );
217        tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
218        assert( tr_isTorrent( tor ) );
219
220        if( !stopCurrent )
221        {
222            if( changed )
223                tr_torrentSaveResume( tor );
224            fireCheckDone( tor, currentNode.verify_done_cb );
225        }
226    }
227
228    verifyThread = NULL;
229    tr_lockUnlock( getVerifyLock( ) );
230}
231
232void
233tr_verifyAdd( tr_torrent *      tor,
234              tr_verify_done_cb verify_done_cb )
235{
236    const int uncheckedCount = tr_torrentCountUncheckedPieces( tor );
237
238    assert( tr_isTorrent( tor ) );
239
240    if( !uncheckedCount )
241    {
242        /* doesn't need to be checked... */
243        fireCheckDone( tor, verify_done_cb );
244    }
245    else
246    {
247        struct verify_node * node;
248
249        tr_torinf( tor, _( "Queued for verification" ) );
250
251        node = tr_new( struct verify_node, 1 );
252        node->torrent = tor;
253        node->verify_done_cb = verify_done_cb;
254
255        tr_lockLock( getVerifyLock( ) );
256        tr_torrentSetVerifyState( tor, verifyList ? TR_VERIFY_WAIT : TR_VERIFY_NOW );
257        tr_list_append( &verifyList, node );
258        if( verifyThread == NULL )
259            verifyThread = tr_threadNew( verifyThreadFunc, NULL );
260        tr_lockUnlock( getVerifyLock( ) );
261    }
262}
263
264static int
265compareVerifyByTorrent( const void * va,
266                        const void * vb )
267{
268    const struct verify_node * a = va;
269    const tr_torrent *         b = vb;
270
271    return a->torrent - b;
272}
273
274int
275tr_verifyInProgress( const tr_torrent * tor )
276{
277    int found = FALSE;
278    tr_lock * lock = getVerifyLock( );
279    tr_lockLock( lock );
280
281    assert( tr_isTorrent( tor ) );
282
283    found = ( tor == currentNode.torrent )
284         || ( tr_list_find( verifyList, tor, compareVerifyByTorrent ) != NULL );
285
286    tr_lockUnlock( lock );
287    return found;
288}
289
290void
291tr_verifyRemove( tr_torrent * tor )
292{
293    tr_lock * lock = getVerifyLock( );
294    tr_lockLock( lock );
295
296    assert( tr_isTorrent( tor ) );
297
298    if( tor == currentNode.torrent )
299    {
300        stopCurrent = TRUE;
301        while( stopCurrent )
302        {
303            tr_lockUnlock( lock );
304            tr_wait( 100 );
305            tr_lockLock( lock );
306        }
307    }
308    else
309    {
310        tr_free( tr_list_remove( &verifyList, tor, compareVerifyByTorrent ) );
311        tor->verifyState = TR_VERIFY_NONE;
312    }
313
314    tr_lockUnlock( lock );
315}
316
Note: See TracBrowser for help on using the repository browser.