source: trunk/libtransmission/verify.c @ 7816

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

(trunk libT) more assertions

  • Property svn:keywords set to Date Rev Author Id
File size: 6.2 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 7816 2009-01-30 00:41:08Z charles $
11 */
12
13#include <unistd.h> /* S_ISREG */
14#include <sys/stat.h>
15
16#include "transmission.h"
17#include "completion.h"
18#include "resume.h" /* tr_torrentSaveResume() */
19#include "inout.h"
20#include "list.h"
21#include "platform.h"
22#include "torrent.h"
23#include "utils.h" /* tr_buildPath */
24#include "verify.h"
25
26/**
27***
28**/
29
30struct verify_node
31{
32    tr_torrent *         torrent;
33    tr_verify_done_cb    verify_done_cb;
34};
35
36static void
37fireCheckDone( tr_torrent * tor, tr_verify_done_cb verify_done_cb )
38{
39    assert( tr_isTorrent( tor ) );
40
41    if( verify_done_cb )
42        verify_done_cb( tor );
43}
44
45static struct verify_node currentNode;
46static tr_list * verifyList = NULL;
47static tr_thread * verifyThread = NULL;
48static int stopCurrent = FALSE;
49
50static tr_lock*
51getVerifyLock( void )
52{
53    static tr_lock * lock = NULL;
54
55    if( lock == NULL )
56        lock = tr_lockNew( );
57    return lock;
58}
59
60static int
61checkFile( tr_torrent      * tor,
62           void            * buffer,
63           size_t            buflen,
64           tr_file_index_t   fileIndex,
65           int             * abortFlag )
66{
67    tr_piece_index_t i;
68    int              changed = FALSE;
69    int              nofile;
70    struct stat      sb;
71    char           * path;
72    const tr_file  * file = &tor->info.files[fileIndex];
73
74    path = tr_buildPath( tor->downloadDir, file->name, NULL );
75    nofile = stat( path, &sb ) || !S_ISREG( sb.st_mode );
76
77    for( i = file->firstPiece;
78         i <= file->lastPiece && i < tor->info.pieceCount && ( !*abortFlag );
79         ++i )
80    {
81        if( nofile )
82        {
83            tr_torrentSetHasPiece( tor, i, 0 );
84        }
85        else if( !tr_torrentIsPieceChecked( tor, i ) )
86        {
87            const int wasComplete = tr_cpPieceIsComplete( &tor->completion, i );
88
89            if( tr_ioTestPiece( tor, i, buffer, buflen ) ) /* yay */
90            {
91                tr_torrentSetHasPiece( tor, i, TRUE );
92                if( !wasComplete )
93                    changed = TRUE;
94            }
95            else
96            {
97                /* if we were wrong about it being complete,
98                 * reset and start again.  if we were right about
99                 * it being incomplete, do nothing -- we don't
100                 * want to lose blocks in those incomplete pieces */
101
102                if( wasComplete )
103                {
104                    tr_torrentSetHasPiece( tor, i, FALSE );
105                    changed = TRUE;
106                }
107            }
108        }
109
110        tr_torrentSetPieceChecked( tor, i, TRUE );
111    }
112
113    tr_free( path );
114
115    return changed;
116}
117
118static void
119verifyThreadFunc( void * unused UNUSED )
120{
121    for( ;; )
122    {
123        int                  changed = 0;
124        tr_file_index_t      i;
125        tr_torrent         * tor;
126        struct verify_node * node;
127        void               * buffer;
128
129        tr_lockLock( getVerifyLock( ) );
130        stopCurrent = FALSE;
131        node = (struct verify_node*) verifyList ? verifyList->data : NULL;
132        if( node == NULL )
133        {
134            currentNode.torrent = NULL;
135            break;
136        }
137
138        currentNode = *node;
139        tor = currentNode.torrent;
140        tr_list_remove_data( &verifyList, node );
141        tr_free( node );
142        tr_lockUnlock( getVerifyLock( ) );
143
144        tr_torinf( tor, _( "Verifying torrent" ) );
145        assert( tr_isTorrent( tor ) );
146        tor->verifyState = TR_VERIFY_NOW;
147        buffer = tr_new( uint8_t, tor->info.pieceSize );
148        for( i = 0; i < tor->info.fileCount && !stopCurrent; ++i )
149            changed |= checkFile( tor, buffer, tor->info.pieceSize, i, &stopCurrent );
150        tr_free( buffer );
151        tor->verifyState = TR_VERIFY_NONE;
152        assert( tr_isTorrent( tor ) );
153
154        if( !stopCurrent )
155        {
156            if( changed )
157                tr_torrentSaveResume( tor );
158            fireCheckDone( tor, currentNode.verify_done_cb );
159        }
160    }
161
162    verifyThread = NULL;
163    tr_lockUnlock( getVerifyLock( ) );
164}
165
166void
167tr_verifyAdd( tr_torrent *      tor,
168              tr_verify_done_cb verify_done_cb )
169{
170    const int uncheckedCount = tr_torrentCountUncheckedPieces( tor );
171
172    assert( tr_isTorrent( tor ) );
173
174    if( !uncheckedCount )
175    {
176        /* doesn't need to be checked... */
177        fireCheckDone( tor, verify_done_cb );
178    }
179    else
180    {
181        struct verify_node * node;
182
183        tr_torinf( tor, _( "Queued for verification" ) );
184
185        node = tr_new( struct verify_node, 1 );
186        node->torrent = tor;
187        node->verify_done_cb = verify_done_cb;
188
189        tr_lockLock( getVerifyLock( ) );
190        tor->verifyState = verifyList ? TR_VERIFY_WAIT : TR_VERIFY_NOW;
191        tr_list_append( &verifyList, node );
192        if( verifyThread == NULL )
193            verifyThread = tr_threadNew( verifyThreadFunc, NULL );
194        tr_lockUnlock( getVerifyLock( ) );
195    }
196}
197
198static int
199compareVerifyByTorrent( const void * va,
200                        const void * vb )
201{
202    const struct verify_node * a = va;
203    const tr_torrent *         b = vb;
204
205    return a->torrent - b;
206}
207
208int
209tr_verifyInProgress( const tr_torrent * tor )
210{
211    int found = FALSE;
212    tr_lock * lock = getVerifyLock( );
213    tr_lockLock( lock );
214
215    assert( tr_isTorrent( tor ) );
216
217    found = ( tor == currentNode.torrent )
218         || ( tr_list_find( verifyList, tor, compareVerifyByTorrent ) != NULL );
219
220    tr_lockUnlock( lock );
221    return found;
222}
223
224void
225tr_verifyRemove( tr_torrent * tor )
226{
227    tr_lock * lock = getVerifyLock( );
228    tr_lockLock( lock );
229
230    assert( tr_isTorrent( tor ) );
231
232    if( tor == currentNode.torrent )
233    {
234        stopCurrent = TRUE;
235        while( stopCurrent )
236        {
237            tr_lockUnlock( lock );
238            tr_wait( 100 );
239            tr_lockLock( lock );
240        }
241    }
242    else
243    {
244        tr_free( tr_list_remove( &verifyList, tor, compareVerifyByTorrent ) );
245        tor->verifyState = TR_VERIFY_NONE;
246    }
247
248    tr_lockUnlock( lock );
249}
250
Note: See TracBrowser for help on using the repository browser.