2008-04-13 14:29:11 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2008-04-13 14:29:11 +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-04-13 14:29:11 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-05-28 17:17:12 +00:00
|
|
|
* $Id$
|
2008-04-13 14:29:11 +00:00
|
|
|
*/
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
#include <unistd.h> /* unlink */
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-04-13 22:31:07 +00:00
|
|
|
#include "completion.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "metainfo.h" /* tr_metainfoGetBasename () */
|
2008-04-13 22:31:07 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
2012-12-05 17:29:46 +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"
|
|
|
|
#include "utils.h" /* tr_buildPath */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2009-09-10 02:20:35 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAX_REMEMBERED_PEERS = 200
|
|
|
|
};
|
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
getResumeFilename (const tr_torrent * tor)
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * base = tr_metainfoGetBasename (tr_torrentInfo (tor));
|
|
|
|
char * filename = tr_strdup_printf ("%s" TR_PATH_DELIMITER_STR "%s.resume",
|
|
|
|
tr_getResumeDir (tor->session), base);
|
|
|
|
tr_free (base);
|
2010-04-20 23:14:00 +00:00
|
|
|
return filename;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
savePeers (tr_variant * dict, const tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-08-07 05:29:37 +00:00
|
|
|
int count;
|
|
|
|
tr_pex * pex;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
count = tr_peerMgrGetPeers ((tr_torrent*) tor, &pex, TR_AF_INET, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
|
|
|
|
if (count > 0)
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddRaw (dict, TR_KEY_peers2, pex, sizeof (tr_pex) * count);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (pex);
|
2009-08-07 05:29:37 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
count = tr_peerMgrGetPeers ((tr_torrent*) tor, &pex, TR_AF_INET6, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
|
|
|
|
if (count > 0)
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddRaw (dict, TR_KEY_peers2_6, pex, sizeof (tr_pex) * count);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (pex);
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2009-09-10 02:20:35 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
addPeers (tr_torrent * tor, const uint8_t * buf, int buflen)
|
2009-09-10 02:20:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int numAdded = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
const int count = buflen / sizeof (tr_pex);
|
2009-09-10 02:20:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<count && numAdded<MAX_REMEMBERED_PEERS; ++i)
|
2009-09-10 02:20:35 +00:00
|
|
|
{
|
|
|
|
tr_pex pex;
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (&pex, buf + (i * sizeof (tr_pex)), sizeof (tr_pex));
|
|
|
|
if (tr_isPex (&pex))
|
2009-09-10 02:20:35 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_peerMgrAddPex (tor, TR_PEER_FROM_RESUME, &pex, -1);
|
2009-09-10 02:20:35 +00:00
|
|
|
++numAdded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numAdded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadPeers (tr_variant * dict, tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t ret = 0;
|
2008-08-20 18:42:45 +00:00
|
|
|
const uint8_t * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindRaw (dict, TR_KEY_peers, &str, &len))
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int numAdded = addPeers (tor, str, len);
|
|
|
|
tr_tordbg (tor, "Loaded %d IPv4 peers from resume file", numAdded);
|
2008-12-15 00:17:08 +00:00
|
|
|
ret = TR_FR_PEERS;
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindRaw (dict, TR_KEY_peers6, &str, &len))
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int numAdded = addPeers (tor, str, len);
|
|
|
|
tr_tordbg (tor, "Loaded %d IPv6 peers from resume file", numAdded);
|
2008-04-13 22:31:07 +00:00
|
|
|
ret = TR_FR_PEERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-14 15:17:16 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveDND (tr_variant * dict, const tr_torrent * tor)
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * list;
|
2011-04-01 03:13:44 +00:00
|
|
|
tr_file_index_t i;
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_info * const inf = tr_torrentInfo (tor);
|
2008-04-14 15:17:16 +00:00
|
|
|
const tr_file_index_t n = inf->fileCount;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
list = tr_variantDictAddList (dict, TR_KEY_dnd, n);
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<n; ++i)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantListAddInt (list, inf->files[i].dnd ? 1 : 0);
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadDND (tr_variant * dict, tr_torrent * tor)
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2011-04-01 03:13:44 +00:00
|
|
|
uint64_t ret = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * list = NULL;
|
2011-04-01 03:13:44 +00:00
|
|
|
const tr_file_index_t n = tor->info.fileCount;
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindList (dict, TR_KEY_dnd, &list)
|
2012-12-14 04:34:42 +00:00
|
|
|
&& (tr_variantListSize (list) == n))
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t tmp;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_file_index_t * dl = tr_new (tr_file_index_t, n);
|
|
|
|
tr_file_index_t * dnd = tr_new (tr_file_index_t, n);
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t i, dlCount = 0, dndCount = 0;
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<n; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantGetInt (tr_variantListChild (list, i), &tmp) && tmp)
|
2008-04-14 15:17:16 +00:00
|
|
|
dnd[dndCount++] = i;
|
|
|
|
else
|
|
|
|
dl[dlCount++] = i;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (dndCount)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentInitFileDLs (tor, dnd, dndCount, false);
|
|
|
|
tr_tordbg (tor, "Resume file found %d files listed as dnd",
|
|
|
|
dndCount);
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
if (dlCount)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentInitFileDLs (tor, dl, dlCount, true);
|
|
|
|
tr_tordbg (tor,
|
2008-09-23 19:11:04 +00:00
|
|
|
"Resume file found %d files marked for download",
|
2012-12-05 17:29:46 +00:00
|
|
|
dlCount);
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (dnd);
|
|
|
|
tr_free (dl);
|
2009-04-18 23:17:30 +00:00
|
|
|
ret = TR_FR_DND;
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
2008-04-14 19:52:51 +00:00
|
|
|
else
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_tordbg (
|
2008-09-23 19:11:04 +00:00
|
|
|
tor,
|
2010-12-27 19:18:17 +00:00
|
|
|
"Couldn't load DND flags. DND list (%p) has %zu children; torrent has %d files",
|
2012-12-14 04:34:42 +00:00
|
|
|
list, tr_variantListSize (list), (int)n);
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveFilePriorities (tr_variant * dict, const tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * list;
|
2011-04-01 03:13:44 +00:00
|
|
|
tr_file_index_t i;
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_info * const inf = tr_torrentInfo (tor);
|
2008-04-13 22:31:07 +00:00
|
|
|
const tr_file_index_t n = inf->fileCount;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
list = tr_variantDictAddList (dict, TR_KEY_priority, n);
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i = 0; i < n; ++i)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantListAddInt (list, inf->files[i].priority);
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadFilePriorities (tr_variant * dict, tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * list;
|
2011-04-01 03:13:44 +00:00
|
|
|
uint64_t ret = 0;
|
|
|
|
const tr_file_index_t n = tor->info.fileCount;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindList (dict, TR_KEY_priority, &list)
|
2012-12-14 04:34:42 +00:00
|
|
|
&& (tr_variantListSize (list) == n))
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-01-12 18:02:28 +00:00
|
|
|
int64_t priority;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_file_index_t i;
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i = 0; i < n; ++i)
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantGetInt (tr_variantListChild (list, i), &priority))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentInitFilePriority (tor, i, priority);
|
2009-04-18 23:17:30 +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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveSingleSpeedLimit (tr_variant * d, tr_torrent * tor, tr_direction dir)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantDictReserve (d, 3);
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddInt (d, TR_KEY_speed_Bps, tr_torrentGetSpeedLimit_Bps (tor, dir));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveSpeedLimits (tr_variant * dict, tr_torrent * tor)
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveRatioLimits (tr_variant * dict, tr_torrent * tor)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +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
|
|
|
}
|
|
|
|
|
2010-07-16 03:12:57 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveIdleLimits (tr_variant * dict, tr_torrent * tor)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +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
|
|
|
}
|
|
|
|
|
2009-03-04 19:52:57 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
loadSingleSpeedLimit (tr_variant * d, tr_direction dir, tr_torrent * tor)
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool boolVal;
|
2009-03-29 23:05:32 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (d, TR_KEY_speed_Bps, &i))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetSpeedLimit_Bps (tor, dir, i);
|
2012-12-22 20:35:19 +00:00
|
|
|
else if (tr_variantDictFindInt (d, TR_KEY_speed, &i))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetSpeedLimit_Bps (tor, dir, i*1024);
|
2009-03-29 23:05:32 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindBool (d, TR_KEY_use_speed_limit, &boolVal))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentUseSpeedLimit (tor, dir, boolVal);
|
2009-03-29 23:05:32 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindBool (d, TR_KEY_use_global_speed_limit, &boolVal))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentUseSessionLimits (tor, boolVal);
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum old_speed_modes
|
|
|
|
{
|
|
|
|
TR_SPEEDLIMIT_GLOBAL, /* only follow the overall speed limit */
|
|
|
|
TR_SPEEDLIMIT_SINGLE /* only follow the per-torrent limit */
|
|
|
|
};
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadSpeedLimits (tr_variant * dict, tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * d;
|
2011-04-01 03:13:44 +00:00
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (dict, TR_KEY_speed_limit_up, &d))
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
loadSingleSpeedLimit (d, TR_UP, tor);
|
2009-03-04 19:52:57 +00:00
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (dict, TR_KEY_speed_limit_down, &d))
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
loadSingleSpeedLimit (d, TR_DOWN, tor);
|
2009-03-04 19:52:57 +00:00
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
|
|
|
}
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadRatioLimits (tr_variant * dict, tr_torrent * tor)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * d;
|
2011-04-01 03:13:44 +00:00
|
|
|
uint64_t ret = 0;
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (dict, TR_KEY_ratio_limit, &d))
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
double dratio;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindReal (d, TR_KEY_ratio_limit, &dratio))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetRatioLimit (tor, dratio);
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (d, TR_KEY_ratio_mode, &i))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetRatioMode (tor, i);
|
2009-02-13 18:23:56 +00:00
|
|
|
ret = TR_FR_RATIOLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-07-16 03:12:57 +00:00
|
|
|
|
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadIdleLimits (tr_variant * dict, tr_torrent * tor)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * d;
|
2011-04-01 03:13:44 +00:00
|
|
|
uint64_t ret = 0;
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (dict, TR_KEY_idle_limit, &d))
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
int64_t imin;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (d, TR_KEY_idle_limit, &imin))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetIdleLimit (tor, imin);
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (d, TR_KEY_idle_mode, &i))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetIdleMode (tor, i);
|
2010-07-24 02:57:39 +00:00
|
|
|
ret = TR_FR_IDLELIMIT;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
bitfieldToBenc (const tr_bitfield * b, tr_variant * benc)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_bitfieldHasAll (b))
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantInitStr (benc, "all", 3);
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (tr_bitfieldHasNone (b))
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantInitStr (benc, "none", 4);
|
2011-03-30 04:14:57 +00:00
|
|
|
else {
|
|
|
|
size_t byte_count = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
uint8_t * raw = tr_bitfieldGetRaw (b, &byte_count);
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantInitRaw (benc, raw, byte_count);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (raw);
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
saveProgress (tr_variant * dict, tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * l;
|
|
|
|
tr_variant * prog;
|
2011-02-02 21:17:16 +00:00
|
|
|
tr_file_index_t fi;
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo (tor);
|
|
|
|
const time_t now = tr_time ();
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
prog = tr_variantDictAddDict (dict, TR_KEY_progress, 3);
|
2011-02-02 21:17:16 +00:00
|
|
|
|
|
|
|
/* add the file/piece check timestamps... */
|
2012-12-22 20:35:19 +00:00
|
|
|
l = tr_variantDictAddList (prog, TR_KEY_time_checked, inf->fileCount);
|
2012-12-05 17:29:46 +00:00
|
|
|
for (fi=0; fi<inf->fileCount; ++fi)
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
|
|
|
const tr_piece * p;
|
|
|
|
const tr_piece * pend;
|
|
|
|
time_t oldest_nonzero = now;
|
|
|
|
time_t newest = 0;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool has_zero = false;
|
2012-12-05 17:29:46 +00:00
|
|
|
const time_t mtime = tr_torrentGetFileMTime (tor, fi);
|
2011-02-02 21:17:16 +00:00
|
|
|
const tr_file * f = &inf->files[fi];
|
|
|
|
|
|
|
|
/* get the oldest and newest nonzero timestamps for pieces in this file */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]; p!=pend; ++p)
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!p->timeChecked)
|
2011-03-22 15:19:54 +00:00
|
|
|
has_zero = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (oldest_nonzero > p->timeChecked)
|
2011-02-02 21:17:16 +00:00
|
|
|
oldest_nonzero = p->timeChecked;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (newest < p->timeChecked)
|
2011-02-02 21:17:16 +00:00
|
|
|
newest = p->timeChecked;
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2011-02-02 21:17:16 +00:00
|
|
|
/* If some of a file's pieces have been checked more recently than
|
|
|
|
the file's mtime, and some lest recently, then that file will
|
|
|
|
have a list containing timestamps for each piece.
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2011-02-02 21:17:16 +00:00
|
|
|
However, the most common use case is that the file doesn't change
|
|
|
|
after it's downloaded. To reduce overhead in the .resume file,
|
|
|
|
only a single timestamp is saved for the file if *all* or *none*
|
|
|
|
of the pieces were tested more recently than the file's mtime. */
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!has_zero && (mtime <= oldest_nonzero)) /* all checked */
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantListAddInt (l, oldest_nonzero);
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (newest < mtime) /* none checked */
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantListAddInt (l, newest);
|
2011-02-02 21:17:16 +00:00
|
|
|
else { /* some are checked, some aren't... so list piece by piece */
|
|
|
|
const int offset = oldest_nonzero - 1;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * ll = tr_variantListAddList (l, 2 + f->lastPiece - f->firstPiece);
|
|
|
|
tr_variantListAddInt (ll, offset);
|
2012-12-05 17:29:46 +00:00
|
|
|
for (p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]+1; p!=pend; ++p)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantListAddInt (ll, p->timeChecked ? p->timeChecked - offset : 0);
|
2011-02-02 21:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-03-15 23:29:56 +00:00
|
|
|
/* add the progress */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->completeness == TR_SEED)
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddStr (prog, TR_KEY_have, "all");
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/* add the blocks bitfield */
|
2012-12-22 20:35:19 +00:00
|
|
|
bitfieldToBenc (&tor->completion.blockBitfield, tr_variantDictAdd (prog, TR_KEY_blocks));
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2012-12-14 04:34:42 +00:00
|
|
|
loadProgress (tr_variant * dict, tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2011-02-02 21:17:16 +00:00
|
|
|
size_t i, n;
|
|
|
|
uint64_t ret = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * prog;
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo (tor);
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0, n=inf->pieceCount; i<n; ++i)
|
2011-02-02 21:17:16 +00:00
|
|
|
inf->pieces[i].timeChecked = 0;
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (dict, TR_KEY_progress, &prog))
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2010-03-15 23:29:56 +00:00
|
|
|
const char * err;
|
|
|
|
const char * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint8_t * raw;
|
2011-02-02 21:17:16 +00:00
|
|
|
size_t rawlen;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * l;
|
|
|
|
tr_variant * b;
|
2011-03-28 16:31:05 +00:00
|
|
|
struct tr_bitfield blocks = TR_BITFIELD_INIT;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindList (prog, TR_KEY_time_checked, &l))
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2011-02-02 21:17:16 +00:00
|
|
|
/* per-piece timestamps were added in 2.20.
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2011-02-02 21:17:16 +00:00
|
|
|
If some of a file's pieces have been checked more recently than
|
|
|
|
the file's mtime, and some lest recently, then that file will
|
|
|
|
have a list containing timestamps for each piece.
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2011-02-02 21:17:16 +00:00
|
|
|
However, the most common use case is that the file doesn't change
|
|
|
|
after it's downloaded. To reduce overhead in the .resume file,
|
|
|
|
only a single timestamp is saved for the file if *all* or *none*
|
|
|
|
of the pieces were tested more recently than the file's mtime. */
|
|
|
|
|
|
|
|
tr_file_index_t fi;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (fi=0; fi<inf->fileCount; ++fi)
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * b = tr_variantListChild (l, fi);
|
2011-02-02 21:17:16 +00:00
|
|
|
const tr_file * f = &inf->files[fi];
|
|
|
|
tr_piece * p = &inf->pieces[f->firstPiece];
|
2011-02-18 19:31:18 +00:00
|
|
|
const tr_piece * pend = &inf->pieces[f->lastPiece]+1;
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantIsInt (b))
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
|
|
|
int64_t t;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantGetInt (b, &t);
|
2012-12-05 17:29:46 +00:00
|
|
|
for (; p!=pend; ++p)
|
2011-02-02 21:17:16 +00:00
|
|
|
p->timeChecked = (time_t)t;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
else if (tr_variantIsList (b))
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2011-02-02 21:17:16 +00:00
|
|
|
int i = 0;
|
|
|
|
int64_t offset = 0;
|
|
|
|
const int pieces = f->lastPiece + 1 - f->firstPiece;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantGetInt (tr_variantListChild (b, 0), &offset);
|
2011-02-02 21:17:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<pieces; ++i)
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
2011-02-02 21:17:16 +00:00
|
|
|
int64_t t = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantGetInt (tr_variantListChild (b, i+1), &t);
|
2011-02-18 19:31:18 +00:00
|
|
|
inf->pieces[f->firstPiece+i].timeChecked = (time_t)(t ? t + offset : 0);
|
2011-01-24 05:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else if (tr_variantDictFindList (prog, TR_KEY_mtimes, &l))
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
|
|
|
tr_file_index_t fi;
|
|
|
|
|
|
|
|
/* Before 2.20, we stored the files' mtimes in the .resume file.
|
|
|
|
When loading the .resume file, a torrent's file would be flagged
|
|
|
|
as untested if its stored mtime didn't match its real mtime. */
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (fi=0; fi<inf->fileCount; ++fi)
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
|
|
|
int64_t t;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantGetInt (tr_variantListChild (l, fi), &t))
|
2011-02-02 21:17:16 +00:00
|
|
|
{
|
|
|
|
const tr_file * f = &inf->files[fi];
|
|
|
|
tr_piece * p = &inf->pieces[f->firstPiece];
|
|
|
|
const tr_piece * pend = &inf->pieces[f->lastPiece];
|
2012-12-05 17:29:46 +00:00
|
|
|
const time_t mtime = tr_torrentGetFileMTime (tor, fi);
|
2011-02-02 21:17:16 +00:00
|
|
|
const time_t timeChecked = mtime==t ? mtime : 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (; p!=pend; ++p)
|
2011-02-02 21:17:16 +00:00
|
|
|
p->timeChecked = timeChecked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-03-15 23:29:56 +00:00
|
|
|
err = NULL;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldConstruct (&blocks, tor->blockCount);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if ((b = tr_variantDictFind (prog, TR_KEY_blocks)))
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
size_t buflen;
|
|
|
|
const uint8_t * buf;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!tr_variantGetRaw (b, &buf, &buflen))
|
2011-03-28 16:31:05 +00:00
|
|
|
err = "Invalid value for \"blocks\"";
|
2012-12-05 17:29:46 +00:00
|
|
|
else if ((buflen == 3) && !memcmp (buf, "all", 3))
|
|
|
|
tr_bitfieldSetHasAll (&blocks);
|
|
|
|
else if ((buflen == 4) && !memcmp (buf, "none", 4))
|
|
|
|
tr_bitfieldSetHasNone (&blocks);
|
2011-03-28 16:31:05 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldSetRaw (&blocks, buf, buflen, true);
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else if (tr_variantDictFindStr (prog, TR_KEY_have, &str, NULL))
|
2010-03-15 23:29:56 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!strcmp (str, "all"))
|
|
|
|
tr_bitfieldSetHasAll (&blocks);
|
2010-03-15 23:29:56 +00:00
|
|
|
else
|
|
|
|
err = "Invalid value for HAVE";
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else if (tr_variantDictFindRaw (prog, TR_KEY_bitfield, &raw, &rawlen))
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldSetRaw (&blocks, raw, rawlen, true);
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
else err = "Couldn't find 'pieces' or 'have' or 'bitfield'";
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (err != NULL)
|
|
|
|
tr_tordbg (tor, "Torrent needs to be verified - %s", err);
|
2011-03-30 04:14:57 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpBlockInit (&tor->completion, &blocks);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldDestruct (&blocks);
|
2008-04-13 22:31:07 +00:00
|
|
|
ret = TR_FR_PROGRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSaveResume (tr_torrent * tor)
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2010-03-22 00:55:43 +00:00
|
|
|
int err;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2010-03-22 00:55:43 +00:00
|
|
|
char * filename;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_isTorrent (tor))
|
2008-07-02 01:46:10 +00:00
|
|
|
return;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantInitDict (&top, 50); /* arbitrary "big enough" number */
|
2012-12-22 20:35:19 +00:00
|
|
|
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);
|
|
|
|
tr_variantDictAddStr (&top, TR_KEY_destination, tor->downloadDir);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->incompleteDir != NULL)
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddStr (&top, TR_KEY_incomplete_dir, tor->incompleteDir);
|
|
|
|
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));
|
|
|
|
tr_variantDictAddBool (&top, TR_KEY_paused, !tor->isRunning);
|
2012-12-05 17:29:46 +00:00
|
|
|
savePeers (&top, tor);
|
|
|
|
if (tr_torrentHasMetadata (tor))
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
saveFilePriorities (&top, tor);
|
|
|
|
saveDND (&top, tor);
|
|
|
|
saveProgress (&top, tor);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
saveSpeedLimits (&top, tor);
|
|
|
|
saveRatioLimits (&top, tor);
|
|
|
|
saveIdleLimits (&top, tor);
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
filename = getResumeFilename (tor);
|
2012-12-14 04:34:42 +00:00
|
|
|
if ((err = tr_variantToFile (&top, TR_VARIANT_FMT_BENC, filename)))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetLocalError (tor, "Unable to save resume file: %s", tr_strerror (err));
|
|
|
|
tr_free (filename);
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 20:51:15 +00:00
|
|
|
static uint64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
loadFromFile (tr_torrent * tor, uint64_t fieldsToLoad)
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
2009-08-14 14:41:59 +00:00
|
|
|
int64_t i;
|
2008-04-13 22:31:07 +00:00
|
|
|
const char * str;
|
2009-08-14 14:41:59 +00:00
|
|
|
char * filename;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool boolVal;
|
|
|
|
uint64_t fieldsLoaded = 0;
|
|
|
|
const bool wasDirty = tor->isDirty;
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-09-28 18:22:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
filename = getResumeFilename (tor);
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename))
|
2008-04-14 03:12:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_tordbg (tor, "Couldn't read \"%s\"", filename);
|
2008-04-14 03:12:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (filename);
|
2008-04-14 03:12:24 +00:00
|
|
|
return fieldsLoaded;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_tordbg (tor, "Read resume file \"%s\"", filename);
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_CORRUPT)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_corrupt, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->corruptPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_CORRUPT;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_DOWNLOAD_DIR))
|
2012-12-22 20:35:19 +00:00
|
|
|
&& (tr_variantDictFindStr (&top, TR_KEY_destination, &str, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
&& (str && *str))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (tor->downloadDir);
|
2012-12-14 04:34:42 +00:00
|
|
|
tor->downloadDir = tr_strndup (str, len);
|
2008-05-18 16:44:30 +00:00
|
|
|
fieldsLoaded |= TR_FR_DOWNLOAD_DIR;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_INCOMPLETE_DIR))
|
2012-12-22 20:35:19 +00:00
|
|
|
&& (tr_variantDictFindStr (&top, TR_KEY_incomplete_dir, &str, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
&& (str && *str))
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (tor->incompleteDir);
|
2012-12-14 04:34:42 +00:00
|
|
|
tor->incompleteDir = tr_strndup (str, len);
|
2009-10-19 05:05:00 +00:00
|
|
|
fieldsLoaded |= TR_FR_INCOMPLETE_DIR;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_DOWNLOADED)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_downloaded, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->downloadedPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_DOWNLOADED;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_UPLOADED)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_uploaded, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->uploadedPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_UPLOADED;
|
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_MAX_PEERS)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_max_peers, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->maxConnectedPeers = i;
|
|
|
|
fieldsLoaded |= TR_FR_MAX_PEERS;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_RUN)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindBool (&top, TR_KEY_paused, &boolVal))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-03-29 23:05:32 +00:00
|
|
|
tor->isRunning = !boolVal;
|
2008-04-13 22:31:07 +00:00
|
|
|
fieldsLoaded |= TR_FR_RUN;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_ADDED_DATE)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_added_date, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-06-02 04:41:55 +00:00
|
|
|
tor->addedDate = i;
|
|
|
|
fieldsLoaded |= TR_FR_ADDED_DATE;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_DONE_DATE)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_done_date, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-06-03 19:16:12 +00:00
|
|
|
tor->doneDate = i;
|
|
|
|
fieldsLoaded |= TR_FR_DONE_DATE;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_ACTIVITY_DATE)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_activity_date, &i))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetActivityDate (tor, i);
|
2008-06-03 19:16:12 +00:00
|
|
|
fieldsLoaded |= TR_FR_ACTIVITY_DATE;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_TIME_SEEDING)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_seeding_time_seconds, &i))
|
2010-12-23 19:32:59 +00:00
|
|
|
{
|
|
|
|
tor->secondsSeeding = i;
|
|
|
|
fieldsLoaded |= TR_FR_TIME_SEEDING;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_TIME_DOWNLOADING)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_downloading_time_seconds, &i))
|
2010-12-23 19:32:59 +00:00
|
|
|
{
|
|
|
|
tor->secondsDownloading = i;
|
|
|
|
fieldsLoaded |= TR_FR_TIME_DOWNLOADING;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fieldsToLoad & TR_FR_BANDWIDTH_PRIORITY)
|
2012-12-22 20:35:19 +00:00
|
|
|
&& tr_variantDictFindInt (&top, TR_KEY_bandwidth_priority, &i)
|
2012-12-05 17:29:46 +00:00
|
|
|
&& tr_isPriority (i))
|
2009-04-18 23:17:30 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetPriority (tor, i);
|
2009-04-18 23:17:30 +00:00
|
|
|
fieldsLoaded |= TR_FR_BANDWIDTH_PRIORITY;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_PEERS)
|
|
|
|
fieldsLoaded |= loadPeers (&top, tor);
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_FILE_PRIORITIES)
|
|
|
|
fieldsLoaded |= loadFilePriorities (&top, tor);
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_PROGRESS)
|
|
|
|
fieldsLoaded |= loadProgress (&top, tor);
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_DND)
|
|
|
|
fieldsLoaded |= loadDND (&top, tor);
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_SPEEDLIMIT)
|
|
|
|
fieldsLoaded |= loadSpeedLimits (&top, tor);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_RATIOLIMIT)
|
|
|
|
fieldsLoaded |= loadRatioLimits (&top, tor);
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fieldsToLoad & TR_FR_IDLELIMIT)
|
|
|
|
fieldsLoaded |= loadIdleLimits (&top, tor);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2009-08-14 14:41:59 +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;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (filename);
|
2008-04-13 22:31:07 +00:00
|
|
|
return fieldsLoaded;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 12:47:13 +00:00
|
|
|
static uint64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
setFromCtor (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor, int mode)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fields & TR_FR_RUN)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool isPaused;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_ctorGetPaused (ctor, mode, &isPaused))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-04-18 12:47:13 +00:00
|
|
|
tor->isRunning = !isPaused;
|
|
|
|
ret |= TR_FR_RUN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fields & TR_FR_MAX_PEERS)
|
|
|
|
if (!tr_ctorGetPeerLimit (ctor, mode, &tor->maxConnectedPeers))
|
2008-04-18 12:47:13 +00:00
|
|
|
ret |= TR_FR_MAX_PEERS;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fields & TR_FR_DOWNLOAD_DIR)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-12-03 01:25:45 +00:00
|
|
|
const char * path;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_ctorGetDownloadDir (ctor, mode, &path) && path && *path)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
ret |= TR_FR_DOWNLOAD_DIR;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (tor->downloadDir);
|
|
|
|
tor->downloadDir = tr_strdup (path);
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
useManditoryFields (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return setFromCtor (tor, fields, ctor, TR_FORCE);
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
useFallbackFields (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return setFromCtor (tor, fields, ctor, TR_FALLBACK);
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentLoadResume (tr_torrent * tor,
|
2008-04-18 12:47:13 +00:00
|
|
|
uint64_t fieldsToLoad,
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_ctor * ctor)
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-09-28 18:22:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
ret |= useManditoryFields (tor, fieldsToLoad, ctor);
|
2008-04-18 12:47:13 +00:00
|
|
|
fieldsToLoad &= ~ret;
|
2012-12-05 17:29:46 +00:00
|
|
|
ret |= loadFromFile (tor, fieldsToLoad);
|
2008-04-18 12:47:13 +00:00
|
|
|
fieldsToLoad &= ~ret;
|
2012-12-05 17:29:46 +00:00
|
|
|
ret |= useFallbackFields (tor, fieldsToLoad, ctor);
|
2008-04-18 12:47:13 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentRemoveResume (const tr_torrent * tor)
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * filename = getResumeFilename (tor);
|
|
|
|
unlink (filename);
|
|
|
|
tr_free (filename);
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|