source: trunk/libtransmission/fastresume.c @ 5221

Last change on this file since 5221 was 5221, checked in by charles, 14 years ago

lots more i18n string work -- making strings more consistent, folding redundant strings together, etc.

  • Property svn:keywords set to Date Rev Author Id
File size: 21.0 KB
Line 
1/******************************************************************************
2 * $Id: fastresume.c 5221 2008-03-07 20:48:36Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25/***********************************************************************
26 * Fast resume
27 ***********************************************************************
28 * The format of the resume file is a 4 byte format version (currently 1),
29 * followed by several variable-sized blocks of data.  Each block is
30 * preceded by a 1 byte ID and a 4 byte length.  The currently recognized
31 * IDs are defined below by the FR_ID_* macros.  The length does not include
32 * the 5 bytes for the ID and length.
33 *
34 * The name of the resume file is "resume.<hash>-<tag>", although
35 * older files with a name of "resume.<hash>" will be recognized if
36 * the former doesn't exist.
37 *
38 * All values are stored in the native endianness. Moving a
39 * libtransmission resume file from an architecture to another will not
40 * work, although it will not hurt either (the version will be wrong,
41 * so the resume file will not be read).
42 **********************************************************************/
43
44#include <assert.h>
45#include <errno.h>
46#include <stdio.h>
47#include <stdlib.h> /* calloc */
48#include <string.h> /* strcpy, memset, memcmp */
49
50#include <sys/types.h>
51#include <sys/stat.h> /* stat */
52#include <unistd.h> /* unlink */
53
54#include "transmission.h"
55#include "completion.h"
56#include "fastresume.h"
57#include "peer-mgr.h"
58#include "platform.h"
59#include "torrent.h"
60#include "utils.h"
61
62/* time_t can be 32 or 64 bits... for consistency we'll hardwire 64 */ 
63typedef uint64_t tr_time_t; 
64
65enum
66{
67    /* number of bytes downloaded */
68    FR_ID_DOWNLOADED = 2,
69
70    /* number of bytes uploaded */
71    FR_ID_UPLOADED = 3,
72
73    /* progress data:
74     *  - 4 bytes * number of files: mtimes of files
75     *  - 1 bit * number of blocks: whether we have the block or not */
76    FR_ID_PROGRESS = 5,
77
78    /* dnd and priority
79     * char * number of files: l,n,h for low, normal, high priority
80     * char * number of files: t,f for DND flags */
81    FR_ID_PRIORITY = 6,
82
83    /* transfer speeds
84     * uint32_t: dl speed rate to use when the mode is single
85     * uint32_t: dl's tr_speedlimit
86     * uint32_t: ul speed rate to use when the mode is single
87     * uint32_t: ul's tr_speedlimit
88     */
89    FR_ID_SPEED = 8,
90
91    /* active
92     * char: 't' if running, 'f' if paused
93     */
94    FR_ID_RUN = 9,
95
96    /* number of corrupt bytes downloaded */
97    FR_ID_CORRUPT = 10,
98
99    /* IPs and ports of connectable peers */
100    FR_ID_PEERS = 11,
101
102    /* destination of the torrent: zero-terminated string */
103    FR_ID_DESTINATION = 12,
104
105    /* pex flag
106     * 't' if pex is enabled, 'f' if disabled */
107    FR_ID_PEX = 13,
108
109    /* max connected peers -- uint16_t */
110    FR_ID_MAX_PEERS = 14
111};
112
113
114/* macros for the length of various pieces of the progress data */
115#define FR_MTIME_LEN( t ) \
116  ( sizeof(tr_time_t) * (t)->info.fileCount )
117#define FR_BLOCK_BITFIELD_LEN( t ) \
118  ( ( (t)->blockCount + 7 ) / 8 )
119#define FR_PROGRESS_LEN( t ) \
120  ( FR_MTIME_LEN( t ) + FR_BLOCK_BITFIELD_LEN( t ) )
121#define FR_SPEED_LEN (2 * (sizeof(uint16_t) + sizeof(uint8_t) ) )
122
123static void
124fastResumeFileName( char * buf, size_t buflen, const tr_torrent * tor, int tag )
125{
126    const char * cacheDir = tr_getCacheDirectory ();
127    const char * hash = tor->info.hashString;
128
129    if( !tag )
130    {
131        tr_buildPath( buf, buflen, cacheDir, hash, NULL );
132    }
133    else
134    {
135        char base[1024];
136        snprintf( base, sizeof(base), "%s-%s", hash, tor->handle->tag );
137        tr_buildPath( buf, buflen, cacheDir, base, NULL );
138    }
139}
140
141static tr_time_t*
142getMTimes( const tr_torrent * tor, int * setme_n )
143{
144    int i;
145    const int n = tor->info.fileCount;
146    tr_time_t * m = calloc( n, sizeof(tr_time_t) );
147
148    for( i=0; i<n; ++i ) {
149        char fname[MAX_PATH_LENGTH];
150        struct stat sb;
151        tr_buildPath( fname, sizeof(fname),
152                      tor->destination, tor->info.files[i].name, NULL );
153        if ( !stat( fname, &sb ) && S_ISREG( sb.st_mode ) ) {
154#ifdef SYS_DARWIN
155            m[i] = sb.st_mtimespec.tv_sec;
156#else
157            m[i] = sb.st_mtime;
158#endif
159        }
160    }
161
162    *setme_n = n;
163    return m;
164}
165
166static void
167fastResumeWriteData( uint8_t       id,
168                     const void  * data,
169                     uint32_t      size,
170                     uint32_t      count,
171                     FILE        * file )
172{
173    uint32_t  datalen = size * count;
174
175    fwrite( &id, 1, 1, file );
176    fwrite( &datalen, 4, 1, file );
177    fwrite( data, size, count, file );
178}
179
180void
181tr_fastResumeSave( const tr_torrent * tor )
182{
183    char      path[MAX_PATH_LENGTH];
184    FILE    * file;
185    const int version = 1;
186    uint64_t  total;
187
188    fastResumeFileName( path, sizeof path, tor, 1 );
189    file = fopen( path, "wb+" );
190    if( !file ) {
191        tr_err( _( "Couldn't open \"%s\": %s" ), path, tr_strerror( errno ) );
192        return;
193    }
194   
195    /* Write format version */
196    fwrite( &version, 4, 1, file );
197
198    if( TRUE ) /* FR_ID_DESTINATION */
199    {
200        const char * d = tor->destination ? tor->destination : "";
201        const int byteCount = strlen( d ) + 1;
202        fastResumeWriteData( FR_ID_DESTINATION, d, 1, byteCount, file );
203    }
204
205    /* Write progress data */
206    if (1) {
207        int i, n;
208        tr_time_t * mtimes;
209        uint8_t * buf = malloc( FR_PROGRESS_LEN( tor ) );
210        uint8_t * walk = buf;
211        const tr_bitfield * bitfield;
212
213        /* mtimes */
214        mtimes = getMTimes( tor, &n );
215        for( i=0; i<n; ++i )
216            if( !tr_torrentIsFileChecked( tor, i ) )
217                mtimes[i] = ~(tr_time_t)0; /* force a recheck next time */
218        memcpy( walk, mtimes, n*sizeof(tr_time_t) );
219        walk += n * sizeof(tr_time_t);
220
221        /* completion bitfield */
222        bitfield = tr_cpBlockBitfield( tor->completion );
223        assert( (unsigned)FR_BLOCK_BITFIELD_LEN( tor ) == bitfield->len );
224        memcpy( walk, bitfield->bits, bitfield->len );
225        walk += bitfield->len;
226
227        /* write it */
228        assert( walk-buf == (int)FR_PROGRESS_LEN( tor ) );
229        fastResumeWriteData( FR_ID_PROGRESS, buf, 1, walk-buf, file );
230
231        /* cleanup */
232        free( mtimes );
233        free( buf );
234    }
235
236
237    /* Write the priorities and DND flags */
238    if( TRUE )
239    {
240        int i;
241        const int n = tor->info.fileCount;
242        char * buf = tr_new0( char, n*2 );
243        char * walk = buf;
244
245        /* priorities */
246        for( i=0; i<n; ++i ) {
247            char ch;
248            const int priority = tor->info.files[i].priority;
249            switch( priority ) {
250               case TR_PRI_LOW:   ch = 'l'; break; /* low */
251               case TR_PRI_HIGH:  ch = 'h'; break; /* high */
252               default:           ch = 'n'; break; /* normal */
253            };
254            *walk++ = ch;
255        }
256
257        /* dnd flags */
258        for( i=0; i<n; ++i )
259            *walk++ = tor->info.files[i].dnd ? 't' : 'f';
260
261        /* write it */
262        assert( walk - buf == 2*n );
263        fastResumeWriteData( FR_ID_PRIORITY, buf, 1, walk-buf, file );
264
265        /* cleanup */
266        tr_free( buf );
267    }
268
269
270    /* Write the torrent ul/dl speed caps */
271    if( TRUE )
272    {
273        const int len = FR_SPEED_LEN;
274        char * buf = tr_new0( char, len );
275        char * walk = buf;
276        uint16_t i16;
277        uint8_t i8;
278
279        i16 = (uint16_t) tr_torrentGetSpeedLimit( tor, TR_DOWN );
280        memcpy( walk, &i16, 2 ); walk += 2;
281        i8 = (uint8_t) tr_torrentGetSpeedMode( tor, TR_DOWN );
282        memcpy( walk, &i8, 1 ); walk += 1;
283        i16 = (uint16_t) tr_torrentGetSpeedLimit( tor, TR_UP );
284        memcpy( walk, &i16, 2 ); walk += 2;
285        i8 = (uint8_t) tr_torrentGetSpeedMode( tor, TR_UP );
286        memcpy( walk, &i8, 1 ); walk += 1;
287
288        assert( walk - buf == len );
289        fastResumeWriteData( FR_ID_SPEED, buf, 1, walk-buf, file );
290        tr_free( buf );
291    }
292
293    if( TRUE ) /* FR_ID_RUN */
294    {
295        const char is_running = tor->isRunning ? 't' : 'f';
296        fastResumeWriteData( FR_ID_RUN, &is_running, 1, 1, file );
297    }
298
299    /* Write download and upload totals */
300
301    total = tor->downloadedCur + tor->downloadedPrev;
302    fastResumeWriteData( FR_ID_DOWNLOADED, &total, 8, 1, file );
303
304    total = tor->uploadedCur + tor->uploadedPrev;
305    fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file );
306
307    total = tor->corruptCur + tor->corruptPrev;
308    fastResumeWriteData( FR_ID_CORRUPT, &total, 8, 1, file );
309
310    fastResumeWriteData( FR_ID_MAX_PEERS,
311                         &tor->maxConnectedPeers,
312                         sizeof(uint16_t), 1, file );
313
314    if( !tor->info.isPrivate )
315    {
316        tr_pex * pex;
317        const int count = tr_peerMgrGetPeers( tor->handle->peerMgr,
318                                              tor->info.hash,
319                                              &pex );
320        if( count > 0 )
321            fastResumeWriteData( FR_ID_PEERS, pex, sizeof(tr_pex), count, file );
322        tr_free( pex );
323    }
324
325    fclose( file );
326
327    /*tr_dbg( "Wrote resume file for \"%s\"", tor->info.name );*/
328}
329
330/***
331****
332***/
333
334static uint64_t
335internalIdToPublicBitfield( uint8_t id )
336{
337    uint64_t ret = 0;
338
339    switch( id )
340    {
341        case FR_ID_DOWNLOADED:     ret = TR_FR_DOWNLOADED;    break;
342        case FR_ID_UPLOADED:       ret = TR_FR_UPLOADED;      break;
343        case FR_ID_PROGRESS:       ret = TR_FR_PROGRESS;      break;
344        case FR_ID_PRIORITY:       ret = TR_FR_PRIORITY;      break;
345        case FR_ID_SPEED:          ret = TR_FR_SPEEDLIMIT;    break;
346        case FR_ID_RUN:            ret = TR_FR_RUN;           break;
347        case FR_ID_CORRUPT:        ret = TR_FR_CORRUPT;       break;
348        case FR_ID_PEERS:          ret = TR_FR_PEERS;         break;
349        case FR_ID_DESTINATION:    ret = TR_FR_DESTINATION;   break;
350        case FR_ID_MAX_PEERS:      ret = TR_FR_MAX_PEERS;     break;
351    }
352
353    return ret;
354}
355
356static void
357readBytes( void * target, const uint8_t ** source, size_t byteCount )
358{
359    memcpy( target, *source, byteCount );
360    *source += byteCount;
361}
362
363static uint64_t
364parseDownloaded( tr_torrent * tor, const uint8_t * buf, uint32_t len )
365{
366    if( len != sizeof(uint64_t) )
367        return 0;
368    readBytes( &tor->downloadedPrev, &buf, sizeof(uint64_t) );
369    return TR_FR_DOWNLOADED;
370}
371
372static uint64_t
373parseUploaded( tr_torrent * tor, const uint8_t * buf, uint32_t len )
374{
375    if( len != sizeof(uint64_t) )
376        return 0;
377    readBytes( &tor->uploadedPrev, &buf, sizeof(uint64_t) );
378    return TR_FR_UPLOADED;
379}
380
381static uint64_t
382parseCorrupt( tr_torrent * tor, const uint8_t * buf, uint32_t len )
383{
384    if( len != sizeof(uint64_t) )
385        return 0;
386    readBytes( &tor->corruptPrev, &buf, sizeof(uint64_t) );
387    return TR_FR_CORRUPT;
388}
389
390static uint64_t
391parseConnections( tr_torrent * tor, const uint8_t * buf, uint32_t len )
392{
393    if( len != sizeof(uint16_t) )
394        return 0;
395    readBytes( &tor->maxConnectedPeers, &buf, sizeof(uint16_t) );
396    return TR_FR_MAX_PEERS;
397}
398
399static uint64_t
400parseProgress( tr_torrent     * tor,
401               const uint8_t  * buf,
402               uint32_t         len )
403{
404    int i;
405    uint64_t ret = 0;
406   
407    if( len == FR_PROGRESS_LEN( tor ) )
408    {
409        int n;
410        tr_bitfield bitfield;
411
412        /* compare file mtimes */
413        tr_time_t * curMTimes = getMTimes( tor, &n );
414        const uint8_t * walk = buf;
415        tr_time_t mtime;
416        for( i=0; i<n; ++i ) {
417            readBytes( &mtime, &walk, sizeof(tr_time_t) );
418            if ( curMTimes[i] == mtime )
419                tr_torrentSetFileChecked( tor, i, TRUE );
420            else {
421                tr_torrentSetFileChecked( tor, i, FALSE );
422                tr_dbg( _( "Torrent \"%s\" needs to be verified" ), tor->info.files[i].name );
423            }
424        }
425        free( curMTimes );
426
427        /* get the completion bitfield */
428        memset( &bitfield, 0, sizeof bitfield );
429        bitfield.len = FR_BLOCK_BITFIELD_LEN( tor );
430        bitfield.bits = (uint8_t*) walk;
431        tr_cpBlockBitfieldSet( tor->completion, &bitfield );
432
433        ret = TR_FR_PROGRESS;
434    }
435
436    /* the files whose mtimes are wrong,
437       remove from completion pending a recheck... */
438    for( i=0; i<tor->info.pieceCount; ++i )
439        if( !tr_torrentIsPieceChecked( tor, i ) )
440            tr_cpPieceRem( tor->completion, i );
441
442    return ret;
443}
444
445static uint64_t
446parsePriorities( tr_torrent * tor, const uint8_t * buf, uint32_t len )
447{
448    uint64_t ret = 0;
449
450    if( len == (uint32_t)(2 * tor->info.fileCount) )
451    {
452        const size_t n = tor->info.fileCount;
453        const size_t len = 2 * n;
454        int *dnd = NULL, dndCount = 0;
455        int *dl = NULL, dlCount = 0;
456        size_t i;
457        const uint8_t * walk = buf;
458
459        /* set file priorities */
460        for( i=0; i<n; ++i ) {
461           tr_priority_t priority;
462           const char ch = *walk++;
463           switch( ch ) {
464               case 'l': priority = TR_PRI_LOW; break;
465               case 'h': priority = TR_PRI_HIGH; break;
466               default:  priority = TR_PRI_NORMAL; break;
467           }
468           tr_torrentInitFilePriority( tor, i, priority );
469        }
470
471        /* set the dnd flags */
472        dl = tr_new( int, len );
473        dnd = tr_new( int, len );
474        for( i=0; i<n; ++i )
475            if( *walk++ == 't' ) /* 't' means the DND flag is true */
476                dnd[dndCount++] = i;
477            else
478                dl[dlCount++] = i;
479
480        if( dndCount )
481            tr_torrentInitFileDLs ( tor, dnd, dndCount, FALSE );
482        if( dlCount )
483            tr_torrentInitFileDLs ( tor, dl, dlCount, TRUE );
484
485        tr_free( dnd );
486        tr_free( dl );
487
488        ret = TR_FR_PRIORITY;
489    }
490
491    return ret;
492}
493
494static uint64_t
495parseSpeedLimit( tr_torrent * tor, const uint8_t * buf, uint32_t len )
496{
497    uint64_t ret = 0;
498
499    if( len == FR_SPEED_LEN )
500    {
501        uint8_t i8;
502        uint16_t i16;
503
504        readBytes( &i16, &buf, sizeof(i16) );
505        tr_torrentSetSpeedLimit( tor, TR_DOWN, i16 );
506        readBytes( &i8, &buf, sizeof(i8) );
507        tr_torrentSetSpeedMode( tor, TR_DOWN, (tr_speedlimit)i8 );
508        readBytes( &i16, &buf, sizeof(i16) );
509        tr_torrentSetSpeedLimit( tor, TR_UP, i16 );
510        readBytes( &i8, &buf, sizeof(i8) );
511        tr_torrentSetSpeedMode( tor, TR_UP, (tr_speedlimit)i8 );
512
513        ret = TR_FR_SPEEDLIMIT;
514    }
515
516    return ret;
517}
518
519static uint64_t
520parseRun( tr_torrent * tor, const uint8_t * buf, uint32_t len )
521{
522    if( len != 1 )
523        return 0;
524    tor->isRunning = *buf=='t';
525    return TR_FR_RUN;
526}
527
528static uint64_t
529parsePeers( tr_torrent * tor, const uint8_t * buf, uint32_t len )
530{
531    uint64_t ret = 0;
532
533    if( !tor->info.isPrivate )
534    {
535        int i;
536        const int count = len / sizeof(tr_pex);
537
538        for( i=0; i<count; ++i )
539        {
540            tr_pex pex;
541            readBytes( &pex, &buf, sizeof( tr_pex ) );
542            tr_peerMgrAddPex( tor->handle->peerMgr, tor->info.hash, TR_PEER_FROM_CACHE, &pex );
543        }
544
545        tr_dbg( _( "Fead %i peers from resume file" ), count );
546        ret = TR_FR_PEERS;
547    }
548
549    return ret;
550}
551
552static uint64_t
553parseDestination( tr_torrent * tor, const uint8_t * buf, uint32_t len )
554{
555    uint64_t ret = 0;
556
557    if( buf && *buf && len ) {
558        tr_free( tor->destination );
559        tor->destination = tr_strndup( (char*)buf, len );
560        ret = TR_FR_DESTINATION;
561    }
562
563    return ret;
564}
565
566static uint64_t
567parseVersion1( tr_torrent * tor, const uint8_t * buf, const uint8_t * end,
568               uint64_t fieldsToLoad )
569{
570    uint64_t ret = 0;
571
572    while( end-buf >= 5 )
573    {
574        uint8_t id;
575        uint32_t len;
576        readBytes( &id, &buf, sizeof(id) );
577        readBytes( &len, &buf, sizeof(len) );
578
579        if( fieldsToLoad & internalIdToPublicBitfield( id ) ) switch( id )
580        {
581            case FR_ID_DOWNLOADED:   ret |= parseDownloaded( tor, buf, len ); break;
582            case FR_ID_UPLOADED:     ret |= parseUploaded( tor, buf, len ); break;
583            case FR_ID_PROGRESS:     ret |= parseProgress( tor, buf, len ); break;
584            case FR_ID_PRIORITY:     ret |= parsePriorities( tor, buf, len ); break;
585            case FR_ID_SPEED:        ret |= parseSpeedLimit( tor, buf, len ); break;
586            case FR_ID_RUN:          ret |= parseRun( tor, buf, len ); break;
587            case FR_ID_CORRUPT:      ret |= parseCorrupt( tor, buf, len ); break;
588            case FR_ID_PEERS:        ret |= parsePeers( tor, buf, len ); break;
589            case FR_ID_MAX_PEERS:    ret |= parseConnections( tor, buf, len ); break;
590            case FR_ID_DESTINATION:  ret |= parseDestination( tor, buf, len ); break;
591            default:                 tr_dbg( _( "Skipping unknown resume code %d" ), (int)id ); break;
592        }
593
594        buf += len;
595    }
596
597    return ret;
598}
599
600static uint8_t* 
601loadResumeFile( const tr_torrent * tor, size_t * len )
602{
603    uint8_t * ret = NULL;
604    char path[MAX_PATH_LENGTH];
605    const char * cacheDir = tr_getCacheDirectory ();
606    const char * hash = tor->info.hashString;
607
608    if( !ret && tor->handle->tag )
609    {
610        char base[1024];
611        snprintf( base, sizeof(base), "%s-%s", hash, tor->handle->tag );
612        tr_buildPath( path, sizeof(path), cacheDir, base, NULL );
613        ret = tr_loadFile( path, len );
614    }
615
616    if( !ret )
617    {
618        tr_buildPath( path, sizeof(path), cacheDir, hash, NULL );
619        ret = tr_loadFile( path, len );
620    }
621
622    return ret;
623}
624
625static uint64_t
626fastResumeLoadImpl ( tr_torrent   * tor,
627                     uint64_t       fieldsToLoad )
628{
629    uint64_t ret = 0;
630    size_t size = 0;
631    uint8_t * buf = loadResumeFile( tor, &size );
632
633    if( !buf )
634        tr_inf( _( "Couldn't read resume file for \"%s\"" ), tor->info.name );
635    else {
636        const uint8_t * walk = buf;
637        const uint8_t * end = walk + size;
638        if( end - walk >= 4 ) {
639            uint32_t version;
640            readBytes( &version, &walk, sizeof(version) );
641            if( version == 1 )
642                ret |= parseVersion1 ( tor, walk, end, fieldsToLoad );
643            else
644                tr_inf( _( "Skipping invalid resume file for \"%s\"" ), tor->info.name );
645        }
646
647        tr_free( buf );
648    }
649
650    return ret;
651}
652
653static uint64_t
654setFromCtor( tr_torrent * tor, uint64_t fields, const tr_ctor * ctor, int mode )
655{
656    uint64_t ret = 0;
657
658    if( fields & TR_FR_RUN ) {
659        uint8_t isPaused;
660        if( !tr_ctorGetPaused( ctor, mode, &isPaused ) ) {
661            tor->isRunning = !isPaused;
662            ret |= TR_FR_RUN;
663        }
664    }
665
666    if( fields & TR_FR_MAX_PEERS ) 
667        if( !tr_ctorGetMaxConnectedPeers( ctor, mode, &tor->maxConnectedPeers ) )
668            ret |= TR_FR_MAX_PEERS;
669
670    if( fields & TR_FR_DESTINATION ) {
671        const char * destination;
672        if( !tr_ctorGetDestination( ctor, mode, &destination ) ) {
673            ret |= TR_FR_DESTINATION;
674            tr_free( tor->destination );
675            tor->destination = tr_strdup( destination );
676        }
677    }
678
679    return ret;
680}
681
682static uint64_t
683useManditoryFields( tr_torrent * tor, uint64_t fields, const tr_ctor * ctor )
684{
685    return setFromCtor( tor, fields, ctor, TR_FORCE );
686}
687
688static uint64_t
689useFallbackFields( tr_torrent * tor, uint64_t fields, const tr_ctor * ctor )
690{
691    return setFromCtor( tor, fields, ctor, TR_FALLBACK );
692}
693
694uint64_t
695tr_fastResumeLoad( tr_torrent     * tor,
696                   uint64_t         fieldsToLoad,
697                   const tr_ctor  * ctor )
698{
699    uint64_t ret = 0;
700
701    ret |= useManditoryFields( tor, fieldsToLoad, ctor );
702    fieldsToLoad &= ~ret;
703    ret |= fastResumeLoadImpl( tor, fieldsToLoad );
704    fieldsToLoad &= ~ret;
705    ret |= useFallbackFields( tor, fieldsToLoad, ctor );
706
707    return ret;
708}
709
710void
711tr_fastResumeRemove( const tr_torrent * tor )
712{
713    char path[MAX_PATH_LENGTH];
714    const char * cacheDir = tr_getCacheDirectory ();
715    const char * hash = tor->info.hashString;
716
717    if( tor->handle->tag )
718    {
719        char base[1024];
720        snprintf( base, sizeof(base), "%s-%s", hash, tor->handle->tag );
721        tr_buildPath( path, sizeof(path), cacheDir, base, NULL );
722        unlink( path );
723    }
724    else
725    {
726        tr_buildPath( path, sizeof(path), cacheDir, hash, NULL );
727        unlink( path );
728    }
729}
Note: See TracBrowser for help on using the repository browser.