DownloadContext.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "StringFormat.h"
  39. #include "util.h"
  40. #include "wallclock.h"
  41. #include "DlAbortEx.h"
  42. namespace aria2 {
  43. DownloadContext::DownloadContext():
  44. dir_(A2STR::DOT_C),
  45. pieceLength_(0),
  46. checksumVerified_(false),
  47. knowsTotalLength_(true),
  48. ownerRequestGroup_(0),
  49. downloadStartTime_(0),
  50. downloadStopTime_(downloadStartTime_) {}
  51. DownloadContext::DownloadContext(size_t pieceLength,
  52. uint64_t totalLength,
  53. const std::string& path):
  54. dir_(A2STR::DOT_C),
  55. pieceLength_(pieceLength),
  56. checksumVerified_(false),
  57. knowsTotalLength_(true),
  58. ownerRequestGroup_(0),
  59. downloadStartTime_(0),
  60. downloadStopTime_(0)
  61. {
  62. SharedHandle<FileEntry> fileEntry(new FileEntry(path, totalLength, 0));
  63. fileEntries_.push_back(fileEntry);
  64. }
  65. void DownloadContext::resetDownloadStartTime()
  66. {
  67. downloadStartTime_ = global::wallclock;
  68. downloadStopTime_.reset(0);
  69. }
  70. void DownloadContext::resetDownloadStopTime()
  71. {
  72. downloadStopTime_ = global::wallclock;
  73. }
  74. int64_t DownloadContext::calculateSessionTime() const
  75. {
  76. if(downloadStopTime_ > downloadStartTime_) {
  77. return
  78. downloadStartTime_.differenceInMillis(downloadStopTime_);
  79. } else {
  80. return 0;
  81. }
  82. }
  83. SharedHandle<FileEntry>
  84. DownloadContext::findFileEntryByOffset(off_t offset) const
  85. {
  86. if(fileEntries_.empty() ||
  87. (offset > 0 &&
  88. fileEntries_.back()->getOffset()+fileEntries_.back()->getLength() <=
  89. static_cast<uint64_t>(offset))){
  90. return SharedHandle<FileEntry>();
  91. }
  92. SharedHandle<FileEntry> obj(new FileEntry());
  93. obj->setOffset(offset);
  94. std::vector<SharedHandle<FileEntry> >::const_iterator i =
  95. std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj);
  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(StringFormat("No such file with index=%u",
  110. static_cast<unsigned int>(index)).str());
  111. }
  112. }
  113. void DownloadContext::setFileFilter(IntSequence seq)
  114. {
  115. std::vector<int32_t> fileIndexes = seq.flush();
  116. std::sort(fileIndexes.begin(), fileIndexes.end());
  117. fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
  118. fileIndexes.end());
  119. bool selectAll = fileIndexes.empty() || fileEntries_.size() == 1;
  120. int32_t index = 1;
  121. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  122. fileEntries_.begin(), eoi = fileEntries_.end();
  123. i != eoi; ++i, ++index) {
  124. (*i)->setRequested
  125. (selectAll ||
  126. std::binary_search(fileIndexes.begin(), fileIndexes.end(), index));
  127. }
  128. }
  129. void DownloadContext::setAttribute
  130. (const std::string& key, const SharedHandle<ContextAttribute>& value)
  131. {
  132. std::map<std::string, SharedHandle<ContextAttribute> >::value_type p =
  133. std::make_pair(key, value);
  134. std::pair<std::map<std::string, SharedHandle<ContextAttribute> >::iterator,
  135. bool> r = attrs_.insert(p);
  136. if(!r.second) {
  137. (*r.first).second = value;
  138. }
  139. }
  140. const SharedHandle<ContextAttribute>& DownloadContext::getAttribute
  141. (const std::string& key)
  142. {
  143. std::map<std::string, SharedHandle<ContextAttribute> >::const_iterator itr =
  144. attrs_.find(key);
  145. if(itr == attrs_.end()) {
  146. throw DL_ABORT_EX(StringFormat("No attribute named %s", key.c_str()).str());
  147. } else {
  148. return (*itr).second;
  149. }
  150. }
  151. bool DownloadContext::hasAttribute(const std::string& key) const
  152. {
  153. return attrs_.count(key) == 1;
  154. }
  155. void DownloadContext::releaseRuntimeResource()
  156. {
  157. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  158. fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
  159. (*i)->releaseRuntimeResource();
  160. }
  161. }
  162. size_t DownloadContext::getNumPieces() const
  163. {
  164. if(pieceLength_ == 0) {
  165. return 0;
  166. } else {
  167. assert(!fileEntries_.empty());
  168. return (fileEntries_.back()->getLastOffset()+pieceLength_-1)/pieceLength_;
  169. }
  170. }
  171. uint64_t DownloadContext::getTotalLength() const
  172. {
  173. if(fileEntries_.empty()) {
  174. return 0;
  175. } else {
  176. return fileEntries_.back()->getLastOffset();
  177. }
  178. }
  179. const std::string& DownloadContext::getBasePath() const
  180. {
  181. if(basePath_.empty()) {
  182. assert(!fileEntries_.empty());
  183. return getFirstFileEntry()->getPath();
  184. } else {
  185. return basePath_;
  186. }
  187. }
  188. bool DownloadContext::isChecksumVerificationNeeded() const
  189. {
  190. return pieceHashAlgo_.empty() &&
  191. !checksum_.empty() && !checksumHashAlgo_.empty() && !checksumVerified_;
  192. }
  193. } // namespace aria2