FileEntry.h 9.5 KB

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