RequestGroup.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 "DefaultSegmentManFactory.h"
  38. #include "NullProgressInfoFile.h"
  39. #include "SegmentManFactory.h"
  40. #include "Dependency.h"
  41. #include "prefs.h"
  42. #include "InitiateConnectionCommandFactory.h"
  43. #include "CUIDCounter.h"
  44. #include "File.h"
  45. #include "message.h"
  46. #include "Util.h"
  47. #include "BtRegistry.h"
  48. #include "LogFactory.h"
  49. #include "DiskAdaptor.h"
  50. #include "DiskWriterFactory.h"
  51. #include "RecoverableException.h"
  52. #include "StreamCheckIntegrityEntry.h"
  53. #include "CheckIntegrityCommand.h"
  54. #include "UnknownLengthPieceStorage.h"
  55. #include "SingleFileDownloadContext.h"
  56. #include "DlAbortEx.h"
  57. #include "DownloadFailureException.h"
  58. #include "RequestGroupMan.h"
  59. #include "DefaultBtProgressInfoFile.h"
  60. #include "DefaultPieceStorage.h"
  61. #include "PostDownloadHandler.h"
  62. #ifdef ENABLE_MESSAGE_DIGEST
  63. # include "CheckIntegrityCommand.h"
  64. #endif // ENABLE_MESSAGE_DIGEST
  65. #ifdef ENABLE_BITTORRENT
  66. # include "BtCheckIntegrityEntry.h"
  67. # include "DefaultPeerStorage.h"
  68. # include "DefaultBtAnnounce.h"
  69. # include "BtSetup.h"
  70. # include "BtFileAllocationEntry.h"
  71. # include "BtPostDownloadHandler.h"
  72. #endif // ENABLE_BITTORRENT
  73. #ifdef ENABLE_METALINK
  74. # include "MetalinkPostDownloadHandler.h"
  75. #endif // ENABLE_METALINK
  76. int32_t RequestGroup::_gidCounter = 0;
  77. RequestGroup::RequestGroup(const Option* option,
  78. const Strings& uris):
  79. _gid(++_gidCounter),
  80. _uris(uris),
  81. _numConcurrentCommand(0),
  82. _numStreamConnection(0),
  83. _numCommand(0),
  84. _segmentMan(0),
  85. _segmentManFactory(new DefaultSegmentManFactory(option)),
  86. _downloadContext(0),
  87. _pieceStorage(0),
  88. _progressInfoFile(new NullProgressInfoFile()),
  89. _diskWriterFactory(0),
  90. _dependency(0),
  91. _preLocalFileCheckEnabled(true),
  92. _haltRequested(false),
  93. _option(option),
  94. _logger(LogFactory::getInstance())
  95. {
  96. if(_option->get(PREF_FILE_ALLOCATION) == V_PREALLOC) {
  97. _fileAllocationEnabled = true;
  98. } else {
  99. _fileAllocationEnabled = false;
  100. }
  101. initializePostDownloadHandler();
  102. }
  103. RequestGroup::~RequestGroup() {}
  104. SegmentManHandle RequestGroup::initSegmentMan()
  105. {
  106. _segmentMan = _segmentManFactory->createNewInstance(_downloadContext,
  107. _pieceStorage);
  108. return _segmentMan;
  109. }
  110. bool RequestGroup::downloadFinished() const
  111. {
  112. if(_pieceStorage.isNull()) {
  113. return false;
  114. } else {
  115. return _pieceStorage->downloadFinished();
  116. }
  117. }
  118. bool RequestGroup::allDownloadFinished() const
  119. {
  120. if(_pieceStorage.isNull()) {
  121. return false;
  122. } else {
  123. return _pieceStorage->allDownloadFinished();
  124. }
  125. }
  126. void RequestGroup::closeFile()
  127. {
  128. if(!_pieceStorage.isNull()) {
  129. _pieceStorage->getDiskAdaptor()->closeFile();
  130. }
  131. }
  132. Commands RequestGroup::createInitialCommand(DownloadEngine* e)
  133. {
  134. #ifdef ENABLE_BITTORRENT
  135. {
  136. BtContextHandle btContext = _downloadContext;
  137. if(!btContext.isNull()) {
  138. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  139. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  140. getFilePath().c_str());
  141. }
  142. initPieceStorage();
  143. if(btContext->getFileEntries().size() > 1) {
  144. // this is really multi file torrent.
  145. // clear http/ftp uris because the current implementation does not
  146. // allow integrating multi-file torrent and http/ftp.
  147. _logger->debug("Clearing http/ftp URIs because the current implementation does not allow integrating multi-file torrent and http/ftp.");
  148. _uris.clear();
  149. _pieceStorage->setFileFilter(Util::parseIntRange(_option->get(PREF_SELECT_FILE)));
  150. }
  151. BtProgressInfoFileHandle progressInfoFile =
  152. new DefaultBtProgressInfoFile(_downloadContext,
  153. _pieceStorage,
  154. _option);
  155. BtRegistry::registerBtContext(btContext->getInfoHashAsString(), btContext);
  156. BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
  157. _pieceStorage);
  158. BtRegistry::registerBtProgressInfoFile(btContext->getInfoHashAsString(),
  159. progressInfoFile);
  160. BtRuntimeHandle btRuntime = new BtRuntime();
  161. btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT));
  162. BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime);
  163. PeerStorageHandle peerStorage = new DefaultPeerStorage(btContext, _option);
  164. BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), peerStorage);
  165. BtAnnounceHandle btAnnounce = new DefaultBtAnnounce(btContext, _option);
  166. BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce);
  167. btAnnounce->shuffleAnnounce();
  168. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  169. new PeerObjectCluster());
  170. // Call Load, Save and file allocation command here
  171. if(progressInfoFile->exists()) {
  172. // load .aria2 file if it exists.
  173. progressInfoFile->load();
  174. _pieceStorage->getDiskAdaptor()->openFile();
  175. } else {
  176. if(_pieceStorage->getDiskAdaptor()->fileExists()) {
  177. if(_option->get(PREF_CHECK_INTEGRITY) != V_TRUE &&
  178. _option->get(PREF_ALLOW_OVERWRITE) != V_TRUE) {
  179. // TODO we need this->haltRequested = true?
  180. throw new DownloadFailureException(MSG_FILE_ALREADY_EXISTS,
  181. getFilePath().c_str());
  182. } else {
  183. _pieceStorage->getDiskAdaptor()->openFile();
  184. }
  185. } else {
  186. _pieceStorage->getDiskAdaptor()->openFile();
  187. }
  188. }
  189. _progressInfoFile = progressInfoFile;
  190. CheckIntegrityEntryHandle entry = new BtCheckIntegrityEntry(this);
  191. return processCheckIntegrityEntry(entry, e);
  192. }
  193. }
  194. #endif // ENABLE_BITTORRENT
  195. // TODO I assume here when totallength is set to DownloadContext and it is
  196. // not 0, then filepath is also set DownloadContext correctly....
  197. if(_downloadContext->getTotalLength() == 0) {
  198. return createNextCommand(e, 1);
  199. }else {
  200. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  201. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  202. getFilePath().c_str());
  203. }
  204. initPieceStorage();
  205. BtProgressInfoFileHandle infoFile =
  206. new DefaultBtProgressInfoFile(_downloadContext, _pieceStorage, _option);
  207. if(!infoFile->exists() && downloadFinishedByFileLength()) {
  208. return Commands();
  209. }
  210. loadAndOpenFile(infoFile);
  211. return processCheckIntegrityEntry(new StreamCheckIntegrityEntry(0, this), e);
  212. }
  213. }
  214. Commands RequestGroup::processCheckIntegrityEntry(const CheckIntegrityEntryHandle& entry, DownloadEngine* e)
  215. {
  216. #ifdef ENABLE_MESSAGE_DIGEST
  217. if(File(getFilePath()).size() > 0 &&
  218. e->option->get(PREF_CHECK_INTEGRITY) == V_TRUE &&
  219. entry->isValidationReady()) {
  220. entry->initValidator();
  221. CheckIntegrityCommand* command =
  222. new CheckIntegrityCommand(CUIDCounterSingletonHolder::instance()->newID(), this, e, entry);
  223. Commands commands;
  224. commands.push_back(command);
  225. return commands;
  226. } else
  227. #endif // ENABLE_MESSAGE_DIGEST
  228. {
  229. return entry->onDownloadIncomplete(e);
  230. }
  231. }
  232. void RequestGroup::initPieceStorage()
  233. {
  234. if(_downloadContext->getTotalLength() == 0) {
  235. UnknownLengthPieceStorageHandle ps = new UnknownLengthPieceStorage(_downloadContext, _option);
  236. if(!_diskWriterFactory.isNull()) {
  237. ps->setDiskWriterFactory(_diskWriterFactory);
  238. }
  239. _pieceStorage = ps;
  240. } else {
  241. DefaultPieceStorageHandle ps = new DefaultPieceStorage(_downloadContext, _option);
  242. if(!_diskWriterFactory.isNull()) {
  243. ps->setDiskWriterFactory(_diskWriterFactory);
  244. }
  245. _pieceStorage = ps;
  246. }
  247. _pieceStorage->initStorage();
  248. initSegmentMan();
  249. }
  250. bool RequestGroup::downloadFinishedByFileLength()
  251. {
  252. // assuming that a control file doesn't exist.
  253. if(!isPreLocalFileCheckEnabled() ||
  254. _option->get(PREF_ALLOW_OVERWRITE) == V_TRUE ||
  255. _option->get(PREF_CHECK_INTEGRITY) == V_TRUE &&
  256. !_downloadContext->getPieceHashes().empty()) {
  257. return false;
  258. }
  259. // TODO consider the case when the getFilePath() returns dir path.
  260. File outfile(getFilePath());
  261. if(outfile.exists() && getTotalLength() == outfile.size()) {
  262. _pieceStorage->markAllPiecesDone();
  263. _logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, _gid, getFilePath().c_str());
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. }
  269. void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoFile)
  270. {
  271. try {
  272. if(!isPreLocalFileCheckEnabled()) {
  273. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  274. return;
  275. }
  276. if(progressInfoFile->exists()) {
  277. progressInfoFile->load();
  278. _pieceStorage->getDiskAdaptor()->openExistingFile();
  279. } else {
  280. File outfile(getFilePath());
  281. if(outfile.exists() && _option->get(PREF_CONTINUE) == V_TRUE) {
  282. if(getTotalLength() < outfile.size()) {
  283. throw new DlAbortEx(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE,
  284. getFilePath().c_str(),
  285. Util::llitos(outfile.size()).c_str(),
  286. Util::llitos(getTotalLength()).c_str());
  287. }
  288. _pieceStorage->getDiskAdaptor()->openExistingFile();
  289. _pieceStorage->markPiecesDone(outfile.size());
  290. } else {
  291. #ifdef ENABLE_MESSAGE_DIGEST
  292. if(outfile.exists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
  293. _pieceStorage->getDiskAdaptor()->openExistingFile();
  294. } else {
  295. shouldCancelDownloadForSafety();
  296. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  297. }
  298. #else // ENABLE_MESSAGE_DIGEST
  299. shouldCancelDownloadForSafety();
  300. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  301. #endif // ENABLE_MESSAGE_DIGEST
  302. }
  303. }
  304. setProgressInfoFile(progressInfoFile);
  305. } catch(RecoverableException* e) {
  306. throw new DownloadFailureException(e, EX_DOWNLOAD_ABORTED);
  307. }
  308. }
  309. // assuming that a control file does not exist
  310. void RequestGroup::shouldCancelDownloadForSafety()
  311. {
  312. if(_option->get(PREF_ALLOW_OVERWRITE) == V_TRUE) {
  313. return;
  314. }
  315. File outfile(getFilePath());
  316. if(outfile.exists()) {
  317. if(_option->get(PREF_AUTO_FILE_RENAMING) == V_TRUE) {
  318. if(tryAutoFileRenaming()) {
  319. _logger->notice(MSG_FILE_RENAMED, getFilePath().c_str());
  320. } else {
  321. throw new DownloadFailureException("File renaming failed: %s",
  322. getFilePath().c_str());
  323. }
  324. } else {
  325. throw new DownloadFailureException(MSG_FILE_ALREADY_EXISTS,
  326. getFilePath().c_str());
  327. }
  328. }
  329. }
  330. bool RequestGroup::tryAutoFileRenaming()
  331. {
  332. string filepath = getFilePath();
  333. if(filepath.empty()) {
  334. return false;
  335. }
  336. for(int32_t i = 1; i < 10000; ++i) {
  337. File newfile(filepath+"."+Util::itos(i));
  338. if(!newfile.exists()) {
  339. SingleFileDownloadContextHandle(_downloadContext)->setUFilename(newfile.getBasename());
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. Commands RequestGroup::createNextCommandWithAdj(DownloadEngine* e, int32_t numAdj)
  346. {
  347. int32_t numCommand = _numConcurrentCommand == 0 ? _uris.size() : _numConcurrentCommand+numAdj;
  348. return createNextCommand(e, numCommand, "GET");
  349. }
  350. Commands RequestGroup::createNextCommand(DownloadEngine* e, int32_t numCommand, const string& method)
  351. {
  352. Commands commands;
  353. for(;!_uris.empty() && numCommand--; _uris.pop_front()) {
  354. string uri = _uris.front();
  355. _spentUris.push_back(uri);
  356. RequestHandle req = new Request();
  357. req->setReferer(_option->get(PREF_REFERER));
  358. req->setMethod(method);
  359. if(req->setUrl(uri)) {
  360. commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), req, this, e));
  361. } else {
  362. _logger->error(MSG_UNRECOGNIZED_URI, req->getUrl().c_str());
  363. }
  364. }
  365. return commands;
  366. }
  367. string RequestGroup::getFilePath() const
  368. {
  369. assert(!_downloadContext.isNull());
  370. if(_downloadContext.isNull()) {
  371. return "";
  372. } else {
  373. return _downloadContext->getActualBasePath();
  374. }
  375. }
  376. int64_t RequestGroup::getTotalLength() const
  377. {
  378. if(_pieceStorage.isNull()) {
  379. return 0;
  380. } else {
  381. if(_pieceStorage->isSelectiveDownloadingMode()) {
  382. return _pieceStorage->getFilteredTotalLength();
  383. } else {
  384. return _pieceStorage->getTotalLength();
  385. }
  386. }
  387. }
  388. int64_t RequestGroup::getCompletedLength() const
  389. {
  390. if(_pieceStorage.isNull()) {
  391. return 0;
  392. } else {
  393. if(_pieceStorage->isSelectiveDownloadingMode()) {
  394. return _pieceStorage->getFilteredCompletedLength();
  395. } else {
  396. return _pieceStorage->getCompletedLength();
  397. }
  398. }
  399. }
  400. void RequestGroup::validateFilename(const string& expectedFilename,
  401. const string& actualFilename) const
  402. {
  403. if(expectedFilename.empty()) {
  404. return;
  405. }
  406. if(expectedFilename != actualFilename) {
  407. throw new DlAbortEx(EX_FILENAME_MISMATCH,
  408. expectedFilename.c_str(),
  409. actualFilename.c_str());
  410. }
  411. }
  412. void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
  413. int64_t actualTotalLength) const
  414. {
  415. if(expectedTotalLength <= 0) {
  416. return;
  417. }
  418. if(expectedTotalLength != actualTotalLength) {
  419. throw new DlAbortEx(EX_SIZE_MISMATCH,
  420. Util::llitos(expectedTotalLength, true).c_str(),
  421. Util::llitos(actualTotalLength, true).c_str());
  422. }
  423. }
  424. void RequestGroup::validateFilename(const string& actualFilename) const
  425. {
  426. validateFilename(_downloadContext->getFileEntries().front()->getBasename(), actualFilename);
  427. }
  428. void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
  429. {
  430. validateTotalLength(getTotalLength(), actualTotalLength);
  431. }
  432. void RequestGroup::increaseStreamConnection()
  433. {
  434. ++_numStreamConnection;
  435. }
  436. void RequestGroup::decreaseStreamConnection()
  437. {
  438. --_numStreamConnection;
  439. }
  440. int32_t RequestGroup::getNumConnection() const
  441. {
  442. int32_t numConnection = _numStreamConnection;
  443. #ifdef ENABLE_BITTORRENT
  444. {
  445. BtContextHandle btContext = _downloadContext;
  446. if(!btContext.isNull()) {
  447. BtRuntimeHandle btRuntime = BT_RUNTIME(btContext);
  448. if(!btRuntime.isNull()) {
  449. numConnection += btRuntime->getConnections();
  450. }
  451. }
  452. }
  453. #endif // ENABLE_BITTORRENT
  454. return numConnection;
  455. }
  456. void RequestGroup::increaseNumCommand()
  457. {
  458. ++_numCommand;
  459. }
  460. void RequestGroup::decreaseNumCommand()
  461. {
  462. --_numCommand;
  463. }
  464. TransferStat RequestGroup::calculateStat()
  465. {
  466. TransferStat stat;
  467. #ifdef ENABLE_BITTORRENT
  468. {
  469. BtContextHandle btContext = _downloadContext;
  470. if(!btContext.isNull()) {
  471. PeerStorageHandle peerStorage = PEER_STORAGE(btContext);
  472. if(!peerStorage.isNull()) {
  473. stat = peerStorage->calculateStat();
  474. }
  475. }
  476. }
  477. #endif // ENABLE_BITTORRENT
  478. if(!_segmentMan.isNull()) {
  479. stat.setDownloadSpeed(stat.getDownloadSpeed()+_segmentMan->calculateDownloadSpeed());
  480. }
  481. return stat;
  482. }
  483. void RequestGroup::setHaltRequested(bool f)
  484. {
  485. _haltRequested = f;
  486. #ifdef ENABLE_BITTORRENT
  487. {
  488. BtContextHandle btContext = _downloadContext;
  489. if(!btContext.isNull()) {
  490. BtRuntimeHandle btRuntime = BT_RUNTIME(btContext);
  491. if(!btRuntime.isNull()) {
  492. btRuntime->setHalt(f);
  493. }
  494. }
  495. }
  496. #endif // ENABLE_BITTORRENT
  497. }
  498. void RequestGroup::releaseRuntimeResource()
  499. {
  500. #ifdef ENABLE_BITTORRENT
  501. BtContextHandle btContext = _downloadContext;
  502. if(!btContext.isNull()) {
  503. BtContextHandle btContextInReg = BtRegistry::getBtContext(btContext->getInfoHashAsString());
  504. if(!btContextInReg.isNull() &&
  505. btContextInReg->getOwnerRequestGroup()->getGID() ==
  506. btContext->getOwnerRequestGroup()->getGID()) {
  507. BtRegistry::unregister(btContext->getInfoHashAsString());
  508. }
  509. }
  510. #endif // ENABLE_BITTORRENT
  511. if(!_pieceStorage.isNull()) {
  512. _pieceStorage->removeAdvertisedPiece(0);
  513. }
  514. }
  515. RequestGroups RequestGroup::postDownloadProcessing()
  516. {
  517. _logger->debug("Finding PostDownloadHandler for path %s.", getFilePath().c_str());
  518. try {
  519. for(PostDownloadHandlers::const_iterator itr = _postDownloadHandlers.begin();
  520. itr != _postDownloadHandlers.end(); ++itr) {
  521. if((*itr)->canHandle(getFilePath())) {
  522. return (*itr)->getNextRequestGroups(getFilePath());
  523. }
  524. }
  525. } catch(RecoverableException* ex) {
  526. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  527. delete ex;
  528. return RequestGroups();
  529. }
  530. _logger->debug("No PostDownloadHandler found.");
  531. return RequestGroups();
  532. }
  533. void RequestGroup::initializePostDownloadHandler()
  534. {
  535. #ifdef ENABLE_BITTORRENT
  536. if(_option->get(PREF_FOLLOW_TORRENT) == V_TRUE) {
  537. _postDownloadHandlers.push_back(new BtPostDownloadHandler(_option));
  538. }
  539. #endif // ENABLE_BITTORRENT
  540. #ifdef ENABLE_METALINK
  541. if(_option->get(PREF_FOLLOW_METALINK) == V_TRUE) {
  542. _postDownloadHandlers.push_back(new MetalinkPostDownloadHandler(_option));
  543. }
  544. #endif // ENABLE_METALINK
  545. }
  546. Strings RequestGroup::getUris() const
  547. {
  548. Strings temp(_spentUris.begin(), _spentUris.end());
  549. temp.insert(temp.end(), _uris.begin(), _uris.end());
  550. return temp;
  551. }
  552. bool RequestGroup::isDependencyResolved()
  553. {
  554. if(_dependency.isNull()) {
  555. return true;
  556. }
  557. return _dependency->resolve();
  558. }
  559. void RequestGroup::setSegmentManFactory(const SegmentManFactoryHandle& segmentManFactory)
  560. {
  561. _segmentManFactory = segmentManFactory;
  562. }
  563. void RequestGroup::dependsOn(const DependencyHandle& dep)
  564. {
  565. _dependency = dep;
  566. }
  567. void RequestGroup::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  568. {
  569. _diskWriterFactory = diskWriterFactory;
  570. }
  571. DiskWriterFactoryHandle RequestGroup::getDiskWriterFactory() const
  572. {
  573. return _diskWriterFactory;
  574. }
  575. void RequestGroup::addPostDownloadHandler(const PostDownloadHandlerHandle& handler)
  576. {
  577. _postDownloadHandlers.push_back(handler);
  578. }
  579. void RequestGroup::clearPostDowloadHandler()
  580. {
  581. _postDownloadHandlers.clear();
  582. }
  583. SegmentManHandle RequestGroup::getSegmentMan() const
  584. {
  585. return _segmentMan;
  586. }
  587. DownloadContextHandle RequestGroup::getDownloadContext() const
  588. {
  589. return _downloadContext;
  590. }
  591. void RequestGroup::setDownloadContext(const DownloadContextHandle& downloadContext)
  592. {
  593. _downloadContext = downloadContext;
  594. }
  595. PieceStorageHandle RequestGroup::getPieceStorage() const
  596. {
  597. return _pieceStorage;
  598. }
  599. void RequestGroup::setPieceStorage(const PieceStorageHandle& pieceStorage)
  600. {
  601. _pieceStorage = pieceStorage;
  602. }
  603. BtProgressInfoFileHandle RequestGroup::getProgressInfoFile() const
  604. {
  605. return _progressInfoFile;
  606. }
  607. void RequestGroup::setProgressInfoFile(const BtProgressInfoFileHandle& progressInfoFile)
  608. {
  609. _progressInfoFile = progressInfoFile;
  610. }
  611. bool RequestGroup::needsFileAllocation() const
  612. {
  613. return isFileAllocationEnabled() &&
  614. _option->getAsLLInt(PREF_NO_FILE_ALLOCATION_LIMIT) <= getTotalLength() &&
  615. !_pieceStorage->getDiskAdaptor()->fileAllocationIterator()->finished();
  616. }