FileEntry.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. size_t setUris(const std::vector<std::string>& uris)
  106. {
  107. _uris.clear();
  108. return addUris(uris.begin(), uris.end());
  109. }
  110. template<typename InputIterator>
  111. size_t addUris(InputIterator first, InputIterator last)
  112. {
  113. size_t count = 0;
  114. for(; first != last; ++first) {
  115. if(addUri(*first)) {
  116. ++count;
  117. }
  118. }
  119. return count;
  120. }
  121. bool addUri(const std::string& uri)
  122. {
  123. if(Request().setUri(uri)) {
  124. _uris.push_back(uri);
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. bool insertUri(const std::string& uri, size_t pos)
  131. {
  132. if(Request().setUri(uri)) {
  133. pos = std::min(pos, _uris.size());
  134. _uris.insert(_uris.begin()+pos, uri);
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. // Inserts _uris and _spentUris into uris.
  141. void getUris(std::vector<std::string>& uris) const;
  142. void setContentType(const std::string& contentType)
  143. {
  144. _contentType = contentType;
  145. }
  146. const std::string& getContentType() const { return _contentType; }
  147. std::string selectUri(const SharedHandle<URISelector>& uriSelector);
  148. // If pooled Request object is available, one of them is removed
  149. // from the pool and returned. If pool is empty, then select URI
  150. // using selectUri(selector) and construct Request object using it
  151. // and return the Request object. If referer is given, it is set to
  152. // newly created Request. If Request object is retrieved from the
  153. // pool, referer is ignored. If method is given, it is set to newly
  154. // created Request. If Request object is retrieved from the pool,
  155. // method is ignored.
  156. SharedHandle<Request> getRequest
  157. (const SharedHandle<URISelector>& selector,
  158. const std::string& referer = A2STR::NIL,
  159. const std::string& method = Request::METHOD_GET);
  160. // Finds pooled Request object which is faster than passed one,
  161. // comparing their PeerStat objects. If such Request is found, it is
  162. // removed from the pool and returned.
  163. SharedHandle<Request> findFasterRequest(const SharedHandle<Request>& base);
  164. void poolRequest(const SharedHandle<Request>& request);
  165. bool removeRequest(const SharedHandle<Request>& request);
  166. size_t countInFlightRequest() const
  167. {
  168. return _inFlightRequests.size();
  169. }
  170. size_t countPooledRequest() const
  171. {
  172. return _requestPool.size();
  173. }
  174. const std::deque<SharedHandle<Request> >& getInFlightRequests() const
  175. {
  176. return _inFlightRequests;
  177. }
  178. bool operator<(const FileEntry& fileEntry) const;
  179. bool exists() const;
  180. // Translate global offset goff to file local offset.
  181. off_t gtoloff(off_t goff) const;
  182. void removeURIWhoseHostnameIs(const std::string& hostname);
  183. void removeIdenticalURI(const std::string& uri);
  184. void addURIResult(std::string uri, downloadresultcode::RESULT result);
  185. const std::deque<URIResult>& getURIResults() const
  186. {
  187. return _uriResults;
  188. }
  189. // Extracts URIResult whose _result is r and stores them into res.
  190. // The extracted URIResults are removed from _uriResults.
  191. void extractURIResult
  192. (std::deque<URIResult>& res, downloadresultcode::RESULT r);
  193. void disableSingleHostMultiConnection()
  194. {
  195. _singleHostMultiConnection = false;
  196. }
  197. bool isSingleHostMultiConnectionEnabled() const
  198. {
  199. return _singleHostMultiConnection;
  200. }
  201. // Reuse URIs which have not emitted error so far. Thie method
  202. // tries to reuse at least num URIs. If less than num URIs found to
  203. // reuse, those URIs are used more than once so that num URIs total
  204. // are available to reuse.
  205. void reuseUri(size_t num);
  206. void releaseRuntimeResource();
  207. void setOriginalName(const std::string& originalName)
  208. {
  209. _originalName = originalName;
  210. }
  211. const std::string& getOriginalName() const
  212. {
  213. return _originalName;
  214. }
  215. bool removeUri(const std::string& uri);
  216. };
  217. // Returns the first FileEntry which isRequested() method returns
  218. // true. If no such FileEntry exists, then returns
  219. // SharedHandle<FileEntry>().
  220. template<typename InputIterator>
  221. SharedHandle<FileEntry> getFirstRequestedFileEntry
  222. (InputIterator first, InputIterator last)
  223. {
  224. for(; first != last; ++first) {
  225. if((*first)->isRequested()) {
  226. return *first;
  227. }
  228. }
  229. return SharedHandle<FileEntry>();
  230. }
  231. // Counts the number of files selected in the given iterator range
  232. // [first, last).
  233. template<typename InputIterator>
  234. size_t countRequestedFileEntry(InputIterator first, InputIterator last)
  235. {
  236. size_t count = 0;
  237. for(; first != last; ++first) {
  238. if((*first)->isRequested()) {
  239. ++count;
  240. }
  241. }
  242. return count;
  243. }
  244. // Returns true if at least one requested FileEntry has URIs.
  245. template<typename InputIterator>
  246. bool isUriSuppliedForRequsetFileEntry(InputIterator first, InputIterator last)
  247. {
  248. for(; first != last; ++first) {
  249. if((*first)->isRequested() && !(*first)->getRemainingUris().empty()) {
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. // Writes first filename to given o. If memory is true, the output is
  256. // "[MEMORY]" plus the basename of the first filename. If there is no
  257. // FileEntry, writes "n/a" to o. If more than 1 FileEntry are in the
  258. // iterator range [first, last), "(Nmore)" is written at the end where
  259. // N is the number of files in iterator range [first, last) minus 1.
  260. template<typename InputIterator>
  261. void writeFilePath
  262. (InputIterator first, InputIterator last, std::ostream& o, bool memory)
  263. {
  264. SharedHandle<FileEntry> e = getFirstRequestedFileEntry(first, last);
  265. if(e.isNull()) {
  266. o << "n/a";
  267. } else {
  268. if(e->getPath().empty()) {
  269. std::vector<std::string> uris;
  270. e->getUris(uris);
  271. if(uris.empty()) {
  272. o << "n/a";
  273. } else {
  274. o << uris.front();
  275. }
  276. } else {
  277. if(memory) {
  278. o << "[MEMORY]" << File(e->getPath()).getBasename();
  279. } else {
  280. o << e->getPath();
  281. }
  282. size_t count = countRequestedFileEntry(first, last);
  283. if(count > 1) {
  284. o << " (" << count-1 << "more)";
  285. }
  286. }
  287. }
  288. }
  289. }
  290. #endif // _D_FILE_ENTRY_H_