FileEntry.h 8.7 KB

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