RequestGroup.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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 <cassert>
  37. #include <algorithm>
  38. #include "PostDownloadHandler.h"
  39. #include "DownloadEngine.h"
  40. #include "DefaultSegmentManFactory.h"
  41. #include "SegmentMan.h"
  42. #include "NullProgressInfoFile.h"
  43. #include "Dependency.h"
  44. #include "prefs.h"
  45. #include "InitiateConnectionCommandFactory.h"
  46. #include "File.h"
  47. #include "message.h"
  48. #include "Util.h"
  49. #include "BtRegistry.h"
  50. #include "LogFactory.h"
  51. #include "Logger.h"
  52. #include "DiskAdaptor.h"
  53. #include "DiskWriterFactory.h"
  54. #include "RecoverableException.h"
  55. #include "StreamCheckIntegrityEntry.h"
  56. #include "CheckIntegrityCommand.h"
  57. #include "UnknownLengthPieceStorage.h"
  58. #include "BtContext.h"
  59. #include "SingleFileDownloadContext.h"
  60. #include "DlAbortEx.h"
  61. #include "DownloadFailureException.h"
  62. #include "RequestGroupMan.h"
  63. #include "DefaultBtProgressInfoFile.h"
  64. #include "DefaultPieceStorage.h"
  65. #include "DownloadHandlerFactory.h"
  66. #include "MemoryBufferPreDownloadHandler.h"
  67. #include "DownloadHandlerConstants.h"
  68. #include "ServerHost.h"
  69. #include "Option.h"
  70. #include "FileEntry.h"
  71. #include "Request.h"
  72. #include "FileAllocationIterator.h"
  73. #include "StringFormat.h"
  74. #include "A2STR.h"
  75. #include "URISelector.h"
  76. #include "InOrderURISelector.h"
  77. #include "PieceSelector.h"
  78. #ifdef ENABLE_MESSAGE_DIGEST
  79. # include "CheckIntegrityCommand.h"
  80. #endif // ENABLE_MESSAGE_DIGEST
  81. #ifdef ENABLE_BITTORRENT
  82. # include "BtCheckIntegrityEntry.h"
  83. # include "DefaultPeerStorage.h"
  84. # include "DefaultBtAnnounce.h"
  85. # include "BtRuntime.h"
  86. # include "BtSetup.h"
  87. # include "BtFileAllocationEntry.h"
  88. # include "BtPostDownloadHandler.h"
  89. # include "DHTSetup.h"
  90. # include "DHTRegistry.h"
  91. # include "BtMessageFactory.h"
  92. # include "BtRequestFactory.h"
  93. # include "BtMessageDispatcher.h"
  94. # include "BtMessageReceiver.h"
  95. # include "PeerConnection.h"
  96. # include "ExtensionMessageFactory.h"
  97. # include "DHTPeerAnnounceStorage.h"
  98. # include "DHTEntryPointNameResolveCommand.h"
  99. # include "LongestSequencePieceSelector.h"
  100. #endif // ENABLE_BITTORRENT
  101. #ifdef ENABLE_METALINK
  102. # include "MetalinkPostDownloadHandler.h"
  103. #endif // ENABLE_METALINK
  104. namespace aria2 {
  105. int32_t RequestGroup::_gidCounter = 0;
  106. const std::string RequestGroup::ACCEPT_METALINK = "application/metalink+xml";
  107. RequestGroup::RequestGroup(const Option* option,
  108. const std::deque<std::string>& uris):
  109. _gid(++_gidCounter),
  110. _uris(uris),
  111. _numConcurrentCommand(option->getAsInt(PREF_SPLIT)),
  112. _numStreamConnection(0),
  113. _numCommand(0),
  114. _segmentManFactory(new DefaultSegmentManFactory(option)),
  115. _progressInfoFile(new NullProgressInfoFile()),
  116. _preLocalFileCheckEnabled(true),
  117. _haltRequested(false),
  118. _forceHaltRequested(false),
  119. _singleHostMultiConnectionEnabled(true),
  120. _uriSelector(new InOrderURISelector()),
  121. _lastModifiedTime(Time::null()),
  122. _fileNotFoundCount(0),
  123. _timeout(option->getAsInt(PREF_TIMEOUT)),
  124. _maxTries(option->getAsInt(PREF_MAX_TRIES)),
  125. _inMemoryDownload(false),
  126. _maxDownloadSpeedLimit(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT)),
  127. _maxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT)),
  128. _option(option),
  129. _logger(LogFactory::getInstance())
  130. {
  131. _fileAllocationEnabled = _option->get(PREF_FILE_ALLOCATION) != V_NONE;
  132. // Add types to be sent as a Accept header value here.
  133. // It would be good to put this value in Option so that user can tweak
  134. // and add this list.
  135. // ACCEPT_METALINK is used for `transparent metalink'.
  136. addAcceptType(ACCEPT_METALINK);
  137. if(!_option->getAsBool(PREF_DRY_RUN)) {
  138. initializePreDownloadHandler();
  139. initializePostDownloadHandler();
  140. }
  141. }
  142. RequestGroup::~RequestGroup() {}
  143. SegmentManHandle RequestGroup::initSegmentMan()
  144. {
  145. _segmentMan = _segmentManFactory->createNewInstance(_downloadContext,
  146. _pieceStorage);
  147. return _segmentMan;
  148. }
  149. bool RequestGroup::downloadFinished() const
  150. {
  151. if(_pieceStorage.isNull()) {
  152. return false;
  153. } else {
  154. return _pieceStorage->downloadFinished();
  155. }
  156. }
  157. bool RequestGroup::allDownloadFinished() const
  158. {
  159. if(_pieceStorage.isNull()) {
  160. return false;
  161. } else {
  162. return _pieceStorage->allDownloadFinished();
  163. }
  164. }
  165. DownloadResult::RESULT RequestGroup::downloadResult() const
  166. {
  167. if (downloadFinished())
  168. return DownloadResult::FINISHED;
  169. else {
  170. if (_uriResults.empty()) {
  171. return DownloadResult::UNKNOWN_ERROR;
  172. } else {
  173. return _uriResults.back().getResult();
  174. }
  175. }
  176. }
  177. void RequestGroup::closeFile()
  178. {
  179. if(!_pieceStorage.isNull()) {
  180. _pieceStorage->getDiskAdaptor()->closeFile();
  181. }
  182. }
  183. void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
  184. DownloadEngine* e,
  185. const std::string& method)
  186. {
  187. #ifdef ENABLE_BITTORRENT
  188. {
  189. BtContextHandle btContext = dynamic_pointer_cast<BtContext>(_downloadContext);
  190. if(!btContext.isNull()) {
  191. if(_option->getAsBool(PREF_DRY_RUN)) {
  192. throw DownloadFailureException
  193. ("Cancel BitTorrent download in dry-run context.");
  194. }
  195. SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
  196. if(!btRegistry->getBtContext(btContext->getInfoHashAsString()).isNull()) {
  197. throw DownloadFailureException
  198. (StringFormat("InfoHash %s is already registered.",
  199. btContext->getInfoHashAsString().c_str()).str());
  200. }
  201. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  202. throw DownloadFailureException
  203. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  204. getFilePath().c_str()).str());
  205. }
  206. initPieceStorage();
  207. if(btContext->getFileEntries().size() > 1) {
  208. // this is really multi file torrent.
  209. // clear http/ftp uris because the current implementation does not
  210. // allow integrating multi-file torrent and http/ftp.
  211. _logger->debug("Clearing http/ftp URIs because the current implementation does not allow integrating multi-file torrent and http/ftp.");
  212. _uris.clear();
  213. _pieceStorage->setFileFilter(btContext->getFileFilter());
  214. } else if(btContext->getFileEntries().size() == 1) {
  215. // web-seeding is only enabled for single file torrent
  216. SharedHandle<FileEntry> fileEntry = btContext->getFileEntries().front();
  217. _uris.insert(_uris.end(),
  218. fileEntry->getAssociatedUris().begin(),
  219. fileEntry->getAssociatedUris().end());
  220. }
  221. SharedHandle<DefaultBtProgressInfoFile>
  222. progressInfoFile(new DefaultBtProgressInfoFile(_downloadContext,
  223. _pieceStorage,
  224. _option));
  225. btRegistry->registerBtContext(btContext->getInfoHashAsString(),
  226. btContext);
  227. btRegistry->registerPieceStorage(btContext->getInfoHashAsString(),
  228. _pieceStorage);
  229. btRegistry->registerBtProgressInfoFile(btContext->getInfoHashAsString(),
  230. progressInfoFile);
  231. BtRuntimeHandle btRuntime(new BtRuntime());
  232. btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT));
  233. btRuntime->setMaxPeers(_option->getAsInt(PREF_BT_MAX_PEERS));
  234. btRegistry->registerBtRuntime(btContext->getInfoHashAsString(),
  235. btRuntime);
  236. _btRuntime = btRuntime;
  237. progressInfoFile->setBtRuntime(btRuntime);
  238. SharedHandle<DefaultPeerStorage> peerStorage
  239. (new DefaultPeerStorage(btContext, _option));
  240. peerStorage->setBtRuntime(btRuntime);
  241. peerStorage->setPieceStorage(_pieceStorage);
  242. btRegistry->registerPeerStorage(btContext->getInfoHashAsString(),
  243. peerStorage);
  244. _peerStorage = peerStorage;
  245. progressInfoFile->setPeerStorage(peerStorage);
  246. SharedHandle<DefaultBtAnnounce> btAnnounce
  247. (new DefaultBtAnnounce(btContext, _option));
  248. btAnnounce->setBtRuntime(btRuntime);
  249. btAnnounce->setPieceStorage(_pieceStorage);
  250. btAnnounce->setPeerStorage(peerStorage);
  251. btAnnounce->setUserDefinedInterval
  252. (_option->getAsInt(PREF_BT_TRACKER_INTERVAL));
  253. btRegistry->registerBtAnnounce(btContext->getInfoHashAsString(),
  254. btAnnounce);
  255. btAnnounce->shuffleAnnounce();
  256. // Remove the control file if download file doesn't exist
  257. if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) {
  258. progressInfoFile->removeFile();
  259. _logger->notice(MSG_REMOVED_DEFUNCT_CONTROL_FILE,
  260. progressInfoFile->getFilename().c_str(),
  261. getFilePath().c_str());
  262. }
  263. {
  264. uint64_t actualFileSize = _pieceStorage->getDiskAdaptor()->size();
  265. if(actualFileSize == btContext->getTotalLength()) {
  266. // First, make DiskAdaptor read-only mode to allow the
  267. // program to seed file in read-only media.
  268. _pieceStorage->getDiskAdaptor()->enableReadOnly();
  269. } else {
  270. // Open file in writable mode to allow the program
  271. // truncate the file to btContext->getTotalLength()
  272. _logger->debug("File size not match. File is opened in writable mode."
  273. " Expected:%s Actual:%s",
  274. Util::uitos(btContext->getTotalLength()).c_str(),
  275. Util::uitos(actualFileSize).c_str());
  276. }
  277. }
  278. // Call Load, Save and file allocation command here
  279. if(progressInfoFile->exists()) {
  280. // load .aria2 file if it exists.
  281. progressInfoFile->load();
  282. _pieceStorage->getDiskAdaptor()->openFile();
  283. } else {
  284. if(_pieceStorage->getDiskAdaptor()->fileExists()) {
  285. if(!_option->getAsBool(PREF_CHECK_INTEGRITY) &&
  286. !_option->getAsBool(PREF_ALLOW_OVERWRITE) &&
  287. !_option->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
  288. // TODO we need this->haltRequested = true?
  289. throw DownloadFailureException
  290. (StringFormat
  291. (MSG_FILE_ALREADY_EXISTS,
  292. getFilePath().c_str()).str());
  293. } else {
  294. _pieceStorage->getDiskAdaptor()->openFile();
  295. }
  296. if(_option->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
  297. _pieceStorage->markAllPiecesDone();
  298. }
  299. } else {
  300. _pieceStorage->getDiskAdaptor()->openFile();
  301. }
  302. }
  303. _progressInfoFile = progressInfoFile;
  304. if(!btContext->isPrivate() && _option->getAsBool(PREF_ENABLE_DHT)) {
  305. std::deque<Command*> commands;
  306. DHTSetup().setup(commands, e, _option);
  307. e->addCommand(commands);
  308. if(!btContext->getNodes().empty() && DHTSetup::initialized()) {
  309. DHTEntryPointNameResolveCommand* command =
  310. new DHTEntryPointNameResolveCommand(e->newCUID(), e,
  311. btContext->getNodes());
  312. command->setTaskQueue(DHTRegistry::_taskQueue);
  313. command->setTaskFactory(DHTRegistry::_taskFactory);
  314. command->setRoutingTable(DHTRegistry::_routingTable);
  315. command->setLocalNode(DHTRegistry::_localNode);
  316. e->commands.push_back(command);
  317. }
  318. }
  319. CheckIntegrityEntryHandle entry(new BtCheckIntegrityEntry(this));
  320. // --bt-seed-unverified=true is given and download has completed, skip
  321. // validation for piece hashes.
  322. if(_option->getAsBool(PREF_BT_SEED_UNVERIFIED) &&
  323. _pieceStorage->downloadFinished()) {
  324. entry->onDownloadFinished(commands, e);
  325. } else {
  326. processCheckIntegrityEntry(commands, entry, e);
  327. }
  328. return;
  329. }
  330. }
  331. #endif // ENABLE_BITTORRENT
  332. // TODO I assume here when totallength is set to DownloadContext and it is
  333. // not 0, then filepath is also set DownloadContext correctly....
  334. if(_option->getAsBool(PREF_DRY_RUN) ||
  335. _downloadContext->getTotalLength() == 0) {
  336. createNextCommand(commands, e, 1, method);
  337. }else {
  338. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  339. throw DownloadFailureException
  340. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  341. getFilePath().c_str()).str());
  342. }
  343. initPieceStorage();
  344. BtProgressInfoFileHandle infoFile
  345. (new DefaultBtProgressInfoFile(_downloadContext, _pieceStorage, _option));
  346. if(infoFile->exists() || !downloadFinishedByFileLength()) {
  347. loadAndOpenFile(infoFile);
  348. SharedHandle<CheckIntegrityEntry> checkIntegrityEntry
  349. (new StreamCheckIntegrityEntry(SharedHandle<Request>(), this));
  350. processCheckIntegrityEntry(commands, checkIntegrityEntry, e);
  351. }
  352. }
  353. }
  354. void RequestGroup::processCheckIntegrityEntry(std::deque<Command*>& commands,
  355. const CheckIntegrityEntryHandle& entry,
  356. DownloadEngine* e)
  357. {
  358. #ifdef ENABLE_MESSAGE_DIGEST
  359. if(e->option->getAsBool(PREF_CHECK_INTEGRITY) &&
  360. entry->isValidationReady()) {
  361. entry->initValidator();
  362. entry->cutTrailingGarbage();
  363. e->_checkIntegrityMan->pushEntry(entry);
  364. } else
  365. #endif // ENABLE_MESSAGE_DIGEST
  366. {
  367. entry->onDownloadIncomplete(commands, e);
  368. }
  369. }
  370. void RequestGroup::initPieceStorage()
  371. {
  372. if(_downloadContext->knowsTotalLength()) {
  373. #ifdef ENABLE_BITTORRENT
  374. SharedHandle<DefaultPieceStorage> ps;
  375. SharedHandle<PieceSelector> selector;
  376. // Use LongestSequencePieceSelector when HTTP/FTP/BitTorrent integrated
  377. // downloads. Currently multi-file integrated download is not supported.
  378. if(!_uris.empty() &&
  379. _downloadContext->getFileEntries().size() == 1 &&
  380. !dynamic_pointer_cast<BtContext>(_downloadContext).isNull()) {
  381. _logger->debug("Using LongestSequencePieceSelector");
  382. selector.reset(new LongestSequencePieceSelector());
  383. }
  384. ps.reset(new DefaultPieceStorage(_downloadContext, _option, selector));
  385. #else // !ENABLE_BITTORRENT
  386. SharedHandle<DefaultPieceStorage> ps
  387. (new DefaultPieceStorage(_downloadContext, _option));
  388. #endif // !ENABLE_BITTORRENT
  389. if(!_diskWriterFactory.isNull()) {
  390. ps->setDiskWriterFactory(_diskWriterFactory);
  391. }
  392. _pieceStorage = ps;
  393. } else {
  394. UnknownLengthPieceStorageHandle ps
  395. (new UnknownLengthPieceStorage(_downloadContext, _option));
  396. if(!_diskWriterFactory.isNull()) {
  397. ps->setDiskWriterFactory(_diskWriterFactory);
  398. }
  399. _pieceStorage = ps;
  400. }
  401. _pieceStorage->initStorage();
  402. initSegmentMan();
  403. }
  404. bool RequestGroup::downloadFinishedByFileLength()
  405. {
  406. // assuming that a control file doesn't exist.
  407. if(!isPreLocalFileCheckEnabled() ||
  408. _option->getAsBool(PREF_ALLOW_OVERWRITE) ||
  409. (_option->getAsBool(PREF_CHECK_INTEGRITY) &&
  410. !_downloadContext->getPieceHashes().empty())) {
  411. return false;
  412. }
  413. // TODO consider the case when the getFilePath() returns dir path.
  414. File outfile(getFilePath());
  415. if(outfile.exists() && getTotalLength() == outfile.size()) {
  416. _pieceStorage->markAllPiecesDone();
  417. _logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, _gid, getFilePath().c_str());
  418. return true;
  419. } else {
  420. return false;
  421. }
  422. }
  423. void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoFile)
  424. {
  425. try {
  426. if(!isPreLocalFileCheckEnabled()) {
  427. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  428. return;
  429. }
  430. // Remove the control file if download file doesn't exist
  431. if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) {
  432. progressInfoFile->removeFile();
  433. _logger->notice(MSG_REMOVED_DEFUNCT_CONTROL_FILE,
  434. progressInfoFile->getFilename().c_str(),
  435. getFilePath().c_str());
  436. }
  437. while(1) {
  438. if(progressInfoFile->exists()) {
  439. progressInfoFile->load();
  440. _pieceStorage->getDiskAdaptor()->openExistingFile();
  441. } else {
  442. File outfile(getFilePath());
  443. if(outfile.exists() && _option->getAsBool(PREF_CONTINUE) &&
  444. outfile.size() <= getTotalLength()) {
  445. _pieceStorage->getDiskAdaptor()->openExistingFile();
  446. _pieceStorage->markPiecesDone(outfile.size());
  447. } else {
  448. #ifdef ENABLE_MESSAGE_DIGEST
  449. if(outfile.exists() && _option->getAsBool(PREF_CHECK_INTEGRITY)) {
  450. _pieceStorage->getDiskAdaptor()->openExistingFile();
  451. } else {
  452. #endif // ENABLE_MESSAGE_DIGEST
  453. shouldCancelDownloadForSafety();
  454. // call updateFilename here in case when filename is renamed
  455. // by tryAutoFileRenaming()
  456. progressInfoFile->updateFilename();
  457. if(progressInfoFile->exists()) {
  458. // Close DiskAdaptor here. Renmaed file will be opened in the
  459. // next loop .
  460. _pieceStorage->getDiskAdaptor()->closeFile();
  461. continue;
  462. }
  463. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  464. #ifdef ENABLE_MESSAGE_DIGEST
  465. }
  466. #endif // ENABLE_MESSAGE_DIGEST
  467. }
  468. }
  469. setProgressInfoFile(progressInfoFile);
  470. break;
  471. }
  472. } catch(RecoverableException& e) {
  473. throw DownloadFailureException
  474. (StringFormat(EX_DOWNLOAD_ABORTED).str(), e);
  475. }
  476. }
  477. // assuming that a control file does not exist
  478. void RequestGroup::shouldCancelDownloadForSafety()
  479. {
  480. if(_option->getAsBool(PREF_ALLOW_OVERWRITE)) {
  481. return;
  482. }
  483. File outfile(getFilePath());
  484. if(outfile.exists()) {
  485. if(_option->getAsBool(PREF_AUTO_FILE_RENAMING)) {
  486. if(tryAutoFileRenaming()) {
  487. _logger->notice(MSG_FILE_RENAMED, getFilePath().c_str());
  488. } else {
  489. throw DownloadFailureException
  490. (StringFormat("File renaming failed: %s",
  491. getFilePath().c_str()).str());
  492. }
  493. } else {
  494. throw DownloadFailureException
  495. (StringFormat(MSG_FILE_ALREADY_EXISTS,
  496. getFilePath().c_str()).str());
  497. }
  498. }
  499. }
  500. bool RequestGroup::tryAutoFileRenaming()
  501. {
  502. std::string filepath = getFilePath();
  503. if(filepath.empty()) {
  504. return false;
  505. }
  506. SingleFileDownloadContextHandle ctx =
  507. dynamic_pointer_cast<SingleFileDownloadContext>(_downloadContext);
  508. // Make a copy of ctx.
  509. SingleFileDownloadContextHandle tempCtx(new SingleFileDownloadContext(*ctx.get()));
  510. DefaultBtProgressInfoFile tempInfoFile(tempCtx, SharedHandle<PieceStorage>(), 0);
  511. for(unsigned int i = 1; i < 10000; ++i) {
  512. File newfile(filepath+"."+Util::uitos(i));
  513. tempCtx->setUFilename(newfile.getPath());
  514. tempInfoFile.updateFilename();
  515. if(!newfile.exists() || (newfile.exists() && tempInfoFile.exists())) {
  516. ctx->setUFilename(newfile.getPath());
  517. return true;
  518. }
  519. }
  520. return false;
  521. }
  522. void RequestGroup::createNextCommandWithAdj(std::deque<Command*>& commands,
  523. DownloadEngine* e, int numAdj)
  524. {
  525. int numCommand;
  526. if(getTotalLength() == 0) {
  527. numCommand = 1+numAdj;
  528. } else {
  529. if(_numConcurrentCommand == 0) {
  530. // TODO remove _uris.size() support
  531. numCommand = _uris.size();
  532. } else {
  533. numCommand = _numConcurrentCommand;
  534. }
  535. numCommand = std::min(static_cast<int>(_downloadContext->getNumPieces()),
  536. numCommand);
  537. numCommand += numAdj;
  538. }
  539. if(numCommand > 0) {
  540. createNextCommand(commands, e, numCommand);
  541. }
  542. }
  543. void RequestGroup::createNextCommand(std::deque<Command*>& commands,
  544. DownloadEngine* e,
  545. unsigned int numCommand,
  546. const std::string& method)
  547. {
  548. if(_option->getAsBool(PREF_REUSE_URI) && _uris.empty()) {
  549. std::deque<std::string> uris = _spentUris;
  550. std::sort(uris.begin(), uris.end());
  551. uris.erase(std::unique(uris.begin(), uris.end()), uris.end());
  552. std::deque<std::string> errorUris(_uriResults.size());
  553. std::transform(_uriResults.begin(), _uriResults.end(),
  554. errorUris.begin(), std::mem_fun_ref(&URIResult::getURI));
  555. std::sort(errorUris.begin(), errorUris.end());
  556. errorUris.erase(std::unique(errorUris.begin(), errorUris.end()),
  557. errorUris.end());
  558. std::deque<std::string> reusableURIs;
  559. std::set_difference(uris.begin(), uris.end(),
  560. errorUris.begin(), errorUris.end(),
  561. std::back_inserter(reusableURIs));
  562. size_t ininum = reusableURIs.size();
  563. _logger->debug("Found %u reusable URIs",
  564. static_cast<unsigned int>(ininum));
  565. // Reuse at least _numConcurrentCommand URIs here to avoid to
  566. // run this process repeatedly.
  567. if(ininum > 0 && ininum < _numConcurrentCommand) {
  568. _logger->debug("fewer than _numConcurrentCommand=%u",
  569. _numConcurrentCommand);
  570. for(size_t i = 0; i < _numConcurrentCommand/ininum; ++i) {
  571. _uris.insert(_uris.end(), reusableURIs.begin(), reusableURIs.end());
  572. }
  573. _uris.insert(_uris.end(), reusableURIs.begin(),
  574. reusableURIs.begin()+(_numConcurrentCommand%ininum));
  575. _logger->debug("Duplication complete: now %u URIs for reuse",
  576. static_cast<unsigned int>(_uris.size()));
  577. }
  578. }
  579. std::deque<std::string> pendingURIs;
  580. for(; numCommand--; ) {
  581. std::string uri = _uriSelector->select(_uris);
  582. if(uri.empty())
  583. continue;
  584. RequestHandle req(new Request());
  585. if(req->setUrl(uri)) {
  586. ServerHostHandle sv;
  587. if(!_singleHostMultiConnectionEnabled){
  588. sv = searchServerHost(req->getHost());
  589. }
  590. if(sv.isNull()) {
  591. _spentUris.push_back(uri);
  592. req->setReferer(_option->get(PREF_REFERER));
  593. req->setMethod(method);
  594. Command* command =
  595. InitiateConnectionCommandFactory::createInitiateConnectionCommand
  596. (e->newCUID(), req, this, e);
  597. ServerHostHandle sv(new ServerHost(command->getCuid(), req->getHost()));
  598. registerServerHost(sv);
  599. // give a chance to be executed in the next loop in DownloadEngine
  600. command->setStatus(Command::STATUS_ONESHOT_REALTIME);
  601. commands.push_back(command);
  602. } else {
  603. pendingURIs.push_back(uri);
  604. }
  605. } else {
  606. _logger->error(MSG_UNRECOGNIZED_URI, req->getUrl().c_str());
  607. }
  608. }
  609. _uris.insert(_uris.begin(), pendingURIs.begin(), pendingURIs.end());
  610. }
  611. std::string RequestGroup::getFilePath() const
  612. {
  613. assert(!_downloadContext.isNull());
  614. if(inMemoryDownload()) {
  615. static const std::string DIR_MEMORY("[MEMORY]");
  616. return DIR_MEMORY+File(_downloadContext->getActualBasePath()).getBasename();
  617. } else {
  618. return _downloadContext->getActualBasePath();
  619. }
  620. }
  621. uint64_t RequestGroup::getTotalLength() const
  622. {
  623. if(_pieceStorage.isNull()) {
  624. return 0;
  625. } else {
  626. if(_pieceStorage->isSelectiveDownloadingMode()) {
  627. return _pieceStorage->getFilteredTotalLength();
  628. } else {
  629. return _pieceStorage->getTotalLength();
  630. }
  631. }
  632. }
  633. uint64_t RequestGroup::getCompletedLength() const
  634. {
  635. if(_pieceStorage.isNull()) {
  636. return 0;
  637. } else {
  638. if(_pieceStorage->isSelectiveDownloadingMode()) {
  639. return _pieceStorage->getFilteredCompletedLength();
  640. } else {
  641. return _pieceStorage->getCompletedLength();
  642. }
  643. }
  644. }
  645. void RequestGroup::validateFilename(const std::string& expectedFilename,
  646. const std::string& actualFilename) const
  647. {
  648. if(expectedFilename.empty()) {
  649. return;
  650. }
  651. if(expectedFilename != actualFilename) {
  652. throw DlAbortEx(StringFormat(EX_FILENAME_MISMATCH,
  653. expectedFilename.c_str(),
  654. actualFilename.c_str()).str());
  655. }
  656. }
  657. void RequestGroup::validateTotalLength(uint64_t expectedTotalLength,
  658. uint64_t actualTotalLength) const
  659. {
  660. if(expectedTotalLength <= 0) {
  661. return;
  662. }
  663. if(expectedTotalLength != actualTotalLength) {
  664. throw DlAbortEx
  665. (StringFormat(EX_SIZE_MISMATCH,
  666. Util::itos(expectedTotalLength, true).c_str(),
  667. Util::itos(actualTotalLength, true).c_str()).str());
  668. }
  669. }
  670. void RequestGroup::validateFilename(const std::string& actualFilename) const
  671. {
  672. validateFilename(_downloadContext->getFileEntries().front()->getBasename(), actualFilename);
  673. }
  674. void RequestGroup::validateTotalLength(uint64_t actualTotalLength) const
  675. {
  676. validateTotalLength(getTotalLength(), actualTotalLength);
  677. }
  678. void RequestGroup::increaseStreamConnection()
  679. {
  680. ++_numStreamConnection;
  681. }
  682. void RequestGroup::decreaseStreamConnection()
  683. {
  684. --_numStreamConnection;
  685. }
  686. unsigned int RequestGroup::getNumConnection() const
  687. {
  688. unsigned int numConnection = _numStreamConnection;
  689. #ifdef ENABLE_BITTORRENT
  690. if(!_btRuntime.isNull()) {
  691. numConnection += _btRuntime->getConnections();
  692. }
  693. #endif // ENABLE_BITTORRENT
  694. return numConnection;
  695. }
  696. void RequestGroup::increaseNumCommand()
  697. {
  698. ++_numCommand;
  699. }
  700. void RequestGroup::decreaseNumCommand()
  701. {
  702. --_numCommand;
  703. }
  704. TransferStat RequestGroup::calculateStat()
  705. {
  706. TransferStat stat;
  707. #ifdef ENABLE_BITTORRENT
  708. if(!_peerStorage.isNull()) {
  709. stat = _peerStorage->calculateStat();
  710. }
  711. #endif // ENABLE_BITTORRENT
  712. if(!_segmentMan.isNull()) {
  713. stat.setDownloadSpeed(stat.getDownloadSpeed()+_segmentMan->calculateDownloadSpeed());
  714. }
  715. return stat;
  716. }
  717. void RequestGroup::setHaltRequested(bool f)
  718. {
  719. _haltRequested = f;
  720. #ifdef ENABLE_BITTORRENT
  721. if(!_btRuntime.isNull()) {
  722. _btRuntime->setHalt(f);
  723. }
  724. #endif // ENABLE_BITTORRENT
  725. }
  726. void RequestGroup::setForceHaltRequested(bool f)
  727. {
  728. setHaltRequested(f);
  729. _forceHaltRequested = f;
  730. }
  731. void RequestGroup::releaseRuntimeResource(DownloadEngine* e)
  732. {
  733. #ifdef ENABLE_BITTORRENT
  734. BtContextHandle btContext = dynamic_pointer_cast<BtContext>(_downloadContext);
  735. if(!btContext.isNull()) {
  736. SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
  737. BtContextHandle btContextInReg =
  738. btRegistry->getBtContext(btContext->getInfoHashAsString());
  739. if(!btContextInReg.isNull() &&
  740. btContextInReg->getOwnerRequestGroup()->getGID() ==
  741. btContext->getOwnerRequestGroup()->getGID()) {
  742. btRegistry->unregister(btContext->getInfoHashAsString());
  743. if(!DHTRegistry::_peerAnnounceStorage.isNull()) {
  744. DHTRegistry::_peerAnnounceStorage->
  745. removeLocalPeerAnnounce(btContext->getInfoHash());
  746. }
  747. }
  748. }
  749. #endif // ENABLE_BITTORRENT
  750. if(!_pieceStorage.isNull()) {
  751. _pieceStorage->removeAdvertisedPiece(0);
  752. }
  753. }
  754. void RequestGroup::preDownloadProcessing()
  755. {
  756. _logger->debug("Finding PreDownloadHandler for path %s.", getFilePath().c_str());
  757. try {
  758. for(PreDownloadHandlers::const_iterator itr = _preDownloadHandlers.begin();
  759. itr != _preDownloadHandlers.end(); ++itr) {
  760. if((*itr)->canHandle(this)) {
  761. (*itr)->execute(this);
  762. return;
  763. }
  764. }
  765. } catch(RecoverableException& ex) {
  766. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  767. return;
  768. }
  769. _logger->debug("No PreDownloadHandler found.");
  770. return;
  771. }
  772. void RequestGroup::postDownloadProcessing
  773. (std::deque<SharedHandle<RequestGroup> >& groups)
  774. {
  775. _logger->debug("Finding PostDownloadHandler for path %s.", getFilePath().c_str());
  776. try {
  777. for(PostDownloadHandlers::const_iterator itr = _postDownloadHandlers.begin();
  778. itr != _postDownloadHandlers.end(); ++itr) {
  779. if((*itr)->canHandle(this)) {
  780. (*itr)->getNextRequestGroups(groups, this);
  781. return;
  782. }
  783. }
  784. } catch(RecoverableException& ex) {
  785. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  786. }
  787. _logger->debug("No PostDownloadHandler found.");
  788. }
  789. void RequestGroup::initializePreDownloadHandler()
  790. {
  791. #ifdef ENABLE_BITTORRENT
  792. if(_option->get(PREF_FOLLOW_TORRENT) == V_MEM) {
  793. _preDownloadHandlers.push_back(DownloadHandlerFactory::getBtPreDownloadHandler());
  794. }
  795. #endif // ENABLE_BITTORRENT
  796. #ifdef ENABLE_METALINK
  797. if(_option->get(PREF_FOLLOW_METALINK) == V_MEM) {
  798. _preDownloadHandlers.push_back(DownloadHandlerFactory::getMetalinkPreDownloadHandler());
  799. }
  800. #endif // ENABLE_METALINK
  801. }
  802. void RequestGroup::initializePostDownloadHandler()
  803. {
  804. #ifdef ENABLE_BITTORRENT
  805. if(_option->getAsBool(PREF_FOLLOW_TORRENT) ||
  806. _option->get(PREF_FOLLOW_TORRENT) == V_MEM) {
  807. _postDownloadHandlers.push_back(DownloadHandlerFactory::getBtPostDownloadHandler());
  808. }
  809. #endif // ENABLE_BITTORRENT
  810. #ifdef ENABLE_METALINK
  811. if(_option->getAsBool(PREF_FOLLOW_METALINK) ||
  812. _option->get(PREF_FOLLOW_METALINK) == V_MEM) {
  813. _postDownloadHandlers.push_back(DownloadHandlerFactory::getMetalinkPostDownloadHandler());
  814. }
  815. #endif // ENABLE_METALINK
  816. }
  817. void RequestGroup::getURIs(std::deque<std::string>& uris) const
  818. {
  819. uris.insert(uris.end(), _spentUris.begin(), _spentUris.end());
  820. uris.insert(uris.end(), _uris.begin(), _uris.end());
  821. }
  822. bool RequestGroup::isDependencyResolved()
  823. {
  824. if(_dependency.isNull()) {
  825. return true;
  826. }
  827. return _dependency->resolve();
  828. }
  829. void RequestGroup::setSegmentManFactory(const SegmentManFactoryHandle& segmentManFactory)
  830. {
  831. _segmentManFactory = segmentManFactory;
  832. }
  833. void RequestGroup::dependsOn(const DependencyHandle& dep)
  834. {
  835. _dependency = dep;
  836. }
  837. void RequestGroup::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  838. {
  839. _diskWriterFactory = diskWriterFactory;
  840. }
  841. DiskWriterFactoryHandle RequestGroup::getDiskWriterFactory() const
  842. {
  843. return _diskWriterFactory;
  844. }
  845. void RequestGroup::addPostDownloadHandler(const PostDownloadHandlerHandle& handler)
  846. {
  847. _postDownloadHandlers.push_back(handler);
  848. }
  849. void RequestGroup::addPreDownloadHandler(const PreDownloadHandlerHandle& handler)
  850. {
  851. _preDownloadHandlers.push_back(handler);
  852. }
  853. void RequestGroup::clearPostDowloadHandler()
  854. {
  855. _postDownloadHandlers.clear();
  856. }
  857. void RequestGroup::clearPreDowloadHandler()
  858. {
  859. _preDownloadHandlers.clear();
  860. }
  861. SegmentManHandle RequestGroup::getSegmentMan() const
  862. {
  863. return _segmentMan;
  864. }
  865. DownloadContextHandle RequestGroup::getDownloadContext() const
  866. {
  867. return _downloadContext;
  868. }
  869. void RequestGroup::setDownloadContext(const DownloadContextHandle& downloadContext)
  870. {
  871. _downloadContext = downloadContext;
  872. }
  873. PieceStorageHandle RequestGroup::getPieceStorage() const
  874. {
  875. return _pieceStorage;
  876. }
  877. void RequestGroup::setPieceStorage(const PieceStorageHandle& pieceStorage)
  878. {
  879. _pieceStorage = pieceStorage;
  880. }
  881. BtProgressInfoFileHandle RequestGroup::getProgressInfoFile() const
  882. {
  883. return _progressInfoFile;
  884. }
  885. void RequestGroup::setProgressInfoFile(const BtProgressInfoFileHandle& progressInfoFile)
  886. {
  887. _progressInfoFile = progressInfoFile;
  888. }
  889. bool RequestGroup::needsFileAllocation() const
  890. {
  891. return isFileAllocationEnabled() &&
  892. (uint64_t)_option->getAsLLInt(PREF_NO_FILE_ALLOCATION_LIMIT) <= getTotalLength() &&
  893. !_pieceStorage->getDiskAdaptor()->fileAllocationIterator()->finished();
  894. }
  895. DownloadResultHandle RequestGroup::createDownloadResult() const
  896. {
  897. std::deque<std::string> uris;
  898. getURIs(uris);
  899. uint64_t sessionDownloadLength = 0;
  900. #ifdef ENABLE_BITTORRENT
  901. if(!_peerStorage.isNull()) {
  902. sessionDownloadLength +=
  903. _peerStorage->calculateStat().getSessionDownloadLength();
  904. }
  905. #endif // ENABLE_BITTORRENT
  906. if(!_segmentMan.isNull()) {
  907. sessionDownloadLength +=
  908. _segmentMan->calculateSessionDownloadLength();
  909. }
  910. return
  911. SharedHandle<DownloadResult>
  912. (new DownloadResult(_gid,
  913. getFilePath(),
  914. getTotalLength(),
  915. uris.empty() ? A2STR::NIL:uris.front(),
  916. uris.size(),
  917. sessionDownloadLength,
  918. _downloadContext->calculateSessionTime(),
  919. downloadResult()));
  920. }
  921. void RequestGroup::registerServerHost(const ServerHostHandle& serverHost)
  922. {
  923. _serverHosts.push_back(serverHost);
  924. }
  925. class FindServerHostByCUID
  926. {
  927. private:
  928. int32_t _cuid;
  929. public:
  930. FindServerHostByCUID(int32_t cuid):_cuid(cuid) {}
  931. bool operator()(const ServerHostHandle& sv) const
  932. {
  933. return sv->getCuid() == _cuid;
  934. }
  935. };
  936. ServerHostHandle RequestGroup::searchServerHost(int32_t cuid) const
  937. {
  938. std::deque<SharedHandle<ServerHost> >::const_iterator itr =
  939. std::find_if(_serverHosts.begin(), _serverHosts.end(), FindServerHostByCUID(cuid));
  940. if(itr == _serverHosts.end()) {
  941. return SharedHandle<ServerHost>();
  942. } else {
  943. return *itr;
  944. }
  945. }
  946. class FindServerHostByHostname
  947. {
  948. private:
  949. std::string _hostname;
  950. public:
  951. FindServerHostByHostname(const std::string& hostname):_hostname(hostname) {}
  952. bool operator()(const ServerHostHandle& sv) const
  953. {
  954. return sv->getHostname() == _hostname;
  955. }
  956. };
  957. ServerHostHandle RequestGroup::searchServerHost(const std::string& hostname) const
  958. {
  959. std::deque<SharedHandle<ServerHost> >::const_iterator itr =
  960. std::find_if(_serverHosts.begin(), _serverHosts.end(), FindServerHostByHostname(hostname));
  961. if(itr == _serverHosts.end()) {
  962. return SharedHandle<ServerHost>();
  963. } else {
  964. return *itr;
  965. }
  966. }
  967. void RequestGroup::removeServerHost(int32_t cuid)
  968. {
  969. _serverHosts.erase(std::remove_if(_serverHosts.begin(), _serverHosts.end(), FindServerHostByCUID(cuid)), _serverHosts.end());
  970. }
  971. void RequestGroup::removeURIWhoseHostnameIs(const std::string& hostname)
  972. {
  973. std::deque<std::string> newURIs;
  974. Request req;
  975. for(std::deque<std::string>::const_iterator itr = _uris.begin(); itr != _uris.end(); ++itr) {
  976. if(((*itr).find(hostname) == std::string::npos) ||
  977. (req.setUrl(*itr) && (req.getHost() != hostname))) {
  978. newURIs.push_back(*itr);
  979. }
  980. }
  981. _logger->debug("GUID#%d - Removed %d duplicate hostname URIs",
  982. _gid, _uris.size()-newURIs.size());
  983. _uris = newURIs;
  984. }
  985. void RequestGroup::removeIdenticalURI(const std::string& uri)
  986. {
  987. _uris.erase(std::remove(_uris.begin(), _uris.end(), uri), _uris.end());
  988. }
  989. void RequestGroup::reportDownloadFinished()
  990. {
  991. _logger->notice(MSG_FILE_DOWNLOAD_COMPLETED,
  992. getFilePath().c_str());
  993. _uriSelector->resetCounters();
  994. #ifdef ENABLE_BITTORRENT
  995. SharedHandle<BtContext> ctx = dynamic_pointer_cast<BtContext>(_downloadContext);
  996. if(!ctx.isNull()) {
  997. TransferStat stat = calculateStat();
  998. double shareRatio = ((stat.getAllTimeUploadLength()*10)/getCompletedLength())/10.0;
  999. _logger->notice(MSG_SHARE_RATIO_REPORT,
  1000. shareRatio,
  1001. Util::abbrevSize(stat.getAllTimeUploadLength()).c_str(),
  1002. Util::abbrevSize(getCompletedLength()).c_str());
  1003. }
  1004. #endif // ENABLE_BITTORRENT
  1005. }
  1006. const std::deque<std::string>& RequestGroup::getAcceptFeatures() const
  1007. {
  1008. return _acceptFeatures;
  1009. }
  1010. void RequestGroup::addAcceptFeatureHeader(const std::string& feature)
  1011. {
  1012. if(std::find(_acceptFeatures.begin(), _acceptFeatures.end(), feature) == _acceptFeatures.end()) {
  1013. _acceptFeatures.push_back(feature);
  1014. }
  1015. }
  1016. void RequestGroup::removeAcceptFeatureHeader(const std::string& feature)
  1017. {
  1018. std::deque<std::string>::iterator i = std::find(_acceptFeatures.begin(), _acceptFeatures.end(), feature);
  1019. if(i != _acceptFeatures.end()) {
  1020. _acceptFeatures.erase(i);
  1021. }
  1022. }
  1023. const std::deque<std::string>& RequestGroup::getAcceptTypes() const
  1024. {
  1025. return _acceptTypes;
  1026. }
  1027. void RequestGroup::addAcceptType(const std::string& type)
  1028. {
  1029. if(std::find(_acceptTypes.begin(), _acceptTypes.end(), type) == _acceptTypes.end()) {
  1030. _acceptTypes.push_back(type);
  1031. }
  1032. }
  1033. void RequestGroup::removeAcceptType(const std::string& type)
  1034. {
  1035. _acceptTypes.erase(std::remove(_acceptTypes.begin(), _acceptTypes.end(), type),
  1036. _acceptTypes.end());
  1037. }
  1038. void RequestGroup::setURISelector(const SharedHandle<URISelector>& uriSelector)
  1039. {
  1040. _uriSelector = uriSelector;
  1041. }
  1042. void RequestGroup::applyLastModifiedTimeToLocalFiles()
  1043. {
  1044. if(!_pieceStorage.isNull() && _lastModifiedTime.good()) {
  1045. time_t t = _lastModifiedTime.getTime();
  1046. _logger->info("Applying Last-Modified time: %s in local time zone",
  1047. ctime(&t));
  1048. size_t n =
  1049. _pieceStorage->getDiskAdaptor()->utime(Time(), _lastModifiedTime);
  1050. _logger->info("Last-Modified attrs of %lu files were updated.",
  1051. static_cast<unsigned long>(n));
  1052. }
  1053. }
  1054. void RequestGroup::updateLastModifiedTime(const Time& time)
  1055. {
  1056. if(time.good() && _lastModifiedTime < time) {
  1057. _lastModifiedTime = time;
  1058. }
  1059. }
  1060. void RequestGroup::increaseAndValidateFileNotFoundCount()
  1061. {
  1062. ++_fileNotFoundCount;
  1063. const unsigned int maxCount = _option->getAsInt(PREF_MAX_FILE_NOT_FOUND);
  1064. if(maxCount > 0 && _fileNotFoundCount >= maxCount &&
  1065. _segmentMan->calculateSessionDownloadLength() == 0) {
  1066. throw DownloadFailureException
  1067. (StringFormat("Reached max-file-not-found count=%u", maxCount).str(),
  1068. DownloadResult::MAX_FILE_NOT_FOUND);
  1069. }
  1070. }
  1071. unsigned int RequestGroup::getNumConcurrentCommand() const
  1072. {
  1073. return _numConcurrentCommand;
  1074. }
  1075. void RequestGroup::markInMemoryDownload()
  1076. {
  1077. _inMemoryDownload = true;
  1078. }
  1079. bool RequestGroup::inMemoryDownload() const
  1080. {
  1081. return _inMemoryDownload;
  1082. }
  1083. void RequestGroup::tuneDownloadCommand(DownloadCommand* command)
  1084. {
  1085. _uriSelector->tuneDownloadCommand(_uris, command);
  1086. }
  1087. void RequestGroup::addURIResult(std::string uri, DownloadResult::RESULT result)
  1088. {
  1089. _uriResults.push_back(URIResult(uri, result));
  1090. }
  1091. const std::deque<URIResult>& RequestGroup::getURIResults() const
  1092. {
  1093. return _uriResults;
  1094. }
  1095. class FindURIResultByResult {
  1096. private:
  1097. DownloadResult::RESULT _r;
  1098. public:
  1099. FindURIResultByResult(DownloadResult::RESULT r):_r(r) {}
  1100. bool operator()(const URIResult& uriResult) const
  1101. {
  1102. return uriResult.getResult() == _r;
  1103. }
  1104. };
  1105. void RequestGroup::extractURIResult
  1106. (std::deque<URIResult>& res, DownloadResult::RESULT r)
  1107. {
  1108. std::deque<URIResult>::iterator i =
  1109. std::stable_partition(_uriResults.begin(), _uriResults.end(),
  1110. FindURIResultByResult(r));
  1111. std::copy(_uriResults.begin(), i, std::back_inserter(res));
  1112. _uriResults.erase(_uriResults.begin(), i);
  1113. }
  1114. void RequestGroup::setTimeout(time_t timeout)
  1115. {
  1116. _timeout = timeout;
  1117. }
  1118. time_t RequestGroup::getTimeout() const
  1119. {
  1120. return _timeout;
  1121. }
  1122. void RequestGroup::setMaxTries(unsigned int maxTries)
  1123. {
  1124. _maxTries = maxTries;
  1125. }
  1126. unsigned int RequestGroup::getMaxTries() const
  1127. {
  1128. return _maxTries;
  1129. }
  1130. bool RequestGroup::doesDownloadSpeedExceed()
  1131. {
  1132. return _maxDownloadSpeedLimit > 0 &&
  1133. _maxDownloadSpeedLimit < calculateStat().getDownloadSpeed();
  1134. }
  1135. bool RequestGroup::doesUploadSpeedExceed()
  1136. {
  1137. return _maxUploadSpeedLimit > 0 &&
  1138. _maxUploadSpeedLimit < calculateStat().getUploadSpeed();
  1139. }
  1140. } // namespace aria2