1 | /* |
---|
2 | * This file Copyright (C) 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:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <unistd.h> /* unlink */ |
---|
14 | |
---|
15 | #include <string.h> |
---|
16 | |
---|
17 | #include "transmission.h" |
---|
18 | #include "bencode.h" |
---|
19 | #include "completion.h" |
---|
20 | #include "fastresume.h" |
---|
21 | #include "peer-mgr.h" /* pex */ |
---|
22 | #include "platform.h" /* tr_getResumeDir */ |
---|
23 | #include "resume.h" |
---|
24 | #include "torrent.h" |
---|
25 | #include "utils.h" /* tr_buildPath */ |
---|
26 | |
---|
27 | #define KEY_CORRUPT "corrupt" |
---|
28 | #define KEY_DESTINATION "destination" |
---|
29 | #define KEY_DND "dnd" |
---|
30 | #define KEY_DOWNLOADED "downloaded" |
---|
31 | #define KEY_MAX_PEERS "max-peers" |
---|
32 | #define KEY_PAUSED "paused" |
---|
33 | #define KEY_PEERS "peers" |
---|
34 | #define KEY_PRIORITY "priority" |
---|
35 | #define KEY_PROGRESS "progress" |
---|
36 | #define KEY_SPEEDLIMIT "speed-limit" |
---|
37 | #define KEY_UPLOADED "uploaded" |
---|
38 | |
---|
39 | #define KEY_SPEEDLIMIT_DOWN_SPEED "down-speed" |
---|
40 | #define KEY_SPEEDLIMIT_DOWN_MODE "down-mode" |
---|
41 | #define KEY_SPEEDLIMIT_UP_SPEED "up-speed" |
---|
42 | #define KEY_SPEEDLIMIT_UP_MODE "up-mode" |
---|
43 | |
---|
44 | #define KEY_PROGRESS_MTIMES "mtimes" |
---|
45 | #define KEY_PROGRESS_BITFIELD "bitfield" |
---|
46 | |
---|
47 | static void |
---|
48 | getResumeFilename( char * buf, size_t buflen, const tr_torrent * tor ) |
---|
49 | { |
---|
50 | const char * dir = tr_getResumeDir( tor->handle ); |
---|
51 | char base[4096]; |
---|
52 | snprintf( base, sizeof( base ), "%s.%16.16s.resume", |
---|
53 | tor->info.name, |
---|
54 | tor->info.hashString ); |
---|
55 | tr_buildPath( buf, buflen, dir, base, NULL ); |
---|
56 | } |
---|
57 | |
---|
58 | /*** |
---|
59 | **** |
---|
60 | ***/ |
---|
61 | |
---|
62 | static void |
---|
63 | savePeers( tr_benc * dict, const tr_torrent * tor ) |
---|
64 | { |
---|
65 | tr_pex * pex = NULL; |
---|
66 | const int count = tr_peerMgrGetPeers( tor->handle->peerMgr, |
---|
67 | tor->info.hash, &pex ); |
---|
68 | if( count > 0 ) |
---|
69 | tr_bencInitRaw( tr_bencDictAdd( dict, KEY_PEERS ), pex, sizeof(tr_pex)*count ); |
---|
70 | tr_free( pex ); |
---|
71 | } |
---|
72 | |
---|
73 | static uint64_t |
---|
74 | loadPeers( tr_benc * dict, tr_torrent * tor ) |
---|
75 | { |
---|
76 | uint64_t ret = 0; |
---|
77 | tr_benc * p; |
---|
78 | |
---|
79 | if(( p = tr_bencDictFindType( dict, KEY_PEERS, TYPE_STR ))) |
---|
80 | { |
---|
81 | int i; |
---|
82 | const char * str = p->val.s.s; |
---|
83 | const size_t len = p->val.s.i; |
---|
84 | const int count = len / sizeof( tr_pex ); |
---|
85 | for( i=0; i<count; ++i ) { |
---|
86 | tr_pex pex; |
---|
87 | memcpy( &pex, str + (i*sizeof(tr_pex)), sizeof(tr_pex) ); |
---|
88 | tr_peerMgrAddPex( tor->handle->peerMgr, |
---|
89 | tor->info.hash, TR_PEER_FROM_CACHE, &pex ); |
---|
90 | } |
---|
91 | tr_tordbg( tor, "Loaded %d peers from resume file", count ); |
---|
92 | ret = TR_FR_PEERS; |
---|
93 | } |
---|
94 | |
---|
95 | return ret; |
---|
96 | } |
---|
97 | |
---|
98 | /*** |
---|
99 | **** |
---|
100 | ***/ |
---|
101 | |
---|
102 | static void |
---|
103 | saveDND( tr_benc * dict, const tr_torrent * tor ) |
---|
104 | { |
---|
105 | const tr_info * inf = &tor->info; |
---|
106 | const tr_file_index_t n = inf->fileCount; |
---|
107 | tr_file_index_t i; |
---|
108 | tr_benc * list; |
---|
109 | |
---|
110 | list = tr_bencDictAddList( dict, KEY_DND, n ); |
---|
111 | for( i=0; i<n; ++i ) |
---|
112 | tr_bencInitInt( tr_bencListAdd( list ), inf->files[i].dnd ? 1 : 0 ); |
---|
113 | } |
---|
114 | |
---|
115 | static uint64_t |
---|
116 | loadDND( tr_benc * dict, tr_torrent * tor ) |
---|
117 | { |
---|
118 | uint64_t ret = 0; |
---|
119 | tr_info * inf = &tor->info; |
---|
120 | const tr_file_index_t n = inf->fileCount; |
---|
121 | tr_benc * list = NULL; |
---|
122 | |
---|
123 | if( tr_bencDictFindList( dict, KEY_DND, &list ) |
---|
124 | && ( list->val.l.count == (int)n ) ) |
---|
125 | { |
---|
126 | int64_t tmp; |
---|
127 | tr_file_index_t * dl = tr_new( tr_file_index_t, n ); |
---|
128 | tr_file_index_t * dnd = tr_new( tr_file_index_t, n ); |
---|
129 | tr_file_index_t i, dlCount=0, dndCount=0; |
---|
130 | |
---|
131 | for( i=0; i<n; ++i ) { |
---|
132 | if( tr_bencGetInt( &list->val.l.vals[i], &tmp ) && tmp ) |
---|
133 | dnd[dndCount++] = i; |
---|
134 | else |
---|
135 | dl[dlCount++] = i; |
---|
136 | } |
---|
137 | |
---|
138 | if( dndCount ) { |
---|
139 | tr_torrentInitFileDLs ( tor, dnd, dndCount, FALSE ); |
---|
140 | tr_tordbg( tor, "Resume file found %d files listed as dnd", dndCount ); |
---|
141 | } |
---|
142 | if( dlCount ) { |
---|
143 | tr_torrentInitFileDLs ( tor, dl, dlCount, TRUE ); |
---|
144 | tr_tordbg( tor, "Resume file found %d files marked for download", dlCount ); |
---|
145 | } |
---|
146 | |
---|
147 | tr_free( dnd ); |
---|
148 | tr_free( dl ); |
---|
149 | ret = TR_FR_PRIORITY; |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | tr_tordbg( tor, "Couldn't load DND flags. dnd list (%p) has %d children; torrent has %d files", |
---|
154 | list, ( list ? list->val.l.count : -1 ), (int)n ); |
---|
155 | } |
---|
156 | |
---|
157 | return ret; |
---|
158 | } |
---|
159 | |
---|
160 | /*** |
---|
161 | **** |
---|
162 | ***/ |
---|
163 | |
---|
164 | static void |
---|
165 | savePriorities( tr_benc * dict, const tr_torrent * tor ) |
---|
166 | { |
---|
167 | const tr_info * inf = &tor->info; |
---|
168 | const tr_file_index_t n = inf->fileCount; |
---|
169 | tr_file_index_t i; |
---|
170 | tr_benc * list; |
---|
171 | |
---|
172 | list = tr_bencDictAddList( dict, KEY_PRIORITY, n ); |
---|
173 | for( i=0; i<n; ++i ) |
---|
174 | tr_bencInitInt( tr_bencListAdd( list ), inf->files[i].priority ); |
---|
175 | } |
---|
176 | |
---|
177 | static uint64_t |
---|
178 | loadPriorities( tr_benc * dict, tr_torrent * tor ) |
---|
179 | { |
---|
180 | uint64_t ret = 0; |
---|
181 | tr_info * inf = &tor->info; |
---|
182 | const tr_file_index_t n = inf->fileCount; |
---|
183 | tr_benc * list; |
---|
184 | |
---|
185 | if( tr_bencDictFindList( dict, KEY_PRIORITY, &list ) |
---|
186 | && ( list->val.l.count == (int)n ) ) |
---|
187 | { |
---|
188 | int64_t tmp; |
---|
189 | tr_file_index_t i; |
---|
190 | for( i=0; i<n; ++i ) |
---|
191 | if( tr_bencGetInt( &list->val.l.vals[i], &tmp ) ) |
---|
192 | inf->files[i].priority = tmp; |
---|
193 | ret = TR_FR_PRIORITY; |
---|
194 | } |
---|
195 | |
---|
196 | return ret; |
---|
197 | } |
---|
198 | |
---|
199 | /*** |
---|
200 | **** |
---|
201 | ***/ |
---|
202 | |
---|
203 | static void |
---|
204 | saveSpeedLimits( tr_benc * dict, const tr_torrent * tor ) |
---|
205 | { |
---|
206 | tr_benc * d = tr_bencDictAddDict( dict, KEY_SPEEDLIMIT, 4 ); |
---|
207 | tr_bencDictAddInt( d, KEY_SPEEDLIMIT_DOWN_SPEED, |
---|
208 | tr_torrentGetSpeedLimit( tor, TR_DOWN ) ); |
---|
209 | tr_bencDictAddInt( d, KEY_SPEEDLIMIT_DOWN_MODE, |
---|
210 | tr_torrentGetSpeedMode( tor, TR_DOWN ) ); |
---|
211 | tr_bencDictAddInt( d, KEY_SPEEDLIMIT_UP_SPEED, |
---|
212 | tr_torrentGetSpeedLimit( tor, TR_UP ) ); |
---|
213 | tr_bencDictAddInt( d, KEY_SPEEDLIMIT_UP_MODE, |
---|
214 | tr_torrentGetSpeedMode( tor, TR_UP ) ); |
---|
215 | } |
---|
216 | |
---|
217 | static uint64_t |
---|
218 | loadSpeedLimits( tr_benc * dict, tr_torrent * tor ) |
---|
219 | { |
---|
220 | uint64_t ret = 0; |
---|
221 | tr_benc * d; |
---|
222 | |
---|
223 | if( tr_bencDictFindDict( dict, KEY_SPEEDLIMIT, &d ) ) |
---|
224 | { |
---|
225 | int64_t i; |
---|
226 | if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_DOWN_SPEED, &i ) ) |
---|
227 | tr_torrentSetSpeedLimit( tor, TR_DOWN, i ); |
---|
228 | if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_DOWN_MODE, &i ) ) |
---|
229 | tr_torrentSetSpeedMode( tor, TR_DOWN, i ); |
---|
230 | if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_UP_SPEED, &i ) ) |
---|
231 | tr_torrentSetSpeedLimit( tor, TR_UP, i ); |
---|
232 | if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_UP_MODE, &i ) ) |
---|
233 | tr_torrentSetSpeedMode( tor, TR_UP, i ); |
---|
234 | ret = TR_FR_SPEEDLIMIT; |
---|
235 | } |
---|
236 | |
---|
237 | return ret; |
---|
238 | } |
---|
239 | |
---|
240 | /*** |
---|
241 | **** |
---|
242 | ***/ |
---|
243 | |
---|
244 | static void |
---|
245 | saveProgress( tr_benc * dict, const tr_torrent * tor ) |
---|
246 | { |
---|
247 | int i; |
---|
248 | int n; |
---|
249 | time_t * mtimes; |
---|
250 | tr_benc * p; |
---|
251 | tr_benc * m; |
---|
252 | tr_benc * b; |
---|
253 | const tr_bitfield * bitfield; |
---|
254 | |
---|
255 | p = tr_bencDictAdd( dict, KEY_PROGRESS ); |
---|
256 | tr_bencInitDict( p, 2 ); |
---|
257 | |
---|
258 | /* add the mtimes */ |
---|
259 | mtimes = tr_torrentGetMTimes( tor, &n ); |
---|
260 | m = tr_bencDictAddList( p, KEY_PROGRESS_MTIMES, n ); |
---|
261 | for( i=0; i<n; ++i ) { |
---|
262 | if( !tr_torrentIsFileChecked( tor, i ) ) |
---|
263 | mtimes[i] = ~(time_t)0; /* force a recheck */ |
---|
264 | tr_bencListAddInt( m, mtimes[i] ); |
---|
265 | } |
---|
266 | |
---|
267 | /* add the bitfield */ |
---|
268 | bitfield = tr_cpBlockBitfield( tor->completion ); |
---|
269 | b = tr_bencDictAdd( p, KEY_PROGRESS_BITFIELD ); |
---|
270 | tr_bencInitRaw( b, bitfield->bits, bitfield->len ); |
---|
271 | |
---|
272 | /* cleanup */ |
---|
273 | tr_free( mtimes ); |
---|
274 | } |
---|
275 | |
---|
276 | static uint64_t |
---|
277 | loadProgress( tr_benc * dict, tr_torrent * tor ) |
---|
278 | { |
---|
279 | uint64_t ret = 0; |
---|
280 | tr_benc * p; |
---|
281 | |
---|
282 | if( tr_bencDictFindDict( dict, KEY_PROGRESS, &p ) ) |
---|
283 | { |
---|
284 | tr_benc * m; |
---|
285 | tr_benc * b; |
---|
286 | int n; |
---|
287 | time_t * curMTimes = tr_torrentGetMTimes( tor, &n ); |
---|
288 | |
---|
289 | if( tr_bencDictFindList( p, KEY_PROGRESS_MTIMES, &m ) |
---|
290 | && ( m->val.l.count == (int64_t)tor->info.fileCount ) |
---|
291 | && ( m->val.l.count == n ) ) |
---|
292 | { |
---|
293 | int i; |
---|
294 | for( i=0; i<n; ++i ) |
---|
295 | { |
---|
296 | int64_t tmp; |
---|
297 | if( !tr_bencGetInt( &m->val.l.vals[i], &tmp ) ) { |
---|
298 | tr_tordbg( tor, "File #%d needs to be verified - couldn't find benc entry", i ); |
---|
299 | tr_torrentSetFileChecked( tor, i, FALSE ); |
---|
300 | } else { |
---|
301 | const time_t t = (time_t) tmp; |
---|
302 | if( t == curMTimes[i] ) |
---|
303 | tr_torrentSetFileChecked( tor, i, TRUE ); |
---|
304 | else { |
---|
305 | tr_tordbg( tor, "File #%d needs to be verified - times %lu and %lu don't match", i, t, curMTimes[i] ); |
---|
306 | tr_torrentSetFileChecked( tor, i, FALSE ); |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | } |
---|
311 | else |
---|
312 | { |
---|
313 | tr_torrentUncheck( tor ); |
---|
314 | tr_tordbg( tor, "Torrent needs to be verified - unable to find mtimes" ); |
---|
315 | } |
---|
316 | |
---|
317 | if(( b = tr_bencDictFindType( p, KEY_PROGRESS_BITFIELD, TYPE_STR ))) |
---|
318 | { |
---|
319 | tr_bitfield tmp; |
---|
320 | tmp.len = b->val.s.i; |
---|
321 | tmp.bits = (uint8_t*) b->val.s.s; |
---|
322 | if( tr_cpBlockBitfieldSet( tor->completion, &tmp ) ) { |
---|
323 | tr_torrentUncheck( tor ); |
---|
324 | tr_tordbg( tor, "Torrent needs to be verified - error loading bitfield" ); |
---|
325 | } |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | tr_torrentUncheck( tor ); |
---|
330 | tr_tordbg( tor, "Torrent needs to be verified - unable to find bitfield" ); |
---|
331 | } |
---|
332 | |
---|
333 | tr_free( curMTimes ); |
---|
334 | ret = TR_FR_PROGRESS; |
---|
335 | } |
---|
336 | |
---|
337 | return ret; |
---|
338 | } |
---|
339 | |
---|
340 | /*** |
---|
341 | **** |
---|
342 | ***/ |
---|
343 | |
---|
344 | void |
---|
345 | tr_torrentSaveResume( const tr_torrent * tor ) |
---|
346 | { |
---|
347 | tr_benc top; |
---|
348 | char filename[MAX_PATH_LENGTH]; |
---|
349 | |
---|
350 | tr_bencInitDict( &top, 10 ); |
---|
351 | tr_bencDictAddInt( &top, KEY_CORRUPT, |
---|
352 | tor->corruptPrev + tor->corruptCur ); |
---|
353 | tr_bencDictAddStr( &top, KEY_DESTINATION, |
---|
354 | tor->destination ); |
---|
355 | tr_bencDictAddInt( &top, KEY_DOWNLOADED, |
---|
356 | tor->downloadedPrev + tor->downloadedCur ); |
---|
357 | tr_bencDictAddInt( &top, KEY_UPLOADED, |
---|
358 | tor->uploadedPrev + tor->uploadedCur ); |
---|
359 | tr_bencDictAddInt( &top, KEY_MAX_PEERS, |
---|
360 | tor->maxConnectedPeers ); |
---|
361 | tr_bencDictAddInt( &top, KEY_PAUSED, |
---|
362 | tor->isRunning ? 0 : 1 ); |
---|
363 | savePeers( &top, tor ); |
---|
364 | savePriorities( &top, tor ); |
---|
365 | saveDND( &top, tor ); |
---|
366 | saveProgress( &top, tor ); |
---|
367 | saveSpeedLimits( &top, tor ); |
---|
368 | |
---|
369 | getResumeFilename( filename, sizeof( filename ), tor ); |
---|
370 | tr_bencSaveFile( filename, &top ); |
---|
371 | |
---|
372 | tr_bencFree( &top ); |
---|
373 | } |
---|
374 | |
---|
375 | uint64_t |
---|
376 | tr_torrentLoadResume( tr_torrent * tor, |
---|
377 | uint64_t fieldsToLoad, |
---|
378 | const tr_ctor * ctor ) |
---|
379 | { |
---|
380 | int64_t i; |
---|
381 | const char * str; |
---|
382 | uint64_t fieldsLoaded = 0; |
---|
383 | char filename[MAX_PATH_LENGTH]; |
---|
384 | tr_benc top; |
---|
385 | |
---|
386 | getResumeFilename( filename, sizeof( filename ), tor ); |
---|
387 | |
---|
388 | if( tr_bencLoadFile( filename, &top ) ) |
---|
389 | { |
---|
390 | tr_tordbg( tor, "Couldn't read \"%s\"; trying old format.", filename ); |
---|
391 | fieldsLoaded = tr_fastResumeLoad( tor, fieldsToLoad, ctor ); |
---|
392 | |
---|
393 | if( ( fieldsLoaded != 0 ) && ( fieldsToLoad == ~(uint64_t)0 ) ) |
---|
394 | { |
---|
395 | tr_torrentSaveResume( tor ); |
---|
396 | tr_fastResumeRemove( tor ); |
---|
397 | tr_tordbg( tor, "Migrated resume file to \"%s\"", filename ); |
---|
398 | } |
---|
399 | |
---|
400 | return fieldsLoaded; |
---|
401 | } |
---|
402 | |
---|
403 | tr_tordbg( tor, "Read resume file \"%s\"", filename ); |
---|
404 | |
---|
405 | if( ( fieldsToLoad & TR_FR_CORRUPT ) |
---|
406 | && tr_bencDictFindInt( &top, KEY_CORRUPT, &i ) ) { |
---|
407 | tor->corruptPrev = i; |
---|
408 | fieldsLoaded |= TR_FR_CORRUPT; |
---|
409 | } |
---|
410 | |
---|
411 | if( ( fieldsToLoad & ( TR_FR_PROGRESS | TR_FR_DESTINATION ) ) |
---|
412 | && tr_bencDictFindStr( &top, KEY_DESTINATION, &str ) ) { |
---|
413 | tr_free( tor->destination ); |
---|
414 | tor->destination = tr_strdup( str ); |
---|
415 | fieldsLoaded |= TR_FR_DESTINATION; |
---|
416 | } |
---|
417 | |
---|
418 | if( ( fieldsToLoad & TR_FR_DOWNLOADED ) |
---|
419 | && tr_bencDictFindInt( &top, KEY_DOWNLOADED, &i ) ) { |
---|
420 | tor->downloadedPrev = i; |
---|
421 | fieldsLoaded |= TR_FR_DOWNLOADED; |
---|
422 | } |
---|
423 | |
---|
424 | if( ( fieldsToLoad & TR_FR_UPLOADED ) |
---|
425 | && tr_bencDictFindInt( &top, KEY_UPLOADED, &i ) ) { |
---|
426 | tor->uploadedPrev = i; |
---|
427 | fieldsLoaded |= TR_FR_UPLOADED; |
---|
428 | } |
---|
429 | |
---|
430 | if( ( fieldsToLoad & TR_FR_MAX_PEERS ) |
---|
431 | && tr_bencDictFindInt( &top, KEY_MAX_PEERS, &i ) ) { |
---|
432 | tor->maxConnectedPeers = i; |
---|
433 | fieldsLoaded |= TR_FR_MAX_PEERS; |
---|
434 | } |
---|
435 | |
---|
436 | if( ( fieldsToLoad & TR_FR_RUN ) |
---|
437 | && tr_bencDictFindInt( &top, KEY_PAUSED, &i ) ) { |
---|
438 | tor->isRunning = i ? 0 : 1; |
---|
439 | fieldsLoaded |= TR_FR_RUN; |
---|
440 | } |
---|
441 | |
---|
442 | if( fieldsToLoad & TR_FR_PEERS ) |
---|
443 | fieldsLoaded |= loadPeers( &top, tor ); |
---|
444 | |
---|
445 | if( fieldsToLoad & TR_FR_PRIORITY ) |
---|
446 | fieldsLoaded |= loadPriorities( &top, tor ); |
---|
447 | |
---|
448 | if( fieldsToLoad & TR_FR_PROGRESS ) |
---|
449 | fieldsLoaded |= loadProgress( &top, tor ); |
---|
450 | |
---|
451 | if( fieldsToLoad & TR_FR_DND ) |
---|
452 | fieldsLoaded |= loadDND( &top, tor ); |
---|
453 | |
---|
454 | if( fieldsToLoad & TR_FR_SPEEDLIMIT ) |
---|
455 | fieldsLoaded |= loadSpeedLimits( &top, tor ); |
---|
456 | |
---|
457 | tr_bencFree( &top ); |
---|
458 | return fieldsLoaded; |
---|
459 | } |
---|
460 | |
---|
461 | void |
---|
462 | tr_torrentRemoveResume( const tr_torrent * tor ) |
---|
463 | { |
---|
464 | char filename[MAX_PATH_LENGTH]; |
---|
465 | getResumeFilename( filename, sizeof( filename ), tor ); |
---|
466 | unlink( filename ); |
---|
467 | tr_fastResumeRemove( tor ); |
---|
468 | } |
---|