DownloadContext.cc 5.2 KB

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