2009-01-07 06:53:29 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-01-07 06:53:29 +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.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-05-19 15:53:34 +00:00
|
|
|
#ifdef HAVE_MEMMEM
|
|
|
|
#define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
|
|
|
|
#endif
|
|
|
|
|
2010-02-23 06:06:45 +00:00
|
|
|
#if defined(SYS_DARWIN)
|
|
|
|
#define HAVE_GETPAGESIZE
|
2010-07-07 16:48:23 +00:00
|
|
|
#define HAVE_ICONV_OPEN
|
2010-02-23 06:06:45 +00:00
|
|
|
#define HAVE_VALLOC
|
|
|
|
#undef HAVE_POSIX_MEMALIGN /* not supported on OS X 10.5 and lower */
|
|
|
|
#endif
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <ctype.h> /* isdigit(), isalpha(), tolower() */
|
2007-07-19 11:54:37 +00:00
|
|
|
#include <errno.h>
|
2010-10-21 23:47:23 +00:00
|
|
|
#include <float.h> /* DBL_EPSILON */
|
|
|
|
#include <locale.h> /* localeconv() */
|
2010-10-11 15:05:24 +00:00
|
|
|
#include <math.h> /* pow(), fabs(), floor() */
|
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>
|
2010-06-30 15:05:43 +00:00
|
|
|
#include <string.h> /* strerror(), memset(), memmem() */
|
2010-07-01 03:59:06 +00:00
|
|
|
#include <time.h> /* nanosleep() */
|
2007-07-19 11:54:37 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
#ifdef HAVE_ICONV_OPEN
|
|
|
|
#include <iconv.h>
|
|
|
|
#endif
|
2010-06-30 15:05:43 +00:00
|
|
|
#include <libgen.h> /* basename() */
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <unistd.h> /* stat(), getcwd(), getpagesize(), unlink() */
|
2007-07-18 17:19:14 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/event.h>
|
2007-11-06 21:14:30 +00:00
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifdef WIN32
|
2010-06-21 16:44:35 +00:00
|
|
|
#include <w32api.h>
|
2010-06-30 15:05:43 +00:00
|
|
|
#define WINVER WindowsXP /* freeaddrinfo(), getaddrinfo(), getnameinfo() */
|
|
|
|
#include <direct.h> /* _getcwd() */
|
|
|
|
#include <windows.h> /* Sleep() */
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2010-09-18 23:06:03 +00:00
|
|
|
#include "bencode.h"
|
2009-04-26 07:59:36 +00:00
|
|
|
#include "fdlimit.h"
|
2009-01-10 22:48:58 +00:00
|
|
|
#include "ConvertUTF.h"
|
2008-12-30 20:32:00 +00:00
|
|
|
#include "list.h"
|
2007-07-19 11:54:37 +00:00
|
|
|
#include "utils.h"
|
2011-07-13 03:23:37 +00:00
|
|
|
#include "platform.h" /* tr_lockLock(), TR_PATH_MAX */
|
2009-01-25 16:14:11 +00:00
|
|
|
#include "version.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2010-12-31 01:30:35 +00:00
|
|
|
time_t __tr_current_time = 0;
|
|
|
|
tr_msg_level __tr_message_level = TR_MSG_ERR;
|
2010-03-12 19:24:45 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool messageQueuing = false;
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_msg_list * messageQueue = NULL;
|
|
|
|
static tr_msg_list ** messageQueueTail = &messageQueue;
|
2009-12-01 23:20:00 +00:00
|
|
|
static int messageQueueCount = 0;
|
2006-07-16 23:40:22 +00:00
|
|
|
|
2008-10-19 17:43:04 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* make null versions of these win32 functions */
|
2011-03-22 15:19:54 +00:00
|
|
|
static inline int IsDebuggerPresent( void ) { return false; }
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline void OutputDebugString( const void * unused UNUSED ) { }
|
2008-10-19 17:43:04 +00:00
|
|
|
#endif
|
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-09-22 15:17:16 +00:00
|
|
|
static tr_lock*
|
|
|
|
getMessageLock( void )
|
2006-08-20 18:15:25 +00:00
|
|
|
{
|
2010-09-22 15:17:16 +00:00
|
|
|
static tr_lock * l = NULL;
|
|
|
|
|
|
|
|
if( !l )
|
|
|
|
l = tr_lockNew( );
|
2008-12-29 18:11:56 +00:00
|
|
|
|
2010-09-22 15:17:16 +00:00
|
|
|
return l;
|
2006-08-20 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 18:04:23 +00:00
|
|
|
void*
|
2007-09-20 23:33:46 +00:00
|
|
|
tr_getLog( void )
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool initialized = false;
|
2008-09-23 19:11:04 +00:00
|
|
|
static FILE * file = NULL;
|
2007-09-20 23:33:46 +00:00
|
|
|
|
|
|
|
if( !initialized )
|
|
|
|
{
|
|
|
|
const char * str = getenv( "TR_DEBUG_FD" );
|
2008-09-23 19:11:04 +00:00
|
|
|
int fd = 0;
|
2007-09-20 23:33:46 +00:00
|
|
|
if( str && *str )
|
|
|
|
fd = atoi( str );
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( fd )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
file = stdout; break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
file = stderr; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
file = NULL; break;
|
2007-09-20 23:33:46 +00:00
|
|
|
}
|
2011-03-22 15:19:54 +00:00
|
|
|
initialized = true;
|
2007-09-20 23:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_setMessageLevel( tr_msg_level level )
|
2006-08-20 18:15:25 +00:00
|
|
|
{
|
2010-12-31 01:30:35 +00:00
|
|
|
__tr_message_level = level;
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
void
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_setMessageQueuing( bool enabled )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-08-22 02:12:58 +00:00
|
|
|
messageQueuing = enabled;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2008-10-21 19:40:12 +00:00
|
|
|
tr_getMessageQueuing( void )
|
|
|
|
{
|
2010-06-30 06:03:55 +00:00
|
|
|
return messageQueuing != 0;
|
2008-10-21 19:40:12 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 03:53:18 +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;
|
2010-09-22 15:17:16 +00:00
|
|
|
tr_lockLock( getMessageLock( ) );
|
2008-12-29 18:11:56 +00:00
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
ret = messageQueue;
|
|
|
|
messageQueue = NULL;
|
|
|
|
messageQueueTail = &messageQueue;
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-12-01 23:20:00 +00:00
|
|
|
messageQueueCount = 0;
|
2006-08-22 02:12:58 +00:00
|
|
|
|
2010-09-22 15:17:16 +00:00
|
|
|
tr_lockUnlock( getMessageLock( ) );
|
2006-08-22 02:12:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-13 03:53:18 +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 );
|
2008-03-13 03:53:18 +00:00
|
|
|
free( list->name );
|
2006-08-22 02:12:58 +00:00
|
|
|
free( list );
|
|
|
|
list = next;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
}
|
|
|
|
|
2008-04-25 02:57:33 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2009-03-25 18:42:39 +00:00
|
|
|
struct tm *
|
|
|
|
tr_localtime_r( const time_t *_clock, struct tm *_result )
|
2008-10-19 17:43:04 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_LOCALTIME_R
|
|
|
|
return localtime_r( _clock, _result );
|
|
|
|
#else
|
|
|
|
struct tm *p = localtime( _clock );
|
|
|
|
if( p )
|
|
|
|
*(_result) = *p;
|
|
|
|
return p;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-04-25 02:57:33 +00:00
|
|
|
char*
|
2009-01-25 16:14:11 +00:00
|
|
|
tr_getLogTimeStr( char * buf, int buflen )
|
2008-04-25 02:57:33 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char tmp[64];
|
|
|
|
struct tm now_tm;
|
2008-04-25 02:57:33 +00:00
|
|
|
struct timeval tv;
|
2010-01-29 18:07:21 +00:00
|
|
|
time_t seconds;
|
2008-09-23 19:11:04 +00:00
|
|
|
int milliseconds;
|
2008-04-25 02:57:33 +00:00
|
|
|
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
|
2010-01-29 18:07:21 +00:00
|
|
|
seconds = tv.tv_sec;
|
|
|
|
tr_localtime_r( &seconds, &now_tm );
|
2008-09-23 19:11:04 +00:00
|
|
|
strftime( tmp, sizeof( tmp ), "%H:%M:%S", &now_tm );
|
2011-02-12 17:27:47 +00:00
|
|
|
milliseconds = tv.tv_usec / 1000;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, buflen, "%s.%03d", tmp, milliseconds );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2008-10-26 15:39:04 +00:00
|
|
|
tr_deepLoggingIsActive( void )
|
|
|
|
{
|
2009-01-03 04:57:40 +00:00
|
|
|
static int8_t deepLoggingIsActive = -1;
|
2009-01-03 02:43:17 +00:00
|
|
|
|
2009-01-03 04:57:40 +00:00
|
|
|
if( deepLoggingIsActive < 0 )
|
2009-01-03 02:43:17 +00:00
|
|
|
deepLoggingIsActive = IsDebuggerPresent() || (tr_getLog()!=NULL);
|
|
|
|
|
2009-01-03 04:57:40 +00:00
|
|
|
return deepLoggingIsActive != 0;
|
2008-10-26 15:39:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-25 02:57:33 +00:00
|
|
|
void
|
2008-10-26 15:39:04 +00:00
|
|
|
tr_deepLog( const char * file,
|
|
|
|
int line,
|
|
|
|
const char * name,
|
|
|
|
const char * fmt,
|
2008-09-23 19:11:04 +00:00
|
|
|
... )
|
2008-04-25 02:57:33 +00:00
|
|
|
{
|
|
|
|
FILE * fp = tr_getLog( );
|
2008-10-14 17:57:44 +00:00
|
|
|
if( fp || IsDebuggerPresent( ) )
|
2008-04-25 02:57:33 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
va_list args;
|
|
|
|
char timestr[64];
|
2009-06-14 01:00:36 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-10-14 03:39:16 +00:00
|
|
|
char * base = tr_basename( file );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
evbuffer_add_printf( buf, "[%s] ",
|
|
|
|
tr_getLogTimeStr( timestr, sizeof( timestr ) ) );
|
2008-04-25 02:57:33 +00:00
|
|
|
if( name )
|
|
|
|
evbuffer_add_printf( buf, "%s ", name );
|
|
|
|
va_start( args, fmt );
|
|
|
|
evbuffer_add_vprintf( buf, fmt, args );
|
|
|
|
va_end( args );
|
2008-10-14 03:39:16 +00:00
|
|
|
evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
|
2009-06-02 18:21:23 +00:00
|
|
|
/* FIXME(libevent2) ifdef this out for nonwindows platforms */
|
2010-12-20 02:07:51 +00:00
|
|
|
OutputDebugString( evbuffer_pullup( buf, -1 ) );
|
2011-01-06 02:33:39 +00:00
|
|
|
if( fp )
|
2011-01-22 13:33:05 +00:00
|
|
|
fputs( (const char*)evbuffer_pullup( buf, -1 ), fp );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2008-10-14 03:39:16 +00:00
|
|
|
tr_free( base );
|
2009-06-14 01:00:36 +00:00
|
|
|
evbuffer_free( buf );
|
2008-04-25 02:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-12-29 18:11:56 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
void
|
2009-05-08 20:52:12 +00:00
|
|
|
tr_msg( const char * file, int line,
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_msg_level level,
|
|
|
|
const char * name,
|
2009-05-08 20:52:12 +00:00
|
|
|
const char * fmt, ... )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2009-05-08 20:52:12 +00:00
|
|
|
const int err = errno; /* message logging shouldn't affect errno */
|
2009-12-05 01:28:42 +00:00
|
|
|
char buf[1024];
|
2009-12-05 01:02:37 +00:00
|
|
|
va_list ap;
|
2010-09-22 15:17:16 +00:00
|
|
|
tr_lockLock( getMessageLock( ) );
|
2007-11-06 21:14:30 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
/* build the text message */
|
|
|
|
*buf = '\0';
|
|
|
|
va_start( ap, fmt );
|
|
|
|
evutil_vsnprintf( buf, sizeof( buf ), fmt, ap );
|
|
|
|
va_end( ap );
|
2007-11-06 21:14:30 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
OutputDebugString( buf );
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
if( *buf )
|
|
|
|
{
|
|
|
|
if( messageQueuing )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2009-12-05 01:02:37 +00:00
|
|
|
tr_msg_list * newmsg;
|
|
|
|
newmsg = tr_new0( tr_msg_list, 1 );
|
|
|
|
newmsg->level = level;
|
|
|
|
newmsg->when = tr_time( );
|
|
|
|
newmsg->message = tr_strdup( buf );
|
|
|
|
newmsg->file = file;
|
|
|
|
newmsg->line = line;
|
|
|
|
newmsg->name = tr_strdup( name );
|
|
|
|
|
|
|
|
*messageQueueTail = newmsg;
|
|
|
|
messageQueueTail = &newmsg->next;
|
|
|
|
++messageQueueCount;
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-12-05 01:28:42 +00:00
|
|
|
if( messageQueueCount > TR_MAX_MSG_LOG )
|
2007-11-06 23:10:04 +00:00
|
|
|
{
|
2009-12-05 01:02:37 +00:00
|
|
|
tr_msg_list * old = messageQueue;
|
|
|
|
messageQueue = old->next;
|
|
|
|
old->next = NULL;
|
|
|
|
tr_freeMessageList(old);
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
--messageQueueCount;
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
assert( messageQueueCount == TR_MAX_MSG_LOG );
|
2007-11-06 23:10:04 +00:00
|
|
|
}
|
2009-12-05 01:02:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char timestr[64];
|
|
|
|
FILE * fp;
|
2009-02-18 05:09:28 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
fp = tr_getLog( );
|
|
|
|
if( fp == NULL )
|
|
|
|
fp = stderr;
|
2009-02-18 05:09:28 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
tr_getLogTimeStr( timestr, sizeof( timestr ) );
|
2009-02-18 05:09:28 +00:00
|
|
|
|
2009-12-05 01:02:37 +00:00
|
|
|
if( name )
|
|
|
|
fprintf( fp, "[%s] %s: %s\n", timestr, name, buf );
|
|
|
|
else
|
|
|
|
fprintf( fp, "[%s] %s\n", timestr, buf );
|
|
|
|
fflush( fp );
|
2006-08-22 02:12:58 +00:00
|
|
|
}
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
|
2010-09-22 15:17:16 +00:00
|
|
|
tr_lockUnlock( getMessageLock( ) );
|
2009-05-08 20:52:12 +00:00
|
|
|
errno = err;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
void*
|
|
|
|
tr_malloc( size_t size )
|
|
|
|
{
|
|
|
|
return size ? malloc( size ) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
tr_malloc0( size_t size )
|
|
|
|
{
|
|
|
|
return size ? calloc( 1, size ) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_free( void * p )
|
|
|
|
{
|
|
|
|
if( p != NULL )
|
|
|
|
free( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
tr_memdup( const void * src, size_t byteCount )
|
|
|
|
{
|
|
|
|
return memcpy( tr_malloc( byteCount ), src, byteCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
const char*
|
|
|
|
tr_strip_positional_args( const char* str )
|
|
|
|
{
|
2009-06-23 14:43:09 +00:00
|
|
|
const char * in = str;
|
2008-08-21 21:04:57 +00:00
|
|
|
static size_t bufsize = 0;
|
|
|
|
static char * buf = NULL;
|
2011-01-27 05:00:09 +00:00
|
|
|
const size_t len = str ? strlen( str ) : 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
char * out;
|
2008-08-21 21:04:57 +00:00
|
|
|
|
2010-10-24 05:52:38 +00:00
|
|
|
if( !buf || ( bufsize < len ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2010-10-24 05:52:38 +00:00
|
|
|
bufsize = len * 2 + 1;
|
2008-08-21 21:04:57 +00:00
|
|
|
buf = tr_renew( char, buf, bufsize );
|
|
|
|
}
|
|
|
|
|
2011-01-27 18:54:25 +00:00
|
|
|
for( out = buf; str && *str; ++str )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-08-21 21:04:57 +00:00
|
|
|
*out++ = *str;
|
2010-06-25 01:13:35 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( *str == '%' ) && isdigit( str[1] ) )
|
|
|
|
{
|
2008-08-21 21:04:57 +00:00
|
|
|
const char * tmp = str + 1;
|
|
|
|
while( isdigit( *tmp ) )
|
|
|
|
++tmp;
|
|
|
|
if( *tmp == '$' )
|
2010-06-25 01:13:35 +00:00
|
|
|
str = tmp[1]=='\'' ? tmp+1 : tmp;
|
2008-08-21 21:04:57 +00:00
|
|
|
}
|
2010-06-25 01:13:35 +00:00
|
|
|
|
|
|
|
if( ( *str == '%' ) && ( str[1] == '\'' ) )
|
|
|
|
str = str + 1;
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
|
2011-01-27 18:54:25 +00:00
|
|
|
return !in || strcmp( buf, in ) ? buf : in;
|
2008-08-21 21:04:57 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2009-06-15 03:24:40 +00:00
|
|
|
void
|
2009-11-26 16:53:45 +00:00
|
|
|
tr_timerAdd( struct event * timer, int seconds, int microseconds )
|
2009-06-15 03:24:40 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2009-12-12 01:05:29 +00:00
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = microseconds;
|
|
|
|
|
|
|
|
assert( tv.tv_sec >= 0 );
|
|
|
|
assert( tv.tv_usec >= 0 );
|
|
|
|
assert( tv.tv_usec < 1000000 );
|
|
|
|
|
|
|
|
evtimer_add( timer, &tv );
|
2009-06-15 03:24:40 +00:00
|
|
|
}
|
2009-06-12 23:01:35 +00:00
|
|
|
|
2009-12-10 19:04:10 +00:00
|
|
|
void
|
|
|
|
tr_timerAddMsec( struct event * timer, int msec )
|
|
|
|
{
|
|
|
|
const int seconds = msec / 1000;
|
|
|
|
const int usec = (msec%1000) * 1000;
|
|
|
|
tr_timerAdd( timer, seconds, usec );
|
|
|
|
}
|
|
|
|
|
2009-06-12 23:01:35 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-10-15 20:15:34 +00:00
|
|
|
uint8_t *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_loadFile( const char * path,
|
|
|
|
size_t * size )
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2009-04-26 20:44:18 +00:00
|
|
|
uint8_t * buf;
|
2007-10-15 20:15:34 +00:00
|
|
|
struct stat sb;
|
2009-04-26 20:44:18 +00:00
|
|
|
int fd;
|
2009-05-07 01:05:10 +00:00
|
|
|
ssize_t n;
|
2010-06-30 16:40:19 +00:00
|
|
|
const char * const err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
|
2007-10-15 20:15:34 +00:00
|
|
|
|
|
|
|
/* try to stat the file */
|
|
|
|
errno = 0;
|
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
const int err = errno;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_dbg( err_fmt, path, tr_strerror( errno ) );
|
2008-10-03 04:49:06 +00:00
|
|
|
errno = err;
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( sb.st_mode & S_IFMT ) != S_IFREG )
|
|
|
|
{
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( err_fmt, path, _( "Not a regular file" ) );
|
2008-10-03 04:49:06 +00:00
|
|
|
errno = EISDIR;
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the torrent file into our buffer */
|
2009-04-26 20:44:18 +00:00
|
|
|
fd = tr_open_file_for_scanning( path );
|
|
|
|
if( fd < 0 )
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
const int err = errno;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( err_fmt, path, tr_strerror( errno ) );
|
2008-10-03 04:49:06 +00:00
|
|
|
errno = err;
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-27 16:12:17 +00:00
|
|
|
buf = tr_malloc( sb.st_size + 1 );
|
2008-10-03 04:49:06 +00:00
|
|
|
if( !buf )
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
const int err = errno;
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( err_fmt, path, _( "Memory allocation failed" ) );
|
2009-04-26 20:44:18 +00:00
|
|
|
tr_close_file( fd );
|
2008-10-03 04:49:06 +00:00
|
|
|
errno = err;
|
2008-03-04 02:02:25 +00:00
|
|
|
return NULL;
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
2010-06-30 16:40:19 +00:00
|
|
|
n = read( fd, buf, (size_t)sb.st_size );
|
2009-05-07 01:05:10 +00:00
|
|
|
if( n == -1 )
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
const int err = errno;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( err_fmt, path, tr_strerror( errno ) );
|
2009-04-26 20:44:18 +00:00
|
|
|
tr_close_file( fd );
|
2008-09-06 03:02:30 +00:00
|
|
|
free( buf );
|
2008-10-03 04:49:06 +00:00
|
|
|
errno = err;
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
tr_close_file( fd );
|
2009-04-28 20:42:55 +00:00
|
|
|
buf[ sb.st_size ] = '\0';
|
2007-10-15 20:15:34 +00:00
|
|
|
*size = sb.st_size;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-10-14 03:39:16 +00:00
|
|
|
char*
|
|
|
|
tr_basename( const char * path )
|
|
|
|
{
|
|
|
|
char * tmp = tr_strdup( path );
|
|
|
|
char * ret = tr_strdup( basename( tmp ) );
|
|
|
|
tr_free( tmp );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
tr_dirname( const char * path )
|
|
|
|
{
|
|
|
|
char * tmp = tr_strdup( path );
|
|
|
|
char * ret = tr_strdup( dirname( tmp ) );
|
|
|
|
tr_free( tmp );
|
|
|
|
return ret;
|
2008-10-13 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
2007-08-01 00:40:49 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_mkdir( const char * path,
|
|
|
|
int permissions
|
2007-08-01 00:40:49 +00:00
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
UNUSED
|
2007-08-01 00:40:49 +00:00
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
)
|
2007-08-01 00:40:49 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
if( path && isalpha( path[0] ) && path[1] == ':' && !path[2] )
|
2008-02-28 16:38:48 +00:00
|
|
|
return 0;
|
2007-08-01 00:40:49 +00:00
|
|
|
return mkdir( path );
|
|
|
|
#else
|
|
|
|
return mkdir( path, permissions );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_mkdirp( const char * path_in,
|
|
|
|
int permissions )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * path = tr_strdup( path_in );
|
|
|
|
char * p, * pp;
|
2006-07-16 19:39:23 +00:00
|
|
|
struct stat sb;
|
2008-09-23 19:11:04 +00:00
|
|
|
int done;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
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;
|
2008-09-23 19:11:04 +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 */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_mkdir( path, permissions ) )
|
|
|
|
{
|
2008-01-18 01:40:41 +00:00
|
|
|
const int err = errno;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( _(
|
|
|
|
"Couldn't create \"%1$s\": %2$s" ), path,
|
|
|
|
tr_strerror( err ) );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2008-01-18 01:40:41 +00:00
|
|
|
errno = err;
|
|
|
|
return -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
|
|
|
|
{
|
|
|
|
/* Node exists but isn't a folder */
|
2008-10-14 03:39:16 +00:00
|
|
|
char * buf = tr_strdup_printf( _( "File \"%s\" is in the way" ), path );
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf );
|
2008-10-14 03:39:16 +00:00
|
|
|
tr_free( buf );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2008-01-18 01:40:41 +00:00
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
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
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
char*
|
|
|
|
tr_buildPath( const char *first_element, ... )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-10-29 11:56:55 +00:00
|
|
|
size_t bufLen = 0;
|
|
|
|
const char * element;
|
|
|
|
char * buf;
|
|
|
|
char * pch;
|
|
|
|
va_list vl;
|
2008-08-11 04:40:29 +00:00
|
|
|
|
2008-10-29 11:56:55 +00:00
|
|
|
/* pass 1: allocate enough space for the string */
|
2007-06-18 19:39:52 +00:00
|
|
|
va_start( vl, first_element );
|
2008-10-29 11:56:55 +00:00
|
|
|
element = first_element;
|
|
|
|
while( element ) {
|
|
|
|
bufLen += strlen( element ) + 1;
|
2011-03-03 21:51:11 +00:00
|
|
|
element = va_arg( vl, const char* );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2008-10-30 19:47:00 +00:00
|
|
|
pch = buf = tr_new( char, bufLen );
|
2008-10-29 11:56:55 +00:00
|
|
|
va_end( vl );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-29 11:56:55 +00:00
|
|
|
/* pass 2: build the string piece by piece */
|
|
|
|
va_start( vl, first_element );
|
|
|
|
element = first_element;
|
|
|
|
while( element ) {
|
|
|
|
const size_t elementLen = strlen( element );
|
|
|
|
memcpy( pch, element, elementLen );
|
|
|
|
pch += elementLen;
|
2008-10-30 19:47:00 +00:00
|
|
|
*pch++ = TR_PATH_DELIMITER;
|
2011-03-03 21:51:11 +00:00
|
|
|
element = va_arg( vl, const char* );
|
2008-10-29 11:56:55 +00:00
|
|
|
}
|
2008-08-11 04:40:29 +00:00
|
|
|
va_end( vl );
|
2008-10-29 11:56:55 +00:00
|
|
|
|
2010-12-27 19:18:17 +00:00
|
|
|
/* terminate the string. if nonempty, eat the unwanted trailing slash */
|
2008-10-30 19:47:00 +00:00
|
|
|
if( pch != buf )
|
|
|
|
--pch;
|
2008-10-29 11:56:55 +00:00
|
|
|
*pch++ = '\0';
|
2008-10-30 19:47:00 +00:00
|
|
|
|
|
|
|
/* sanity checks & return */
|
2008-10-29 12:08:26 +00:00
|
|
|
assert( pch - buf == (off_t)bufLen );
|
2008-10-29 11:56:55 +00:00
|
|
|
return buf;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
char*
|
|
|
|
evbuffer_free_to_str( struct evbuffer * buf )
|
|
|
|
{
|
|
|
|
const size_t n = evbuffer_get_length( buf );
|
|
|
|
char * ret = tr_new( char, n + 1 );
|
|
|
|
evbuffer_copyout( buf, ret, n );
|
|
|
|
evbuffer_free( buf );
|
|
|
|
ret[n] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
char*
|
|
|
|
tr_strdup( const void * in )
|
|
|
|
{
|
|
|
|
return tr_strndup( in, in ? (int)strlen((const char *)in) : 0 );
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
char*
|
2009-01-19 14:05:43 +00:00
|
|
|
tr_strndup( const void * in, int len )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
char * out = NULL;
|
2007-07-15 19:12:54 +00:00
|
|
|
|
2007-11-26 05:03:28 +00:00
|
|
|
if( len < 0 )
|
|
|
|
{
|
|
|
|
out = tr_strdup( in );
|
|
|
|
}
|
2008-08-01 16:43:22 +00:00
|
|
|
else if( in )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +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
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-05-19 04:48:33 +00:00
|
|
|
const char*
|
2009-05-20 04:25:45 +00:00
|
|
|
tr_memmem( const char * haystack, size_t haystacklen,
|
|
|
|
const char * needle, size_t needlelen )
|
2009-05-19 04:48:33 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_MEMMEM
|
2009-05-20 04:25:45 +00:00
|
|
|
return memmem( haystack, haystacklen, needle, needlelen );
|
2009-05-19 04:48:33 +00:00
|
|
|
#else
|
2009-05-20 04:25:45 +00:00
|
|
|
size_t i;
|
2009-05-22 05:35:51 +00:00
|
|
|
if( !needlelen )
|
|
|
|
return haystack;
|
|
|
|
if( needlelen > haystacklen || !haystack || !needle )
|
|
|
|
return NULL;
|
|
|
|
for( i=0; i<=haystacklen-needlelen; ++i )
|
2009-05-20 04:30:55 +00:00
|
|
|
if( !memcmp( haystack+i, needle, needlelen ) )
|
|
|
|
return haystack+i;
|
2009-05-19 04:48:33 +00:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
char*
|
2008-12-30 03:29:09 +00:00
|
|
|
tr_strdup_printf( const char * fmt, ... )
|
2008-05-15 15:44:51 +00:00
|
|
|
{
|
2009-06-14 01:01:46 +00:00
|
|
|
va_list ap;
|
|
|
|
char * ret;
|
|
|
|
size_t len;
|
|
|
|
char statbuf[2048];
|
2008-05-15 15:44:51 +00:00
|
|
|
|
|
|
|
va_start( ap, fmt );
|
2009-06-14 01:01:46 +00:00
|
|
|
len = evutil_vsnprintf( statbuf, sizeof( statbuf ), fmt, ap );
|
2008-08-11 04:40:29 +00:00
|
|
|
va_end( ap );
|
2009-06-14 01:01:46 +00:00
|
|
|
if( len < sizeof( statbuf ) )
|
|
|
|
ret = tr_strndup( statbuf, len );
|
|
|
|
else {
|
|
|
|
ret = tr_new( char, len + 1 );
|
|
|
|
va_start( ap, fmt );
|
|
|
|
evutil_vsnprintf( ret, len + 1, fmt, ap );
|
|
|
|
va_end( ap );
|
|
|
|
}
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-19 04:16:04 +00:00
|
|
|
const char*
|
|
|
|
tr_strerror( int i )
|
|
|
|
{
|
|
|
|
const char * ret = strerror( i );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-19 04:16:04 +00:00
|
|
|
if( ret == NULL )
|
|
|
|
ret = "Unknown Error";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-03 03:01:12 +00:00
|
|
|
int
|
|
|
|
tr_strcmp0( const char * str1, const char * str2 )
|
|
|
|
{
|
|
|
|
if( str1 && str2 ) return strcmp( str1, str2 );
|
|
|
|
if( str1 ) return 1;
|
|
|
|
if( str2 ) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2010-12-08 14:57:34 +00:00
|
|
|
/* https://bugs.launchpad.net/percona-patches/+bug/526863/+attachment/1160199/+files/solaris_10_fix.patch */
|
|
|
|
char*
|
|
|
|
tr_strsep( char ** str, const char * delims )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_STRSEP
|
|
|
|
return strsep( str, delims );
|
|
|
|
#else
|
|
|
|
char *token;
|
|
|
|
|
|
|
|
if (*str == NULL) {
|
|
|
|
/* No more tokens */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
token = *str;
|
|
|
|
while (**str != '\0') {
|
|
|
|
if (strchr(delims, **str) != NULL) {
|
|
|
|
**str = '\0';
|
|
|
|
(*str)++;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
(*str)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is not another token */
|
|
|
|
*str = NULL;
|
|
|
|
|
|
|
|
return token;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
char*
|
|
|
|
tr_strstrip( char * str )
|
|
|
|
{
|
|
|
|
if( str != NULL )
|
|
|
|
{
|
|
|
|
size_t pos;
|
|
|
|
size_t len = strlen( str );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( len && isspace( str[len - 1] ) )
|
2008-09-05 19:11:30 +00:00
|
|
|
--len;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( pos = 0; pos < len && isspace( str[pos] ); )
|
2008-09-05 19:11:30 +00:00
|
|
|
++pos;
|
|
|
|
|
|
|
|
len -= pos;
|
2008-09-23 19:11:04 +00:00
|
|
|
memmove( str, str + pos, len );
|
2008-09-05 19:11:30 +00:00
|
|
|
str[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2010-06-07 14:25:31 +00:00
|
|
|
tr_str_has_suffix( const char *str, const char *suffix )
|
|
|
|
{
|
|
|
|
size_t str_len;
|
|
|
|
size_t suffix_len;
|
|
|
|
|
|
|
|
if( !str )
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2010-06-07 14:25:31 +00:00
|
|
|
if( !suffix )
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2010-06-07 14:25:31 +00:00
|
|
|
|
|
|
|
str_len = strlen( str );
|
|
|
|
suffix_len = strlen( suffix );
|
|
|
|
if( str_len < suffix_len )
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2010-06-07 14:25:31 +00:00
|
|
|
|
2011-03-13 08:21:55 +00:00
|
|
|
return !evutil_ascii_strncasecmp( str + str_len - suffix_len, suffix, suffix_len );
|
2010-06-07 14:25:31 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
uint64_t
|
2010-07-11 20:49:19 +00:00
|
|
|
tr_time_msec( void )
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-07-01 03:59:06 +00:00
|
|
|
tr_wait_msec( long int msec )
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
2008-12-02 03:57:01 +00:00
|
|
|
#ifdef WIN32
|
2010-07-01 03:59:06 +00:00
|
|
|
Sleep( (DWORD)msec );
|
2007-07-12 17:51:45 +00:00
|
|
|
#else
|
2010-07-01 03:59:06 +00:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = msec / 1000;
|
2010-07-01 16:09:47 +00:00
|
|
|
ts.tv_nsec = ( msec % 1000 ) * 1000000;
|
2010-07-01 03:59:06 +00:00
|
|
|
nanosleep( &ts, NULL );
|
2007-07-12 17:51:45 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
int
|
2009-11-25 19:26:30 +00:00
|
|
|
tr_snprintf( char * buf, size_t buflen, const char * fmt, ... )
|
2008-07-15 17:16:57 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int len;
|
2008-07-15 17:16:57 +00:00
|
|
|
va_list args;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
va_start( args, fmt );
|
|
|
|
len = evutil_vsnprintf( buf, buflen, fmt, args );
|
|
|
|
va_end( args );
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-11-08 04:11:09 +00:00
|
|
|
/*
|
2010-12-27 19:18:17 +00:00
|
|
|
* Copy src to string dst of size siz. At most siz-1 characters
|
|
|
|
* will be copied. Always NUL terminates (unless siz == 0).
|
2007-11-08 04:11:09 +00:00
|
|
|
* Returns strlen(src); if retval >= siz, truncation occurred.
|
|
|
|
*/
|
|
|
|
size_t
|
2010-12-27 19:18:17 +00:00
|
|
|
tr_strlcpy( char * dst, const void * src, size_t siz )
|
2007-11-08 04:11:09 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
#ifdef HAVE_STRLCPY
|
|
|
|
return strlcpy( dst, src, siz );
|
|
|
|
#else
|
2008-09-23 19:11:04 +00:00
|
|
|
char * d = dst;
|
2008-05-18 16:44:30 +00:00
|
|
|
const char *s = src;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t n = siz;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( s );
|
|
|
|
assert( d );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* Copy as many bytes as will fit */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( n != 0 )
|
|
|
|
{
|
|
|
|
while( --n != 0 )
|
|
|
|
{
|
|
|
|
if( ( *d++ = *s++ ) == '\0' )
|
2008-05-18 16:44:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( n == 0 )
|
|
|
|
{
|
|
|
|
if( siz != 0 )
|
2008-05-18 16:44:30 +00:00
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
2008-09-23 19:11:04 +00:00
|
|
|
while( *s++ )
|
2008-05-18 16:44:30 +00:00
|
|
|
;
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
return s - (char*)src - 1; /* count does not include NUL */
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2007-11-08 04:11:09 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 19:13:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
double
|
2010-07-01 05:14:34 +00:00
|
|
|
tr_getRatio( uint64_t numerator, uint64_t denominator )
|
2008-01-04 19:13:30 +00:00
|
|
|
{
|
|
|
|
double ratio;
|
|
|
|
|
2010-07-01 05:14:34 +00:00
|
|
|
if( denominator > 0 )
|
|
|
|
ratio = numerator / (double)denominator;
|
|
|
|
else if( numerator > 0 )
|
2008-01-04 19:13:30 +00:00
|
|
|
ratio = TR_RATIO_INF;
|
|
|
|
else
|
|
|
|
ratio = TR_RATIO_NA;
|
|
|
|
|
|
|
|
return ratio;
|
|
|
|
}
|
2008-02-25 20:21:22 +00:00
|
|
|
|
|
|
|
void
|
2009-11-20 04:38:19 +00:00
|
|
|
tr_sha1_to_hex( char * out, const uint8_t * sha1 )
|
2008-02-25 20:21:22 +00:00
|
|
|
{
|
2009-11-20 04:38:19 +00:00
|
|
|
int i;
|
2009-10-22 18:48:26 +00:00
|
|
|
static const char hex[] = "0123456789abcdef";
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
for( i=0; i<20; ++i )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-11-20 04:38:19 +00:00
|
|
|
const unsigned int val = *sha1++;
|
2008-02-25 20:21:22 +00:00
|
|
|
*out++ = hex[val >> 4];
|
|
|
|
*out++ = hex[val & 0xf];
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2008-02-25 20:21:22 +00:00
|
|
|
*out = '\0';
|
|
|
|
}
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
void
|
|
|
|
tr_hex_to_sha1( uint8_t * out, const char * in )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
for( i=0; i<20; ++i )
|
|
|
|
{
|
2010-01-21 05:10:00 +00:00
|
|
|
const int hi = strchr( hex, tolower( *in++ ) ) - hex;
|
|
|
|
const int lo = strchr( hex, tolower( *in++ ) ) - hex;
|
2009-11-20 04:38:19 +00:00
|
|
|
*out++ = (uint8_t)( (hi<<4) | lo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-24 15:58:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2010-08-21 12:47:28 +00:00
|
|
|
isValidURLChars( const char * url, int url_len )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2009-08-12 19:44:32 +00:00
|
|
|
const char * c;
|
2010-08-21 12:47:28 +00:00
|
|
|
const char * end;
|
2008-05-21 20:56:12 +00:00
|
|
|
static const char * rfc2396_valid_chars =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" /* lowalpha */
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* upalpha */
|
|
|
|
"0123456789" /* digit */
|
|
|
|
"-_.!~*'()" /* mark */
|
|
|
|
";/?:@&=+$," /* reserved */
|
|
|
|
"<>#%<\"" /* delims */
|
|
|
|
"{}|\\^[]`"; /* unwise */
|
|
|
|
|
2008-08-27 15:47:41 +00:00
|
|
|
if( url == NULL )
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2008-08-27 15:46:50 +00:00
|
|
|
|
2010-08-21 12:47:28 +00:00
|
|
|
for( c=url, end=c+url_len; c && *c && c!=end; ++c )
|
2008-05-21 20:56:12 +00:00
|
|
|
if( !strchr( rfc2396_valid_chars, *c ) )
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2008-05-21 20:56:12 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2010-02-20 15:57:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
/** @brief return true if the URL is a http or https or UDP one that Transmission understands */
|
|
|
|
bool
|
2010-02-20 15:57:05 +00:00
|
|
|
tr_urlIsValidTracker( const char * url )
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool valid;
|
2010-08-21 12:47:28 +00:00
|
|
|
const int len = url ? strlen(url) : 0;
|
2010-02-20 15:57:05 +00:00
|
|
|
|
2010-08-21 12:47:28 +00:00
|
|
|
valid = isValidURLChars( url, len )
|
2011-04-01 03:23:55 +00:00
|
|
|
&& !tr_urlParse( url, len, NULL, NULL, NULL, NULL )
|
|
|
|
&& ( !memcmp(url,"http://",7) || !memcmp(url,"https://",8) || !memcmp(url,"udp://",6) );
|
2010-02-20 15:57:05 +00:00
|
|
|
|
|
|
|
return valid;
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
/** @brief return true if the URL is a http or https or ftp or sftp one that Transmission understands */
|
|
|
|
bool
|
2010-08-21 12:47:28 +00:00
|
|
|
tr_urlIsValid( const char * url, int url_len )
|
2010-02-20 15:57:05 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool valid;
|
2010-08-21 12:47:28 +00:00
|
|
|
if( ( url_len < 0 ) && ( url != NULL ) )
|
|
|
|
url_len = strlen( url );
|
2010-02-20 15:57:05 +00:00
|
|
|
|
2010-08-21 12:47:28 +00:00
|
|
|
valid = isValidURLChars( url, url_len )
|
2011-04-01 03:23:55 +00:00
|
|
|
&& !tr_urlParse( url, url_len, NULL, NULL, NULL, NULL )
|
|
|
|
&& ( !memcmp(url,"http://",7) || !memcmp(url,"https://",8) || !memcmp(url,"ftp://",6) || !memcmp(url,"sftp://",7) );
|
2010-02-20 15:57:05 +00:00
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_addressIsIP( const char * str )
|
2010-02-01 23:52:46 +00:00
|
|
|
{
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address tmp;
|
|
|
|
return tr_address_from_string( &tmp, str );
|
2010-02-01 23:52:46 +00:00
|
|
|
}
|
|
|
|
|
2008-03-24 15:58:06 +00:00
|
|
|
int
|
2010-02-20 15:57:05 +00:00
|
|
|
tr_urlParse( const char * url_in,
|
|
|
|
int len,
|
|
|
|
char ** setme_protocol,
|
|
|
|
char ** setme_host,
|
|
|
|
int * setme_port,
|
|
|
|
char ** setme_path )
|
2008-03-24 15:58:06 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
|
|
|
int port = 0;
|
|
|
|
int n;
|
|
|
|
char * tmp;
|
|
|
|
char * pch;
|
2011-04-05 00:55:09 +00:00
|
|
|
size_t host_len;
|
|
|
|
size_t protocol_len;
|
2008-04-24 01:42:53 +00:00
|
|
|
const char * host = NULL;
|
2011-04-05 00:55:09 +00:00
|
|
|
const char * protocol = NULL;
|
2008-04-24 01:42:53 +00:00
|
|
|
const char * path = NULL;
|
|
|
|
|
|
|
|
tmp = tr_strndup( url_in, len );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( pch = strstr( tmp, "://" ) ) )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
*pch = '\0';
|
|
|
|
protocol = tmp;
|
2011-04-05 00:55:09 +00:00
|
|
|
protocol_len = pch - protocol;
|
2008-09-23 19:11:04 +00:00
|
|
|
pch += 3;
|
2009-12-10 09:13:08 +00:00
|
|
|
/*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch);*/
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( n = strcspn( pch, ":/" ) ) )
|
|
|
|
{
|
|
|
|
const int havePort = pch[n] == ':';
|
|
|
|
host = pch;
|
2011-04-05 00:55:09 +00:00
|
|
|
host_len = n;
|
2008-09-23 19:11:04 +00:00
|
|
|
pch += n;
|
2011-04-05 22:21:18 +00:00
|
|
|
if( pch && *pch )
|
|
|
|
*pch++ = '\0';
|
2008-04-24 01:42:53 +00:00
|
|
|
/*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/
|
2008-09-23 19:11:04 +00:00
|
|
|
if( havePort )
|
|
|
|
{
|
|
|
|
char * end;
|
|
|
|
port = strtol( pch, &end, 10 );
|
|
|
|
pch = end;
|
2008-04-24 01:42:53 +00:00
|
|
|
/*fprintf( stderr, "port is [%d]... what's left is [%s]\n", port, pch );*/
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
path = pch;
|
2008-04-24 01:42:53 +00:00
|
|
|
/*fprintf( stderr, "path is [%s]\n", path );*/
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2010-02-20 15:57:05 +00:00
|
|
|
err = !host || !path || !protocol;
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !err && !port )
|
|
|
|
{
|
2011-03-17 21:22:58 +00:00
|
|
|
if( !strcmp( protocol, "udp" ) ) port = 80;
|
|
|
|
else if( !strcmp( protocol, "ftp" ) ) port = 21;
|
|
|
|
else if( !strcmp( protocol, "sftp" ) ) port = 22;
|
|
|
|
else if( !strcmp( protocol, "http" ) ) port = 80;
|
|
|
|
else if( !strcmp( protocol, "https" ) ) port = 443;
|
2008-03-24 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !err )
|
|
|
|
{
|
2011-04-05 00:55:09 +00:00
|
|
|
if( setme_protocol ) *setme_protocol = tr_strndup( protocol, protocol_len );
|
2010-02-20 15:57:05 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host =
|
2011-04-05 00:55:09 +00:00
|
|
|
tr_strndup( host, host_len ); }
|
2011-04-05 22:21:18 +00:00
|
|
|
if( setme_path ){ if( !*path ) *setme_path = tr_strdup( "/" );
|
|
|
|
else if( path[0] == '/' ) *setme_path = tr_strdup( path );
|
2009-12-10 09:13:08 +00:00
|
|
|
else { ( (char*)path )[-1] = '/'; *setme_path = tr_strdup( path - 1 ); } }
|
2008-09-23 19:11:04 +00:00
|
|
|
if( setme_port ) *setme_port = port;
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
2008-03-24 15:58:06 +00:00
|
|
|
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_free( tmp );
|
|
|
|
return err;
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
|
|
|
|
char *
|
2009-12-09 12:44:23 +00:00
|
|
|
tr_base64_encode( const void * input, int length, int * setme_len )
|
2008-05-20 17:33:54 +00:00
|
|
|
{
|
2009-12-09 12:44:23 +00:00
|
|
|
int retlen = 0;
|
|
|
|
char * ret = NULL;
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2009-12-09 12:44:23 +00:00
|
|
|
if( input != NULL )
|
|
|
|
{
|
|
|
|
BIO * b64;
|
|
|
|
BIO * bmem;
|
|
|
|
BUF_MEM * bptr;
|
|
|
|
|
|
|
|
if( length < 1 )
|
2010-07-03 00:25:22 +00:00
|
|
|
length = (int)strlen( input );
|
2009-12-09 12:44:23 +00:00
|
|
|
|
|
|
|
bmem = BIO_new( BIO_s_mem( ) );
|
|
|
|
b64 = BIO_new( BIO_f_base64( ) );
|
2011-01-31 22:47:07 +00:00
|
|
|
BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL );
|
2009-12-09 12:44:23 +00:00
|
|
|
b64 = BIO_push( b64, bmem );
|
|
|
|
BIO_write( b64, input, length );
|
|
|
|
(void) BIO_flush( b64 );
|
|
|
|
BIO_get_mem_ptr( b64, &bptr );
|
|
|
|
ret = tr_strndup( bptr->data, bptr->length );
|
|
|
|
retlen = bptr->length;
|
|
|
|
BIO_free_all( b64 );
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
if( setme_len )
|
2009-12-09 12:44:23 +00:00
|
|
|
*setme_len = retlen;
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_base64_decode( const void * input,
|
|
|
|
int length,
|
|
|
|
int * setme_len )
|
2008-05-20 17:33:54 +00:00
|
|
|
{
|
|
|
|
char * ret;
|
2008-09-23 19:11:04 +00:00
|
|
|
BIO * b64;
|
|
|
|
BIO * bmem;
|
|
|
|
int retlen;
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
if( length < 1 )
|
2008-09-23 19:11:04 +00:00
|
|
|
length = strlen( input );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
ret = tr_new0( char, length );
|
|
|
|
b64 = BIO_new( BIO_f_base64( ) );
|
|
|
|
bmem = BIO_new_mem_buf( (unsigned char*)input, length );
|
|
|
|
bmem = BIO_push( b64, bmem );
|
|
|
|
retlen = BIO_read( bmem, ret, length );
|
2008-05-22 03:42:26 +00:00
|
|
|
if( !retlen )
|
|
|
|
{
|
|
|
|
/* try again, but with the BIO_FLAGS_BASE64_NO_NL flag */
|
|
|
|
BIO_free_all( bmem );
|
|
|
|
b64 = BIO_new( BIO_f_base64( ) );
|
|
|
|
BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL );
|
|
|
|
bmem = BIO_new_mem_buf( (unsigned char*)input, length );
|
|
|
|
bmem = BIO_push( b64, bmem );
|
|
|
|
retlen = BIO_read( bmem, ret, length );
|
|
|
|
}
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
if( setme_len )
|
|
|
|
*setme_len = retlen;
|
|
|
|
|
|
|
|
BIO_free_all( bmem );
|
|
|
|
return ret;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-30 20:32:00 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-10-11 15:41:27 +00:00
|
|
|
void
|
|
|
|
tr_removeElementFromArray( void * array,
|
|
|
|
unsigned int index_to_remove,
|
|
|
|
size_t sizeof_element,
|
|
|
|
size_t nmemb )
|
|
|
|
{
|
2011-03-03 21:51:11 +00:00
|
|
|
char * a = array;
|
2010-10-11 15:41:27 +00:00
|
|
|
|
|
|
|
memmove( a + sizeof_element * index_to_remove,
|
|
|
|
a + sizeof_element * ( index_to_remove + 1 ),
|
|
|
|
sizeof_element * ( --nmemb - index_to_remove ) );
|
|
|
|
}
|
|
|
|
|
2009-01-04 16:29:44 +00:00
|
|
|
int
|
|
|
|
tr_lowerBound( const void * key,
|
|
|
|
const void * base,
|
|
|
|
size_t nmemb,
|
|
|
|
size_t size,
|
|
|
|
int (* compar)(const void* key, const void* arrayMember),
|
2011-03-22 15:19:54 +00:00
|
|
|
bool * exact_match )
|
2009-01-04 16:29:44 +00:00
|
|
|
{
|
|
|
|
size_t first = 0;
|
2009-01-04 18:01:15 +00:00
|
|
|
const char * cbase = base;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool exact = false;
|
2009-01-04 16:29:44 +00:00
|
|
|
|
2009-11-01 01:35:06 +00:00
|
|
|
while( nmemb != 0 )
|
2009-01-04 16:29:44 +00:00
|
|
|
{
|
|
|
|
const size_t half = nmemb / 2;
|
|
|
|
const size_t middle = first + half;
|
2009-11-01 01:35:06 +00:00
|
|
|
const int c = compar( key, cbase + size*middle );
|
2009-01-04 16:29:44 +00:00
|
|
|
|
2009-10-31 22:16:06 +00:00
|
|
|
if( c <= 0 ) {
|
|
|
|
if( c == 0 )
|
2011-03-22 15:19:54 +00:00
|
|
|
exact = true;
|
2009-10-31 22:16:06 +00:00
|
|
|
nmemb = half;
|
|
|
|
} else {
|
2009-01-04 16:29:44 +00:00
|
|
|
first = middle + 1;
|
|
|
|
nmemb = nmemb - half - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-01 01:35:06 +00:00
|
|
|
*exact_match = exact;
|
2009-01-04 16:29:44 +00:00
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
static char*
|
|
|
|
strip_non_utf8( const char * in, size_t inlen )
|
2009-01-10 22:48:58 +00:00
|
|
|
{
|
|
|
|
const char * end;
|
2010-07-07 16:48:23 +00:00
|
|
|
const char zero = '\0';
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
while( !tr_utf8_validate( in, inlen, &end ) )
|
|
|
|
{
|
|
|
|
const int good_len = end - in;
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
evbuffer_add( buf, in, good_len );
|
|
|
|
inlen -= ( good_len + 1 );
|
|
|
|
in += ( good_len + 1 );
|
|
|
|
evbuffer_add( buf, "?", 1 );
|
|
|
|
}
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
evbuffer_add( buf, in, inlen );
|
|
|
|
evbuffer_add( buf, &zero, 1 );
|
2010-12-20 02:07:51 +00:00
|
|
|
return evbuffer_free_to_str( buf );
|
2010-07-07 16:48:23 +00:00
|
|
|
}
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
static char*
|
|
|
|
to_utf8( const char * in, size_t inlen )
|
|
|
|
{
|
|
|
|
char * ret = NULL;
|
2009-04-16 21:00:48 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
#ifdef HAVE_ICONV_OPEN
|
|
|
|
int i;
|
|
|
|
const char * encodings[] = { "CURRENT", "ISO-8859-15" };
|
|
|
|
const int encoding_count = sizeof(encodings) / sizeof(encodings[1]);
|
|
|
|
const size_t buflen = inlen*4 + 10;
|
|
|
|
char * out = tr_new( char, buflen );
|
|
|
|
|
|
|
|
for( i=0; !ret && i<encoding_count; ++i )
|
2009-01-10 22:48:58 +00:00
|
|
|
{
|
2010-07-07 16:48:23 +00:00
|
|
|
char * inbuf = (char*) in;
|
|
|
|
char * outbuf = out;
|
|
|
|
size_t inbytesleft = inlen;
|
|
|
|
size_t outbytesleft = buflen;
|
|
|
|
const char * test_encoding = encodings[i];
|
|
|
|
|
|
|
|
iconv_t cd = iconv_open( "UTF-8", test_encoding );
|
|
|
|
if( cd != (iconv_t)-1 ) {
|
|
|
|
if( iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ) != (size_t)-1 )
|
|
|
|
ret = tr_strndup( out, buflen-outbytesleft );
|
|
|
|
iconv_close( cd );
|
|
|
|
}
|
2009-04-16 21:00:48 +00:00
|
|
|
}
|
2011-03-28 03:45:31 +00:00
|
|
|
|
|
|
|
tr_free( out );
|
2010-07-07 16:48:23 +00:00
|
|
|
#endif
|
2009-04-16 21:00:48 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
if( ret == NULL )
|
|
|
|
ret = strip_non_utf8( in, inlen );
|
2009-04-16 21:00:48 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
char*
|
|
|
|
tr_utf8clean( const char * str, int max_len )
|
|
|
|
{
|
|
|
|
char * ret;
|
|
|
|
const char * end;
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
if( max_len < 0 )
|
|
|
|
max_len = (int) strlen( str );
|
|
|
|
|
|
|
|
if( tr_utf8_validate( str, max_len, &end ) )
|
|
|
|
ret = tr_strndup( str, max_len );
|
|
|
|
else
|
|
|
|
ret = to_utf8( str, max_len );
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
assert( tr_utf8_validate( ret, -1, NULL ) );
|
|
|
|
return ret;
|
|
|
|
}
|
2009-02-09 17:25:48 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct number_range
|
|
|
|
{
|
|
|
|
int low;
|
|
|
|
int high;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should be a single number (ex. "6") or a range (ex. "6-9").
|
|
|
|
* Anything else is an error and will return failure.
|
|
|
|
*/
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-02-09 17:25:48 +00:00
|
|
|
parseNumberSection( const char * str, int len, struct number_range * setme )
|
|
|
|
{
|
|
|
|
long a, b;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool success;
|
2009-02-09 17:25:48 +00:00
|
|
|
char * end;
|
|
|
|
const int error = errno;
|
|
|
|
char * tmp = tr_strndup( str, len );
|
|
|
|
|
|
|
|
errno = 0;
|
2009-02-18 05:09:28 +00:00
|
|
|
a = b = strtol( tmp, &end, 10 );
|
2009-02-09 17:25:48 +00:00
|
|
|
if( errno || ( end == tmp ) ) {
|
2011-03-22 15:19:54 +00:00
|
|
|
success = false;
|
2009-02-09 17:25:48 +00:00
|
|
|
} else if( *end != '-' ) {
|
2011-03-22 15:19:54 +00:00
|
|
|
success = true;
|
2009-02-09 17:25:48 +00:00
|
|
|
} else {
|
|
|
|
const char * pch = end + 1;
|
|
|
|
b = strtol( pch, &end, 10 );
|
|
|
|
if( errno || ( pch == end ) )
|
2011-03-22 15:19:54 +00:00
|
|
|
success = false;
|
2009-02-09 17:25:48 +00:00
|
|
|
else if( *end ) /* trailing data */
|
2011-03-22 15:19:54 +00:00
|
|
|
success = false;
|
2009-02-09 17:25:48 +00:00
|
|
|
else
|
2011-03-22 15:19:54 +00:00
|
|
|
success = true;
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
tr_free( tmp );
|
|
|
|
|
|
|
|
setme->low = MIN( a, b );
|
|
|
|
setme->high = MAX( a, b );
|
|
|
|
|
|
|
|
errno = error;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:23:48 +00:00
|
|
|
int
|
2009-02-09 17:25:48 +00:00
|
|
|
compareInt( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const int a = *(const int *)va;
|
|
|
|
const int b = *(const int *)vb;
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a string like "1-4" or "1-4,6,9,14-51", this allocates and returns an
|
|
|
|
* array of setmeCount ints of all the values in the array.
|
|
|
|
* For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
|
2009-08-10 20:04:08 +00:00
|
|
|
* It's the caller's responsibility to call tr_free() on the returned array.
|
2009-02-09 17:25:48 +00:00
|
|
|
* If a fragment of the string can't be parsed, NULL is returned.
|
|
|
|
*/
|
|
|
|
int*
|
|
|
|
tr_parseNumberRange( const char * str_in, int len, int * setmeCount )
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int * uniq = NULL;
|
|
|
|
char * str = tr_strndup( str_in, len );
|
|
|
|
const char * walk;
|
|
|
|
tr_list * ranges = NULL;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool success = true;
|
2009-02-09 17:25:48 +00:00
|
|
|
|
|
|
|
walk = str;
|
|
|
|
while( walk && *walk && success ) {
|
|
|
|
struct number_range range;
|
|
|
|
const char * pch = strchr( walk, ',' );
|
|
|
|
if( pch ) {
|
|
|
|
success = parseNumberSection( walk, pch-walk, &range );
|
|
|
|
walk = pch + 1;
|
|
|
|
} else {
|
|
|
|
success = parseNumberSection( walk, strlen( walk ), &range );
|
|
|
|
walk += strlen( walk );
|
|
|
|
}
|
|
|
|
if( success )
|
|
|
|
tr_list_append( &ranges, tr_memdup( &range, sizeof( struct number_range ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
*setmeCount = 0;
|
|
|
|
uniq = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n2;
|
|
|
|
tr_list * l;
|
|
|
|
int * sorted = NULL;
|
|
|
|
|
|
|
|
/* build a sorted number array */
|
|
|
|
n = n2 = 0;
|
|
|
|
for( l=ranges; l!=NULL; l=l->next ) {
|
|
|
|
const struct number_range * r = l->data;
|
|
|
|
n += r->high + 1 - r->low;
|
|
|
|
}
|
|
|
|
sorted = tr_new( int, n );
|
|
|
|
for( l=ranges; l!=NULL; l=l->next ) {
|
|
|
|
const struct number_range * r = l->data;
|
|
|
|
int i;
|
|
|
|
for( i=r->low; i<=r->high; ++i )
|
|
|
|
sorted[n2++] = i;
|
|
|
|
}
|
|
|
|
qsort( sorted, n, sizeof( int ), compareInt );
|
|
|
|
assert( n == n2 );
|
|
|
|
|
|
|
|
/* remove duplicates */
|
|
|
|
uniq = tr_new( int, n );
|
|
|
|
for( i=n=0; i<n2; ++i )
|
|
|
|
if( !n || uniq[n-1] != sorted[i] )
|
|
|
|
uniq[n++] = sorted[i];
|
|
|
|
|
|
|
|
tr_free( sorted );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_list_free( &ranges, tr_free );
|
|
|
|
tr_free( str );
|
|
|
|
|
|
|
|
/* return the result */
|
|
|
|
*setmeCount = n;
|
|
|
|
return uniq;
|
|
|
|
}
|
2009-03-04 16:16:02 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-07-14 20:09:46 +00:00
|
|
|
double
|
2010-10-21 23:47:23 +00:00
|
|
|
tr_truncd( double x, int precision )
|
2009-03-04 16:16:02 +00:00
|
|
|
{
|
2010-10-21 23:47:23 +00:00
|
|
|
char * pt;
|
|
|
|
char buf[128];
|
2010-11-11 15:31:11 +00:00
|
|
|
const int max_precision = (int) log10( 1.0 / DBL_EPSILON ) - 1;
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.*f", max_precision, x );
|
2010-10-21 23:47:23 +00:00
|
|
|
if(( pt = strstr( buf, localeconv()->decimal_point )))
|
|
|
|
pt[precision ? precision+1 : 0] = '\0';
|
|
|
|
return atof(buf);
|
2009-03-04 16:16:02 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 23:46:31 +00:00
|
|
|
/* return a truncated double as a string */
|
|
|
|
static char*
|
2010-06-24 17:34:56 +00:00
|
|
|
tr_strtruncd( char * buf, double x, int precision, size_t buflen )
|
|
|
|
{
|
|
|
|
tr_snprintf( buf, buflen, "%.*f", precision, tr_truncd( x, precision ) );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-06-22 22:30:58 +00:00
|
|
|
char*
|
|
|
|
tr_strpercent( char * buf, double x, size_t buflen )
|
|
|
|
{
|
|
|
|
if( x < 10.0 )
|
|
|
|
tr_strtruncd( buf, x, 2, buflen );
|
|
|
|
else if( x < 100.0 )
|
|
|
|
tr_strtruncd( buf, x, 1, buflen );
|
|
|
|
else
|
|
|
|
tr_strtruncd( buf, x, 0, buflen );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-03-04 16:16:02 +00:00
|
|
|
char*
|
|
|
|
tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity )
|
|
|
|
{
|
|
|
|
if( (int)ratio == TR_RATIO_NA )
|
|
|
|
tr_strlcpy( buf, _( "None" ), buflen );
|
|
|
|
else if( (int)ratio == TR_RATIO_INF )
|
|
|
|
tr_strlcpy( buf, infinity, buflen );
|
|
|
|
else
|
2010-06-22 22:30:58 +00:00
|
|
|
tr_strpercent( buf, ratio, buflen );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
int
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_moveFile( const char * oldpath, const char * newpath, bool * renamed )
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
|
|
|
int in;
|
|
|
|
int out;
|
|
|
|
char * buf;
|
|
|
|
struct stat st;
|
2009-10-30 03:53:41 +00:00
|
|
|
off_t bytesLeft;
|
2010-06-30 16:40:19 +00:00
|
|
|
const size_t buflen = 1024 * 128; /* 128 KiB buffer */
|
2009-05-13 15:54:04 +00:00
|
|
|
|
|
|
|
/* make sure the old file exists */
|
|
|
|
if( stat( oldpath, &st ) ) {
|
|
|
|
const int err = errno;
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if( !S_ISREG( st.st_mode ) ) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bytesLeft = st.st_size;
|
|
|
|
|
|
|
|
/* make sure the target directory exists */
|
|
|
|
{
|
|
|
|
char * newdir = tr_dirname( newpath );
|
|
|
|
int i = tr_mkdirp( newdir, 0777 );
|
|
|
|
tr_free( newdir );
|
|
|
|
if( i )
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-10-27 23:00:34 +00:00
|
|
|
/* they might be on the same filesystem... */
|
|
|
|
{
|
|
|
|
const int i = rename( oldpath, newpath );
|
|
|
|
if( renamed != NULL )
|
|
|
|
*renamed = i == 0;
|
|
|
|
if( !i )
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
|
|
|
|
/* copy the file */
|
|
|
|
in = tr_open_file_for_scanning( oldpath );
|
|
|
|
out = tr_open_file_for_writing( newpath );
|
2010-02-23 07:29:21 +00:00
|
|
|
buf = tr_valloc( buflen );
|
2009-05-13 15:54:04 +00:00
|
|
|
while( bytesLeft > 0 )
|
|
|
|
{
|
|
|
|
ssize_t bytesWritten;
|
2010-07-13 13:23:51 +00:00
|
|
|
const off_t bytesThisPass = MIN( bytesLeft, (off_t)buflen );
|
2009-05-13 15:54:04 +00:00
|
|
|
const int numRead = read( in, buf, bytesThisPass );
|
|
|
|
if( numRead < 0 )
|
|
|
|
break;
|
|
|
|
bytesWritten = write( out, buf, numRead );
|
|
|
|
if( bytesWritten < 0 )
|
|
|
|
break;
|
|
|
|
bytesLeft -= bytesWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( buf );
|
|
|
|
tr_close_file( out );
|
|
|
|
tr_close_file( in );
|
|
|
|
if( bytesLeft != 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
unlink( oldpath );
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-23 06:06:45 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2010-12-22 07:04:11 +00:00
|
|
|
tr_is_same_file( const char * filename1, const char * filename2 )
|
|
|
|
{
|
|
|
|
struct stat sb1, sb2;
|
|
|
|
|
|
|
|
return !stat( filename1, &sb1 )
|
|
|
|
&& !stat( filename2, &sb2 )
|
|
|
|
&& ( sb1.st_dev == sb2.st_dev )
|
|
|
|
&& ( sb1.st_ino == sb2.st_ino );
|
|
|
|
}
|
|
|
|
|
2010-02-23 06:06:45 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void*
|
|
|
|
tr_valloc( size_t bufLen )
|
|
|
|
{
|
|
|
|
size_t allocLen;
|
|
|
|
void * buf = NULL;
|
|
|
|
static size_t pageSize = 0;
|
|
|
|
|
|
|
|
if( !pageSize ) {
|
|
|
|
#ifdef HAVE_GETPAGESIZE
|
2010-07-03 00:25:22 +00:00
|
|
|
pageSize = (size_t) getpagesize();
|
2010-02-23 06:06:45 +00:00
|
|
|
#else /* guess */
|
|
|
|
pageSize = 4096;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
allocLen = pageSize;
|
|
|
|
while( allocLen < bufLen )
|
|
|
|
allocLen += pageSize;
|
|
|
|
|
|
|
|
#ifdef HAVE_POSIX_MEMALIGN
|
|
|
|
if( !buf )
|
2011-01-06 02:17:22 +00:00
|
|
|
if( posix_memalign( &buf, pageSize, allocLen ) )
|
|
|
|
buf = NULL; /* just retry with valloc/malloc */
|
2010-02-23 06:06:45 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_VALLOC
|
|
|
|
if( !buf )
|
|
|
|
buf = valloc( allocLen );
|
|
|
|
#endif
|
|
|
|
if( !buf )
|
2011-04-27 16:12:17 +00:00
|
|
|
buf = tr_malloc( allocLen );
|
2010-02-23 06:06:45 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2010-06-21 16:44:35 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
tr_realpath( const char * path, char * resolved_path )
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
/* From a message to the Mingw-msys list, Jun 2, 2005 by Mark Junker. */
|
|
|
|
if( GetFullPathNameA( path, TR_PATH_MAX, resolved_path, NULL ) == 0 )
|
|
|
|
return NULL;
|
|
|
|
return resolved_path;
|
|
|
|
#else
|
|
|
|
return realpath( path, resolved_path );
|
|
|
|
#endif
|
|
|
|
}
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
uint64_t
|
2011-03-13 20:23:23 +00:00
|
|
|
tr_htonll( uint64_t x )
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2011-03-13 20:23:23 +00:00
|
|
|
#ifdef HAVE_HTONLL
|
|
|
|
return htonll( x );
|
2011-03-13 00:18:11 +00:00
|
|
|
#else
|
2011-03-13 20:39:08 +00:00
|
|
|
/* fallback code by Runner and Juan Carlos Cobas at
|
|
|
|
* http://www.codeproject.com/KB/cpp/endianness.aspx */
|
2011-03-28 13:37:46 +00:00
|
|
|
return (((uint64_t)(htonl((int)((x << 32) >> 32))) << 32) |
|
2011-03-13 20:23:23 +00:00
|
|
|
(unsigned int)htonl(((int)(x >> 32))));
|
2011-03-13 00:18:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2011-03-13 20:23:23 +00:00
|
|
|
tr_ntohll( uint64_t x )
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2011-03-13 20:23:23 +00:00
|
|
|
#ifdef HAVE_NTOHLL
|
|
|
|
return ntohll( x );
|
2011-03-13 00:18:11 +00:00
|
|
|
#else
|
2011-03-13 20:39:08 +00:00
|
|
|
/* fallback code by Runner and Juan Carlos Cobas at
|
|
|
|
* http://www.codeproject.com/KB/cpp/endianness.aspx */
|
2011-03-28 13:37:46 +00:00
|
|
|
return (((uint64_t)(ntohl((int)((x << 32) >> 32))) << 32) |
|
2011-03-13 20:23:23 +00:00
|
|
|
(unsigned int)ntohl(((int)(x >> 32))));
|
2011-03-13 00:18:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-06-22 13:34:46 +00:00
|
|
|
struct formatter_unit
|
|
|
|
{
|
|
|
|
char * name;
|
2011-01-19 18:14:01 +00:00
|
|
|
int64_t value;
|
2010-06-22 13:34:46 +00:00
|
|
|
};
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
struct formatter_units
|
|
|
|
{
|
2010-06-22 13:34:46 +00:00
|
|
|
struct formatter_unit units[4];
|
2010-06-22 04:34:16 +00:00
|
|
|
};
|
|
|
|
|
2010-07-06 20:25:54 +00:00
|
|
|
enum { TR_FMT_KB, TR_FMT_MB, TR_FMT_GB, TR_FMT_TB };
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
static void
|
|
|
|
formatter_init( struct formatter_units * units,
|
2010-07-03 00:25:22 +00:00
|
|
|
unsigned int kilo,
|
2010-07-06 20:25:54 +00:00
|
|
|
const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb )
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2010-07-10 02:05:25 +00:00
|
|
|
uint64_t value = kilo;
|
2010-06-22 13:34:46 +00:00
|
|
|
units->units[TR_FMT_KB].name = tr_strdup( kb );
|
2010-07-06 20:25:54 +00:00
|
|
|
units->units[TR_FMT_KB].value = value;
|
2010-06-22 13:34:46 +00:00
|
|
|
|
2010-07-06 20:25:54 +00:00
|
|
|
value *= kilo;
|
2010-06-22 13:34:46 +00:00
|
|
|
units->units[TR_FMT_MB].name = tr_strdup( mb );
|
2010-07-06 20:25:54 +00:00
|
|
|
units->units[TR_FMT_MB].value = value;
|
2010-06-22 13:34:46 +00:00
|
|
|
|
2010-07-06 20:25:54 +00:00
|
|
|
value *= kilo;
|
2010-06-22 13:34:46 +00:00
|
|
|
units->units[TR_FMT_GB].name = tr_strdup( gb );
|
2010-07-06 20:25:54 +00:00
|
|
|
units->units[TR_FMT_GB].value = value;
|
|
|
|
|
|
|
|
value *= kilo;
|
|
|
|
units->units[TR_FMT_TB].name = tr_strdup( tb );
|
|
|
|
units->units[TR_FMT_TB].value = value;
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
formatter_get_size_str( const struct formatter_units * u,
|
2011-01-19 18:14:01 +00:00
|
|
|
char * buf, int64_t bytes, size_t buflen )
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
|
|
|
int precision;
|
2010-06-22 13:34:46 +00:00
|
|
|
double value;
|
2010-06-22 04:34:16 +00:00
|
|
|
const char * units;
|
2010-06-22 13:34:46 +00:00
|
|
|
const struct formatter_unit * unit;
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-06-22 13:34:46 +00:00
|
|
|
if( bytes < u->units[1].value ) unit = &u->units[0];
|
|
|
|
else if( bytes < u->units[2].value ) unit = &u->units[1];
|
|
|
|
else if( bytes < u->units[3].value ) unit = &u->units[2];
|
|
|
|
else unit = &u->units[3];
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
value = (double)bytes / unit->value;
|
2010-06-22 13:34:46 +00:00
|
|
|
units = unit->name;
|
2010-06-23 20:14:30 +00:00
|
|
|
if( unit->value == 1 )
|
|
|
|
precision = 0;
|
|
|
|
else if( value < 100 )
|
|
|
|
precision = 2;
|
|
|
|
else
|
|
|
|
precision = 1;
|
2010-06-22 13:34:46 +00:00
|
|
|
tr_snprintf( buf, buflen, "%.*f %s", precision, value, units );
|
2010-06-22 04:34:16 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-06-22 13:34:46 +00:00
|
|
|
static struct formatter_units size_units;
|
2010-06-22 04:34:16 +00:00
|
|
|
|
|
|
|
void
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_formatter_size_init( unsigned int kilo,
|
2010-07-06 20:52:54 +00:00
|
|
|
const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb )
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2010-07-06 20:52:54 +00:00
|
|
|
formatter_init( &size_units, kilo, kb, mb, gb, tb );
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2011-01-19 18:14:01 +00:00
|
|
|
tr_formatter_size_B( char * buf, int64_t bytes, size_t buflen )
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
|
|
|
return formatter_get_size_str( &size_units, buf, bytes, buflen );
|
|
|
|
}
|
|
|
|
|
2010-06-22 13:34:46 +00:00
|
|
|
static struct formatter_units speed_units;
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
unsigned int tr_speed_K = 0u;
|
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
void
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_formatter_speed_init( unsigned int kilo,
|
2010-07-06 20:52:54 +00:00
|
|
|
const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb )
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_speed_K = kilo;
|
2010-07-06 20:52:54 +00:00
|
|
|
formatter_init( &speed_units, kilo, kb, mb, gb, tb );
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2010-07-09 20:53:38 +00:00
|
|
|
tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2010-07-08 03:20:07 +00:00
|
|
|
const double K = speed_units.units[TR_FMT_KB].value;
|
2010-07-09 20:53:38 +00:00
|
|
|
double speed = KBps;
|
2010-07-08 03:20:07 +00:00
|
|
|
|
|
|
|
if( speed <= 999.95 ) /* 0.0 KB to 999.9 KB */
|
2011-07-17 14:15:02 +00:00
|
|
|
tr_snprintf( buf, buflen, "%d %s", (int)speed, speed_units.units[TR_FMT_KB].name );
|
2010-07-08 03:20:07 +00:00
|
|
|
else {
|
|
|
|
speed /= K;
|
|
|
|
if( speed <= 99.995 ) /* 0.98 MB to 99.99 MB */
|
|
|
|
tr_snprintf( buf, buflen, "%.2f %s", speed, speed_units.units[TR_FMT_MB].name );
|
|
|
|
else if (speed <= 999.95) /* 100.0 MB to 999.9 MB */
|
|
|
|
tr_snprintf( buf, buflen, "%.1f %s", speed, speed_units.units[TR_FMT_MB].name );
|
|
|
|
else {
|
|
|
|
speed /= K;
|
|
|
|
tr_snprintf( buf, buflen, "%.1f %s", speed, speed_units.units[TR_FMT_GB].name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
return buf;
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct formatter_units mem_units;
|
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
unsigned int tr_mem_K = 0u;
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
void
|
|
|
|
tr_formatter_mem_init( unsigned int kilo,
|
2010-07-06 20:52:54 +00:00
|
|
|
const char * kb, const char * mb,
|
|
|
|
const char * gb, const char * tb )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_mem_K = kilo;
|
2010-07-06 20:52:54 +00:00
|
|
|
formatter_init( &mem_units, kilo, kb, mb, gb, tb );
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2011-01-19 18:14:01 +00:00
|
|
|
tr_formatter_mem_B( char * buf, int64_t bytes_per_second, size_t buflen )
|
2010-06-22 13:34:46 +00:00
|
|
|
{
|
2010-07-03 00:25:22 +00:00
|
|
|
return formatter_get_size_str( &mem_units, buf, bytes_per_second, buflen );
|
2010-06-22 13:34:46 +00:00
|
|
|
}
|
2010-09-18 23:06:03 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_formatter_get_units( tr_benc * d )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tr_benc * l;
|
|
|
|
|
|
|
|
tr_bencDictReserve( d, 6 );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, "memory-bytes", mem_units.units[TR_FMT_KB].value );
|
|
|
|
l = tr_bencDictAddList( d, "memory-units", 4 );
|
|
|
|
for( i=0; i<4; i++ ) tr_bencListAddStr( l, mem_units.units[i].name );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, "size-bytes", size_units.units[TR_FMT_KB].value );
|
|
|
|
l = tr_bencDictAddList( d, "size-units", 4 );
|
|
|
|
for( i=0; i<4; i++ ) tr_bencListAddStr( l, size_units.units[i].name );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, "speed-bytes", speed_units.units[TR_FMT_KB].value );
|
|
|
|
l = tr_bencDictAddList( d, "speed-units", 4 );
|
|
|
|
for( i=0; i<4; i++ ) tr_bencListAddStr( l, speed_units.units[i].name );
|
|
|
|
}
|
|
|
|
|