DownloadContext.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "DownloadContext.h"
  36. #include <algorithm>
  37. #include "FileEntry.h"
  38. #include "fmt.h"
  39. #include "util.h"
  40. #include "wallclock.h"
  41. #include "DlAbortEx.h"
  42. #include "a2functional.h"
  43. #include "Signature.h"
  44. #include "RequestGroupMan.h"
  45. namespace aria2 {
  46. DownloadContext::DownloadContext()
  47. : ownerRequestGroup_(nullptr),
  48. attrs_(MAX_CTX_ATTR),
  49. downloadStopTime_(Timer::zero()),
  50. pieceLength_(0),
  51. checksumVerified_(false),
  52. knowsTotalLength_(true),
  53. acceptMetalink_(true)
  54. {
  55. }
  56. DownloadContext::DownloadContext(int32_t pieceLength, int64_t totalLength,
  57. std::string path)
  58. : ownerRequestGroup_(nullptr),
  59. attrs_(MAX_CTX_ATTR),
  60. downloadStopTime_(Timer::zero()),
  61. pieceLength_(pieceLength),
  62. checksumVerified_(false),
  63. knowsTotalLength_(true),
  64. acceptMetalink_(true)
  65. {
  66. fileEntries_.push_back(
  67. std::make_shared<FileEntry>(std::move(path), totalLength, 0));
  68. }
  69. DownloadContext::~DownloadContext() = default;
  70. void DownloadContext::resetDownloadStartTime()
  71. {
  72. downloadStopTime_ = Timer::zero();
  73. netStat_.downloadStart();
  74. }
  75. void DownloadContext::resetDownloadStopTime()
  76. {
  77. downloadStopTime_ = global::wallclock();
  78. netStat_.downloadStop();
  79. }
  80. Timer::Clock::duration DownloadContext::calculateSessionTime() const
  81. {
  82. const Timer& startTime = netStat_.getDownloadStartTime();
  83. return startTime.difference(downloadStopTime_);
  84. }
  85. std::shared_ptr<FileEntry>
  86. DownloadContext::findFileEntryByOffset(int64_t offset) const
  87. {
  88. if (fileEntries_.empty() ||
  89. (offset > 0 && fileEntries_.back()->getLastOffset() <= offset)) {
  90. return nullptr;
  91. }
  92. auto obj = std::make_shared<FileEntry>();
  93. obj->setOffset(offset);
  94. auto i = std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
  95. DerefLess<std::shared_ptr<FileEntry>>());
  96. if (i != fileEntries_.end() && (*i)->getOffset() == offset) {
  97. return *i;
  98. }
  99. else {
  100. return *(--i);
  101. }
  102. }
  103. void DownloadContext::setFilePathWithIndex(size_t index,
  104. const std::string& path)
  105. {
  106. if (0 < index && index <= fileEntries_.size()) {
  107. // We don't escape path because path may come from users.
  108. fileEntries_[index - 1]->setPath(path);
  109. }
  110. else {
  111. throw DL_ABORT_EX(
  112. fmt("No such file with index=%u", static_cast<unsigned int>(index)));
  113. }
  114. }
  115. void DownloadContext::setFileFilter(SegList<int> sgl)
  116. {
  117. using namespace std::placeholders;
  118. if (!sgl.hasNext() || fileEntries_.size() == 1) {
  119. std::for_each(fileEntries_.begin(), fileEntries_.end(),
  120. std::bind(&FileEntry::setRequested, _1, true));
  121. return;
  122. }
  123. assert(sgl.peek() >= 1);
  124. size_t len = fileEntries_.size();
  125. size_t i = 0;
  126. for (; i < len && sgl.hasNext(); ++i) {
  127. size_t idx = sgl.peek() - 1;
  128. if (i == idx) {
  129. fileEntries_[i]->setRequested(true);
  130. sgl.next();
  131. }
  132. else if (i < idx) {
  133. fileEntries_[i]->setRequested(false);
  134. }
  135. }
  136. for (; i < len; ++i) {
  137. fileEntries_[i]->setRequested(false);
  138. }
  139. }
  140. void DownloadContext::setAttribute(ContextAttributeType key,
  141. std::shared_ptr<ContextAttribute> value)
  142. {
  143. assert(key < MAX_CTX_ATTR);
  144. attrs_[key] = std::move(value);
  145. }
  146. const std::shared_ptr<ContextAttribute>&
  147. DownloadContext::getAttribute(ContextAttributeType key)
  148. {
  149. assert(key < MAX_CTX_ATTR);
  150. const auto& attr = attrs_[key];
  151. if (!attr) {
  152. throw DL_ABORT_EX(
  153. fmt("No attribute named %s", strContextAttributeType(key)));
  154. }
  155. return attr;
  156. }
  157. bool DownloadContext::hasAttribute(ContextAttributeType key) const
  158. {
  159. assert(key < MAX_CTX_ATTR);
  160. return attrs_[key].get();
  161. }
  162. const std::vector<std::shared_ptr<ContextAttribute>>&
  163. DownloadContext::getAttributes() const
  164. {
  165. return attrs_;
  166. }
  167. void DownloadContext::releaseRuntimeResource()
  168. {
  169. for (std::vector<std::shared_ptr<FileEntry>>::const_iterator
  170. i = fileEntries_.begin(),
  171. eoi = fileEntries_.end();
  172. i != eoi; ++i) {
  173. (*i)->putBackRequest();
  174. (*i)->releaseRuntimeResource();
  175. }
  176. }
  177. size_t DownloadContext::getNumPieces() const
  178. {
  179. if (pieceLength_ == 0) {
  180. return 0;
  181. }
  182. else {
  183. assert(!fileEntries_.empty());
  184. return (fileEntries_.back()->getLastOffset() + pieceLength_ - 1) /
  185. pieceLength_;
  186. }
  187. }
  188. int64_t DownloadContext::getTotalLength() const
  189. {
  190. if (fileEntries_.empty()) {
  191. return 0;
  192. }
  193. else {
  194. return fileEntries_.back()->getLastOffset();
  195. }
  196. }
  197. const std::string& DownloadContext::getBasePath() const
  198. {
  199. if (basePath_.empty()) {
  200. assert(!fileEntries_.empty());
  201. return getFirstFileEntry()->getPath();
  202. }
  203. else {
  204. return basePath_;
  205. }
  206. }
  207. std::shared_ptr<FileEntry> DownloadContext::getFirstRequestedFileEntry() const
  208. {
  209. for (auto& e : fileEntries_) {
  210. if (e->isRequested()) {
  211. return e;
  212. }
  213. }
  214. return nullptr;
  215. }
  216. size_t DownloadContext::countRequestedFileEntry() const
  217. {
  218. size_t numFiles = 0;
  219. for (const auto& e : fileEntries_) {
  220. if (e->isRequested()) {
  221. ++numFiles;
  222. }
  223. }
  224. return numFiles;
  225. }
  226. bool DownloadContext::isChecksumVerificationNeeded() const
  227. {
  228. return pieceHashType_.empty() && !digest_.empty() && !hashType_.empty() &&
  229. !checksumVerified_;
  230. }
  231. bool DownloadContext::isChecksumVerificationAvailable() const
  232. {
  233. return !digest_.empty() && !hashType_.empty();
  234. }
  235. bool DownloadContext::isPieceHashVerificationAvailable() const
  236. {
  237. return !pieceHashType_.empty() && pieceHashes_.size() > 0 &&
  238. pieceHashes_.size() == getNumPieces();
  239. }
  240. const std::string& DownloadContext::getPieceHash(size_t index) const
  241. {
  242. if (index < pieceHashes_.size()) {
  243. return pieceHashes_[index];
  244. }
  245. else {
  246. return A2STR::NIL;
  247. }
  248. }
  249. void DownloadContext::setDigest(const std::string& hashType,
  250. const std::string& digest)
  251. {
  252. hashType_ = hashType;
  253. digest_ = digest;
  254. }
  255. void DownloadContext::setBasePath(const std::string& basePath)
  256. {
  257. basePath_ = basePath;
  258. }
  259. void DownloadContext::setSignature(std::unique_ptr<Signature> signature)
  260. {
  261. signature_ = std::move(signature);
  262. }
  263. void DownloadContext::updateDownload(size_t bytes)
  264. {
  265. netStat_.updateDownload(bytes);
  266. RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan();
  267. if (rgman) {
  268. rgman->getNetStat().updateDownload(bytes);
  269. }
  270. }
  271. void DownloadContext::updateUploadSpeed(size_t bytes)
  272. {
  273. netStat_.updateUploadSpeed(bytes);
  274. auto rgman = ownerRequestGroup_->getRequestGroupMan();
  275. if (rgman) {
  276. rgman->getNetStat().updateUploadSpeed(bytes);
  277. }
  278. }
  279. void DownloadContext::updateUploadLength(size_t bytes)
  280. {
  281. netStat_.updateUploadLength(bytes);
  282. auto rgman = ownerRequestGroup_->getRequestGroupMan();
  283. if (rgman) {
  284. rgman->getNetStat().updateUploadLength(bytes);
  285. }
  286. }
  287. } // namespace aria2