diff --git a/doc/aria2c.1.asciidoc b/doc/aria2c.1.asciidoc index 0cdc5018..b41cde7a 100644 --- a/doc/aria2c.1.asciidoc +++ b/doc/aria2c.1.asciidoc @@ -574,6 +574,16 @@ BitTorrent Specific Options last SIZE bytes of each file. SIZE can include 'K' or 'M'(1K = 1024, 1M = 1024K). If SIZE is omitted, SIZE=1M is used. +[[aria2_optref_bt_remove_unselected_file]]*--bt-remove-unselected-file*[='true'|'false']:: + + Removes the unselected files when download is completed in + BitTorrent. To selecting files, use + *<>* option. If it is + not used, all files are assumed to be selected. Please use this + option with care because it will actually remove files from your + disk. + Default: 'false' + [[aria2_optref_bt_require_crypto]]*--bt-require-crypto*[='true'|'false']:: If true is given, aria2 doesn't accept and establish connection with legacy BitTorrent handshake(\19BitTorrent protocol). @@ -1564,6 +1574,7 @@ of URIs. These optional lines must start with white space(s). * *<>* * *<>* * *<>* +* *<>* * *<>* * *<>* * *<>* @@ -2833,6 +2844,7 @@ The following options are available for active downloads: * *<>* * *<>* +* *<>* * *<>* * *<>* diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index dad99665..a31804ae 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -1646,6 +1646,19 @@ OptionHandlerFactory::createOptionHandlers() op->setChangeOptionForReserved(true); handlers.push_back(op); } + { + SharedHandle op(new BooleanOptionHandler + (PREF_BT_REMOVE_UNSELECTED_FILE, + TEXT_BT_REMOVE_UNSELECTED_FILE, + A2_V_FALSE, + OptionHandler::OPT_ARG)); + op->addTag(TAG_BITTORRENT); + op->setInitialOption(true); + op->setChangeOption(true); + op->setChangeGlobalOption(true); + op->setChangeOptionForReserved(true); + handlers.push_back(op); + } { SharedHandle op(new UnitNumberOptionHandler (PREF_BT_REQUEST_PEER_SPEED_LIMIT, diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 1517f1ca..f315b944 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -78,6 +78,9 @@ #include "UriListParser.h" #include "SingletonHolder.h" #include "Notifier.h" +#ifdef ENABLE_BITTORRENT +# include "bittorrent_helper.h" +#endif // ENABLE_BITTORRENT namespace aria2 { @@ -359,6 +362,27 @@ public: static_cast(nextGroups.size()))); e_->getRequestGroupMan()->insertReservedGroup(0, nextGroups); } + // For in-memory download (e.g., Magnet URI), the + // FileEntry::getPath() does not return actual file path, so + // we don't remove it. + if(group->getOption()->getAsBool(PREF_BT_REMOVE_UNSELECTED_FILE) && + !group->inMemoryDownload() && + dctx->hasAttribute(bittorrent::BITTORRENT)) { + A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE, group->getGID())); + const std::vector >& files = + dctx->getFileEntries(); + for(std::vector >::const_iterator i = + files.begin(), eoi = files.end(); i != eoi; ++i) { + if(!(*i)->isRequested()) { + if(File((*i)->getPath()).remove()) { + A2_LOG_INFO(fmt(MSG_FILE_REMOVED, (*i)->getPath().c_str())); + } else { + A2_LOG_INFO(fmt(MSG_FILE_COULD_NOT_REMOVED, + (*i)->getPath().c_str())); + } + } + } + } } else { A2_LOG_NOTICE (fmt(_("Download GID#%lld not complete: %s"), diff --git a/src/message.h b/src/message.h index 18c76b30..53b2ea78 100644 --- a/src/message.h +++ b/src/message.h @@ -195,6 +195,9 @@ #define MSG_DIR_TRAVERSAL_DETECTED _("Detected directory traversal directive in %s") #define MSG_HASH_CHECK_NOT_DONE \ "File has already been downloaded but hash check has not been done yet." +#define MSG_REMOVING_UNSELECTED_FILE _("GID#%lld - Removing unselected file.") +#define MSG_FILE_REMOVED _("File %s removed.") +#define MSG_FILE_COULD_NOT_REMOVED _("File %s could not be removed.") #define EX_TIME_OUT _("Timeout.") #define EX_INVALID_CHUNK_SIZE _("Invalid chunk size.") diff --git a/src/prefs.cc b/src/prefs.cc index 4adf03ae..3473e7c2 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -497,6 +497,9 @@ const Pref* PREF_ON_BT_DOWNLOAD_COMPLETE = makePref("on-bt-download-complete"); const Pref* PREF_BT_TRACKER = makePref("bt-tracker"); // values: string const Pref* PREF_BT_EXCLUDE_TRACKER = makePref("bt-exclude-tracker"); +// values: true | false +const Pref* PREF_BT_REMOVE_UNSELECTED_FILE = + makePref("bt-remove-unselected-file"); /** * Metalink related preferences diff --git a/src/prefs.h b/src/prefs.h index bf6b8574..221c5c24 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -440,6 +440,8 @@ extern const Pref* PREF_ON_BT_DOWNLOAD_COMPLETE; extern const Pref* PREF_BT_TRACKER; // values: string extern const Pref* PREF_BT_EXCLUDE_TRACKER; +// values: true | false +extern const Pref* PREF_BT_REMOVE_UNSELECTED_FILE; /** * Metalink related preferences diff --git a/src/usage_text.h b/src/usage_text.h index f8ed7d6f..e762c4ce 100644 --- a/src/usage_text.h +++ b/src/usage_text.h @@ -867,3 +867,11 @@ " file contains a lot of URIs to download.\n" \ " If false is given, aria2 reads all URIs and\n" \ " options at startup.") +#define TEXT_BT_REMOVE_UNSELECTED_FILE \ + _(" --bt-remove-unselected-file[=true|false] Removes the unselected files when\n" \ + " download is completed in BitTorrent. To\n" \ + " selecting files, use --select-file option. If\n" \ + " it is not used, all files are assumed to be\n" \ + " selected. Please use this option with care\n" \ + " because it will actually remove files from\n" \ + " your disk.")