DownloadContext.cc 8.0 KB

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