BtDependency.cc 6.2 KB

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