diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2017-05-22 22:37:34 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2017-05-22 22:37:34 +0900 |
| commit | e3b6460d073e02a5040e151f08bf7c85c66c75c3 (patch) | |
| tree | 698dabf62ce457c05c45bdeb5e96a259d76c142a | |
| parent | 9f50d7353caa375cb49c16c107799d4741934787 (diff) | |
| parent | 2b81eb09d47a39f9c0476d77c09526cbbb136451 (diff) | |
Merge branch 'bt-load-saved-metadata'
| -rw-r--r-- | doc/manual-src/en/aria2c.rst | 9 | ||||
| -rw-r--r-- | src/OptionHandlerFactory.cc | 10 | ||||
| -rw-r--r-- | src/download_helper.cc | 41 | ||||
| -rw-r--r-- | src/prefs.cc | 2 | ||||
| -rw-r--r-- | src/prefs.h | 2 | ||||
| -rw-r--r-- | src/usage_text.h | 8 |
6 files changed, 67 insertions, 5 deletions
diff --git a/doc/manual-src/en/aria2c.rst b/doc/manual-src/en/aria2c.rst index 17caaa05..5a5bb3f3 100644 --- a/doc/manual-src/en/aria2c.rst +++ b/doc/manual-src/en/aria2c.rst @@ -713,6 +713,14 @@ BitTorrent Specific Options option to ``false``. This option has effect only on BitTorrent download. Default: ``true`` +.. option:: --bt-load-saved-metadata[=true|false] + + Before getting torrent metadata from DHT when downloading with + magnet link, first try to read file saved by + :option:`--bt-save-metadata` option. If it is successful, then skip + downloading metadata from DHT. + Default: ``false`` + .. option:: --bt-lpd-interface=<INTERFACE> Use given interface for Local Peer Discovery. If this option is not @@ -2111,6 +2119,7 @@ of URIs. These optional lines must start with white space(s). * :option:`bt-external-ip <--bt-external-ip>` * :option:`bt-force-encryption <--bt-force-encryption>` * :option:`bt-hash-check-seed <--bt-hash-check-seed>` + * :option:`bt-load-saved-metadata <--bt-load-saved-metadata>` * :option:`bt-max-peers <--bt-max-peers>` * :option:`bt-metadata-only <--bt-metadata-only>` * :option:`bt-min-crypto-level <--bt-min-crypto-level>` diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index b1721c80..6f352cd4 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -1528,6 +1528,16 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers() handlers.push_back(op); } { + OptionHandler* op(new BooleanOptionHandler( + PREF_BT_LOAD_SAVED_METADATA, TEXT_BT_LOAD_SAVED_METADATA, A2_V_FALSE, + OptionHandler::OPT_ARG)); + op->addTag(TAG_BITTORRENT); + op->setInitialOption(true); + op->setChangeGlobalOption(true); + op->setChangeOptionForReserved(true); + handlers.push_back(op); + } + { OptionHandler* op(new DefaultOptionHandler( PREF_BT_LPD_INTERFACE, TEXT_BT_LPD_INTERFACE, NO_DEFAULT_VALUE, "interface, IP address", OptionHandler::REQ_ARG)); diff --git a/src/download_helper.cc b/src/download_helper.cc index 9a19fe2f..7caf8c43 100644 --- a/src/download_helper.cc +++ b/src/download_helper.cc @@ -237,23 +237,54 @@ std::shared_ptr<RequestGroup> createBtMagnetRequestGroup(const std::string& magnetLink, const std::shared_ptr<Option>& optionTemplate) { - auto option = util::copy(optionTemplate); - auto gid = getGID(option); - auto rg = std::make_shared<RequestGroup>(gid, option); auto dctx = std::make_shared<DownloadContext>(METADATA_PIECE_SIZE, 0); // We only know info hash. Total Length is unknown at this moment. dctx->markTotalLengthIsUnknown(); - rg->setFileAllocationEnabled(false); - rg->setPreLocalFileCheckEnabled(false); + bittorrent::loadMagnet(magnetLink, dctx); auto torrentAttrs = bittorrent::getTorrentAttrs(dctx); + + if (optionTemplate->getAsBool(PREF_BT_LOAD_SAVED_METADATA)) { + // Try to read .torrent file saved by aria2 (see + // UTMetadataPostDownloadHandler and --bt-save-metadata option). + auto torrentFilename = + util::applyDir(optionTemplate->get(PREF_DIR), + util::toHex(torrentAttrs->infoHash) + ".torrent"); + + bittorrent::ValueBaseBencodeParser parser; + auto torrent = parseFile(parser, torrentFilename); + if (torrent) { + auto rg = createBtRequestGroup(torrentFilename, optionTemplate, {}, + torrent.get()); + const auto& actualInfoHash = + bittorrent::getTorrentAttrs(rg->getDownloadContext())->infoHash; + + if (torrentAttrs->infoHash == actualInfoHash) { + A2_LOG_NOTICE(fmt("BitTorrent metadata was loaded from %s", + torrentFilename.c_str())); + rg->setMetadataInfo(createMetadataInfo(rg->getGroupId(), magnetLink)); + return rg; + } + + A2_LOG_WARN( + fmt("BitTorrent metadata loaded from %s has unexpected infohash %s\n", + torrentFilename.c_str(), util::toHex(actualInfoHash).c_str())); + } + } + + auto option = util::copy(optionTemplate); bittorrent::adjustAnnounceUri(torrentAttrs, option); // torrentAttrs->name may contain "/", but we use basename of // FileEntry::getPath() to print out in-memory download entry. // Since "/" is treated as separator, we replace it with "-". dctx->getFirstFileEntry()->setPath( util::replace(torrentAttrs->name, "/", "-")); + + auto gid = getGID(option); + auto rg = std::make_shared<RequestGroup>(gid, option); + rg->setFileAllocationEnabled(false); + rg->setPreLocalFileCheckEnabled(false); rg->setDownloadContext(dctx); rg->clearPostDownloadHandler(); rg->addPostDownloadHandler( diff --git a/src/prefs.cc b/src/prefs.cc index 4f213f2c..1668dede 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -563,6 +563,8 @@ PrefPtr PREF_BT_FORCE_ENCRYPTION = makePref("bt-force-encryption"); // values: true | false PrefPtr PREF_BT_ENABLE_HOOK_AFTER_HASH_CHECK = makePref("bt-enable-hook-after-hash-check"); +// values: true | false +PrefPtr PREF_BT_LOAD_SAVED_METADATA = makePref("bt-load-saved-metadata"); /** * Metalink related preferences diff --git a/src/prefs.h b/src/prefs.h index 91fa8769..0bc7392f 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -514,6 +514,8 @@ extern PrefPtr PREF_BT_DETACH_SEED_ONLY; extern PrefPtr PREF_BT_FORCE_ENCRYPTION; // values: true | false extern PrefPtr PREF_BT_ENABLE_HOOK_AFTER_HASH_CHECK; +// values: true | false +extern PrefPtr PREF_BT_LOAD_SAVED_METADATA; /** * Metalink related preferences diff --git a/src/usage_text.h b/src/usage_text.h index 19817bcd..b957a878 100644 --- a/src/usage_text.h +++ b/src/usage_text.h @@ -1117,4 +1117,12 @@ " number of unfinished download result to keep. If\n" \ " that is undesirable, turn this option off.") +#define TEXT_BT_LOAD_SAVED_METADATA \ + _(" --bt-load-saved-metadata[=true|false]\n" \ + " Before getting torrent metadata from DHT when\n" \ + " downloading with magnet link, first try to read\n" \ + " file saved by --bt-save-metadata option. If it is\n" \ + " successful, then skip downloading metadata from\n" \ + " DHT.") + // clang-format on |
