UnknownLengthPieceStorage.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. namespace aria2 {
  44. UnknownLengthPieceStorage::UnknownLengthPieceStorage(const DownloadContextHandle& downloadContext,
  45. const Option* option):
  46. _downloadContext(downloadContext),
  47. _option(option),
  48. _diskAdaptor(0),
  49. _diskWriterFactory(new DefaultDiskWriterFactory()),
  50. _totalLength(0),
  51. _downloadFinished(false),
  52. _piece(0) {}
  53. UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
  54. void UnknownLengthPieceStorage::initStorage()
  55. {
  56. DiskWriterHandle writer = _diskWriterFactory->newDiskWriter();
  57. DirectDiskAdaptorHandle directDiskAdaptor = new DirectDiskAdaptor();
  58. directDiskAdaptor->setDiskWriter(writer);
  59. directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength());
  60. _diskAdaptor = directDiskAdaptor;
  61. std::string storeDir = _downloadContext->getDir();
  62. // if(storeDir == "") {
  63. // storeDir = ".";
  64. // }
  65. _diskAdaptor->setStoreDir(storeDir);
  66. _diskAdaptor->setFileEntries(_downloadContext->getFileEntries());
  67. }
  68. PieceHandle UnknownLengthPieceStorage::getMissingPiece()
  69. {
  70. if(_downloadFinished) {
  71. return 0;
  72. }
  73. if(_piece.isNull()) {
  74. _piece = new Piece();
  75. return _piece;
  76. } else {
  77. return 0;
  78. }
  79. }
  80. PieceHandle UnknownLengthPieceStorage::getMissingPiece(int32_t index)
  81. {
  82. if(index == 0) {
  83. return getMissingPiece();
  84. } else {
  85. return 0;
  86. }
  87. }
  88. PieceHandle UnknownLengthPieceStorage::getPiece(int32_t index)
  89. {
  90. if(index == 0) {
  91. if(_piece.isNull()) {
  92. return new Piece();
  93. } else {
  94. return _piece;
  95. }
  96. } else {
  97. return 0;
  98. }
  99. }
  100. void UnknownLengthPieceStorage::completePiece(const PieceHandle& piece)
  101. {
  102. if(_piece == piece) {
  103. _downloadFinished = true;
  104. _totalLength = _piece->getLength();
  105. _diskAdaptor->setTotalLength(_totalLength);
  106. _piece = 0;
  107. }
  108. }
  109. void UnknownLengthPieceStorage::cancelPiece(const PieceHandle& piece)
  110. {
  111. if(_piece == piece) {
  112. _piece = 0;
  113. }
  114. }
  115. bool UnknownLengthPieceStorage::hasPiece(int32_t index)
  116. {
  117. if(index == 0 && _downloadFinished) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. bool UnknownLengthPieceStorage::isPieceUsed(int32_t index)
  124. {
  125. if(index == 0 && !_piece.isNull()) {
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. DiskAdaptorHandle UnknownLengthPieceStorage::getDiskAdaptor()
  132. {
  133. return _diskAdaptor;
  134. }
  135. int32_t UnknownLengthPieceStorage::getPieceLength(int32_t index)
  136. {
  137. if(index == 0) {
  138. return _totalLength;
  139. } else {
  140. return 0;
  141. }
  142. }
  143. void UnknownLengthPieceStorage::markAllPiecesDone()
  144. {
  145. if(!_piece.isNull()) {
  146. _totalLength = _piece->getLength();
  147. _piece = 0;
  148. }
  149. _downloadFinished = true;
  150. }
  151. Pieces UnknownLengthPieceStorage::getInFlightPieces()
  152. {
  153. return Pieces();
  154. }
  155. void UnknownLengthPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  156. {
  157. _diskWriterFactory = diskWriterFactory;
  158. }
  159. } // namespace aria2