IteratableChunkChecksumValidator.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. IteratableChunkChecksumValidator::~IteratableChunkChecksumValidator() {}
  63. void IteratableChunkChecksumValidator::validateChunk()
  64. {
  65. if(!finished()) {
  66. std::string actualChecksum;
  67. try {
  68. actualChecksum = calculateActualChecksum();
  69. if(actualChecksum == dctx_->getPieceHashes()[currentIndex_]) {
  70. bitfield_->setBit(currentIndex_);
  71. } else {
  72. A2_LOG_INFO
  73. (fmt(EX_INVALID_CHUNK_CHECKSUM,
  74. static_cast<unsigned long>(currentIndex_),
  75. static_cast<int64_t>(getCurrentOffset()),
  76. util::toHex(dctx_->getPieceHashes()[currentIndex_]).c_str(),
  77. util::toHex(actualChecksum).c_str()));
  78. bitfield_->unsetBit(currentIndex_);
  79. }
  80. } catch(RecoverableException& ex) {
  81. A2_LOG_DEBUG_EX(fmt("Caught exception while validating piece index=%lu."
  82. " Some part of file may be missing."
  83. " Continue operation.",
  84. static_cast<unsigned long>(currentIndex_)),
  85. ex);
  86. bitfield_->unsetBit(currentIndex_);
  87. }
  88. ++currentIndex_;
  89. if(finished()) {
  90. pieceStorage_->setBitfield(bitfield_->getBitfield(), bitfield_->getBitfieldLength());
  91. }
  92. }
  93. }
  94. std::string IteratableChunkChecksumValidator::calculateActualChecksum()
  95. {
  96. int64_t offset = getCurrentOffset();
  97. size_t length;
  98. // When validating last piece
  99. if(currentIndex_+1 == dctx_->getNumPieces()) {
  100. length = dctx_->getTotalLength()-offset;
  101. } else {
  102. length = dctx_->getPieceLength();
  103. }
  104. return digest(offset, length);
  105. }
  106. void IteratableChunkChecksumValidator::init()
  107. {
  108. ctx_ = MessageDigest::create(dctx_->getPieceHashType());
  109. bitfield_->clearAllBit();
  110. currentIndex_ = 0;
  111. }
  112. std::string IteratableChunkChecksumValidator::digest(int64_t offset, size_t length)
  113. {
  114. std::array<unsigned char, 4_k> buf;
  115. ctx_->reset();
  116. int64_t max = offset+length;
  117. while(offset < max) {
  118. size_t r = pieceStorage_->getDiskAdaptor()->readDataDropCache(
  119. buf.data(), std::min(static_cast<int64_t>(buf.size()), max - offset),
  120. offset);
  121. if(r == 0) {
  122. throw DL_ABORT_EX
  123. (fmt(EX_FILE_READ, dctx_->getBasePath().c_str(),
  124. "data is too short"));
  125. }
  126. ctx_->update(buf.data(), r);
  127. offset += r;
  128. }
  129. return ctx_->digest();
  130. }
  131. bool IteratableChunkChecksumValidator::finished() const
  132. {
  133. if(currentIndex_ >= dctx_->getNumPieces()) {
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. int64_t IteratableChunkChecksumValidator::getCurrentOffset() const
  140. {
  141. return static_cast<int64_t>(currentIndex_)*dctx_->getPieceLength();
  142. }
  143. int64_t IteratableChunkChecksumValidator::getTotalLength() const
  144. {
  145. return dctx_->getTotalLength();
  146. }
  147. } // namespace aria2