SessionSerializer.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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. #include "SessionSerializer.h"
  36. #include <fstream>
  37. #include <iterator>
  38. #include "RequestGroupMan.h"
  39. #include "a2functional.h"
  40. #include "File.h"
  41. #include "A2STR.h"
  42. #include "download_helper.h"
  43. #include "Option.h"
  44. #include "DownloadResult.h"
  45. #include "FileEntry.h"
  46. #include "prefs.h"
  47. #include "util.h"
  48. #include "array_fun.h"
  49. namespace aria2 {
  50. SessionSerializer::SessionSerializer
  51. (const SharedHandle<RequestGroupMan>& requestGroupMan):
  52. rgman_(requestGroupMan),
  53. saveError_(true),
  54. saveInProgress_(true),
  55. saveWaiting_(true) {}
  56. bool SessionSerializer::save(const std::string& filename) const
  57. {
  58. std::string tempFilename = strconcat(filename, "__temp");
  59. {
  60. std::ofstream out(tempFilename.c_str(), std::ios::binary);
  61. if(!out) {
  62. return false;
  63. }
  64. save(out);
  65. out.flush();
  66. if(!out) {
  67. return false;
  68. }
  69. }
  70. return File(tempFilename).renameTo(filename);
  71. }
  72. namespace {
  73. const std::vector<std::string>& getCumulativeOpts()
  74. {
  75. static std::string cumulativeOpts[] = { PREF_INDEX_OUT, PREF_HEADER };
  76. static std::vector<std::string> opts
  77. (vbegin(cumulativeOpts), vend(cumulativeOpts));
  78. return opts;
  79. }
  80. } // namespace
  81. namespace {
  82. bool inCumulativeOpts(const std::string& opt)
  83. {
  84. const std::vector<std::string>& cumopts = getCumulativeOpts();
  85. for(std::vector<std::string>::const_iterator itr = cumopts.begin(),
  86. eoi = cumopts.end(); itr != eoi; ++itr) {
  87. if(opt == *itr) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. } // namespace
  94. namespace {
  95. void writeOption(std::ostream& out, const SharedHandle<Option>& op)
  96. {
  97. const std::set<std::string>& requestOptions = listRequestOptions();
  98. for(std::set<std::string>::const_iterator itr = requestOptions.begin(),
  99. eoi = requestOptions.end(); itr != eoi; ++itr) {
  100. if(inCumulativeOpts(*itr)) {
  101. continue;
  102. }
  103. if(op->defined(*itr)) {
  104. out << " " << *itr << "=" << op->get(*itr) << "\n";
  105. }
  106. }
  107. const std::vector<std::string>& cumopts = getCumulativeOpts();
  108. for(std::vector<std::string>::const_iterator opitr = cumopts.begin(),
  109. eoi = cumopts.end(); opitr != eoi; ++opitr) {
  110. if(op->defined(*opitr)) {
  111. std::vector<std::string> v;
  112. util::split(op->get(*opitr), std::back_inserter(v), "\n",
  113. false, false);
  114. for(std::vector<std::string>::const_iterator i = v.begin(), eoi = v.end();
  115. i != eoi; ++i) {
  116. out << " " << *opitr << "=" << *i << "\n";
  117. }
  118. }
  119. }
  120. }
  121. } // namespace
  122. namespace {
  123. void writeDownloadResult
  124. (std::ostream& out, std::set<int64_t>& metainfoCache,
  125. const SharedHandle<DownloadResult>& dr)
  126. {
  127. const SharedHandle<MetadataInfo>& mi = dr->metadataInfo;
  128. if(dr->belongsTo != 0 || (mi && mi->dataOnly())) {
  129. return;
  130. }
  131. if(!mi) {
  132. // only save first file entry
  133. if(dr->fileEntries.empty()) {
  134. return;
  135. }
  136. const SharedHandle<FileEntry>& file = dr->fileEntries[0];
  137. std::vector<std::string> uris;
  138. file->getUris(uris);
  139. if(uris.empty()) {
  140. return;
  141. }
  142. std::copy(uris.begin(), uris.end(),
  143. std::ostream_iterator<std::string>(out, "\t"));
  144. out << "\n";
  145. } else {
  146. if(metainfoCache.count(mi->getId()) != 0) {
  147. return;
  148. } else {
  149. metainfoCache.insert(mi->getId());
  150. out << mi->getUri() << "\n";
  151. }
  152. }
  153. writeOption(out, dr->option);
  154. }
  155. } // namespace
  156. void SessionSerializer::save(std::ostream& out) const
  157. {
  158. std::set<int64_t> metainfoCache;
  159. const std::deque<SharedHandle<DownloadResult> >& results =
  160. rgman_->getDownloadResults();
  161. for(std::deque<SharedHandle<DownloadResult> >::const_iterator itr =
  162. results.begin(), eoi = results.end(); itr != eoi; ++itr) {
  163. if((*itr)->result == downloadresultcode::FINISHED) {
  164. continue;
  165. } else if((*itr)->result == downloadresultcode::IN_PROGRESS) {
  166. if(saveInProgress_) {
  167. writeDownloadResult(out, metainfoCache, *itr);
  168. }
  169. } else {
  170. // error download
  171. if(saveError_) {
  172. writeDownloadResult(out, metainfoCache, *itr);
  173. }
  174. }
  175. }
  176. if(saveInProgress_) {
  177. const std::deque<SharedHandle<RequestGroup> >& groups =
  178. rgman_->getRequestGroups();
  179. for(std::deque<SharedHandle<RequestGroup> >::const_iterator itr =
  180. groups.begin(), eoi = groups.end(); itr != eoi; ++itr) {
  181. SharedHandle<DownloadResult> result = (*itr)->createDownloadResult();
  182. if(result->result == downloadresultcode::FINISHED) {
  183. continue;
  184. }
  185. writeDownloadResult(out, metainfoCache, result);
  186. }
  187. }
  188. if(saveWaiting_) {
  189. const std::deque<SharedHandle<RequestGroup> >& groups =
  190. rgman_->getReservedGroups();
  191. for(std::deque<SharedHandle<RequestGroup> >::const_iterator itr =
  192. groups.begin(), eoi = groups.end(); itr != eoi; ++itr) {
  193. SharedHandle<DownloadResult> result = (*itr)->createDownloadResult();
  194. writeDownloadResult(out, metainfoCache, result);
  195. }
  196. }
  197. }
  198. } // namespace aria2