Piece.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "Piece.h"
  36. #include "util.h"
  37. #include "BitfieldMan.h"
  38. #include "A2STR.h"
  39. #include "util.h"
  40. #include "a2functional.h"
  41. #include "WrDiskCache.h"
  42. #include "WrDiskCacheEntry.h"
  43. #include "LogFactory.h"
  44. #include "fmt.h"
  45. #include "DiskAdaptor.h"
  46. #ifdef ENABLE_MESSAGE_DIGEST
  47. # include "MessageDigest.h"
  48. #endif // ENABLE_MESSAGE_DIGEST
  49. namespace aria2 {
  50. Piece::Piece():index_(0), length_(0), blockLength_(BLOCK_LENGTH), bitfield_(0),
  51. usedBySegment_(false), wrCache_(0)
  52. #ifdef ENABLE_MESSAGE_DIGEST
  53. , nextBegin_(0)
  54. #endif // ENABLE_MESSAGE_DIGEST
  55. {}
  56. Piece::Piece(size_t index, int32_t length, int32_t blockLength)
  57. : index_(index),
  58. length_(length),
  59. blockLength_(blockLength),
  60. bitfield_(new BitfieldMan(blockLength_, length)),
  61. usedBySegment_(false), wrCache_(0)
  62. #ifdef ENABLE_MESSAGE_DIGEST
  63. ,nextBegin_(0)
  64. #endif // ENABLE_MESSAGE_DIGEST
  65. {}
  66. Piece::~Piece()
  67. {
  68. delete wrCache_;
  69. delete bitfield_;
  70. }
  71. void Piece::completeBlock(size_t blockIndex) {
  72. bitfield_->setBit(blockIndex);
  73. bitfield_->unsetUseBit(blockIndex);
  74. }
  75. void Piece::clearAllBlock() {
  76. bitfield_->clearAllBit();
  77. bitfield_->clearAllUseBit();
  78. }
  79. void Piece::setAllBlock() {
  80. bitfield_->setAllBit();
  81. }
  82. bool Piece::pieceComplete() const {
  83. return bitfield_->isAllBitSet();
  84. }
  85. size_t Piece::countBlock() const
  86. {
  87. return bitfield_->countBlock();
  88. }
  89. int32_t Piece::getBlockLength(size_t index) const
  90. {
  91. return bitfield_->getBlockLength(index);
  92. }
  93. int32_t Piece::getBlockLength() const
  94. {
  95. return bitfield_->getBlockLength();
  96. }
  97. const unsigned char* Piece::getBitfield() const
  98. {
  99. return bitfield_->getBitfield();
  100. }
  101. size_t Piece::getBitfieldLength() const
  102. {
  103. return bitfield_->getBitfieldLength();
  104. }
  105. bool Piece::isBlockUsed(size_t index) const
  106. {
  107. return bitfield_->isUseBitSet(index);
  108. }
  109. void Piece::cancelBlock(size_t blockIndex) {
  110. bitfield_->unsetUseBit(blockIndex);
  111. }
  112. size_t Piece::countCompleteBlock() const
  113. {
  114. return bitfield_->countBlock()-bitfield_->countMissingBlock();
  115. }
  116. size_t Piece::countMissingBlock() const
  117. {
  118. return bitfield_->countMissingBlock();
  119. }
  120. bool Piece::hasBlock(size_t blockIndex) const
  121. {
  122. return bitfield_->isBitSet(blockIndex);
  123. }
  124. bool Piece::getMissingUnusedBlockIndex(size_t& index) const
  125. {
  126. if(bitfield_->getFirstMissingUnusedIndex(index)) {
  127. bitfield_->setUseBit(index);
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. size_t Piece::getMissingUnusedBlockIndex
  134. (std::vector<size_t>& indexes, size_t n) const
  135. {
  136. size_t num = bitfield_->getFirstNMissingUnusedIndex(indexes, n);
  137. if(num) {
  138. for(std::vector<size_t>::const_iterator i = indexes.end()-num,
  139. eoi = indexes.end(); i != eoi; ++i) {
  140. bitfield_->setUseBit(*i);
  141. }
  142. }
  143. return num;
  144. }
  145. bool Piece::getFirstMissingBlockIndexWithoutLock(size_t& index) const
  146. {
  147. return bitfield_->getFirstMissingIndex(index);
  148. }
  149. bool Piece::getAllMissingBlockIndexes
  150. (unsigned char* misbitfield, size_t mislen) const
  151. {
  152. return bitfield_->getAllMissingIndexes(misbitfield, mislen);
  153. }
  154. std::string Piece::toString() const {
  155. return fmt("piece: index=%lu, length=%d",
  156. static_cast<unsigned long>(index_), length_);
  157. }
  158. void Piece::reconfigure(int32_t length)
  159. {
  160. delete bitfield_;
  161. length_ = length;
  162. bitfield_ = new BitfieldMan(blockLength_, length_);
  163. }
  164. void Piece::setBitfield(const unsigned char* bitfield, size_t len)
  165. {
  166. bitfield_->setBitfield(bitfield, len);
  167. }
  168. int32_t Piece::getCompletedLength()
  169. {
  170. return bitfield_->getCompletedLength();
  171. }
  172. #ifdef ENABLE_MESSAGE_DIGEST
  173. void Piece::setHashType(const std::string& hashType)
  174. {
  175. hashType_ = hashType;
  176. }
  177. bool Piece::updateHash
  178. (int32_t begin, const unsigned char* data, size_t dataLength)
  179. {
  180. if(hashType_.empty()) {
  181. return false;
  182. }
  183. if(begin == nextBegin_ &&
  184. nextBegin_+dataLength <= static_cast<size_t>(length_)) {
  185. if(!mdctx_) {
  186. mdctx_ = MessageDigest::create(hashType_);
  187. }
  188. mdctx_->update(data, dataLength);
  189. nextBegin_ += dataLength;
  190. return true;
  191. } else {
  192. return false;
  193. }
  194. }
  195. bool Piece::isHashCalculated() const
  196. {
  197. return mdctx_ && nextBegin_ == length_;
  198. }
  199. std::string Piece::getDigest()
  200. {
  201. if(!mdctx_) {
  202. return A2STR::NIL;
  203. } else {
  204. std::string hash = mdctx_->digest();
  205. destroyHashContext();
  206. return hash;
  207. }
  208. }
  209. namespace {
  210. void updateHashWithRead(const SharedHandle<MessageDigest>& mdctx,
  211. const SharedHandle<DiskAdaptor>& adaptor,
  212. int64_t offset, size_t len)
  213. {
  214. const size_t BUFSIZE = 4096;
  215. unsigned char buf[BUFSIZE];
  216. ldiv_t res = ldiv(len, BUFSIZE);
  217. for(int j = 0; j < res.quot; ++j) {
  218. ssize_t nread = adaptor->readData(buf, BUFSIZE, offset);
  219. if((size_t)nread != BUFSIZE) {
  220. throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
  221. }
  222. mdctx->update(buf, nread);
  223. offset += nread;
  224. }
  225. if(res.rem) {
  226. ssize_t nread = adaptor->readData(buf, res.rem, offset);
  227. if(nread != res.rem) {
  228. throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
  229. }
  230. mdctx->update(buf, nread);
  231. offset += nread;
  232. }
  233. }
  234. } // namespace
  235. std::string Piece::getDigestWithWrCache
  236. (size_t pieceLength, const SharedHandle<DiskAdaptor>& adaptor)
  237. {
  238. SharedHandle<MessageDigest> mdctx(MessageDigest::create(hashType_));
  239. int64_t start = static_cast<int64_t>(index_)*pieceLength;
  240. int64_t goff = start;
  241. if(wrCache_) {
  242. const WrDiskCacheEntry::DataCellSet& dataSet = wrCache_->getDataSet();
  243. for(WrDiskCacheEntry::DataCellSet::iterator i = dataSet.begin(),
  244. eoi = dataSet.end(); i != eoi; ++i) {
  245. if(goff < (*i)->goff) {
  246. updateHashWithRead(mdctx, adaptor, goff, (*i)->goff - goff);
  247. }
  248. mdctx->update((*i)->data+(*i)->offset, (*i)->len);
  249. goff = (*i)->goff + (*i)->len;
  250. }
  251. updateHashWithRead(mdctx, adaptor, goff, start+length_-goff);
  252. } else {
  253. updateHashWithRead(mdctx, adaptor, goff, length_);
  254. }
  255. return mdctx->digest();
  256. }
  257. void Piece::destroyHashContext()
  258. {
  259. mdctx_.reset();
  260. nextBegin_ = 0;
  261. }
  262. #endif // ENABLE_MESSAGE_DIGEST
  263. bool Piece::usedBy(cuid_t cuid) const
  264. {
  265. return std::find(users_.begin(), users_.end(), cuid) != users_.end();
  266. }
  267. void Piece::addUser(cuid_t cuid)
  268. {
  269. if(std::find(users_.begin(), users_.end(), cuid) == users_.end()) {
  270. users_.push_back(cuid);
  271. }
  272. }
  273. void Piece::removeUser(cuid_t cuid)
  274. {
  275. users_.erase(std::remove(users_.begin(), users_.end(), cuid), users_.end());
  276. }
  277. void Piece::initWrCache(WrDiskCache* diskCache,
  278. const SharedHandle<DiskAdaptor>& diskAdaptor)
  279. {
  280. assert(wrCache_ == 0);
  281. wrCache_ = new WrDiskCacheEntry(diskAdaptor);
  282. bool rv = diskCache->add(wrCache_);
  283. assert(rv);
  284. }
  285. void Piece::flushWrCache(WrDiskCache* diskCache)
  286. {
  287. assert(wrCache_);
  288. ssize_t size = static_cast<ssize_t>(wrCache_->getSize());
  289. diskCache->update(wrCache_, -size);
  290. wrCache_->writeToDisk();
  291. }
  292. void Piece::clearWrCache(WrDiskCache* diskCache)
  293. {
  294. assert(wrCache_);
  295. ssize_t size = static_cast<ssize_t>(wrCache_->getSize());
  296. diskCache->update(wrCache_, -size);
  297. wrCache_->clear();
  298. }
  299. void Piece::updateWrCache(WrDiskCache* diskCache, unsigned char* data,
  300. size_t offset, size_t len, int64_t goff)
  301. {
  302. A2_LOG_DEBUG(fmt("updateWrCache entry=%p", wrCache_));
  303. assert(wrCache_);
  304. WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
  305. cell->goff = goff;
  306. cell->data = data;
  307. cell->offset = offset;
  308. cell->len = len;
  309. bool rv;
  310. rv = wrCache_->cacheData(cell);
  311. assert(rv);
  312. rv = diskCache->update(wrCache_, len);
  313. assert(rv);
  314. }
  315. void Piece::releaseWrCache(WrDiskCache* diskCache)
  316. {
  317. if(wrCache_) {
  318. diskCache->remove(wrCache_);
  319. delete wrCache_;
  320. wrCache_ = 0;
  321. }
  322. }
  323. } // namespace aria2