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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/**************************************************************************
* Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net> *
* *
* 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 "verificationthread.h"
#include "verifier.h"
#include <KDebug>
#include <QtCore/QFile>
VerificationThread::VerificationThread(QObject *parent)
: QThread(parent),
m_abort(false),
m_length(0),
m_type(Nothing)
{
}
VerificationThread::~VerificationThread()
{
m_mutex.lock();
m_abort = true;
m_mutex.unlock();
wait();
}
void VerificationThread::verifiy(const QString &type, const QString &checksum, const KUrl &file)
{
QMutexLocker locker(&m_mutex);
m_types.append(type);
m_checksums.append(checksum);
m_files.append(file);
m_type = Verify;
if (!isRunning())
{
start();
}
}
void VerificationThread::findBrokenPieces(const QString &type, const QList<QString> checksums, KIO::filesize_t length, const KUrl &file)
{
QMutexLocker locker(&m_mutex);
m_types.clear();
m_types.append(type);
m_checksums = checksums;
m_files.clear();
m_files.append(file);
m_length = length;
m_type = BrokenPieces;
if (!isRunning())
{
start();
}
}
void VerificationThread::run()
{
if (m_type == Nothing)
{
return;
}
if (m_type == Verify)
{
doVerify();
}
else if (m_type == BrokenPieces)
{
doBrokenPieces();
}
}
void VerificationThread::doVerify()
{
m_mutex.lock();
bool run = m_files.count();
m_mutex.unlock();
while (run && !m_abort)
{
m_mutex.lock();
const QString type = m_types.takeFirst();
const QString checksum = m_checksums.takeFirst();
const KUrl url = m_files.takeFirst();
m_mutex.unlock();
if (type.isEmpty() || checksum.isEmpty())
{
m_mutex.lock();
run = m_files.count();
m_mutex.unlock();
continue;
}
const QString hash = Verifier::checksum(url, type, &m_abort);
kDebug(5001) << "Type:" << type << "Calculated checksum:" << hash << "Entered checksum:" << checksum;
const bool fileVerified = (hash == checksum);
if (m_abort)
{
return;
}
m_mutex.lock();
if (!m_abort)
{
emit verified(type, fileVerified, url);
emit verified(fileVerified);
}
run = m_files.count();
m_mutex.unlock();
}
}
void VerificationThread::doBrokenPieces()
{
m_mutex.lock();
const QString type = m_types.takeFirst();
const QStringList checksums = m_checksums;
m_checksums.clear();
const KUrl url = m_files.takeFirst();
const KIO::filesize_t length = m_length;
m_mutex.unlock();
QList<KIO::fileoffset_t> broken;
if (QFile::exists(url.pathOrUrl()))
{
QFile file(url.pathOrUrl());
if (!file.open(QIODevice::ReadOnly))
{
emit brokenPieces(broken, length);
return;
}
const KIO::filesize_t fileSize = file.size();
if (!length || !fileSize)
{
emit brokenPieces(broken, length);
return;
}
const QStringList fileChecksums = Verifier::partialChecksums(url, type, length, &m_abort).checksums();
if (m_abort)
{
emit brokenPieces(broken, length);
return;
}
if (fileChecksums.size() != checksums.size())
{
kDebug(5001) << "Number of checksums differs!";
emit brokenPieces(broken, length);
return;
}
for (int i = 0; i < checksums.size(); ++i)
{
if (fileChecksums.at(i) != checksums.at(i))
{
const int brokenStart = length * i;
kDebug(5001) << url << "broken segment" << i << "start" << brokenStart << "length" << length;
broken.append(brokenStart);
}
}
}
emit brokenPieces(broken, length);
}
|