UnknownLengthPieceStorage.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "DefaultDiskWriter.h"
  37. #include "DirectDiskAdaptor.h"
  38. #include "prefs.h"
  39. #include "DefaultDiskWriterFactory.h"
  40. #include "DownloadContext.h"
  41. #include "Piece.h"
  42. #include "FileEntry.h"
  43. #include <stdlib.h>
  44. namespace aria2 {
  45. UnknownLengthPieceStorage::UnknownLengthPieceStorage(const DownloadContextHandle& downloadContext,
  46. const Option* option):
  47. _downloadContext(downloadContext),
  48. _option(option),
  49. _diskWriterFactory(new DefaultDiskWriterFactory()),
  50. _totalLength(0),
  51. _downloadFinished(false) {}
  52. UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
  53. void UnknownLengthPieceStorage::initStorage()
  54. {
  55. DiskWriterHandle writer = _diskWriterFactory->newDiskWriter();
  56. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  57. directDiskAdaptor->setDiskWriter(writer);
  58. directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength());
  59. _diskAdaptor = directDiskAdaptor;
  60. std::string storeDir = _downloadContext->getDir();
  61. // if(storeDir == "") {
  62. // storeDir = ".";
  63. // }
  64. _diskAdaptor->setStoreDir(storeDir);
  65. _diskAdaptor->setFileEntries(_downloadContext->getFileEntries());
  66. }
  67. bool UnknownLengthPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer)
  68. {
  69. abort();
  70. }
  71. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  72. {
  73. abort();
  74. }
  75. SharedHandle<Piece> UnknownLengthPieceStorage::getMissingFastPiece(const SharedHandle<Peer>& peer)
  76. {
  77. abort();
  78. }
  79. PieceHandle UnknownLengthPieceStorage::getMissingPiece()
  80. {
  81. if(_downloadFinished) {
  82. return SharedHandle<Piece>();
  83. }
  84. if(_piece.isNull()) {
  85. _piece.reset(new Piece());
  86. return _piece;
  87. } else {
  88. return SharedHandle<Piece>();
  89. }
  90. }
  91. PieceHandle UnknownLengthPieceStorage::getMissingPiece(size_t index)
  92. {
  93. if(index == 0) {
  94. return getMissingPiece();
  95. } else {
  96. return SharedHandle<Piece>();
  97. }
  98. }
  99. PieceHandle UnknownLengthPieceStorage::getPiece(size_t index)
  100. {
  101. if(index == 0) {
  102. if(_piece.isNull()) {
  103. return SharedHandle<Piece>(new Piece());
  104. } else {
  105. return _piece;
  106. }
  107. } else {
  108. return SharedHandle<Piece>();
  109. }
  110. }
  111. void UnknownLengthPieceStorage::completePiece(const PieceHandle& piece)
  112. {
  113. if(_piece == piece) {
  114. _downloadFinished = true;
  115. _totalLength = _piece->getLength();
  116. _diskAdaptor->setTotalLength(_totalLength);
  117. _piece.reset();
  118. }
  119. }
  120. void UnknownLengthPieceStorage::cancelPiece(const PieceHandle& piece)
  121. {
  122. if(_piece == piece) {
  123. _piece.reset();
  124. }
  125. }
  126. bool UnknownLengthPieceStorage::hasPiece(size_t index)
  127. {
  128. if(index == 0 && _downloadFinished) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. bool UnknownLengthPieceStorage::isPieceUsed(size_t index)
  135. {
  136. if(index == 0 && !_piece.isNull()) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. DiskAdaptorHandle UnknownLengthPieceStorage::getDiskAdaptor()
  143. {
  144. return _diskAdaptor;
  145. }
  146. size_t UnknownLengthPieceStorage::getPieceLength(size_t index)
  147. {
  148. if(index == 0) {
  149. return _totalLength;
  150. } else {
  151. return 0;
  152. }
  153. }
  154. void UnknownLengthPieceStorage::markAllPiecesDone()
  155. {
  156. if(!_piece.isNull()) {
  157. _totalLength = _piece->getLength();
  158. _piece.reset();
  159. }
  160. _downloadFinished = true;
  161. }
  162. void UnknownLengthPieceStorage::markPiecesDone(uint64_t length)
  163. {
  164. // TODO not implemented yet
  165. abort();
  166. }
  167. void UnknownLengthPieceStorage::markPieceMissing(size_t index)
  168. {
  169. // TODO not implemented yet
  170. abort();
  171. }
  172. void UnknownLengthPieceStorage::getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces)
  173. {}
  174. void UnknownLengthPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  175. {
  176. _diskWriterFactory = diskWriterFactory;
  177. }
  178. } // namespace aria2