2008-07-22 23:28:28 +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
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-07-22 23:28:28 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
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
|
|
|
|
2008-12-02 03:57:01 +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
|
2008-09-23 19:11:04 +00:00
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
2009-07-09 18:18:14 +00:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <FindDirectory.h>
|
|
|
|
#endif
|
2009-04-05 13:56:41 +00:00
|
|
|
#define _XOPEN_SOURCE 600 /* needed for recursive locks. */
|
2008-09-23 19:11:04 +00:00
|
|
|
#ifndef __USE_UNIX98
|
2007-12-02 17:15:52 +00:00
|
|
|
#define __USE_UNIX98 /* some older Linuxes need it spelt out for them */
|
2008-09-23 19:11:04 +00:00
|
|
|
#endif
|
|
|
|
#include <pthread.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2008-12-16 01:11:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-01-05 04:41:19 +00:00
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
#define HAVE_SYS_STATVFS_H
|
|
|
|
#define HAVE_STATVFS
|
|
|
|
#endif
|
|
|
|
|
2008-07-11 04:11:22 +00:00
|
|
|
#include <sys/stat.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <sys/types.h>
|
2011-01-05 04:41:19 +00:00
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
2008-10-13 22:26:02 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <dirent.h>
|
2007-11-27 15:39:59 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h> /* getuid getpid close */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2008-07-11 04:07:14 +00:00
|
|
|
#include "list.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 15:27:52 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** THREADS
|
|
|
|
***/
|
|
|
|
|
2008-12-02 03:57:01 +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
|
|
|
{
|
2008-12-02 03:57:01 +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
|
|
|
{
|
2008-12-02 03:57:01 +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;
|
2008-02-28 16:40:31 +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
|
|
|
}
|
|
|
|
|
2007-07-31 19:56:40 +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);
|
2007-07-31 19:56:40 +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
|
|
|
|
2008-12-02 03:57:01 +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;
|
2008-12-02 03:57:01 +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
|
|
|
|
2008-12-02 03:57:01 +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
|
|
|
{
|
2008-12-02 03:57:01 +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
|
|
|
{
|
2008-12-02 03:57:01 +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);
|
2008-12-02 03:57:01 +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
|
|
|
|
2008-12-02 03:57:01 +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
|
|
|
|
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
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
home = tr_strdup (getenv ("HOME"));
|
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
|
|
|
{
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifdef WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
char appdata[MAX_PATH]; /* SHGetFolderPath () requires MAX_PATH */
|
|
|
|
*appdata = '\0';
|
|
|
|
SHGetFolderPath (NULL, CSIDL_PERSONAL, NULL, 0, appdata);
|
|
|
|
home = tr_strdup (appdata);
|
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
|
|
|
}
|
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
static const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
getOldConfigDir (void)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * path = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!path)
|
2007-12-15 22:22:30 +00:00
|
|
|
{
|
2008-12-02 03:57:01 +00:00
|
|
|
#ifdef SYS_DARWIN
|
2012-12-05 17:29:46 +00:00
|
|
|
path = tr_buildPath (getHomeDir (), "Library",
|
|
|
|
"Application Support",
|
|
|
|
"Transmission", NULL);
|
|
|
|
#elif defined (WIN32)
|
|
|
|
char appdata[MAX_PATH]; /* SHGetFolderPath () requires MAX_PATH */
|
|
|
|
SHGetFolderPath (NULL, CSIDL_APPDATA, NULL, 0, appdata);
|
|
|
|
path = tr_buildPath (appdata, "Transmission", NULL);
|
|
|
|
#elif defined (__HAIKU__)
|
|
|
|
char buf[TR_PATH_MAX];
|
|
|
|
find_directory (B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof (buf));
|
|
|
|
path = tr_buildPath (buf, "Transmission", NULL);
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
path = tr_buildPath (getHomeDir (), ".transmission", NULL);
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2007-12-15 22:22:30 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return path;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#if defined (SYS_DARWIN) || 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
|
|
|
static const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
getOldTorrentsDir (void)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * path = NULL;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!path)
|
|
|
|
path = tr_buildPath (getOldConfigDir (), TORRENT_SUBDIR, NULL);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return path;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
static const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
getOldCacheDir (void)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static char * path = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!path)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
#if defined (WIN32)
|
|
|
|
path = tr_buildPath (getOldConfigDir (), "Cache", NULL);
|
|
|
|
#elif defined (SYS_DARWIN)
|
|
|
|
path = tr_buildPath (getHomeDir (), "Library", "Caches", "Transmission", NULL);
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
path = tr_buildPath (getOldConfigDir (), "cache", NULL);
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return path;
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
moveFiles (const char * oldDir, const char * newDir)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (oldDir && newDir && strcmp (oldDir, newDir))
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
DIR * dirh = opendir (oldDir);
|
|
|
|
if (dirh)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int count = 0;
|
|
|
|
struct dirent * dirp;
|
|
|
|
while ((dirp = readdir (dirh)))
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * name = dirp->d_name;
|
|
|
|
if (name && strcmp (name, ".") && strcmp (name, ".."))
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * o = tr_buildPath (oldDir, name, NULL);
|
|
|
|
char * n = tr_buildPath (newDir, name, NULL);
|
|
|
|
rename (o, n);
|
|
|
|
++count;
|
|
|
|
tr_free (n);
|
|
|
|
tr_free (o);
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-10 02:53:03 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (count)
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_inf (_("Migrated %1$d files from \"%2$s\" to \"%3$s\""), count, oldDir, newDir);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
closedir (dirh);
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 01:33:53 +00:00
|
|
|
/**
|
|
|
|
* This function is for transmission-gtk users to migrate the config files
|
|
|
|
* from $HOME/.transmission/ (where they were kept before Transmission 1.30)
|
|
|
|
* to $HOME/.config/$appname as per the XDG directory spec.
|
|
|
|
*/
|
2008-04-05 20:12:11 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
migrateFiles (const tr_session * session)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static int migrated = false;
|
|
|
|
const bool should_migrate = strstr (getOldConfigDir (), ".transmission") != NULL;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!migrated && should_migrate)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * oldDir;
|
|
|
|
const char * newDir;
|
|
|
|
|
|
|
|
migrated = true;
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
oldDir = getOldTorrentsDir ();
|
|
|
|
newDir = tr_getTorrentDir (session);
|
|
|
|
moveFiles (oldDir, newDir);
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
oldDir = getOldCacheDir ();
|
|
|
|
newDir = tr_getResumeDir (session);
|
|
|
|
moveFiles (oldDir, newDir);
|
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);
|
|
|
|
tr_mkdirp (path, 0777);
|
|
|
|
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);
|
|
|
|
tr_mkdirp (path, 0777);
|
|
|
|
session->torrentDir = path;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
migrateFiles (session);
|
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
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((s = getenv ("TRANSMISSION_HOME")))
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_strdup (s);
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-05 20:27:13 +00:00
|
|
|
#ifdef SYS_DARWIN
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_buildPath (getHomeDir (), "Library", "Application Support", appname, NULL);
|
|
|
|
#elif defined (WIN32)
|
|
|
|
char appdata[TR_PATH_MAX]; /* SHGetFolderPath () requires MAX_PATH */
|
|
|
|
SHGetFolderPath (NULL, CSIDL_APPDATA, NULL, 0, appdata);
|
|
|
|
s = tr_buildPath (appdata, appname, NULL);
|
|
|
|
#elif defined (__HAIKU__)
|
|
|
|
char buf[TR_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
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((s = getenv ("XDG_CONFIG_HOME")))
|
|
|
|
s = tr_buildPath (s, appname, NULL);
|
|
|
|
else
|
|
|
|
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
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * config_home;
|
|
|
|
char * config_file;
|
|
|
|
char * content;
|
|
|
|
size_t content_len;
|
|
|
|
|
|
|
|
/* figure out where to look for user-dirs.dirs */
|
|
|
|
config_home = getenv ("XDG_CONFIG_HOME");
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
isWebClientDir (const char * path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
char * tmp = tr_buildPath (path, "index.html", NULL);
|
|
|
|
const int ret = !stat (tmp, &sb);
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_inf (_("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
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((s = getenv ("CLUTCH_HOME")))
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_strdup (s);
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if ((s = getenv ("TRANSMISSION_WEB_HOME")))
|
2008-07-14 16:00:20 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
s = tr_strdup (s);
|
2008-07-14 16:00:20 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2010-03-24 23:41:08 +00:00
|
|
|
#ifdef SYS_DARWIN /* 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
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#elif defined (WIN32)
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* SHGetFolderPath explicitly requires MAX_PATH length */
|
|
|
|
char dir[MAX_PATH];
|
2009-08-10 20:04:08 +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 */
|
|
|
|
{
|
|
|
|
SHGetFolderPath (NULL, CSIDL_COMMON_APPDATA, NULL, 0, dir);
|
|
|
|
s = tr_buildPath (dir, "Transmission", "Web", NULL);
|
|
|
|
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 */
|
|
|
|
{
|
|
|
|
SHGetFolderPath (NULL, CSIDL_APPDATA, NULL, 0, dir);
|
|
|
|
s = tr_buildPath (dir, "Transmission", "Web", NULL);
|
|
|
|
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 */
|
|
|
|
{
|
|
|
|
GetModuleFileName (GetModuleHandle (NULL), dir, sizeof (dir));
|
|
|
|
s = tr_buildPath (dirname (dir), "Web", NULL);
|
|
|
|
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;
|
|
|
|
const 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 */
|
|
|
|
tmp = getenv ("XDG_DATA_HOME");
|
|
|
|
if (tmp && *tmp)
|
|
|
|
{
|
|
|
|
tr_list_append (&candidates, tr_strdup (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);
|
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;
|
|
|
|
const char * xdg = getenv ("XDG_DATA_DIRS");
|
|
|
|
const char * fallback = "/usr/local/share:/usr/share";
|
|
|
|
char * buf = tr_strdup_printf ("%s:%s:%s", (pkg?pkg:""), (xdg?xdg:""), fallback);
|
|
|
|
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));
|
|
|
|
tmp = end + 1;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-28 19:06:23 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-01-05 04:41:19 +00:00
|
|
|
int64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getFreeSpace (const char * path)
|
2011-01-05 04:41:19 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
2012-12-05 17:29:46 +00:00
|
|
|
uint64_t freeBytesAvailable = 0;
|
|
|
|
return GetDiskFreeSpaceEx (path, &freeBytesAvailable, NULL, NULL)
|
|
|
|
? (int64_t)freeBytesAvailable
|
|
|
|
: -1;
|
|
|
|
#elif defined (HAVE_STATVFS)
|
|
|
|
struct statvfs buf;
|
|
|
|
return statvfs (path, &buf) ? -1 : (int64_t)buf.f_bavail * (int64_t)buf.f_frsize;
|
2011-01-05 04:41:19 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
#warning FIXME: not implemented
|
|
|
|
return -1;
|
2011-01-05 04:41:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
/* The following mmap functions are by Joerg Walter, and were taken from
|
2012-12-05 17:29:46 +00:00
|
|
|
* his paper at: http://www.genesys-e.de/jwalter/mix4win.htm */
|
2008-09-05 14:31:52 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
__declspec (align (4)) static LONG volatile g_sl;
|
2008-10-14 17:30:07 +00:00
|
|
|
#else
|
2012-12-07 01:53:31 +00:00
|
|
|
static LONG volatile g_sl __attribute__((aligned (4)));
|
2008-10-14 17:30:07 +00:00
|
|
|
#endif
|
2008-09-05 14:32:28 +00:00
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
/* Wait for spin lock */
|
2008-09-23 19:11:04 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
slwait (LONG volatile *sl)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
while (InterlockedCompareExchange (sl, 1, 0) != 0)
|
|
|
|
Sleep (0);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return 0;
|
2008-09-05 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release spin lock */
|
2008-09-23 19:11:04 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
slrelease (LONG volatile *sl)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
InterlockedExchange (sl, 0);
|
|
|
|
return 0;
|
2008-09-05 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
2008-09-05 14:32:32 +00:00
|
|
|
/* getpagesize for windows */
|
2008-09-23 19:11:04 +00:00
|
|
|
static long
|
2012-12-05 17:29:46 +00:00
|
|
|
getpagesize (void)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static long g_pagesize = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!g_pagesize)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
GetSystemInfo (&system_info);
|
|
|
|
g_pagesize = system_info.dwPageSize;
|
2008-09-05 14:32:32 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
return g_pagesize;
|
2008-09-05 14:32:32 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static long
|
2012-12-05 17:29:46 +00:00
|
|
|
getregionsize (void)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static long g_regionsize = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!g_regionsize)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
GetSystemInfo (&system_info);
|
|
|
|
g_regionsize = system_info.dwAllocationGranularity;
|
2008-09-05 14:32:32 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
return g_regionsize;
|
2008-09-05 14:32:32 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void *
|
2012-12-05 17:29:46 +00:00
|
|
|
mmap (void *ptr, long size, long prot, long type, long handle, long arg)
|
|
|
|
{
|
|
|
|
static long g_pagesize;
|
|
|
|
static long g_regionsize;
|
|
|
|
|
|
|
|
/* Wait for spin lock */
|
|
|
|
slwait (&g_sl);
|
|
|
|
|
|
|
|
/* First time initialization */
|
|
|
|
if (!g_pagesize)
|
|
|
|
g_pagesize = getpagesize ();
|
|
|
|
if (!g_regionsize)
|
|
|
|
g_regionsize = getregionsize ();
|
|
|
|
|
|
|
|
/* Allocate this */
|
|
|
|
ptr = VirtualAlloc (ptr, size, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
|
|
|
|
if (!ptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
ptr = (void *) -1;
|
|
|
|
goto mmap_exit;
|
2008-09-05 14:31:52 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
mmap_exit:
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Release spin lock */
|
|
|
|
slrelease (&g_sl);
|
|
|
|
return ptr;
|
2008-09-05 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
long
|
2012-12-05 17:29:46 +00:00
|
|
|
munmap (void *ptr, long size)
|
|
|
|
{
|
|
|
|
static long g_pagesize;
|
|
|
|
static long g_regionsize;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
/* Wait for spin lock */
|
|
|
|
slwait (&g_sl);
|
|
|
|
|
|
|
|
/* First time initialization */
|
|
|
|
if (!g_pagesize)
|
|
|
|
g_pagesize = getpagesize ();
|
|
|
|
if (!g_regionsize)
|
|
|
|
g_regionsize = getregionsize ();
|
|
|
|
|
|
|
|
/* Free this */
|
|
|
|
if (!VirtualFree (ptr, 0, MEM_RELEASE))
|
|
|
|
goto munmap_exit;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
munmap_exit:
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Release spin lock */
|
|
|
|
slrelease (&g_sl);
|
|
|
|
return rc;
|
2008-09-05 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|