2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "common.h"
|
2008-11-03 07:49:13 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <numeric>
|
2010-02-28 12:30:11 +00:00
|
|
|
#include <vector>
|
2011-08-09 14:33:55 +00:00
|
|
|
#include <iostream>
|
2008-11-03 07:49:13 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "SharedHandle.h"
|
2007-03-21 10:19:23 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2006-07-30 12:58:27 +00:00
|
|
|
#include "FeatureConfig.h"
|
2007-05-20 13:51:52 +00:00
|
|
|
#include "MultiUrlRequestInfo.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "SimpleRandomizer.h"
|
2007-03-26 12:16:57 +00:00
|
|
|
#include "File.h"
|
2007-07-05 15:14:00 +00:00
|
|
|
#include "message.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "a2algo.h"
|
2007-07-23 13:04:48 +00:00
|
|
|
#include "a2io.h"
|
2007-07-31 16:45:16 +00:00
|
|
|
#include "a2time.h"
|
2007-07-23 13:04:48 +00:00
|
|
|
#include "Platform.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroup.h"
|
2008-04-22 10:29:48 +00:00
|
|
|
#include "ConsoleStatCalc.h"
|
|
|
|
#include "NullStatCalc.h"
|
2008-11-13 13:40:40 +00:00
|
|
|
#include "download_helper.h"
|
2008-11-13 15:08:52 +00:00
|
|
|
#include "Exception.h"
|
2008-12-20 10:59:16 +00:00
|
|
|
#include "ProtocolDetector.h"
|
2009-01-12 12:29:41 +00:00
|
|
|
#include "RecoverableException.h"
|
2009-01-15 15:23:16 +00:00
|
|
|
#include "SocketCore.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2011-08-09 14:33:55 +00:00
|
|
|
#include "NullOutputFile.h"
|
|
|
|
#include "console.h"
|
2012-02-05 09:57:16 +00:00
|
|
|
#include "UriListParser.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
|
|
|
# include "bittorrent_helper.h"
|
|
|
|
#endif // ENABLE_BITTORRENT
|
2008-02-08 15:53:45 +00:00
|
|
|
#ifdef ENABLE_METALINK
|
2011-02-05 14:21:06 +00:00
|
|
|
# include "metalink_helper.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
# include "MetalinkEntry.h"
|
|
|
|
#endif // ENABLE_METALINK
|
2008-02-28 17:40:47 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2011-02-05 14:38:51 +00:00
|
|
|
# include "message_digest_helper.h"
|
2008-02-28 17:40:47 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2008-11-03 07:49:13 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
extern char* optarg;
|
|
|
|
extern int optind, opterr, optopt;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2009-05-08 16:41:21 +00:00
|
|
|
SharedHandle<StatCalc> getStatCalc(const SharedHandle<Option>& op)
|
2008-04-22 10:29:48 +00:00
|
|
|
{
|
|
|
|
SharedHandle<StatCalc> statCalc;
|
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
|
|
|
statCalc.reset(new NullStatCalc());
|
|
|
|
} else {
|
2011-03-27 10:16:54 +00:00
|
|
|
SharedHandle<ConsoleStatCalc> impl
|
|
|
|
(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL),
|
|
|
|
op->getAsBool(PREF_HUMAN_READABLE)));
|
|
|
|
impl->setReadoutVisibility(op->getAsBool(PREF_SHOW_CONSOLE_READOUT));
|
2011-06-15 11:27:14 +00:00
|
|
|
impl->setTruncate(op->getAsBool(PREF_TRUNCATE_CONSOLE_READOUT));
|
2011-03-27 10:16:54 +00:00
|
|
|
statCalc = impl;
|
2008-04-22 10:29:48 +00:00
|
|
|
}
|
|
|
|
return statCalc;
|
|
|
|
}
|
|
|
|
|
2011-08-09 14:33:55 +00:00
|
|
|
SharedHandle<OutputFile> getSummaryOut(const SharedHandle<Option>& op)
|
2008-04-22 10:29:48 +00:00
|
|
|
{
|
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
2011-08-09 14:33:55 +00:00
|
|
|
return SharedHandle<OutputFile>(new NullOutputFile());
|
2008-04-22 10:29:48 +00:00
|
|
|
} else {
|
2011-08-09 16:28:20 +00:00
|
|
|
return global::cout();
|
2008-04-22 10:29:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-23 14:14:59 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void showTorrentFile(const std::string& uri)
|
2008-12-20 10:59:16 +00:00
|
|
|
{
|
2010-12-03 04:08:22 +00:00
|
|
|
SharedHandle<Option> op(new Option());
|
2009-06-28 10:37:15 +00:00
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
2010-12-03 04:08:22 +00:00
|
|
|
bittorrent::load(uri, dctx, op);
|
2011-08-09 16:28:20 +00:00
|
|
|
bittorrent::print(*global::cout(), dctx);
|
2008-12-20 10:59:16 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-12-23 14:14:59 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2008-12-20 10:59:16 +00:00
|
|
|
|
2008-12-23 14:14:59 +00:00
|
|
|
#ifdef ENABLE_METALINK
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void showMetalinkFile
|
2009-05-08 16:41:21 +00:00
|
|
|
(const std::string& uri, const SharedHandle<Option>& op)
|
2008-12-20 10:59:16 +00:00
|
|
|
{
|
2010-02-27 15:14:40 +00:00
|
|
|
std::vector<SharedHandle<MetalinkEntry> > metalinkEntries;
|
2011-05-14 13:46:04 +00:00
|
|
|
metalink::parseAndQuery(metalinkEntries, uri, op.get(),
|
|
|
|
op->get(PREF_METALINK_BASE_URI));
|
2010-02-27 15:14:40 +00:00
|
|
|
std::vector<SharedHandle<FileEntry> > fileEntries;
|
2008-12-20 10:59:16 +00:00
|
|
|
MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
|
2011-08-09 16:28:20 +00:00
|
|
|
util::toStream(fileEntries.begin(), fileEntries.end(), *global::cout());
|
|
|
|
global::cout()->write("\n");
|
|
|
|
global::cout()->flush();
|
2008-12-20 10:59:16 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-12-23 14:14:59 +00:00
|
|
|
#endif // ENABLE_METALINK
|
2008-12-20 10:59:16 +00:00
|
|
|
|
2008-12-23 14:14:59 +00:00
|
|
|
#if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void showFiles
|
2010-02-28 12:30:11 +00:00
|
|
|
(const std::vector<std::string>& uris, const SharedHandle<Option>& op)
|
2008-12-20 10:59:16 +00:00
|
|
|
{
|
|
|
|
ProtocolDetector dt;
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = uris.begin(),
|
|
|
|
eoi = uris.end(); i != eoi; ++i) {
|
2008-12-23 02:44:11 +00:00
|
|
|
printf(">>> ");
|
2008-12-20 11:09:05 +00:00
|
|
|
printf(MSG_SHOW_FILES, (*i).c_str());
|
2008-12-20 10:59:16 +00:00
|
|
|
printf("\n");
|
2008-12-23 02:41:43 +00:00
|
|
|
try {
|
2008-12-23 14:14:59 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2008-12-23 02:41:43 +00:00
|
|
|
if(dt.guessTorrentFile(*i)) {
|
2010-01-05 16:01:46 +00:00
|
|
|
showTorrentFile(*i);
|
2008-12-23 14:14:59 +00:00
|
|
|
} else
|
|
|
|
#endif // ENABLE_BITTORRENT
|
|
|
|
#ifdef ENABLE_METALINK
|
2010-01-05 16:01:46 +00:00
|
|
|
if(dt.guessMetalinkFile(*i)) {
|
|
|
|
showMetalinkFile(*i, op);
|
|
|
|
} else
|
2008-12-23 14:14:59 +00:00
|
|
|
#endif // ENABLE_METALINK
|
2010-01-05 16:01:46 +00:00
|
|
|
{
|
|
|
|
printf(MSG_NOT_TORRENT_METALINK);
|
|
|
|
printf("\n\n");
|
|
|
|
}
|
2008-12-23 02:41:43 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2011-08-09 16:28:20 +00:00
|
|
|
global::cout()->printf("%s\n", e.stackTrace().c_str());
|
2008-12-20 10:59:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-12-23 14:14:59 +00:00
|
|
|
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
|
2008-12-20 10:59:16 +00:00
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
extern void option_processing(Option& option, std::vector<std::string>& uris,
|
2011-04-22 14:38:59 +00:00
|
|
|
int argc, char* argv[]);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::Value main(int argc, char* argv[])
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> args;
|
2009-05-08 16:41:21 +00:00
|
|
|
SharedHandle<Option> op(new Option());
|
|
|
|
option_processing(*op.get(), args, argc, argv);
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
SimpleRandomizer::init();
|
2009-07-04 15:20:18 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2009-10-05 13:04:06 +00:00
|
|
|
bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
|
2009-07-04 15:20:18 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2010-06-23 14:15:35 +00:00
|
|
|
LogFactory::setLogFile(op->get(PREF_LOG));
|
2009-05-08 16:41:21 +00:00
|
|
|
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
|
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
2008-04-22 10:29:48 +00:00
|
|
|
LogFactory::setConsoleOutput(false);
|
|
|
|
}
|
2011-12-27 08:14:29 +00:00
|
|
|
LogFactory::reconfigure();
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::Value exitStatus = error_code::FINISHED;
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO("<<--- --- --- ---");
|
|
|
|
A2_LOG_INFO(" --- --- --- ---");
|
|
|
|
A2_LOG_INFO(" --- --- --- --->>");
|
|
|
|
A2_LOG_INFO(fmt("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET));
|
|
|
|
A2_LOG_INFO(MSG_LOGGING_STARTED);
|
2006-04-19 17:23:58 +00:00
|
|
|
|
2008-02-28 17:40:47 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2011-02-05 14:38:51 +00:00
|
|
|
message_digest::staticSHA1DigestInit();
|
2008-02-28 17:40:47 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2010-02-12 13:53:52 +00:00
|
|
|
if(op->getAsBool(PREF_DISABLE_IPV6)) {
|
|
|
|
SocketCore::setProtocolFamily(AF_INET);
|
|
|
|
// Get rid of AI_ADDRCONFIG. It causes name resolution error
|
|
|
|
// when none of network interface has IPv4 address.
|
|
|
|
setDefaultAIFlags(0);
|
|
|
|
}
|
|
|
|
// Bind interface
|
|
|
|
if(!op->get(PREF_INTERFACE).empty()) {
|
|
|
|
std::string iface = op->get(PREF_INTERFACE);
|
|
|
|
SocketCore::bindAddress(iface);
|
|
|
|
}
|
2009-12-03 14:41:08 +00:00
|
|
|
|
2007-07-23 13:04:48 +00:00
|
|
|
#ifdef SIGPIPE
|
2010-02-12 13:53:52 +00:00
|
|
|
util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
|
2007-07-23 13:04:48 +00:00
|
|
|
#endif
|
2009-07-16 13:07:29 +00:00
|
|
|
#ifdef SIGCHLD
|
2010-02-12 13:53:52 +00:00
|
|
|
// Avoid to create zombie process when forked child processes are
|
|
|
|
// died.
|
|
|
|
util::setGlobalSignalHandler(SIGCHLD, SIG_IGN, 0);
|
2009-07-16 13:07:29 +00:00
|
|
|
#endif // SIGCHILD
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<SharedHandle<RequestGroup> > requestGroups;
|
2012-02-05 09:57:16 +00:00
|
|
|
SharedHandle<UriListParser> uriListParser;
|
2006-08-07 16:05:00 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2010-02-12 13:53:52 +00:00
|
|
|
if(!op->blank(PREF_TORRENT_FILE)) {
|
2010-09-11 12:48:03 +00:00
|
|
|
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
|
2010-02-12 13:53:52 +00:00
|
|
|
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)) {
|
2010-09-11 12:48:03 +00:00
|
|
|
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
|
2010-02-12 13:53:52 +00:00
|
|
|
showMetalinkFile(op->get(PREF_METALINK_FILE), op);
|
2010-01-05 16:01:46 +00:00
|
|
|
return exitStatus;
|
2007-10-11 16:58:24 +00:00
|
|
|
} else {
|
2010-02-12 13:53:52 +00:00
|
|
|
createRequestGroupForMetalink(requestGroups, op);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-08-07 16:05:00 +00:00
|
|
|
else
|
|
|
|
#endif // ENABLE_METALINK
|
2010-02-12 13:53:52 +00:00
|
|
|
if(!op->blank(PREF_INPUT_FILE)) {
|
2012-02-05 09:57:16 +00:00
|
|
|
if(op->getAsBool(PREF_DEFERRED_INPUT)) {
|
|
|
|
uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
|
|
|
|
} else {
|
|
|
|
createRequestGroupForUriList(requestGroups, op);
|
|
|
|
}
|
2008-12-23 14:14:59 +00:00
|
|
|
#if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
|
2010-09-11 12:48:03 +00:00
|
|
|
} else if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
|
2010-02-12 13:53:52 +00:00
|
|
|
showFiles(args, op);
|
|
|
|
return exitStatus;
|
2008-12-23 14:14:59 +00:00
|
|
|
#endif // ENABLE_METALINK || ENABLE_METALINK
|
2010-02-12 13:53:52 +00:00
|
|
|
} else {
|
2010-12-04 08:19:31 +00:00
|
|
|
createRequestGroupForUri(requestGroups, op, args, false, false, true);
|
2010-02-12 13:53:52 +00:00
|
|
|
}
|
2008-11-13 13:40:40 +00:00
|
|
|
|
2010-02-12 13:53:52 +00:00
|
|
|
// Remove option values which is only valid for URIs specified in
|
2011-03-18 13:45:25 +00:00
|
|
|
// command-line. If they are left, because op is used as a template
|
|
|
|
// for new RequestGroup(such as created in RPC command), they causes
|
|
|
|
// unintentional effect.
|
2010-02-12 13:53:52 +00:00
|
|
|
op->remove(PREF_OUT);
|
|
|
|
op->remove(PREF_FORCE_SEQUENTIAL);
|
|
|
|
op->remove(PREF_INPUT_FILE);
|
|
|
|
op->remove(PREF_INDEX_OUT);
|
|
|
|
op->remove(PREF_SELECT_FILE);
|
2011-06-16 12:37:15 +00:00
|
|
|
op->remove(PREF_PAUSE);
|
2011-08-23 08:25:15 +00:00
|
|
|
op->remove(PREF_CHECKSUM);
|
2012-02-05 09:57:16 +00:00
|
|
|
if(!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
|
|
|
|
!uriListParser) {
|
2011-08-09 16:28:20 +00:00
|
|
|
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
|
2010-02-12 13:53:52 +00:00
|
|
|
} else {
|
|
|
|
exitStatus = MultiUrlRequestInfo(requestGroups, op, getStatCalc(op),
|
2012-02-05 09:57:16 +00:00
|
|
|
getSummaryOut(op),
|
|
|
|
uriListParser).execute();
|
2006-03-02 02:54:49 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
return exitStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
2010-02-12 13:53:52 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2010-11-28 07:52:02 +00:00
|
|
|
aria2::error_code::Value r;
|
2010-02-12 13:53:52 +00:00
|
|
|
try {
|
|
|
|
aria2::Platform platform;
|
|
|
|
r = aria2::main(argc, argv);
|
|
|
|
} catch(aria2::Exception& ex) {
|
2011-08-09 16:28:20 +00:00
|
|
|
aria2::global::cerr()->printf("%s\n%s\n",
|
|
|
|
EX_EXCEPTION_CAUGHT,
|
|
|
|
ex.stackTrace().c_str());
|
2010-12-05 06:13:32 +00:00
|
|
|
r = ex.getErrorCode();
|
2010-02-12 13:53:52 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
return r;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|