diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-09-24 11:35:45 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-09-24 11:42:43 +0900 |
| commit | 55f311a9084aba9f0601388183ff10abb0baac8c (patch) | |
| tree | b070d65cd15c4afb9cc3ed98e253d29bbe227abc /src/SessionSerializer.cc | |
| parent | c3aedf480dd4e224165d9b8b5ed8735191eede6c (diff) | |
Add --keep-unfinished-download-result option
This option keeps unfinished download results even if doing so exceeds
--max-download-result. This is useful if all unfinished downloads must
be saved in session file (see --save-session option). Please keep in
mind that there is no upper bound to the number of unfinished download
result to keep. User should use this option only when they know the
total number of downloads in advance.
Diffstat (limited to 'src/SessionSerializer.cc')
| -rw-r--r-- | src/SessionSerializer.cc | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/src/SessionSerializer.cc b/src/SessionSerializer.cc index 0d260a3f..8dd9008a 100644 --- a/src/SessionSerializer.cc +++ b/src/SessionSerializer.cc @@ -272,11 +272,14 @@ bool writeDownloadResult(IOFile& fp, std::set<a2_gid_t>& metainfoCache, } } // namespace -bool SessionSerializer::save(IOFile& fp) const +namespace { +template <typename InputIt> +bool saveDownloadResult(IOFile& fp, std::set<a2_gid_t>& metainfoCache, + InputIt first, InputIt last, bool saveInProgress, + bool saveError) { - std::set<a2_gid_t> metainfoCache; - const DownloadResultList& results = rgman_->getDownloadResults(); - for (const auto& dr : results) { + for (; first != last; ++first) { + const auto& dr = *first; auto save = false; switch (dr->result) { case error_code::FINISHED: @@ -284,20 +287,41 @@ bool SessionSerializer::save(IOFile& fp) const save = dr->option->getAsBool(PREF_FORCE_SAVE); break; case error_code::IN_PROGRESS: - save = saveInProgress_; + save = saveInProgress; break; case error_code::RESOURCE_NOT_FOUND: case error_code::MAX_FILE_NOT_FOUND: - save = saveError_ && dr->option->getAsBool(PREF_SAVE_NOT_FOUND); + save = saveError && dr->option->getAsBool(PREF_SAVE_NOT_FOUND); break; default: - save = saveError_; + save = saveError; break; } if (save && !writeDownloadResult(fp, metainfoCache, dr, false)) { return false; } } + return true; +} +} // namespace + +bool SessionSerializer::save(IOFile& fp) const +{ + std::set<a2_gid_t> metainfoCache; + + const auto& unfinishedResults = rgman_->getUnfinishedDownloadResult(); + if (!saveDownloadResult(fp, metainfoCache, std::begin(unfinishedResults), + std::end(unfinishedResults), saveInProgress_, + saveError_)) { + return false; + } + + const auto& results = rgman_->getDownloadResults(); + if (!saveDownloadResult(fp, metainfoCache, std::begin(results), + std::end(results), saveInProgress_, saveError_)) { + return false; + } + { // Save active downloads. const RequestGroupList& groups = rgman_->getRequestGroups(); |
