DownloadContext.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "ContextAttribute.h"
  45. namespace aria2 {
  46. DownloadContext::DownloadContext():
  47. pieceLength_(0),
  48. checksumVerified_(false),
  49. knowsTotalLength_(true),
  50. ownerRequestGroup_(0),
  51. downloadStartTime_(0),
  52. downloadStopTime_(downloadStartTime_),
  53. metalinkServerContacted_(false) {}
  54. DownloadContext::DownloadContext(size_t pieceLength,
  55. uint64_t totalLength,
  56. const std::string& path):
  57. pieceLength_(pieceLength),
  58. checksumVerified_(false),
  59. knowsTotalLength_(true),
  60. ownerRequestGroup_(0),
  61. downloadStartTime_(0),
  62. downloadStopTime_(0),
  63. metalinkServerContacted_(false)
  64. {
  65. SharedHandle<FileEntry> fileEntry(new FileEntry(path, totalLength, 0));
  66. fileEntries_.push_back(fileEntry);
  67. }
  68. DownloadContext::~DownloadContext() {}
  69. void DownloadContext::resetDownloadStartTime()
  70. {
  71. downloadStartTime_ = global::wallclock;
  72. downloadStopTime_.reset(0);
  73. }
  74. void DownloadContext::resetDownloadStopTime()
  75. {
  76. downloadStopTime_ = global::wallclock;
  77. }
  78. int64_t DownloadContext::calculateSessionTime() const
  79. {
  80. if(downloadStopTime_ > downloadStartTime_) {
  81. return
  82. downloadStartTime_.differenceInMillis(downloadStopTime_);
  83. } else {
  84. return 0;
  85. }
  86. }
  87. SharedHandle<FileEntry>
  88. DownloadContext::findFileEntryByOffset(off_t offset) const
  89. {
  90. if(fileEntries_.empty() ||
  91. (offset > 0 &&
  92. fileEntries_.back()->getOffset()+fileEntries_.back()->getLength() <=
  93. static_cast<uint64_t>(offset))){
  94. return SharedHandle<FileEntry>();
  95. }
  96. SharedHandle<FileEntry> obj(new FileEntry());
  97. obj->setOffset(offset);
  98. std::vector<SharedHandle<FileEntry> >::const_iterator i =
  99. std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
  100. DerefLess<SharedHandle<FileEntry> >());
  101. if(i != fileEntries_.end() && (*i)->getOffset() == offset) {
  102. return *i;
  103. } else {
  104. return *(--i);
  105. }
  106. }
  107. void DownloadContext::setFilePathWithIndex
  108. (size_t index, const std::string& path)
  109. {
  110. if(0 < index && index <= fileEntries_.size()) {
  111. // We don't escape path because path may come from users.
  112. fileEntries_[index-1]->setPath(path);
  113. } else {
  114. throw DL_ABORT_EX(fmt("No such file with index=%u",
  115. static_cast<unsigned int>(index)));
  116. }
  117. }
  118. void DownloadContext::setFileFilter(IntSequence seq)
  119. {
  120. std::vector<int32_t> fileIndexes = seq.flush();
  121. std::sort(fileIndexes.begin(), fileIndexes.end());
  122. fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
  123. fileIndexes.end());
  124. bool selectAll = fileIndexes.empty() || fileEntries_.size() == 1;
  125. int32_t index = 1;
  126. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  127. fileEntries_.begin(), eoi = fileEntries_.end();
  128. i != eoi; ++i, ++index) {
  129. (*i)->setRequested
  130. (selectAll ||
  131. std::binary_search(fileIndexes.begin(), fileIndexes.end(), index));
  132. }
  133. }
  134. void DownloadContext::setAttribute
  135. (const std::string& key, const SharedHandle<ContextAttribute>& value)
  136. {
  137. std::map<std::string, SharedHandle<ContextAttribute> >::value_type p =
  138. std::make_pair(key, value);
  139. std::pair<std::map<std::string, SharedHandle<ContextAttribute> >::iterator,
  140. bool> r = attrs_.insert(p);
  141. if(!r.second) {
  142. (*r.first).second = value;
  143. }
  144. }
  145. const SharedHandle<ContextAttribute>& DownloadContext::getAttribute
  146. (const std::string& key)
  147. {
  148. std::map<std::string, SharedHandle<ContextAttribute> >::const_iterator itr =
  149. attrs_.find(key);
  150. if(itr == attrs_.end()) {
  151. throw DL_ABORT_EX(fmt("No attribute named %s", key.c_str()));
  152. } else {
  153. return (*itr).second;
  154. }
  155. }
  156. bool DownloadContext::hasAttribute(const std::string& key) const
  157. {
  158. return attrs_.count(key) == 1;
  159. }
  160. void DownloadContext::releaseRuntimeResource()
  161. {
  162. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  163. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  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. uint64_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. SharedHandle<FileEntry>
  194. DownloadContext::getFirstRequestedFileEntry() const
  195. {
  196. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  197. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  198. if((*i)->isRequested()) {
  199. return *i;
  200. }
  201. }
  202. return SharedHandle<FileEntry>();
  203. }
  204. size_t DownloadContext::countRequestedFileEntry() const
  205. {
  206. size_t numFiles = 0;
  207. for(std::vector<SharedHandle<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 SharedHandle<Signature>& signature)
  249. {
  250. signature_ = signature;
  251. }
  252. } // namespace aria2