1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-24 16:52:39 +00:00

benc cleanup

This commit is contained in:
Charles Kerr 2008-04-18 16:23:59 +00:00
parent ab2b3a957d
commit c88aa6135d
7 changed files with 145 additions and 227 deletions

View file

@ -660,25 +660,15 @@ loadstate( void )
{
const char * str;
int64_t tmp;
uint8_t * buf;
size_t len;
benc_val_t top, * list;
int ii;
struct tor * tor;
buf = readfile( gl_state, &len );
if( NULL == buf )
if( tr_bencLoadFile( gl_state, &top ) )
{
return -1;
}
if( tr_bencLoad( buf, len, &top, NULL ) )
{
free( buf );
errmsg( "failed to load bencoded data from %s", gl_state );
return -1;
}
free( buf );
if( tr_bencDictFindInt( &top, "autostart", &tmp ) )
gl_autostart = tmp != 0;
@ -751,80 +741,42 @@ loadstate( void )
int
savestate( void )
{
benc_val_t top, * list, * tor;
benc_val_t top, * list;
struct tor * ii;
uint8_t * buf;
int len;
tr_bencInit( &top, TYPE_DICT );
if( tr_bencDictReserve( &top, 9 ) )
{
nomem:
tr_bencFree( &top );
errmsg( "failed to save state: failed to allocate memory" );
return -1;
}
tr_bencInitInt( tr_bencDictAdd( &top, "autostart" ), gl_autostart );
tr_bencInitInt( tr_bencDictAdd( &top, "port" ), gl_port );
tr_bencInitInt( tr_bencDictAdd( &top, "default-pex" ), gl_pex );
tr_bencInitInt( tr_bencDictAdd( &top, "port-mapping" ), gl_mapping );
tr_bencInitInt( tr_bencDictAdd( &top, "upload-limit" ), gl_uplimit );
tr_bencInitInt( tr_bencDictAdd( &top, "download-limit" ), gl_downlimit );
tr_bencInitStr( tr_bencDictAdd( &top, "default-directory" ),
gl_dir, -1, 1 );
if(TR_ENCRYPTION_REQUIRED == gl_crypto)
tr_bencInitStr(tr_bencDictAdd(&top, "encryption-mode"), "required", -1, 1);
else
tr_bencInitStr(tr_bencDictAdd(&top, "encryption-mode"), "preferred", -1, 1);
list = tr_bencDictAdd( &top, "torrents" );
tr_bencInit( list, TYPE_LIST );
len = 0;
int torrentCount;
torrentCount = 0;
RB_FOREACH( ii, tortree, &gl_tree )
{
len++;
}
if( tr_bencListReserve( list, len ) )
{
goto nomem;
}
++torrentCount;
tr_bencInitDict( &top, 9 );
tr_bencDictAddInt( &top, "autostart", gl_autostart );
tr_bencDictAddInt( &top, "port", gl_port );
tr_bencDictAddInt( &top, "default-pex", gl_pex );
tr_bencDictAddInt( &top, "port-mapping", gl_mapping );
tr_bencDictAddInt( &top, "upload-limit", gl_uplimit );
tr_bencDictAddInt( &top, "download-limit", gl_downlimit );
tr_bencDictAddStr( &top, "default-directory", gl_dir );
tr_bencDictAddStr( &top, "encryption-mode", TR_ENCRYPTION_REQUIRED == gl_crypto
? "required" : "preferred" );
list = tr_bencDictAddList( &top, "torrents", torrentCount );
RB_FOREACH( ii, tortree, &gl_tree )
{
const tr_info * inf;
const tr_stat * st;
tor = tr_bencListAdd( list );
assert( NULL != tor );
tr_bencInit( tor, TYPE_DICT );
inf = tr_torrentInfo( ii->tor );
st = tr_torrentStat( ii->tor );
if( tr_bencDictReserve( tor, 3 ) )
{
goto nomem;
}
tr_bencInitStr( tr_bencDictAdd( tor, "hash" ),
inf->hashString, 2 * SHA_DIGEST_LENGTH, 1 );
tr_bencInitInt( tr_bencDictAdd( tor, "paused" ),
!TR_STATUS_IS_ACTIVE( st->status ) );
tr_bencInitStr( tr_bencDictAdd( tor, "directory" ),
tr_torrentGetFolder( ii->tor ), -1, 1 );
const tr_info * inf = tr_torrentInfo( ii->tor );
const tr_stat * st = tr_torrentStat( ii->tor );
tr_benc * tor = tr_bencListAddDict( list, 3 );
tr_bencDictAddStr( tor, "hash", inf->hashString );
tr_bencDictAddInt( tor, "paused", !TR_STATUS_IS_ACTIVE( st->status ) );
tr_bencDictAddStr( tor, "directory", tr_torrentGetFolder( ii->tor ) );
}
buf = ( uint8_t * )tr_bencSave( &top, &len );
SAFEBENCFREE( &top );
if( NULL == buf )
if( tr_bencSaveFile( gl_newstate, &top ) )
{
errnomsg( "failed to save state: bencoding failed" );
errnomsg( "failed to save state: failed to write to %s", gl_newstate );
return -1;
}
if( 0 > writefile( gl_newstate, buf, len ) )
{
free( buf );
return -1;
}
free( buf );
if( 0 > rename( gl_newstate, gl_state ) )
{
errnomsg( "failed to save state: failed to rename %s to %s",

View file

@ -450,38 +450,6 @@ tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme )
return found;
}
tr_benc*
tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitInt( child, val );
return child;
}
tr_benc*
tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitStrDup( child, val );
return child;
}
tr_benc*
tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitList( child, reserveCount );
return child;
}
tr_benc*
tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitDict( child, reserveCount );
return child;
}
/***
****
***/
@ -573,14 +541,34 @@ tr_bencListAdd( tr_benc * list )
return item;
}
tr_benc *
tr_bencListAddInt( tr_benc * list, int64_t i )
tr_bencListAddInt( tr_benc * list, int64_t val )
{
tr_benc * node = tr_bencListAdd( list );
tr_bencInitInt( node, i );
tr_bencInitInt( node, val );
return node;
}
tr_benc *
tr_bencListAddStr( tr_benc * list, const char * val )
{
tr_benc * node = tr_bencListAdd( list );
tr_bencInitStrDup( node, val );
return node;
}
tr_benc*
tr_bencListAddList( tr_benc * list, int reserveCount )
{
tr_benc * child = tr_bencListAdd( list );
tr_bencInitList( child, reserveCount );
return child;
}
tr_benc*
tr_bencListAddDict( tr_benc * list, int reserveCount )
{
tr_benc * child = tr_bencListAdd( list );
tr_bencInitDict( child, reserveCount );
return child;
}
tr_benc *
tr_bencDictAdd( tr_benc * dict, const char * key )
@ -598,6 +586,41 @@ tr_bencDictAdd( tr_benc * dict, const char * key )
return itemval;
}
tr_benc*
tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitInt( child, val );
return child;
}
tr_benc*
tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitStrDup( child, val );
return child;
}
tr_benc*
tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitList( child, reserveCount );
return child;
}
tr_benc*
tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitDict( child, reserveCount );
return child;
}
tr_benc*
tr_bencDictAddRaw( tr_benc * dict, const char * key, const void * src, size_t len )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitRaw( child, src, len );
return child;
}
/***
**** BENC WALKING

View file

@ -101,12 +101,16 @@ int tr_bencListReserve( tr_benc * list, int count );
int tr_bencDictReserve( tr_benc * dict, int count );
tr_benc * tr_bencListAdd( tr_benc * list );
tr_benc * tr_bencListAddInt( tr_benc * list, int64_t val );
tr_benc * tr_bencListAddStr( tr_benc * list, const char * val );
tr_benc * tr_bencListAddList( tr_benc * list, int reserveCount );
tr_benc * tr_bencListAddDict( tr_benc * list, int reserveCount );
/* note: key must not be freed or modified while val is in use */
tr_benc * tr_bencDictAdd( tr_benc * dict, const char * key );
tr_benc * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
tr_benc * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
tr_benc * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount );
tr_benc * tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount );
tr_benc * tr_bencDictAddRaw( tr_benc * dict, const char * key, const void *, size_t len );
char* tr_bencSave( const tr_benc * val, int * len );
char* tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len );

