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
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define _XOPEN_SOURCE 600 /* needed for recursive locks. */
|
2013-07-08 17:07:31 +00:00
|
|
|
#ifndef __USE_UNIX98
|
2017-04-19 12:04:45 +00:00
|
|
|
#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
|
|
|
|
|
|
|
#ifdef __HAIKU__
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <limits.h> /* PATH_MAX */
|
2014-09-21 18:03:13 +00:00
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <process.h> /* _beginthreadex(), _endthreadex() */
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <windows.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <shlobj.h> /* SHGetKnownFolderPath(), FOLDERID_... */
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2014-12-13 15:22:39 +00:00
|
|
|
#include <unistd.h> /* getuid() */
|
2017-04-19 12:04:45 +00:00
|
|
|
#ifdef BUILD_MAC_CLIENT
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <FindDirectory.h>
|
|
|
|
#endif
|
|
|
|
#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 "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
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_thread_id tr_getCurrentThread(void)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
return GetCurrentThreadId();
|
2007-10-01 15:17:15 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
return pthread_self();
|
2007-10-01 15:17:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool 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
|
2017-04-19 12:04:45 +00:00
|
|
|
return a == b;
|
2007-10-01 15:17:15 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void (* func)(void*);
|
|
|
|
void* arg;
|
|
|
|
tr_thread_id thread;
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
HANDLE thread_handle;
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_amInThread(tr_thread const* t)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
2017-04-19 12:04:45 +00:00
|
|
|
#define ThreadFuncReturnType unsigned WINAPI
|
2007-07-31 19:56:40 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define ThreadFuncReturnType void
|
2007-07-31 19:56:40 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static ThreadFuncReturnType ThreadFunc(void* _t)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2017-02-22 21:07:39 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
pthread_detach(pthread_self());
|
2017-02-22 21:07:39 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_thread* t = _t;
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
t->func(t->arg);
|
2007-07-31 19:56:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(t);
|
2017-02-22 21:07:39 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
_endthreadex(0);
|
|
|
|
return 0;
|
2007-07-31 19:56:40 +00:00
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_thread* tr_threadNew(void (* func)(void*), void* arg)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_thread* t = tr_new0(tr_thread, 1);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +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
|
2017-04-19 12:04:45 +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
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
pthread_create(&t->thread, NULL, (void* (*)(void*))ThreadFunc, t);
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int depth;
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
DWORD lockThread;
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
pthread_mutex_t lock;
|
|
|
|
pthread_t lockThread;
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lock* tr_lockNew(void)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
InitializeCriticalSection(&l->lock); /* supports recursion */
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +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
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return l;
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_lockFree(tr_lock* l)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
DeleteCriticalSection(&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
pthread_mutex_destroy(&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_free(l);
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_lockLock(tr_lock* l)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
EnterCriticalSection(&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
pthread_mutex_lock(&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#endif
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(l->depth >= 0);
|
2017-04-30 16:25:26 +00:00
|
|
|
assert(l->depth == 0 || tr_areThreadsEqual(l->lockThread, tr_getCurrentThread()));
|
2017-04-19 12:04:45 +00:00
|
|
|
l->lockThread = tr_getCurrentThread();
|
|
|
|
++l->depth;
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_lockHave(tr_lock const* l)
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return l->depth > 0 && tr_areThreadsEqual(l->lockThread, tr_getCurrentThread());
|
2007-07-30 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_lockUnlock(tr_lock* l)
|
2007-07-30 15:27:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(l->depth > 0);
|
|
|
|
assert(tr_areThreadsEqual(l->lockThread, tr_getCurrentThread()));
|
|
|
|
|
|
|
|
--l->depth;
|
|
|
|
assert(l->depth >= 0);
|
2007-10-01 15:17:15 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
LeaveCriticalSection(&l->lock);
|
2007-07-30 15:27:52 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +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
|
2017-04-19 12:04:45 +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
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* win32_get_known_folder_ex(REFKNOWNFOLDERID folder_id, DWORD flags)
|
2014-09-21 18:03:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret = NULL;
|
|
|
|
PWSTR path;
|
2015-04-10 22:15:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (SHGetKnownFolderPath(folder_id, flags | KF_FLAG_DONT_UNEXPAND, NULL, &path) == S_OK)
|
2015-04-10 22:15:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_win32_native_to_utf8(path, -1);
|
|
|
|
CoTaskMemFree(path);
|
2015-04-10 22:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* win32_get_known_folder(REFKNOWNFOLDERID folder_id)
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return win32_get_known_folder_ex(folder_id, KF_FLAG_DONT_VERIFY);
|
2016-09-21 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getHomeDir(void)
|
2006-10-13 06:29:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* home = NULL;
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (home == NULL)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
home = tr_env_get_string("HOME", NULL);
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (home == NULL)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
home = win32_get_known_folder(&FOLDERID_Profile);
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
struct passwd* pw = getpwuid(getuid());
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (pw != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
home = tr_strdup(pw->pw_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (home == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
home = tr_strdup("");
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return home;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
|
|
|
#define RESUME_SUBDIR "Resume"
|
|
|
|
#define TORRENT_SUBDIR "Torrents"
|
2008-10-14 03:03:29 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define RESUME_SUBDIR "resume"
|
|
|
|
#define TORRENT_SUBDIR "torrents"
|
2008-10-14 03:03:29 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_setConfigDir(tr_session* session, char const* configDir)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* path;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
session->configDir = tr_strdup(configDir);
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
path = tr_buildPath(configDir, RESUME_SUBDIR, NULL);
|
|
|
|
tr_sys_dir_create(path, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL);
|
|
|
|
session->resumeDir = path;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
path = tr_buildPath(configDir, TORRENT_SUBDIR, NULL);
|
|
|
|
tr_sys_dir_create(path, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL);
|
|
|
|
session->torrentDir = path;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetConfigDir(tr_session const* session)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return session->configDir;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_getTorrentDir(tr_session const* session)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return session->torrentDir;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_getResumeDir(tr_session const* session)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return session->resumeDir;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2007-06-18 19:39:52 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_getDefaultConfigDir(char const* appname)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* s = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (appname == NULL || *appname == '\0')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
appname = "Transmission";
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (s == NULL)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string("TRANSMISSION_HOME", NULL);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s == NULL)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2014-07-03 19:20:12 +00:00
|
|
|
#ifdef __APPLE__
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
s = tr_buildPath(getHomeDir(), "Library", "Application Support", appname, NULL);
|
|
|
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
|
|
|
char* appdata = win32_get_known_folder(&FOLDERID_LocalAppData);
|
|
|
|
s = tr_buildPath(appdata, appname, NULL);
|
|
|
|
tr_free(appdata);
|
|
|
|
|
|
|
|
#elif defined(__HAIKU__)
|
|
|
|
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
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
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
char* const xdg_config_home = tr_env_get_string("XDG_CONFIG_HOME", NULL);
|
|
|
|
|
|
|
|
if (xdg_config_home != NULL)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_buildPath(xdg_config_home, appname, NULL);
|
|
|
|
tr_free(xdg_config_home);
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_buildPath(getHomeDir(), ".config", appname, NULL);
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-02-28 19:06:23 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_getDefaultDownloadDir(void)
|
2008-11-15 17:39:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* user_dir = NULL;
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (user_dir == NULL)
|
2009-04-05 13:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* config_home;
|
|
|
|
char* config_file;
|
|
|
|
char* content;
|
|
|
|
size_t content_len;
|
|
|
|
|
|
|
|
/* figure out where to look for user-dirs.dirs */
|
|
|
|
config_home = tr_env_get_string("XDG_CONFIG_HOME", NULL);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (config_home != NULL && *config_home != '\0')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
config_file = tr_buildPath(config_home, "user-dirs.dirs", NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config_file = tr_buildPath(getHomeDir(), ".config", "user-dirs.dirs", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(config_home);
|
|
|
|
|
|
|
|
/* read in user-dirs.dirs and look for the download dir entry */
|
|
|
|
content = (char*)tr_loadFile(config_file, &content_len, NULL);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (content != NULL && content_len > 0)
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* key = "XDG_DOWNLOAD_DIR=\"";
|
2017-04-19 12:04:45 +00:00
|
|
|
char* line = strstr(content, key);
|
|
|
|
|
|
|
|
if (line != NULL)
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* value = line + strlen(key);
|
|
|
|
char* end = strchr(value, '"');
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (end != NULL)
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
if (strncmp(value, "$HOME/", 6) == 0)
|
|
|
|
{
|
|
|
|
user_dir = tr_buildPath(getHomeDir(), value + 6, NULL);
|
|
|
|
}
|
|
|
|
else if (strcmp(value, "$HOME") == 0)
|
|
|
|
{
|
|
|
|
user_dir = tr_strdup(getHomeDir());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user_dir = tr_strdup(value);
|
|
|
|
}
|
2009-04-05 13:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 22:15:41 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (user_dir == NULL)
|
|
|
|
{
|
|
|
|
user_dir = win32_get_known_folder(&FOLDERID_Downloads);
|
|
|
|
}
|
|
|
|
|
2015-04-10 22:15:41 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (user_dir == NULL)
|
|
|
|
{
|
2009-07-09 18:18:14 +00:00
|
|
|
#ifdef __HAIKU__
|
2017-04-19 12:04:45 +00:00
|
|
|
user_dir = tr_buildPath(getHomeDir(), "Desktop", NULL);
|
2009-07-09 18:18:14 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
user_dir = tr_buildPath(getHomeDir(), "Downloads", NULL);
|
2009-07-09 18:18:14 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return user_dir;
|
2008-11-15 17:39:54 +00:00
|
|
|
}
|
|
|
|
|
2008-07-11 04:07:14 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool isWebClientDir(char const* path)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = tr_buildPath(path, "index.html", NULL);
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const ret = tr_sys_path_exists(tmp, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo(_("Searching for web interface file \"%s\""), tmp);
|
|
|
|
tr_free(tmp);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_getWebClientDir(tr_session const* session UNUSED)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* s = NULL;
|
2008-07-11 04:07:14 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (s == NULL)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string("CLUTCH_HOME", NULL);
|
|
|
|
|
|
|
|
if (s == NULL)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string("TRANSMISSION_WEB_HOME", NULL);
|
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s == NULL)
|
|
|
|
{
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Look in the Application Support folder */
|
|
|
|
s = tr_buildPath(tr_sessionGetConfigDir(session), "web", NULL);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!isWebClientDir(s))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
CFURLRef appURL = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
|
|
CFStringRef appRef = CFURLCopyFileSystemPath(appURL, kCFURLPOSIXPathStyle);
|
2017-04-20 16:02:19 +00:00
|
|
|
CFIndex const appStringLength = CFStringGetMaximumSizeOfFileSystemRepresentation(appRef);
|
2011-08-15 00:10:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* appString = tr_malloc(appStringLength);
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const success = CFStringGetFileSystemRepresentation(appRef, appString, appStringLength);
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(success);
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
CFRelease(appURL);
|
|
|
|
CFRelease(appRef);
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Fallback to the app bundle */
|
|
|
|
s = tr_buildPath(appString, "Contents", "Resources", "web", NULL);
|
|
|
|
|
|
|
|
if (!isWebClientDir(s))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
|
|
|
s = NULL;
|
2009-02-28 21:45:16 +00:00
|
|
|
}
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(appString);
|
2009-02-28 21:45:16 +00:00
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#elif defined(_WIN32)
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Generally, Web interface should be stored in a Web subdir of
|
|
|
|
* calling executable dir. */
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static REFKNOWNFOLDERID known_folder_ids[] =
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
&FOLDERID_LocalAppData,
|
|
|
|
&FOLDERID_RoamingAppData,
|
|
|
|
&FOLDERID_ProgramData
|
2015-04-10 22:15:41 +00:00
|
|
|
};
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (size_t i = 0; s == NULL && i < ARRAYSIZE(known_folder_ids); ++i)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* dir = win32_get_known_folder(known_folder_ids[i]);
|
|
|
|
s = tr_buildPath(dir, "Transmission", "Web", NULL);
|
|
|
|
tr_free(dir);
|
|
|
|
|
|
|
|
if (!isWebClientDir(s))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2014-09-21 18:03:13 +00:00
|
|
|
wchar_t wide_module_path[MAX_PATH];
|
2017-04-19 12:04:45 +00:00
|
|
|
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);
|
|
|
|
|
2017-01-16 23:21:47 +00:00
|
|
|
if (dir != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
s = tr_buildPath(dir, "Web", NULL);
|
|
|
|
tr_free(dir);
|
|
|
|
|
|
|
|
if (!isWebClientDir(s))
|
|
|
|
{
|
|
|
|
tr_free(s);
|
2017-01-16 23:21:47 +00:00
|
|
|
s = NULL;
|
2017-04-19 12:04:45 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* candidates = NULL, * l;
|
|
|
|
char* tmp;
|
|
|
|
|
|
|
|
/* XDG_DATA_HOME should be the first in the list of candidates */
|
|
|
|
tmp = tr_env_get_string("XDG_DATA_HOME", NULL);
|
2008-07-11 04:07:14 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tmp != NULL && *tmp != '\0')
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list_append(&candidates, tmp);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-12-14 01:19:50 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* dhome = tr_buildPath(getHomeDir(), ".local", "share", NULL);
|
|
|
|
tr_list_append(&candidates, dhome);
|
|
|
|
tr_free(tmp);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* XDG_DATA_DIRS are the backup directories */
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* pkg = PACKAGE_DATA_DIR;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* xdg = tr_env_get_string("XDG_DATA_DIRS", NULL);
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* fallback = "/usr/local/share:/usr/share";
|
2017-04-19 12:04:45 +00:00
|
|
|
char* buf = tr_strdup_printf("%s:%s:%s", (pkg ? pkg : ""), (xdg ? xdg : ""), fallback);
|
|
|
|
tr_free(xdg);
|
|
|
|
tmp = buf;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (tmp != NULL && *tmp != '\0')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end = strchr(tmp, ':');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (end != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (end - tmp > 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_list_append(&candidates, tr_strndup(tmp, (size_t)(end - tmp)));
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = (char*)end + 1;
|
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (tmp != NULL && *tmp != '\0')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_list_append(&candidates, tr_strdup(tmp));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(buf);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* walk through the candidates & look for a match */
|
2017-04-30 16:25:26 +00:00
|
|
|
for (l = candidates; l != NULL; l = l->next)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* path = tr_buildPath(l->data, "transmission", "web", NULL);
|
2017-04-30 16:25:26 +00:00
|
|
|
bool const found = isWebClientDir(path);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (found)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s = path;
|
|
|
|
break;
|
2008-10-14 03:03:29 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_free(path);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s;
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tr_getSessionIdDir(void)
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_strdup("/tmp");
|
2016-09-21 20:56:03 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* program_data_dir = win32_get_known_folder_ex(&FOLDERID_ProgramData, KF_FLAG_CREATE);
|
|
|
|
char* result = tr_buildPath(program_data_dir, "Transmission", NULL);
|
|
|
|
tr_free(program_data_dir);
|
|
|
|
tr_sys_dir_create(result, 0, 0, NULL);
|
|
|
|
return result;
|
2016-09-21 20:56:03 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|