DownloadContext.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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() {}
  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::unique_ptr<ContextAttribute> value)
  142. {
  143. assert(key < MAX_CTX_ATTR);
  144. attrs_[key] = std::move(value);
  145. }
  146. const std::unique_ptr<ContextAttribute>&
  147. DownloadContext::getAttribute(ContextAttributeType key)
  148. {
  149. assert(key < MAX_CTX_ATTR);
  150. const std::unique_ptr<ContextAttribute>& attr = attrs_[key];
  151. if (attr) {
  152. return attr;
  153. }
  154. else {
  155. throw DL_ABORT_EX(
  156. fmt("No attribute named %s", strContextAttributeType(key)));
  157. }
  158. }
  159. bool DownloadContext::hasAttribute(ContextAttributeType key) const
  160. {
  161. assert(key < MAX_CTX_ATTR);
  162. return attrs_[key].get();
  163. }
  164. void DownloadContext::releaseRuntimeResource()
  165. {
  166. for (std::vector<std::shared_ptr<FileEntry>>::const_iterator
  167. i = fileEntries_.begin(),
  168. eoi = fileEntries_.end();
  169. i != eoi; ++i) {
  170. (*i)->putBackRequest();
  171. (*i)->releaseRuntimeResource();
  172. }
  173. }
  174. size_t DownloadContext::getNumPieces() const
  175. {
  176. if (pieceLength_ == 0) {
  177. return 0;
  178. }
  179. else {
  180. assert(!fileEntries_.empty());
  181. return (fileEntries_.back()->getLastOffset() + pieceLength_ - 1) /
  182. pieceLength_;
  183. }
  184. }
  185. int64_t DownloadContext::getTotalLength() const
  186. {
  187. if (fileEntries_.empty()) {
  188. return 0;
  189. }
  190. else {
  191. return fileEntries_.back()->getLastOffset();
  192. }
  193. }
  194. const std::string& DownloadContext::getBasePath() const
  195. {
  196. if (basePath_.empty()) {
  197. assert(!fileEntries_.empty());
  198. return getFirstFileEntry()->getPath();
  199. }
  200. else {
  201. return basePath_;
  202. }
  203. }
  204. std::shared_ptr<FileEntry> DownloadContext::getFirstRequestedFileEntry() const
  205. {
  206. for (auto& e : fileEntries_) {
  207. if (e->isRequested()) {
  208. return e;
  209. }
  210. }
  211. return nullptr;
  212. }
  213. size_t DownloadContext::countRequestedFileEntry() const
  214. {
  215. size_t numFiles = 0;
  216. for (const auto& e : fileEntries_) {
  217. if (e->isRequested()) {
  218. ++numFiles;
  219. }
  220. }
  221. return numFiles;
  222. }
  223. bool DownloadContext::isChecksumVerificationNeeded() const
  224. {
  225. return pieceHashType_.empty() && !digest_.empty() && !hashType_.empty() &&
  226. !checksumVerified_;
  227. }
  228. bool DownloadContext::isChecksumVerificationAvailable() const
  229. {
  230. return !digest_.empty() && !hashType_.empty();
  231. }
  232. bool DownloadContext::isPieceHashVerificationAvailable() const
  233. {
  234. return !pieceHashType_.empty() && pieceHashes_.size() > 0 &&
  235. pieceHashes_.size() == getNumPieces();
  236. }
  237. const std::string& DownloadContext::getPieceHash(size_t index) const
  238. {
  239. if (index < pieceHashes_.size()) {
  240. return pieceHashes_[index];
  241. }
  242. else {
  243. return A2STR::NIL;
  244. }
  245. }
  246. void DownloadContext::setDigest(const std::string& hashType,
  247. const std::string& digest)
  248. {
  249. hashType_ = hashType;
  250. digest_ = digest;
  251. }
  252. void DownloadContext::setBasePath(const std::string& basePath)
  253. {
  254. basePath_ = basePath;
  255. }
  256. void DownloadContext::setSignature(std::unique_ptr<Signature> signature)
  257. {
  258. signature_ = std::move(signature);
  259. }
  260. void DownloadContext::updateDownload(size_t bytes)
  261. {
  262. netStat_.updateDownload(bytes);
  263. RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan();
  264. if (rgman) {
  265. rgman->getNetStat().updateDownload(bytes);
  266. }
  267. }
  268. void DownloadContext::updateUploadSpeed(size_t bytes)
  269. {
  270. netStat_.updateUploadSpeed(bytes);
  271. auto rgman = ownerRequestGroup_->getRequestGroupMan();
  272. if (rgman) {
  273. rgman->getNetStat().updateUploadSpeed(bytes);
  274. }
  275. }
  276. void DownloadContext::updateUploadLength(size_t bytes)
  277. {
  278. netStat_.updateUploadLength(bytes);
  279. auto rgman = ownerRequestGroup_->getRequestGroupMan();
  280. if (rgman) {
  281. rgman->getNetStat().updateUploadLength(bytes);
  282. }
  283. }
  284. } // namespace aria2