source: trunk/libtransmission/verify.c @ 6896

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

have tr_buildPath() allocate memory from the heap rather than using an input buffer

  • Property svn:keywords set to Date Rev Author Id
File size: 5.5 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.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 6896 2008-10-14 03:03:29Z charles $
11 */
12
13#include <sys/stat.h>
14
15#include "transmission.h"
16#include "completion.h"
17#include "resume.h" /* tr_torrentSaveResume() */
18#include "inout.h"
19#include "list.h"
20#include "platform.h"
21#include "torrent.h"
22#include "utils.h" /* tr_buildPath */
23#include "verify.h"
24
25/**
26***
27**/
28
29struct verify_node
30{
31    tr_torrent *         torrent;
32    tr_verify_done_cb    verify_done_cb;
33};
34
35static void
36fireCheckDone( tr_torrent *      torrent,
37               tr_verify_done_cb verify_done_cb )
38{
39    if( verify_done_cb )
40        verify_done_cb( torrent );
41}
42
43static struct verify_node currentNode;
44
45static tr_list *          verifyList = NULL;
46
47static tr_thread *        verifyThread = NULL;
48
49static int                stopCurrent = FALSE;
50
51static tr_lock*
52getVerifyLock( void )
53{
54    static tr_lock * lock = NULL;
55
56    if( lock == NULL )
57        lock = tr_lockNew( );
58    return lock;
59}
60
61static int
62checkFile( tr_torrent *    tor,
63           tr_file_index_t fileIndex,
64           int *           abortFlag )
65{
66    tr_piece_index_t i;
67    int              changed = FALSE;
68    int              nofile;
69    struct stat      sb;
70    char           * path;
71    const tr_file  * file = &tor->info.files[fileIndex];
72
73    path = tr_buildPath( tor->downloadDir, file->name, NULL );
74    nofile = stat( path, &sb ) || !S_ISREG( sb.st_mode );
75
76    for( i = file->firstPiece;
77         i <= file->lastPiece && i < tor->info.pieceCount && ( !*abortFlag );
78         ++i )
79    {
80        if( nofile )
81        {
82            tr_torrentSetHasPiece( tor, i, 0 );
83        }
84        else if( !tr_torrentIsPieceChecked( tor, i ) )
85        {
86            const int      wasComplete = tr_cpPieceIsComplete(
87                tor->completion, i );
88
89            if( tr_ioTestPiece( tor, i ) ) /* 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
128        tr_lockLock( getVerifyLock( ) );
129        stopCurrent = FALSE;
130        node = (struct verify_node*) verifyList ? verifyList->data : NULL;
131        if( node == NULL )
132        {
133            currentNode.torrent = NULL;
134            break;
135        }
136
137        currentNode = *node;
138        tor = currentNode.torrent;
139        tr_list_remove_data( &verifyList, node );
140        tr_free( node );
141        tr_lockUnlock( getVerifyLock( ) );
142
143        tor->verifyState = TR_VERIFY_NOW;
144
145        tr_torinf( tor, _( "Verifying torrent" ) );
146        for( i = 0; i < tor->info.fileCount && !stopCurrent; ++i )
147            changed |= checkFile( tor, i, &stopCurrent );
148
149        tor->verifyState = TR_VERIFY_NONE;
150
151        if( !stopCurrent )
152        {
153            if( changed )
154                tr_torrentSaveResume( tor );
155            fireCheckDone( tor, currentNode.verify_done_cb );
156        }
157    }
158
159    verifyThread = NULL;
160    tr_lockUnlock( getVerifyLock( ) );
161}
162
163void
164tr_verifyAdd( tr_torrent *      tor,
165              tr_verify_done_cb verify_done_cb )
166{
167    const int uncheckedCount = tr_torrentCountUncheckedPieces( tor );
168
169    if( !uncheckedCount )
170    {
171        /* doesn't need to be checked... */
172        fireCheckDone( tor, verify_done_cb );
173    }
174    else
175    {
176        struct verify_node * node;
177
178        tr_torinf( tor, _( "Queued for verification" ) );
179
180        node = tr_new( struct verify_node, 1 );
181        node->torrent = tor;
182        node->verify_done_cb = verify_done_cb;
183
184        tr_lockLock( getVerifyLock( ) );
185        tor->verifyState = verifyList ? TR_VERIFY_WAIT : TR_VERIFY_NOW;
186        tr_list_append( &verifyList, node );
187        if( verifyThread == NULL )
188            verifyThread = tr_threadNew( verifyThreadFunc, NULL );
189        tr_lockUnlock( getVerifyLock( ) );
190    }
191}
192
193static int
194compareVerifyByTorrent( const void * va,
195                        const void * vb )
196{
197    const struct verify_node * a = va;
198    const tr_torrent *         b = vb;
199
200    return a->torrent - b;
201}
202
203void
204tr_verifyRemove( tr_torrent * tor )
205{
206    tr_lock * lock = getVerifyLock( );
207
208    tr_lockLock( lock );
209
210    if( tor == currentNode.torrent )
211    {
212        stopCurrent = TRUE;
213        while( stopCurrent )
214        {
215            tr_lockUnlock( lock );
216            tr_wait( 100 );
217            tr_lockLock( lock );
218        }
219    }
220    else
221    {
222        tr_free( tr_list_remove( &verifyList, tor, compareVerifyByTorrent ) );
223        tor->verifyState = TR_VERIFY_NONE;
224    }
225
226    tr_lockUnlock( lock );
227}
228
Note: See TracBrowser for help on using the repository browser.