2009-11-24 02:16:31 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-11-24 02:16:31 +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)
|
2009-11-24 02:16:31 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-11-24 02:16:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdio.h> /* remove () */
|
|
|
|
#include <string.h> /* memcpy (), memset (), memcmp () */
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "transmission.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "crypto.h" /* tr_sha1 () */
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "magnet.h"
|
|
|
|
#include "metainfo.h"
|
2010-02-06 05:22:27 +00:00
|
|
|
#include "resume.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrent-magnet.h"
|
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "web.h"
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(tor, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (tr_deepLoggingIsActive ()) \
|
|
|
|
tr_deepLog (__FILE__, __LINE__, tr_torrentName (tor), __VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
while (0)
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* don't ask for the same metadata piece more than this often */
|
2009-11-24 17:10:40 +00:00
|
|
|
MIN_REPEAT_INTERVAL_SECS = 3
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct metadata_node
|
|
|
|
{
|
|
|
|
time_t requestedAt;
|
|
|
|
int piece;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_incomplete_metadata
|
|
|
|
{
|
|
|
|
uint8_t * metadata;
|
|
|
|
int metadata_size;
|
|
|
|
int pieceCount;
|
|
|
|
|
|
|
|
/** sorted from least to most recently requested */
|
|
|
|
struct metadata_node * piecesNeeded;
|
|
|
|
int piecesNeededCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
incompleteMetadataFree (struct tr_incomplete_metadata * m)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (m->metadata);
|
|
|
|
tr_free (m->piecesNeeded);
|
|
|
|
tr_free (m);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetMetadataSizeHint (tr_torrent * tor, int size)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_torrentHasMetadata (tor))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->incompleteMetadata == NULL)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tr_incomplete_metadata * m;
|
2012-12-05 17:29:46 +00:00
|
|
|
const int n = (size + (METADATA_PIECE_SIZE - 1)) / METADATA_PIECE_SIZE;
|
|
|
|
dbgmsg (tor, "metadata is %d bytes in %d pieces", size, n);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
m = tr_new (struct tr_incomplete_metadata, 1);
|
2009-11-24 02:16:31 +00:00
|
|
|
m->pieceCount = n;
|
2012-12-05 17:29:46 +00:00
|
|
|
m->metadata = tr_new (uint8_t, size);
|
2009-11-24 02:16:31 +00:00
|
|
|
m->metadata_size = size;
|
|
|
|
m->piecesNeededCount = n;
|
2012-12-05 17:29:46 +00:00
|
|
|
m->piecesNeeded = tr_new (struct metadata_node, n);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<n; ++i) {
|
2009-11-24 02:16:31 +00:00
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor->incompleteMetadata = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 03:05:23 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
findInfoDictOffset (const tr_torrent * tor)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
|
|
|
size_t fileLen;
|
|
|
|
uint8_t * fileContents;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
/* load the file, and find the info dict's offset inside the file */
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((fileContents = tr_loadFile (tor->info.torrent, &fileLen)))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!tr_variantFromBenc (&top, fileContents, fileLen))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * infoDict;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (&top, TR_KEY_info, &infoDict))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
|
|
|
int infoLen;
|
2012-12-14 04:34:42 +00:00
|
|
|
char * infoContents = tr_variantToStr (infoDict, TR_VARIANT_FMT_BENC, &infoLen);
|
2012-12-05 17:29:46 +00:00
|
|
|
const uint8_t * i = (const uint8_t*) tr_memmem ((char*)fileContents, fileLen, infoContents, infoLen);
|
2010-09-19 21:07:57 +00:00
|
|
|
offset = i != NULL ? i - fileContents : 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (infoContents);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (fileContents);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
ensureInfoDictOffsetIsCached (tr_torrent * tor)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_torrentHasMetadata (tor));
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tor->infoDictOffsetIsCached)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tor->infoDictOffset = findInfoDictOffset (tor);
|
2011-03-22 15:19:54 +00:00
|
|
|
tor->infoDictOffsetIsCached = true;
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentGetMetadataPiece (tr_torrent * tor, int piece, int * len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
char * ret = NULL;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
|
|
|
assert (piece >= 0);
|
|
|
|
assert (len != NULL);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_torrentHasMetadata (tor))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-06-16 03:05:23 +00:00
|
|
|
FILE * fp;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
ensureInfoDictOffsetIsCached (tor);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tor->infoDictLength > 0);
|
|
|
|
assert (tor->infoDictOffset >= 0);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
fp = fopen (tor->info.torrent, "rb");
|
|
|
|
if (fp != NULL)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2009-12-15 16:34:12 +00:00
|
|
|
const int o = piece * METADATA_PIECE_SIZE;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!fseek (fp, tor->infoDictOffset + o, SEEK_SET))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2009-12-15 16:34:12 +00:00
|
|
|
const int l = o + METADATA_PIECE_SIZE <= tor->infoDictLength
|
2009-11-24 02:16:31 +00:00
|
|
|
? METADATA_PIECE_SIZE
|
2009-12-15 16:34:12 +00:00
|
|
|
: tor->infoDictLength - o;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (0<l && l<=METADATA_PIECE_SIZE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * buf = tr_new (char, l);
|
|
|
|
const int n = fread (buf, 1, l, fp);
|
|
|
|
if (n == l)
|
2009-12-15 16:34:12 +00:00
|
|
|
{
|
|
|
|
*len = l;
|
|
|
|
ret = buf;
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (buf);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
fclose (fp);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetMetadataPiece (tr_torrent * tor, int piece, const void * data, int len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tr_incomplete_metadata * m;
|
|
|
|
const int offset = piece * METADATA_PIECE_SIZE;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "got metadata piece %d", piece);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
/* are we set up to download metadata? */
|
|
|
|
m = tor->incompleteMetadata;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (m == NULL)
|
2009-11-24 02:16:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* does this data pass the smell test? */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (offset + len > m->metadata_size)
|
2009-11-24 02:16:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* do we need this piece? */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<m->piecesNeededCount; ++i)
|
|
|
|
if (m->piecesNeeded[i].piece == piece)
|
2009-11-24 02:16:31 +00:00
|
|
|
break;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i==m->piecesNeededCount)
|
2009-11-24 02:16:31 +00:00
|
|
|
return;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (m->metadata + offset, data, len);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_removeElementFromArray (m->piecesNeeded, i,
|
|
|
|
sizeof (struct metadata_node),
|
|
|
|
m->piecesNeededCount--);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
/* are we done? */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (m->piecesNeededCount == 0)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool success = false;
|
|
|
|
bool checksumPassed = false;
|
|
|
|
bool metainfoParsed = false;
|
2009-11-24 02:16:31 +00:00
|
|
|
uint8_t sha1[SHA_DIGEST_LENGTH];
|
2010-02-02 22:45:22 +00:00
|
|
|
|
|
|
|
/* we've got a complete set of metainfo... see if it passes the checksum test */
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "metainfo piece %d was the last one", piece);
|
|
|
|
tr_sha1 (sha1, m->metadata, m->metadata_size, NULL);
|
|
|
|
if ((checksumPassed = !memcmp (sha1, tor->info.hash, SHA_DIGEST_LENGTH)))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
/* checksum passed; now try to parse it as benc */
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant infoDict;
|
|
|
|
const int err = tr_variantFromBenc (&infoDict, m->metadata, m->metadata_size);
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "err is %d", err);
|
|
|
|
if ((metainfoParsed = !err))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-02-06 05:22:27 +00:00
|
|
|
/* yay we have bencoded metainfo... merge it into our .torrent file */
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant newMetainfo;
|
2012-12-05 17:29:46 +00:00
|
|
|
char * path = tr_strdup (tor->info.torrent);
|
2010-02-06 05:22:27 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!tr_variantFromFile (&newMetainfo, TR_VARIANT_FMT_BENC, path))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool hasInfo;
|
2010-06-24 20:36:05 +00:00
|
|
|
tr_info info;
|
|
|
|
int infoDictLength;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-02-06 05:22:27 +00:00
|
|
|
/* remove any old .torrent and .resume files */
|
2012-12-05 17:29:46 +00:00
|
|
|
remove (path);
|
|
|
|
tr_torrentRemoveResume (tor);
|
2010-02-06 05:22:27 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "Saving completed metadata to \"%s\"", path);
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantMergeDicts (tr_variantDictAddDict (&newMetainfo, TR_KEY_info, 0), &infoDict);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (&info, 0, sizeof (tr_info));
|
|
|
|
success = tr_metainfoParse (tor->session, &newMetainfo, &info, &hasInfo, &infoDictLength);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (success && !tr_getBlockSize (info.pieceSize))
|
2010-06-24 20:36:05 +00:00
|
|
|
{
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_torrentSetLocalError (tor, "%s", _("Magnet torrent's metadata is not usable"));
|
2011-03-22 15:19:54 +00:00
|
|
|
success = false;
|
2010-06-24 20:36:05 +00:00
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (success)
|
2010-06-24 20:36:05 +00:00
|
|
|
{
|
|
|
|
/* keep the new info */
|
|
|
|
tor->info = info;
|
|
|
|
tor->infoDictLength = infoDictLength;
|
|
|
|
|
|
|
|
/* save the new .torrent file */
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantToFile (&newMetainfo, TR_VARIANT_FMT_BENC, tor->info.torrent);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionSetTorrentFile (tor->session, tor->info.hashString, tor->info.torrent);
|
|
|
|
tr_torrentGotNewInfoDict (tor);
|
|
|
|
tr_torrentSetDirty (tor);
|
2010-06-24 20:36:05 +00:00
|
|
|
}
|
2009-11-24 19:45:36 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&newMetainfo);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&infoDict);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (path);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (success)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
incompleteMetadataFree (tor->incompleteMetadata);
|
2009-11-24 02:16:31 +00:00
|
|
|
tor->incompleteMetadata = NULL;
|
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
|
|
|
const int n = m->pieceCount;
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<n; ++i)
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2009-11-24 02:16:31 +00:00
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
|
|
|
}
|
|
|
|
m->piecesNeededCount = n;
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "metadata error; trying again. %d pieces left", n);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_err ("magnet status: checksum passed %d, metainfo parsed %d",
|
|
|
|
(int)checksumPassed, (int)metainfoParsed);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentGetNextMetadataRequest (tr_torrent * tor, time_t now, int * setme_piece)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool have_request = false;
|
2009-11-24 02:16:31 +00:00
|
|
|
struct tr_incomplete_metadata * m;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
m = tor->incompleteMetadata;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((m != NULL)
|
|
|
|
&& (m->piecesNeededCount > 0)
|
|
|
|
&& (m->piecesNeeded[0].requestedAt + MIN_REPEAT_INTERVAL_SECS < now))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int piece = m->piecesNeeded[0].piece;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_removeElementFromArray (m->piecesNeeded, 0,
|
|
|
|
sizeof (struct metadata_node),
|
|
|
|
m->piecesNeededCount--);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
i = m->piecesNeededCount++;
|
2009-11-24 17:10:40 +00:00
|
|
|
m->piecesNeeded[i].piece = piece;
|
|
|
|
m->piecesNeeded[i].requestedAt = now;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (tor, "next piece to request: %d", piece);
|
2009-11-24 02:16:31 +00:00
|
|
|
*setme_piece = piece;
|
2011-03-22 15:19:54 +00:00
|
|
|
have_request = true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return have_request;
|
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2010-07-01 05:14:34 +00:00
|
|
|
double
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentGetMetadataPercent (const tr_torrent * tor)
|
2009-11-24 17:10:40 +00:00
|
|
|
{
|
2010-07-01 05:14:34 +00:00
|
|
|
double ret;
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_torrentHasMetadata (tor))
|
2009-11-24 17:10:40 +00:00
|
|
|
ret = 1.0;
|
|
|
|
else {
|
|
|
|
const struct tr_incomplete_metadata * m = tor->incompleteMetadata;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!m || !m->pieceCount)
|
2009-11-24 17:10:40 +00:00
|
|
|
ret = 0.0;
|
|
|
|
else
|
2010-07-01 05:14:34 +00:00
|
|
|
ret = (m->pieceCount - m->piecesNeededCount) / (double)m->pieceCount;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
|
|
|
|
char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentInfoGetMagnetLink (const tr_info * inf)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2012-12-30 22:06:45 +00:00
|
|
|
unsigned int i;
|
2010-05-11 15:30:30 +00:00
|
|
|
const char * name;
|
2012-12-05 17:29:46 +00:00
|
|
|
struct evbuffer * s = evbuffer_new ();
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_add_printf (s, "magnet:?xt=urn:btih:%s", inf->hashString);
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2012-07-23 15:28:27 +00:00
|
|
|
name = inf->name;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (name && *name)
|
2010-05-11 15:30:30 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_add_printf (s, "%s", "&dn=");
|
|
|
|
tr_http_escape (s, name, -1, true);
|
2010-05-11 15:30:30 +00:00
|
|
|
}
|
2012-07-23 15:28:27 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->trackerCount; ++i)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_add_printf (s, "%s", "&tr=");
|
|
|
|
tr_http_escape (s, inf->trackers[i].announce, -1, true);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return evbuffer_free_to_str (s);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|