BtDependency.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "fmt.h"
  51. #include "FileEntry.h"
  52. #include "SimpleRandomizer.h"
  53. namespace aria2 {
  54. BtDependency::BtDependency
  55. (RequestGroup* dependant,
  56. const std::shared_ptr<RequestGroup>& dependee)
  57. : dependant_(dependant),
  58. dependee_(dependee)
  59. {}
  60. BtDependency::~BtDependency() {}
  61. namespace {
  62. void copyValues(const std::shared_ptr<FileEntry>& d,
  63. const std::shared_ptr<FileEntry>& s)
  64. {
  65. d->setRequested(true);
  66. d->setPath(s->getPath());
  67. d->addUris(s->getRemainingUris().begin(),
  68. s->getRemainingUris().end());
  69. d->setMaxConnectionPerServer(s->getMaxConnectionPerServer());
  70. d->setUniqueProtocol(s->isUniqueProtocol());
  71. }
  72. } // namespace
  73. namespace {
  74. struct EntryCmp {
  75. bool operator()
  76. (const std::shared_ptr<FileEntry>& lhs,
  77. const std::shared_ptr<FileEntry>& rhs) const
  78. {
  79. return lhs->getOriginalName() < rhs->getOriginalName();
  80. }
  81. };
  82. } // namespace
  83. bool BtDependency::resolve()
  84. {
  85. if(!dependee_) {
  86. return true;
  87. }
  88. if(dependee_->getNumCommand() == 0 && dependee_->downloadFinished()) {
  89. std::shared_ptr<RequestGroup> dependee = dependee_;
  90. // cut reference here
  91. dependee_.reset();
  92. auto context = std::make_shared<DownloadContext>();
  93. try {
  94. std::shared_ptr<DiskAdaptor> diskAdaptor =
  95. dependee->getPieceStorage()->getDiskAdaptor();
  96. diskAdaptor->openExistingFile();
  97. std::string content = util::toString(diskAdaptor);
  98. if(dependee->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
  99. auto attrs =
  100. bittorrent::getTorrentAttrs(dependee->getDownloadContext());
  101. bittorrent::loadFromMemory
  102. (bittorrent::metadata2Torrent(content, attrs), context,
  103. dependant_->getOption(), "default");
  104. // We don't call bittorrent::adjustAnnounceUri() because it
  105. // has already been called with attrs.
  106. } else {
  107. bittorrent::loadFromMemory
  108. (content, context, dependant_->getOption(),
  109. File(dependee->getFirstFilePath()).getBasename());
  110. bittorrent::adjustAnnounceUri(bittorrent::getTorrentAttrs(context),
  111. dependant_->getOption());
  112. }
  113. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  114. context->getFileEntries();
  115. for (auto &fe : fileEntries) {
  116. auto &uri = fe->getRemainingUris();
  117. std::random_shuffle(std::begin(uri), std::end(uri),
  118. *SimpleRandomizer::getInstance());
  119. }
  120. const std::vector<std::shared_ptr<FileEntry> >& dependantFileEntries =
  121. dependant_->getDownloadContext()->getFileEntries();
  122. // If dependant's FileEntry::getOriginalName() is empty, we
  123. // assume that torrent is single file. In Metalink3, this is
  124. // always assumed.
  125. if(fileEntries.size() == 1 && dependantFileEntries.size() == 1 &&
  126. dependantFileEntries[0]->getOriginalName().empty()) {
  127. copyValues(fileEntries[0], dependantFileEntries[0]);
  128. } else {
  129. std::vector<std::shared_ptr<FileEntry> > destFiles;
  130. destFiles.reserve(fileEntries.size());
  131. for(auto & e : fileEntries) {
  132. e->setRequested(false);
  133. destFiles.push_back(e);
  134. }
  135. std::sort(destFiles.begin(), destFiles.end(), EntryCmp());
  136. // Copy file path in dependant_'s FileEntries to newly created
  137. // context's FileEntries to endorse the path structure of
  138. // dependant_. URIs and singleHostMultiConnection are also copied.
  139. for(const auto& e: dependantFileEntries){
  140. const auto d = std::lower_bound(destFiles.begin(), destFiles.end(), e,
  141. EntryCmp());
  142. if(d == destFiles.end() ||
  143. (*d)->getOriginalName() != e->getOriginalName()) {
  144. throw DL_ABORT_EX
  145. (fmt("No entry %s in torrent file", e->getOriginalName().c_str()));
  146. } else {
  147. copyValues(*d, e);
  148. }
  149. }
  150. }
  151. } catch(RecoverableException& e) {
  152. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  153. A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
  154. GroupId::toHex(dependant_->getGID()).c_str()));
  155. return true;
  156. }
  157. A2_LOG_INFO(fmt("Dependency resolved for GID#%s",
  158. GroupId::toHex(dependant_->getGID()).c_str()));
  159. dependant_->setDownloadContext(context);
  160. return true;
  161. } else if(dependee_->getNumCommand() == 0) {
  162. // dependee_'s download failed.
  163. // cut reference here
  164. dependee_.reset();
  165. A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
  166. GroupId::toHex(dependant_->getGID()).c_str()));
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. } // namespace aria2