1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
39
#include "data/chunk_list.h"
40
#include "torrent/exceptions.h"
42
#include "hash_torrent.h"
43
#include "hash_queue.h"
48
HashTorrent::HashTorrent(ChunkList* c) :
57
HashTorrent::start(bool tryQuick) {
58
if (m_position == m_chunkList->size())
61
if (m_position > 0 || m_chunkList->empty())
62
throw internal_error("HashTorrent::start() call failed.");
67
return m_position == m_chunkList->size();
71
HashTorrent::clear() {
77
rak::priority_queue_erase(&taskScheduler, &m_delayChecked);
81
HashTorrent::is_checked() {
82
// When closed the chunk list is empty. Position can be equal to
83
// chunk list for a short while as we have outstanding chunks, so
85
return !m_chunkList->empty() && m_position == m_chunkList->size() && m_outstanding == -1;
88
// After all chunks are checked it won't show as is_checked until
89
// after this function is called. This allows for the hash done signal
92
HashTorrent::confirm_checked() {
93
if (m_outstanding != 0)
94
throw internal_error("HashTorrent::confirm_checked() m_outstanding != 0.");
100
HashTorrent::receive_chunkdone() {
101
if (m_outstanding <= 0)
102
throw internal_error("HashTorrent::receive_chunkdone() m_outstanding <= 0.");
104
// m_signalChunk will always point to
105
// DownloadMain::receive_hash_done, so it will take care of cleanup.
107
// Make sure we call chunkdone before torrentDone has a chance to
114
// Mark unsuccessful checks so that if we have just stopped the
115
// hash checker it will ensure those pieces get rechecked upon
118
HashTorrent::receive_chunk_cleared(uint32_t index) {
119
if (m_outstanding <= 0)
120
throw internal_error("HashTorrent::receive_chunk_cleared() m_outstanding < 0.");
122
if (m_ranges.has(index))
123
throw internal_error("HashTorrent::receive_chunk_cleared() m_ranges.has(index).");
126
m_ranges.insert(index, index + 1);
130
HashTorrent::queue(bool quick) {
132
throw internal_error("HashTorrent::queue() called but it's not running.");
134
while (m_position < m_chunkList->size()) {
135
if (m_outstanding >= 30)
138
// Not very efficient, but this is seldomly done.
139
Ranges::iterator itr = m_ranges.find(m_position);
141
if (itr == m_ranges.end()) {
142
m_position = m_chunkList->size();
144
} else if (m_position < itr->first) {
145
m_position = itr->first;
148
// Need to do increment later if we're going to support resume
149
// hashing a quick hashed torrent.
150
ChunkHandle handle = m_chunkList->get(m_position, false);
153
// We're not actually interested in doing any hashing, so just
154
// skip what we know is not possible to hash.
156
// If the file does not exist then no valid error number is
159
if (m_outstanding != 0)
160
throw internal_error("HashTorrent::queue() quick hashing but m_outstanding != 0.");
162
if (handle.is_valid())
163
return m_chunkList->release(&handle);
165
if (handle.error_number().is_valid() && handle.error_number().value() != rak::error_number::e_noent)
172
// If the error number is not valid, then we've just encountered a
173
// file that hasn't be created/resized. Which means we ignore it
174
// when doing initial hashing.
175
if (handle.error_number().is_valid() && handle.error_number().value() != rak::error_number::e_noent) {
176
if (handle.is_valid())
177
throw internal_error("HashTorrent::queue() error, but handle.is_valid().");
179
// We wait for all the outstanding chunks to be checked before
180
// borking completely, else low-memory devices might not be able
181
// to finish the hash check.
182
if (m_outstanding != 0)
185
// The rest of the outstanding chunks get ignored by
186
// DownloadWrapper::receive_hash_done. Obsolete.
189
m_errno = handle.error_number().value();
191
// rak::priority_queue_erase(&taskScheduler, &m_delayChecked);
192
rak::priority_queue_insert(&taskScheduler, &m_delayChecked, cachedTime);
198
if (!handle.is_valid() && !handle.error_number().is_valid())
199
throw internal_error("Hash torrent errno == 0.");
201
// Missing file, skip the hash check.
202
if (!handle.is_valid())
210
if (m_outstanding == 0) {
211
// Erase the scheduled item just to make sure that if hashing is
212
// started again during the delay it won't cause an exception.
213
rak::priority_queue_erase(&taskScheduler, &m_delayChecked);
214
rak::priority_queue_insert(&taskScheduler, &m_delayChecked, cachedTime);