/* */ #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" extern volatile sig_atomic_t btHaltRequested; void torrentHandler(int signal) { btHaltRequested = 1; } RequestInfos TorrentRequestInfo::execute() { { DNSCacheHandle dnsCache = new NullDNSCache(); DNSCacheSingletonHolder::instance(dnsCache); } BtContextHandle btContext(new DefaultBtContext()); btContext->load(torrentFile); if(op->get(PREF_SHOW_FILES) == V_TRUE) { showFileEntry(btContext); 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()->openExistingFile(); #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()->openExistingFile(); #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()->initAndOpenFile(); } } 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("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(); } void TorrentRequestInfo::showFileEntry(const BtContextHandle& btContext) { FileEntries fileEntries = btContext->getFileEntries(); cout << _("Files:") << endl; cout << "idx|path/length" << endl; cout << "===+===========================================================================" << endl; int count = 1; for(FileEntries::const_iterator itr = fileEntries.begin(); itr != fileEntries.end(); count++, itr++) { printf("%3d|%s\n |%s Bytes\n", count, (*itr)->getPath().c_str(), Util::llitos((*itr)->getLength(), true).c_str()); cout << "---+---------------------------------------------------------------------------" << endl; } }