2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-03-23 08:28:01 +00:00
|
|
|
* Copyright (c) 2005-2007 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <assert.h>
|
2007-06-18 03:40:41 +00:00
|
|
|
#include <ctype.h>
|
2007-07-19 11:54:37 +00:00
|
|
|
#include <errno.h>
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <stdarg.h>
|
2007-07-19 11:54:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h> /* usleep, stat */
|
2007-07-18 17:19:14 +00:00
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h> /* for Sleep */
|
2007-08-04 01:17:39 +00:00
|
|
|
#elif defined(__BEOS__)
|
|
|
|
#include <kernel/OS.h>
|
2007-08-03 16:28:32 +00:00
|
|
|
extern int vasprintf( char **, const char *, va_list );
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-18 17:19:14 +00:00
|
|
|
#include "trcompat.h"
|
2007-07-19 11:54:37 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "platform.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
#define SPRINTF_BUFSIZE 100
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_lock * messageLock = NULL;
|
|
|
|
static int messageLevel = 0;
|
|
|
|
static int messageQueuing = FALSE;
|
|
|
|
static tr_msg_list * messageQueue = NULL;
|
|
|
|
static tr_msg_list ** messageQueueTail = &messageQueue;
|
2006-07-16 23:40:22 +00:00
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
void tr_msgInit( void )
|
2006-08-20 18:15:25 +00:00
|
|
|
{
|
2007-07-30 15:27:52 +00:00
|
|
|
if( !messageLock )
|
|
|
|
messageLock = tr_lockNew( );
|
2006-08-20 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
FILE*
|
|
|
|
tr_getLog( void )
|
|
|
|
{
|
|
|
|
static int initialized = FALSE;
|
|
|
|
static FILE * file= NULL;
|
|
|
|
|
|
|
|
if( !initialized )
|
|
|
|
{
|
|
|
|
const char * str = getenv( "TR_DEBUG_FD" );
|
|
|
|
int fd;
|
|
|
|
if( str && *str )
|
|
|
|
fd = atoi( str );
|
|
|
|
switch( fd ) {
|
|
|
|
case 1: file = stdout; break;
|
|
|
|
case 2: file = stderr; break;
|
|
|
|
default: file = NULL; break;
|
|
|
|
}
|
|
|
|
initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
char*
|
|
|
|
tr_getLogTimeStr( char * buf, int buflen )
|
|
|
|
{
|
|
|
|
char tmp[64];
|
|
|
|
time_t now;
|
|
|
|
struct tm now_tm;
|
|
|
|
struct timeval tv;
|
|
|
|
int milliseconds;
|
|
|
|
|
|
|
|
now = time( NULL );
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
|
|
|
|
localtime_r( &now, &now_tm );
|
|
|
|
strftime( tmp, sizeof(tmp), "%H:%M:%S", &now_tm );
|
|
|
|
milliseconds = (int)(tv.tv_usec / 1000);
|
|
|
|
snprintf( buf, buflen, "%s.%03d", tmp, milliseconds );
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
void
|
|
|
|
tr_setMessageLevel( int level )
|
2006-08-20 18:15:25 +00:00
|
|
|
{
|
2006-08-30 16:35:33 +00:00
|
|
|
tr_msgInit();
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
messageLevel = MAX( 0, level );
|
|
|
|
tr_lockUnlock( messageLock );
|
2006-08-20 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tr_getMessageLevel( void )
|
2006-07-16 23:40:22 +00:00
|
|
|
{
|
2006-08-22 02:12:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-08-30 16:35:33 +00:00
|
|
|
tr_msgInit();
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
ret = messageLevel;
|
|
|
|
tr_lockUnlock( messageLock );
|
|
|
|
|
|
|
|
return ret;
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
void tr_setMessageQueuing( int enabled )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-08-30 16:35:33 +00:00
|
|
|
tr_msgInit();
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
messageQueuing = enabled;
|
|
|
|
tr_lockUnlock( messageLock );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_msg_list * tr_getQueuedMessages( void )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_msg_list * ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-08-30 16:35:33 +00:00
|
|
|
assert( NULL != messageLock );
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
ret = messageQueue;
|
|
|
|
messageQueue = NULL;
|
|
|
|
messageQueueTail = &messageQueue;
|
|
|
|
tr_lockUnlock( messageLock );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_freeMessageList( tr_msg_list * list )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_msg_list * next;
|
2006-08-22 02:12:58 +00:00
|
|
|
|
|
|
|
while( NULL != list )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-08-22 02:12:58 +00:00
|
|
|
next = list->next;
|
|
|
|
free( list->message );
|
|
|
|
free( list );
|
|
|
|
list = next;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
}
|
|
|
|
|
2007-10-17 18:24:53 +00:00
|
|
|
void tr_msg( int level, const char * msg, ... )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
va_list args1, args2;
|
|
|
|
tr_msg_list * newmsg;
|
2007-09-20 23:33:46 +00:00
|
|
|
int len1, len2;
|
|
|
|
FILE * fp;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-08-30 16:35:33 +00:00
|
|
|
assert( NULL != messageLock );
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
2006-07-16 23:40:22 +00:00
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
fp = tr_getLog( );
|
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
if( !messageLevel )
|
2006-07-16 23:40:22 +00:00
|
|
|
{
|
2006-08-22 02:12:58 +00:00
|
|
|
char * env;
|
|
|
|
env = getenv( "TR_DEBUG" );
|
|
|
|
messageLevel = ( env ? atoi( env ) : 0 ) + 1;
|
|
|
|
messageLevel = MAX( 1, messageLevel );
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
|
|
|
|
if( messageLevel >= level )
|
2006-07-16 23:40:22 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
va_start( args1, msg );
|
2006-08-22 02:12:58 +00:00
|
|
|
if( messageQueuing )
|
|
|
|
{
|
|
|
|
newmsg = calloc( 1, sizeof( *newmsg ) );
|
|
|
|
if( NULL != newmsg )
|
|
|
|
{
|
|
|
|
newmsg->level = level;
|
2006-08-22 02:32:46 +00:00
|
|
|
newmsg->when = time( NULL );
|
2006-09-25 18:37:45 +00:00
|
|
|
len1 = len2 = 0;
|
|
|
|
va_start( args2, msg );
|
|
|
|
tr_vsprintf( &newmsg->message, &len1, &len2, msg,
|
|
|
|
args1, args2 );
|
|
|
|
va_end( args2 );
|
2007-09-20 23:33:46 +00:00
|
|
|
if( fp != NULL )
|
|
|
|
fprintf( fp, "%s\n", newmsg->message );
|
2006-08-22 02:12:58 +00:00
|
|
|
if( NULL == newmsg->message )
|
|
|
|
{
|
|
|
|
free( newmsg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*messageQueueTail = newmsg;
|
|
|
|
messageQueueTail = &newmsg->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-20 23:33:46 +00:00
|
|
|
if( fp == NULL )
|
|
|
|
fp = stderr;
|
|
|
|
vfprintf( fp, msg, args1 );
|
|
|
|
fputc( '\n', fp );
|
|
|
|
fflush( fp );
|
2006-08-22 02:12:58 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
va_end( args1 );
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
|
|
|
|
tr_lockUnlock( messageLock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tr_rand( int sup )
|
|
|
|
{
|
|
|
|
static int init = 0;
|
2007-07-26 03:57:45 +00:00
|
|
|
|
|
|
|
assert( sup > 0 );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( !init )
|
|
|
|
{
|
|
|
|
srand( tr_date() );
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
return rand() % sup;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_set_compare( const void * va, size_t aCount,
|
|
|
|
const void * vb, size_t bCount,
|
|
|
|
int compare( const void * a, const void * b ),
|
|
|
|
size_t elementSize,
|
|
|
|
tr_set_func in_a_cb,
|
|
|
|
tr_set_func in_b_cb,
|
|
|
|
tr_set_func in_both_cb,
|
|
|
|
void * userData )
|
|
|
|
{
|
|
|
|
const uint8_t * a = (const uint8_t *) va;
|
|
|
|
const uint8_t * b = (const uint8_t *) vb;
|
|
|
|
const uint8_t * aend = a + elementSize*aCount;
|
|
|
|
const uint8_t * bend = b + elementSize*bCount;
|
|
|
|
|
|
|
|
while( a!=aend || b!=bend )
|
|
|
|
{
|
|
|
|
if( a==aend )
|
|
|
|
{
|
|
|
|
(*in_b_cb)( (void*)b, userData );
|
|
|
|
b += elementSize;
|
|
|
|
}
|
|
|
|
else if ( b==bend )
|
|
|
|
{
|
|
|
|
(*in_a_cb)( (void*)a, userData );
|
|
|
|
a += elementSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const int val = (*compare)( a, b );
|
|
|
|
|
|
|
|
if( !val )
|
|
|
|
{
|
|
|
|
(*in_both_cb)( (void*)a, userData );
|
|
|
|
a += elementSize;
|
|
|
|
b += elementSize;
|
|
|
|
}
|
|
|
|
else if( val < 0 )
|
|
|
|
{
|
|
|
|
(*in_a_cb)( (void*)a, userData );
|
|
|
|
a += elementSize;
|
|
|
|
}
|
|
|
|
else if( val > 0 )
|
|
|
|
{
|
|
|
|
(*in_b_cb)( (void*)b, userData );
|
|
|
|
b += elementSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-15 22:18:49 +00:00
|
|
|
void * tr_memmem ( const void *vbig, size_t big_len,
|
|
|
|
const void *vlittle, size_t little_len )
|
|
|
|
{
|
|
|
|
const char *big = vbig;
|
|
|
|
const char *little = vlittle;
|
|
|
|
size_t ii, jj;
|
|
|
|
|
|
|
|
if( 0 == big_len || 0 == little_len )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ii = 0; ii + little_len <= big_len; ii++ )
|
|
|
|
{
|
|
|
|
for( jj = 0; jj < little_len; jj++ )
|
|
|
|
{
|
|
|
|
if( big[ii + jj] != little[jj] )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( jj == little_len )
|
|
|
|
{
|
|
|
|
return (char*)big + ii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_compareUint16( uint16_t a, uint16_t b )
|
|
|
|
{
|
|
|
|
if( a < b ) return -1;
|
|
|
|
if( a > b ) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_compareUint32( uint32_t a, uint32_t b )
|
|
|
|
{
|
|
|
|
if( a < b ) return -1;
|
|
|
|
if( a > b ) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-08-23 20:33:01 +00:00
|
|
|
struct timeval
|
2007-10-18 18:32:58 +00:00
|
|
|
timevalMsec( uint64_t milliseconds )
|
2007-08-23 20:33:01 +00:00
|
|
|
{
|
|
|
|
struct timeval ret;
|
2007-10-18 18:32:58 +00:00
|
|
|
const uint64_t microseconds = milliseconds * 1000;
|
2007-08-23 20:33:01 +00:00
|
|
|
ret.tv_sec = microseconds / 1000000;
|
|
|
|
ret.tv_usec = microseconds % 1000000;
|
|
|
|
return ret;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-15 20:15:34 +00:00
|
|
|
uint8_t *
|
|
|
|
tr_loadFile( const char * path, size_t * size )
|
|
|
|
{
|
|
|
|
uint8_t * buf;
|
|
|
|
struct stat sb;
|
|
|
|
FILE * file;
|
|
|
|
|
|
|
|
/* try to stat the file */
|
|
|
|
errno = 0;
|
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
|
|
|
tr_err( "Couldn't get information for file \"%s\" %s", path, strerror(errno) );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( sb.st_mode & S_IFMT ) != S_IFREG )
|
|
|
|
{
|
|
|
|
tr_err( "Not a regular file (%s)", path );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the torrent file into our buffer */
|
|
|
|
file = fopen( path, "rb" );
|
|
|
|
if( !file )
|
|
|
|
{
|
|
|
|
tr_err( "Couldn't open file \"%s\" %s", path, strerror(errno) );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = malloc( sb.st_size );
|
|
|
|
if( NULL == buf )
|
|
|
|
{
|
|
|
|
tr_err( "Couldn't allocate memory (%"PRIu64" bytes)",
|
|
|
|
( uint64_t )sb.st_size );
|
|
|
|
fclose( file );
|
|
|
|
}
|
|
|
|
fseek( file, 0, SEEK_SET );
|
|
|
|
if( fread( buf, sb.st_size, 1, file ) != 1 )
|
|
|
|
{
|
|
|
|
tr_err( "Error reading \"%s\" %s", path, strerror(errno) );
|
|
|
|
free( buf );
|
|
|
|
fclose( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fclose( file );
|
|
|
|
|
|
|
|
*size = sb.st_size;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-08-01 00:40:49 +00:00
|
|
|
int
|
|
|
|
tr_mkdir( const char * path, int permissions
|
|
|
|
#ifdef WIN32
|
|
|
|
UNUSED
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return mkdir( path );
|
|
|
|
#else
|
|
|
|
return mkdir( path, permissions );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_mkdirp( const char * path_in, int permissions )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
char * path = tr_strdup( path_in );
|
|
|
|
char * p, * pp;
|
2006-07-16 19:39:23 +00:00
|
|
|
struct stat sb;
|
|
|
|
int done;
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/* walk past the root */
|
2006-07-16 19:39:23 +00:00
|
|
|
p = path;
|
2007-10-30 18:35:06 +00:00
|
|
|
while( *p == TR_PATH_DELIMITER )
|
|
|
|
++p;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
pp = p;
|
|
|
|
done = 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
while( ( p = strchr( pp, TR_PATH_DELIMITER ) ) || ( p = strchr( pp, '\0' ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
if( !*p )
|
2006-07-16 19:39:23 +00:00
|
|
|
done = 1;
|
|
|
|
else
|
|
|
|
*p = '\0';
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
|
|
|
/* Folder doesn't exist yet */
|
2007-08-01 00:40:49 +00:00
|
|
|
if( tr_mkdir( path, permissions ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tr_err( "Could not create directory %s (%s)", path,
|
|
|
|
strerror( errno ) );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2006-07-16 19:39:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
|
|
|
|
{
|
|
|
|
/* Node exists but isn't a folder */
|
|
|
|
tr_err( "Remove %s, it's in the way.", path );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2006-07-16 19:39:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( done )
|
|
|
|
break;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
*p = TR_PATH_DELIMITER;
|
2006-07-16 19:39:23 +00:00
|
|
|
p++;
|
|
|
|
pp = p;
|
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2006-07-16 19:39:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
int
|
|
|
|
tr_strncasecmp( const char * s1, const char * s2, size_t n )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
if ( !n )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while( n-- != 0 && tolower( *s1 ) == tolower( *s2 ) ) {
|
|
|
|
if( !n || !*s1 || !*s2 )
|
|
|
|
break;
|
|
|
|
++s1;
|
|
|
|
++s2;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tr_sprintf( char ** buf, int * used, int * max, const char * format, ... )
|
|
|
|
{
|
|
|
|
va_list ap1, ap2;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start( ap1, format );
|
|
|
|
va_start( ap2, format );
|
|
|
|
ret = tr_vsprintf( buf, used, max, format, ap1, ap2 );
|
|
|
|
va_end( ap2 );
|
|
|
|
va_end( ap1 );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tr_vsprintf( char ** buf, int * used, int * max, const char * fmt,
|
|
|
|
va_list ap1, va_list ap2 )
|
|
|
|
{
|
|
|
|
int want;
|
|
|
|
|
|
|
|
want = vsnprintf( NULL, 0, fmt, ap1 );
|
|
|
|
|
2007-01-28 23:26:57 +00:00
|
|
|
if( tr_concat( buf, used, max, NULL, want ) )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-01-28 23:26:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
assert( *used + want + 1 <= *max );
|
|
|
|
|
|
|
|
*used += vsnprintf( *buf + *used, *max - *used, fmt, ap2 );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-18 17:19:14 +00:00
|
|
|
#ifndef HAVE_ASPRINTF
|
|
|
|
|
|
|
|
int
|
|
|
|
asprintf( char ** buf, const char * format, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start( ap, format );
|
|
|
|
ret = vasprintf( buf, format, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vasprintf( char ** buf, const char * format, va_list ap )
|
|
|
|
{
|
|
|
|
va_list ap2;
|
2007-07-18 22:11:47 +00:00
|
|
|
int used, max;
|
2007-07-18 17:19:14 +00:00
|
|
|
|
|
|
|
va_copy( ap2, ap );
|
|
|
|
|
|
|
|
*buf = NULL;
|
|
|
|
used = 0;
|
|
|
|
max = 0;
|
|
|
|
|
|
|
|
if( tr_vsprintf( buf, &used, &max, format, ap, ap2 ) )
|
|
|
|
{
|
|
|
|
free( *buf );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_ASPRINTF */
|
|
|
|
|
2007-01-28 23:26:57 +00:00
|
|
|
int tr_concat( char ** buf, int * used, int * max, const char * data, int len )
|
|
|
|
{
|
|
|
|
int newmax;
|
|
|
|
char * newbuf;
|
|
|
|
|
|
|
|
newmax = *max;
|
|
|
|
while( *used + len + 1 > newmax )
|
|
|
|
{
|
|
|
|
newmax += SPRINTF_BUFSIZE;
|
|
|
|
}
|
|
|
|
if( newmax > *max )
|
|
|
|
{
|
|
|
|
newbuf = realloc( *buf, newmax );
|
2006-09-25 18:37:45 +00:00
|
|
|
if( NULL == newbuf )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*buf = newbuf;
|
2007-01-28 23:26:57 +00:00
|
|
|
*max = newmax;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-01-28 23:26:57 +00:00
|
|
|
if( NULL != data )
|
|
|
|
{
|
|
|
|
memcpy( *buf + *used, data, len );
|
|
|
|
*used += len;
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
void
|
|
|
|
tr_buildPath ( char *buf, size_t buflen, const char *first_element, ... )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
va_list vl;
|
|
|
|
char* walk = buf;
|
|
|
|
const char * element = first_element;
|
2007-08-03 16:28:32 +00:00
|
|
|
|
|
|
|
if( first_element == NULL )
|
|
|
|
return;
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
va_start( vl, first_element );
|
|
|
|
for( ;; ) {
|
|
|
|
const size_t n = strlen( element );
|
|
|
|
memcpy( walk, element, n );
|
|
|
|
walk += n;
|
|
|
|
element = (const char*) va_arg( vl, const char* );
|
|
|
|
if( element == NULL )
|
|
|
|
break;
|
|
|
|
*walk++ = TR_PATH_DELIMITER;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-06-18 19:39:52 +00:00
|
|
|
*walk = '\0';
|
|
|
|
assert( walk-buf <= (int)buflen );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-04-20 02:05:07 +00:00
|
|
|
int
|
|
|
|
tr_ioErrorFromErrno( void )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
switch( errno )
|
2007-04-20 02:05:07 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
case EACCES:
|
|
|
|
case EROFS:
|
|
|
|
return TR_ERROR_IO_PERMISSIONS;
|
|
|
|
case ENOSPC:
|
|
|
|
return TR_ERROR_IO_SPACE;
|
|
|
|
case EMFILE:
|
2007-07-01 01:50:14 +00:00
|
|
|
return TR_ERROR_IO_OPEN_FILES;
|
2007-08-13 15:40:45 +00:00
|
|
|
case EFBIG:
|
|
|
|
return TR_ERROR_IO_FILE_TOO_BIG;
|
2007-06-18 03:40:41 +00:00
|
|
|
default:
|
|
|
|
tr_dbg( "generic i/o errno from errno: %s", strerror( errno ) );
|
|
|
|
return TR_ERROR_IO_OTHER;
|
2007-04-20 02:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
char *
|
|
|
|
tr_errorString( int code )
|
|
|
|
{
|
|
|
|
switch( code )
|
|
|
|
{
|
|
|
|
case TR_OK:
|
|
|
|
return "No error";
|
|
|
|
case TR_ERROR:
|
|
|
|
return "Generic error";
|
|
|
|
case TR_ERROR_ASSERT:
|
|
|
|
return "Assert error";
|
|
|
|
case TR_ERROR_IO_PERMISSIONS:
|
2007-01-14 16:57:31 +00:00
|
|
|
return "Insufficient permissions";
|
2007-04-20 02:05:07 +00:00
|
|
|
case TR_ERROR_IO_SPACE:
|
|
|
|
return "Insufficient free space";
|
2007-05-11 18:56:59 +00:00
|
|
|
case TR_ERROR_IO_DUP_DOWNLOAD:
|
2007-05-05 14:43:52 +00:00
|
|
|
return "Already active transfer with same name and download folder";
|
2007-07-01 01:50:14 +00:00
|
|
|
case TR_ERROR_IO_FILE_TOO_BIG:
|
|
|
|
return "File too large";
|
|
|
|
case TR_ERROR_IO_OPEN_FILES:
|
|
|
|
return "Too many open files";
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_ERROR_IO_OTHER:
|
|
|
|
return "Generic I/O error";
|
|
|
|
}
|
|
|
|
return "Unknown error";
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
char*
|
|
|
|
tr_strdup( const char * in )
|
|
|
|
{
|
|
|
|
return tr_strndup( in, in ? strlen(in) : 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
tr_strndup( const char * in, int len )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
char * out = NULL;
|
2007-07-15 19:12:54 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
if( in != NULL )
|
|
|
|
{
|
2007-07-15 19:12:54 +00:00
|
|
|
out = tr_malloc( len+1 );
|
2007-06-18 03:40:41 +00:00
|
|
|
memcpy( out, in, len );
|
2007-07-15 19:12:54 +00:00
|
|
|
out[len] = '\0';
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
void*
|
|
|
|
tr_calloc( size_t nmemb, size_t size )
|
|
|
|
{
|
|
|
|
return nmemb && size ? calloc( nmemb, size ) : NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
void*
|
|
|
|
tr_malloc( size_t size )
|
|
|
|
{
|
|
|
|
return size ? malloc( size ) : NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
void*
|
|
|
|
tr_malloc0( size_t size )
|
|
|
|
{
|
|
|
|
void * ret = tr_malloc( size );
|
|
|
|
memset( ret, 0, size );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-04 20:31:19 +00:00
|
|
|
void
|
|
|
|
tr_free( void * p )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
if( p )
|
|
|
|
free( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
/* note that the argument is how many bits are needed, not bytes */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield*
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldNew( size_t bitcount )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) );
|
2007-06-18 03:40:41 +00:00
|
|
|
if( NULL == ret )
|
|
|
|
return NULL;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
ret->len = (bitcount+7u) / 8u;
|
2007-06-18 03:40:41 +00:00
|
|
|
ret->bits = calloc( ret->len, 1 );
|
|
|
|
if( NULL == ret->bits ) {
|
|
|
|
free( ret );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield*
|
|
|
|
tr_bitfieldDup( const tr_bitfield * in )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) );
|
2007-06-18 03:40:41 +00:00
|
|
|
ret->len = in->len;
|
|
|
|
ret->bits = malloc( ret->len );
|
|
|
|
memcpy( ret->bits, in->bits, ret->len );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_bitfieldFree( tr_bitfield * bitfield )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
if( bitfield )
|
|
|
|
{
|
|
|
|
free( bitfield->bits );
|
|
|
|
free( bitfield );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldClear( tr_bitfield * bitfield )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
memset( bitfield->bits, 0, bitfield->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldIsEmpty( const tr_bitfield * bitfield )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for( i=0; i<bitfield->len; ++i )
|
2007-07-14 04:26:30 +00:00
|
|
|
if( bitfield->bits[i] )
|
2007-06-18 03:40:41 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-25 17:43:39 +00:00
|
|
|
int
|
|
|
|
tr_bitfieldHas( const tr_bitfield * bitfield, size_t nth )
|
|
|
|
{
|
|
|
|
static const uint8_t ands[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
|
|
|
|
return bitfield!=NULL && (bitfield->bits[nth>>3u] & ands[nth&7u] );
|
|
|
|
}
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldAdd( tr_bitfield * bitfield, size_t nth )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-25 17:43:39 +00:00
|
|
|
static const uint8_t ands[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
|
|
|
|
bitfield->bits[nth>>3u] |= ands[nth&7u];
|
2007-10-31 04:23:51 +00:00
|
|
|
assert( tr_bitfieldHas( bitfield, nth ) );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldAddRange( tr_bitfield * bitfield,
|
|
|
|
size_t begin,
|
|
|
|
size_t end )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
/* TODO: there are faster ways to do this */
|
|
|
|
unsigned int i;
|
2007-07-15 03:52:51 +00:00
|
|
|
for( i=begin; i<end; ++i )
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldAdd( bitfield, i );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldRem( tr_bitfield * bitfield,
|
|
|
|
size_t nth )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-25 17:43:39 +00:00
|
|
|
static const uint8_t rems[8] = { 127, 191, 223, 239, 247, 251, 253, 254 };
|
|
|
|
|
2007-06-30 21:11:17 +00:00
|
|
|
if( bitfield != NULL )
|
2007-09-25 17:43:39 +00:00
|
|
|
bitfield->bits[nth>>3u] &= rems[nth&7u];
|
2007-10-31 04:23:51 +00:00
|
|
|
|
|
|
|
assert( !tr_bitfieldHas( bitfield, nth ) );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldRemRange ( tr_bitfield * b,
|
|
|
|
size_t begin,
|
|
|
|
size_t end )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
/* TODO: there are faster ways to do this */
|
|
|
|
unsigned int i;
|
2007-07-15 03:52:51 +00:00
|
|
|
for( i=begin; i<end; ++i )
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldRem( b, i );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield*
|
|
|
|
tr_bitfieldAnd( tr_bitfield * a, const tr_bitfield * b )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
uint8_t *ait;
|
|
|
|
const uint8_t *aend, *bit;
|
|
|
|
|
|
|
|
assert( a->len == b->len );
|
|
|
|
|
|
|
|
for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; ++ait, ++bit )
|
|
|
|
*ait &= *bit;
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldCountTrueBits( const tr_bitfield* b )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
const uint8_t *it, *end;
|
|
|
|
static const int trueBitCount[512] = {
|
|
|
|
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
|
|
|
|
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
|
|
|
|
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
|
|
|
|
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
|
|
|
|
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
|
|
|
|
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
|
|
|
|
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
|
|
|
|
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
|
|
|
|
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
|
|
|
|
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
|
|
|
|
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
|
|
|
|
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
|
|
|
|
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
|
|
|
|
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
|
|
|
|
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
|
|
|
|
4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9
|
|
|
|
};
|
|
|
|
|
2007-07-24 01:33:59 +00:00
|
|
|
if( !b )
|
|
|
|
return 0;
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
for( it=b->bits, end=it+b->len; it!=end; ++it )
|
|
|
|
ret += trueBitCount[*it];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-07-12 17:51:45 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_date( void )
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_wait( uint64_t delay_milliseconds )
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-08-02 19:43:29 +00:00
|
|
|
snooze( 1000 * delay_milliseconds );
|
|
|
|
#elif defined(WIN32)
|
|
|
|
Sleep( (DWORD)delay_milliseconds );
|
2007-07-12 17:51:45 +00:00
|
|
|
#else
|
2007-08-02 19:43:29 +00:00
|
|
|
usleep( 1000 * delay_milliseconds );
|
2007-07-12 17:51:45 +00:00
|
|
|
#endif
|
|
|
|
}
|