DownloadContext.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. namespace aria2 {
  42. DownloadContext::DownloadContext():
  43. _dir(A2STR::DOT_C),
  44. _pieceLength(0),
  45. _knowsTotalLength(true),
  46. _ownerRequestGroup(0),
  47. _downloadStartTime(0),
  48. _downloadStopTime(_downloadStartTime) {}
  49. DownloadContext::DownloadContext(size_t pieceLength,
  50. uint64_t totalLength,
  51. const std::string& path):
  52. _dir(A2STR::DOT_C),
  53. _pieceLength(pieceLength),
  54. _knowsTotalLength(true),
  55. _ownerRequestGroup(0),
  56. _downloadStartTime(0),
  57. _downloadStopTime(0)
  58. {
  59. SharedHandle<FileEntry> fileEntry
  60. (new FileEntry(util::escapePath(path), totalLength, 0));
  61. _fileEntries.push_back(fileEntry);
  62. }
  63. void DownloadContext::resetDownloadStartTime()
  64. {
  65. _downloadStartTime = global::wallclock;
  66. }
  67. void DownloadContext::resetDownloadStopTime()
  68. {
  69. _downloadStopTime = global::wallclock;
  70. }
  71. int64_t DownloadContext::calculateSessionTime() const
  72. {
  73. if(_downloadStopTime.isNewer(_downloadStartTime)) {
  74. return
  75. _downloadStopTime.getTimeInMillis()-_downloadStartTime.getTimeInMillis();
  76. } else {
  77. return 0;
  78. }
  79. }
  80. SharedHandle<FileEntry>
  81. DownloadContext::findFileEntryByOffset(off_t offset) const
  82. {
  83. if(_fileEntries.empty() ||
  84. (offset > 0 &&
  85. _fileEntries.back()->getOffset()+_fileEntries.back()->getLength() <=
  86. static_cast<uint64_t>(offset))){
  87. return SharedHandle<FileEntry>();
  88. }
  89. SharedHandle<FileEntry> obj(new FileEntry());
  90. obj->setOffset(offset);
  91. std::vector<SharedHandle<FileEntry> >::const_iterator i =
  92. std::upper_bound(_fileEntries.begin(), _fileEntries.end(), obj);
  93. if(i != _fileEntries.end() && (*i)->getOffset() == offset) {
  94. return *i;
  95. } else {
  96. return *(--i);
  97. }
  98. }
  99. void DownloadContext::setFilePathWithIndex
  100. (size_t index, const std::string& path)
  101. {
  102. if(0 < index && index <= _fileEntries.size()) {
  103. // We don't escape path because path may come from users.
  104. _fileEntries[index-1]->setPath(path);
  105. } else {
  106. throw DL_ABORT_EX(StringFormat("No such file with index=%u",
  107. static_cast<unsigned int>(index)).str());
  108. }
  109. }
  110. void DownloadContext::setFileFilter(IntSequence seq)
  111. {
  112. std::vector<int32_t> fileIndexes = seq.flush();
  113. std::sort(fileIndexes.begin(), fileIndexes.end());
  114. fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
  115. fileIndexes.end());
  116. bool selectAll = fileIndexes.empty() || _fileEntries.size() == 1;
  117. int32_t index = 1;
  118. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  119. _fileEntries.begin(), eoi = _fileEntries.end();
  120. i != eoi; ++i, ++index) {
  121. (*i)->setRequested
  122. (selectAll ||
  123. std::binary_search(fileIndexes.begin(), fileIndexes.end(), index));
  124. }
  125. }
  126. void DownloadContext::ensureAttrs()
  127. {
  128. if(_attrs.isNone()) {
  129. _attrs = BDE::dict();
  130. }
  131. }
  132. void DownloadContext::setAttribute(const std::string& key, const BDE& value)
  133. {
  134. ensureAttrs();
  135. _attrs[key] = value;
  136. }
  137. BDE& DownloadContext::getAttribute(const std::string& key)
  138. {
  139. ensureAttrs();
  140. if(_attrs.containsKey(key)) {
  141. return _attrs[key];
  142. } else {
  143. throw DL_ABORT_EX(StringFormat("No attribute named %s", key.c_str()).str());
  144. }
  145. }
  146. bool DownloadContext::hasAttribute(const std::string& key) const
  147. {
  148. if(_attrs.isNone()) {
  149. return false;
  150. } else {
  151. return _attrs.containsKey(key);
  152. }
  153. }
  154. void DownloadContext::releaseRuntimeResource()
  155. {
  156. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  157. _fileEntries.begin(), eoi = _fileEntries.end(); i != eoi; ++i) {
  158. (*i)->releaseRuntimeResource();
  159. }
  160. }
  161. } // namespace aria2