UnknownLengthPieceStorage.cc 5.1 KB

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