source: trunk/libtransmission/verify.c @ 11339

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

(trunk libT) #3622 "verification ignores return value of pread" -- commit fixVerify.patch

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