View file

@ -296,10 +296,10 @@ ipc_initval( const struct ipc_info * session,
else
{
tr_bencInitList( pk, 3 );
tr_bencInitStr( tr_bencListAdd( pk ), MSGNAME( msg_id ), -1, 1 );
tr_bencListAddStr( pk, MSGNAME( msg_id ) );
ret = tr_bencListAdd( pk );
if( 0 < tag )
tr_bencInitInt( tr_bencListAdd( pk ), tag );
tr_bencListAddInt( pk, tag );
}
tr_bencInit( ret, benc_type );
@ -409,13 +409,11 @@ ipc_mkvers( size_t * len, const char * label )
uint8_t * ret;
tr_bencInitDict( &pk, 1 );
dict = tr_bencDictAdd( &pk, MSGNAME( IPC_MSG_VERSION ) );
tr_bencInitDict( dict, 3 );
tr_bencInitInt( tr_bencDictAdd( dict, "min" ), PROTO_VERS_MIN );
tr_bencInitInt( tr_bencDictAdd( dict, "max" ), PROTO_VERS_MAX );
dict = tr_bencDictAddDict( &pk, MSGNAME( IPC_MSG_VERSION ), 3 );
tr_bencDictAddInt( dict, "min", PROTO_VERS_MIN );
tr_bencDictAddInt( dict, "max", PROTO_VERS_MAX );
if( label )
tr_bencInitStr( tr_bencDictAdd( dict, "label" ), label, -1, 1 );
tr_bencDictAddStr( dict, "label", label );
ret = ipc_serialize( &pk, len );
SAFEBENCFREE( &pk );
@ -486,7 +484,7 @@ ipc_createInfoRequest( const struct ipc_info * session,
for( ii = 0; TORRENT_ID_VALID( ids[ii] ); ii++ ) { }
tr_bencInitList( idlist, ii );
for( ii = 0; TORRENT_ID_VALID( ids[ii] ); ii++ )
tr_bencInitInt( tr_bencListAdd( idlist ), ids[ii] );
tr_bencListAddInt( idlist, ids[ii] );
}
/* get the type name array */
@ -533,11 +531,11 @@ static void
filltracker( tr_benc * val, const tr_tracker_info * tk )
{
tr_bencInitDict( val, 4 );
tr_bencInitStr( tr_bencDictAdd( val, "address" ), tk->address, -1, 1 );
tr_bencInitInt( tr_bencDictAdd( val, "port" ), tk->port );
tr_bencInitStr( tr_bencDictAdd( val, "announce" ), tk->announce, -1, 1 );
tr_bencDictAddStr( val, "address", tk->address );
tr_bencDictAddInt( val, "port", tk->port );
tr_bencDictAddStr( val, "announce", tk->announce );
if( tk->scrape )
tr_bencInitStr( tr_bencDictAdd( val, "scrape" ), tk->scrape, -1, 1 );
tr_bencDictAddStr( val, "scrape", tk->scrape );
}
/**

View file

@ -274,7 +274,6 @@ getFileInfo( const char * topFile,
tr_benc * uninitialized_length,
tr_benc * uninitialized_path )
{
tr_benc *sub;
const char *pch, *prev;
const size_t topLen = strlen(topFile) + 1; /* +1 for '/' */
int n;
@ -298,8 +297,7 @@ getFileInfo( const char * topFile,
memcpy( buf, prev, pch-prev );
buf[pch-prev] = '\0';
sub = tr_bencListAdd( uninitialized_path );
tr_bencInitStrDup( sub, buf );
tr_bencListAddStr( uninitialized_path, buf );
prev = pch + 1;
if (!*pch)
@ -307,114 +305,72 @@ getFileInfo( const char * topFile,
}
}
static void
makeFilesList( tr_benc * list,
const tr_metainfo_builder * builder )
{
uint32_t i = 0;
tr_bencListReserve( list, builder->fileCount );
for( i=0; i<builder->fileCount; ++i )
{
tr_benc * dict = tr_bencListAdd( list );
tr_benc *length, *pathVal;
tr_bencInitDict( dict, 2 );
length = tr_bencDictAdd( dict, "length" );
pathVal = tr_bencDictAdd( dict, "path" );
getFileInfo( builder->top, &builder->files[i], length, pathVal );
}
}
static void
makeInfoDict ( tr_benc * dict,
tr_metainfo_builder * builder )
{
uint8_t * pch;
tr_benc * val;
char base[MAX_PATH_LENGTH];
tr_bencDictReserve( dict, 5 );
if ( builder->isSingleFile )
{
val = tr_bencDictAdd( dict, "length" );
tr_bencInitInt( val, builder->files[0].size );
}
else
{
val = tr_bencDictAdd( dict, "files" );
tr_bencInit( val, TYPE_LIST );
makeFilesList( val, builder );
tr_bencDictAddInt( dict, "length", builder->files[0].size );
else {
uint32_t i;
tr_benc * list = tr_bencDictAddList( dict, "files", builder->fileCount );
for( i=0; i<builder->fileCount; ++i ) {
tr_benc * dict = tr_bencListAddDict( list, 2 );
tr_benc * length = tr_bencDictAdd( dict, "length" );
tr_benc * pathVal = tr_bencDictAdd( dict, "path" );
getFileInfo( builder->top, &builder->files[i], length, pathVal );
}
}
val = tr_bencDictAdd( dict, "name" );
strlcpy( base, builder->top, sizeof( base ) );
tr_bencInitStrDup ( val, basename( base ) );
tr_bencDictAddStr( dict, "name", basename( base ) );
val = tr_bencDictAdd( dict, "piece length" );
tr_bencInitInt( val, builder->pieceSize );
tr_bencDictAddInt( dict, "piece length", builder->pieceSize );
if( ( pch = getHashInfo( builder ) ) ) {
val = tr_bencDictAdd( dict, "pieces" );
tr_bencInitStr( val, pch, SHA_DIGEST_LENGTH * builder->pieceCount, 0 );
if(( pch = getHashInfo( builder ))) {
tr_bencDictAddRaw( dict, "pieces", pch, SHA_DIGEST_LENGTH * builder->pieceCount );
tr_free( pch );
}
val = tr_bencDictAdd( dict, "private" );
tr_bencInitInt( val, builder->isPrivate ? 1 : 0 );
tr_bencDictAddInt( dict, "private", builder->isPrivate ? 1 : 0 );
}
static void
tr_realMakeMetaInfo ( tr_metainfo_builder * builder )
{
int n = 5;
tr_benc top, *val;
tr_benc top;
if ( builder->comment && *builder->comment ) ++n;
tr_bencInitDict( &top, n );
val = tr_bencDictAdd( &top, "announce" );
tr_bencInitStrDup( val, builder->announce );
tr_bencDictAddStr( &top, "announce", builder->announce );
if( tr_httpParseURL( builder->announce, -1, NULL, NULL, NULL ) )
{
builder->result = TR_MAKEMETA_URL;
}
if( !builder->result && !builder->abortFlag )
{
if( builder->comment && *builder->comment ) {
val = tr_bencDictAdd( &top, "comment" );
tr_bencInitStrDup( val, builder->comment );
}
val = tr_bencDictAdd( &top, "created by" );
tr_bencInitStrDup( val, TR_NAME "/" LONG_VERSION_STRING );
val = tr_bencDictAdd( &top, "creation date" );
tr_bencInitInt( val, time(0) );
val = tr_bencDictAdd( &top, "encoding" );
tr_bencInitStrDup( val, "UTF-8" );
val = tr_bencDictAdd( &top, "info" );
tr_bencInitDict( val, 666 );
makeInfoDict( val, builder );
if( builder->comment && *builder->comment )
tr_bencDictAddStr( &top, "comment", builder->comment );
tr_bencDictAddStr( &top, "created by", TR_NAME "/" LONG_VERSION_STRING );
tr_bencDictAddInt( &top, "creation date", time(0) );
tr_bencDictAddStr( &top, "encoding", "UTF-8" );
makeInfoDict( tr_bencDictAddDict( &top, "info", 666 ), builder );
}
/* save the file */
if ( !builder->result && !builder->abortFlag ) {
char * pch = tr_bencSave( &top, &n );
FILE * fp = fopen( builder->outputFile, "wb+" );
size_t nmemb = n;
if( !fp || ( fwrite( pch, 1, nmemb, fp ) != nmemb ) ) {
if( tr_bencSaveFile( builder->outputFile, &top ) ) {
builder->my_errno = errno;
strlcpy( builder->errfile, builder->outputFile, sizeof( builder->errfile ) );
builder->result = TR_MAKEMETA_IO_WRITE;
}
if( fp )
fclose( fp );
tr_free( pch );
}
/* cleanup */

View file

@ -109,7 +109,7 @@ saveDND( tr_benc * dict, const tr_torrent * tor )
list = tr_bencDictAddList( dict, KEY_DND, n );
for( i=0; i<n; ++i )
tr_bencInitInt( tr_bencListAdd( list ), inf->files[i].dnd ? 1 : 0 );
tr_bencListAddInt( list, inf->files[i].dnd ? 1 : 0 );
}
static uint64_t
@ -171,7 +171,7 @@ savePriorities( tr_benc * dict, const tr_torrent * tor )
list = tr_bencDictAddList( dict, KEY_PRIORITY, n );
for( i=0; i<n; ++i )
tr_bencInitInt( tr_bencListAdd( list ), inf->files[i].priority );
tr_bencListAddInt( list, inf->files[i].priority );
}
static uint64_t

View file

@ -28,14 +28,21 @@ struct tr_stats_handle
time_t startTime;
};
static void
parseCumulativeStats( tr_session_stats * setme,
const uint8_t * content,
size_t len )
static char*
getFilename( const tr_handle * handle, char * buf, size_t buflen )
{
tr_buildPath( buf, buflen, tr_getConfigDir(handle), "stats.benc", NULL );
return buf;
}
static void
loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
{
char filename[MAX_PATH_LENGTH];
tr_benc top;
if( !tr_bencLoad( content, len, &top, NULL ) )
getFilename( handle, filename, sizeof(filename) );
if( !tr_bencLoadFile( filename, &top ) )
{
int64_t i;
@ -58,28 +65,6 @@ parseCumulativeStats( tr_session_stats * setme,
}
}
static char*
getFilename( const tr_handle * handle, char * buf, size_t buflen )
{
tr_buildPath( buf, buflen, tr_getConfigDir(handle), "stats.benc", NULL );
return buf;
}
static void
loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
{
size_t len;
uint8_t * content;
char filename[MAX_PATH_LENGTH];
getFilename( handle, filename, sizeof(filename) );
content = tr_loadFile( filename, &len );
if( content != NULL )
parseCumulativeStats( setme, content, len );
tr_free( content );
}
static void
saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
{
@ -87,11 +72,11 @@ saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
tr_benc top;
tr_bencInitDict( &top, 5 );
tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), s->uploadedBytes );
tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), s->downloadedBytes );
tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), s->filesAdded );
tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), s->sessionCount );
tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), s->secondsActive );
tr_bencDictAddInt( &top, "uploaded-bytes", s->uploadedBytes );
tr_bencDictAddInt( &top, "downloaded-bytes", s->downloadedBytes );
tr_bencDictAddInt( &top, "files-added", s->filesAdded );
tr_bencDictAddInt( &top, "session-count", s->sessionCount );
tr_bencDictAddInt( &top, "seconds-active", s->secondsActive );
getFilename( handle, filename, sizeof(filename) );
tr_bencSaveFile( filename, &top );