main.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "HttpInitiateConnectionCommand.h"
  23. #include "ConsoleDownloadEngine.h"
  24. #include "TorrentConsoleDownloadEngine.h"
  25. #include "SegmentMan.h"
  26. #include "TorrentMan.h"
  27. #include "SplitSlowestSegmentSplitter.h"
  28. #include "SimpleLogger.h"
  29. #include "common.h"
  30. #include "DefaultDiskWriter.h"
  31. #include "Util.h"
  32. #include "InitiateConnectionCommandFactory.h"
  33. #include "prefs.h"
  34. #include "TrackerInitCommand.h"
  35. #include "PeerListenCommand.h"
  36. #include "TorrentAutoSaveCommand.h"
  37. #include "TrackerWatcherCommand.h"
  38. #include <deque>
  39. #include <algorithm>
  40. #include <time.h>
  41. #include <signal.h>
  42. #include <unistd.h>
  43. #include <libgen.h>
  44. #include <utility>
  45. extern char* optarg;
  46. extern int optind, opterr, optopt;
  47. #include <getopt.h>
  48. #ifdef HAVE_LIBSSL
  49. // for SSL
  50. # include <openssl/err.h>
  51. # include <openssl/ssl.h>
  52. #endif // HAVE_LIBSSL
  53. #ifdef HAVE_LIBGNUTLS
  54. # include <gnutls/gnutls.h>
  55. #endif // HAVE_LIBGNUTLS
  56. using namespace std;
  57. typedef deque<Request*> Requests;
  58. void printDownloadCompeleteMessage(string filename) {
  59. printf(_("\nThe download was complete. <%s>\n"), filename.c_str());
  60. }
  61. void printDownloadAbortMessage() {
  62. printf(_("\nThe download was not complete because of errors. Check the log.\n"));
  63. }
  64. void clearRequest(Request* req) {
  65. delete(req);
  66. }
  67. DownloadEngine* e;
  68. TorrentDownloadEngine* te;
  69. void handler(int signal) {
  70. cout << _("\nSIGINT signal received.") << endl;
  71. e->segmentMan->save();
  72. if(e->diskWriter != NULL) {
  73. e->diskWriter->closeFile();
  74. }
  75. exit(0);
  76. }
  77. void torrentHandler(int signal) {
  78. cout << _("\nSIGINT signal received.") << endl;
  79. if(te->torrentMan->diskWriter != NULL) {
  80. te->torrentMan->diskWriter->closeFile();
  81. }
  82. if(te->torrentMan->downloadComplete() && te->isFilenameFixed()) {
  83. te->torrentMan->remove();
  84. te->torrentMan->deleteTempFile();
  85. printDownloadCompeleteMessage(te->torrentMan->getFilePath());
  86. } else {
  87. te->torrentMan->save();
  88. }
  89. exit(0);
  90. }
  91. void addCommand(int cuid, const string& url, string referer, Requests& requests) {
  92. Request* req = new Request();
  93. req->setReferer(referer);
  94. if(req->setUrl(url)) {
  95. e->commands.push(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e));
  96. requests.push_back(req);
  97. } else {
  98. fprintf(stderr, _("Unrecognized URL or unsupported protocol: %s\n"), req->getUrl().c_str());
  99. delete(req);
  100. }
  101. }
  102. void showVersion() {
  103. cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl;
  104. cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl;
  105. cout << endl;
  106. cout <<
  107. _("This program is free software; you can redistribute it and/or modify\n"
  108. "it under the terms of the GNU General Public License as published by\n"
  109. "the Free Software Foundation; either version 2 of the License, or\n"
  110. "(at your option) any later version.\n"
  111. "\n"
  112. "This program is distributed in the hope that it will be useful,\n"
  113. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  114. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  115. "GNU General Public License for more details.\n"
  116. "\n"
  117. "You should have received a copy of the GNU General Public License\n"
  118. "along with this program; if not, write to the Free Software\n"
  119. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
  120. cout << endl;
  121. printf(_("Contact Info: %s\n"), "Tasuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>");
  122. cout << endl;
  123. }
  124. void showUsage() {
  125. printf(_("Usage: %s [options] URL ...\n"), PACKAGE_NAME);
  126. cout << endl;
  127. cout << _("Options:") << endl;
  128. cout << _(" -d, --dir=DIR The directory to store downloaded file.") << endl;
  129. cout << _(" -o, --out=FILE The file name for downloaded file.") << endl;
  130. cout << _(" -l, --log=LOG The file path to store log. If '-' is specified,\n"
  131. " log is written to stdout.") << endl;
  132. cout << _(" -D, --daemon Run as daemon.") << endl;
  133. cout << _(" -s, --split=N Download a file using N connections. N must be\n"
  134. " between 1 and 5. This option affects all URLs.\n"
  135. " Thus, aria2 connects to each URL with\n"
  136. " N connections.") << endl;
  137. cout << _(" --retry-wait=SEC Set amount of time in second between requests\n"
  138. " for errors. Specify a value between 0 and 60.\n"
  139. " Default: 5") << endl;
  140. cout << _(" -t, --timeout=SEC Set timeout in second. Default: 60") << endl;
  141. cout << _(" -m, --max-tries=N Set number of tries. 0 means unlimited.\n"
  142. " Default: 5") << endl;
  143. cout << _(" --min-segment-size=SIZE[K|M] Set minimum segment size. You can append\n"
  144. " K or M(1K = 1024, 1M = 1024K). This\n"
  145. " value must be greater than or equal to\n"
  146. " 1024.") << endl;
  147. cout << _(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all\n"
  148. " URLs.") << endl;
  149. cout << _(" --http-user=USER Set HTTP user. This affects to all URLs.") << endl;
  150. cout << _(" --http-passwd=PASSWD Set HTTP password. This affects to all URLs.") << endl;
  151. cout << _(" --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs") << endl;
  152. cout << _(" --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs.") << endl;
  153. cout << _(" --http-proxy-method=METHOD Set the method to use in proxy request.\n"
  154. " METHOD is either 'get' or 'tunnel'.\n"
  155. " Default: tunnel") << endl;
  156. cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
  157. " is the only supported scheme. You MUST specify\n"
  158. " this option in order to use HTTP authentication\n"
  159. " as well as --http-user and --http-passwd.") << endl;
  160. cout << _(" --referer=REFERER Set Referer. This affects to all URLs.") << endl;
  161. cout << _(" --ftp-user=USER Set FTP user. This affects to all URLs.\n"
  162. " Default: anonymous") << endl;
  163. cout << _(" --ftp-passwd=PASSWD Set FTP password. This affects to all URLs.\n"
  164. " Default: ARIA2USER@") << endl;
  165. cout << _(" --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'\n"
  166. " or 'ascii'.\n"
  167. " Default: binary") << endl;
  168. cout << _(" -p, --ftp-pasv Use passive mode in FTP.") << endl;
  169. cout << _(" --ftp-via-http-proxy=METHOD Use HTTP proxy in FTP. METHOD is either 'get' or\n"
  170. " 'tunnel'.\n"
  171. " Default: tunnel") << endl;
  172. #ifdef ENABLE_BITTORRENT
  173. cout << _(" --torrent-file=TORRENT_FILE The file path to .torrent file.") << endl;
  174. cout << _(" --follow-torrent=true|false Setting this option to false prevents aria2 to\n"
  175. " enter BitTorrent mode even if the filename of\n"
  176. " downloaded file ends with .torrent.\n"
  177. " Default: true") << endl;
  178. #endif // ENABLE_BITTORRENT
  179. cout << _(" -v, --version Print the version number and exit.") << endl;
  180. cout << _(" -h, --help Print this message and exit.") << endl;
  181. cout << endl;
  182. cout << "URL:" << endl;
  183. cout << _(" You can specify multiple URLs. All URLs must point to the same file\n"
  184. " or downloading fails.") << endl;
  185. cout << endl;
  186. cout << _("Examples:") << endl;
  187. cout << _(" Download a file by 1 connection:") << endl;
  188. cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
  189. cout << _(" Download a file by 2 connections:") << endl;
  190. cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
  191. cout << _(" Download a file by 2 connections, each connects to a different server:") << endl;
  192. cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
  193. cout << _(" You can mix up different protocols:") << endl;
  194. cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
  195. #ifdef ENABLE_BITTORRENT
  196. cout << _(" Download a torrent:") << endl;
  197. cout << " aria2c -o test.torrent http://AAA.BBB.CCC/file.torrent" << endl;
  198. cout << _(" Download a torrent using local .torrent file:") << endl;
  199. cout << " aria2c --torrent-file test.torrent" << endl;
  200. cout << endl;
  201. #endif // ENABLE_BITTORRENT
  202. printf(_("Reports bugs to %s"), "<tujikawa at users dot sourceforge dot net>");
  203. cout << endl;
  204. }
  205. int main(int argc, char* argv[]) {
  206. #ifdef ENABLE_NLS
  207. setlocale (LC_CTYPE, "");
  208. setlocale (LC_MESSAGES, "");
  209. bindtextdomain (PACKAGE, LOCALEDIR);
  210. textdomain (PACKAGE);
  211. #endif // ENABLE_NLS
  212. bool stdoutLog = false;
  213. string logfile;
  214. string dir = ".";
  215. string ufilename;
  216. int split = 1;
  217. bool daemonMode = false;
  218. string referer;
  219. string torrentFile;
  220. Strings args;
  221. #ifdef ENABLE_BITTORRENT
  222. bool followTorrent = true;
  223. #else
  224. bool followTorrent = false;
  225. #endif // ENABLE_BITTORRENT
  226. int c;
  227. Option* op = new Option();
  228. op->put(PREF_RETRY_WAIT, "5");
  229. op->put(PREF_TIMEOUT, "60");
  230. op->put(PREF_PEER_CONNECTION_TIMEOUT, "60");
  231. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  232. op->put(PREF_MAX_TRIES, "5");
  233. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  234. op->put(PREF_FTP_USER, "anonymous");
  235. op->put(PREF_FTP_PASSWD, "ARIA2USER@");
  236. op->put(PREF_FTP_TYPE, V_BINARY);
  237. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  238. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  239. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  240. while(1) {
  241. int optIndex = 0;
  242. int lopt;
  243. static struct option longOpts[] = {
  244. { "daemon", no_argument, NULL, 'D' },
  245. { "dir", required_argument, NULL, 'd' },
  246. { "out", required_argument, NULL, 'o' },
  247. { "log", required_argument, NULL, 'l' },
  248. { "split", required_argument, NULL, 's' },
  249. { "timeout", required_argument, NULL, 't' },
  250. { "max-tries", required_argument, NULL, 'm' },
  251. { "http-proxy", required_argument, &lopt, 1 },
  252. { "http-user", required_argument, &lopt, 2 },
  253. { "http-passwd", required_argument, &lopt, 3 },
  254. { "http-proxy-user", required_argument, &lopt, 4 },
  255. { "http-proxy-passwd", required_argument, &lopt, 5 },
  256. { "http-auth-scheme", required_argument, &lopt, 6 },
  257. { "referer", required_argument, &lopt, 7 },
  258. { "retry-wait", required_argument, &lopt, 8 },
  259. { "ftp-user", required_argument, &lopt, 9 },
  260. { "ftp-passwd", required_argument, &lopt, 10 },
  261. { "ftp-type", required_argument, &lopt, 11 },
  262. { "ftp-pasv", no_argument, NULL, 'p' },
  263. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  264. { "min-segment-size", required_argument, &lopt, 13 },
  265. { "http-proxy-method", required_argument, &lopt, 14 },
  266. #ifdef ENABLE_BITTORRENT
  267. { "torrent-file", required_argument, &lopt, 15 },
  268. { "follow-torrent", required_argument, &lopt, 16 },
  269. { "torrent-show-files", no_argument, &lopt, 17 },
  270. { "no-preallocation", no_argument, &lopt, 18 },
  271. { "direct-file-mapping", required_argument, &lopt, 19 },
  272. #endif // ENABLE_BITTORRENT
  273. { "version", no_argument, NULL, 'v' },
  274. { "help", no_argument, NULL, 'h' },
  275. { 0, 0, 0, 0 }
  276. };
  277. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vh", longOpts, &optIndex);
  278. if(c == -1) {
  279. break;
  280. }
  281. switch(c) {
  282. case 0:{
  283. switch(lopt) {
  284. case 1: {
  285. pair<string, string> proxy;
  286. Util::split(proxy, optarg, ':');
  287. int port = (int)strtol(proxy.second.c_str(), NULL, 10);
  288. if(proxy.first.empty() || proxy.second.empty() ||
  289. !(0 < port && port <= 65535)) {
  290. cerr << _("unrecognized proxy format") << endl;
  291. showUsage();
  292. exit(1);
  293. }
  294. op->put(PREF_HTTP_PROXY_HOST, proxy.first);
  295. op->put(PREF_HTTP_PROXY_PORT, Util::itos(port));
  296. op->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  297. break;
  298. }
  299. case 2:
  300. op->put(PREF_HTTP_USER, optarg);
  301. break;
  302. case 3:
  303. op->put(PREF_HTTP_PASSWD, optarg);
  304. break;
  305. case 4:
  306. op->put(PREF_HTTP_PROXY_USER, optarg);
  307. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  308. break;
  309. case 5:
  310. op->put(PREF_HTTP_PROXY_PASSWD, optarg);
  311. break;
  312. case 6:
  313. if(string(V_BASIC) == optarg) {
  314. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  315. } else {
  316. cerr << _("Currently, supported authentication scheme is basic.") << endl;
  317. }
  318. break;
  319. case 7:
  320. referer = optarg;
  321. break;
  322. case 8: {
  323. int wait = (int)strtol(optarg, NULL, 10);
  324. if(!(0 <= wait && wait <= 60)) {
  325. cerr << _("retry-wait must be between 0 and 60.") << endl;
  326. showUsage();
  327. exit(1);
  328. }
  329. op->put(PREF_RETRY_WAIT, Util::itos(wait));
  330. break;
  331. }
  332. case 9:
  333. op->put(PREF_FTP_USER, optarg);
  334. break;
  335. case 10:
  336. op->put(PREF_FTP_PASSWD, optarg);
  337. break;
  338. case 11:
  339. if(string(optarg) == V_BINARY || string(optarg) == V_ASCII) {
  340. op->put(PREF_FTP_TYPE, optarg);
  341. } else {
  342. cerr << _("ftp-type must be either 'binary' or 'ascii'.") << endl;
  343. showUsage();
  344. exit(1);
  345. }
  346. break;
  347. case 12:
  348. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  349. op->put(PREF_FTP_VIA_HTTP_PROXY, optarg);
  350. } else {
  351. cerr << _("ftp-via-http-proxy must be either 'get' or 'tunnel'.") << endl;
  352. showUsage();
  353. exit(1);
  354. }
  355. break;
  356. case 13: {
  357. string::size_type p = string(optarg).find_first_of("KM");
  358. int mult = 1;
  359. if(p != string::npos) {
  360. if(optarg[p] == 'K') {
  361. mult = 1024;
  362. } else if(optarg[p] == 'M') {
  363. mult = 1024*1024;
  364. }
  365. optarg[p] = '\0';
  366. }
  367. long long int size = strtoll(optarg, NULL, 10)*mult;
  368. if(size < 1024) {
  369. cerr << _("min-segment-size invalid") << endl;
  370. showUsage();
  371. exit(1);
  372. }
  373. op->put(PREF_MIN_SEGMENT_SIZE, Util::llitos(size));
  374. break;
  375. }
  376. case 14:
  377. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  378. op->put(PREF_HTTP_PROXY_METHOD, optarg);
  379. } else {
  380. cerr << _("http-proxy-method must be either 'get' or 'tunnel'.") << endl;
  381. showUsage();
  382. exit(1);
  383. }
  384. break;
  385. case 15:
  386. torrentFile = string(optarg);
  387. break;
  388. case 16:
  389. if(string(optarg) == "true") {
  390. followTorrent = true;
  391. } else if(string(optarg) == "false") {
  392. followTorrent = false;
  393. } else {
  394. cerr << _("follow-torrent must be either 'true' or 'false'.") << endl;
  395. showUsage();
  396. exit(1);
  397. }
  398. break;
  399. case 17:
  400. op->put(PREF_TORRENT_SHOW_FILES, V_TRUE);
  401. break;
  402. case 18:
  403. op->put(PREF_NO_PREALLOCATION, V_TRUE);
  404. break;
  405. case 19:
  406. if(string(optarg) == "true") {
  407. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  408. } else if(string(optarg) == "false") {
  409. op->put(PREF_DIRECT_FILE_MAPPING, V_FALSE);
  410. } else {
  411. cerr << "direct-file-mapping must be either 'true' or 'false'." << endl;
  412. showUsage();
  413. exit(1);
  414. }
  415. }
  416. break;
  417. }
  418. case 'D':
  419. daemonMode = true;
  420. break;
  421. case 'd':
  422. dir = optarg;
  423. break;
  424. case 'o':
  425. ufilename = optarg;
  426. break;
  427. case 'l':
  428. if(strcmp("-", optarg) == 0) {
  429. stdoutLog = true;
  430. } else {
  431. logfile = optarg;
  432. }
  433. break;
  434. case 's':
  435. split = (int)strtol(optarg, NULL, 10);
  436. if(!(1 <= split && split <= 5)) {
  437. cerr << _("split must be between 1 and 5.") << endl;
  438. showUsage();
  439. exit(1);
  440. }
  441. break;
  442. case 't': {
  443. int timeout = (int)strtol(optarg, NULL, 10);
  444. if(1 <= timeout && timeout <= 600) {
  445. op->put(PREF_TIMEOUT, Util::itos(timeout));
  446. } else {
  447. cerr << _("timeout must be between 1 and 600") << endl;
  448. showUsage();
  449. exit(1);
  450. }
  451. break;
  452. }
  453. case 'm': {
  454. int retries = (int)strtol(optarg, NULL, 10);
  455. if(retries < 0) {
  456. cerr << _("max-tries invalid") << endl;
  457. showUsage();
  458. exit(1);
  459. }
  460. op->put(PREF_MAX_TRIES, Util::itos(retries));
  461. break;
  462. }
  463. case 'p':
  464. op->put(PREF_FTP_PASV_ENABLED, V_TRUE);
  465. break;
  466. case 'v':
  467. showVersion();
  468. exit(0);
  469. case 'h':
  470. showUsage();
  471. exit(0);
  472. default:
  473. showUsage();
  474. exit(1);
  475. }
  476. }
  477. if(torrentFile.empty()) {
  478. if(optind == argc) {
  479. cerr << _("specify at least one URL") << endl;
  480. showUsage();
  481. exit(1);
  482. }
  483. }
  484. if(daemonMode) {
  485. if(daemon(1, 1) < 0) {
  486. perror("daemon failed");
  487. exit(1);
  488. }
  489. }
  490. for(int i = 1; optind+i-1 < argc; i++) {
  491. args.push_back(argv[optind+i-1]);
  492. }
  493. #ifdef HAVE_LIBSSL
  494. // for SSL initialization
  495. SSL_load_error_strings();
  496. SSL_library_init();
  497. #endif // HAVE_LIBSSL
  498. #ifdef HAVE_LIBGNUTLS
  499. gnutls_global_init();
  500. #endif // HAVE_LIBGNUTLS
  501. srandom(time(NULL));
  502. SimpleLogger* logger;
  503. if(stdoutLog) {
  504. logger = new SimpleLogger(stdout);
  505. } else if(logfile.size()) {
  506. logger = new SimpleLogger(logfile);
  507. } else {
  508. logger = new SimpleLogger("/dev/null");
  509. }
  510. SegmentSplitter* splitter = new SplitSlowestSegmentSplitter();
  511. splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  512. struct sigaction sigactIgn;
  513. sigactIgn.sa_handler = SIG_IGN;
  514. sigactIgn.sa_flags = 0;
  515. sigemptyset(&sigactIgn.sa_mask);
  516. sigaction(SIGPIPE, &sigactIgn, NULL);
  517. bool readyToTorrentMode = false;
  518. string downloadedTorrentFile;
  519. if(torrentFile.empty()) {
  520. struct sigaction sigact;
  521. sigact.sa_handler = handler;
  522. sigact.sa_flags = 0;
  523. sigemptyset(&sigact.sa_mask);
  524. sigaction(SIGINT, &sigact, NULL);
  525. splitter->logger = logger;
  526. e = new ConsoleDownloadEngine();
  527. e->logger = logger;
  528. e->option = op;
  529. e->diskWriter = new DefaultDiskWriter();
  530. e->segmentMan = new SegmentMan();
  531. e->segmentMan->dir = dir;
  532. e->segmentMan->ufilename = ufilename;
  533. e->segmentMan->logger = logger;
  534. e->segmentMan->option = op;
  535. e->segmentMan->splitter = splitter;
  536. Requests requests;
  537. int cuidCounter = 1;
  538. for(Strings::const_iterator itr = args.begin(); itr != args.end(); itr++) {
  539. for(int s = 1; s <= split; s++) {
  540. addCommand(cuidCounter, *itr, referer, requests);
  541. cuidCounter++;
  542. }
  543. }
  544. e->run();
  545. if(e->segmentMan->finished()) {
  546. printDownloadCompeleteMessage(e->segmentMan->getFilePath());
  547. if(Util::endsWith(e->segmentMan->getFilePath(), ".torrent")) {
  548. downloadedTorrentFile = e->segmentMan->getFilePath();
  549. readyToTorrentMode = true;
  550. }
  551. } else {
  552. printDownloadAbortMessage();
  553. }
  554. for_each(requests.begin(), requests.end(), clearRequest);
  555. requests.clear();
  556. delete(e->segmentMan);
  557. delete(e->diskWriter);
  558. delete(e);
  559. }
  560. if(!torrentFile.empty() || followTorrent && readyToTorrentMode) {
  561. try {
  562. //op->put(PREF_MAX_TRIES, "0");
  563. struct sigaction sigact;
  564. sigact.sa_handler = torrentHandler;
  565. sigact.sa_flags = 0;
  566. sigemptyset(&sigact.sa_mask);
  567. sigaction(SIGINT, &sigact, NULL);
  568. Request* req = new Request();
  569. req->isTorrent = true;
  570. req->setTrackerEvent(Request::STARTED);
  571. te = new TorrentConsoleDownloadEngine();
  572. te->logger = logger;
  573. te->option = op;
  574. te->diskWriter = new DefaultDiskWriter();
  575. te->segmentMan = new SegmentMan();
  576. te->segmentMan->logger = logger;
  577. te->segmentMan->option = op;
  578. te->segmentMan->splitter = splitter;
  579. te->torrentMan = new TorrentMan();
  580. te->torrentMan->setStoreDir(dir);
  581. te->torrentMan->logger = logger;
  582. te->torrentMan->option = op;
  583. string targetTorrentFile = torrentFile.empty() ?
  584. downloadedTorrentFile : torrentFile;
  585. if(op->get(PREF_TORRENT_SHOW_FILES) == V_TRUE) {
  586. te->torrentMan->readFileEntryFromMetaInfoFile(targetTorrentFile);
  587. cout << "Files:" << endl;
  588. switch(te->torrentMan->getFileMode()) {
  589. case TorrentMan::SINGLE:
  590. printf("%s %s Bytes\n", te->torrentMan->getName().c_str(),
  591. Util::llitos(te->torrentMan->getTotalLength(), true).c_str());
  592. break;
  593. case TorrentMan::MULTI: {
  594. const MultiFileEntries& entries = te->torrentMan->getMultiFileEntries();
  595. for(MultiFileEntries::const_iterator itr = entries.begin();
  596. itr != entries.end(); itr++) {
  597. printf("%s %s Bytes\n", itr->path.c_str(),
  598. Util::llitos(itr->length, true).c_str());
  599. }
  600. break;
  601. }
  602. }
  603. exit(0);
  604. } else {
  605. te->torrentMan->setup(targetTorrentFile);
  606. if(!torrentFile.empty() && !args.empty() &&
  607. te->torrentMan->getFileMode() == TorrentMan::MULTI) {
  608. te->torrentMan->setFileEntriesToDownload(args);
  609. }
  610. te->torrentMan->setupDiskWriter();
  611. }
  612. PeerListenCommand* listenCommand =
  613. new PeerListenCommand(te->torrentMan->getNewCuid(), te);
  614. int port = listenCommand->bindPort(6881, 6999);
  615. if(port == -1) {
  616. printf("Errors occurred while binding port.\n");
  617. exit(1);
  618. }
  619. te->torrentMan->setPort(port);
  620. te->commands.push(listenCommand);
  621. te->commands.push(new TrackerWatcherCommand(te->torrentMan->getNewCuid(),
  622. req, te));
  623. te->commands.push(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(),
  624. te,
  625. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  626. te->run();
  627. if(te->torrentMan->downloadComplete()) {
  628. printDownloadCompeleteMessage(te->torrentMan->getFilePath());
  629. } else {
  630. printDownloadAbortMessage();
  631. }
  632. delete(req);
  633. delete(te->segmentMan);
  634. delete(te->torrentMan);
  635. delete(te->diskWriter);
  636. delete(te);
  637. } catch(Exception* ex) {
  638. cerr << ex->getMsg() << endl;
  639. delete ex;
  640. exit(1);
  641. }
  642. }
  643. delete(logger);
  644. delete(op);
  645. delete(splitter);
  646. #ifdef HAVE_LIBGNUTLS
  647. gnutls_global_deinit();
  648. #endif // HAVE_LIBGNUTLS
  649. return 0;
  650. }