UnknownLengthPieceStorage.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "UnknownLengthPieceStorage.h"
  36. #include <cstdlib>
  37. #include "DefaultDiskWriter.h"
  38. #include "DirectDiskAdaptor.h"
  39. #include "prefs.h"
  40. #include "DefaultDiskWriterFactory.h"
  41. #include "DownloadContext.h"
  42. #include "Piece.h"
  43. #include "FileEntry.h"
  44. #include "BitfieldMan.h"
  45. namespace aria2 {
  46. UnknownLengthPieceStorage::UnknownLengthPieceStorage(
  47. const std::shared_ptr<DownloadContext>& downloadContext)
  48. : downloadContext_(downloadContext),
  49. diskWriterFactory_(std::make_shared<DefaultDiskWriterFactory>()),
  50. totalLength_(0),
  51. downloadFinished_(false)
  52. {
  53. }
  54. UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
  55. void UnknownLengthPieceStorage::initStorage()
  56. {
  57. auto directDiskAdaptor = std::make_shared<DirectDiskAdaptor>();
  58. directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
  59. directDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
  60. downloadContext_->getFileEntries().end());
  61. directDiskAdaptor->setDiskWriter(
  62. diskWriterFactory_->newDiskWriter(directDiskAdaptor->getFilePath()));
  63. diskAdaptor_ = std::move(directDiskAdaptor);
  64. }
  65. #ifdef ENABLE_BITTORRENT
  66. bool UnknownLengthPieceStorage::hasMissingPiece(
  67. const std::shared_ptr<Peer>& peer)
  68. {
  69. abort();
  70. }
  71. void UnknownLengthPieceStorage::getMissingPiece(
  72. std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
  73. const std::shared_ptr<Peer>& peer, cuid_t cuid)
  74. {
  75. abort();
  76. }
  77. void UnknownLengthPieceStorage::getMissingPiece(
  78. std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
  79. const std::shared_ptr<Peer>& peer,
  80. const std::vector<size_t>& excludedIndexes, cuid_t cuid)
  81. {
  82. abort();
  83. }
  84. void UnknownLengthPieceStorage::getMissingFastPiece(
  85. std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
  86. const std::shared_ptr<Peer>& peer, cuid_t cuid)
  87. {
  88. abort();
  89. }
  90. void UnknownLengthPieceStorage::getMissingFastPiece(
  91. std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
  92. const std::shared_ptr<Peer>& peer,
  93. const std::vector<size_t>& excludedIndexes, cuid_t cuid)
  94. {
  95. abort();
  96. }
  97. std::shared_ptr<Piece>
  98. UnknownLengthPieceStorage::getMissingPiece(const std::shared_ptr<Peer>& peer,
  99. cuid_t cuid)
  100. {
  101. abort();
  102. }
  103. std::shared_ptr<Piece> UnknownLengthPieceStorage::getMissingPiece(
  104. const std::shared_ptr<Peer>& peer,
  105. const std::vector<size_t>& excludedIndexes, cuid_t cuid)
  106. {
  107. abort();
  108. }
  109. #endif // ENABLE_BITTORRENT
  110. bool UnknownLengthPieceStorage::hasMissingUnusedPiece() { abort(); }
  111. std::shared_ptr<Piece>
  112. UnknownLengthPieceStorage::getMissingPiece(size_t minSplitSize,
  113. const unsigned char* ignoreBitfield,
  114. size_t length, cuid_t cuid)
  115. {
  116. if (downloadFinished_) {
  117. return nullptr;
  118. }
  119. if (!piece_) {
  120. piece_ = std::make_shared<Piece>();
  121. return piece_;
  122. }
  123. else {
  124. return nullptr;
  125. }
  126. }
  127. std::shared_ptr<Piece> UnknownLengthPieceStorage::getMissingPiece(size_t index,
  128. cuid_t cuid)
  129. {
  130. if (index == 0) {
  131. return getMissingPiece(0, nullptr, 0, cuid);
  132. }
  133. else {
  134. return nullptr;
  135. }
  136. }
  137. std::shared_ptr<Piece> UnknownLengthPieceStorage::getPiece(size_t index)
  138. {
  139. if (index == 0) {
  140. if (!piece_) {
  141. return std::make_shared<Piece>();
  142. }
  143. else {
  144. return piece_;
  145. }
  146. }
  147. else {
  148. return nullptr;
  149. }
  150. }
  151. void UnknownLengthPieceStorage::completePiece(
  152. const std::shared_ptr<Piece>& piece)
  153. {
  154. if (*piece_ == *piece) {
  155. downloadFinished_ = true;
  156. totalLength_ = piece_->getLength();
  157. diskAdaptor_->setTotalLength(totalLength_);
  158. piece_.reset();
  159. createBitfield();
  160. }
  161. }
  162. void UnknownLengthPieceStorage::cancelPiece(const std::shared_ptr<Piece>& piece,
  163. cuid_t cuid)
  164. {
  165. if (*piece_ == *piece) {
  166. piece_.reset();
  167. }
  168. }
  169. bool UnknownLengthPieceStorage::hasPiece(size_t index)
  170. {
  171. if (index == 0 && downloadFinished_) {
  172. return true;
  173. }
  174. else {
  175. return false;
  176. }
  177. }
  178. bool UnknownLengthPieceStorage::isPieceUsed(size_t index)
  179. {
  180. if (index == 0 && piece_) {
  181. return true;
  182. }
  183. else {
  184. return false;
  185. }
  186. }
  187. std::shared_ptr<DiskAdaptor> UnknownLengthPieceStorage::getDiskAdaptor()
  188. {
  189. return diskAdaptor_;
  190. }
  191. int32_t UnknownLengthPieceStorage::getPieceLength(size_t index)
  192. {
  193. // TODO Basically, PieceStorage::getPieceLength() is only used by
  194. // BitTorrent, and it does not use UnknownLengthPieceStorage.
  195. abort();
  196. }
  197. void UnknownLengthPieceStorage::createBitfield()
  198. {
  199. if (totalLength_ > 0) {
  200. bitfield_ = make_unique<BitfieldMan>(downloadContext_->getPieceLength(),
  201. totalLength_);
  202. bitfield_->setAllBit();
  203. }
  204. }
  205. void UnknownLengthPieceStorage::markAllPiecesDone()
  206. {
  207. if (piece_) {
  208. totalLength_ = piece_->getLength();
  209. piece_.reset();
  210. }
  211. createBitfield();
  212. downloadFinished_ = true;
  213. }
  214. void UnknownLengthPieceStorage::markPiecesDone(int64_t length)
  215. {
  216. // TODO not implemented yet
  217. abort();
  218. }
  219. void UnknownLengthPieceStorage::markPieceMissing(size_t index)
  220. {
  221. // TODO not implemented yet
  222. abort();
  223. }
  224. void UnknownLengthPieceStorage::getInFlightPieces(
  225. std::vector<std::shared_ptr<Piece>>& pieces)
  226. {
  227. }
  228. void UnknownLengthPieceStorage::setDiskWriterFactory(
  229. const std::shared_ptr<DiskWriterFactory>& diskWriterFactory)
  230. {
  231. diskWriterFactory_ = diskWriterFactory;
  232. }
  233. const unsigned char* UnknownLengthPieceStorage::getBitfield()
  234. {
  235. if (bitfield_) {
  236. return bitfield_->getBitfield();
  237. }
  238. return nullptr;
  239. }
  240. size_t UnknownLengthPieceStorage::getBitfieldLength()
  241. {
  242. if (bitfield_) {
  243. return bitfield_->getBitfieldLength();
  244. }
  245. return 0;
  246. }
  247. } // namespace aria2