DownloadContext.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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(SegList<int>& sgl)
  119. {
  120. sgl.normalize();
  121. if(!sgl.hasNext() || fileEntries_.size() == 1) {
  122. std::for_each(fileEntries_.begin(), fileEntries_.end(),
  123. std::bind2nd(mem_fun_sh(&FileEntry::setRequested), true));
  124. return;
  125. }
  126. assert(sgl.peek() >= 1);
  127. size_t i = 0;
  128. while(i < fileEntries_.size() && sgl.hasNext()) {
  129. size_t idx = sgl.peek()-1;
  130. if(i == idx) {
  131. fileEntries_[i]->setRequested(true);
  132. ++i;
  133. sgl.next();
  134. } else if(i < idx) {
  135. fileEntries_[i]->setRequested(false);
  136. ++i;
  137. } else {
  138. sgl.next();
  139. }
  140. }
  141. for(; i < fileEntries_.size(); ++i) {
  142. fileEntries_[i]->setRequested(false);
  143. }
  144. }
  145. void DownloadContext::setAttribute
  146. (const std::string& key, const SharedHandle<ContextAttribute>& value)
  147. {
  148. std::map<std::string, SharedHandle<ContextAttribute> >::value_type p =
  149. std::make_pair(key, value);
  150. std::pair<std::map<std::string, SharedHandle<ContextAttribute> >::iterator,
  151. bool> r = attrs_.insert(p);
  152. if(!r.second) {
  153. (*r.first).second = value;
  154. }
  155. }
  156. const SharedHandle<ContextAttribute>& DownloadContext::getAttribute
  157. (const std::string& key)
  158. {
  159. std::map<std::string, SharedHandle<ContextAttribute> >::const_iterator itr =
  160. attrs_.find(key);
  161. if(itr == attrs_.end()) {
  162. throw DL_ABORT_EX(fmt("No attribute named %s", key.c_str()));
  163. } else {
  164. return (*itr).second;
  165. }
  166. }
  167. bool DownloadContext::hasAttribute(const std::string& key) const
  168. {
  169. return attrs_.count(key) == 1;
  170. }
  171. void DownloadContext::releaseRuntimeResource()
  172. {
  173. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  174. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  175. (*i)->releaseRuntimeResource();
  176. }
  177. }
  178. size_t DownloadContext::getNumPieces() const
  179. {
  180. if(pieceLength_ == 0) {
  181. return 0;
  182. } else {
  183. assert(!fileEntries_.empty());
  184. return (fileEntries_.back()->getLastOffset()+pieceLength_-1)/pieceLength_;
  185. }
  186. }
  187. uint64_t DownloadContext::getTotalLength() const
  188. {
  189. if(fileEntries_.empty()) {
  190. return 0;
  191. } else {
  192. return fileEntries_.back()->getLastOffset();
  193. }
  194. }
  195. const std::string& DownloadContext::getBasePath() const
  196. {
  197. if(basePath_.empty()) {
  198. assert(!fileEntries_.empty());
  199. return getFirstFileEntry()->getPath();
  200. } else {
  201. return basePath_;
  202. }
  203. }
  204. SharedHandle<FileEntry>
  205. DownloadContext::getFirstRequestedFileEntry() const
  206. {
  207. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  208. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  209. if((*i)->isRequested()) {
  210. return *i;
  211. }
  212. }
  213. return SharedHandle<FileEntry>();
  214. }
  215. size_t DownloadContext::countRequestedFileEntry() const
  216. {
  217. size_t numFiles = 0;
  218. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  219. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  220. if((*i)->isRequested()) {
  221. ++numFiles;
  222. }
  223. }
  224. return numFiles;
  225. }
  226. bool DownloadContext::isChecksumVerificationNeeded() const
  227. {
  228. return pieceHashType_.empty() &&
  229. !digest_.empty() && !hashType_.empty() && !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() &&
  238. pieceHashes_.size() > 0 && 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. } else {
  245. return A2STR::NIL;
  246. }
  247. }
  248. void DownloadContext::setDigest
  249. (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(const SharedHandle<Signature>& signature)
  260. {
  261. signature_ = signature;
  262. }
  263. } // namespace aria2