Piece.cc 9.9 KB

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