1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/libtransmission/fastresume.c

696 lines
20 KiB
C
Raw Normal View History

/******************************************************************************
2007-08-18 17:19:49 +00:00
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2005-2008 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
/***********************************************************************
* Fast resume
***********************************************************************
* The format of the resume file is a 4 byte format version (currently 1),
* followed by several variable-sized blocks of data. Each block is
* preceded by a 1 byte ID and a 4 byte length. The currently recognized
* IDs are defined below by the FR_ID_* macros. The length does not include
* the 5 bytes for the ID and length.
*
* The name of the resume file is "resume.<hash>-<tag>", although
* older files with a name of "resume.<hash>" will be recognized if
* the former doesn't exist.
*
* All values are stored in the native endianness. Moving a
* libtransmission resume file from an architecture to another will not
* work, although it will not hurt either (the version will be wrong,
* so the resume file will not be read).
**********************************************************************/
2007-11-09 20:07:52 +00:00
#include <assert.h>
2008-03-06 21:59:00 +00:00
#include <errno.h>
#include <stdio.h>
2008-02-19 18:39:49 +00:00
#include <stdlib.h> /* calloc */
#include <string.h> /* strcpy, memset, memcmp */
2007-07-12 17:51:45 +00:00
#include <sys/types.h>
2008-02-19 18:39:49 +00:00
#include <sys/stat.h> /* stat */
#include <unistd.h> /* unlink */
#include "transmission.h"
#include "completion.h"
#include "fastresume.h"
#include "peer-mgr.h"
#include "platform.h"
#include "resume.h" /* TR_FR_ bitwise enum */
#include "torrent.h"
#include "utils.h"
/* time_t can be 32 or 64 bits... for consistency we'll hardwire 64 */
typedef uint64_t tr_time_t;
enum
{
/* number of bytes downloaded */
FR_ID_DOWNLOADED = 2,
/* number of bytes uploaded */
FR_ID_UPLOADED = 3,
/* progress data:
* - 4 bytes * number of files: mtimes of files
* - 1 bit * number of blocks: whether we have the block or not */
FR_ID_PROGRESS = 5,
/* dnd and priority
* char * number of files: l,n,h for low, normal, high priority
* char * number of files: t,f for DND flags */
FR_ID_PRIORITY = 6,
/* transfer speeds
* uint32_t: dl speed rate to use when the mode is single
* uint32_t: dl's tr_speedlimit
* uint32_t: ul speed rate to use when the mode is single
* uint32_t: ul's tr_speedlimit
*/
FR_ID_SPEED = 8,
/* active
* char: 't' if running, 'f' if paused
*/
FR_ID_RUN = 9,
/* number of corrupt bytes downloaded */
FR_ID_CORRUPT = 10,
/* IPs and ports of connectable peers */
FR_ID_PEERS = 11,
2008-05-18 16:44:30 +00:00
/* download dir of the torrent: zero-terminated string */
FR_ID_DOWNLOAD_DIR = 12,
/* pex flag
* 't' if pex is enabled, 'f' if disabled */
FR_ID_PEX = 13,
/* max connected peers -- uint16_t */
FR_ID_MAX_PEERS = 14
};
/* macros for the length of various pieces of the progress data */
#define FR_MTIME_LEN( t ) \
( sizeof(tr_time_t) * (t)->info.fileCount )
#define FR_BLOCK_BITFIELD_LEN( t ) \
( ( (t)->blockCount + 7u ) / 8u )
#define FR_PROGRESS_LEN( t ) \
( FR_MTIME_LEN( t ) + FR_BLOCK_BITFIELD_LEN( t ) )
2007-07-20 06:08:34 +00:00
#define FR_SPEED_LEN (2 * (sizeof(uint16_t) + sizeof(uint8_t) ) )
#if 0
static void
fastResumeFileName( char * buf, size_t buflen, const tr_torrent * tor, int tag )
{
const char * cacheDir = tr_getResumeDir( tor->handle );
const char * hash = tor->info.hashString;
if( !tag )
{
tr_buildPath( buf, buflen, cacheDir, hash, NULL );
}
else
{
char base[1024];
snprintf( base, sizeof(base), "%s-%s", hash, tor->handle->tag );
tr_buildPath( buf, buflen, cacheDir, base, NULL );
}
}
#endif
static tr_time_t*
getMTimes( const tr_torrent * tor, int * setme_n )
{
int i;
const int n = tor->info.fileCount;
tr_time_t * m = calloc( n, sizeof(tr_time_t) );
for( i=0; i<n; ++i ) {
char fname[MAX_PATH_LENGTH];
struct stat sb;
tr_buildPath( fname, sizeof(fname),
2008-05-18 16:44:30 +00:00
tor->downloadDir, tor->info.files[i].name, NULL );
if ( !stat( fname, &sb ) && S_ISREG( sb.st_mode ) ) {
#ifdef SYS_DARWIN
m[i] = sb.st_mtimespec.tv_sec;
#else
m[i] = sb.st_mtime;
#endif
}
}
*setme_n = n;
return m;
}
#if 0
static void
fastResumeWriteData( uint8_t id,
const void * data,
uint32_t size,
uint32_t count,
FILE * file )
{
uint32_t datalen = size * count;
fwrite( &id, 1, 1, file );
fwrite( &datalen, 4, 1, file );
fwrite( data, size, count, file );
}
void
tr_fastResumeSave( const tr_torrent * tor )
{
char path[MAX_PATH_LENGTH];
FILE * file;
const int version = 1;
uint64_t total;
fastResumeFileName( path, sizeof path, tor, 1 );
file = fopen( path, "wb+" );
2008-03-06 21:59:00 +00:00
if( !file ) {
2008-03-30 13:52:55 +00:00
tr_torerr( tor, _( "Couldn't open \"%1$s\": %2$s" ), path, tr_strerror( errno ) );
return;
}
/* Write format version */
fwrite( &version, 4, 1, file );
2008-05-18 16:44:30 +00:00
if( TRUE ) /* FR_ID_DOWNLOAD_DIR */
{
2008-05-18 16:44:30 +00:00
const char * d = tor->downloadDir ? tor->downloadDir : "";
const int byteCount = strlen( d ) + 1;
2008-05-18 16:44:30 +00:00
fastResumeWriteData( FR_ID_DOWNLOAD_DIR, d, 1, byteCount, file );
}
/* Write progress data */
2008-05-06 20:31:05 +00:00
{
int i, n;
tr_time_t * mtimes;
uint8_t * buf = malloc( FR_PROGRESS_LEN( tor ) );
uint8_t * walk = buf;
const tr_bitfield * bitfield;
/* mtimes */
mtimes = getMTimes( tor, &n );
for( i=0; i<n; ++i )
if( !tr_torrentIsFileChecked( tor, i ) )
mtimes[i] = ~(tr_time_t)0; /* force a recheck next time */
memcpy( walk, mtimes, n*sizeof(tr_time_t) );
walk += n * sizeof(tr_time_t);
/* completion bitfield */
bitfield = tr_cpBlockBitfield( tor->completion );
assert( (unsigned)FR_BLOCK_BITFIELD_LEN( tor ) == bitfield->len );
memcpy( walk, bitfield->bits, bitfield->len );
walk += bitfield->len;
/* write it */
assert( walk-buf == (int)FR_PROGRESS_LEN( tor ) );
fastResumeWriteData( FR_ID_PROGRESS, buf, 1, walk-buf, file );
/* cleanup */
free( mtimes );
free( buf );
}
/* Write the priorities and DND flags */
if( TRUE )
{
int i;
const int n = tor->info.fileCount;
char * buf = tr_new0( char, n*2 );
char * walk = buf;
/* priorities */
for( i=0; i<n; ++i ) {
char ch;
const int priority = tor->info.files[i].priority;
switch( priority ) {
case TR_PRI_LOW: ch = 'l'; break; /* low */
case TR_PRI_HIGH: ch = 'h'; break; /* high */
default: ch = 'n'; break; /* normal */
};
*walk++ = ch;
}
/* dnd flags */
for( i=0; i<n; ++i )
2007-07-10 03:23:07 +00:00
*walk++ = tor->info.files[i].dnd ? 't' : 'f';
/* write it */
assert( walk - buf == 2*n );
fastResumeWriteData( FR_ID_PRIORITY, buf, 1, walk-buf, file );
/* cleanup */
tr_free( buf );
}
/* Write the torrent ul/dl speed caps */
if( TRUE )
{
2007-07-20 06:08:34 +00:00
const int len = FR_SPEED_LEN;
char * buf = tr_new0( char, len );
char * walk = buf;
2007-07-20 06:08:34 +00:00
uint16_t i16;
uint8_t i8;
i16 = (uint16_t) tr_torrentGetSpeedLimit( tor, TR_DOWN );
memcpy( walk, &i16, 2 ); walk += 2;
i8 = (uint8_t) tr_torrentGetSpeedMode( tor, TR_DOWN );
memcpy( walk, &i8, 1 ); walk += 1;
i16 = (uint16_t) tr_torrentGetSpeedLimit( tor, TR_UP );
memcpy( walk, &i16, 2 ); walk += 2;
i8 = (uint8_t) tr_torrentGetSpeedMode( tor, TR_UP );
memcpy( walk, &i8, 1 ); walk += 1;
assert( walk - buf == len );
fastResumeWriteData( FR_ID_SPEED, buf, 1, walk-buf, file );
2007-07-29 19:16:13 +00:00
tr_free( buf );
}
if( TRUE ) /* FR_ID_RUN */
{
const char is_running = tor->isRunning ? 't' : 'f';
fastResumeWriteData( FR_ID_RUN, &is_running, 1, 1, file );
}
/* Write download and upload totals */
total = tor->downloadedCur + tor->downloadedPrev;
fastResumeWriteData( FR_ID_DOWNLOADED, &total, 8, 1, file );
total = tor->uploadedCur + tor->uploadedPrev;
fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file );
total = tor->corruptCur + tor->corruptPrev;
fastResumeWriteData( FR_ID_CORRUPT, &total, 8, 1, file );
fastResumeWriteData( FR_ID_MAX_PEERS,
&tor->maxConnectedPeers,
sizeof(uint16_t), 1, file );
2007-10-15 20:58:39 +00:00
if( !tor->info.isPrivate )
{
tr_pex * pex;
const int count = tr_peerMgrGetPeers( tor->handle->peerMgr,
tor->info.hash,
&pex );
if( count > 0 )
fastResumeWriteData( FR_ID_PEERS, pex, sizeof(tr_pex), count, file );
tr_free( pex );
}
fclose( file );
}
#endif
/***
****
***/
static uint64_t
internalIdToPublicBitfield( uint8_t id )
{
uint64_t ret = 0;
switch( id )
{
case FR_ID_DOWNLOADED: ret = TR_FR_DOWNLOADED; break;
case FR_ID_UPLOADED: ret = TR_FR_UPLOADED; break;
case FR_ID_PROGRESS: ret = TR_FR_PROGRESS; break;
case FR_ID_PRIORITY: ret = TR_FR_PRIORITY; break;
case FR_ID_SPEED: ret = TR_FR_SPEEDLIMIT; break;
case FR_ID_RUN: ret = TR_FR_RUN; break;
case FR_ID_CORRUPT: ret = TR_FR_CORRUPT; break;
case FR_ID_PEERS: ret = TR_FR_PEERS; break;
2008-05-18 16:44:30 +00:00
case FR_ID_DOWNLOAD_DIR: ret = TR_FR_DOWNLOAD_DIR; break;
case FR_ID_MAX_PEERS: ret = TR_FR_MAX_PEERS; break;
}
return ret;
}
static void
readBytes( void * target, const uint8_t ** source, size_t byteCount )
{
memcpy( target, *source, byteCount );
*source += byteCount;
}
static uint64_t
parseDownloaded( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
if( len != sizeof(uint64_t) )
return 0;
readBytes( &tor->downloadedPrev, &buf, sizeof(uint64_t) );
return TR_FR_DOWNLOADED;
}
static uint64_t
parseUploaded( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
if( len != sizeof(uint64_t) )
return 0;
readBytes( &tor->uploadedPrev, &buf, sizeof(uint64_t) );
return TR_FR_UPLOADED;
}
static uint64_t
parseCorrupt( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
if( len != sizeof(uint64_t) )
return 0;
readBytes( &tor->corruptPrev, &buf, sizeof(uint64_t) );
return TR_FR_CORRUPT;
}
static uint64_t
parseConnections( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
if( len != sizeof(uint16_t) )
return 0;
readBytes( &tor->maxConnectedPeers, &buf, sizeof(uint16_t) );
return TR_FR_MAX_PEERS;
}
static uint64_t
2008-01-02 18:05:05 +00:00
parseProgress( tr_torrent * tor,
const uint8_t * buf,
uint32_t len )
{
uint64_t ret = 0;
if( len == FR_PROGRESS_LEN( tor ) )
{
int i;
int n;
tr_bitfield bitfield;
/* compare file mtimes */
tr_time_t * curMTimes = getMTimes( tor, &n );
const uint8_t * walk = buf;
tr_time_t mtime;
for( i=0; i<n; ++i ) {
readBytes( &mtime, &walk, sizeof(tr_time_t) );
if ( curMTimes[i] == mtime )
2008-01-02 18:05:05 +00:00
tr_torrentSetFileChecked( tor, i, TRUE );
else {
tr_torrentSetFileChecked( tor, i, FALSE );
tr_tordbg( tor, "Torrent needs to be verified" );
}
}
free( curMTimes );
/* get the completion bitfield */
memset( &bitfield, 0, sizeof bitfield );
bitfield.byteCount = FR_BLOCK_BITFIELD_LEN( tor );
bitfield.bitCount = bitfield.byteCount * 8;
bitfield.bits = (uint8_t*) walk;
if( !tr_cpBlockBitfieldSet( tor->completion, &bitfield ) )
ret = TR_FR_PROGRESS;
else {
tr_torrentUncheck( tor );
tr_tordbg( tor, "Torrent needs to be verified" );
}
}
/* the files whose mtimes are wrong,
remove from completion pending a recheck... */
{
tr_piece_index_t i;
for( i=0; i<tor->info.pieceCount; ++i )
if( !tr_torrentIsPieceChecked( tor, i ) )
tr_cpPieceRem( tor->completion, i );
}
return ret;
}
static uint64_t
parsePriorities( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
uint64_t ret = 0;
if( len == (uint32_t)(2 * tor->info.fileCount) )
{
const size_t n = tor->info.fileCount;
const size_t len = 2 * n;
tr_file_index_t *dnd = NULL, dndCount = 0;
tr_file_index_t *dl = NULL, dlCount = 0;
size_t i;
const uint8_t * walk = buf;
/* set file priorities */
for( i=0; i<n; ++i ) {
tr_priority_t priority;
const char ch = *walk++;
switch( ch ) {
case 'l': priority = TR_PRI_LOW; break;
case 'h': priority = TR_PRI_HIGH; break;
default: priority = TR_PRI_NORMAL; break;
}
2007-12-25 06:37:21 +00:00
tr_torrentInitFilePriority( tor, i, priority );
}
/* set the dnd flags */
dl = tr_new( tr_file_index_t, len );
dnd = tr_new( tr_file_index_t, len );
for( i=0; i<n; ++i )
if( *walk++ == 't' ) /* 't' means the DND flag is true */
dnd[dndCount++] = i;
else
dl[dlCount++] = i;
if( dndCount )
tr_torrentInitFileDLs ( tor, dnd, dndCount, FALSE );
if( dlCount )
tr_torrentInitFileDLs ( tor, dl, dlCount, TRUE );
tr_free( dnd );
tr_free( dl );
ret = TR_FR_PRIORITY;
}
return ret;
}
static uint64_t
parseSpeedLimit( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
uint64_t ret = 0;
if( len == FR_SPEED_LEN )
{
uint8_t i8;
uint16_t i16;
readBytes( &i16, &buf, sizeof(i16) );
tr_torrentSetSpeedLimit( tor, TR_DOWN, i16 );
readBytes( &i8, &buf, sizeof(i8) );
tr_torrentSetSpeedMode( tor, TR_DOWN, (tr_speedlimit)i8 );
readBytes( &i16, &buf, sizeof(i16) );
tr_torrentSetSpeedLimit( tor, TR_UP, i16 );
readBytes( &i8, &buf, sizeof(i8) );
tr_torrentSetSpeedMode( tor, TR_UP, (tr_speedlimit)i8 );
ret = TR_FR_SPEEDLIMIT;
}
return ret;
}
static uint64_t
parseRun( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
if( len != 1 )
return 0;
tor->isRunning = *buf=='t';
return TR_FR_RUN;
}
static uint64_t
parsePeers( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
uint64_t ret = 0;
if( !tor->info.isPrivate )
{
int i;
const int count = len / sizeof(tr_pex);
for( i=0; i<count; ++i )
{
tr_pex pex;
readBytes( &pex, &buf, sizeof( tr_pex ) );
tr_peerMgrAddPex( tor->handle->peerMgr, tor->info.hash, TR_PEER_FROM_CACHE, &pex );
}
tr_tordbg( tor, "Loaded %d peers from resume file", count );
ret = TR_FR_PEERS;
}
return ret;
}
static uint64_t
2008-05-18 16:44:30 +00:00
parseDownloadDir( tr_torrent * tor, const uint8_t * buf, uint32_t len )
{
2007-12-21 22:18:40 +00:00
uint64_t ret = 0;
if( buf && *buf && len ) {
2008-05-18 16:44:30 +00:00
tr_free( tor->downloadDir );
tor->downloadDir = tr_strndup( (char*)buf, len );
ret = TR_FR_DOWNLOAD_DIR;
2007-12-21 22:18:40 +00:00
}
2007-12-21 22:18:40 +00:00
return ret;
}
static uint64_t
parseVersion1( tr_torrent * tor, const uint8_t * buf, const uint8_t * end,
2008-01-02 18:05:05 +00:00
uint64_t fieldsToLoad )
{
uint64_t ret = 0;
while( end-buf >= 5 )
{
uint8_t id;
uint32_t len;
readBytes( &id, &buf, sizeof(id) );
readBytes( &len, &buf, sizeof(len) );
if( buf + len > end )
{
tr_torerr( tor, "Resume file seems to be corrupt. Skipping." );
}
else if( fieldsToLoad & internalIdToPublicBitfield( id ) ) switch( id )
{
case FR_ID_DOWNLOADED: ret |= parseDownloaded( tor, buf, len ); break;
case FR_ID_UPLOADED: ret |= parseUploaded( tor, buf, len ); break;
2008-01-02 18:05:05 +00:00
case FR_ID_PROGRESS: ret |= parseProgress( tor, buf, len ); break;
case FR_ID_PRIORITY: ret |= parsePriorities( tor, buf, len ); break;
case FR_ID_SPEED: ret |= parseSpeedLimit( tor, buf, len ); break;
case FR_ID_RUN: ret |= parseRun( tor, buf, len ); break;
case FR_ID_CORRUPT: ret |= parseCorrupt( tor, buf, len ); break;
case FR_ID_PEERS: ret |= parsePeers( tor, buf, len ); break;
case FR_ID_MAX_PEERS: ret |= parseConnections( tor, buf, len ); break;
2008-05-18 16:44:30 +00:00
case FR_ID_DOWNLOAD_DIR: ret |= parseDownloadDir( tor, buf, len ); break;
default: tr_tordbg( tor, "Skipping unknown resume code %d", (int)id ); break;
}
buf += len;
}
return ret;
}
static uint8_t*
loadResumeFile( const tr_torrent * tor, size_t * len )
{
uint8_t * ret = NULL;
char path[MAX_PATH_LENGTH];
const char * cacheDir = tr_getResumeDir( tor->handle );
const char * hash = tor->info.hashString;
if( !ret && tor->handle->tag )
{
char base[1024];
snprintf( base, sizeof(base), "%s-%s", hash, tor->handle->tag );
tr_buildPath( path, sizeof(path), cacheDir, base, NULL );
ret = tr_loadFile( path, len );
}
if( !ret )
{
tr_buildPath( path, sizeof(path), cacheDir, hash, NULL );
ret = tr_loadFile( path, len );
}
return ret;
}
static uint64_t
fastResumeLoadImpl ( tr_torrent * tor,
2008-01-02 18:05:05 +00:00
uint64_t fieldsToLoad )
{
uint64_t ret = 0;
size_t size = 0;
uint8_t * buf = loadResumeFile( tor, &size );
if( !buf )
/* %s is the torrent name */
2008-04-21 15:22:54 +00:00
tr_torinf( tor, _( "Couldn't read resume file" ) );
else {
const uint8_t * walk = buf;
const uint8_t * end = walk + size;
if( end - walk >= 4 ) {
uint32_t version;
readBytes( &version, &walk, sizeof(version) );
if( version == 1 )
2008-01-02 18:05:05 +00:00
ret |= parseVersion1 ( tor, walk, end, fieldsToLoad );
else
/* %s is the torrent name */
2008-04-21 15:22:54 +00:00
tr_torinf( tor, _( "Couldn't read resume file" ) );
}
tr_free( buf );
}
return ret;
}
2007-12-21 22:18:40 +00:00
uint64_t
tr_fastResumeLoad( tr_torrent * tor,
uint64_t fieldsToLoad )
2007-12-21 22:18:40 +00:00
{
return fastResumeLoadImpl( tor, fieldsToLoad );
}
void
tr_fastResumeRemove( const tr_torrent * tor )
{
char path[MAX_PATH_LENGTH];
const char * cacheDir = tr_getResumeDir( tor->handle );
const char * hash = tor->info.hashString;
if( tor->handle->tag )
{
char base[1024];
snprintf( base, sizeof(base), "%s-%s", hash, tor->handle->tag );
tr_buildPath( path, sizeof(path), cacheDir, base, NULL );
unlink( path );
}
else
{
tr_buildPath( path, sizeof(path), cacheDir, hash, NULL );
unlink( path );
}
}