/* */ #include "TorrentRequestInfo.h" #include "DownloadEngineFactory.h" #include "prefs.h" #include "Util.h" #include "BtRegistry.h" #include "DefaultBtContext.h" #include "FatalException.h" #include "message.h" #include "RecoverableException.h" #include "DNSCache.h" #include #ifndef SA_RESETHAND # define SA_RESETHAND 0x80000000 #endif // SA_RESETHAND extern volatile sig_atomic_t btHaltRequested; void torrentHandler(int signal) { btHaltRequested = 1; } RequestInfos TorrentRequestInfo::execute() { { DNSCacheHandle dnsCache = new NullDNSCache(); DNSCacheSingletonHolder::instance(dnsCache); } DefaultBtContextHandle btContext = new DefaultBtContext(); btContext->load(torrentFile); if(op->defined(PREF_PEER_ID_PREFIX)) { btContext->setPeerIdPrefix(op->get(PREF_PEER_ID_PREFIX)); } if(op->get(PREF_SHOW_FILES) == V_TRUE) { Util::toStream(cout, btContext->getFileEntries()); return RequestInfos(); } // set max_tries to 1. AnnounceList handles retries. op->put(PREF_MAX_TRIES, "1"); SharedHandle e(DownloadEngineFactory::newTorrentConsoleEngine(btContext, op, targetFiles)); if(BT_PROGRESS_INFO_FILE(btContext)->exists()) { // load .aria2 file if it exists. BT_PROGRESS_INFO_FILE(btContext)->load(); PIECE_STORAGE(btContext)->getDiskAdaptor()->openFile(); #ifdef ENABLE_MESSAGE_DIGEST if(op->get(PREF_CHECK_INTEGRITY) == V_TRUE) { PIECE_STORAGE(btContext)->checkIntegrity(); } #endif // ENABLE_MESSAGE_DIGEST } else { if(PIECE_STORAGE(btContext)->getDiskAdaptor()->fileExists()) { if(op->get(PREF_ALLOW_OVERWRITE) != V_TRUE) { logger->notice(MSG_FILE_ALREADY_EXISTS, PIECE_STORAGE(btContext)->getDiskAdaptor()->getFilePath().c_str(), BT_PROGRESS_INFO_FILE(btContext)->getFilename().c_str()); throw new FatalException(EX_DOWNLOAD_ABORTED); } else { PIECE_STORAGE(btContext)->getDiskAdaptor()->openFile(); #ifdef ENABLE_MESSAGE_DIGEST if(op->get(PREF_CHECK_INTEGRITY) == V_TRUE) { PIECE_STORAGE(btContext)->markAllPiecesDone(); PIECE_STORAGE(btContext)->checkIntegrity(); } #endif // ENABLE_MESSAGE_DIGEST } } else { PIECE_STORAGE(btContext)->getDiskAdaptor()->openFile(); } } Util::setGlobalSignalHandler(SIGINT, torrentHandler, SA_RESETHAND); Util::setGlobalSignalHandler(SIGTERM, torrentHandler, SA_RESETHAND); try { e->run(); if(PIECE_STORAGE(btContext)->downloadFinished()) { printDownloadCompeleteMessage(); } } catch(RecoverableException* ex) { logger->error(EX_EXCEPTION_CAUGHT, ex); fail = true; delete ex; } // TODO we want just 1 torrent download to clear BtRegistry::clear(); Util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0); Util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0); return RequestInfos(); }