2008-02-15 16:00:46 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2008-02-15 16:00:46 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-02-15 16:00:46 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-06-23 21:16:33 +00:00
|
|
|
#if defined (HAVE_POSIX_FADVISE) && (!defined (_XOPEN_SOURCE) || _XOPEN_SOURCE < 600)
|
|
|
|
#undef _XOPEN_SOURCE
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#endif
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <string.h> /* memcmp () */
|
|
|
|
#include <stdlib.h> /* free () */
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2009-04-06 23:51:55 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <fcntl.h> /* posix_fadvise () */
|
2009-04-06 23:51:55 +00:00
|
|
|
#endif
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "completion.h"
|
2014-12-04 12:13:59 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-07-28 04:13:38 +00:00
|
|
|
#include "file.h"
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "list.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "platform.h" /* tr_lock () */
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "torrent.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "utils.h" /* tr_valloc (), tr_free () */
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "verify.h"
|
|
|
|
|
2009-04-06 04:02:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-09-06 14:05:06 +00:00
|
|
|
enum
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 100
|
2009-09-06 14:05:06 +00:00
|
|
|
};
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
verifyTorrent (tr_torrent * tor, bool * stopFlag)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
time_t end;
|
2014-12-04 12:13:59 +00:00
|
|
|
tr_sha1_ctx_t sha;
|
2014-07-28 04:13:38 +00:00
|
|
|
tr_sys_file_t fd = TR_BAD_SYS_FILE;
|
|
|
|
uint64_t filePos = 0;
|
2014-11-30 19:38:47 +00:00
|
|
|
bool changed = false;
|
|
|
|
bool hadPiece = false;
|
2012-12-05 17:29:46 +00:00
|
|
|
time_t lastSleptAt = 0;
|
|
|
|
uint32_t piecePos = 0;
|
|
|
|
tr_file_index_t fileIndex = 0;
|
|
|
|
tr_file_index_t prevFileIndex = !fileIndex;
|
|
|
|
tr_piece_index_t pieceIndex = 0;
|
|
|
|
const time_t begin = tr_time ();
|
|
|
|
const size_t buflen = 1024 * 128; /* 128 KiB buffer */
|
|
|
|
uint8_t * buffer = tr_valloc (buflen);
|
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
sha = tr_sha1_init ();
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddTorDbg (tor, "%s", "verifying torrent...");
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetChecked (tor, 0);
|
|
|
|
while (!*stopFlag && (pieceIndex < tor->info.pieceCount))
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
uint64_t leftInPiece;
|
|
|
|
uint64_t bytesThisPass;
|
2012-12-05 17:29:46 +00:00
|
|
|
uint64_t leftInFile;
|
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* if we're starting a new piece... */
|
|
|
|
if (piecePos == 0)
|
2013-08-18 13:06:39 +00:00
|
|
|
hadPiece = tr_torrentPieceIsComplete (tor, pieceIndex);
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* if we're starting a new file... */
|
2014-07-28 04:13:38 +00:00
|
|
|
if (filePos == 0 && fd == TR_BAD_SYS_FILE && fileIndex != prevFileIndex)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * filename = tr_torrentFindFile (tor, fileIndex);
|
2014-07-28 04:13:38 +00:00
|
|
|
fd = filename == NULL ? TR_BAD_SYS_FILE : tr_sys_file_open (filename,
|
|
|
|
TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (filename);
|
|
|
|
prevFileIndex = fileIndex;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* figure out how much we can read this pass */
|
|
|
|
leftInPiece = tr_torPieceCountBytes (tor, pieceIndex) - piecePos;
|
|
|
|
leftInFile = file->length - filePos;
|
|
|
|
bytesThisPass = MIN (leftInFile, leftInPiece);
|
|
|
|
bytesThisPass = MIN (bytesThisPass, buflen);
|
|
|
|
|
|
|
|
/* read a bit */
|
2014-07-28 04:13:38 +00:00
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
uint64_t numRead;
|
|
|
|
if (tr_sys_file_read_at (fd, buffer, bytesThisPass, filePos, &numRead, NULL) && numRead > 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
bytesThisPass = numRead;
|
2014-12-04 12:13:59 +00:00
|
|
|
tr_sha1_update (sha, buffer, bytesThisPass);
|
2009-12-25 15:48:41 +00:00
|
|
|
#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
|
2015-05-09 14:10:55 +00:00
|
|
|
(void) posix_fadvise (fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED);
|
2009-12-16 06:34:17 +00:00
|
|
|
#endif
|
2010-10-19 13:56:58 +00:00
|
|
|
}
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* move our offsets */
|
|
|
|
leftInPiece -= bytesThisPass;
|
|
|
|
leftInFile -= bytesThisPass;
|
|
|
|
piecePos += bytesThisPass;
|
|
|
|
filePos += bytesThisPass;
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* if we're finishing a piece... */
|
|
|
|
if (leftInPiece == 0)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
time_t now;
|
|
|
|
bool hasPiece;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
tr_sha1_final (sha, hash);
|
2016-03-13 22:11:01 +00:00
|
|
|
hasPiece = memcmp (hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH) == 0;
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (hasPiece || hadPiece)
|
|
|
|
{
|
|
|
|
tr_torrentSetHasPiece (tor, pieceIndex, hasPiece);
|
|
|
|
changed |= hasPiece != hadPiece;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
tr_torrentSetPieceChecked (tor, pieceIndex);
|
|
|
|
now = tr_time ();
|
|
|
|
tor->anyDate = now;
|
|
|
|
|
|
|
|
/* sleeping even just a few msec per second goes a long
|
|
|
|
* way towards reducing IO load... */
|
|
|
|
if (lastSleptAt != now)
|
|
|
|
{
|
|
|
|
lastSleptAt = now;
|
|
|
|
tr_wait_msec (MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY);
|
2009-09-06 14:05:06 +00:00
|
|
|
}
|
2009-08-14 20:55:22 +00:00
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
sha = tr_sha1_init ();
|
2012-12-05 17:29:46 +00:00
|
|
|
pieceIndex++;
|
|
|
|
piecePos = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* if we're finishing a file... */
|
|
|
|
if (leftInFile == 0)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
tr_sys_file_close (fd, NULL);
|
|
|
|
fd = TR_BAD_SYS_FILE;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
fileIndex++;
|
|
|
|
filePos = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* cleanup */
|
2014-07-28 04:13:38 +00:00
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
|
|
|
tr_sys_file_close (fd, NULL);
|
2014-12-04 12:13:59 +00:00
|
|
|
tr_sha1_final (sha, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
free (buffer);
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* stopwatch */
|
|
|
|
end = tr_time ();
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddTorDbg (tor, "Verification is done. It took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
|
2012-12-05 17:29:46 +00:00
|
|
|
(int)(end-begin), tor->info.totalSize,
|
|
|
|
(uint64_t)(tor->info.totalSize/ (1+ (end-begin))));
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return changed;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-02-15 16:00:46 +00:00
|
|
|
|
|
|
|
struct verify_node
|
|
|
|
{
|
2013-01-31 21:58:25 +00:00
|
|
|
tr_torrent * torrent;
|
|
|
|
tr_verify_done_func callback_func;
|
|
|
|
void * callback_data;
|
|
|
|
uint64_t current_size;
|
2008-02-15 16:00:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct verify_node currentNode;
|
2009-01-30 00:41:08 +00:00
|
|
|
static tr_list * verifyList = NULL;
|
|
|
|
static tr_thread * verifyThread = NULL;
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool stopCurrent = false;
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_lock*
|
2012-12-05 17:29:46 +00:00
|
|
|
getVerifyLock (void)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
static tr_lock * lock = NULL;
|
|
|
|
|
|
|
|
if (lock == NULL)
|
|
|
|
lock = tr_lockNew ();
|
|
|
|
|
|
|
|
return lock;
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
verifyThreadFunc (void * unused UNUSED)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
for (;;)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int changed = 0;
|
|
|
|
tr_torrent * tor;
|
|
|
|
struct verify_node * node;
|
|
|
|
|
|
|
|
tr_lockLock (getVerifyLock ());
|
|
|
|
stopCurrent = false;
|
|
|
|
node = (struct verify_node*) verifyList ? verifyList->data : NULL;
|
|
|
|
if (node == NULL)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
currentNode.torrent = NULL;
|
|
|
|
break;
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
currentNode = *node;
|
|
|
|
tor = currentNode.torrent;
|
|
|
|
tr_list_remove_data (&verifyList, node);
|
|
|
|
tr_free (node);
|
|
|
|
tr_lockUnlock (getVerifyLock ());
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddTorInfo (tor, "%s", _("Verifying torrent"));
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetVerifyState (tor, TR_VERIFY_NOW);
|
|
|
|
changed = verifyTorrent (tor, &stopCurrent);
|
|
|
|
tr_torrentSetVerifyState (tor, TR_VERIFY_NONE);
|
|
|
|
assert (tr_isTorrent (tor));
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2013-01-31 21:58:25 +00:00
|
|
|
if (!stopCurrent && changed)
|
|
|
|
tr_torrentSetDirty (tor);
|
|
|
|
|
|
|
|
if (currentNode.callback_func)
|
|
|
|
(*currentNode.callback_func)(tor, stopCurrent, currentNode.callback_data);
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
verifyThread = NULL;
|
|
|
|
tr_lockUnlock (getVerifyLock ());
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 15:31:11 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compareVerifyByPriorityAndSize (const void * va, const void * vb)
|
2010-11-11 15:31:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct verify_node * a = va;
|
|
|
|
const struct verify_node * b = vb;
|
|
|
|
|
|
|
|
/* higher priority comes before lower priority */
|
|
|
|
const tr_priority_t pa = tr_torrentGetPriority (a->torrent);
|
|
|
|
const tr_priority_t pb = tr_torrentGetPriority (b->torrent);
|
|
|
|
if (pa != pb)
|
|
|
|
return pa > pb ? -1 : 1;
|
|
|
|
|
|
|
|
/* smaller torrents come before larger ones because they verify faster */
|
|
|
|
if (a->current_size < b->current_size)
|
|
|
|
return -1;
|
|
|
|
if (a->current_size > b->current_size)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2010-11-11 15:31:11 +00:00
|
|
|
}
|
2010-07-19 14:44:24 +00:00
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
void
|
2013-01-31 21:58:25 +00:00
|
|
|
tr_verifyAdd (tr_torrent * tor,
|
|
|
|
tr_verify_done_func callback_func,
|
|
|
|
void * callback_data)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct verify_node * node;
|
|
|
|
|
|
|
|
assert (tr_isTorrent (tor));
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddTorInfo (tor, "%s", _("Queued for verification"));
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
node = tr_new (struct verify_node, 1);
|
|
|
|
node->torrent = tor;
|
2013-01-31 21:58:25 +00:00
|
|
|
node->callback_func = callback_func;
|
|
|
|
node->callback_data = callback_data;
|
2012-12-05 17:29:46 +00:00
|
|
|
node->current_size = tr_torrentGetCurrentSizeOnDisk (tor);
|
|
|
|
|
|
|
|
tr_lockLock (getVerifyLock ());
|
|
|
|
tr_torrentSetVerifyState (tor, TR_VERIFY_WAIT);
|
|
|
|
tr_list_insert_sorted (&verifyList, node, compareVerifyByPriorityAndSize);
|
|
|
|
if (verifyThread == NULL)
|
|
|
|
verifyThread = tr_threadNew (verifyThreadFunc, NULL);
|
|
|
|
tr_lockUnlock (getVerifyLock ());
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compareVerifyByTorrent (const void * va, const void * vb)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct verify_node * a = va;
|
|
|
|
const tr_torrent * b = vb;
|
|
|
|
return a->torrent - b;
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_verifyRemove (tr_torrent * tor)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lock * lock = getVerifyLock ();
|
|
|
|
tr_lockLock (lock);
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-01-30 00:41:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor == currentNode.torrent)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
stopCurrent = true;
|
|
|
|
|
|
|
|
while (stopCurrent)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockUnlock (lock);
|
|
|
|
tr_wait_msec (100);
|
|
|
|
tr_lockLock (lock);
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2013-02-01 00:21:30 +00:00
|
|
|
struct verify_node * node = tr_list_remove (&verifyList, tor, compareVerifyByTorrent);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetVerifyState (tor, TR_VERIFY_NONE);
|
2013-02-01 00:21:30 +00:00
|
|
|
|
|
|
|
if (node != NULL)
|
|
|
|
{
|
|
|
|
if (node->callback_func != NULL)
|
|
|
|
(*node->callback_func)(tor, true, node->callback_data);
|
|
|
|
|
|
|
|
tr_free (node);
|
|
|
|
}
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockUnlock (lock);
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-16 13:10:25 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_verifyClose (tr_session * session UNUSED)
|
2009-04-16 13:10:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockLock (getVerifyLock ());
|
2009-04-16 13:10:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
stopCurrent = true;
|
|
|
|
tr_list_free (&verifyList, tr_free);
|
2009-04-16 13:10:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_lockUnlock (getVerifyLock ());
|
2009-04-16 13:10:25 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|