SessionSerializer.cc 6.3 KB

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