RequestGroup.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "RequestGroup.h"
  36. #include "DownloadEngine.h"
  37. #include "prefs.h"
  38. #include "DefaultDiskWriter.h"
  39. #include "RequestFactory.h"
  40. #include "InitiateConnectionCommandFactory.h"
  41. #include "CUIDCounter.h"
  42. #include "DlAbortEx.h"
  43. #include "File.h"
  44. #include "message.h"
  45. #include "DlAbortEx.h"
  46. #include "Util.h"
  47. #include "FatalException.h"
  48. #include "DownloadCommand.h"
  49. #ifdef ENABLE_MESSAGE_DIGEST
  50. #include "CheckIntegrityCommand.h"
  51. #include "CheckIntegrityEntry.h"
  52. #endif // ENABLE_MESSAGE_DIGEST
  53. #include <cerrno>
  54. SegmentManHandle RequestGroup::initSegmentMan()
  55. {
  56. _segmentMan = _segmentManFactory->createNewInstance();
  57. _segmentMan->ufilename = _ufilename;
  58. if(!_topDir.empty() && _topDir != ".") {
  59. _segmentMan->dir += "/"+_topDir;
  60. }
  61. return _segmentMan;
  62. }
  63. Commands RequestGroup::createNextCommandWithAdj(DownloadEngine* e, int32_t numAdj)
  64. {
  65. int32_t numCommand = _numConcurrentCommand == 0 ? _uris.size() : _numConcurrentCommand+numAdj;
  66. return createNextCommand(e, numCommand, "GET");
  67. }
  68. Commands RequestGroup::createNextCommand(DownloadEngine* e, int32_t numCommand, const string& method)
  69. {
  70. Commands commands;
  71. for(;!_uris.empty() && commands.size() < (size_t)numCommand; _uris.pop_front()) {
  72. string uri = _uris.front();
  73. _spentUris.push_back(uri);
  74. RequestHandle req = RequestFactorySingletonHolder::instance()->createRequest();
  75. req->setReferer(_option->get(PREF_REFERER));
  76. req->setMethod(method);
  77. if(req->setUrl(uri)) {
  78. commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), req, this, e));
  79. } else {
  80. logger->error(MSG_UNRECOGNIZED_URI,
  81. req->getUrl().c_str());
  82. }
  83. }
  84. return commands;
  85. }
  86. void RequestGroup::initBitfield()
  87. {
  88. _segmentMan->initBitfield(_option->getAsInt(PREF_SEGMENT_SIZE),
  89. _segmentMan->totalSize);
  90. }
  91. void RequestGroup::openExistingFile()
  92. {
  93. _segmentMan->diskWriter->openExistingFile(_segmentMan->getFilePath());
  94. }
  95. void RequestGroup::markAllPiecesDone()
  96. {
  97. _segmentMan->markAllPiecesDone();
  98. }
  99. void RequestGroup::markExistingPiecesDone()
  100. {
  101. _segmentMan->markPieceDone(File(_segmentMan->getFilePath()).size());
  102. }
  103. void RequestGroup::markPieceDone(int64_t length)
  104. {
  105. _segmentMan->markPieceDone(length);
  106. }
  107. void RequestGroup::shouldCancelDownloadForSafety()
  108. {
  109. if(_segmentMan->shouldCancelDownloadForSafety()) {
  110. logger->notice(MSG_FILE_ALREADY_EXISTS,
  111. _segmentMan->getFilePath().c_str(),
  112. _segmentMan->getSegmentFilePath().c_str());
  113. throw new FatalException(EX_DOWNLOAD_ABORTED);
  114. }
  115. }
  116. void RequestGroup::initAndOpenFile()
  117. {
  118. File d(getDir());
  119. if(d.isDir()) {
  120. } else if(d.exists()) {
  121. throw new FatalException(EX_MAKE_DIR, getDir().c_str(), MSG_NOT_DIRECTORY);
  122. } else if(!d.mkdirs()) {
  123. throw new FatalException(EX_MAKE_DIR, getDir().c_str(), strerror(errno));
  124. }
  125. _segmentMan->diskWriter->initAndOpenFile(_segmentMan->getFilePath());
  126. }
  127. bool RequestGroup::needsFileAllocation() const
  128. {
  129. return _option->get(PREF_FILE_ALLOCATION) == V_PREALLOC
  130. && File(_segmentMan->getFilePath()).size() < _segmentMan->totalSize;
  131. }
  132. bool RequestGroup::fileExists() const
  133. {
  134. return _segmentMan->fileExists();
  135. }
  136. bool RequestGroup::segmentFileExists() const
  137. {
  138. return _segmentMan->segmentFileExists();
  139. }
  140. string RequestGroup::getFilePath() const
  141. {
  142. if(_segmentMan.isNull()) {
  143. return "";
  144. } else {
  145. return _segmentMan->getFilePath();
  146. }
  147. }
  148. string RequestGroup::getDir() const
  149. {
  150. if(_segmentMan.isNull()) {
  151. return "";
  152. } else {
  153. return _segmentMan->dir;
  154. }
  155. }
  156. void RequestGroup::loadAndOpenFile()
  157. {
  158. bool segFileExists = segmentFileExists();
  159. if(segFileExists) {
  160. load();
  161. openExistingFile();
  162. } else if(fileExists() && _option->get(PREF_CONTINUE) == V_TRUE) {
  163. File existingFile(getFilePath());
  164. if(getTotalLength() < existingFile.size()) {
  165. throw new DlAbortEx(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE,
  166. getFilePath().c_str(),
  167. Util::llitos(existingFile.size()).c_str(),
  168. Util::llitos(getTotalLength()).c_str());
  169. }
  170. initBitfield();
  171. openExistingFile();
  172. _segmentMan->markPieceDone(existingFile.size());
  173. } else {
  174. shouldCancelDownloadForSafety();
  175. initBitfield();
  176. #ifdef ENABLE_MESSAGE_DIGEST
  177. if(fileExists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
  178. openExistingFile();
  179. } else {
  180. initAndOpenFile();
  181. }
  182. #else // ENABLE_MESSAGE_DIGEST
  183. initAndOpenFile();
  184. #endif // ENABLE_MESSAGE_DIGEST
  185. }
  186. }
  187. bool RequestGroup::downloadFinishedByFileLength()
  188. {
  189. if(_segmentMan->segmentFileExists()) {
  190. return false;
  191. }
  192. File existingFile(getFilePath());
  193. if(existingFile.exists() &&
  194. getTotalLength() == existingFile.size()) {
  195. _segmentMan->downloadStarted = true;
  196. initBitfield();
  197. _segmentMan->markAllPiecesDone();
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. void RequestGroup::prepareForNextAction(int cuid, const RequestHandle& req, DownloadEngine* e, DownloadCommand* downloadCommand)
  204. {
  205. File existingFile(getFilePath());
  206. #ifdef ENABLE_MESSAGE_DIGEST
  207. if(existingFile.size() > 0 && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
  208. // purge SegmentEntries
  209. _segmentMan->purgeSegmentEntry();
  210. CheckIntegrityEntryHandle entry = new CheckIntegrityEntry(cuid, req, this, downloadCommand);
  211. entry->initValidator();
  212. CheckIntegrityCommand* command = new CheckIntegrityCommand(cuid, this, e, entry);
  213. e->commands.push_back(command);
  214. } else
  215. #endif // ENABLE_MESSAGE_DIGEST
  216. if(needsFileAllocation()) {
  217. FileAllocationEntryHandle entry = new FileAllocationEntry(cuid, req, this, downloadCommand, existingFile.size());
  218. e->_fileAllocationMan->pushFileAllocationEntry(entry);
  219. } else {
  220. if(downloadCommand) {
  221. e->commands.push_back(downloadCommand);
  222. } else {
  223. Commands commands = createNextCommandWithAdj(e, -1);
  224. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, this, e);
  225. commands.push_front(command);
  226. e->addCommand(commands);
  227. }
  228. }
  229. }
  230. void RequestGroup::validateFilename(const string& expectedFilename,
  231. const string& actualFilename) const
  232. {
  233. if(expectedFilename.empty()) {
  234. return;
  235. }
  236. if(expectedFilename != actualFilename) {
  237. throw new DlAbortEx(EX_FILENAME_MISMATCH,
  238. expectedFilename.c_str(),
  239. actualFilename.c_str());
  240. }
  241. }
  242. void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
  243. int64_t actualTotalLength) const
  244. {
  245. if(expectedTotalLength <= 0) {
  246. return;
  247. }
  248. if(expectedTotalLength != actualTotalLength) {
  249. throw new DlAbortEx(EX_SIZE_MISMATCH,
  250. expectedTotalLength,
  251. actualTotalLength);
  252. }
  253. }
  254. void RequestGroup::validateFilename(const string& actualFilename) const
  255. {
  256. validateFilename(_segmentMan->filename, actualFilename);
  257. }
  258. void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
  259. {
  260. validateTotalLength(_segmentMan->totalSize, actualTotalLength);
  261. }
  262. void RequestGroup::validateFilenameByHint(const string& actualFilename) const
  263. {
  264. validateFilename(_hintFilename, actualFilename);
  265. }
  266. void RequestGroup::validateTotalLengthByHint(int64_t actualTotalLength) const
  267. {
  268. validateTotalLength(_hintTotalLength, actualTotalLength);
  269. }
  270. void RequestGroup::setUserDefinedFilename(const string& filename)
  271. {
  272. _ufilename = filename;
  273. }
  274. int64_t RequestGroup::getExistingFileLength() const
  275. {
  276. return File(getFilePath()).size();
  277. }