File indexing completed on 2024-11-24 04:31:14

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "chunk.h"
0008 #include "cache.h"
0009 #include "piecedata.h"
0010 #include <util/sha1hash.h>
0011 #ifndef Q_WS_WIN
0012 #include <util/signalcatcher.h>
0013 #endif
0014 
0015 namespace bt
0016 {
0017 Chunk::Chunk(Uint32 index, Uint32 size, Cache *cache)
0018     : status(Chunk::NOT_DOWNLOADED)
0019     , index(index)
0020     , size(size)
0021     , priority(NORMAL_PRIORITY)
0022     , cache(cache)
0023 {
0024 }
0025 
0026 Chunk::~Chunk()
0027 {
0028 }
0029 
0030 bool Chunk::readPiece(Uint32 off, Uint32 len, Uint8 *data)
0031 {
0032     PieceData::Ptr d = cache->loadPiece(this, off, len);
0033     if (d && d->ok())
0034         return d->read(data, len) == len;
0035     else
0036         return false;
0037 }
0038 
0039 bool Chunk::checkHash(const SHA1Hash &h)
0040 {
0041     PieceData::Ptr d = getPiece(0, size, true);
0042     if (!d || !d->ok())
0043         return false;
0044     else
0045         return d->generateHash() == h;
0046 }
0047 
0048 PieceData::Ptr Chunk::getPiece(Uint32 off, Uint32 len, bool read_only)
0049 {
0050     if (read_only)
0051         return cache->loadPiece(this, off, len);
0052     else
0053         return cache->preparePiece(this, off, len);
0054 }
0055 
0056 void Chunk::savePiece(PieceData::Ptr piece)
0057 {
0058     cache->savePiece(piece);
0059 }
0060 }