1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#include "Context.h"
#include <unistd.h>
#include <getopt.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif // HAVE_SYS_RESOURCE_H
#include <numeric>
#include <vector>
#include <iostream>
#include "LogFactory.h"
#include "Logger.h"
#include "util.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 "FileEntry.h"
#include "RequestGroup.h"
#include "download_helper.h"
#include "Exception.h"
#include "ProtocolDetector.h"
#include "RecoverableException.h"
#include "SocketCore.h"
#include "DownloadContext.h"
#include "fmt.h"
#include "console.h"
#include "UriListParser.h"
#include "message_digest_helper.h"
#ifdef ENABLE_BITTORRENT
# include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
# include "metalink_helper.h"
# include "MetalinkEntry.h"
#endif // ENABLE_METALINK
extern char* optarg;
extern int optind, opterr, optopt;
namespace aria2 {
#ifdef ENABLE_BITTORRENT
namespace {
void showTorrentFile(const std::string& uri)
{
std::shared_ptr<Option> op(new Option());
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
bittorrent::load(uri, dctx, op);
bittorrent::print(*global::cout(), dctx);
}
} // namespace
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
namespace {
void showMetalinkFile
(const std::string& uri, const std::shared_ptr<Option>& op)
{
auto fileEntries = MetalinkEntry::toFileEntry
(metalink::parseAndQuery(uri, op.get(), op->get(PREF_METALINK_BASE_URI)));
util::toStream(std::begin(fileEntries), std::end(fileEntries),
*global::cout());
global::cout()->write("\n");
global::cout()->flush();
}
} // namespace
#endif // ENABLE_METALINK
#if defined(ENABLE_BITTORRENT) || defined(ENABLE_METALINK)
namespace {
void showFiles
(const std::vector<std::string>& uris, const std::shared_ptr<Option>& op)
{
ProtocolDetector dt;
for(const auto & uri : uris) {
printf(">>> ");
printf(MSG_SHOW_FILES, (uri).c_str());
printf("\n");
try {
#ifdef ENABLE_BITTORRENT
if(dt.guessTorrentFile(uri)) {
showTorrentFile(uri);
} else
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(dt.guessMetalinkFile(uri)) {
showMetalinkFile(uri, op);
} else
#endif // ENABLE_METALINK
{
printf(MSG_NOT_TORRENT_METALINK);
printf("\n\n");
}
} catch(RecoverableException& e) {
global::cout()->printf("%s\n", e.stackTrace().c_str());
}
}
}
} // namespace
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
extern error_code::Value option_processing(Option& option, bool standalone,
std::vector<std::string>& uris,
int argc, char** argv,
const KeyVals& options);
Context::Context(bool standalone,
int argc, char** argv, const KeyVals& options)
{
std::vector<std::string> args;
std::shared_ptr<Option> op(new Option());
error_code::Value rv;
rv = option_processing(*op.get(), standalone, args, argc, argv, options);
if(rv != error_code::FINISHED) {
if(standalone) {
exit(rv);
} else {
throw DL_ABORT_EX("Option processing failed");
}
}
SimpleRandomizer::getInstance()->init();
#ifdef ENABLE_BITTORRENT
bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
#endif // ENABLE_BITTORRENT
LogFactory::setLogFile(op->get(PREF_LOG));
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
LogFactory::setConsoleLogLevel(op->get(PREF_CONSOLE_LOG_LEVEL));
LogFactory::setColorOutput(op->getAsBool(PREF_ENABLE_COLOR));
if(op->getAsBool(PREF_QUIET)) {
LogFactory::setConsoleOutput(false);
}
LogFactory::reconfigure();
A2_LOG_INFO("<<--- --- --- ---");
A2_LOG_INFO(" --- --- --- ---");
A2_LOG_INFO(" --- --- --- --->>");
A2_LOG_INFO(fmt("%s %s", PACKAGE, PACKAGE_VERSION));
A2_LOG_INFO(usedCompilerAndPlatform());
A2_LOG_INFO(getOperatingSystemInfo());
A2_LOG_INFO(usedLibs());
A2_LOG_INFO(MSG_LOGGING_STARTED);
#if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
rlimit r = { 0, 0 };
if (getrlimit(RLIMIT_NOFILE, &r) >= 0 && r.rlim_cur != RLIM_INFINITY) {
// Thanks portability, for making it easy :p
auto rlim_new = r.rlim_cur; // So we get the right type for free.
if (r.rlim_cur != RLIM_INFINITY) {
rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
rlim_new = std::max(r.rlim_cur, rlim_new);
if (r.rlim_max != RLIM_INFINITY) {
rlim_new = std::min(r.rlim_max, rlim_new);
}
}
if (rlim_new != r.rlim_cur) {
if (setrlimit(RLIMIT_NOFILE, &r) != 0) {
int errNum = errno;
A2_LOG_WARN(fmt("Failed to set rlimit NO_FILE from %" PRIu64 " to "
"%" PRIu64 ": %s",
(uint64_t)r.rlim_cur, (uint64_t)rlim_new,
util::safeStrerror(errNum).c_str()));
}
else {
A2_LOG_DEBUG(fmt("Set rlimit NO_FILE from %" PRIu64 " to %" PRIu64,
(uint64_t)r.rlim_cur, (uint64_t)rlim_new));
}
}
else {
rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
A2_LOG_DEBUG(fmt("Not setting rlimit NO_FILE: %" PRIu64 " >= %" PRIu64,
(uint64_t)r.rlim_cur, (uint64_t)rlim_new));
}
}
#endif // defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
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);
}
SocketCore::setIpDscp(op->getAsInt(PREF_DSCP));
net::checkAddrconfig();
// Bind interface
if(!op->get(PREF_INTERFACE).empty()) {
std::string iface = op->get(PREF_INTERFACE);
SocketCore::bindAddress(iface);
}
std::vector<std::shared_ptr<RequestGroup> > requestGroups;
std::shared_ptr<UriListParser> uriListParser;
#ifdef ENABLE_BITTORRENT
if(!op->blank(PREF_TORRENT_FILE)) {
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showTorrentFile(op->get(PREF_TORRENT_FILE));
return;
} else {
createRequestGroupForBitTorrent(requestGroups, op, args,
op->get(PREF_TORRENT_FILE));
}
}
else
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(!op->blank(PREF_METALINK_FILE)) {
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showMetalinkFile(op->get(PREF_METALINK_FILE), op);
return;
} else {
createRequestGroupForMetalink(requestGroups, op);
}
}
else
#endif // ENABLE_METALINK
if(!op->blank(PREF_INPUT_FILE)) {
if(op->getAsBool(PREF_DEFERRED_INPUT)) {
uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
} else {
createRequestGroupForUriList(requestGroups, op);
}
#if defined(ENABLE_BITTORRENT) || defined(ENABLE_METALINK)
} else if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showFiles(args, op);
return;
#endif // ENABLE_METALINK || ENABLE_METALINK
} else {
createRequestGroupForUri(requestGroups, op, args, false, false, true);
}
// Remove option values which is only valid for URIs specified in
// 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.
for(auto i = op; i; i = i->getParent()) {
i->remove(PREF_OUT);
i->remove(PREF_FORCE_SEQUENTIAL);
i->remove(PREF_INPUT_FILE);
i->remove(PREF_INDEX_OUT);
i->remove(PREF_SELECT_FILE);
i->remove(PREF_PAUSE);
i->remove(PREF_CHECKSUM);
i->remove(PREF_GID);
}
if(standalone &&
!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
!uriListParser) {
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
} else {
reqinfo.reset(new MultiUrlRequestInfo(std::move(requestGroups),
op,
uriListParser));
}
}
Context::~Context()
{
}
} // namespace aria2
|