FileEntry.h 8.8 KB

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