summaryrefslogtreecommitdiff
path: root/src/BtPostDownloadHandler.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2012-07-26 21:07:10 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2012-07-26 21:07:10 +0900
commit01e1fdaf495342ae8735ec74eb57f912fa67d535 (patch)
treee5573e8aa7f3786d8364c76cabf5397f7473ed2f /src/BtPostDownloadHandler.cc
parent7176107414f73d7fe0cfd7d5ce516aa41a2f0b22 (diff)
Parse torrent file on the fly if --follow-torrent=mem is given
In this changes, Bencode parser in bencode2 is removed and the request is delegated to BencodeParser.
Diffstat (limited to 'src/BtPostDownloadHandler.cc')
-rw-r--r--src/BtPostDownloadHandler.cc44
1 files changed, 35 insertions, 9 deletions
diff --git a/src/BtPostDownloadHandler.cc b/src/BtPostDownloadHandler.cc
index 00ecea28..c3faccfc 100644
--- a/src/BtPostDownloadHandler.cc
+++ b/src/BtPostDownloadHandler.cc
@@ -48,6 +48,10 @@
#include "DownloadContext.h"
#include "download_helper.h"
#include "fmt.h"
+#include "ValueBaseBencodeParser.h"
+#include "DiskWriter.h"
+#include "AbstractSingleDiskAdaptor.h"
+#include "BencodeDiskWriter.h"
namespace aria2 {
@@ -70,19 +74,41 @@ void BtPostDownloadHandler::getNextRequestGroups
{
A2_LOG_INFO(fmt("Generating RequestGroups for Torrent file %s",
requestGroup->getFirstFilePath().c_str()));
- std::string content;
- try {
- requestGroup->getPieceStorage()->getDiskAdaptor()->openExistingFile();
- content = util::toString(requestGroup->getPieceStorage()->getDiskAdaptor());
- requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile();
- } catch(Exception& e) {
- requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile();
- throw;
+ SharedHandle<ValueBase> torrent;
+ if(requestGroup->inMemoryDownload()) {
+ const SharedHandle<DiskWriter>& dw =
+ static_pointer_cast<AbstractSingleDiskAdaptor>
+ (requestGroup->getPieceStorage()->getDiskAdaptor())->getDiskWriter();
+ const SharedHandle<bittorrent::BencodeDiskWriter>& bdw =
+ static_pointer_cast<bittorrent::BencodeDiskWriter>(dw);
+ int error = bdw->finalize();
+ if(error == 0) {
+ torrent = bdw->getResult();
+ }
+ } else {
+ std::string content;
+ try {
+ requestGroup->getPieceStorage()->getDiskAdaptor()->openExistingFile();
+ content = util::toString(requestGroup->getPieceStorage()
+ ->getDiskAdaptor());
+ requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile();
+ } catch(Exception& e) {
+ requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile();
+ throw;
+ }
+ ssize_t error;
+ torrent = bittorrent::ValueBaseBencodeParser().parseFinal
+ (content.c_str(), content.size(), error);
+ }
+ if(!torrent) {
+ throw DL_ABORT_EX2("Could not parse BitTorrent metainfo",
+ error_code::BENCODE_PARSE_ERROR);
}
std::vector<SharedHandle<RequestGroup> > newRgs;
createRequestGroupForBitTorrent(newRgs, requestGroup->getOption(),
std::vector<std::string>(),
- content);
+ "",
+ torrent);
requestGroup->followedBy(newRgs.begin(), newRgs.end());
SharedHandle<MetadataInfo> mi =
createMetadataInfoFromFirstFileEntry(requestGroup->getDownloadContext());