RequestGroup.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "RequestGroup.h"
  36. #include "DownloadEngine.h"
  37. #include "prefs.h"
  38. #include "DefaultDiskWriter.h"
  39. #include "RequestFactory.h"
  40. #include "InitiateConnectionCommandFactory.h"
  41. #include "CUIDCounter.h"
  42. #include "DlAbortEx.h"
  43. #include "File.h"
  44. #include "message.h"
  45. #include "DlAbortEx.h"
  46. #include "Util.h"
  47. #include "CheckIntegrityCommand.h"
  48. #include "FatalException.h"
  49. SegmentManHandle RequestGroup::initSegmentMan()
  50. {
  51. _segmentMan = _segmentManFactory->createNewInstance();
  52. return _segmentMan;
  53. }
  54. Commands RequestGroup::createNextCommand(DownloadEngine* e, const string& method)
  55. {
  56. int32_t numCommand = _numConcurrentCommand == 0 ? _uris.size() : _numConcurrentCommand;
  57. return createNextCommand(e, numCommand, method);
  58. }
  59. Commands RequestGroup::createNextCommand(DownloadEngine* e, int32_t numCommand, const string& method)
  60. {
  61. Commands commands;
  62. for(;!_uris.empty() && commands.size() < (size_t)numCommand; _uris.pop_front()) {
  63. string uri = _uris.front();
  64. _spentUris.push_back(uri);
  65. RequestHandle req = RequestFactorySingletonHolder::instance()->createRequest();
  66. req->setReferer(_option->get(PREF_REFERER));
  67. req->setMethod(method);
  68. if(req->setUrl(uri)) {
  69. commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), req, this, e));
  70. } else {
  71. logger->info(_("Unrecognized URL or unsupported protocol: %s\n"),
  72. req->getUrl().c_str());
  73. }
  74. }
  75. return commands;
  76. }
  77. void RequestGroup::initBitfield()
  78. {
  79. _segmentMan->initBitfield(_option->getAsInt(PREF_SEGMENT_SIZE),
  80. _segmentMan->totalSize);
  81. }
  82. void RequestGroup::openExistingFile()
  83. {
  84. _segmentMan->diskWriter->openExistingFile(_segmentMan->getFilePath());
  85. }
  86. void RequestGroup::markAllPiecesDone()
  87. {
  88. _segmentMan->markAllPiecesDone();
  89. }
  90. void RequestGroup::markExistingPiecesDone()
  91. {
  92. _segmentMan->markPieceDone(File(_segmentMan->getFilePath()).size());
  93. }
  94. void RequestGroup::markPieceDone(int64_t length)
  95. {
  96. _segmentMan->markPieceDone(length);
  97. }
  98. void RequestGroup::shouldCancelDownloadForSafety()
  99. {
  100. if(_segmentMan->shouldCancelDownloadForSafety()) {
  101. logger->notice(MSG_FILE_ALREADY_EXISTS,
  102. _segmentMan->getFilePath().c_str(),
  103. _segmentMan->getSegmentFilePath().c_str());
  104. throw new FatalException(EX_DOWNLOAD_ABORTED);
  105. }
  106. }
  107. void RequestGroup::initAndOpenFile()
  108. {
  109. _segmentMan->diskWriter->initAndOpenFile(_segmentMan->getFilePath());
  110. }
  111. bool RequestGroup::needsFileAllocation() const
  112. {
  113. return _option->get(PREF_FILE_ALLOCATION) == V_PREALLOC
  114. && File(_segmentMan->getFilePath()).size() < _segmentMan->totalSize;
  115. }
  116. bool RequestGroup::fileExists() const
  117. {
  118. return _segmentMan->fileExists();
  119. }
  120. bool RequestGroup::segmentFileExists() const
  121. {
  122. return _segmentMan->segmentFileExists();
  123. }
  124. string RequestGroup::getFilePath() const
  125. {
  126. if(_segmentMan.isNull()) {
  127. return "";
  128. } else {
  129. return _segmentMan->getFilePath();
  130. }
  131. }
  132. void RequestGroup::loadAndOpenFile()
  133. {
  134. bool segFileExists = segmentFileExists();
  135. if(segFileExists) {
  136. load();
  137. openExistingFile();
  138. } else if(fileExists() && _option->get(PREF_CONTINUE) == V_TRUE) {
  139. File existingFile(getFilePath());
  140. if(getTotalLength() < existingFile.size()) {
  141. throw new DlAbortEx("Invalid file length. Cannot continue download %s: local %s, remote %s",
  142. getFilePath().c_str(),
  143. Util::llitos(existingFile.size()).c_str(),
  144. Util::llitos(getTotalLength()).c_str());
  145. }
  146. initBitfield();
  147. openExistingFile();
  148. } else {
  149. shouldCancelDownloadForSafety();
  150. initBitfield();
  151. if(fileExists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
  152. openExistingFile();
  153. } else {
  154. initAndOpenFile();
  155. }
  156. }
  157. }
  158. bool RequestGroup::downloadFinishedByFileLength()
  159. {
  160. if(_segmentMan->segmentFileExists()) {
  161. return false;
  162. }
  163. File existingFile(getFilePath());
  164. if(existingFile.exists() &&
  165. getTotalLength() == existingFile.size()) {
  166. _segmentMan->downloadStarted = true;
  167. initBitfield();
  168. _segmentMan->markAllPiecesDone();
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. }
  174. void RequestGroup::prepareForNextAction(int cuid, const RequestHandle& req, DownloadEngine* e, DownloadCommand* downloadCommand)
  175. {
  176. File existingFile(getFilePath());
  177. if(existingFile.size() > 0 && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
  178. CheckIntegrityCommand* command = new CheckIntegrityCommand(cuid, req, this, e);
  179. command->setNextDownloadCommand(downloadCommand);
  180. command->initValidator();
  181. e->commands.push_back(command);
  182. } else if(needsFileAllocation()) {
  183. FileAllocationEntryHandle entry = new FileAllocationEntry(cuid, req, this);
  184. entry->setNextDownloadCommand(downloadCommand);
  185. e->_fileAllocationMan->pushFileAllocationEntry(entry);
  186. } else {
  187. if(downloadCommand) {
  188. e->commands.push_back(downloadCommand);
  189. } else {
  190. Commands commands = createNextCommand(e);
  191. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, this, e);
  192. commands.push_front(command);
  193. e->addCommand(commands);
  194. }
  195. }
  196. }
  197. void RequestGroup::validateFilename(const string& expectedFilename,
  198. const string& actualFilename) const
  199. {
  200. if(expectedFilename.empty()) {
  201. return;
  202. }
  203. if(expectedFilename != actualFilename) {
  204. throw new DlAbortEx(EX_FILENAME_MISMATCH,
  205. expectedFilename.c_str(),
  206. actualFilename.c_str());
  207. }
  208. }
  209. void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
  210. int64_t actualTotalLength) const
  211. {
  212. if(expectedTotalLength <= 0) {
  213. return;
  214. }
  215. if(expectedTotalLength != actualTotalLength) {
  216. throw new DlAbortEx(EX_SIZE_MISMATCH,
  217. expectedTotalLength,
  218. actualTotalLength);
  219. }
  220. }
  221. void RequestGroup::validateFilename(const string& actualFilename) const
  222. {
  223. validateFilename(_segmentMan->filename, actualFilename);
  224. }
  225. void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
  226. {
  227. validateTotalLength(_segmentMan->totalSize, actualTotalLength);
  228. }
  229. void RequestGroup::validateFilenameByHint(const string& actualFilename) const
  230. {
  231. validateFilename(_hintFilename, actualFilename);
  232. }
  233. void RequestGroup::validateTotalLengthByHint(int64_t actualTotalLength) const
  234. {
  235. validateTotalLength(_hintTotalLength, actualTotalLength);
  236. }
  237. void RequestGroup::setUserDefinedFilename(const string& filename)
  238. {
  239. if(_segmentMan.isNull()) {
  240. throw new FatalException("SegmentMan is not initialized yet. Call initSegmentMan() before calling this function.");
  241. }
  242. _segmentMan->ufilename = filename;
  243. }