DownloadContext.cc 5.1 KB

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