DownloadContext.cc 8.0 KB

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