diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index fa6d5a3b..3c7f386f 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -706,8 +706,8 @@ void gatherBitTorrentMetadata if(torrentAttrs->creationDate) { btDict->put(KEY_CREATION_DATE, Integer::g(torrentAttrs->creationDate)); } - if(!torrentAttrs->mode.empty()) { - btDict->put(KEY_MODE, torrentAttrs->mode); + if(torrentAttrs->mode) { + btDict->put(KEY_MODE, bittorrent::getModeString(torrentAttrs->mode)); } SharedHandle destAnnounceList = List::g(); for(std::vector >::const_iterator l = diff --git a/src/TorrentAttribute.cc b/src/TorrentAttribute.cc index 76a34bb1..c28c3dbe 100644 --- a/src/TorrentAttribute.cc +++ b/src/TorrentAttribute.cc @@ -37,7 +37,10 @@ namespace aria2 { TorrentAttribute::TorrentAttribute() - : metadataSize(0), privateTorrent(false), creationDate(0) + : mode(BT_FILE_MODE_NONE), + metadataSize(0), + privateTorrent(false), + creationDate(0) {} TorrentAttribute::~TorrentAttribute() {} diff --git a/src/TorrentAttribute.h b/src/TorrentAttribute.h index aba5e4e8..709e6ef4 100644 --- a/src/TorrentAttribute.h +++ b/src/TorrentAttribute.h @@ -40,13 +40,14 @@ #include #include +#include #include "a2time.h" namespace aria2 { struct TorrentAttribute:public ContextAttribute { std::string name; - std::string mode; + BtFileMode mode; std::vector > announceList; std::vector > nodes; // raw hash value 20 bytes. diff --git a/src/aria2api.cc b/src/aria2api.cc index 93028ca9..2e4702ff 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -600,9 +600,7 @@ struct RequestGroupDH : public DownloadHandle { res.announceList = torrentAttrs->announceList; res.comment = torrentAttrs->comment; res.creationDate = torrentAttrs->creationDate; - // TODO Use BtFileMode for torrentAttrs->mode - res.mode = torrentAttrs->mode == "single" ? - BT_FILE_MODE_SINGLE : BT_FILE_MODE_MULTI; + res.mode = torrentAttrs->mode; if(!torrentAttrs->metadata.empty()) { res.name = torrentAttrs->name; } diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 14429c33..7b85f64d 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -235,7 +235,7 @@ void extractFileEntries int64_t length = 0; int64_t offset = 0; // multi-file mode - torrent->mode = MULTI; + torrent->mode = BT_FILE_MODE_MULTI; for(List::ValueType::const_iterator itr = filesList->begin(), eoi = filesList->end(); itr != eoi; ++itr) { const Dict* fileDict = downcast(*itr); @@ -303,7 +303,7 @@ void extractFileEntries } } else { // single-file mode; - torrent->mode = SINGLE; + torrent->mode = BT_FILE_MODE_SINGLE; const Integer* lengthData = downcast(infoDict->get(C_LENGTH)); if(!lengthData) { throw DL_ABORT_EX2(fmt(MSG_MISSING_BT_INFO, C_LENGTH.c_str()), @@ -333,7 +333,7 @@ void extractFileEntries fileEntries.push_back(fileEntry); } ctx->setFileEntries(fileEntries.begin(), fileEntries.end()); - if(torrent->mode == MULTI) { + if(torrent->mode == BT_FILE_MODE_MULTI) { ctx->setBasePath(util::applyDir(option->get(PREF_DIR), util::escapePath(utf8Name))); } @@ -1076,6 +1076,18 @@ void adjustAnnounceUri addAnnounceUri(attrs, addUris); } +const char* getModeString(BtFileMode mode) +{ + switch(mode) { + case BT_FILE_MODE_SINGLE: + return "single"; + case BT_FILE_MODE_MULTI: + return "multi"; + default: + return ""; + } +} + } // namespace bittorrent } // namespace aria2 diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h index 95d0d6a6..ba19c79f 100644 --- a/src/bittorrent_helper.h +++ b/src/bittorrent_helper.h @@ -336,6 +336,9 @@ void extractPeer int getCompactLength(int family); +// Returns textual representation of the |mode|. +const char* getModeString(BtFileMode mode); + // Writes the detailed information about torrent loaded in dctx. template void print(Output& o, const SharedHandle& dctx) @@ -352,7 +355,7 @@ void print(Output& o, const SharedHandle& dctx) if(!torrentAttrs->createdBy.empty()) { o.printf("Created By: %s\n", torrentAttrs->createdBy.c_str()); } - o.printf("Mode: %s\n", torrentAttrs->mode.c_str()); + o.printf("Mode: %s\n", getModeString(torrentAttrs->mode)); o.write("Announce:\n"); for(std::vector >::const_iterator tierIter = torrentAttrs->announceList.begin(), diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index 4929576b..94ebe427 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -453,6 +453,11 @@ struct FileData { * BitTorrent file mode */ enum BtFileMode { + /** + * Indicating no mode. This value is used when file mode is not + * available. + */ + BT_FILE_MODE_NONE, /** * Indicating single file torrent */ diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 895e9ad8..fe4714e9 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -219,14 +219,14 @@ void BittorrentHelperTest::testGetFileModeMulti() { SharedHandle dctx(new DownloadContext()); load(A2_TEST_DIR"/test.torrent", dctx, option_); - CPPUNIT_ASSERT_EQUAL(MULTI, getTorrentAttrs(dctx)->mode); + CPPUNIT_ASSERT_EQUAL(BT_FILE_MODE_MULTI, getTorrentAttrs(dctx)->mode); } void BittorrentHelperTest::testGetFileModeSingle() { SharedHandle dctx(new DownloadContext()); load(A2_TEST_DIR"/single.torrent", dctx, option_); - CPPUNIT_ASSERT_EQUAL(SINGLE, getTorrentAttrs(dctx)->mode); + CPPUNIT_ASSERT_EQUAL(BT_FILE_MODE_SINGLE, getTorrentAttrs(dctx)->mode); } void BittorrentHelperTest::testGetNameMulti() { diff --git a/test/RpcMethodTest.cc b/test/RpcMethodTest.cc index 043a9ec8..4084d17f 100644 --- a/test/RpcMethodTest.cc +++ b/test/RpcMethodTest.cc @@ -915,7 +915,7 @@ void RpcMethodTest::testGatherBitTorrentMetadata() SharedHandle modBtAttrs = bittorrent::getTorrentAttrs(dctx); modBtAttrs->comment.clear(); modBtAttrs->creationDate = 0; - modBtAttrs->mode.clear(); + modBtAttrs->mode = BT_FILE_MODE_NONE; modBtAttrs->metadata.clear(); btDict = Dict::g(); gatherBitTorrentMetadata(btDict, modBtAttrs);