BtDependency.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "BtDependency.h"
  36. #include "RequestGroup.h"
  37. #include "Option.h"
  38. #include "LogFactory.h"
  39. #include "Logger.h"
  40. #include "DownloadContext.h"
  41. #include "RecoverableException.h"
  42. #include "message.h"
  43. #include "prefs.h"
  44. #include "util.h"
  45. #include "PieceStorage.h"
  46. #include "DiskAdaptor.h"
  47. #include "File.h"
  48. #include "bittorrent_helper.h"
  49. #include "DlAbortEx.h"
  50. #include "StringFormat.h"
  51. namespace aria2 {
  52. BtDependency::BtDependency(const WeakHandle<RequestGroup>& dependant,
  53. const SharedHandle<RequestGroup>& dependee):
  54. _dependant(dependant),
  55. _dependee(dependee),
  56. _logger(LogFactory::getInstance()) {}
  57. BtDependency::~BtDependency() {}
  58. static void copyValues(const SharedHandle<FileEntry>& d,
  59. const SharedHandle<FileEntry>& s)
  60. {
  61. d->setRequested(true);
  62. d->setPath(s->getPath());
  63. d->addUris(s->getRemainingUris().begin(),
  64. s->getRemainingUris().end());
  65. if(!s->isSingleHostMultiConnectionEnabled()) {
  66. d->disableSingleHostMultiConnection();
  67. }
  68. }
  69. bool BtDependency::resolve()
  70. {
  71. if(_dependee->getNumCommand() == 0 && _dependee->downloadFinished()) {
  72. SharedHandle<RequestGroup> dependee = _dependee;
  73. // cut reference here
  74. _dependee.reset();
  75. SharedHandle<DownloadContext> context(new DownloadContext());
  76. context->setDir(_dependant->getDownloadContext()->getDir());
  77. try {
  78. SharedHandle<DiskAdaptor> diskAdaptor =
  79. dependee->getPieceStorage()->getDiskAdaptor();
  80. diskAdaptor->openExistingFile();
  81. std::string content = util::toString(diskAdaptor);
  82. if(dependee->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
  83. SharedHandle<TorrentAttribute> attrs =
  84. bittorrent::getTorrentAttrs(dependee->getDownloadContext());
  85. bittorrent::loadFromMemory
  86. (bittorrent::metadata2Torrent(content, attrs), context, "default");
  87. } else {
  88. bittorrent::loadFromMemory
  89. (content, context, File(dependee->getFirstFilePath()).getBasename());
  90. }
  91. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  92. context->getFileEntries();
  93. const std::vector<SharedHandle<FileEntry> >& dependantFileEntries =
  94. _dependant->getDownloadContext()->getFileEntries();
  95. // If dependant's FileEntry::getOriginalName() is empty, we
  96. // assume that torrent is single file. In Metalink3, this is
  97. // always assumed.
  98. if(fileEntries.size() == 1 && dependantFileEntries.size() == 1 &&
  99. dependantFileEntries[0]->getOriginalName().empty()) {
  100. copyValues(fileEntries[0], dependantFileEntries[0]);
  101. } else {
  102. std::for_each(fileEntries.begin(), fileEntries.end(),
  103. std::bind2nd(mem_fun_sh(&FileEntry::setRequested),false));
  104. // Copy file path in _dependant's FileEntries to newly created
  105. // context's FileEntries to endorse the path structure of
  106. // _dependant. URIs and singleHostMultiConnection are also copied.
  107. std::vector<SharedHandle<FileEntry> >::const_iterator ctxFilesEnd =
  108. fileEntries.end();
  109. for(std::vector<SharedHandle<FileEntry> >::const_iterator s =
  110. dependantFileEntries.begin(), eoi = dependantFileEntries.end();
  111. s != eoi; ++s){
  112. std::vector<SharedHandle<FileEntry> >::const_iterator d =
  113. fileEntries.begin();
  114. for(; d != ctxFilesEnd; ++d) {
  115. if((*d)->getOriginalName() == (*s)->getOriginalName()) {
  116. break;
  117. }
  118. }
  119. if(d == ctxFilesEnd) {
  120. throw DL_ABORT_EX
  121. (StringFormat("No entry %s in torrent file",
  122. (*s)->getOriginalName().c_str()).str());
  123. }
  124. copyValues(*d, *s);
  125. }
  126. }
  127. } catch(RecoverableException& e) {
  128. _logger->error(EX_EXCEPTION_CAUGHT, e);
  129. if(_logger->info()) {
  130. _logger->info("BtDependency for GID#%s failed. Go without Bt.",
  131. util::itos(_dependant->getGID()).c_str());
  132. }
  133. return true;
  134. }
  135. if(_logger->info()) {
  136. _logger->info("Dependency resolved for GID#%s",
  137. util::itos(_dependant->getGID()).c_str());
  138. }
  139. _dependant->setDownloadContext(context);
  140. return true;
  141. } else if(_dependee->getNumCommand() == 0) {
  142. // _dependee's download failed.
  143. // cut reference here
  144. _dependee.reset();
  145. if(_logger->info()) {
  146. _logger->info("BtDependency for GID#%s failed. Go without Bt.",
  147. util::itos(_dependant->getGID()).c_str());
  148. }
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. } // namespace aria2