2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#define _XOPEN_SOURCE 600 /* needed for recursive locks. */
|
|
|
|
#ifndef __USE_UNIX98
|
|
|
|
#define __USE_UNIX98 /* some older Linuxes need it spelt out for them */
|
2012-12-27 19:39:44 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-09-21 18:03:13 +00:00
|
|
|
#include <unistd.h> /* getuid() */
|
|
|
|
|
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2010-06-21 16:44:35 +00:00
|
|
|
#include <w32api.h>
|
|
|
|
#define WINVER WindowsXP
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <windows.h>
|
2008-10-13 22:26:02 +00:00
|
|
|
#include <shlobj.h> /* for CSIDL_APPDATA, CSIDL_MYDOCUMENTS */
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2013-10-27 19:31:36 +00:00
|
|
|
#ifdef BUILD_MAC_CLIENT
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
2009-07-09 18:18:14 +00:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <FindDirectory.h>
|
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <pthread.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
#include <libgen.h> /* dirname() */
|
2008-10-13 22:26:02 +00:00
|
|
|
#endif
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2008-07-11 04:07:14 +00:00
|
|
|
#include "list.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "session.h"
|
2014-09-21 18:03:13 +00:00
|
|
|
#include "utils.h"
|
2007-07-30 15:27:52 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
**** THREADS
|
|
|
|
***/
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2007-10-01 15:17:15 +00:00
|
|
|
typedef DWORD tr_thread_id;
|
|
|
|
#else
|
|
|
|
typedef pthread_t tr_thread_id;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static tr_thread_id
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getCurrentThread (void)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
return GetCurrentThreadId ();
|
2007-10-01 15:17:15 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
return pthread_self ();
|
2007-10-01 15:17:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_areThreadsEqual (tr_thread_id a, tr_thread_id b)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
return a == b;
|
2007-10-01 15:17:15 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
return pthread_equal (a, b) != 0;
|
2007-10-01 15:17:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief portability wrapper around OS-dependent threads */
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_thread
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
void (* func)(void *);
|
|
|
|
void * arg;
|
|
|
|
tr_thread_id thread;
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
HANDLE thread_handle;
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_amInThread (const tr_thread * t)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_areThreadsEqual (tr_getCurrentThread (), t->thread);
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#define ThreadFuncReturnType unsigned WINAPI
|
2007-07-31 19:56:40 +00:00
|
|
|
#else
|
2008-09-23 19:11:04 +00:00
|
|
|
#define ThreadFuncReturnType void
|
2007-07-31 19:56:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static ThreadFuncReturnType
|
2012-12-05 17:29:46 +00:00
|
|
|
ThreadFunc (void * _t)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_thread * t = _t;
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
t->func (t->arg);
|
2007-07-31 19:56:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (t);
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
_endthreadex (0);
|
|
|
|
return 0;
|
2007-07-31 19:56:40 +00:00
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_thread *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_threadNew (void (*func)(void *), void * arg)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_thread * t = tr_new0 (tr_thread, 1);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
t->func = func;
|
|
|
|
t->arg = arg;
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
t->thread_handle = (HANDLE) _beginthreadex (NULL, 0, &ThreadFunc, t, 0, &id);
|
|
|
|
t->thread = (DWORD) id;
|
|
|
|
}
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
pthread_create (&t->thread, NULL, (void* (*)(void*))ThreadFunc, t);
|
|
|
|
pthread_detach (t->thread);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return t;
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
/***
|
|
|
|
**** LOCKS
|
|
|
|
***/
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief portability wrapper around OS-dependent thread mutexes */
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_lock
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int depth;
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
DWORD lockThread;
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
pthread_mutex_t lock;
|
|
|
|
pthread_t lockThread;
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockNew (void)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lock * l = tr_new0 (tr_lock, 1);
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
InitializeCriticalSection (&l->lock); /* supports recursion */
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init (&attr);
|
|
|
|
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init (&l->lock, &attr);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return l;
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockFree (tr_lock * l)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
DeleteCriticalSection (&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
pthread_mutex_destroy (&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (l);
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockLock (tr_lock * l)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
EnterCriticalSection (&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
pthread_mutex_lock (&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
assert (l->depth >= 0);
|
|
|
|
assert (!l->depth || tr_areThreadsEqual (l->lockThread, tr_getCurrentThread ()));
|
|
|
|
l->lockThread = tr_getCurrentThread ();
|
|
|
|
++l->depth;
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockHave (const tr_lock * l)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return (l->depth > 0) &&
|
|
|
|
(tr_areThreadsEqual (l->lockThread, tr_getCurrentThread ()));
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockUnlock (tr_lock * l)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (l->depth > 0);
|
|
|
|
assert (tr_areThreadsEqual (l->lockThread, tr_getCurrentThread ()));
|
2007-10-01 15:17:15 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
--l->depth;
|
|
|
|
assert (l->depth >= 0);
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
LeaveCriticalSection (&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
pthread_mutex_unlock (&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PATHS
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <pwd.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
char *
|
|
|
|
win32_get_special_folder (int folder_id)
|
|
|
|
{
|
|
|
|
wchar_t path[MAX_PATH]; /* SHGetFolderPath () requires MAX_PATH */
|
|
|
|
*path = L'\0';
|
|
|
|
SHGetFolderPathW (NULL, folder_id, NULL, 0, path);
|
|
|
|
return tr_win32_native_to_utf8 (path, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-12-15 22:22:30 +00:00
|
|
|
static const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
getHomeDir (void)
|
2006-10-13 06:29:26 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * home = NULL;
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!home)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2014-09-21 18:05:14 +00:00
|
|
|
home = tr_env_get_string ("HOME", NULL);
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!home)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2014-09-21 18:03:13 +00:00
|
|
|
home = win32_get_special_folder (CSIDL_PERSONAL);
|
2007-08-02 19:43:29 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
struct passwd * pw = getpwuid (getuid ());
|
|
|
|
if (pw)
|
|
|
|
home = tr_strdup (pw->pw_dir);
|
|
|
|
endpwent ();
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!home)
|
|
|
|
home = tr_strdup ("");
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return home;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 20:08:20 +00:00
|
|
|
#if defined (__APPLE__) || defined (_WIN32)
|
2008-10-14 03:03:29 +00:00
|
|
|
#define RESUME_SUBDIR "Resume"
|
|
|
|
#define TORRENT_SUBDIR "Torrents"
|
|
|
|
#else
|
|
|
|
#define RESUME_SUBDIR "resume"
|
|
|
|
#define TORRENT_SUBDIR "torrents"
|
|
|
|
#endif
|
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_setConfigDir (tr_session * session, const char * configDir)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * path;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
session->configDir = tr_strdup (configDir);
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
path = tr_buildPath (configDir, RESUME_SUBDIR, NULL);
|
2014-09-21 17:55:39 +00:00
|
|
|
tr_sys_dir_create (path, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
session->resumeDir = path;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
path = tr_buildPath (configDir, TORRENT_SUBDIR, NULL);
|
2014-09-21 17:55:39 +00:00
|
|
|
tr_sys_dir_create (path, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
session->torrentDir = path;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionGetConfigDir (const tr_session * session)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return session->configDir;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getTorrentDir (const tr_session * session)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return session->torrentDir;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getResumeDir (const tr_session * session)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return session->resumeDir;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2007-06-18 19:39:52 +00:00
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getDefaultConfigDir (const char * appname)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * s = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!appname || !*appname)
|
|
|
|
appname = "Transmission";
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!s)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2014-09-21 18:05:14 +00:00
|
|
|
s = tr_env_get_string ("TRANSMISSION_HOME", NULL);
|
|
|
|
|
|
|
|
if (s == NULL)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2014-07-03 19:20:12 +00:00
|
|
|
#ifdef __APPLE__
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_buildPath (getHomeDir (), "Library", "Application Support", appname, NULL);
|
2014-07-04 00:00:07 +00:00
|
|
|
#elif defined (_WIN32)
|
2014-09-21 18:03:13 +00:00
|
|
|
char * appdata = win32_get_special_folder (CSIDL_APPDATA);
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_buildPath (appdata, appname, NULL);
|
2014-09-21 18:03:13 +00:00
|
|
|
tr_free (appdata);
|
2012-12-05 17:29:46 +00:00
|
|
|
#elif defined (__HAIKU__)
|
2014-09-21 18:03:13 +00:00
|
|
|
char buf[PATH_MAX];
|
2012-12-05 17:29:46 +00:00
|
|
|
find_directory (B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof (buf));
|
|
|
|
s = tr_buildPath (buf, appname, NULL);
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2014-09-21 18:05:14 +00:00
|
|
|
if ((s = tr_env_get_string ("XDG_CONFIG_HOME", NULL)))
|
|
|
|
{
|
|
|
|
s = tr_buildPath (s, appname, NULL);
|
|
|
|
tr_free (s);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
|
|
|
s = tr_buildPath (getHomeDir (), ".config", appname, NULL);
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-02-28 19:06:23 +00:00
|
|
|
|
2008-11-15 17:39:54 +00:00
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getDefaultDownloadDir (void)
|
2008-11-15 17:39:54 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * user_dir = NULL;
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (user_dir == NULL)
|
2009-04-05 13:41:38 +00:00
|
|
|
{
|
2014-09-21 18:05:14 +00:00
|
|
|
char * config_home;
|
2012-12-05 17:29:46 +00:00
|
|
|
char * config_file;
|
|
|
|
char * content;
|
|
|
|
size_t content_len;
|
|
|
|
|
|
|
|
/* figure out where to look for user-dirs.dirs */
|
2014-09-21 18:05:14 +00:00
|
|
|
config_home = tr_env_get_string ("XDG_CONFIG_HOME", NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (config_home && *config_home)
|
|
|
|
config_file = tr_buildPath (config_home, "user-dirs.dirs", NULL);
|
|
|
|
else
|
|
|
|
config_file = tr_buildPath (getHomeDir (), ".config", "user-dirs.dirs", NULL);
|
2014-09-21 18:05:14 +00:00
|
|
|
tr_free (config_home);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
/* read in user-dirs.dirs and look for the download dir entry */
|
|
|
|
content = (char *) tr_loadFile (config_file, &content_len);
|
|
|
|
if (content && content_len>0)
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * key = "XDG_DOWNLOAD_DIR=\"";
|
|
|
|
char * line = strstr (content, key);
|
|
|
|
if (line != NULL)
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * value = line + strlen (key);
|
|
|
|
char * end = strchr (value, '"');
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (end)
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
*end = '\0';
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!memcmp (value, "$HOME/", 6))
|
|
|
|
user_dir = tr_buildPath (getHomeDir (), value+6, NULL);
|
2012-12-26 23:44:33 +00:00
|
|
|
else if (!strcmp (value, "$HOME"))
|
|
|
|
user_dir = tr_strdup (getHomeDir ());
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
|
|
|
user_dir = tr_strdup (value);
|
2009-04-05 13:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (user_dir == NULL)
|
2009-07-09 18:18:14 +00:00
|
|
|
#ifdef __HAIKU__
|
2012-12-05 17:29:46 +00:00
|
|
|
user_dir = tr_buildPath (getHomeDir (), "Desktop", NULL);
|
2009-07-09 18:18:14 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
user_dir = tr_buildPath (getHomeDir (), "Downloads", NULL);
|
2009-07-09 18:18:14 +00:00
|
|
|
#endif
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (content);
|
|
|
|
tr_free (config_file);
|
2009-04-05 13:41:38 +00:00
|
|
|
}
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return user_dir;
|
2008-11-15 17:39:54 +00:00
|
|
|
}
|
|
|
|
|
2008-07-11 04:07:14 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
isWebClientDir (const char * path)
|
|
|
|
{
|
|
|
|
char * tmp = tr_buildPath (path, "index.html", NULL);
|
2014-07-08 00:08:43 +00:00
|
|
|
const bool ret = tr_sys_path_exists (tmp, NULL);
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddInfo (_("Searching for web interface file \"%s\""), tmp);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (tmp);
|
|
|
|
|
|
|
|
return ret;
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getWebClientDir (const tr_session * session UNUSED)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * s = NULL;
|
2008-07-11 04:07:14 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!s)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2014-09-21 18:05:14 +00:00
|
|
|
s = tr_env_get_string ("CLUTCH_HOME", NULL);
|
|
|
|
if (s == NULL)
|
|
|
|
s = tr_env_get_string ("TRANSMISSION_WEB_HOME", NULL);
|
|
|
|
if (s == NULL)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2013-10-27 19:31:36 +00:00
|
|
|
#ifdef BUILD_MAC_CLIENT /* on Mac, look in the Application Support folder first, then in the app bundle. */
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Look in the Application Support folder */
|
|
|
|
s = tr_buildPath (tr_sessionGetConfigDir (session), "web", NULL);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!isWebClientDir (s))
|
|
|
|
{
|
|
|
|
tr_free (s);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
CFURLRef appURL = CFBundleCopyBundleURL (CFBundleGetMainBundle ());
|
|
|
|
CFStringRef appRef = CFURLCopyFileSystemPath (appURL,
|
|
|
|
kCFURLPOSIXPathStyle);
|
|
|
|
const CFIndex appStringLength = CFStringGetMaximumSizeOfFileSystemRepresentation (appRef);
|
2011-08-15 00:10:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
char * appString = tr_malloc (appStringLength);
|
|
|
|
const bool success = CFStringGetFileSystemRepresentation (appRef, appString, appStringLength);
|
|
|
|
assert (success);
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
CFRelease (appURL);
|
|
|
|
CFRelease (appRef);
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Fallback to the app bundle */
|
|
|
|
s = tr_buildPath (appString, "Contents", "Resources", "web", NULL);
|
|
|
|
if (!isWebClientDir (s))
|
|
|
|
{
|
|
|
|
tr_free (s);
|
|
|
|
s = NULL;
|
2009-02-28 21:45:16 +00:00
|
|
|
}
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (appString);
|
2009-02-28 21:45:16 +00:00
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#elif defined (_WIN32)
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Generally, Web interface should be stored in a Web subdir of
|
|
|
|
* calling executable dir. */
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (s == NULL) /* check personal AppData/Transmission/Web */
|
|
|
|
{
|
2014-09-21 18:03:13 +00:00
|
|
|
char * dir = win32_get_special_folder (CSIDL_COMMON_APPDATA);
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_buildPath (dir, "Transmission", "Web", NULL);
|
2014-09-21 18:03:13 +00:00
|
|
|
tr_free (dir);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!isWebClientDir (s))
|
|
|
|
{
|
|
|
|
tr_free (s);
|
|
|
|
s = NULL;
|
2008-10-19 17:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (s == NULL) /* check personal AppData */
|
|
|
|
{
|
2014-09-21 18:03:13 +00:00
|
|
|
char * dir = win32_get_special_folder (CSIDL_APPDATA);
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_buildPath (dir, "Transmission", "Web", NULL);
|
2014-09-21 18:03:13 +00:00
|
|
|
tr_free (dir);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!isWebClientDir (s))
|
|
|
|
{
|
|
|
|
tr_free (s);
|
|
|
|
s = NULL;
|
2008-10-19 17:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (s == NULL) /* check calling module place */
|
|
|
|
{
|
2014-09-21 18:03:13 +00:00
|
|
|
wchar_t wide_module_path[MAX_PATH];
|
|
|
|
char * module_path;
|
|
|
|
char * dir;
|
|
|
|
GetModuleFileNameW (NULL, wide_module_path,
|
|
|
|
sizeof (wide_module_path) / sizeof (*wide_module_path));
|
|
|
|
module_path = tr_win32_native_to_utf8 (wide_module_path, -1);
|
|
|
|
dir = tr_sys_path_dirname (module_path, NULL);
|
|
|
|
tr_free (module_path);
|
|
|
|
s = tr_buildPath (dir, "Web", NULL);
|
|
|
|
tr_free (dir);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!isWebClientDir (s))
|
|
|
|
{
|
|
|
|
tr_free (s);
|
2008-10-19 17:43:04 +00:00
|
|
|
s = NULL;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 14:01:49 +00:00
|
|
|
#else /* everyone else, follow the XDG spec */
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list *candidates = NULL, *l;
|
2014-09-21 18:05:14 +00:00
|
|
|
char * tmp;
|
2008-07-11 04:07:14 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* XDG_DATA_HOME should be the first in the list of candidates */
|
2014-09-21 18:05:14 +00:00
|
|
|
tmp = tr_env_get_string ("XDG_DATA_HOME", NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tmp && *tmp)
|
|
|
|
{
|
2014-09-21 18:05:14 +00:00
|
|
|
tr_list_append (&candidates, tmp);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2008-12-14 01:19:50 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * dhome = tr_buildPath (getHomeDir (), ".local", "share", NULL);
|
|
|
|
tr_list_append (&candidates, dhome);
|
2014-09-21 18:05:14 +00:00
|
|
|
tr_free (tmp);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* XDG_DATA_DIRS are the backup directories */
|
|
|
|
{
|
|
|
|
const char * pkg = PACKAGE_DATA_DIR;
|
2014-09-21 18:05:14 +00:00
|
|
|
char * xdg = tr_env_get_string ("XDG_DATA_DIRS", NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * fallback = "/usr/local/share:/usr/share";
|
|
|
|
char * buf = tr_strdup_printf ("%s:%s:%s", (pkg?pkg:""), (xdg?xdg:""), fallback);
|
2014-09-21 18:05:14 +00:00
|
|
|
tr_free (xdg);
|
2012-12-05 17:29:46 +00:00
|
|
|
tmp = buf;
|
|
|
|
while (tmp && *tmp)
|
|
|
|
{
|
|
|
|
const char * end = strchr (tmp, ':');
|
|
|
|
if (end)
|
|
|
|
{
|
|
|
|
if ((end - tmp) > 1)
|
|
|
|
tr_list_append (&candidates, tr_strndup (tmp, end - tmp));
|
2014-09-21 18:05:14 +00:00
|
|
|
tmp = (char *) end + 1;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
else if (tmp && *tmp)
|
|
|
|
{
|
|
|
|
tr_list_append (&candidates, tr_strdup (tmp));
|
2008-07-11 04:07:14 +00:00
|
|
|
break;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tr_free (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* walk through the candidates & look for a match */
|
|
|
|
for (l=candidates; l; l=l->next)
|
|
|
|
{
|
|
|
|
char * path = tr_buildPath (l->data, "transmission", "web", NULL);
|
|
|
|
const int found = isWebClientDir (path);
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
s = path;
|
|
|
|
break;
|
2008-10-14 03:03:29 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (path);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_free (&candidates, tr_free);
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2008-07-11 04:07:14 +00:00
|
|
|
#endif
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return s;
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|