summaryrefslogtreecommitdiff
path: root/src/FileEntry.h
blob: a049b0e58515f8ce52141f98821771c8f9414281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#ifndef _D_FILE_ENTRY_H_
#define _D_FILE_ENTRY_H_

#include "common.h"

#include <string>
#include <deque>
#include <vector>
#include <ostream>

#include "SharedHandle.h"
#include "File.h"
#include "Request.h"
#include "URIResult.h"
#include "DownloadResultCode.h"
#include "A2STR.h"

namespace aria2 {

class URISelector;

class Logger;

class FileEntry {
private:
  std::string path;
  std::deque<std::string> _uris;
  std::deque<std::string> _spentUris;
  uint64_t length;
  off_t offset;
  bool requested;
  std::deque<SharedHandle<Request> > _requestPool;
  std::deque<SharedHandle<Request> > _inFlightRequests;
  std::string _contentType;
  // URIResult is stored in the ascending order of the time when its result is
  // available.
  std::deque<URIResult> _uriResults;
  bool _singleHostMultiConnection;
  std::string _originalName;
  Logger* _logger;

  void storePool(const SharedHandle<Request>& request);
public:
  FileEntry();

  FileEntry(const std::string& path, uint64_t length, off_t offset,
            const std::vector<std::string>& uris = std::vector<std::string>());

  ~FileEntry();

  FileEntry& operator=(const FileEntry& entry);

  std::string getBasename() const
  {
    return File(path).getBasename();
  }

  std::string getDirname() const
  {
    return File(path).getDirname();
  }

  const std::string& getPath() const { return path; }

  void setPath(const std::string& path) { this->path = path; }

  uint64_t getLength() const { return length; }

  void setLength(uint64_t length) { this->length = length; }

  off_t getOffset() const { return offset; }

  void setOffset(off_t offset) { this->offset = offset; }

  off_t getLastOffset() { return offset+length; }

  bool isRequested() const { return requested; }

  void setRequested(bool flag) { this->requested = flag; }

  void setupDir();

  const std::deque<std::string>& getRemainingUris() const
  {
    return _uris;
  }

  std::deque<std::string>& getRemainingUris()
  {
    return _uris;
  }

  const std::deque<std::string>& getSpentUris() const
  {
    return _spentUris;
  }

  size_t setUris(const std::vector<std::string>& uris)
  {
    _uris.clear();
    return addUris(uris.begin(), uris.end());
  }

  template<typename InputIterator>
  size_t addUris(InputIterator first, InputIterator last)
  {
    size_t count = 0;
    for(; first != last; ++first) {
      if(addUri(*first)) {
        ++count;
      }
    }
    return count;
  }

  bool addUri(const std::string& uri)
  {
    if(Request().setUri(uri)) {
      _uris.push_back(uri);
      return true;
    } else {
      return false;
    }
  }

  bool insertUri(const std::string& uri, size_t pos)
  {
    if(Request().setUri(uri)) {
      pos = std::min(pos, _uris.size());
      _uris.insert(_uris.begin()+pos, uri);
      return true;
    } else {
      return false;
    }
  }

  // Inserts _uris and _spentUris into uris.
  void getUris(std::vector<std::string>& uris) const;

  void setContentType(const std::string& contentType)
  {
    _contentType = contentType;
  }

  const std::string& getContentType() const { return _contentType; }

  std::string selectUri(const SharedHandle<URISelector>& uriSelector);

  // If pooled Request object is available, one of them is removed
  // from the pool and returned.  If pool is empty, then select URI
  // using selectUri(selector) and construct Request object using it
  // and return the Request object.  If referer is given, it is set to
  // newly created Request. If Request object is retrieved from the
  // pool, referer is ignored.  If method is given, it is set to newly
  // created Request. If Request object is retrieved from the pool,
  // method is ignored.
  SharedHandle<Request> getRequest
  (const SharedHandle<URISelector>& selector,
   const std::string& referer = A2STR::NIL,
   const std::string& method = Request::METHOD_GET);

  // Finds pooled Request object which is faster than passed one,
  // comparing their PeerStat objects. If such Request is found, it is
  // removed from the pool and returned.
  SharedHandle<Request> findFasterRequest(const SharedHandle<Request>& base);

