summaryrefslogtreecommitdiff
path: root/src/SessionSerializer.cc
blob: 9e149f0a5f9780a116295711308264516371314c (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2010 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 --> */
#include "SessionSerializer.h"

#include <cstdio>
#include <cassert>
#include <iterator>
#include <set>

#include "RequestGroupMan.h"
#include "a2functional.h"
#include "File.h"
#include "A2STR.h"
#include "download_helper.h"
#include "Option.h"
#include "DownloadResult.h"
#include "FileEntry.h"
#include "prefs.h"
#include "util.h"
#include "array_fun.h"
#include "BufferedFile.h"
#include "OptionParser.h"
#include "OptionHandler.h"
#include "MessageDigest.h"

#if HAVE_ZLIB
#include "GZipFile.h"
#endif

namespace aria2 {

SessionSerializer::SessionSerializer
(RequestGroupMan* requestGroupMan)
  : rgman_{requestGroupMan},
    saveError_{true},
    saveInProgress_{true},
    saveWaiting_{true}
{}

bool SessionSerializer::save(const std::string& filename) const
{
  std::string tempFilename = filename;
  tempFilename += "__temp";
  {
    std::shared_ptr<IOFile> fp;
#if HAVE_ZLIB
    if (util::endsWith(filename, ".gz")) {
      fp.reset(new GZipFile(tempFilename.c_str(), IOFile::WRITE));
    }
    else
#endif
    {
     fp.reset(new BufferedFile(tempFilename.c_str(), IOFile::WRITE));
    }
    if(!*fp) {
      return false;
    }
    if(!save(*fp) || fp->close() == EOF) {
      return false;
    }
  }
  return File(tempFilename).renameTo(filename);
}

namespace {
// Write 1 line of option name/value pair. This function returns true
// if it succeeds, or false.
bool writeOptionLine(IOFile& fp, PrefPtr pref,
                     const std::string& val)
{
  size_t prefLen = strlen(pref->k);
  return fp.write(" ", 1) == 1 &&
    fp.write(pref->k, prefLen) == prefLen &&
    fp.write("=", 1) == 1 &&
    fp.write(val.c_str(), val.size()) == val.size() &&
    fp.write("\n", 1) == 1;
}
} // namespace

namespace {
bool writeOption(IOFile& fp, const std::shared_ptr<Option>& op)
{
  const std::shared_ptr<OptionParser>& oparser = OptionParser::getInstance();
  for(size_t i = 1, len = option::countOption(); i < len; ++i) {
    PrefPtr pref = option::i2p(i);
    const OptionHandler* h = oparser->find(pref);
    if(h && h->getInitialOption() && op->definedLocal(pref)) {
      if(h->getCumulative()) {
        const std::string& val = op->get(pref);
        std::vector<std::string> v;
        util::split(val.begin(), val.end(), std::back_inserter(v), '\n',
                    false, false);
        for(std::vector<std::string>::const_iterator j = v.begin(),
              eoj = v.end(); j != eoj; ++j) {
          if(!writeOptionLine(fp, pref, *j)) {
            return false;
          }
        }
      } else {
        if(!writeOptionLine(fp, pref, op->get(pref))) {
          return false;
        }
      }
    }
  }
  return true;
}
} // namespace

namespace {
  template<typename T>
  class Unique {
    typedef T type;
    struct PointerCmp {
      inline bool operator()(const type* x, const type* y) {
        return *x < *y;
      }
    };
    std::set<const type*, PointerCmp> known;
  public:
    inline bool operator()(const type& v) {
      return known.insert(&v).second;
    }
  };

  bool writeUri(IOFile& fp, const std::string& uri)
  {
    return fp.write(uri.c_str(), uri.size()) == uri.size() &&
      fp.write("\t", 1) == 1;
  }

  template<typename InputIterator, class UnaryPredicate>
  bool writeUri(IOFile& fp, InputIterator first, InputIterator last,
                UnaryPredicate& filter)
  {
    for(; first != last; ++first) {
      if (!filter(*first)) {
        continue;
      }
      if(!writeUri(fp, *first)) {
        return false;
      }
    }
    return true;
  }
} // namespace

// The downloads whose followedBy() is empty is persisited with its
// GID without no problem. For other cases, there are several patterns.
//
// 1. magnet URI
//  GID of metadata download is persisted.
// 2. URI to torrent file
//  GID of torrent file download is persisted.
// 3. URI to metalink file
//  GID of metalink file download is persisted.
// 4. local torrent file
//  GID of torrent download itself is persisted.
// 5. local metalink file
//  No GID is persisted. GID is saved but it is just a random GID.

namespace {
bool writeDownloadResult
(IOFile& fp, std::set<a2_gid_t>& metainfoCache,
 const std::shared_ptr<DownloadResult>& dr)
{
  const std::shared_ptr<MetadataInfo>& mi = dr->metadataInfo;
  if(dr->belongsTo != 0 || (mi && mi->dataOnly())) {
    return true;
  }
  if(!mi) {
    // With --force-save option, same gid may be saved twice. (e.g.,
    // Downloading .meta4 followed by its conent download. First
    // .meta4 download is saved and second content download is also
    // saved with the same gid.)
    if(metainfoCache.count(dr->gid->getNumericId()) != 0) {
      return true;
    } else {
      metainfoCache.insert(dr->gid->getNumericId());
    }
    // only save first file entry
    if(dr->fileEntries.empty()) {
      return true;
    }
    const std::shared_ptr<FileEntry>& file = dr->fileEntries[0];
    // Don't save download if there are no URIs.
    const bool hasRemaining = !file->getRemainingUris().empty();
    const bool hasSpent = !file->getSpentUris().empty();
    if (!hasRemaining && !hasSpent) {
      return true;
    }

    // Save spent URIs + remaining URIs. Remove URI in spent URI which
    // also exists in remaining URIs.
    {
      Unique<std::string> unique;
      if (hasRemaining && !writeUri(fp, file->getRemainingUris().begin(),
                                    file->getRemainingUris().end(),
                                    unique)) {
        return false;
      }
      if (hasSpent && !writeUri(fp, file->getSpentUris().begin(),
                                file->getSpentUris().end(),
                                unique)) {
        return false;
      }
    }
    if(fp.write("\n", 1) != 1) {
      return false;
    }
    if(!writeOptionLine(fp, PREF_GID, dr->gid->toHex())) {
      return false;
    }
  } else {
    if(metainfoCache.count(mi->getGID()) != 0) {
      return true;
    } else {
      metainfoCache.insert(mi->getGID());
      if (fp.write(mi->getUri().c_str(),
                   mi->getUri().size()) != mi->getUri().size() ||
          fp.write("\n", 1) != 1) {
        return false;
      }
      // For downloads generated by metadata (e.g., BitTorrent,
      // Metalink), save gid of Metadata download.
      if(!writeOptionLine(fp, PREF_GID, GroupId::toHex(mi->getGID()))) {
        return false;
      }
    }
  }
  return writeOption(fp, dr->option);
}
} // namespace

bool SessionSerializer::save(IOFile& fp) const
{
  std::set<a2_gid_t> metainfoCache;
  const DownloadResultList& results = rgman_->getDownloadResults();
  for(const auto& dr : results) {
    if(dr->result == error_code::FINISHED ||
       dr->result == error_code::REMOVED) {
      if(dr->option->getAsBool(PREF_FORCE_SAVE)) {
        if(!writeDownloadResult(fp, metainfoCache, dr)) {
          return false;
        }
      } else {
        continue;
      }
    } else if(dr->result == error_code::IN_PROGRESS) {
      if(saveInProgress_) {
        if(!writeDownloadResult(fp, metainfoCache, dr)) {
          return false;
        }
      }
    } else {
      // error download
      if(saveError_) {
        if(!writeDownloadResult(fp, metainfoCache, dr)) {
          return false;
        }
      }
    }
  }
  {
    // Save active downloads.
    const RequestGroupList& groups = rgman_->getRequestGroups();
    for(const auto& rg : groups) {
      std::shared_ptr<DownloadResult> dr = rg->createDownloadResult();
      bool stopped = dr->result == error_code::FINISHED ||
        dr->result == error_code::REMOVED;
      if((!stopped && saveInProgress_) ||
         (stopped && dr->option->getAsBool(PREF_FORCE_SAVE))) {
        if(!writeDownloadResult(fp, metainfoCache, dr)) {
          return false;
        }
      }
    }
  }
  if(saveWaiting_) {
    const RequestGroupList& groups = rgman_->getReservedGroups();
    for(const auto& rg : groups) {
      std::shared_ptr<DownloadResult> result = rg->createDownloadResult();
      if(!writeDownloadResult(fp, metainfoCache, result)) {
        return false;
      }
      // PREF_PAUSE was removed from option, so save it here looking
      // property separately.
      if(rg->isPauseRequested()) {
        if(!writeOptionLine(fp, PREF_PAUSE, A2_V_TRUE)) {
          return false;
        }
      }
    }
  }
  return true;
}

namespace {
// Class to calculate SHA1 hash value for data written into this
// object.  No file I/O is done in this class.
class SHA1IOFile : public IOFile {
public:
  SHA1IOFile()
    : sha1_(MessageDigest::sha1())
  {}

  std::string digest()
  {
    return sha1_->digest();
  }
protected:
  virtual size_t onRead(void* ptr, size_t count) CXX11_OVERRIDE
  {
    assert(0);
  }

  virtual size_t onWrite(const void* ptr, size_t count) CXX11_OVERRIDE
  {
    sha1_->update(ptr, count);

    return count;
  }

  virtual char* onGets(char* s, int size) CXX11_OVERRIDE
  {
    assert(0);
  }

  virtual int onVprintf(const char* format, va_list va) CXX11_OVERRIDE
  {
    assert(0);
  }

  virtual int onFlush() CXX11_OVERRIDE
  {
    return 0;
  }

  virtual int onClose() CXX11_OVERRIDE
  {
    return 0;
  }

  virtual bool onSupportsColor() CXX11_OVERRIDE
  {
    return false;
  }

  virtual bool isError() const CXX11_OVERRIDE
  {
    return false;
  }

  virtual bool isEOF() const CXX11_OVERRIDE
  {
    return false;
  }

  virtual bool isOpen() const CXX11_OVERRIDE
  {
    return true;
  }

private:
  std::unique_ptr<MessageDigest> sha1_;
};
} // namespace

std::string SessionSerializer::calculateHash() const
{
  SHA1IOFile sha1io;

  auto rv = save(sha1io);

  if(!rv) {
    return "";
  }

  return sha1io.digest();
}

} // namespace aria2