IteratableChunkChecksumValidator.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "IteratableChunkChecksumValidator.h"
  36. #include <array>
  37. #include <cstring>
  38. #include <cstdlib>
  39. #include "util.h"
  40. #include "message.h"
  41. #include "DiskAdaptor.h"
  42. #include "FileEntry.h"
  43. #include "RecoverableException.h"
  44. #include "DownloadContext.h"
  45. #include "PieceStorage.h"
  46. #include "BitfieldMan.h"
  47. #include "LogFactory.h"
  48. #include "Logger.h"
  49. #include "MessageDigest.h"
  50. #include "fmt.h"
  51. #include "DlAbortEx.h"
  52. namespace aria2 {
  53. IteratableChunkChecksumValidator::IteratableChunkChecksumValidator(
  54. const std::shared_ptr<DownloadContext>& dctx,
  55. const std::shared_ptr<PieceStorage>& pieceStorage)
  56. : dctx_(dctx),
  57. pieceStorage_(pieceStorage),
  58. bitfield_(make_unique<BitfieldMan>(dctx_->getPieceLength(),
  59. dctx_->getTotalLength())),
  60. currentIndex_(0)
  61. {
  62. }
  63. IteratableChunkChecksumValidator::~IteratableChunkChecksumValidator() = default;
  64. void IteratableChunkChecksumValidator::validateChunk()
  65. {
  66. if (!finished()) {
  67. std::string actualChecksum;
  68. try {
  69. actualChecksum = calculateActualChecksum();
  70. if (actualChecksum == dctx_->getPieceHashes()[currentIndex_]) {
  71. bitfield_->setBit(currentIndex_);
  72. }
  73. else {
  74. A2_LOG_INFO(
  75. fmt(EX_INVALID_CHUNK_CHECKSUM,
  76. static_cast<unsigned long>(currentIndex_),
  77. static_cast<int64_t>(getCurrentOffset()),
  78. util::toHex(dctx_->getPieceHashes()[currentIndex_]).c_str(),
  79. util::toHex(actualChecksum).c_str()));
  80. bitfield_->unsetBit(currentIndex_);
  81. }
  82. }
  83. catch (RecoverableException& ex) {
  84. A2_LOG_DEBUG_EX(fmt("Caught exception while validating piece index=%lu."
  85. " Some part of file may be missing."
  86. " Continue operation.",
  87. static_cast<unsigned long>(currentIndex_)),
  88. ex);
  89. bitfield_->unsetBit(currentIndex_);
  90. }
  91. ++currentIndex_;
  92. if (finished()) {
  93. pieceStorage_->setBitfield(bitfield_->getBitfield(),
  94. bitfield_->getBitfieldLength());
  95. }
  96. }
  97. }
  98. std::string IteratableChunkChecksumValidator::calculateActualChecksum()
  99. {
  100. int64_t offset = getCurrentOffset();
  101. size_t length;
  102. // When validating last piece
  103. if (currentIndex_ + 1 == dctx_->getNumPieces()) {
  104. length = dctx_->getTotalLength() - offset;
  105. }
  106. else {
  107. length = dctx_->getPieceLength();
  108. }
  109. return digest(offset, length);
  110. }
  111. void IteratableChunkChecksumValidator::init()
  112. {
  113. ctx_ = MessageDigest::create(dctx_->getPieceHashType());
  114. bitfield_->clearAllBit();
  115. currentIndex_ = 0;
  116. }
  117. std::string IteratableChunkChecksumValidator::digest(int64_t offset,
  118. size_t length)
  119. {
  120. std::array<unsigned char, 4_k> buf;
  121. ctx_->reset();
  122. int64_t max = offset + length;
  123. while (offset < max) {
  124. size_t r = pieceStorage_->getDiskAdaptor()->readDataDropCache(
  125. buf.data(), std::min(static_cast<int64_t>(buf.size()), max - offset),
  126. offset);
  127. if (r == 0) {
  128. throw DL_ABORT_EX(
  129. fmt(EX_FILE_READ, dctx_->getBasePath().c_str(), "data is too short"));
  130. }
  131. ctx_->update(buf.data(), r);
  132. offset += r;
  133. }
  134. return ctx_->digest();
  135. }
  136. bool IteratableChunkChecksumValidator::finished() const
  137. {
  138. if (currentIndex_ >= dctx_->getNumPieces()) {
  139. return true;
  140. }
  141. else {
  142. return false;
  143. }
  144. }
  145. int64_t IteratableChunkChecksumValidator::getCurrentOffset() const
  146. {
  147. return static_cast<int64_t>(currentIndex_) * dctx_->getPieceLength();
  148. }
  149. int64_t IteratableChunkChecksumValidator::getTotalLength() const
  150. {
  151. return dctx_->getTotalLength();
  152. }
  153. } // namespace aria2