MultiUrlRequestInfo.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "MultiUrlRequestInfo.h"
  36. #include <signal.h>
  37. #include <ostream>
  38. #include "RequestGroupMan.h"
  39. #include "DownloadEngine.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. #include "RequestGroup.h"
  43. #include "prefs.h"
  44. #include "DownloadEngineFactory.h"
  45. #include "RecoverableException.h"
  46. #include "message.h"
  47. #include "util.h"
  48. #include "Option.h"
  49. #include "StatCalc.h"
  50. #include "CookieStorage.h"
  51. #include "File.h"
  52. #include "Netrc.h"
  53. #include "AuthConfigFactory.h"
  54. #include "DownloadContext.h"
  55. #include "SessionSerializer.h"
  56. #include "ServerStatMan.h"
  57. #include "FileAllocationEntry.h"
  58. #include "CheckIntegrityEntry.h"
  59. #ifdef ENABLE_SSL
  60. # include "SocketCore.h"
  61. # include "TLSContext.h"
  62. #endif // ENABLE_SSL
  63. namespace aria2 {
  64. #ifndef SA_RESETHAND
  65. # define SA_RESETHAND 0x80000000
  66. #endif // SA_RESETHAND
  67. namespace global {
  68. extern volatile sig_atomic_t globalHaltRequested;
  69. } // namespace global
  70. static void handler(int signal) {
  71. if(
  72. #ifdef SIGHUP
  73. signal == SIGHUP ||
  74. #endif // SIGHUP
  75. signal == SIGTERM) {
  76. if(global::globalHaltRequested == 0 || global::globalHaltRequested == 2) {
  77. global::globalHaltRequested = 3;
  78. }
  79. } else {
  80. if(global::globalHaltRequested == 0) {
  81. global::globalHaltRequested = 1;
  82. } else if(global::globalHaltRequested == 2) {
  83. global::globalHaltRequested = 3;
  84. }
  85. }
  86. }
  87. MultiUrlRequestInfo::MultiUrlRequestInfo
  88. (const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  89. const SharedHandle<Option>& op,
  90. const SharedHandle<StatCalc>& statCalc,
  91. std::ostream& summaryOut)
  92. :
  93. requestGroups_(requestGroups),
  94. option_(op),
  95. statCalc_(statCalc),
  96. summaryOut_(summaryOut),
  97. logger_(LogFactory::getInstance())
  98. {}
  99. MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
  100. void MultiUrlRequestInfo::printMessageForContinue()
  101. {
  102. summaryOut_ << "\n"
  103. << _("aria2 will resume download if the transfer is restarted.")
  104. << "\n"
  105. << _("If there are any errors, then see the log file. See '-l' option in help/man page for details.")
  106. << "\n";
  107. }
  108. downloadresultcode::RESULT MultiUrlRequestInfo::execute()
  109. {
  110. downloadresultcode::RESULT returnValue = downloadresultcode::FINISHED;
  111. try {
  112. DownloadEngineHandle e =
  113. DownloadEngineFactory().newDownloadEngine(option_.get(), requestGroups_);
  114. if(!option_->blank(PREF_LOAD_COOKIES)) {
  115. File cookieFile(option_->get(PREF_LOAD_COOKIES));
  116. if(cookieFile.isFile()) {
  117. e->getCookieStorage()->load(cookieFile.getPath());
  118. logger_->info("Loaded cookies from '%s'.",
  119. cookieFile.getPath().c_str());
  120. } else {
  121. logger_->error(MSG_LOADING_COOKIE_FAILED, cookieFile.getPath().c_str());
  122. }
  123. }
  124. SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory());
  125. File netrccf(option_->get(PREF_NETRC_PATH));
  126. if(!option_->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  127. mode_t mode = netrccf.mode();
  128. if(mode&(S_IRWXG|S_IRWXO)) {
  129. logger_->notice(MSG_INCORRECT_NETRC_PERMISSION,
  130. option_->get(PREF_NETRC_PATH).c_str());
  131. } else {
  132. SharedHandle<Netrc> netrc(new Netrc());
  133. netrc->parse(option_->get(PREF_NETRC_PATH));
  134. authConfigFactory->setNetrc(netrc);
  135. }
  136. }
  137. e->setAuthConfigFactory(authConfigFactory);
  138. #ifdef ENABLE_SSL
  139. SharedHandle<TLSContext> tlsContext(new TLSContext());
  140. if(!option_->blank(PREF_CERTIFICATE) &&
  141. !option_->blank(PREF_PRIVATE_KEY)) {
  142. tlsContext->addClientKeyFile(option_->get(PREF_CERTIFICATE),
  143. option_->get(PREF_PRIVATE_KEY));
  144. }
  145. if(!option_->blank(PREF_CA_CERTIFICATE)) {
  146. if(!tlsContext->addTrustedCACertFile(option_->get(PREF_CA_CERTIFICATE))) {
  147. logger_->warn(MSG_WARN_NO_CA_CERT);
  148. }
  149. } else if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
  150. logger_->warn(MSG_WARN_NO_CA_CERT);
  151. }
  152. if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
  153. tlsContext->enablePeerVerification();
  154. }
  155. SocketCore::setTLSContext(tlsContext);
  156. #endif
  157. if(!Timer::monotonicClock()) {
  158. logger_->warn("Don't change system time while aria2c is running."
  159. " Doing this may make aria2c hang for long time.");
  160. }
  161. std::string serverStatIf = option_->get(PREF_SERVER_STAT_IF);
  162. if(!serverStatIf.empty()) {
  163. e->getRequestGroupMan()->loadServerStat(serverStatIf);
  164. e->getRequestGroupMan()->removeStaleServerStat
  165. (option_->getAsInt(PREF_SERVER_STAT_TIMEOUT));
  166. }
  167. e->setStatCalc(statCalc_);
  168. #ifdef SIGHUP
  169. util::setGlobalSignalHandler(SIGHUP, handler, 0);
  170. #endif // SIGHUP
  171. util::setGlobalSignalHandler(SIGINT, handler, 0);
  172. util::setGlobalSignalHandler(SIGTERM, handler, 0);
  173. e->run();
  174. if(!option_->blank(PREF_SAVE_COOKIES)) {
  175. e->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
  176. }
  177. std::string serverStatOf = option_->get(PREF_SERVER_STAT_OF);
  178. if(!serverStatOf.empty()) {
  179. e->getRequestGroupMan()->saveServerStat(serverStatOf);
  180. }
  181. e->getRequestGroupMan()->showDownloadResults(summaryOut_);
  182. summaryOut_ << std::flush;
  183. RequestGroupMan::DownloadStat s =
  184. e->getRequestGroupMan()->getDownloadStat();
  185. if(!s.allCompleted()) {
  186. printMessageForContinue();
  187. if(s.getLastErrorResult() == downloadresultcode::FINISHED &&
  188. s.getInProgress() > 0) {
  189. returnValue = downloadresultcode::IN_PROGRESS;
  190. } else {
  191. returnValue = s.getLastErrorResult();
  192. }
  193. }
  194. SessionSerializer sessionSerializer(e->getRequestGroupMan());
  195. // TODO Add option: --save-session-status=error,inprogress,waiting
  196. if(!option_->blank(PREF_SAVE_SESSION)) {
  197. const std::string& filename = option_->get(PREF_SAVE_SESSION);
  198. if(sessionSerializer.save(filename)) {
  199. logger_->notice("Serialized session to '%s' successfully.",
  200. filename.c_str());
  201. } else {
  202. logger_->notice("Failed to serialize session to '%s'.",
  203. filename.c_str());
  204. }
  205. }
  206. } catch(RecoverableException& e) {
  207. if(returnValue == downloadresultcode::FINISHED) {
  208. returnValue = downloadresultcode::UNKNOWN_ERROR;
  209. }
  210. logger_->error(EX_EXCEPTION_CAUGHT, e);
  211. }
  212. #ifdef SIGHUP
  213. util::setGlobalSignalHandler(SIGHUP, SIG_DFL, 0);
  214. #endif // SIGHUP
  215. util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
  216. util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
  217. return returnValue;
  218. }
  219. } // namespace aria2