FileEntry.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_FILE_ENTRY_H
  36. #define D_FILE_ENTRY_H
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <vector>
  41. #include <ostream>
  42. #include <set>
  43. #include <memory>
  44. #include "File.h"
  45. #include "Request.h"
  46. #include "URIResult.h"
  47. #include "error_code.h"
  48. #include "A2STR.h"
  49. #include "TimerA2.h"
  50. #include "util.h"
  51. #include "a2functional.h"
  52. namespace aria2 {
  53. class URISelector;
  54. class ServerStatMan;
  55. class FileEntry {
  56. public:
  57. typedef std::set<std::shared_ptr<Request>, RefLess<Request>>
  58. InFlightRequestSet;
  59. private:
  60. class RequestFaster {
  61. public:
  62. bool operator()(const std::shared_ptr<Request>& lhs,
  63. const std::shared_ptr<Request>& rhs) const;
  64. };
  65. typedef std::set<std::shared_ptr<Request>, RequestFaster> RequestPool;
  66. int64_t length_;
  67. int64_t offset_;
  68. std::deque<std::string> uris_;
  69. std::deque<std::string> spentUris_;
  70. // URIResult is stored in the ascending order of the time when its result is
  71. // available.
  72. std::deque<URIResult> uriResults_;
  73. RequestPool requestPool_;
  74. InFlightRequestSet inFlightRequests_;
  75. std::string path_;
  76. std::string contentType_;
  77. std::string originalName_;
  78. // path_ without parent directory component. This is primarily used
  79. // to change directory (PREF_DIR option).
  80. std::string suffixPath_;
  81. Timer lastFasterReplace_;
  82. int maxConnectionPerServer_;
  83. bool requested_;
  84. bool uniqueProtocol_;
  85. void storePool(const std::shared_ptr<Request>& request);
  86. std::shared_ptr<Request> getRequestWithInFlightHosts(
  87. URISelector* selector, bool uriReuse,
  88. const std::vector<std::pair<size_t, std::string>>& usedHosts,
  89. const std::string& referer, const std::string& method,
  90. const std::vector<std::string>& inFlightHosts);
  91. public:
  92. FileEntry();
  93. FileEntry(std::string path, int64_t length, int64_t offset,
  94. const std::vector<std::string>& uris = std::vector<std::string>());
  95. ~FileEntry();
  96. FileEntry& operator=(const FileEntry& entry);
  97. std::string getBasename() const;
  98. std::string getDirname() const;
  99. const std::string& getPath() const { return path_; }
  100. void setPath(std::string path);
  101. int64_t getLength() const { return length_; }
  102. void setLength(int64_t length) { length_ = length; }
  103. int64_t getOffset() const { return offset_; }
  104. void setOffset(int64_t offset) { offset_ = offset; }
  105. int64_t getLastOffset() { return offset_ + length_; }
  106. bool isRequested() const { return requested_; }
  107. void setRequested(bool flag) { requested_ = flag; }
  108. const std::deque<std::string>& getRemainingUris() const { return uris_; }
  109. std::deque<std::string>& getRemainingUris() { return uris_; }
  110. const std::deque<std::string>& getSpentUris() const { return spentUris_; }
  111. // Exposed for unittest
  112. std::deque<std::string>& getSpentUris() { return spentUris_; }
  113. size_t setUris(const std::vector<std::string>& uris);
  114. template <typename InputIterator>
  115. size_t addUris(InputIterator first, InputIterator last)
  116. {
  117. size_t count = 0;
  118. for (; first != last; ++first) {
  119. if (addUri(*first)) {
  120. ++count;
  121. }
  122. }
  123. return count;
  124. }
  125. bool addUri(const std::string& uri);
  126. bool insertUri(const std::string& uri, size_t pos);
  127. // Returns uris_ and spentUris_ in single std::vector<std::string>.
  128. std::vector<std::string> getUris() const;
  129. void setContentType(std::string contentType);
  130. const std::string& getContentType() const { return contentType_; }
  131. // If pooled Request object is available, one of them is removed
  132. // from the pool and returned. If pool is empty, then select URI
  133. // using selectUri(selector) and construct Request object using it
  134. // and return the Request object. If referer is given, it is set to
  135. // newly created Request. If Request object is retrieved from the
  136. // pool, referer is ignored. If method is given, it is set to newly
  137. // created Request. If Request object is retrieved from the pool,
  138. // method is ignored. If uriReuse is true and selector does not
  139. // returns Request object either because uris_ is empty or all URI
  140. // are not be usable because maxConnectionPerServer_ limit, then
  141. // reuse used URIs and do selection again.
  142. std::shared_ptr<Request>
  143. getRequest(URISelector* selector, bool uriReuse,
  144. const std::vector<std::pair<size_t, std::string>>& usedHosts,
  145. const std::string& referer = A2STR::NIL,
  146. const std::string& method = Request::METHOD_GET);
  147. // Finds pooled Request object which is faster than passed one,
  148. // comparing their PeerStat objects. If such Request is found, it is
  149. // removed from the pool and returned.
  150. std::shared_ptr<Request>
  151. findFasterRequest(const std::shared_ptr<Request>& base);
  152. // Finds faster server using ServerStatMan.
  153. std::shared_ptr<Request> findFasterRequest(
  154. const std::shared_ptr<Request>& base,
  155. const std::vector<std::pair<size_t, std::string>>& usedHosts,
  156. const std::shared_ptr<ServerStatMan>& serverStatMan);
  157. void poolRequest(const std::shared_ptr<Request>& request);
  158. bool removeRequest(const std::shared_ptr<Request>& request);
  159. size_t countInFlightRequest() const;
  160. size_t countPooledRequest() const;
  161. const InFlightRequestSet& getInFlightRequests() const
  162. {
  163. return inFlightRequests_;
  164. }
  165. bool operator<(const FileEntry& fileEntry) const;
  166. bool exists() const;
  167. // Translate global offset goff to file local offset.
  168. int64_t gtoloff(int64_t goff) const;
  169. void removeURIWhoseHostnameIs(const std::string& hostname);
  170. void removeIdenticalURI(const std::string& uri);
  171. void addURIResult(std::string uri, error_code::Value result);
  172. const std::deque<URIResult>& getURIResults() const { return uriResults_; }
  173. // Extracts URIResult whose _result is r and stores them into res.
  174. // The extracted URIResults are removed from uriResults_.
  175. void extractURIResult(std::deque<URIResult>& res, error_code::Value r);
  176. void setMaxConnectionPerServer(int n) { maxConnectionPerServer_ = n; }
  177. int getMaxConnectionPerServer() const { return maxConnectionPerServer_; }
  178. // Reuse URIs which have not emitted error so far and whose host
  179. // component is not included in ignore. The reusable URIs are
  180. // appended to uris_ maxConnectionPerServer_ times.
  181. void reuseUri(const std::vector<std::string>& ignore);
  182. void releaseRuntimeResource();
  183. // Push URIs in pooled or in-flight requests to the front of uris_.
  184. void putBackRequest();
  185. void setOriginalName(std::string originalName);
  186. const std::string& getOriginalName() const { return originalName_; }
  187. void setSuffixPath(std::string suffixPath);
  188. const std::string& getSuffixPath() const { return suffixPath_; }
  189. bool removeUri(const std::string& uri);
  190. bool emptyRequestUri() const;
  191. void setUniqueProtocol(bool f) { uniqueProtocol_ = f; }
  192. bool isUniqueProtocol() const { return uniqueProtocol_; }
  193. };
  194. // Returns the first FileEntry which isRequested() method returns
  195. // true. If no such FileEntry exists, then returns
  196. // std::shared_ptr<FileEntry>().
  197. template <typename InputIterator>
  198. std::shared_ptr<FileEntry> getFirstRequestedFileEntry(InputIterator first,
  199. InputIterator last)
  200. {
  201. for (; first != last; ++first) {
  202. if ((*first)->isRequested()) {
  203. return *first;
  204. }
  205. }
  206. return nullptr;
  207. }
  208. // Counts the number of files selected in the given iterator range
  209. // [first, last).
  210. template <typename InputIterator>
  211. size_t countRequestedFileEntry(InputIterator first, InputIterator last)
  212. {
  213. size_t count = 0;
  214. for (; first != last; ++first) {
  215. if ((*first)->isRequested()) {
  216. ++count;
  217. }
  218. }
  219. return count;
  220. }
  221. // Returns true if at least one requested FileEntry has URIs.
  222. template <typename InputIterator>
  223. bool isUriSuppliedForRequsetFileEntry(InputIterator first, InputIterator last)
  224. {
  225. for (; first != last; ++first) {
  226. if ((*first)->isRequested() && !(*first)->getRemainingUris().empty()) {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. // Writes filename to given o. If memory is true, the output is
  233. // "[MEMORY]" plus the basename of the filename. If there is no
  234. // FileEntry, writes "n/a" to o.
  235. void writeFilePath(std::ostream& o, const std::shared_ptr<FileEntry>& entry,
  236. bool memory);
  237. // Writes first filename to given o. If memory is true, the output is
  238. // "[MEMORY]" plus the basename of the first filename. If there is no
  239. // FileEntry, writes "n/a" to o. If more than 1 FileEntry are in the
  240. // iterator range [first, last), "(Nmore)" is written at the end where
  241. // N is the number of files in iterator range [first, last) minus 1.
  242. template <typename InputIterator>
  243. void writeFilePath(InputIterator first, InputIterator last, std::ostream& o,
  244. bool memory)
  245. {
  246. std::shared_ptr<FileEntry> e = getFirstRequestedFileEntry(first, last);
  247. if (!e) {
  248. o << "n/a";
  249. }
  250. else {
  251. writeFilePath(o, e, memory);
  252. if (!e->getPath().empty()) {
  253. size_t count = countRequestedFileEntry(first, last);
  254. if (count > 1) {
  255. o << " (" << count - 1 << "more)";
  256. }
  257. }
  258. }
  259. }
  260. } // namespace aria2
  261. #endif // D_FILE_ENTRY_H