DownloadContext.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. DownloadContext::DownloadContext(size_t pieceLength,
  54. uint64_t totalLength,
  55. const std::string& path):
  56. pieceLength_(pieceLength),
  57. checksumVerified_(false),
  58. knowsTotalLength_(true),
  59. ownerRequestGroup_(0),
  60. downloadStartTime_(0),
  61. downloadStopTime_(0)
  62. {
  63. SharedHandle<FileEntry> fileEntry(new FileEntry(path, totalLength, 0));
  64. fileEntries_.push_back(fileEntry);
  65. }
  66. DownloadContext::~DownloadContext() {}
  67. void DownloadContext::resetDownloadStartTime()
  68. {
  69. downloadStartTime_ = global::wallclock;
  70. downloadStopTime_.reset(0);
  71. }
  72. void DownloadContext::resetDownloadStopTime()
  73. {
  74. downloadStopTime_ = global::wallclock;
  75. }
  76. int64_t DownloadContext::calculateSessionTime() const
  77. {
  78. if(downloadStopTime_ > downloadStartTime_) {
  79. return
  80. downloadStartTime_.differenceInMillis(downloadStopTime_);
  81. } else {
  82. return 0;
  83. }
  84. }
  85. SharedHandle<FileEntry>
  86. DownloadContext::findFileEntryByOffset(off_t offset) const
  87. {
  88. if(fileEntries_.empty() ||
  89. (offset > 0 &&
  90. fileEntries_.back()->getOffset()+fileEntries_.back()->getLength() <=
  91. static_cast<uint64_t>(offset))){
  92. return SharedHandle<FileEntry>();
  93. }
  94. SharedHandle<FileEntry> obj(new FileEntry());
  95. obj->setOffset(offset);
  96. std::vector<SharedHandle<FileEntry> >::const_iterator i =
  97. std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
  98. DerefLess<SharedHandle<FileEntry> >());
  99. if(i != fileEntries_.end() && (*i)->getOffset() == offset) {
  100. return *i;
  101. } else {
  102. return *(--i);
  103. }
  104. }
  105. void DownloadContext::setFilePathWithIndex
  106. (size_t index, const std::string& path)
  107. {
  108. if(0 < index && index <= fileEntries_.size()) {
  109. // We don't escape path because path may come from users.
  110. fileEntries_[index-1]->setPath(path);
  111. } else {
  112. throw DL_ABORT_EX(fmt("No such file with index=%u",
  113. static_cast<unsigned int>(index)));
  114. }
  115. }
  116. void DownloadContext::setFileFilter(IntSequence seq)
  117. {
  118. std::vector<int32_t> fileIndexes = seq.flush();
  119. std::sort(fileIndexes.begin(), fileIndexes.end());
  120. fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
  121. fileIndexes.end());
  122. bool selectAll = fileIndexes.empty() || fileEntries_.size() == 1;
  123. int32_t index = 1;
  124. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  125. fileEntries_.begin(), eoi = fileEntries_.end();
  126. i != eoi; ++i, ++index) {
  127. (*i)->setRequested
  128. (selectAll ||
  129. std::binary_search(fileIndexes.begin(), fileIndexes.end(), index));
  130. }
  131. }
  132. void DownloadContext::setAttribute
  133. (const std::string& key, const SharedHandle<ContextAttribute>& value)
  134. {
  135. std::map<std::string, SharedHandle<ContextAttribute> >::value_type p =
  136. std::make_pair(key, value);
  137. std::pair<std::map<std::string, SharedHandle<ContextAttribute> >::iterator,
  138. bool> r = attrs_.insert(p);
  139. if(!r.second) {
  140. (*r.first).second = value;
  141. }
  142. }
  143. const SharedHandle<ContextAttribute>& DownloadContext::getAttribute
  144. (const std::string& key)
  145. {
  146. std::map<std::string, SharedHandle<ContextAttribute> >::const_iterator itr =
  147. attrs_.find(key);
  148. if(itr == attrs_.end()) {
  149. throw DL_ABORT_EX(fmt("No attribute named %s", key.c_str()));
  150. } else {
  151. return (*itr).second;
  152. }
  153. }
  154. bool DownloadContext::hasAttribute(const std::string& key) const
  155. {
  156. return attrs_.count(key) == 1;
  157. }
  158. void DownloadContext::releaseRuntimeResource()
  159. {
  160. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  161. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  162. (*i)->releaseRuntimeResource();
  163. }
  164. }
  165. size_t DownloadContext::getNumPieces() const
  166. {
  167. if(pieceLength_ == 0) {
  168. return 0;
  169. } else {
  170. assert(!fileEntries_.empty());
  171. return (fileEntries_.back()->getLastOffset()+pieceLength_-1)/pieceLength_;
  172. }
  173. }
  174. uint64_t DownloadContext::getTotalLength() const
  175. {
  176. if(fileEntries_.empty()) {
  177. return 0;
  178. } else {
  179. return fileEntries_.back()->getLastOffset();
  180. }
  181. }
  182. const std::string& DownloadContext::getBasePath() const
  183. {
  184. if(basePath_.empty()) {
  185. assert(!fileEntries_.empty());
  186. return getFirstFileEntry()->getPath();
  187. } else {
  188. return basePath_;
  189. }
  190. }
  191. SharedHandle<FileEntry>
  192. DownloadContext::getFirstRequestedFileEntry() const
  193. {
  194. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  195. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  196. if((*i)->isRequested()) {
  197. return *i;
  198. }
  199. }
  200. return SharedHandle<FileEntry>();
  201. }
  202. size_t DownloadContext::countRequestedFileEntry() const
  203. {
  204. size_t numFiles = 0;
  205. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  206. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  207. if((*i)->isRequested()) {
  208. ++numFiles;
  209. }
  210. }
  211. return numFiles;
  212. }
  213. bool DownloadContext::isChecksumVerificationNeeded() const
  214. {
  215. return pieceHashAlgo_.empty() &&
  216. !checksum_.empty() && !checksumHashAlgo_.empty() && !checksumVerified_;
  217. }
  218. bool DownloadContext::isChecksumVerificationAvailable() const
  219. {
  220. return !checksum_.empty() && !checksumHashAlgo_.empty();
  221. }
  222. bool DownloadContext::isPieceHashVerificationAvailable() const
  223. {
  224. return !pieceHashAlgo_.empty() &&
  225. pieceHashes_.size() > 0 && pieceHashes_.size() == getNumPieces();
  226. }
  227. const std::string& DownloadContext::getPieceHash(size_t index) const
  228. {
  229. if(index < pieceHashes_.size()) {
  230. return pieceHashes_[index];
  231. } else {
  232. return A2STR::NIL;
  233. }
  234. }
  235. void DownloadContext::setPieceHashAlgo(const std::string& algo)
  236. {
  237. pieceHashAlgo_ = algo;
  238. }
  239. void DownloadContext::setChecksum(const std::string& checksum)
  240. {
  241. checksum_ = checksum;
  242. }
  243. void DownloadContext::setChecksumHashAlgo(const std::string& algo)
  244. {
  245. checksumHashAlgo_ = algo;
  246. }
  247. void DownloadContext::setBasePath(const std::string& basePath)
  248. {
  249. basePath_ = basePath;
  250. }
  251. void DownloadContext::setSignature(const SharedHandle<Signature>& signature)
  252. {
  253. signature_ = signature;
  254. }
  255. } // namespace aria2