DHTAutoSaveCommand.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "DHTAutoSaveCommand.h"
  36. #include <cstring>
  37. #include <fstream>
  38. #include "DHTRoutingTable.h"
  39. #include "DHTNode.h"
  40. #include "File.h"
  41. #include "DHTRoutingTableSerializer.h"
  42. #include "RecoverableException.h"
  43. #include "DownloadEngine.h"
  44. #include "DHTBucket.h"
  45. #include "RequestGroupMan.h"
  46. #include "prefs.h"
  47. #include "Option.h"
  48. #include "message.h"
  49. #include "Logger.h"
  50. #include "LogFactory.h"
  51. #include "a2functional.h"
  52. #include "FileEntry.h"
  53. #include "DlAbortEx.h"
  54. #include "fmt.h"
  55. namespace aria2 {
  56. DHTAutoSaveCommand::DHTAutoSaveCommand
  57. (cuid_t cuid, DownloadEngine* e, int family, time_t interval)
  58. : TimeBasedCommand(cuid, e, interval),
  59. family_(family)
  60. {}
  61. DHTAutoSaveCommand::~DHTAutoSaveCommand() {}
  62. void DHTAutoSaveCommand::preProcess()
  63. {
  64. if(getDownloadEngine()->getRequestGroupMan()->downloadFinished() ||
  65. getDownloadEngine()->isHaltRequested()) {
  66. save();
  67. enableExit();
  68. }
  69. }
  70. void DHTAutoSaveCommand::process()
  71. {
  72. save();
  73. }
  74. void DHTAutoSaveCommand::save()
  75. {
  76. std::string dhtFile =
  77. getDownloadEngine()->getOption()->
  78. get(family_ == AF_INET? PREF_DHT_FILE_PATH : PREF_DHT_FILE_PATH6);
  79. A2_LOG_INFO(fmt("Saving DHT routing table to %s.", dhtFile.c_str()));
  80. File tempFile(dhtFile+"__temp");
  81. // Removing tempFile is unnecessary because the file is truncated on
  82. // open. But the bug in 1.10.4 creates directory for this path.
  83. // Because it is directory, opening directory as file fails. So we
  84. // first remove it here.
  85. tempFile.remove();
  86. File(tempFile.getDirname()).mkdirs();
  87. std::vector<SharedHandle<DHTNode> > nodes;
  88. std::vector<SharedHandle<DHTBucket> > buckets;
  89. routingTable_->getBuckets(buckets);
  90. for(std::vector<SharedHandle<DHTBucket> >::const_iterator i = buckets.begin(),
  91. eoi = buckets.end(); i != eoi; ++i) {
  92. const SharedHandle<DHTBucket>& bucket = *i;
  93. std::vector<SharedHandle<DHTNode> > goodNodes;
  94. bucket->getGoodNodes(goodNodes);
  95. nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
  96. }
  97. DHTRoutingTableSerializer serializer(family_);
  98. serializer.setLocalNode(localNode_);
  99. serializer.setNodes(nodes);
  100. try {
  101. {
  102. std::ofstream o(tempFile.getPath().c_str(),
  103. std::ios::out|std::ios::binary);
  104. if(!o) {
  105. throw DL_ABORT_EX
  106. (fmt("Failed to save DHT routing table to %s.",
  107. dhtFile.c_str()));
  108. }
  109. serializer.serialize(o);
  110. }
  111. if(!tempFile.renameTo(dhtFile)) {
  112. A2_LOG_ERROR(fmt("Cannot move file from %s to %s.",
  113. tempFile.getPath().c_str(), dhtFile.c_str()));
  114. }
  115. } catch(RecoverableException& e) {
  116. A2_LOG_ERROR_EX(fmt("Exception caught while saving DHT routing table to %s",
  117. dhtFile.c_str()),
  118. e);
  119. }
  120. }
  121. void DHTAutoSaveCommand::setLocalNode(const SharedHandle<DHTNode>& localNode)
  122. {
  123. localNode_ = localNode;
  124. }
  125. void DHTAutoSaveCommand::setRoutingTable
  126. (const SharedHandle<DHTRoutingTable>& routingTable)
  127. {
  128. routingTable_ = routingTable;
  129. }
  130. } // namespace aria2