summaryrefslogtreecommitdiffstats
path: root/apps/ktcachecheck/singlecachechecker.cpp
blob: ab64fc3a4ab1c3cf51a6faedef1d945edc13c1c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include <util/log.h>
#include <util/file.h>
#include <util/error.h>
#include <util/array.h>
#include <util/functions.h>
#include <torrent/globals.h>
#include <torrent/torrent.h>
#include <torrent/chunkmanager.h>
#include "singlecachechecker.h"

using namespace bt;

namespace ktdebug
{

	SingleCacheChecker::SingleCacheChecker(bt::Torrent& tor): CacheChecker(tor)
	{}


	SingleCacheChecker::~SingleCacheChecker()
	{}


	void SingleCacheChecker::check(const TQString& cache, const TQString& index)
	{
		loadIndex(index);
		Uint32 num_chunks = tor.getNumChunks();
		File fptr;
		if (!fptr.open(cache,"rb"))
		{
			throw Error(TQString("Cannot open file : %1 : %2")
					.arg(cache).arg( fptr.errorString()));
		}

		Uint32 num_ok = 0,num_not_ok = 0,num_not_downloaded = 0,extra_ok = 0;

		Array<Uint8> buf((Uint32)tor.getChunkSize());
		for (Uint32 i = 0;i < num_chunks;i++)
		{
			if (i % 100 == 0)
				Out(SYS_GEN|LOG_DEBUG) << "Checked " << i << " chunks" << endl;
	//	Out() << "Chunk " << i << " : ";
			if (!fptr.eof())
			{
				Uint32 size = i == num_chunks - 1 && tor.getFileLength() % tor.getChunkSize() > 0 ?
						tor.getFileLength() % tor.getChunkSize() : (Uint32)tor.getChunkSize();
				fptr.seek(File::BEGIN,(Int64)i*tor.getChunkSize());
				fptr.read(buf,size);
				SHA1Hash h = SHA1Hash::generate(buf,size);
				bool ok = (h == tor.getHash(i));
				if (ok)
				{
					if (downloaded_chunks.count(i) == 0)
					{
						extra_ok++;
						extra_chunks.insert(i);
						continue;
					}
					num_ok++;
		//		Out() << "OK" << endl;
				}
				else
				{
					if (downloaded_chunks.count(i) == 0)
					{
						num_not_downloaded++;
						continue;
					}
					Out() << "Chunk " << i << " Failed :" << endl;
					Out() << "\tShould be : " << tor.getHash(i).toString() << endl;
					Out() << "\tIs        : " << h.toString() << endl;
					num_not_ok++;
					failed_chunks.insert(i);
				}
			
			}
			else
			{
				num_not_downloaded++;
			//Out() << "Not Downloaded" << endl;
			}
		}
		Out() << "Cache Check Summary" << endl;
		Out() << "===================" << endl;
		Out() << "Extra Chunks : " << extra_ok << endl;
		Out() << "Chunks OK : " << num_ok << endl;
		Out() << "Chunks Not Downloaded : " << num_not_downloaded << endl;
		Out() << "Chunks Failed : " << num_not_ok << endl;
	}

}