UnknownLengthPieceStorage.cc 6.3 KB

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