source: trunk/libtransmission/verify.c @ 10904

Last change on this file since 10904 was 10904, checked in by charles, 11 years ago

(trunk libT) remove unused code

  • Property svn:keywords set to Date Rev Author Id
File size: 9.8 KB
Line 
1/*
2 * This file Copyright (C) 2007-2010 Mnemosyne LLC
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 10904 2010-06-28 13:42:45Z 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#endif
19#if defined(HAVE_POSIX_FADVISE) || defined(SYS_DARWIN)
20 #include <fcntl.h> /* posix_fadvise() / fcntl() */
21#endif
22
23#include <openssl/sha.h>
24
25#include "transmission.h"
26#include "completion.h"
27#include "fdlimit.h"
28#include "inout.h"
29#include "list.h"
30#include "platform.h"
31#include "torrent.h"
32#include "utils.h" /* tr_buildPath */
33#include "verify.h"
34
35
36/***
37****
38***/
39
40enum
41{
42    MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 100
43};
44
45/* #define STOPWATCH */
46
47static tr_bool
48verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
49{
50    SHA_CTX sha;
51    int fd = -1;
52    int64_t filePos = 0;
53    tr_bool changed = 0;
54    tr_bool hadPiece = 0;
55    time_t lastSleptAt = 0;
56    uint32_t piecePos = 0;
57    uint32_t pieceBytesRead = 0;
58    tr_file_index_t fileIndex = 0;
59    tr_file_index_t prevFileIndex = !fileIndex;
60    tr_piece_index_t pieceIndex = 0;
61    const time_t begin = tr_time( );
62    time_t end;
63    const int64_t buflen = 1024 * 128; /* 128 KiB buffer */
64    uint8_t * buffer = tr_valloc( buflen );
65
66    tr_torrentUncheck( tor );
67
68    SHA1_Init( &sha );
69
70    while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) )
71    {
72        int64_t leftInPiece;
73        int64_t leftInFile;
74        int64_t bytesThisPass;
75        const tr_file * file = &tor->info.files[fileIndex];
76
77        /* if we're starting a new piece... */
78        if( piecePos == 0 )
79        {
80            hadPiece = tr_cpPieceIsComplete( &tor->completion, pieceIndex );
81            /* fprintf( stderr, "starting piece %d of %d\n", (int)pieceIndex, (int)tor->info.pieceCount ); */
82        }
83
84        /* if we're starting a new file... */
85        if( !filePos && (fd<0) && (fileIndex!=prevFileIndex) )
86        {
87            char * filename = tr_torrentFindFile( tor, fileIndex );
88            fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
89            /* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
90            tr_free( filename );
91            prevFileIndex = fileIndex;
92        }
93
94        /* figure out how much we can read this pass */
95        leftInPiece = tr_torPieceCountBytes( tor, pieceIndex ) - piecePos;
96        leftInFile = file->length - filePos;
97        bytesThisPass = MIN( leftInFile, leftInPiece );
98        bytesThisPass = MIN( bytesThisPass, buflen );
99        /* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
100
101        /* read a bit */
102        if( fd >= 0 ) {
103            const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
104            if( numRead == bytesThisPass )
105                SHA1_Update( &sha, buffer, numRead );
106            if( numRead > 0 ) {
107                pieceBytesRead += numRead;
108#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
109                posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
110#endif
111            }
112        }
113
114        /* move our offsets */
115        leftInPiece -= bytesThisPass;
116        leftInFile -= bytesThisPass;
117        piecePos += bytesThisPass;
118        filePos += bytesThisPass;
119
120        /* if we're finishing a piece... */
121        if( leftInPiece == 0 )
122        {
123            time_t now;
124            tr_bool hasPiece;
125            uint8_t hash[SHA_DIGEST_LENGTH];
126
127            SHA1_Final( hash, &sha );
128            hasPiece = !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
129            /* fprintf( stderr, "do the hashes match? %s\n", (hasPiece?"yes":"no") ); */
130
131            if( hasPiece ) {
132                tr_torrentSetHasPiece( tor, pieceIndex, TRUE );
133                if( !hadPiece )
134                    changed = TRUE;
135            } else if( hadPiece ) {
136                tr_torrentSetHasPiece( tor, pieceIndex, FALSE );
137                changed = TRUE;
138            }
139            tr_torrentSetPieceChecked( tor, pieceIndex, TRUE );
140            now = tr_time( );
141            tor->anyDate = now;
142
143            /* sleeping even just a few msec per second goes a long
144             * way towards reducing IO load... */
145            if( lastSleptAt != now ) {
146                lastSleptAt = now;
147                tr_wait_msec( MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY );
148            }
149
150            SHA1_Init( &sha );
151            ++pieceIndex;
152            piecePos = 0;
153            pieceBytesRead = 0;
154        }
155
156        /* if we're finishing a file... */
157        if( leftInFile == 0 )
158        {
159            /* fprintf( stderr, "closing file\n" ); */
160            if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
161            ++fileIndex;
162            filePos = 0;
163        }
164    }
165
166    /* cleanup */
167    if( fd >= 0 )
168        tr_close_file( fd );
169    free( buffer );
170
171    /* stopwatch */
172    end = tr_time( );
173    tr_tordbg( tor, "it took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
174               (int)(end-begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize/(1+(end-begin))) );
175
176    return changed;
177}
178
179/***
180****
181***/
182
183struct verify_node
184{
185    tr_torrent *         torrent;
186    tr_verify_done_cb    verify_done_cb;
187};
188
189static void
190fireCheckDone( tr_torrent * tor, tr_verify_done_cb verify_done_cb )
191{
192    assert( tr_isTorrent( tor ) );
193
194    if( verify_done_cb )
195        verify_done_cb( tor );
196}
197
198static struct verify_node currentNode;
199static tr_list * verifyList = NULL;
200static tr_thread * verifyThread = NULL;
201static tr_bool stopCurrent = FALSE;
202
203static tr_lock*
204getVerifyLock( void )
205{
206    static tr_lock * lock = NULL;
207
208    if( lock == NULL )
209        lock = tr_lockNew( );
210    return lock;
211}
212
213static void
214verifyThreadFunc( void * unused UNUSED )
215{
216    for( ;; )
217    {
218        int                  changed = 0;
219        tr_torrent         * tor;
220        struct verify_node * node;
221
222        tr_lockLock( getVerifyLock( ) );
223        stopCurrent = FALSE;
224        node = (struct verify_node*) verifyList ? verifyList->data : NULL;
225        if( node == NULL )
226        {
227            currentNode.torrent = NULL;
228            break;
229        }
230
231        currentNode = *node;
232        tor = currentNode.torrent;
233        tr_list_remove_data( &verifyList, node );
234        tr_free( node );
235        tr_lockUnlock( getVerifyLock( ) );
236
237        tr_torinf( tor, "%s", _( "Verifying torrent" ) );
238        tr_torrentSetVerifyState( tor, TR_VERIFY_NOW );
239        changed = verifyTorrent( tor, &stopCurrent );
240        tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
241        assert( tr_isTorrent( tor ) );
242
243        if( !stopCurrent )
244        {
245            if( changed )
246                tr_torrentSetDirty( tor );
247            fireCheckDone( tor, currentNode.verify_done_cb );
248        }
249    }
250
251    verifyThread = NULL;
252    tr_lockUnlock( getVerifyLock( ) );
253}
254
255static tr_bool
256torrentHasAnyLocalData( const tr_torrent * tor )
257{
258    tr_file_index_t i;
259    tr_bool hasAny = FALSE;
260    const tr_file_index_t n = tor->info.fileCount;
261
262    assert( tr_isTorrent( tor ) );
263
264    for( i=0; i<n && !hasAny; ++i )
265    {
266        struct stat sb;
267        char * path = tr_torrentFindFile( tor, i );
268        if( ( path != NULL ) && !stat( path, &sb ) && ( sb.st_size > 0 ) )
269            hasAny = TRUE;
270        tr_free( path );
271    }
272
273    return hasAny;
274}
275
276void
277tr_verifyAdd( tr_torrent *      tor,
278              tr_verify_done_cb verify_done_cb )
279{
280    assert( tr_isTorrent( tor ) );
281
282    if( tr_torrentCountUncheckedPieces( tor ) == 0 )
283    {
284        /* doesn't need to be checked... */
285        fireCheckDone( tor, verify_done_cb );
286    }
287    else if( !torrentHasAnyLocalData( tor ) )
288    {
289        /* we haven't downloaded anything for this torrent yet...
290         * no need to leave it waiting in the back of the queue.
291         * we can mark it as all-missing from here and fire
292         * the "done" callback */
293        const tr_bool hadAny = tr_cpHaveTotal( &tor->completion ) != 0;
294        tr_piece_index_t i;
295        for( i=0; i<tor->info.pieceCount; ++i ) {
296            tr_torrentSetHasPiece( tor, i, FALSE );
297            tr_torrentSetPieceChecked( tor, i, TRUE );
298        }
299        if( hadAny ) /* if we thought we had some, flag as dirty */
300            tr_torrentSetDirty( tor );
301        fireCheckDone( tor, verify_done_cb );
302    }
303    else
304    {
305        struct verify_node * node;
306
307        tr_torinf( tor, "%s", _( "Queued for verification" ) );
308
309        node = tr_new( struct verify_node, 1 );
310        node->torrent = tor;
311        node->verify_done_cb = verify_done_cb;
312
313        tr_lockLock( getVerifyLock( ) );
314        tr_torrentSetVerifyState( tor, TR_VERIFY_WAIT );
315        tr_list_append( &verifyList, node );
316        if( verifyThread == NULL )
317            verifyThread = tr_threadNew( verifyThreadFunc, NULL );
318        tr_lockUnlock( getVerifyLock( ) );
319    }
320}
321
322static int
323compareVerifyByTorrent( const void * va, const void * vb )
324{
325    const struct verify_node * a = va;
326    const tr_torrent * b = vb;
327    return a->torrent - b;
328}
329
330void
331tr_verifyRemove( tr_torrent * tor )
332{
333    tr_lock * lock = getVerifyLock( );
334    tr_lockLock( lock );
335
336    assert( tr_isTorrent( tor ) );
337
338    if( tor == currentNode.torrent )
339    {
340        stopCurrent = TRUE;
341        while( stopCurrent )
342        {
343            tr_lockUnlock( lock );
344            tr_wait_msec( 100 );
345            tr_lockLock( lock );
346        }
347    }
348    else
349    {
350        tr_free( tr_list_remove( &verifyList, tor, compareVerifyByTorrent ) );
351        tr_torrentSetVerifyState( tor, TR_VERIFY_NONE );
352    }
353
354    tr_lockUnlock( lock );
355}
356
357void
358tr_verifyClose( tr_session * session UNUSED )
359{
360    tr_lockLock( getVerifyLock( ) );
361
362    stopCurrent = TRUE;
363    tr_list_free( &verifyList, tr_free );
364
365    tr_lockUnlock( getVerifyLock( ) );
366}
Note: See TracBrowser for help on using the repository browser.