1 | /* |
---|
2 | * This file Copyright (C) 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 13868 2013-01-25 23:34:20Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <string.h> /* memcmp () */ |
---|
14 | #include <stdlib.h> /* free () */ |
---|
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 "fdlimit.h" |
---|
26 | #include "list.h" |
---|
27 | #include "log.h" |
---|
28 | #include "platform.h" /* tr_lock () */ |
---|
29 | #include "torrent.h" |
---|
30 | #include "utils.h" /* tr_valloc (), tr_free () */ |
---|
31 | #include "verify.h" |
---|
32 | |
---|
33 | /*** |
---|
34 | **** |
---|
35 | ***/ |
---|
36 | |
---|
37 | enum |
---|
38 | { |
---|
39 | MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 100 |
---|
40 | }; |
---|
41 | |
---|
42 | static bool |
---|
43 | verifyTorrent (tr_torrent * tor, bool * stopFlag) |
---|
44 | { |
---|
45 | time_t end; |
---|
46 | SHA_CTX sha; |
---|
47 | int fd = -1; |
---|
48 | int64_t filePos = 0; |
---|
49 | bool changed = 0; |
---|
50 | bool hadPiece = 0; |
---|
51 | time_t lastSleptAt = 0; |
---|
52 | uint32_t piecePos = 0; |
---|
53 | tr_file_index_t fileIndex = 0; |
---|
54 | tr_file_index_t prevFileIndex = !fileIndex; |
---|
55 | tr_piece_index_t pieceIndex = 0; |
---|
56 | const time_t begin = tr_time (); |
---|
57 | const size_t buflen = 1024 * 128; /* 128 KiB buffer */ |
---|
58 | uint8_t * buffer = tr_valloc (buflen); |
---|
59 | |
---|
60 | SHA1_Init (&sha); |
---|
61 | |
---|
62 | tr_logAddTorDbg (tor, "%s", "verifying torrent..."); |
---|
63 | tr_torrentSetChecked (tor, 0); |
---|
64 | while (!*stopFlag && (pieceIndex < tor->info.pieceCount)) |
---|
65 | { |
---|
66 | uint32_t leftInPiece; |
---|
67 | uint32_t bytesThisPass; |
---|
68 | uint64_t leftInFile; |
---|
69 | const tr_file * file = &tor->info.files[fileIndex]; |
---|
70 | |
---|
71 | /* if we're starting a new piece... */ |
---|
72 | if (piecePos == 0) |
---|
73 | hadPiece = tr_cpPieceIsComplete (&tor->completion, pieceIndex); |
---|
74 | |
---|
75 | /* if we're starting a new file... */ |
---|
76 | if (!filePos && (fd<0) && (fileIndex!=prevFileIndex)) |
---|
77 | { |
---|
78 | char * filename = tr_torrentFindFile (tor, fileIndex); |
---|
79 | fd = filename == NULL ? -1 : tr_open_file_for_scanning (filename); |
---|
80 | tr_free (filename); |
---|
81 | prevFileIndex = fileIndex; |
---|
82 | } |
---|
83 | |
---|
84 | /* figure out how much we can read this pass */ |
---|
85 | leftInPiece = tr_torPieceCountBytes (tor, pieceIndex) - piecePos; |
---|
86 | leftInFile = file->length - filePos; |
---|
87 | bytesThisPass = MIN (leftInFile, leftInPiece); |
---|
88 | bytesThisPass = MIN (bytesThisPass, buflen); |
---|
89 | |
---|
90 | /* read a bit */ |
---|
91 | if (fd >= 0) |
---|
92 | { |
---|
93 | const ssize_t numRead = tr_pread (fd, buffer, bytesThisPass, filePos); |
---|
94 | if (numRead > 0) |
---|
95 | { |
---|
96 | bytesThisPass = (uint32_t)numRead; |
---|
97 | SHA1_Update (&sha, buffer, bytesThisPass); |
---|
98 | #if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED |
---|
99 | posix_fadvise (fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED); |
---|
100 | #endif |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /* move our offsets */ |
---|
105 | leftInPiece -= bytesThisPass; |
---|
106 | leftInFile -= bytesThisPass; |
---|
107 | piecePos += bytesThisPass; |
---|
108 | filePos += bytesThisPass; |
---|
109 | |
---|
110 | /* if we're finishing a piece... */ |
---|
111 | if (leftInPiece == 0) |
---|
112 | { |
---|
113 | time_t now; |
---|
114 | bool hasPiece; |
---|
115 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
116 | |
---|
117 | SHA1_Final (hash, &sha); |
---|
118 | hasPiece = !memcmp (hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH); |
---|
119 | |
---|
120 | if (hasPiece || hadPiece) |
---|
121 | { |
---|
122 | tr_torrentSetHasPiece (tor, pieceIndex, hasPiece); |
---|
123 | changed |= hasPiece != hadPiece; |
---|
124 | } |
---|
125 | |
---|
126 | tr_torrentSetPieceChecked (tor, pieceIndex); |
---|
127 | now = tr_time (); |
---|
128 | tor->anyDate = now; |
---|
129 | |
---|
130 | /* sleeping even just a few msec per second goes a long |
---|
131 | * way towards reducing IO load... */ |
---|
132 | if (lastSleptAt != now) |
---|
133 | { |
---|
134 | lastSleptAt = now; |
---|
135 | tr_wait_msec (MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY); |
---|
136 | } |
---|
137 | |
---|
138 | SHA1_Init (&sha); |
---|
139 | pieceIndex++; |
---|
140 | piecePos = 0; |
---|
141 | } |
---|
142 | |
---|
143 | /* if we're finishing a file... */ |
---|
144 | if (leftInFile == 0) |
---|
145 | { |
---|
146 | if (fd >= 0) |
---|
147 | { |
---|
148 | tr_close_file (fd); |
---|
149 | fd = -1; |
---|
150 | } |
---|
151 | fileIndex++; |
---|
152 | filePos = 0; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | /* cleanup */ |
---|
157 | if (fd >= 0) |
---|
158 | tr_close_file (fd); |
---|
159 | free (buffer); |
---|
160 | |
---|
161 | /* stopwatch */ |
---|
162 | end = tr_time (); |
---|
163 | tr_logAddTorDbg (tor, "Verification is done. It took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)", |
---|
164 | (int)(end-begin), tor->info.totalSize, |
---|
165 | (uint64_t)(tor->info.totalSize/ (1+ (end-begin)))); |
---|
166 | |
---|
167 | return changed; |
---|
168 | } |
---|
169 | |
---|
170 | /*** |
---|
171 | **** |
---|
172 | ***/ |
---|
173 | |
---|
174 | struct verify_node |
---|
175 | { |
---|
176 | tr_torrent * torrent; |
---|
177 | tr_verify_done_cb verify_done_cb; |
---|
178 | uint64_t current_size; |
---|
179 | }; |
---|
180 | |
---|
181 | static void |
---|
182 | fireCheckDone (tr_torrent * tor, tr_verify_done_cb verify_done_cb) |
---|
183 | { |
---|
184 | assert (tr_isTorrent (tor)); |
---|
185 | |
---|
186 | if (verify_done_cb) |
---|
187 | verify_done_cb (tor); |
---|
188 | } |
---|
189 | |
---|
190 | static struct verify_node currentNode; |
---|
191 | static tr_list * verifyList = NULL; |
---|
192 | static tr_thread * verifyThread = NULL; |
---|
193 | static bool stopCurrent = false; |
---|
194 | |
---|
195 | static tr_lock* |
---|
196 | getVerifyLock (void) |
---|
197 | { |
---|
198 | static tr_lock * lock = NULL; |
---|
199 | |
---|
200 | if (lock == NULL) |
---|
201 | lock = tr_lockNew (); |
---|
202 | |
---|
203 | return lock; |
---|
204 | } |
---|
205 | |
---|
206 | static void |
---|
207 | verifyThreadFunc (void * unused UNUSED) |
---|
208 | { |
---|
209 | for (;;) |
---|
210 | { |
---|
211 | int changed = 0; |
---|
212 | tr_torrent * tor; |
---|
213 | struct verify_node * node; |
---|
214 | |
---|
215 | tr_lockLock (getVerifyLock ()); |
---|
216 | stopCurrent = false; |
---|
217 | node = (struct verify_node*) verifyList ? verifyList->data : NULL; |
---|
218 | if (node == NULL) |
---|
219 | { |
---|
220 | currentNode.torrent = NULL; |
---|
221 | break; |
---|
222 | } |
---|
223 | |
---|
224 | currentNode = *node; |
---|
225 | tor = currentNode.torrent; |
---|
226 | tr_list_remove_data (&verifyList, node); |
---|
227 | tr_free (node); |
---|
228 | tr_lockUnlock (getVerifyLock ()); |
---|
229 | |
---|
230 | tr_logAddTorInfo (tor, "%s", _("Verifying torrent")); |
---|
231 | tr_torrentSetVerifyState (tor, TR_VERIFY_NOW); |
---|
232 | changed = verifyTorrent (tor, &stopCurrent); |
---|
233 | tr_torrentSetVerifyState (tor, TR_VERIFY_NONE); |
---|
234 | assert (tr_isTorrent (tor)); |
---|
235 | |
---|
236 | if (!stopCurrent) |
---|
237 | { |
---|
238 | if (changed) |
---|
239 | tr_torrentSetDirty (tor); |
---|
240 | fireCheckDone (tor, currentNode.verify_done_cb); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | verifyThread = NULL; |
---|
245 | tr_lockUnlock (getVerifyLock ()); |
---|
246 | } |
---|
247 | |
---|
248 | static int |
---|
249 | compareVerifyByPriorityAndSize (const void * va, const void * vb) |
---|
250 | { |
---|
251 | const struct verify_node * a = va; |
---|
252 | const struct verify_node * b = vb; |
---|
253 | |
---|
254 | /* higher priority comes before lower priority */ |
---|
255 | const tr_priority_t pa = tr_torrentGetPriority (a->torrent); |
---|
256 | const tr_priority_t pb = tr_torrentGetPriority (b->torrent); |
---|
257 | if (pa != pb) |
---|
258 | return pa > pb ? -1 : 1; |
---|
259 | |
---|
260 | /* smaller torrents come before larger ones because they verify faster */ |
---|
261 | if (a->current_size < b->current_size) |
---|
262 | return -1; |
---|
263 | if (a->current_size > b->current_size) |
---|
264 | return 1; |
---|
265 | return 0; |
---|
266 | } |
---|
267 | |
---|
268 | void |
---|
269 | tr_verifyAdd (tr_torrent * tor, tr_verify_done_cb verify_done_cb) |
---|
270 | { |
---|
271 | struct verify_node * node; |
---|
272 | |
---|
273 | assert (tr_isTorrent (tor)); |
---|
274 | tr_logAddTorInfo (tor, "%s", _("Queued for verification")); |
---|
275 | |
---|
276 | node = tr_new (struct verify_node, 1); |
---|
277 | node->torrent = tor; |
---|
278 | node->verify_done_cb = verify_done_cb; |
---|
279 | node->current_size = tr_torrentGetCurrentSizeOnDisk (tor); |
---|
280 | |
---|
281 | tr_lockLock (getVerifyLock ()); |
---|
282 | tr_torrentSetVerifyState (tor, TR_VERIFY_WAIT); |
---|
283 | tr_list_insert_sorted (&verifyList, node, compareVerifyByPriorityAndSize); |
---|
284 | if (verifyThread == NULL) |
---|
285 | verifyThread = tr_threadNew (verifyThreadFunc, NULL); |
---|
286 | tr_lockUnlock (getVerifyLock ()); |
---|
287 | } |
---|
288 | |
---|
289 | static int |
---|
290 | compareVerifyByTorrent (const void * va, const void * vb) |
---|
291 | { |
---|
292 | const struct verify_node * a = va; |
---|
293 | const tr_torrent * b = vb; |
---|
294 | return a->torrent - b; |
---|
295 | } |
---|
296 | |
---|
297 | void |
---|
298 | tr_verifyRemove (tr_torrent * tor) |
---|
299 | { |
---|
300 | tr_lock * lock = getVerifyLock (); |
---|
301 | tr_lockLock (lock); |
---|
302 | |
---|
303 | assert (tr_isTorrent (tor)); |
---|
304 | |
---|
305 | if (tor == currentNode.torrent) |
---|
306 | { |
---|
307 | stopCurrent = true; |
---|
308 | |
---|
309 | while (stopCurrent) |
---|
310 | { |
---|
311 | tr_lockUnlock (lock); |
---|
312 | tr_wait_msec (100); |
---|
313 | tr_lockLock (lock); |
---|
314 | } |
---|
315 | } |
---|
316 | else |
---|
317 | { |
---|
318 | tr_free (tr_list_remove (&verifyList, tor, compareVerifyByTorrent)); |
---|
319 | tr_torrentSetVerifyState (tor, TR_VERIFY_NONE); |
---|
320 | } |
---|
321 | |
---|
322 | tr_lockUnlock (lock); |
---|
323 | } |
---|
324 | |
---|
325 | void |
---|
326 | tr_verifyClose (tr_session * session UNUSED) |
---|
327 | { |
---|
328 | tr_lockLock (getVerifyLock ()); |
---|
329 | |
---|
330 | stopCurrent = true; |
---|
331 | tr_list_free (&verifyList, tr_free); |
---|
332 | |
---|
333 | tr_lockUnlock (getVerifyLock ()); |
---|
334 | } |
---|
335 | |
---|