/* */ #include "common.h" #include #include #include #include #include #include #include #include "SharedHandle.h" #include "LogFactory.h" #include "Logger.h" #include "Util.h" #include "BitfieldManFactory.h" #include "FeatureConfig.h" #include "MultiUrlRequestInfo.h" #include "SimpleRandomizer.h" #include "File.h" #include "message.h" #include "prefs.h" #include "Option.h" #include "a2algo.h" #include "a2io.h" #include "a2time.h" #include "Platform.h" #include "DefaultBtContext.h" #include "FileEntry.h" #include "RequestGroup.h" #include "ConsoleStatCalc.h" #include "NullStatCalc.h" #include "download_helper.h" #include "Exception.h" #include "ProtocolDetector.h" #include "RecoverableException.h" #ifdef ENABLE_METALINK # include "MetalinkHelper.h" # include "MetalinkEntry.h" #endif // ENABLE_METALINK #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigestHelper.h" #endif // ENABLE_MESSAGE_DIGEST extern char* optarg; extern int optind, opterr, optopt; namespace aria2 { // output stream to /dev/null std::ofstream nullout(DEV_NULL); SharedHandle getStatCalc(const Option* op) { SharedHandle statCalc; if(op->getAsBool(PREF_QUIET)) { statCalc.reset(new NullStatCalc()); } else { statCalc.reset(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL))); } return statCalc; } std::ostream& getSummaryOut(const Option* op) { if(op->getAsBool(PREF_QUIET)) { return nullout; } else { return std::cout; } } #ifdef ENABLE_BITTORRENT static void showTorrentFile(const std::string& uri) { SharedHandle btContext(new DefaultBtContext()); btContext->load(uri); std::cout << btContext << std::endl; } #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK static void showMetalinkFile(const std::string& uri, const Option* op) { std::deque > metalinkEntries; MetalinkHelper::parseAndQuery(metalinkEntries, uri, op); std::deque > fileEntries; MetalinkEntry::toFileEntry(fileEntries, metalinkEntries); Util::toStream(std::cout, fileEntries); std::cout << std::endl; } #endif // ENABLE_METALINK #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK static void showFiles(const std::deque& uris, const Option* op) { ProtocolDetector dt; for(std::deque::const_iterator i = uris.begin(); i != uris.end(); ++i) { printf(">>> "); printf(MSG_SHOW_FILES, (*i).c_str()); printf("\n"); try { #ifdef ENABLE_BITTORRENT if(dt.guessTorrentFile(*i)) { showTorrentFile(*i); } else #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK if(dt.guessMetalinkFile(*i)) { showMetalinkFile(*i, op); } else #endif // ENABLE_METALINK { printf(MSG_NOT_TORRENT_METALINK); printf("\n\n"); } } catch(RecoverableException& e) { std::cout << e.stackTrace() << std::endl; } } } #endif // ENABLE_BITTORRENT || ENABLE_METALINK extern Option* option_processing(int argc, char* const argv[]); DownloadResult::RESULT main(int argc, char* argv[]) { Option* op = option_processing(argc, argv); std::deque args(argv+optind, argv+argc); SimpleRandomizer::init(); BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance()); if(op->get(PREF_LOG) == "-") { LogFactory::setLogFile(DEV_STDOUT); } else if(!op->get(PREF_LOG).empty()) { LogFactory::setLogFile(op->get(PREF_LOG)); } else { LogFactory::setLogFile(DEV_NULL); } LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL)); if(op->getAsBool(PREF_QUIET)) { LogFactory::setConsoleOutput(false); } DownloadResult::RESULT exitStatus = DownloadResult::FINISHED; try { Logger* logger = LogFactory::getInstance(); logger->info("<<--- --- --- ---"); logger->info(" --- --- --- ---"); logger->info(" --- --- --- --->>"); logger->info("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET); logger->info(MSG_LOGGING_STARTED); #ifdef ENABLE_MESSAGE_DIGEST MessageDigestHelper::staticSHA1DigestInit(); #endif // ENABLE_MESSAGE_DIGEST #ifdef SIGPIPE Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0); #endif std::deque > requestGroups; #ifdef ENABLE_BITTORRENT if(!op->blank(PREF_TORRENT_FILE)) { if(op->get(PREF_SHOW_FILES) == V_TRUE) { showTorrentFile(op->get(PREF_TORRENT_FILE)); return exitStatus; } else { createRequestGroupForBitTorrent(requestGroups, op, args); } } else #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK if(!op->blank(PREF_METALINK_FILE)) { if(op->get(PREF_SHOW_FILES) == V_TRUE) { showMetalinkFile(op->get(PREF_METALINK_FILE), op); return exitStatus; } else { createRequestGroupForMetalink(requestGroups, op); } } else #endif // ENABLE_METALINK if(!op->blank(PREF_INPUT_FILE)) { createRequestGroupForUriList(requestGroups, op); #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK } else if(op->get(PREF_SHOW_FILES) == V_TRUE) { showFiles(args, op); #endif // ENABLE_METALINK || ENABLE_METALINK } else { createRequestGroupForUri(requestGroups, op, args); } if(requestGroups.empty()) { std::cout << MSG_NO_FILES_TO_DOWNLOAD << std::endl; } else { exitStatus = MultiUrlRequestInfo(requestGroups, op, getStatCalc(op), getSummaryOut(op)).execute(); } } catch(Exception& ex) { std::cerr << EX_EXCEPTION_CAUGHT << "\n" << ex.stackTrace() << std::endl; exitStatus = DownloadResult::UNKNOWN_ERROR; } delete op; LogFactory::release(); return exitStatus; } } // namespace aria2 int main(int argc, char* argv[]) { aria2::Platform platform; aria2::DownloadResult::RESULT r = aria2::main(argc, argv); return r; }