summaryrefslogtreecommitdiffstats
path: root/apps/ktcachecheck/multicachechecker.cpp
blob: 3132e12fce9fc30547b6149887708b2a6eb9b640 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   [email protected]                                               *
 *                                                                         *
 *   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 "multicachechecker.h"

using namespace bt;

namespace ktdebug
{

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


	MultiCacheChecker::~MultiCacheChecker()
	{}


	void MultiCacheChecker::check(const TQString& cache_dir, const TQString& index)
	{
		loadIndex(index);
		TQString cache = cache_dir;
		if (!cache.endsWith(bt::DirSeparator()))
			cache += bt::DirSeparator();

		Uint32 num_chunks = tor.getNumChunks();
		Uint64 curr_file_off = 0;
		Uint32 curr_file = 0;
		Uint64 chunk_size = tor.getChunkSize();

		Array<Uint8> buf((Uint32)tor.getChunkSize());
		Uint32 num_ok = 0,num_not_ok = 0,num_not_downloaded = 0,extra_ok = 0;
	
		for (Uint32 i = 0;i < num_chunks;i++)
		{
			if (i % 100 == 0)
				Out() << "Checked " << i << " chunks" << endl;


			
			Uint64 size = chunk_size;
			if (i == tor.getNumChunks() - 1 && tor.getFileLength() % chunk_size != 0)
				size = tor.getFileLength() % chunk_size;

			//Out() << "Loading chunk (size = " << size << ")" << endl;
			Uint64 bytes_offset = 0;
			while (bytes_offset < size)
			{
				TorrentFile & tf = tor.getFile(curr_file);
// 				Out() << "Current file : " << tf.getPath() << " (" << curr_file << ")" << endl;
				Uint64 to_read = size - bytes_offset;
// 				Out() << "to_read = " << to_read << endl;
				if (to_read <= tf.getSize() - curr_file_off)
				{
					// we can read the chunk from this file
					File fptr;
					if (!fptr.open(cache + tf.getPath(),"rb"))
						throw Error(TQString("Cannot open %1 : %2").tqarg(cache + tf.getPath()).tqarg(fptr.errorString()));

					fptr.seek(File::BEGIN,curr_file_off);
					fptr.read(buf + bytes_offset,to_read);
					bytes_offset += to_read;
					curr_file_off += to_read;
				}
				else
				{
					
					// read partially the data which can be read
					to_read = tf.getSize() - curr_file_off;
// 					Out() << "Partially reading " << to_read << endl;
					File fptr;
					if (!fptr.open(cache + tf.getPath(),"rb"))
						throw Error(TQString("Cannot open %1 : %2").tqarg(cache + tf.getPath()).tqarg(fptr.errorString()));

					fptr.seek(File::BEGIN,curr_file_off);
					fptr.read(buf + bytes_offset,to_read);
					bytes_offset += to_read;
					// update curr_file and offset
					curr_file++;
					curr_file_off = 0;
				}
			} // end file reading while

			// calculate hash and check it
			SHA1Hash h = SHA1Hash::generate(buf,size);
			if (h != tor.getHash(i))
			{
				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
			{
				if (downloaded_chunks.count(i) == 0)
				{
					extra_ok++;
					extra_chunks.insert(i);
					continue;
				}
				num_ok++;
			}
		}

		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;
	}

}