2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-08-03 16:28:32 +00:00
|
|
|
#include <signal.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <fs_info.h>
|
|
|
|
#include <FindDirectory.h>
|
2007-07-30 15:27:52 +00:00
|
|
|
#include <kernel/OS.h>
|
|
|
|
#define BEOS_MAX_THREADS 256
|
2007-08-01 00:40:49 +00:00
|
|
|
#elif defined(WIN32)
|
2007-08-02 19:43:29 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h> /* for CSIDL_APPDATA, CSIDL_PROFILE */
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2007-11-26 20:21:52 +00:00
|
|
|
#include <unistd.h> /* getuid getpid */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-08-02 19:43:29 +00:00
|
|
|
#include "list.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "platform.h"
|
2007-07-30 15:27:52 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** THREADS
|
|
|
|
***/
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
#ifdef __BEOS__
|
|
|
|
typedef thread_id tr_thread_id;
|
|
|
|
#elif defined(WIN32)
|
|
|
|
typedef DWORD tr_thread_id;
|
|
|
|
#else
|
|
|
|
typedef pthread_t tr_thread_id;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static tr_thread_id
|
|
|
|
tr_getCurrentThread( void )
|
|
|
|
{
|
|
|
|
#ifdef __BEOS__
|
|
|
|
return find_thread( NULL );
|
|
|
|
#elif defined(WIN32)
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
#else
|
|
|
|
return pthread_self( );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tr_areThreadsEqual( tr_thread_id a, tr_thread_id b )
|
|
|
|
{
|
|
|
|
#ifdef __BEOS__
|
|
|
|
return a == b;
|
|
|
|
#elif defined(WIN32)
|
|
|
|
return a == b;
|
|
|
|
#else
|
|
|
|
return pthread_equal( a, b );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_thread
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
|
|
|
void (* func ) ( void * );
|
|
|
|
void * arg;
|
2007-09-20 16:32:01 +00:00
|
|
|
const char * name;
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
thread_id thread;
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
HANDLE thread;
|
2007-09-05 23:01:56 +00:00
|
|
|
unsigned int thread_id;
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_t thread;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
int
|
|
|
|
tr_amInThread ( const tr_thread * t )
|
|
|
|
{
|
|
|
|
return tr_areThreadsEqual( tr_getCurrentThread(), t->thread );
|
|
|
|
}
|
|
|
|
|
2007-07-31 19:56:40 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define ThreadFuncReturnType unsigned WINAPI
|
|
|
|
#else
|
|
|
|
#define ThreadFuncReturnType void
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static ThreadFuncReturnType
|
2007-07-30 15:27:52 +00:00
|
|
|
ThreadFunc( void * _t )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_thread * t = _t;
|
|
|
|
const char * name = t->name;
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
/* This is required because on BeOS, SIGINT is sent to each thread,
|
|
|
|
which kills them not nicely */
|
|
|
|
signal( SIGINT, SIG_IGN );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tr_dbg( "Thread '%s' started", name );
|
|
|
|
t->func( t->arg );
|
|
|
|
tr_dbg( "Thread '%s' exited", name );
|
2007-07-31 19:56:40 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
_endthreadex( 0 );
|
|
|
|
return 0;
|
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_thread *
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_threadNew( void (*func)(void *),
|
|
|
|
void * arg,
|
|
|
|
const char * name )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_thread * t = tr_new0( tr_thread, 1 );
|
2007-07-30 15:27:52 +00:00
|
|
|
t->func = func;
|
|
|
|
t->arg = arg;
|
2007-09-20 16:32:01 +00:00
|
|
|
t->name = name;
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
t->thread = spawn_thread( (void*)ThreadFunc, name, B_NORMAL_PRIORITY, t );
|
|
|
|
resume_thread( t->thread );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-09-05 23:01:56 +00:00
|
|
|
t->thread = (HANDLE) _beginthreadex( NULL, 0, &ThreadFunc, t, 0, &t->thread_id );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_create( &t->thread, NULL, (void * (*) (void *)) ThreadFunc, t );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
2007-09-05 23:01:56 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_threadJoin( tr_thread * t )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
|
|
|
if( t != NULL )
|
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
long exit;
|
|
|
|
wait_for_thread( t->thread, &exit );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-08-01 00:40:49 +00:00
|
|
|
WaitForSingleObject( t->thread, INFINITE );
|
2007-07-31 19:56:40 +00:00
|
|
|
CloseHandle( t->thread );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_join( t->thread, NULL );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tr_dbg( "Thread '%s' joined", t->name );
|
|
|
|
t->name = NULL;
|
|
|
|
t->func = NULL;
|
|
|
|
tr_free( t );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** LOCKS
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_lock
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
uint32_t depth;
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
sem_id lock;
|
2007-10-01 15:17:15 +00:00
|
|
|
thread_id lockThread;
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
CRITICAL_SECTION lock;
|
2007-10-01 15:17:15 +00:00
|
|
|
DWORD lockThread;
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_t lock;
|
2007-10-01 15:17:15 +00:00
|
|
|
pthread_t lockThread;
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock*
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockNew( void )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * l = tr_new0( tr_lock, 1 );
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
l->lock = create_sem( 1, "" );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
InitializeCriticalSection( &l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_init( &l->lock, NULL );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockFree( tr_lock * l )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
delete_sem( l->lock );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
DeleteCriticalSection( &l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_destroy( &l->lock );
|
|
|
|
#endif
|
|
|
|
tr_free( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockLock( tr_lock * l )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-11-16 20:38:51 +00:00
|
|
|
const tr_thread_id currentThread = tr_getCurrentThread( );
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
if( l->lockThread == currentThread )
|
|
|
|
{
|
|
|
|
++l->depth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-10-01 15:17:15 +00:00
|
|
|
acquire_sem( l->lock );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-10-01 15:17:15 +00:00
|
|
|
EnterCriticalSection( &l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2007-10-01 15:17:15 +00:00
|
|
|
pthread_mutex_lock( &l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
2007-11-16 20:38:51 +00:00
|
|
|
l->lockThread = currentThread;
|
2007-10-01 15:17:15 +00:00
|
|
|
l->depth = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_lockHave( const tr_lock * l )
|
|
|
|
{
|
|
|
|
return ( l->depth > 0 )
|
|
|
|
&& ( l->lockThread == tr_getCurrentThread() );
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockUnlock( tr_lock * l )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
assert( tr_lockHave( l ) );
|
|
|
|
|
|
|
|
if( !--l->depth )
|
|
|
|
{
|
|
|
|
l->lockThread = 0;
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-10-01 15:17:15 +00:00
|
|
|
release_sem( l->lock );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-10-01 15:17:15 +00:00
|
|
|
LeaveCriticalSection( &l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2007-10-01 15:17:15 +00:00
|
|
|
pthread_mutex_unlock( &l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** COND
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_cond
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
sem_id sem;
|
2007-08-01 23:22:01 +00:00
|
|
|
thread_id threads[BEOS_MAX_THREADS];
|
2007-07-30 15:27:52 +00:00
|
|
|
int start, end;
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-09-05 23:01:56 +00:00
|
|
|
tr_list * events;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * lock;
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_cond_t cond;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
static DWORD getContEventTLS( void )
|
|
|
|
{
|
|
|
|
static int inited = FALSE;
|
|
|
|
static DWORD event_tls;
|
|
|
|
if( !inited ) {
|
|
|
|
inited = TRUE;
|
|
|
|
event_tls = TlsAlloc();
|
|
|
|
}
|
|
|
|
return event_tls;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cond*
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_condNew( void )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cond * c = tr_new0( tr_cond, 1 );
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
c->sem = create_sem( 1, "" );
|
|
|
|
c->start = 0;
|
|
|
|
c->end = 0;
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-08-02 19:43:29 +00:00
|
|
|
c->events = NULL;
|
|
|
|
c->lock = tr_lockNew( );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_cond_init( &c->cond, NULL );
|
|
|
|
#endif
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_condWait( tr_cond * c, tr_lock * l )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
/* Keep track of that thread */
|
|
|
|
acquire_sem( c->sem );
|
|
|
|
c->threads[c->end] = find_thread( NULL );
|
|
|
|
c->end = ( c->end + 1 ) % BEOS_MAX_THREADS;
|
|
|
|
assert( c->end != c->start ); /* We hit BEOS_MAX_THREADS, arggh */
|
|
|
|
release_sem( c->sem );
|
|
|
|
|
2007-08-03 16:28:32 +00:00
|
|
|
release_sem( l->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
suspend_thread( find_thread( NULL ) ); /* Wait for signal */
|
2007-08-03 16:28:32 +00:00
|
|
|
acquire_sem( l->lock );
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-08-02 19:43:29 +00:00
|
|
|
|
|
|
|
/* get this thread's cond event */
|
|
|
|
DWORD key = getContEventTLS ( );
|
|
|
|
HANDLE hEvent = TlsGetValue( key );
|
|
|
|
if( !hEvent ) {
|
|
|
|
hEvent = CreateEvent( 0, FALSE, FALSE, 0 );
|
|
|
|
TlsSetValue( key, hEvent );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add it to the list of events waiting to be signaled */
|
|
|
|
tr_lockLock( c->lock );
|
2007-08-19 02:10:18 +00:00
|
|
|
tr_list_append( &c->events, hEvent );
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_lockUnlock( c->lock );
|
|
|
|
|
|
|
|
/* now wait for it to be signaled */
|
|
|
|
tr_lockUnlock( l );
|
|
|
|
WaitForSingleObject( hEvent, INFINITE );
|
|
|
|
tr_lockLock( l );
|
|
|
|
|
|
|
|
/* remove it from the list of events waiting to be signaled */
|
|
|
|
tr_lockLock( c->lock );
|
2007-08-19 02:10:18 +00:00
|
|
|
tr_list_remove_data( &c->events, hEvent );
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_lockUnlock( c->lock );
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
pthread_cond_wait( &c->cond, &l->lock );
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-09-20 16:32:01 +00:00
|
|
|
static int condTrySignal( tr_cond * c )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
|
|
|
if( c->start == c->end )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
thread_info info;
|
|
|
|
get_thread_info( c->threads[c->start], &info );
|
|
|
|
if( info.state == B_THREAD_SUSPENDED )
|
|
|
|
{
|
|
|
|
resume_thread( c->threads[c->start] );
|
|
|
|
c->start = ( c->start + 1 ) % BEOS_MAX_THREADS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* The thread is not suspended yet, which can happen since
|
|
|
|
* tr_condWait does not atomically suspends after releasing
|
|
|
|
* the semaphore. Wait a bit and try again. */
|
|
|
|
snooze( 5000 );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2007-08-02 19:43:29 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_condSignal( tr_cond * c )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
acquire_sem( c->sem );
|
|
|
|
condTrySignal( c );
|
|
|
|
release_sem( c->sem );
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-08-02 19:43:29 +00:00
|
|
|
|
|
|
|
tr_lockLock( c->lock );
|
|
|
|
if( c->events != NULL )
|
|
|
|
SetEvent( (HANDLE)c->events->data );
|
|
|
|
tr_lockUnlock( c->lock );
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
pthread_cond_signal( &c->cond );
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-08-02 19:43:29 +00:00
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_condBroadcast( tr_cond * c )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
acquire_sem( c->sem );
|
|
|
|
while( !condTrySignal( c ) );
|
|
|
|
release_sem( c->sem );
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-09-05 23:01:56 +00:00
|
|
|
tr_list * l;
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_lockLock( c->lock );
|
|
|
|
for( l=c->events; l!=NULL; l=l->next )
|
|
|
|
SetEvent( (HANDLE)l->data );
|
|
|
|
tr_lockUnlock( c->lock );
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
pthread_cond_broadcast( &c->cond );
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_condFree( tr_cond * c )
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-07-30 15:27:52 +00:00
|
|
|
delete_sem( c->sem );
|
2007-07-31 19:56:40 +00:00
|
|
|
#elif defined(WIN32)
|
2007-09-28 14:27:56 +00:00
|
|
|
tr_list_free( &c->events, NULL );
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_lockFree( c->lock );
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
|
|
|
pthread_cond_destroy( &c->cond );
|
|
|
|
#endif
|
|
|
|
tr_free( c );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PATHS
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#if !defined(WIN32) && !defined(__BEOS__) && !defined(__AMIGAOS4__)
|
2006-10-13 06:29:26 +00:00
|
|
|
#include <pwd.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2007-06-10 22:26:59 +00:00
|
|
|
const char *
|
|
|
|
tr_getHomeDirectory( void )
|
2006-10-13 06:29:26 +00:00
|
|
|
{
|
2007-08-02 19:43:29 +00:00
|
|
|
static char buf[MAX_PATH_LENGTH];
|
|
|
|
static int init = 0;
|
|
|
|
const char * envHome;
|
2006-10-13 06:29:26 +00:00
|
|
|
|
|
|
|
if( init )
|
2007-08-02 19:43:29 +00:00
|
|
|
return buf;
|
2006-10-13 06:29:26 +00:00
|
|
|
|
|
|
|
envHome = getenv( "HOME" );
|
2007-08-02 19:43:29 +00:00
|
|
|
if( envHome )
|
|
|
|
snprintf( buf, sizeof(buf), "%s", envHome );
|
|
|
|
else {
|
|
|
|
#ifdef WIN32
|
|
|
|
SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, buf );
|
2007-08-04 01:17:39 +00:00
|
|
|
#elif defined(__BEOS__) || defined(__AMIGAOS4__)
|
2007-08-02 19:43:29 +00:00
|
|
|
*buf = '\0';
|
|
|
|
#else
|
|
|
|
struct passwd * pw = getpwuid( getuid() );
|
2006-10-13 06:29:26 +00:00
|
|
|
endpwent();
|
2007-08-02 19:43:29 +00:00
|
|
|
if( pw != NULL )
|
|
|
|
snprintf( buf, sizeof(buf), "%s", pw->pw_dir );
|
|
|
|
#endif
|
2006-10-13 06:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init = 1;
|
2007-08-02 19:43:29 +00:00
|
|
|
return buf;
|
2007-06-10 22:26:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
static void
|
|
|
|
tr_migrateResume( const char *oldDirectory, const char *newDirectory )
|
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
DIR * dirh = opendir( oldDirectory );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
if( dirh != NULL )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
struct dirent * dirp;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
while( ( dirp = readdir( dirh ) ) )
|
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
if( !strncmp( "resume.", dirp->d_name, 7 ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
char o[MAX_PATH_LENGTH];
|
|
|
|
char n[MAX_PATH_LENGTH];
|
|
|
|
tr_buildPath( o, sizeof(o), oldDirectory, dirp->d_name, NULL );
|
|
|
|
tr_buildPath( n, sizeof(n), newDirectory, dirp->d_name, NULL );
|
|
|
|
rename( o, n );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir( dirh );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
const char *
|
|
|
|
tr_getPrefsDirectory( void )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
static char buf[MAX_PATH_LENGTH];
|
|
|
|
static int init = 0;
|
|
|
|
static size_t buflen = sizeof(buf);
|
|
|
|
const char* h;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if( init )
|
2007-06-18 19:39:52 +00:00
|
|
|
return buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
h = tr_getHomeDirectory();
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-06-18 19:39:52 +00:00
|
|
|
find_directory( B_USER_SETTINGS_DIRECTORY,
|
|
|
|
dev_for_path("/boot"), true, buf, buflen );
|
|
|
|
strcat( buf, "/Transmission" );
|
2006-07-16 19:39:23 +00:00
|
|
|
#elif defined( SYS_DARWIN )
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath ( buf, buflen, h,
|
|
|
|
"Library", "Application Support", "Transmission", NULL );
|
2006-07-23 19:39:02 +00:00
|
|
|
#elif defined(__AMIGAOS4__)
|
2007-06-18 19:39:52 +00:00
|
|
|
snprintf( buf, buflen, "PROGDIR:.transmission" );
|
2007-08-02 19:43:29 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
{
|
|
|
|
char tmp[MAX_PATH_LENGTH];
|
|
|
|
SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, tmp );
|
|
|
|
tr_buildPath( buf, sizeof(buf), tmp, "Transmission", NULL );
|
|
|
|
buflen = strlen( buf );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath ( buf, buflen, h, ".transmission", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
|
|
|
|
2007-08-01 00:40:49 +00:00
|
|
|
tr_mkdirp( buf, 0700 );
|
2006-07-16 19:39:23 +00:00
|
|
|
init = 1;
|
|
|
|
|
|
|
|
#ifdef SYS_DARWIN
|
2007-06-18 19:39:52 +00:00
|
|
|
char old[MAX_PATH_LENGTH];
|
|
|
|
tr_buildPath ( old, sizeof(old), h, ".transmission", NULL );
|
|
|
|
tr_migrateResume( old, buf );
|
2007-06-18 20:36:25 +00:00
|
|
|
rmdir( old );
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
return buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
const char *
|
|
|
|
tr_getCacheDirectory( void )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
static char buf[MAX_PATH_LENGTH];
|
2006-07-16 19:39:23 +00:00
|
|
|
static int init = 0;
|
2007-06-18 19:39:52 +00:00
|
|
|
static const size_t buflen = sizeof(buf);
|
|
|
|
const char * p;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if( init )
|
2007-06-18 19:39:52 +00:00
|
|
|
return buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
p = tr_getPrefsDirectory();
|
2007-08-04 01:17:39 +00:00
|
|
|
#if defined(__BEOS__) || defined(WIN32)
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, buflen, p, "Cache", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#elif defined( SYS_DARWIN )
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, buflen, tr_getHomeDirectory(),
|
|
|
|
"Library", "Caches", "Transmission", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, buflen, p, "cache", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
|
|
|
|
2007-08-01 00:40:49 +00:00
|
|
|
tr_mkdirp( buf, 0700 );
|
2006-07-16 19:39:23 +00:00
|
|
|
init = 1;
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
if( strcmp( p, buf ) )
|
|
|
|
tr_migrateResume( p, buf );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
return buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
const char *
|
|
|
|
tr_getTorrentsDirectory( void )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
static char buf[MAX_PATH_LENGTH];
|
2006-07-16 19:39:23 +00:00
|
|
|
static int init = 0;
|
2007-06-18 19:39:52 +00:00
|
|
|
static const size_t buflen = sizeof(buf);
|
|
|
|
const char * p;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if( init )
|
2007-06-18 19:39:52 +00:00
|
|
|
return buf;
|
|
|
|
|
|
|
|
p = tr_getPrefsDirectory ();
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-04 01:17:39 +00:00
|
|
|
#if defined(__BEOS__) || defined(WIN32)
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, buflen, p, "Torrents", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#elif defined( SYS_DARWIN )
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, buflen, p, "Torrents", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, buflen, p, "torrents", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
|
|
|
|
2007-08-01 00:40:49 +00:00
|
|
|
tr_mkdirp( buf, 0700 );
|
2006-07-16 19:39:23 +00:00
|
|
|
init = 1;
|
2007-06-18 19:39:52 +00:00
|
|
|
return buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|