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. std::string selectUri(const SharedHandle<URISelector>& uriSelector);
  124. // If pooled Request object is available, one of them is removed
  125. // from the pool and returned. If pool is empty, then select URI
  126. // using selectUri(selector) and construct Request object using it
  127. // and return the Request object. If referer is given, it is set to
  128. // newly created Request. If Request object is retrieved from the
  129. // pool, referer is ignored. If method is given, it is set to newly
  130. // created Request. If Request object is retrieved from the pool,
  131. // method is ignored. If uriReuse is true and selector does not
  132. // returns Request object either because uris_ is empty or all URI
  133. // are not be usable because maxConnectionPerServer_ limit, then
  134. // reuse used URIs and do selection again.
  135. SharedHandle<Request> getRequest
  136. (const SharedHandle<URISelector>& selector,
  137. bool uriReuse = true,
  138. const std::string& referer = A2STR::NIL,
  139. const std::string& method = Request::METHOD_GET);
  140. // Finds pooled Request object which is faster than passed one,
  141. // comparing their PeerStat objects. If such Request is found, it is
  142. // removed from the pool and returned.
  143. SharedHandle<Request> findFasterRequest(const SharedHandle<Request>& base);
  144. void poolRequest(const SharedHandle<Request>& request);
  145. bool removeRequest(const SharedHandle<Request>& request);
  146. size_t countInFlightRequest() const
  147. {
  148. return inFlightRequests_.size();
  149. }
  150. size_t countPooledRequest() const
  151. {
  152. return requestPool_.size();
  153. }
  154. const std::deque<SharedHandle<Request> >& getInFlightRequests() const
  155. {
  156. return inFlightRequests_;
  157. }
  158. bool operator<(const FileEntry& fileEntry) const;
  159. bool exists() const;
  160. // Translate global offset goff to file local offset.
  161. off_t gtoloff(off_t goff) const;
  162. void removeURIWhoseHostnameIs(const std::string& hostname);
  163. void removeIdenticalURI(const std::string& uri);
  164. void addURIResult(std::string uri, downloadresultcode::RESULT result);
  165. const std::deque<URIResult>& getURIResults() const
  166. {
  167. return uriResults_;
  168. }
  169. // Extracts URIResult whose _result is r and stores them into res.
  170. // The extracted URIResults are removed from uriResults_.
  171. void extractURIResult
  172. (std::deque<URIResult>& res, downloadresultcode::RESULT r);
  173. void setMaxConnectionPerServer(size_t n)
  174. {
  175. maxConnectionPerServer_ = n;
  176. }
  177. size_t getMaxConnectionPerServer() const
  178. {
  179. return maxConnectionPerServer_;
  180. }
  181. // Reuse URIs which have not emitted error so far and whose host
  182. // component is not included in ignore. The reusable URIs are
  183. // appended to uris_ maxConnectionPerServer_ times.
  184. void reuseUri(const std::vector<std::string>& ignore);
  185. void releaseRuntimeResource();
  186. void setOriginalName(const std::string& originalName)
  187. {
  188. originalName_ = originalName;
  189. }
  190. const std::string& getOriginalName() const
  191. {
  192. return originalName_;
  193. }
  194. bool removeUri(const std::string& uri);
  195. bool emptyRequestUri() const
  196. {
  197. return uris_.empty() && inFlightRequests_.empty() && requestPool_.empty();
  198. }
  199. void setUniqueProtocol(bool f)
  200. {
  201. uniqueProtocol_ = f;
  202. }
  203. bool isUniqueProtocol() const
  204. {
  205. return uniqueProtocol_;
  206. }
  207. };
  208. // Returns the first FileEntry which isRequested() method returns
  209. // true. If no such FileEntry exists, then returns
  210. // SharedHandle<FileEntry>().
  211. template<typename InputIterator>
  212. SharedHandle<FileEntry> getFirstRequestedFileEntry
  213. (InputIterator first, InputIterator last)
  214. {
  215. for(; first != last; ++first) {
  216. if((*first)->isRequested()) {
  217. return *first;
  218. }
  219. }
  220. return SharedHandle<FileEntry>();
  221. }
  222. // Counts the number of files selected in the given iterator range
  223. // [first, last).
  224. template<typename InputIterator>
  225. size_t countRequestedFileEntry(InputIterator first, InputIterator last)
  226. {
  227. size_t count = 0;
  228. for(; first != last; ++first) {
  229. if((*first)->isRequested()) {
  230. ++count;
  231. }
  232. }
  233. return count;
  234. }
  235. // Returns true if at least one requested FileEntry has URIs.
  236. template<typename InputIterator>
  237. bool isUriSuppliedForRequsetFileEntry(InputIterator first, InputIterator last)
  238. {
  239. for(; first != last; ++first) {
  240. if((*first)->isRequested() && !(*first)->getRemainingUris().empty()) {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. // Writes first filename to given o. If memory is true, the output is
  247. // "[MEMORY]" plus the basename of the first filename. If there is no
  248. // FileEntry, writes "n/a" to o. If more than 1 FileEntry are in the
  249. // iterator range [first, last), "(Nmore)" is written at the end where
  250. // N is the number of files in iterator range [first, last) minus 1.
  251. template<typename InputIterator>
  252. void writeFilePath
  253. (InputIterator first, InputIterator last, std::ostream& o, bool memory)
  254. {
  255. SharedHandle<FileEntry> e = getFirstRequestedFileEntry(first, last);
  256. if(e.isNull()) {
  257. o << "n/a";
  258. } else {
  259. if(e->getPath().empty()) {
  260. std::vector<std::string> uris;
  261. e->getUris(uris);
  262. if(uris.empty()) {
  263. o << "n/a";
  264. } else {
  265. o << uris.front();
  266. }
  267. } else {
  268. if(memory) {
  269. o << "[MEMORY]" << File(e->getPath()).getBasename();
  270. } else {
  271. o << e->getPath();
  272. }
  273. size_t count = countRequestedFileEntry(first, last);
  274. if(count > 1) {
  275. o << " (" << count-1 << "more)";
  276. }
  277. }
  278. }
  279. }
  280. }
  281. #endif // _D_FILE_ENTRY_H_