DownloadContext.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #ifndef _D_DOWNLOAD_CONTEXT_H_
  36. #define _D_DOWNLOAD_CONTEXT_H_
  37. #include "common.h"
  38. #include <cassert>
  39. #include <string>
  40. #include <vector>
  41. #include "SharedHandle.h"
  42. #include "Signature.h"
  43. #include "TimerA2.h"
  44. #include "A2STR.h"
  45. #include "ValueBase.h"
  46. #include "IntSequence.h"
  47. #include "FileEntry.h"
  48. #include "TorrentAttribute.h"
  49. namespace aria2 {
  50. class RequestGroup;
  51. class DownloadContext
  52. {
  53. private:
  54. std::vector<SharedHandle<FileEntry> > fileEntries_;
  55. std::string dir_;
  56. std::vector<std::string> pieceHashes_;
  57. size_t pieceLength_;
  58. std::string pieceHashAlgo_;
  59. std::string checksum_;
  60. std::string checksumHashAlgo_;
  61. bool checksumVerified_;
  62. std::string basePath_;
  63. bool knowsTotalLength_;
  64. RequestGroup* ownerRequestGroup_;
  65. std::map<std::string, SharedHandle<ContextAttribute> > attrs_;
  66. Timer downloadStartTime_;
  67. Timer downloadStopTime_;
  68. SharedHandle<Signature> signature_;
  69. public:
  70. DownloadContext();
  71. // Convenient constructor that creates single file download. path
  72. // should be escaped with util::escapePath(...).
  73. DownloadContext(size_t pieceLength,
  74. uint64_t totalLength,
  75. const std::string& path = A2STR::NIL);
  76. const std::string& getPieceHash(size_t index) const
  77. {
  78. if(index < pieceHashes_.size()) {
  79. return pieceHashes_[index];
  80. } else {
  81. return A2STR::NIL;
  82. }
  83. }
  84. const std::vector<std::string>& getPieceHashes() const
  85. {
  86. return pieceHashes_;
  87. }
  88. template<typename InputIterator>
  89. void setPieceHashes(InputIterator first, InputIterator last)
  90. {
  91. pieceHashes_.assign(first, last);
  92. }
  93. uint64_t getTotalLength() const;
  94. bool knowsTotalLength() const { return knowsTotalLength_; }
  95. void markTotalLengthIsUnknown() { knowsTotalLength_ = false; }
  96. void markTotalLengthIsKnown() { knowsTotalLength_ = true; }
  97. const std::vector<SharedHandle<FileEntry> >& getFileEntries() const
  98. {
  99. return fileEntries_;
  100. }
  101. const SharedHandle<FileEntry>& getFirstFileEntry() const
  102. {
  103. assert(!fileEntries_.empty());
  104. return fileEntries_[0];
  105. }
  106. template<typename InputIterator>
  107. void setFileEntries(InputIterator first, InputIterator last)
  108. {
  109. fileEntries_.assign(first, last);
  110. }
  111. size_t getPieceLength() const { return pieceLength_; }
  112. void setPieceLength(size_t length) { pieceLength_ = length; }
  113. size_t getNumPieces() const;
  114. const std::string& getPieceHashAlgo() const { return pieceHashAlgo_; }
  115. void setPieceHashAlgo(const std::string& algo)
  116. {
  117. pieceHashAlgo_ = algo;
  118. }
  119. const std::string& getChecksum() const { return checksum_; }
  120. void setChecksum(const std::string& checksum)
  121. {
  122. checksum_ = checksum;
  123. }
  124. const std::string& getChecksumHashAlgo() const { return checksumHashAlgo_; }
  125. void setChecksumHashAlgo(const std::string& algo)
  126. {
  127. checksumHashAlgo_ = algo;
  128. }
  129. // The representative path name for this context. It is used as a
  130. // part of .aria2 control file. If basePath_ is set, returns
  131. // basePath_. Otherwise, the first FileEntry's getFilePath() is
  132. // returned.
  133. const std::string& getBasePath() const;
  134. void setBasePath(const std::string& basePath) { basePath_ = basePath; }
  135. const std::string& getDir() const { return dir_; }
  136. void setDir(const std::string& dir) { dir_ = dir; }
  137. const SharedHandle<Signature>& getSignature() const { return signature_; }
  138. void setSignature(const SharedHandle<Signature>& signature)
  139. {
  140. signature_ = signature;
  141. }
  142. RequestGroup* getOwnerRequestGroup() { return ownerRequestGroup_; }
  143. void setOwnerRequestGroup(RequestGroup* owner)
  144. {
  145. ownerRequestGroup_ = owner;
  146. }
  147. void setFileFilter(IntSequence seq);
  148. // Sets file path for specified index. index starts from 1. The
  149. // index is the same used in setFileFilter(). Please note that path
  150. // is not the actual file path. The actual file path is
  151. // getDir()+"/"+path. path is not escaped by util::escapePath() in
  152. // this function.
  153. void setFilePathWithIndex(size_t index, const std::string& path);
  154. // Returns true if hash check(whole file hash, not piece hash) is
  155. // need to be done
  156. bool isChecksumVerificationNeeded() const;
  157. void setChecksumVerified(bool f)
  158. {
  159. checksumVerified_ = f;
  160. }
  161. void setAttribute
  162. (const std::string& key, const SharedHandle<ContextAttribute>& value);
  163. const SharedHandle<ContextAttribute>& getAttribute(const std::string& key);
  164. bool hasAttribute(const std::string& key) const;
  165. void resetDownloadStartTime();
  166. void resetDownloadStopTime();
  167. const Timer& getDownloadStopTime() const
  168. {
  169. return downloadStopTime_;
  170. }
  171. int64_t calculateSessionTime() const;
  172. // Returns FileEntry at given offset. SharedHandle<FileEntry>() is
  173. // returned if no such FileEntry is found.
  174. SharedHandle<FileEntry> findFileEntryByOffset(off_t offset) const;
  175. void releaseRuntimeResource();
  176. };
  177. } // namespace aria2
  178. #endif // _D_DOWNLOAD_CONTEXT_H_