  void poolRequest(const SharedHandle<Request>& request);

  bool removeRequest(const SharedHandle<Request>& request);

  size_t countInFlightRequest() const
  {
    return _inFlightRequests.size();
  }

  size_t countPooledRequest() const
  {
    return _requestPool.size();
  }

  const std::deque<SharedHandle<Request> >& getInFlightRequests() const
  {
    return _inFlightRequests;
  }

  bool operator<(const FileEntry& fileEntry) const;

  bool exists() const;

  // Translate global offset goff to file local offset.
  off_t gtoloff(off_t goff) const;

  void removeURIWhoseHostnameIs(const std::string& hostname);

  void removeIdenticalURI(const std::string& uri);

  void addURIResult(std::string uri, downloadresultcode::RESULT result);

  const std::deque<URIResult>& getURIResults() const
  {
    return _uriResults;
  }

  // Extracts URIResult whose _result is r and stores them into res.
  // The extracted URIResults are removed from _uriResults.
  void extractURIResult
  (std::deque<URIResult>& res, downloadresultcode::RESULT r);

  void disableSingleHostMultiConnection()
  {
    _singleHostMultiConnection = false;
  }

  bool isSingleHostMultiConnectionEnabled() const
  {
    return _singleHostMultiConnection;
  }

  // Reuse URIs which have not emitted error so far.  Thie method
  // tries to reuse at least num URIs.  If less than num URIs found to
  // reuse, those URIs are used more than once so that num URIs total
  // are available to reuse.
  void reuseUri(size_t num);

  void releaseRuntimeResource();

  void setOriginalName(const std::string& originalName)
  {
    _originalName = originalName;
  }

  const std::string& getOriginalName() const
  {
    return _originalName;
  }

  bool removeUri(const std::string& uri);

  bool emptyRequestUri() const
  {
    return _uris.empty() && _inFlightRequests.empty() && _requestPool.empty();
  }
};

// Returns the first FileEntry which isRequested() method returns
// true.  If no such FileEntry exists, then returns
// SharedHandle<FileEntry>().
template<typename InputIterator>
SharedHandle<FileEntry> getFirstRequestedFileEntry
(InputIterator first, InputIterator last)
{
  for(; first != last; ++first) {
    if((*first)->isRequested()) {
      return *first;
    }
  }
  return SharedHandle<FileEntry>();
}

// Counts the number of files selected in the given iterator range
// [first, last).
template<typename InputIterator>
size_t countRequestedFileEntry(InputIterator first, InputIterator last)
{
  size_t count = 0;
  for(; first != last; ++first) {
    if((*first)->isRequested()) {
      ++count;
    }
  }
  return count;
}

// Returns true if at least one requested FileEntry has URIs.
template<typename InputIterator>
bool isUriSuppliedForRequsetFileEntry(InputIterator first, InputIterator last)
{
  for(; first != last; ++first) {
    if((*first)->isRequested() && !(*first)->getRemainingUris().empty()) {
      return true;
    }
  }
  return false;
}

// Writes first filename to given o.  If memory is true, the output is
// "[MEMORY]" plus the basename of the first filename.  If there is no
// FileEntry, writes "n/a" to o.  If more than 1 FileEntry are in the
// iterator range [first, last), "(Nmore)" is written at the end where
// N is the number of files in iterator range [first, last) minus 1.
template<typename InputIterator>
void writeFilePath
(InputIterator first, InputIterator last, std::ostream& o, bool memory)
{
  SharedHandle<FileEntry> e = getFirstRequestedFileEntry(first, last);
  if(e.isNull()) {
    o << "n/a";
  } else {
    if(e->getPath().empty()) {
      std::vector<std::string> uris;
      e->getUris(uris);
      if(uris.empty()) {
        o << "n/a";
      } else {
        o << uris.front();
      }
    } else {
      if(memory) {
        o << "[MEMORY]" << File(e->getPath()).getBasename();
      } else {
        o << e->getPath();
      }
      size_t count = countRequestedFileEntry(first, last);
      if(count > 1) {
        o << " (" << count-1 << "more)";
      }
    }
  }
}

}

#endif // _D_FILE_ENTRY_H_