2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
#include <algorithm>
|
2022-01-02 17:51:59 +00:00
|
|
|
#include <cstring>
|
2021-09-27 13:45:21 +00:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2021-12-16 22:58:58 +00:00
|
|
|
#include <string_view>
|
2022-01-31 19:34:04 +00:00
|
|
|
#include <thread>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <vector>
|
2012-12-27 19:39:44 +00:00
|
|
|
|
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
|
2022-04-05 01:37:12 +00:00
|
|
|
#include <pwd.h>
|
2014-12-13 15:22:39 +00:00
|
|
|
#include <unistd.h> /* getuid() */
|
2022-01-31 19:34:04 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#ifdef BUILD_MAC_CLIENT
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
2022-01-31 19:34:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <FindDirectory.h>
|
|
|
|
#endif
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2022-04-07 22:26:59 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2022-01-31 19:34:04 +00:00
|
|
|
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.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"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2014-09-21 18:03:13 +00:00
|
|
|
#include "utils.h"
|
2007-07-30 15:27:52 +00:00
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
/***
|
|
|
|
**** PATHS
|
|
|
|
***/
|
2006-07-16 19:39:23 +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
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* ret = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
PWSTR path;
|
2015-04-10 22:15:41 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (SHGetKnownFolderPath(folder_id, flags | KF_FLAG_DONT_UNEXPAND, nullptr, &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
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
static char const* getHomeDir()
|
2006-10-13 06:29:26 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
static char const* home = nullptr;
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (home == nullptr)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
home = tr_env_get_string("HOME", nullptr);
|
2022-04-05 01:37:12 +00:00
|
|
|
}
|
2006-10-13 06:29:26 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (home == nullptr)
|
|
|
|
{
|
|
|
|
home = win32_get_known_folder(FOLDERID_Profile);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (home == nullptr)
|
|
|
|
{
|
|
|
|
struct passwd pwent;
|
|
|
|
struct passwd* pw = nullptr;
|
|
|
|
char buf[4096];
|
|
|
|
getpwuid_r(getuid(), &pwent, buf, sizeof buf, &pw);
|
|
|
|
if (pw != nullptr)
|
|
|
|
{
|
|
|
|
home = tr_strdup(pw->pw_dir);
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (home == nullptr)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
void tr_setConfigDir(tr_session* session, std::string_view config_dir)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
2022-01-17 18:39:50 +00:00
|
|
|
auto constexpr ResumeSubdir = "Resume"sv;
|
|
|
|
auto constexpr TorrentSubdir = "Torrents"sv;
|
2008-10-14 03:03:29 +00:00
|
|
|
#else
|
2022-01-17 18:39:50 +00:00
|
|
|
auto constexpr ResumeSubdir = "resume"sv;
|
|
|
|
auto constexpr TorrentSubdir = "torrents"sv;
|
2008-10-14 03:03:29 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
session->config_dir = config_dir;
|
2022-01-17 18:39:50 +00:00
|
|
|
session->resume_dir = tr_strvPath(config_dir, ResumeSubdir);
|
|
|
|
session->torrent_dir = tr_strvPath(config_dir, TorrentSubdir);
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_create(session->resume_dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
tr_sys_dir_create(session->torrent_dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
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
|
|
|
{
|
2022-01-02 17:51:59 +00:00
|
|
|
return session->config_dir.c_str();
|
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
|
|
|
{
|
2022-01-02 17:51:59 +00:00
|
|
|
return session->torrent_dir.c_str();
|
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_getDefaultConfigDir(char const* appname)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
static char const* s = nullptr;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (tr_str_is_empty(appname))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
appname = "Transmission";
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (s == nullptr)
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
s = tr_env_get_string("TRANSMISSION_HOME", nullptr);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (s == nullptr)
|
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
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(tr_strvPath(getHomeDir(), "Library", "Application Support", appname));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
char* appdata = win32_get_known_folder(FOLDERID_LocalAppData);
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(tr_strvPath(appdata, appname));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(appdata);
|
|
|
|
|
|
|
|
#elif defined(__HAIKU__)
|
|
|
|
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof(buf));
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(tr_strvPath(buf, appname));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
char* const xdg_config_home = tr_env_get_string("XDG_CONFIG_HOME", nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (xdg_config_home != nullptr)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(tr_strvPath(xdg_config_home, appname));
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(tr_strvPath(getHomeDir(), ".config", appname));
|
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
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
static std::string getUserDirsFilename()
|
2008-11-15 17:39:54 +00:00
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
char* const config_home = tr_env_get_string("XDG_CONFIG_HOME", nullptr);
|
|
|
|
auto config_file = !tr_str_is_empty(config_home) ? tr_strvPath(config_home, "user-dirs.dirs") :
|
|
|
|
tr_strvPath(getHomeDir(), ".config", "user-dirs.dirs");
|
2009-04-05 13:33:32 +00:00
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
tr_free(config_home);
|
|
|
|
return config_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getXdgEntryFromUserDirs(std::string_view key)
|
|
|
|
{
|
|
|
|
auto content = std::vector<char>{};
|
2022-03-26 00:31:27 +00:00
|
|
|
if (!tr_loadFile(getUserDirsFilename(), content) && std::empty(content))
|
2009-04-05 13:41:38 +00:00
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
// search for key="val" and extract val
|
2022-04-07 22:26:59 +00:00
|
|
|
auto const search = fmt::format(FMT_STRING("{:s}=\""), key);
|
2021-12-16 22:58:58 +00:00
|
|
|
auto begin = std::search(std::begin(content), std::end(content), std::begin(search), std::end(search));
|
|
|
|
if (begin == std::end(content))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::advance(begin, std::size(search));
|
|
|
|
auto const end = std::find(begin, std::end(content), '"');
|
|
|
|
if (end == std::end(content))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto val = std::string{ begin, end };
|
|
|
|
|
|
|
|
// if val contains "$HOME", replace that with getHomeDir()
|
|
|
|
auto constexpr Home = "$HOME"sv;
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto const it = std::search(std::begin(val), std::end(val), std::begin(Home), std::end(Home)); it != std::end(val))
|
2021-12-16 22:58:58 +00:00
|
|
|
{
|
|
|
|
val.replace(it, it + std::size(Home), std::string_view{ getHomeDir() });
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
return val;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
char const* tr_getDefaultDownloadDir()
|
2021-12-16 22:58:58 +00:00
|
|
|
{
|
|
|
|
static char const* user_dir = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
if (user_dir == nullptr)
|
|
|
|
{
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto const xdg_user_dir = getXdgEntryFromUserDirs("XDG_DOWNLOAD_DIR"sv); !std::empty(xdg_user_dir))
|
2009-04-05 13:33:32 +00:00
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
user_dir = tr_strvDup(xdg_user_dir);
|
2009-04-05 13:33:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 22:15:41 +00:00
|
|
|
#ifdef _WIN32
|
2021-09-15 00:18:09 +00:00
|
|
|
if (user_dir == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
user_dir = win32_get_known_folder(FOLDERID_Downloads);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-04-10 22:15:41 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (user_dir == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-07-09 18:18:14 +00:00
|
|
|
#ifdef __HAIKU__
|
2022-04-05 01:37:12 +00:00
|
|
|
user_dir = tr_strvDup(tr_strvPath(getHomeDir(), "Desktop"));
|
2009-07-09 18:18:14 +00:00
|
|
|
#else
|
2022-04-05 01:37:12 +00:00
|
|
|
user_dir = tr_strvDup(tr_strvPath(getHomeDir(), "Downloads"));
|
2009-07-09 18:18:14 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-12-16 05:16:40 +00:00
|
|
|
static bool isWebClientDir(std::string_view path)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
auto const filename = tr_pathbuf{ path, '/', "index.html"sv };
|
|
|
|
bool const found = tr_sys_path_exists(filename);
|
|
|
|
tr_logAddTrace(fmt::format(FMT_STRING("Searching for web interface file '{:s}'"), filename));
|
|
|
|
return found;
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
char const* tr_getWebClientDir([[maybe_unused]] tr_session const* session)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
static char const* s = nullptr;
|
2008-07-11 04:07:14 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (s == nullptr)
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
s = tr_env_get_string("CLUTCH_HOME", nullptr);
|
2022-04-05 01:37:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (s == nullptr)
|
|
|
|
{
|
|
|
|
s = tr_env_get_string("TRANSMISSION_WEB_HOME", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BUILD_MAC_CLIENT
|
|
|
|
|
|
|
|
// look in the Application Support folder
|
|
|
|
if (s == nullptr)
|
|
|
|
{
|
|
|
|
if (auto path = tr_pathbuf{ session->config_dir, "/public_html"sv }; isWebClientDir(path))
|
2008-07-11 04:07:14 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(path);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-04-05 01:37:12 +00:00
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
// look in the resource bundle
|
|
|
|
if (s == nullptr)
|
|
|
|
{
|
|
|
|
auto app_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
|
|
auto app_ref = CFURLCopyFileSystemPath(app_url, kCFURLPOSIXPathStyle);
|
|
|
|
auto const buflen = CFStringGetMaximumSizeOfFileSystemRepresentation(app_ref);
|
|
|
|
auto buf = std::vector<char>(buflen, '\0');
|
|
|
|
bool const success = CFStringGetFileSystemRepresentation(app_ref, std::data(buf), std::size(buf));
|
|
|
|
TR_ASSERT(success);
|
|
|
|
CFRelease(app_url);
|
|
|
|
CFRelease(app_ref);
|
|
|
|
|
|
|
|
if (auto const path = tr_pathbuf{ std::string_view{ std::data(buf) }, "/Contents/Resources/public_html"sv };
|
|
|
|
isWebClientDir(path))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(path);
|
|
|
|
}
|
|
|
|
}
|
2011-08-15 00:10:06 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
#elif defined(_WIN32)
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (s == nullptr)
|
|
|
|
{
|
|
|
|
/* Generally, Web interface should be stored in a Web subdir of
|
|
|
|
* calling executable dir. */
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
static KNOWNFOLDERID const* const known_folder_ids[] = {
|
|
|
|
&FOLDERID_LocalAppData,
|
|
|
|
&FOLDERID_RoamingAppData,
|
|
|
|
&FOLDERID_ProgramData,
|
|
|
|
};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
for (size_t i = 0; s == nullptr && i < TR_N_ELEMENTS(known_folder_ids); ++i)
|
|
|
|
{
|
|
|
|
char* dir = win32_get_known_folder(*known_folder_ids[i]);
|
2010-03-24 23:41:08 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (auto const path = tr_pathbuf{ std::string_view{ dir }, "/Transmission/Web"sv }; isWebClientDir(path))
|
|
|
|
{
|
|
|
|
s = tr_strvDup(path);
|
2009-02-28 21:45:16 +00:00
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
tr_free(dir);
|
|
|
|
}
|
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (s == nullptr) /* check calling module place */
|
|
|
|
{
|
|
|
|
wchar_t wide_module_path[MAX_PATH];
|
|
|
|
GetModuleFileNameW(nullptr, wide_module_path, TR_N_ELEMENTS(wide_module_path));
|
|
|
|
char* module_path = tr_win32_native_to_utf8(wide_module_path, -1);
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (auto const dir = tr_sys_path_dirname(module_path); !std::empty(dir))
|
|
|
|
{
|
|
|
|
if (auto const path = tr_pathbuf{ dir, "/Web"sv }; isWebClientDir(path))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(path);
|
2008-10-19 17:43:04 +00:00
|
|
|
}
|
2022-04-05 01:37:12 +00:00
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
tr_free(module_path);
|
|
|
|
}
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
#else // everyone else, follow the XDG spec
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
if (s == nullptr)
|
|
|
|
{
|
|
|
|
auto candidates = std::list<std::string>{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
/* XDG_DATA_HOME should be the first in the list of candidates */
|
|
|
|
char* tmp = tr_env_get_string("XDG_DATA_HOME", nullptr);
|
|
|
|
if (!tr_str_is_empty(tmp))
|
|
|
|
{
|
|
|
|
candidates.emplace_back(tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
candidates.emplace_back(tr_strvPath(getHomeDir(), ".local"sv, "share"sv));
|
|
|
|
}
|
|
|
|
tr_free(tmp);
|
2008-07-11 04:07:14 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
/* XDG_DATA_DIRS are the backup directories */
|
|
|
|
{
|
|
|
|
char const* const pkg = PACKAGE_DATA_DIR;
|
|
|
|
auto* xdg = tr_env_get_string("XDG_DATA_DIRS", "");
|
2022-04-07 22:26:59 +00:00
|
|
|
auto const buf = fmt::format(FMT_STRING("{:s}:{:s}:/usr/local/share:/usr/share"), pkg, xdg);
|
2022-04-05 01:37:12 +00:00
|
|
|
tr_free(xdg);
|
|
|
|
|
|
|
|
auto sv = std::string_view{ buf };
|
|
|
|
auto token = std::string_view{};
|
|
|
|
while (tr_strvSep(&sv, &token, ':'))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
token = tr_strvStrip(token);
|
|
|
|
if (!std::empty(token))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
candidates.emplace_back(token);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-05 01:37:12 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
/* walk through the candidates & look for a match */
|
|
|
|
for (auto const& dir : candidates)
|
|
|
|
{
|
|
|
|
if (auto const path = tr_pathbuf{ dir, "/transmission/public_html"sv }; isWebClientDir(path))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
s = tr_strvDup(path);
|
|
|
|
break;
|
2008-07-11 04:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:37:12 +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
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
std::string tr_getSessionIdDir()
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
return std::string{ "/tmp"sv };
|
2016-09-21 20:56:03 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
char* program_data_dir = win32_get_known_folder_ex(FOLDERID_ProgramData, KF_FLAG_CREATE);
|
2021-11-13 00:10:04 +00:00
|
|
|
auto const result = tr_strvPath(program_data_dir, "Transmission");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(program_data_dir);
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_create(result.c_str(), 0, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
return result;
|
2016-09-21 20:56:03 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|