DefaultBtProgressInfoFile.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "DefaultBtProgressInfoFile.h"
  36. #include "BtRegistry.h"
  37. #include "LogFactory.h"
  38. #include "prefs.h"
  39. #include "DlAbortEx.h"
  40. #include "message.h"
  41. #include "File.h"
  42. #include "Util.h"
  43. #include <errno.h>
  44. DefaultBtProgressInfoFile::DefaultBtProgressInfoFile(const BtContextHandle& btContext,
  45. const Option* option):
  46. btContext(btContext),
  47. option(option),
  48. pieceStorage(PIECE_STORAGE(btContext)),
  49. btRuntime(BT_RUNTIME(btContext)),
  50. peerStorage(PEER_STORAGE(btContext))
  51. {
  52. logger = LogFactory::getInstance();
  53. string storeDir = option->get(PREF_DIR);
  54. filename = storeDir+"/"+btContext->getName()+".aria2";
  55. }
  56. DefaultBtProgressInfoFile::~DefaultBtProgressInfoFile() {}
  57. void DefaultBtProgressInfoFile::save() {
  58. logger->info(MSG_SAVING_SEGMENT_FILE, filename.c_str());
  59. FILE* file = openFile(filename, "w");
  60. try {
  61. if(fwrite(btContext->getInfoHash(),
  62. btContext->getInfoHashLength(), 1, file) < 1) {
  63. throw string("writeError:info hash");
  64. }
  65. if(fwrite(pieceStorage->getBitfield(),
  66. pieceStorage->getBitfieldLength(), 1, file) < 1) {
  67. throw string("writeError:bitfield");
  68. }
  69. TransferStat stat = peerStorage->calculateStat();
  70. long long int allTimeDownloadLength = pieceStorage->getCompletedLength();
  71. if(fwrite(&allTimeDownloadLength,
  72. sizeof(allTimeDownloadLength), 1, file) < 1) {
  73. throw string("writeError:download length");
  74. }
  75. long long int allTimeUploadLength =
  76. btRuntime->getUploadLengthAtStartup()+
  77. stat.getSessionUploadLength();
  78. if(fwrite(&allTimeUploadLength,
  79. sizeof(allTimeUploadLength), 1, file) < 1) {
  80. throw string("writeError:upload length");
  81. }
  82. fclose(file);
  83. logger->info(MSG_SAVED_SEGMENT_FILE);
  84. } catch(string ex) {
  85. fclose(file);
  86. throw new DlAbortEx(EX_SEGMENT_FILE_WRITE,
  87. filename.c_str(), strerror(errno));
  88. }
  89. }
  90. void DefaultBtProgressInfoFile::load() {
  91. logger->info(MSG_LOADING_SEGMENT_FILE, filename.c_str());
  92. FILE* file = openFile(filename, "r+");
  93. unsigned char* savedInfoHash = 0;
  94. unsigned char* savedBitfield = 0;
  95. try {
  96. savedInfoHash = new unsigned char[btContext->getInfoHashLength()];
  97. savedBitfield = new unsigned char[pieceStorage->getBitfieldLength()];
  98. if(fread(savedInfoHash, btContext->getInfoHashLength(), 1, file) < 1) {
  99. throw string("readError");
  100. }
  101. if(Util::toHex(savedInfoHash, btContext->getInfoHashLength()) !=
  102. btContext->getInfoHashAsString()) {
  103. throw string("infoHashMismatch");
  104. }
  105. if(fread(savedBitfield, pieceStorage->getBitfieldLength(), 1, file) < 1) {
  106. throw string("readError");
  107. }
  108. pieceStorage->setBitfield(savedBitfield,
  109. pieceStorage->getBitfieldLength());
  110. // allTimeDownloadLength exists for only a compatibility reason.
  111. long long int allTimeDownloadLength;
  112. if(fread(&allTimeDownloadLength,
  113. sizeof(allTimeDownloadLength), 1, file) < 1) {
  114. throw string("readError");
  115. }
  116. long long int allTimeUploadLength;
  117. if(fread(&allTimeUploadLength,
  118. sizeof(allTimeUploadLength), 1, file) < 1) {
  119. throw string("readError");
  120. }
  121. btRuntime->setUploadLengthAtStartup(allTimeUploadLength);
  122. delete [] savedBitfield;
  123. savedBitfield = 0;
  124. delete [] savedInfoHash;
  125. savedInfoHash = 0;
  126. fclose(file);
  127. } catch(string ex) {
  128. if(savedBitfield) {
  129. delete [] savedBitfield;
  130. }
  131. if(savedInfoHash) {
  132. delete [] savedInfoHash;
  133. }
  134. fclose(file);
  135. if(ex == "infoHashMismatch") {
  136. throw new DlAbortEx("The infoHash in torrent file doesn't match to one in .aria2 file.");
  137. } else {
  138. throw new DlAbortEx(EX_SEGMENT_FILE_READ,
  139. filename.c_str(), strerror(errno));
  140. }
  141. }
  142. logger->info(MSG_LOADED_SEGMENT_FILE);
  143. }
  144. void DefaultBtProgressInfoFile::removeFile() {
  145. if(exists()) {
  146. File f(filename);
  147. f.remove();
  148. }
  149. }
  150. FILE* DefaultBtProgressInfoFile::openFile(const string& filename,
  151. const string& mode) const
  152. {
  153. FILE* file = fopen(filename.c_str(), mode.c_str());
  154. if(!file) {
  155. throw new DlAbortEx(EX_SEGMENT_FILE_OPEN,
  156. filename.c_str(), strerror(errno));
  157. }
  158. return file;
  159. }
  160. bool DefaultBtProgressInfoFile::exists() {
  161. File f(filename);
  162. if(f.isFile()) {
  163. logger->info(MSG_SEGMENT_FILE_EXISTS, filename.c_str());
  164. return true;
  165. } else {
  166. logger->info(MSG_SEGMENT_FILE_DOES_NOT_EXIST, filename.c_str());
  167. return false;
  168. }
  169. }