UnknownLengthPieceStorage.cc 6.2 KB

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