2010-06-19 14:25:11 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2010-06-19 14:25:11 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2010-06-19 14:25:11 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2011-03-16 18:04:23 +00:00
|
|
|
#include <stdlib.h> /* qsort() */
|
|
|
|
|
2011-01-29 18:56:53 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "inout.h"
|
|
|
|
#include "peer-common.h" /* MAX_BLOCK_SIZE */
|
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "torrent.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define MY_NAME "Cache"
|
|
|
|
|
2010-07-01 16:26:30 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, MY_NAME, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct cache_block
|
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
|
|
|
|
|
|
|
tr_piece_index_t piece;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t length;
|
|
|
|
|
2010-06-26 16:21:50 +00:00
|
|
|
time_t time;
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_block_index_t block;
|
|
|
|
|
2011-01-29 18:56:53 +00:00
|
|
|
struct evbuffer * evbuf;
|
2010-06-19 14:25:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_cache
|
|
|
|
{
|
|
|
|
tr_ptrArray blocks;
|
2010-07-03 00:25:22 +00:00
|
|
|
int max_blocks;
|
|
|
|
size_t max_bytes;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
|
|
|
size_t disk_writes;
|
|
|
|
size_t disk_write_bytes;
|
|
|
|
size_t cache_writes;
|
|
|
|
size_t cache_write_bytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2010-06-26 16:21:50 +00:00
|
|
|
struct run_info
|
|
|
|
{
|
2010-07-08 17:38:11 +00:00
|
|
|
int pos;
|
|
|
|
int rank;
|
|
|
|
time_t last_block_time;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool is_multi_piece;
|
|
|
|
bool is_piece_done;
|
2010-07-08 17:38:11 +00:00
|
|
|
unsigned len;
|
2010-06-26 16:21:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
/* return a count of how many contiguous blocks there are starting at this pos */
|
|
|
|
static int
|
2010-06-26 16:21:50 +00:00
|
|
|
getBlockRun( const tr_cache * cache, int pos, struct run_info * info )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int n = tr_ptrArraySize( &cache->blocks );
|
|
|
|
const struct cache_block ** blocks = (const struct cache_block**) tr_ptrArrayBase( &cache->blocks );
|
|
|
|
const struct cache_block * ref = blocks[pos];
|
|
|
|
tr_block_index_t block = ref->block;
|
|
|
|
|
|
|
|
for( i=pos; i<n; ++i, ++block ) {
|
|
|
|
const struct cache_block * b = blocks[i];
|
|
|
|
if( b->block != block ) break;
|
|
|
|
if( b->tor != ref->tor ) break;
|
2010-06-26 16:21:50 +00:00
|
|
|
//fprintf( stderr, "pos %d tor %d block %zu time %zu\n", i, b->tor->uniqueId, (size_t)b->block, (size_t)b->time );
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//fprintf( stderr, "run is %d long from [%d to %d)\n", (int)(i-pos), i, (int)pos );
|
2010-06-26 16:21:50 +00:00
|
|
|
if( info != NULL ) {
|
2010-07-08 17:38:11 +00:00
|
|
|
const struct cache_block * b = blocks[i-1];
|
|
|
|
info->last_block_time = b->time;
|
|
|
|
info->is_piece_done = tr_cpPieceIsComplete( &b->tor->completion, b->piece );
|
2011-03-22 15:19:54 +00:00
|
|
|
info->is_multi_piece = b->piece != blocks[pos]->piece ? true : false;
|
2010-07-08 17:38:11 +00:00
|
|
|
info->len = i - pos;
|
|
|
|
info->pos = pos;
|
2010-06-26 16:21:50 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
return i-pos;
|
|
|
|
}
|
|
|
|
|
2011-03-16 00:49:43 +00:00
|
|
|
/* higher rank comes before lower rank */
|
2010-06-19 14:25:11 +00:00
|
|
|
static int
|
2010-07-08 17:38:11 +00:00
|
|
|
compareRuns( const void * va, const void * vb )
|
|
|
|
{
|
2011-03-16 00:49:43 +00:00
|
|
|
const struct run_info * a = va;
|
|
|
|
const struct run_info * b = vb;
|
|
|
|
return b->rank - a->rank;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MULTIFLAG = 0x1000,
|
|
|
|
DONEFLAG = 0x2000,
|
|
|
|
SESSIONFLAG = 0x4000
|
|
|
|
};
|
|
|
|
/* Calculte runs
|
|
|
|
* - Stale runs, runs sitting in cache for a long time or runs not growing, get priority.
|
2010-08-22 09:49:10 +00:00
|
|
|
* Returns number of runs.
|
2010-07-08 17:38:11 +00:00
|
|
|
*/
|
2010-08-22 09:49:10 +00:00
|
|
|
static int
|
2010-07-08 17:38:11 +00:00
|
|
|
calcRuns( tr_cache * cache, struct run_info * runs )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
|
|
|
const int n = tr_ptrArraySize( &cache->blocks );
|
2010-07-08 17:38:11 +00:00
|
|
|
int i = 0, pos;
|
|
|
|
const time_t now = tr_time();
|
|
|
|
|
|
|
|
for( pos = 0; pos < n; pos += runs[i++].len )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2010-07-08 17:38:11 +00:00
|
|
|
int rank = getBlockRun( cache, pos, &runs[i] );
|
|
|
|
|
|
|
|
/* This adds ~2 to the relative length of a run for every minute it has
|
|
|
|
* languished in the cache. */
|
|
|
|
rank += ( now - runs[i].last_block_time ) / 32;
|
|
|
|
|
|
|
|
/* Flushing stale blocks should be a top priority as the probability of them
|
|
|
|
* growing is very small, for blocks on piece boundaries, and nonexistant for
|
|
|
|
* blocks inside pieces. */
|
|
|
|
rank |= runs[i].is_piece_done ? DONEFLAG : 0;
|
|
|
|
|
|
|
|
/* Move the multi piece runs higher */
|
|
|
|
rank |= runs[i].is_multi_piece ? MULTIFLAG : 0;
|
|
|
|
|
|
|
|
runs[i].rank = rank;
|
|
|
|
//fprintf(stderr,"block run at pos %d of length %d and age %ld adjusted +%d\n",runs[i].pos,runs[i].len,now-runs[i].last_block_time,rank-runs[i].len);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 09:49:10 +00:00
|
|
|
//fprintf( stderr, "%d block runs\n", i );
|
2010-07-08 17:38:11 +00:00
|
|
|
qsort( runs, i, sizeof( struct run_info ), compareRuns );
|
2010-08-22 09:49:10 +00:00
|
|
|
return i;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-07-24 00:52:02 +00:00
|
|
|
flushContiguous( tr_cache * cache, int pos, int n )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int err = 0;
|
|
|
|
uint8_t * buf = tr_new( uint8_t, n * MAX_BLOCK_SIZE );
|
|
|
|
uint8_t * walk = buf;
|
|
|
|
struct cache_block ** blocks = (struct cache_block**) tr_ptrArrayBase( &cache->blocks );
|
|
|
|
|
|
|
|
struct cache_block * b = blocks[pos];
|
|
|
|
tr_torrent * tor = b->tor;
|
|
|
|
const tr_piece_index_t piece = b->piece;
|
|
|
|
const uint32_t offset = b->offset;
|
|
|
|
|
2010-07-01 16:26:30 +00:00
|
|
|
//fprintf( stderr, "flushing %d contiguous blocks [%d-%d) from cache to disk\n", n, pos, n+pos );
|
2010-06-19 14:25:11 +00:00
|
|
|
|
|
|
|
for( i=pos; i<pos+n; ++i ) {
|
|
|
|
b = blocks[i];
|
2011-01-29 18:56:53 +00:00
|
|
|
evbuffer_copyout( b->evbuf, walk, b->length );
|
2010-06-19 14:25:11 +00:00
|
|
|
walk += b->length;
|
2011-01-29 18:56:53 +00:00
|
|
|
evbuffer_free( b->evbuf );
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_free( b );
|
|
|
|
}
|
|
|
|
tr_ptrArrayErase( &cache->blocks, pos, pos+n );
|
|
|
|
|
2010-07-13 20:12:54 +00:00
|
|
|
#if 0
|
2010-07-17 22:37:13 +00:00
|
|
|
tr_tordbg( tor, "Writing to disk piece %d, offset %d, len %d", (int)piece, (int)offset, (int)(walk-buf) );
|
2010-07-08 17:38:11 +00:00
|
|
|
tr_ndbg( MY_NAME, "Removing %d blocks from cache, rank: %d - %d left", n, rank, tr_ptrArraySize(&cache->blocks) );
|
2010-07-13 20:12:54 +00:00
|
|
|
fprintf( stderr, "%s - Writing to disk piece %d, offset %d, len %d\n", tr_torrentName(tor), (int)piece, (int)offset, (int)(walk-buf) );
|
|
|
|
fprintf( stderr, "%s - Removing %d blocks from cache; %d left\n", MY_NAME, n, tr_ptrArraySize(&cache->blocks) );
|
|
|
|
#endif
|
2010-06-19 14:25:11 +00:00
|
|
|
|
|
|
|
err = tr_ioWrite( tor, piece, offset, walk-buf, buf );
|
|
|
|
tr_free( buf );
|
|
|
|
|
|
|
|
++cache->disk_writes;
|
|
|
|
cache->disk_write_bytes += walk-buf;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-07-08 17:38:11 +00:00
|
|
|
static int
|
|
|
|
flushRuns( tr_cache * cache, struct run_info * runs, int n )
|
|
|
|
{
|
|
|
|
int i, j, err = 0;
|
|
|
|
|
|
|
|
for( i = 0; !err && i < n; ++i )
|
|
|
|
{
|
2010-07-24 00:52:02 +00:00
|
|
|
err = flushContiguous( cache, runs[i].pos, runs[i].len );
|
2010-07-08 17:38:11 +00:00
|
|
|
for( j = i + 1; j < n; ++j )
|
|
|
|
if( runs[j].pos > runs[i].pos )
|
|
|
|
runs[j].pos -= runs[i].len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
static int
|
|
|
|
cacheTrim( tr_cache * cache )
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
2010-07-08 17:38:11 +00:00
|
|
|
if( tr_ptrArraySize( &cache->blocks ) > cache->max_blocks )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2010-07-08 17:38:11 +00:00
|
|
|
/* Amount of cache that should be removed by the flush. This influences how large
|
|
|
|
* runs can grow as well as how often flushes will happen. */
|
|
|
|
const int cacheCutoff = 1 + cache->max_blocks / 4;
|
|
|
|
struct run_info * runs = tr_new( struct run_info, tr_ptrArraySize( &cache->blocks ) );
|
|
|
|
int i = 0, j = 0;
|
|
|
|
|
|
|
|
calcRuns( cache, runs );
|
|
|
|
while( j < cacheCutoff )
|
|
|
|
j += runs[i++].len;
|
|
|
|
err = flushRuns( cache, runs, i );
|
|
|
|
tr_free( runs );
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
2010-07-03 00:25:22 +00:00
|
|
|
getMaxBlocks( int64_t max_bytes )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2010-07-03 00:25:22 +00:00
|
|
|
return max_bytes / (double)MAX_BLOCK_SIZE;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_cacheSetLimit( tr_cache * cache, int64_t max_bytes )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2010-07-03 00:25:22 +00:00
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
cache->max_bytes = max_bytes;
|
|
|
|
cache->max_blocks = getMaxBlocks( max_bytes );
|
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_mem_B( buf, cache->max_bytes, sizeof( buf ) );
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_ndbg( MY_NAME, "Maximum cache size set to %s (%d blocks)", buf, cache->max_blocks );
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
return cacheTrim( cache );
|
|
|
|
}
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
int64_t
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_cacheGetLimit( const tr_cache * cache )
|
|
|
|
{
|
2010-07-03 00:25:22 +00:00
|
|
|
return cache->max_bytes;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_cache *
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_cacheNew( int64_t max_bytes )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2010-06-26 16:21:50 +00:00
|
|
|
tr_cache * cache = tr_new0( tr_cache, 1 );
|
2010-06-19 14:25:11 +00:00
|
|
|
cache->blocks = TR_PTR_ARRAY_INIT;
|
2010-07-03 00:25:22 +00:00
|
|
|
cache->max_bytes = max_bytes;
|
|
|
|
cache->max_blocks = getMaxBlocks( max_bytes );
|
2010-06-19 14:25:11 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_cacheFree( tr_cache * cache )
|
|
|
|
{
|
|
|
|
assert( tr_ptrArrayEmpty( &cache->blocks ) );
|
|
|
|
tr_ptrArrayDestruct( &cache->blocks, NULL );
|
|
|
|
tr_free( cache );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
|
|
|
cache_block_compare( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct cache_block * a = va;
|
|
|
|
const struct cache_block * b = vb;
|
|
|
|
|
|
|
|
/* primary key: torrent id */
|
|
|
|
if( a->tor->uniqueId != b->tor->uniqueId )
|
|
|
|
return a->tor->uniqueId < b->tor->uniqueId ? -1 : 1;
|
|
|
|
|
|
|
|
/* secondary key: block # */
|
|
|
|
if( a->block != b->block )
|
|
|
|
return a->block < b->block ? -1 : 1;
|
|
|
|
|
2010-06-26 16:21:50 +00:00
|
|
|
/* they're equal */
|
2010-06-19 14:25:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cache_block *
|
|
|
|
findBlock( tr_cache * cache,
|
2010-11-11 15:31:11 +00:00
|
|
|
tr_torrent * torrent,
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset )
|
|
|
|
{
|
|
|
|
struct cache_block key;
|
|
|
|
key.tor = torrent;
|
|
|
|
key.block = _tr_block( torrent, piece, offset );
|
|
|
|
return tr_ptrArrayFindSorted( &cache->blocks, &key, cache_block_compare );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_cacheWriteBlock( tr_cache * cache,
|
|
|
|
tr_torrent * torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length,
|
2011-01-29 18:56:53 +00:00
|
|
|
struct evbuffer * writeme )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
|
|
|
struct cache_block * cb = findBlock( cache, torrent, piece, offset );
|
|
|
|
|
|
|
|
if( cb == NULL )
|
|
|
|
{
|
|
|
|
cb = tr_new( struct cache_block, 1 );
|
|
|
|
cb->tor = torrent;
|
|
|
|
cb->piece = piece;
|
|
|
|
cb->offset = offset;
|
|
|
|
cb->length = length;
|
|
|
|
cb->block = _tr_block( torrent, piece, offset );
|
2011-01-29 18:56:53 +00:00
|
|
|
cb->evbuf = evbuffer_new( );
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_ptrArrayInsertSorted( &cache->blocks, cb, cache_block_compare );
|
|
|
|
}
|
|
|
|
|
2010-06-26 16:21:50 +00:00
|
|
|
cb->time = tr_time();
|
|
|
|
|
2011-01-29 18:56:53 +00:00
|
|
|
assert( cb->length == length );
|
|
|
|
evbuffer_drain( cb->evbuf, evbuffer_get_length( cb->evbuf ) );
|
|
|
|
evbuffer_remove_buffer( writeme, cb->evbuf, cb->length );
|
2010-06-19 14:25:11 +00:00
|
|
|
|
|
|
|
++cache->cache_writes;
|
|
|
|
cache->cache_write_bytes += cb->length;
|
|
|
|
|
|
|
|
return cacheTrim( cache );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_cacheReadBlock( tr_cache * cache,
|
|
|
|
tr_torrent * torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t len,
|
|
|
|
uint8_t * setme )
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct cache_block * cb = findBlock( cache, torrent, piece, offset );
|
|
|
|
|
|
|
|
if( cb )
|
2011-01-29 18:56:53 +00:00
|
|
|
evbuffer_copyout( cb->evbuf, setme, len );
|
2010-06-19 14:25:11 +00:00
|
|
|
else
|
|
|
|
err = tr_ioRead( torrent, piece, offset, len, setme );
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_cachePrefetchBlock( tr_cache * cache,
|
|
|
|
tr_torrent * torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t len )
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct cache_block * cb = findBlock( cache, torrent, piece, offset );
|
|
|
|
|
|
|
|
if( cb == NULL )
|
|
|
|
err = tr_ioPrefetch( torrent, piece, offset, len );
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
2011-02-23 03:54:04 +00:00
|
|
|
findBlockPos( tr_cache * cache, tr_torrent * torrent, tr_piece_index_t block )
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
|
|
|
struct cache_block key;
|
|
|
|
key.tor = torrent;
|
2011-02-23 03:54:04 +00:00
|
|
|
key.block = block;
|
2010-06-19 14:25:11 +00:00
|
|
|
return tr_ptrArrayLowerBound( &cache->blocks, &key, cache_block_compare, NULL );
|
|
|
|
}
|
|
|
|
|
2010-07-08 17:38:11 +00:00
|
|
|
int tr_cacheFlushDone( tr_cache * cache )
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if( tr_ptrArraySize( &cache->blocks ) > 0 )
|
|
|
|
{
|
2010-08-22 09:49:10 +00:00
|
|
|
struct run_info * runs = tr_new( struct run_info, tr_ptrArraySize( &cache->blocks ) );
|
|
|
|
int i = 0, n;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2010-08-22 09:49:10 +00:00
|
|
|
n = calcRuns( cache, runs );
|
|
|
|
while( i < n && ( runs[i].is_piece_done || runs[i].is_multi_piece ) )
|
2010-07-08 17:38:11 +00:00
|
|
|
runs[i++].rank |= SESSIONFLAG;
|
|
|
|
err = flushRuns( cache, runs, i );
|
|
|
|
tr_free( runs );
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
int
|
|
|
|
tr_cacheFlushFile( tr_cache * cache, tr_torrent * torrent, tr_file_index_t i )
|
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
int pos;
|
2010-06-19 14:25:11 +00:00
|
|
|
int err = 0;
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_block_index_t first;
|
|
|
|
tr_block_index_t last;
|
|
|
|
tr_torGetFileBlockRange( torrent, i, &first, &last );
|
|
|
|
pos = findBlockPos( cache, torrent, first );
|
|
|
|
dbgmsg( "flushing file %d from cache to disk: blocks [%zu...%zu]", (int)i, (size_t)first, (size_t)last );
|
2010-06-19 14:25:11 +00:00
|
|
|
|
|
|
|
/* flush out all the blocks in that file */
|
|
|
|
while( !err && ( pos < tr_ptrArraySize( &cache->blocks ) ) )
|
|
|
|
{
|
|
|
|
const struct cache_block * b = tr_ptrArrayNth( &cache->blocks, pos );
|
|
|
|
if( b->tor != torrent ) break;
|
2011-02-23 03:54:04 +00:00
|
|
|
if( ( b->block < first ) || ( b->block > last ) ) break;
|
2010-07-24 00:52:02 +00:00
|
|
|
err = flushContiguous( cache, pos, getBlockRun( cache, pos, NULL ) );
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_cacheFlushTorrent( tr_cache * cache, tr_torrent * torrent )
|
|
|
|
{
|
|
|
|
int err = 0;
|
2011-02-23 03:54:04 +00:00
|
|
|
const int pos = findBlockPos( cache, torrent, 0 );
|
2010-06-19 14:25:11 +00:00
|
|
|
|
|
|
|
/* flush out all the blocks in that torrent */
|
|
|
|
while( !err && ( pos < tr_ptrArraySize( &cache->blocks ) ) )
|
|
|
|
{
|
|
|
|
const struct cache_block * b = tr_ptrArrayNth( &cache->blocks, pos );
|
|
|
|
if( b->tor != torrent ) break;
|
2010-07-24 00:52:02 +00:00
|
|
|
err = flushContiguous( cache, pos, getBlockRun( cache, pos, NULL ) );
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|