2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2021-10-20 02:30:50 +00:00
|
|
|
#include <string_view>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <vector>
|
2008-04-13 14:29:11 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2015-04-11 10:51:59 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2022-01-09 03:03:21 +00:00
|
|
|
#include "magnet-metainfo.h"
|
2008-04-13 22:31:07 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "platform.h" /* tr_getResumeDir() */
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "resume.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2021-11-13 00:10:04 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-09-16 16:22:33 +00:00
|
|
|
namespace
|
2009-09-10 02:20:35 +00:00
|
|
|
{
|
2021-09-16 16:22:33 +00:00
|
|
|
|
|
|
|
constexpr int MAX_REMEMBERED_PEERS = 200;
|
|
|
|
|
|
|
|
} // unnamed namespace
|
2009-09-10 02:20:35 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void savePeers(tr_variant* dict, tr_torrent const* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_pex* pex = nullptr;
|
|
|
|
int count = tr_peerMgrGetPeers(tor, &pex, TR_AF_INET, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
tr_variantDictAddRaw(dict, TR_KEY_peers2, pex, sizeof(tr_pex) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(pex);
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
count = tr_peerMgrGetPeers(tor, &pex, TR_AF_INET6, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
tr_variantDictAddRaw(dict, TR_KEY_peers2_6, pex, sizeof(tr_pex) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(pex);
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
static size_t addPeers(tr_torrent* tor, uint8_t const* buf, size_t buflen)
|
2009-09-10 02:20:35 +00:00
|
|
|
{
|
2021-09-09 20:25:30 +00:00
|
|
|
size_t const n_in = buflen / sizeof(tr_pex);
|
2021-09-19 20:41:35 +00:00
|
|
|
size_t const n_pex = std::min(n_in, size_t{ MAX_REMEMBERED_PEERS });
|
2009-09-10 02:20:35 +00:00
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
tr_pex pex[MAX_REMEMBERED_PEERS];
|
|
|
|
memcpy(pex, buf, sizeof(tr_pex) * n_pex);
|
|
|
|
return tr_peerMgrAddPex(tor, TR_PEER_FROM_RESUME, pex, n_pex);
|
2009-09-10 02:20:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadPeers(tr_variant* dict, tr_torrent* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto ret = uint64_t{};
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
uint8_t const* str = nullptr;
|
|
|
|
auto len = size_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindRaw(dict, TR_KEY_peers2, &str, &len))
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-09-09 20:25:30 +00:00
|
|
|
size_t const numAdded = addPeers(tor, str, len);
|
|
|
|
tr_logAddTorDbg(tor, "Loaded %zu IPv4 peers from resume file", numAdded);
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_PEERS;
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindRaw(dict, TR_KEY_peers2_6, &str, &len))
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2021-09-09 20:25:30 +00:00
|
|
|
size_t const numAdded = addPeers(tor, str, len);
|
|
|
|
tr_logAddTorDbg(tor, "Loaded %zu IPv6 peers from resume file", numAdded);
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_PEERS;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-02-17 09:33:57 +00:00
|
|
|
static void saveLabels(tr_variant* dict, tr_torrent const* tor)
|
|
|
|
{
|
2021-09-29 19:52:19 +00:00
|
|
|
auto const& labels = tor->labels;
|
|
|
|
tr_variant* list = tr_variantDictAddList(dict, TR_KEY_labels, std::size(labels));
|
|
|
|
for (auto const& label : labels)
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantListAddStr(list, label);
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t loadLabels(tr_variant* dict, tr_torrent* tor)
|
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* list = nullptr;
|
2021-11-05 06:29:19 +00:00
|
|
|
if (!tr_variantDictFindList(dict, TR_KEY_labels, &list))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int const n = tr_variantListSize(list);
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
auto sv = std::string_view{};
|
|
|
|
if (tr_variantGetStrView(tr_variantListChild(list, i), &sv) && !std::empty(sv))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
tor->labels.emplace(sv);
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
return TR_FR_LABELS;
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveDND(tr_variant* dict, tr_torrent const* tor)
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* const list = tr_variantDictAddList(dict, TR_KEY_dnd, n);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-28 03:17:47 +00:00
|
|
|
tr_variantListAddBool(list, !tr_torrentFile(tor, i).wanted);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadDND(tr_variant* dict, tr_torrent* tor)
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t ret = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_variant* list = nullptr;
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindList(dict, TR_KEY_dnd, &list) && tr_variantListSize(list) == n)
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
auto wanted = std::vector<tr_file_index_t>{};
|
|
|
|
auto unwanted = std::vector<tr_file_index_t>{};
|
|
|
|
wanted.reserve(n);
|
|
|
|
unwanted.reserve(n);
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto tmp = false;
|
2019-07-14 12:40:41 +00:00
|
|
|
if (tr_variantGetBool(tr_variantListChild(list, i), &tmp) && tmp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
unwanted.push_back(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
wanted.push_back(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
tor->initFilesWanted(std::data(unwanted), std::size(unwanted), false);
|
|
|
|
tor->initFilesWanted(std::data(wanted), std::size(wanted), true);
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_DND;
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-04-14 19:52:51 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddTorDbg(
|
|
|
|
tor,
|
|
|
|
"Couldn't load DND flags. DND list (%p) has %zu"
|
|
|
|
" children; torrent has %d files",
|
|
|
|
(void*)list,
|
|
|
|
tr_variantListSize(list),
|
|
|
|
(int)n);
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveFilePriorities(tr_variant* dict, tr_torrent const* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* const list = tr_variantDictAddList(dict, TR_KEY_priority, n);
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-28 03:17:47 +00:00
|
|
|
tr_variantListAddInt(list, tr_torrentFile(tor, i).priority);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadFilePriorities(tr_variant* dict, tr_torrent* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto ret = uint64_t{};
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* list = nullptr;
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindList(dict, TR_KEY_priority, &list) && tr_variantListSize(list) == n)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto priority = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &priority))
|
|
|
|
{
|
2021-12-01 23:11:57 +00:00
|
|
|
tor->setFilePriority(i, tr_priority_t(priority));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 03:20:18 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_FILE_PRIORITIES;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void saveSingleSpeedLimit(tr_variant* d, tr_torrent* tor, tr_direction dir)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictReserve(d, 3);
|
2021-12-16 08:49:04 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_Bps, tor->speedLimitBps(dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_use_global_speed_limit, tr_torrentUsesSessionLimits(tor));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_use_speed_limit, tr_torrentUsesSpeedLimit(tor, dir));
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void saveSpeedLimits(tr_variant* dict, tr_torrent* tor)
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
saveSingleSpeedLimit(tr_variantDictAddDict(dict, TR_KEY_speed_limit_down, 0), tor, TR_DOWN);
|
|
|
|
saveSingleSpeedLimit(tr_variantDictAddDict(dict, TR_KEY_speed_limit_up, 0), tor, TR_UP);
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void saveRatioLimits(tr_variant* dict, tr_torrent* tor)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantDictAddDict(dict, TR_KEY_ratio_limit, 2);
|
|
|
|
tr_variantDictAddReal(d, TR_KEY_ratio_limit, tr_torrentGetRatioLimit(tor));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_ratio_mode, tr_torrentGetRatioMode(tor));
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void saveIdleLimits(tr_variant* dict, tr_torrent* tor)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantDictAddDict(dict, TR_KEY_idle_limit, 2);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_idle_limit, tr_torrentGetIdleLimit(tor));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_idle_mode, tr_torrentGetIdleMode(tor));
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void loadSingleSpeedLimit(tr_variant* d, tr_direction dir, tr_torrent* tor)
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto i = int64_t{};
|
|
|
|
auto boolVal = false;
|
2009-03-29 23:05:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_speed_Bps, &i))
|
|
|
|
{
|
2021-12-16 08:49:04 +00:00
|
|
|
tor->setSpeedLimitBps(dir, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (tr_variantDictFindInt(d, TR_KEY_speed, &i))
|
|
|
|
{
|
2021-12-16 08:49:04 +00:00
|
|
|
tor->setSpeedLimitBps(dir, i * 1024);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-03-29 23:05:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(d, TR_KEY_use_speed_limit, &boolVal))
|
|
|
|
{
|
|
|
|
tr_torrentUseSpeedLimit(tor, dir, boolVal);
|
|
|
|
}
|
2009-03-29 23:05:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(d, TR_KEY_use_global_speed_limit, &boolVal))
|
|
|
|
{
|
|
|
|
tr_torrentUseSessionLimits(tor, boolVal);
|
|
|
|
}
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadSpeedLimits(tr_variant* dict, tr_torrent* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto ret = uint64_t{};
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* d = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(dict, TR_KEY_speed_limit_up, &d))
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
loadSingleSpeedLimit(d, TR_UP, tor);
|
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
2013-05-23 03:20:18 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(dict, TR_KEY_speed_limit_down, &d))
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
loadSingleSpeedLimit(d, TR_DOWN, tor);
|
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadRatioLimits(tr_variant* dict, tr_torrent* tor)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto ret = uint64_t{};
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* d = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(dict, TR_KEY_ratio_limit, &d))
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto dratio = double{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindReal(d, TR_KEY_ratio_limit, &dratio))
|
|
|
|
{
|
|
|
|
tr_torrentSetRatioLimit(tor, dratio);
|
|
|
|
}
|
2013-05-23 03:20:18 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
auto i = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_ratio_mode, &i))
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_torrentSetRatioMode(tor, tr_ratiolimit(i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 03:20:18 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_RATIOLIMIT;
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadIdleLimits(tr_variant* dict, tr_torrent* tor)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto ret = uint64_t{};
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* d = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(dict, TR_KEY_idle_limit, &d))
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto imin = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_idle_limit, &imin))
|
|
|
|
{
|
|
|
|
tr_torrentSetIdleLimit(tor, imin);
|
|
|
|
}
|
2013-05-23 03:20:18 +00:00
|
|
|
|
2021-10-16 16:17:34 +00:00
|
|
|
auto i = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_idle_mode, &i))
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_torrentSetIdleMode(tor, tr_idlelimit(i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 03:20:18 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_IDLELIMIT;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
2013-01-19 08:43:26 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveName(tr_variant* dict, tr_torrent const* tor)
|
2013-01-22 00:25:42 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(dict, TR_KEY_name, tr_torrentName(tor));
|
2013-01-22 00:25:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadName(tr_variant* dict, tr_torrent* tor)
|
2013-01-22 00:25:42 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
auto name = std::string_view{};
|
|
|
|
if (!tr_variantDictFindStrView(dict, TR_KEY_name, &name))
|
2013-01-22 00:25:42 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-22 00:25:42 +00:00
|
|
|
|
2021-12-10 00:49:30 +00:00
|
|
|
name = tr_strvStrip(name);
|
2021-12-08 01:03:32 +00:00
|
|
|
if (std::empty(name))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:12:33 +00:00
|
|
|
tor->setName(name);
|
2013-01-22 00:25:42 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
return TR_FR_NAME;
|
2013-01-22 00:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveFilenames(tr_variant* dict, tr_torrent const* tor)
|
2013-01-19 08:43:26 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2021-12-26 22:04:20 +00:00
|
|
|
tr_variant* const list = tr_variantDictAddList(dict, TR_KEY_files, n);
|
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-02 01:25:25 +00:00
|
|
|
tr_variantListAddStrView(list, tor->fileSubpath(i));
|
2013-01-19 08:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadFilenames(tr_variant* dict, tr_torrent* tor)
|
2013-01-19 08:43:26 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* list = nullptr;
|
2021-11-05 06:29:19 +00:00
|
|
|
if (!tr_variantDictFindList(dict, TR_KEY_files, &list))
|
2013-01-19 08:43:26 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-19 08:43:26 +00:00
|
|
|
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n_files = tor->fileCount();
|
|
|
|
auto const n_list = tr_variantListSize(list);
|
|
|
|
for (size_t i = 0; i < n_files && i < n_list; ++i)
|
2021-11-05 06:29:19 +00:00
|
|
|
{
|
|
|
|
auto sv = std::string_view{};
|
2022-01-02 01:25:25 +00:00
|
|
|
if (tr_variantGetStrView(tr_variantListChild(list, i), &sv) && !std::empty(sv))
|
2013-01-19 08:43:26 +00:00
|
|
|
{
|
2022-01-02 01:25:25 +00:00
|
|
|
tor->setFileSubpath(i, sv);
|
2013-01-19 08:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
return TR_FR_FILENAMES;
|
2013-01-19 08:43:26 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
static void bitfieldToRaw(tr_bitfield const& b, tr_variant* benc)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (b.hasNone() || std::empty(b))
|
2013-05-23 03:20:18 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
tr_variantInitStr(benc, "none"sv);
|
2013-05-23 03:20:18 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
else if (b.hasAll())
|
2013-05-23 03:20:18 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantInitStrView(benc, "all"sv);
|
2013-05-23 03:20:18 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-05-23 03:20:18 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const raw = b.raw();
|
2021-10-16 14:04:19 +00:00
|
|
|
tr_variantInitRaw(benc, raw.data(), std::size(raw));
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
static void rawToBitfield(tr_bitfield& bitfield, uint8_t const* raw, size_t rawlen)
|
|
|
|
{
|
|
|
|
if (raw == nullptr || rawlen == 0 || (rawlen == 4 && memcmp(raw, "none", 4) == 0))
|
|
|
|
{
|
|
|
|
bitfield.setHasNone();
|
|
|
|
}
|
|
|
|
else if (rawlen == 3 && memcmp(raw, "all", 3) == 0)
|
|
|
|
{
|
|
|
|
bitfield.setHasAll();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-08 00:25:45 +00:00
|
|
|
bitfield.setRaw(raw, rawlen);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 07:14:52 +00:00
|
|
|
static void saveProgress(tr_variant* dict, tr_torrent const* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
tr_variant* const prog = tr_variantDictAddDict(dict, TR_KEY_progress, 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
// add the mtimes
|
2021-12-26 22:04:20 +00:00
|
|
|
auto const& mtimes = tor->file_mtimes_;
|
|
|
|
auto const n = std::size(mtimes);
|
2021-10-29 18:24:30 +00:00
|
|
|
tr_variant* const l = tr_variantDictAddList(prog, TR_KEY_mtimes, n);
|
2021-12-26 22:04:20 +00:00
|
|
|
for (auto const& mtime : mtimes)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-26 22:04:20 +00:00
|
|
|
tr_variantListAddInt(l, mtime);
|
2011-02-02 21:17:16 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
// add the 'checked pieces' bitfield
|
2021-11-25 18:26:51 +00:00
|
|
|
bitfieldToRaw(tor->checked_pieces_, tr_variantDictAdd(prog, TR_KEY_pieces));
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* add the progress */
|
|
|
|
if (tor->completeness == TR_SEED)
|
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(prog, TR_KEY_have, "all"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* add the blocks bitfield */
|
2021-11-25 18:26:51 +00:00
|
|
|
bitfieldToRaw(tor->blocks(), tr_variantDictAdd(prog, TR_KEY_blocks));
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
/*
|
|
|
|
* Transmisison has iterated through a few strategies here, so the
|
|
|
|
* code has some added complexity to support older approaches.
|
|
|
|
*
|
|
|
|
* Current approach: 'progress' is a dict with two entries:
|
|
|
|
* - 'pieces' a bitfield for whether each piece has been checked.
|
|
|
|
* - 'mtimes', an array of per-file timestamps
|
|
|
|
* On startup, 'pieces' is loaded. Then we check to see if the disk
|
|
|
|
* mtimes differ from the 'mtimes' list. Changed files have their
|
|
|
|
* pieces cleared from the bitset.
|
|
|
|
*
|
|
|
|
* Second approach (2.20 - 3.00): the 'progress' dict had a
|
|
|
|
* 'time_checked' entry which was a list with fileCount items.
|
|
|
|
* Each item was either a list of per-piece timestamps, or a
|
|
|
|
* single timestamp if either all or none of the pieces had been
|
|
|
|
* tested more recently than the file's mtime.
|
|
|
|
*
|
|
|
|
* First approach (pre-2.20) had an "mtimes" list identical to
|
|
|
|
* 3.10, but not the 'pieces' bitfield.
|
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint64_t loadProgress(tr_variant* dict, tr_torrent* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto ret = uint64_t{};
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2021-11-28 03:17:47 +00:00
|
|
|
if (tr_variant* prog = nullptr; tr_variantDictFindDict(dict, TR_KEY_progress, &prog))
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
/// CHECKED PIECES
|
|
|
|
|
2021-12-16 08:49:04 +00:00
|
|
|
auto checked = tr_bitfield(tor->pieceCount());
|
2021-10-29 18:24:30 +00:00
|
|
|
auto mtimes = std::vector<time_t>{};
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n_files = tor->fileCount();
|
2021-11-28 03:17:47 +00:00
|
|
|
mtimes.reserve(n_files);
|
2021-10-29 18:24:30 +00:00
|
|
|
|
|
|
|
// try to load mtimes
|
2021-10-16 16:17:34 +00:00
|
|
|
tr_variant* l = nullptr;
|
2021-10-29 18:24:30 +00:00
|
|
|
if (tr_variantDictFindList(prog, TR_KEY_mtimes, &l))
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
auto fi = size_t{};
|
|
|
|
auto t = int64_t{};
|
|
|
|
while (tr_variantGetInt(tr_variantListChild(l, fi++), &t))
|
|
|
|
{
|
|
|
|
mtimes.push_back(t);
|
|
|
|
}
|
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
// try to load the piece-checked bitfield
|
|
|
|
uint8_t const* raw = nullptr;
|
|
|
|
auto rawlen = size_t{};
|
|
|
|
if (tr_variantDictFindRaw(prog, TR_KEY_pieces, &raw, &rawlen))
|
|
|
|
{
|
|
|
|
rawToBitfield(checked, raw, rawlen);
|
|
|
|
}
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
// maybe it's a .resume file from [2.20 - 3.00] with the per-piece mtimes
|
|
|
|
if (tr_variantDictFindList(prog, TR_KEY_time_checked, &l))
|
|
|
|
{
|
2021-11-28 03:17:47 +00:00
|
|
|
for (tr_file_index_t fi = 0; fi < n_files; ++fi)
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
tr_variant* const b = tr_variantListChild(l, fi);
|
|
|
|
auto time_checked = time_t{};
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantIsInt(b))
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto t = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantGetInt(b, &t);
|
2021-10-29 18:24:30 +00:00
|
|
|
time_checked = time_t(t);
|
2011-02-02 21:17:16 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (tr_variantIsList(b))
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
auto offset = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantGetInt(tr_variantListChild(b, 0), &offset);
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
time_checked = tr_time();
|
2021-11-29 01:12:54 +00:00
|
|
|
auto const [begin, end] = tor->piecesInFile(fi);
|
|
|
|
for (size_t i = 0, n = end - begin; i < n; ++i)
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
int64_t piece_time = 0;
|
|
|
|
tr_variantGetInt(tr_variantListChild(b, i + 1), &piece_time);
|
|
|
|
time_checked = std::min(time_checked, time_t(piece_time));
|
2011-01-24 05:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
|
|
|
mtimes.push_back(time_checked);
|
2011-01-24 05:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-12-06 21:26:04 +00:00
|
|
|
if (std::size(mtimes) != n_files)
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
tr_logAddTorErr(tor, "got %zu mtimes; expected %zu", std::size(mtimes), size_t(n_files));
|
2021-10-29 18:24:30 +00:00
|
|
|
// if resizing grows the vector, we'll get 0 mtimes for the
|
|
|
|
// new items which is exactly what we want since the pieces
|
|
|
|
// in an unknown state should be treated as untested
|
2021-12-06 21:26:04 +00:00
|
|
|
mtimes.resize(n_files);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
tor->initCheckedPieces(checked, std::data(mtimes));
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
/// COMPLETION
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2022-01-07 19:13:37 +00:00
|
|
|
auto blocks = tr_bitfield{ tor->blockCount() };
|
2021-10-16 16:17:34 +00:00
|
|
|
char const* err = nullptr;
|
2021-11-05 06:29:19 +00:00
|
|
|
auto sv = std::string_view{};
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant const* const b = tr_variantDictFind(prog, TR_KEY_blocks);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (b != nullptr)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
uint8_t const* buf = nullptr;
|
|
|
|
auto buflen = size_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!tr_variantGetRaw(b, &buf, &buflen))
|
|
|
|
{
|
|
|
|
err = "Invalid value for \"blocks\"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-29 18:24:30 +00:00
|
|
|
rawToBitfield(blocks, buf, buflen);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 06:29:19 +00:00
|
|
|
else if (tr_variantDictFindStrView(prog, TR_KEY_have, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
if (sv == "all"sv)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
blocks.setHasAll();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = "Invalid value for HAVE";
|
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (tr_variantDictFindRaw(prog, TR_KEY_bitfield, &raw, &rawlen))
|
2010-03-15 23:29:56 +00:00
|
|
|
{
|
2021-11-08 00:25:45 +00:00
|
|
|
blocks.setRaw(raw, rawlen);
|
2010-03-15 23:29:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
err = "Couldn't find 'pieces' or 'have' or 'bitfield'";
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (err != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddTorDbg(tor, "Torrent needs to be verified - %s", err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
tor->setBlocks(blocks);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = TR_FR_PROGRESS;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentSaveResume(tr_torrent* tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
|
|
|
|
if (!tr_isTorrent(tor))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantInitDict(&top, 50); /* arbitrary "big enough" number */
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_seeding_time_seconds, tor->secondsSeeding);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_downloading_time_seconds, tor->secondsDownloading);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_activity_date, tor->activityDate);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_added_date, tor->addedDate);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_corrupt, tor->corruptPrev + tor->corruptCur);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_done_date, tor->doneDate);
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_variantDictAddQuark(&top, TR_KEY_destination, tor->downloadDir().quark());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
if (!std::empty(tor->incompleteDir()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_variantDictAddQuark(&top, TR_KEY_incomplete_dir, tor->incompleteDir().quark());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_downloaded, tor->downloadedPrev + tor->downloadedCur);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_uploaded, tor->uploadedPrev + tor->uploadedCur);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_max_peers, tor->maxConnectedPeers);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_bandwidth_priority, tr_torrentGetPriority(tor));
|
2021-12-16 08:49:04 +00:00
|
|
|
tr_variantDictAddBool(&top, TR_KEY_paused, !tor->isRunning && !tor->isQueued());
|
2017-04-19 12:04:45 +00:00
|
|
|
savePeers(&top, tor);
|
|
|
|
|
2021-12-17 04:51:53 +00:00
|
|
|
if (tor->hasMetadata())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
saveFilePriorities(&top, tor);
|
|
|
|
saveDND(&top, tor);
|
|
|
|
saveProgress(&top, tor);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveSpeedLimits(&top, tor);
|
|
|
|
saveRatioLimits(&top, tor);
|
|
|
|
saveIdleLimits(&top, tor);
|
|
|
|
saveFilenames(&top, tor);
|
|
|
|
saveName(&top, tor);
|
2019-02-17 09:33:57 +00:00
|
|
|
saveLabels(&top, tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-18 05:14:00 +00:00
|
|
|
auto const err = tr_variantToFile(&top, TR_VARIANT_FMT_BENC, tor->resumeFile());
|
2021-10-16 16:17:34 +00:00
|
|
|
if (err != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
tor->setLocalError(tr_strvJoin("Unable to save resume file: ", tr_strerror(err)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantFree(&top);
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
static uint64_t loadFromFile(tr_torrent* tor, uint64_t fieldsToLoad, bool* did_migrate_filename)
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2021-10-16 16:17:34 +00:00
|
|
|
auto const wasDirty = tor->isDirty;
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
auto const migrated = tor->metainfo_.migrateFile(tor->session->resume_dir, tor->name(), tor->infoHashString(), ".resume"sv);
|
|
|
|
if (did_migrate_filename != nullptr)
|
2017-07-26 18:20:30 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
*did_migrate_filename = migrated;
|
2017-07-26 18:20:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 05:14:00 +00:00
|
|
|
auto const filename = tor->resumeFile();
|
2021-12-03 23:20:39 +00:00
|
|
|
auto buf = std::vector<char>{};
|
2022-01-15 19:33:57 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
auto top = tr_variant{};
|
2021-12-24 19:37:34 +00:00
|
|
|
if (!tr_loadFile(buf, filename, &error) ||
|
2021-11-18 05:37:35 +00:00
|
|
|
!tr_variantFromBuf(
|
|
|
|
&top,
|
|
|
|
TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE,
|
|
|
|
{ std::data(buf), std::size(buf) },
|
|
|
|
nullptr,
|
|
|
|
&error))
|
2008-04-14 03:12:24 +00:00
|
|
|
{
|
2021-11-20 00:36:25 +00:00
|
|
|
tr_logAddTorDbg(tor, "Couldn't read \"%s\": %s", filename.c_str(), error->message);
|
2017-07-26 18:20:30 +00:00
|
|
|
tr_error_clear(&error);
|
2022-01-15 19:33:57 +00:00
|
|
|
return 0;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2021-11-20 00:36:25 +00:00
|
|
|
tr_logAddTorDbg(tor, "Read resume file \"%s\"", filename.c_str());
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
auto fieldsLoaded = uint64_t{};
|
|
|
|
auto boolVal = false;
|
|
|
|
auto i = int64_t{};
|
|
|
|
auto sv = std::string_view{};
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_CORRUPT) != 0 && tr_variantDictFindInt(&top, TR_KEY_corrupt, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->corruptPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_CORRUPT;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_DOWNLOAD_DIR)) != 0 &&
|
2021-11-05 06:29:19 +00:00
|
|
|
tr_variantDictFindStrView(&top, TR_KEY_destination, &sv) && !std::empty(sv))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
bool const is_current_dir = tor->current_dir == tor->download_dir;
|
|
|
|
tor->download_dir = sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (is_current_dir)
|
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
tor->current_dir = sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fieldsLoaded |= TR_FR_DOWNLOAD_DIR;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_INCOMPLETE_DIR)) != 0 &&
|
2021-11-05 06:29:19 +00:00
|
|
|
tr_variantDictFindStrView(&top, TR_KEY_incomplete_dir, &sv) && !std::empty(sv))
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
bool const is_current_dir = tor->current_dir == tor->incomplete_dir;
|
|
|
|
tor->incomplete_dir = sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (is_current_dir)
|
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
tor->current_dir = sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fieldsLoaded |= TR_FR_INCOMPLETE_DIR;
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_DOWNLOADED) != 0 && tr_variantDictFindInt(&top, TR_KEY_downloaded, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->downloadedPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_DOWNLOADED;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_UPLOADED) != 0 && tr_variantDictFindInt(&top, TR_KEY_uploaded, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->uploadedPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_UPLOADED;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_MAX_PEERS) != 0 && tr_variantDictFindInt(&top, TR_KEY_max_peers, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->maxConnectedPeers = i;
|
|
|
|
fieldsLoaded |= TR_FR_MAX_PEERS;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_RUN) != 0 && tr_variantDictFindBool(&top, TR_KEY_paused, &boolVal))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->isRunning = !boolVal;
|
|
|
|
fieldsLoaded |= TR_FR_RUN;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_ADDED_DATE) != 0 && tr_variantDictFindInt(&top, TR_KEY_added_date, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->addedDate = i;
|
|
|
|
fieldsLoaded |= TR_FR_ADDED_DATE;
|
2008-06-02 04:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_DONE_DATE) != 0 && tr_variantDictFindInt(&top, TR_KEY_done_date, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->doneDate = i;
|
|
|
|
fieldsLoaded |= TR_FR_DONE_DATE;
|
2008-06-03 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_ACTIVITY_DATE) != 0 && tr_variantDictFindInt(&top, TR_KEY_activity_date, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-12-16 09:43:51 +00:00
|
|
|
tor->setDateActive(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
fieldsLoaded |= TR_FR_ACTIVITY_DATE;
|
2008-06-03 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_TIME_SEEDING) != 0 && tr_variantDictFindInt(&top, TR_KEY_seeding_time_seconds, &i))
|
2010-12-23 19:32:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->secondsSeeding = i;
|
|
|
|
fieldsLoaded |= TR_FR_TIME_SEEDING;
|
2010-12-23 19:32:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_TIME_DOWNLOADING) != 0 && tr_variantDictFindInt(&top, TR_KEY_downloading_time_seconds, &i))
|
2010-12-23 19:32:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->secondsDownloading = i;
|
|
|
|
fieldsLoaded |= TR_FR_TIME_DOWNLOADING;
|
2010-12-23 19:32:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_BANDWIDTH_PRIORITY) != 0 && tr_variantDictFindInt(&top, TR_KEY_bandwidth_priority, &i) &&
|
|
|
|
tr_isPriority(i))
|
2009-04-18 23:17:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentSetPriority(tor, i);
|
|
|
|
fieldsLoaded |= TR_FR_BANDWIDTH_PRIORITY;
|
2009-04-18 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_PEERS) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadPeers(&top, tor);
|
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2021-11-03 02:55:33 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_PROGRESS) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-03 02:55:33 +00:00
|
|
|
fieldsLoaded |= loadProgress(&top, tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2021-11-03 02:55:33 +00:00
|
|
|
// Only load file priorities if we are actually downloading.
|
|
|
|
// If we're a seed or partial seed, loading it is a waste of time.
|
|
|
|
// NB: this is why loadProgress() comes before loadFilePriorities()
|
2021-11-28 21:55:29 +00:00
|
|
|
if (!tor->isDone() && (fieldsToLoad & TR_FR_FILE_PRIORITIES) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-03 02:55:33 +00:00
|
|
|
fieldsLoaded |= loadFilePriorities(&top, tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_DND) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadDND(&top, tor);
|
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_SPEEDLIMIT) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadSpeedLimits(&top, tor);
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_RATIOLIMIT) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadRatioLimits(&top, tor);
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_IDLELIMIT) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadIdleLimits(&top, tor);
|
|
|
|
}
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_FILENAMES) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadFilenames(&top, tor);
|
|
|
|
}
|
2013-01-19 08:43:26 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_NAME) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fieldsLoaded |= loadName(&top, tor);
|
|
|
|
}
|
2013-01-22 00:25:42 +00:00
|
|
|
|
2019-02-17 09:33:57 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_LABELS) != 0)
|
|
|
|
{
|
|
|
|
fieldsLoaded |= loadLabels(&top, tor);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* loading the resume file triggers of a lot of changes,
|
|
|
|
* but none of them needs to trigger a re-saving of the
|
|
|
|
* same resume information... */
|
|
|
|
tor->isDirty = wasDirty;
|
2009-08-14 14:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
|
|
|
return fieldsLoaded;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static uint64_t setFromCtor(tr_torrent* tor, uint64_t fields, tr_ctor const* ctor, tr_ctorMode mode)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t ret = 0;
|
2008-04-18 12:47:13 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fields & TR_FR_RUN) != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
auto isPaused = bool{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_ctorGetPaused(ctor, mode, &isPaused))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->isRunning = !isPaused;
|
|
|
|
ret |= TR_FR_RUN;
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
if (((fields & TR_FR_MAX_PEERS) != 0) && tr_ctorGetPeerLimit(ctor, mode, &tor->maxConnectedPeers))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
ret |= TR_FR_MAX_PEERS;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-18 12:47:13 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fields & TR_FR_DOWNLOAD_DIR) != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-10-16 16:17:34 +00:00
|
|
|
char const* path = nullptr;
|
2019-07-13 08:52:44 +00:00
|
|
|
if (tr_ctorGetDownloadDir(ctor, mode, &path) && !tr_str_is_empty(path))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret |= TR_FR_DOWNLOAD_DIR;
|
2021-12-23 17:16:05 +00:00
|
|
|
tor->download_dir = path;
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static uint64_t useManditoryFields(tr_torrent* tor, uint64_t fields, tr_ctor const* ctor)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return setFromCtor(tor, fields, ctor, TR_FORCE);
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static uint64_t useFallbackFields(tr_torrent* tor, uint64_t fields, tr_ctor const* ctor)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return setFromCtor(tor, fields, ctor, TR_FALLBACK);
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 18:20:30 +00:00
|
|
|
uint64_t tr_torrentLoadResume(tr_torrent* tor, uint64_t fieldsToLoad, tr_ctor const* ctor, bool* didRenameToHashOnlyName)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2009-09-28 18:22:57 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret |= useManditoryFields(tor, fieldsToLoad, ctor);
|
|
|
|
fieldsToLoad &= ~ret;
|
2017-07-26 18:20:30 +00:00
|
|
|
ret |= loadFromFile(tor, fieldsToLoad, didRenameToHashOnlyName);
|
2017-04-19 12:04:45 +00:00
|
|
|
fieldsToLoad &= ~ret;
|
|
|
|
ret |= useFallbackFields(tor, fieldsToLoad, ctor);
|
2008-04-18 12:47:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|