浏览代码

Use BtFileMode for TorrentAttribute::mode

Tatsuhiro Tsujikawa 12 年之前
父节点
当前提交
4f5d26a0c7

+ 2 - 2
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<List> destAnnounceList = List::g();
   for(std::vector<std::vector<std::string> >::const_iterator l =

+ 4 - 1
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() {}

+ 2 - 1
src/TorrentAttribute.h

@@ -40,13 +40,14 @@
 #include <string>
 #include <vector>
 
+#include <aria2/aria2.h>
 #include "a2time.h"
 
 namespace aria2 {
 
 struct TorrentAttribute:public ContextAttribute {
   std::string name;
-  std::string mode;
+  BtFileMode mode;
   std::vector<std::vector<std::string> > announceList;
   std::vector<std::pair<std::string, uint16_t> > nodes;
   // raw hash value 20 bytes.

+ 1 - 3
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;
       }

+ 15 - 3
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<Dict>(*itr);
@@ -303,7 +303,7 @@ void extractFileEntries
     }
   } else {
     // single-file mode;
-    torrent->mode = SINGLE;
+    torrent->mode = BT_FILE_MODE_SINGLE;
     const Integer* lengthData = downcast<Integer>(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

+ 4 - 1
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<typename Output>
 void print(Output& o, const SharedHandle<DownloadContext>& dctx)
@@ -352,7 +355,7 @@ void print(Output& o, const SharedHandle<DownloadContext>& 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<std::vector<std::string> >::const_iterator tierIter =
         torrentAttrs->announceList.begin(),

+ 5 - 0
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
    */

+ 2 - 2
test/BittorrentHelperTest.cc

@@ -219,14 +219,14 @@ void BittorrentHelperTest::testGetFileModeMulti() {
   SharedHandle<DownloadContext> 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<DownloadContext> 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() {

+ 1 - 1
test/RpcMethodTest.cc

@@ -915,7 +915,7 @@ void RpcMethodTest::testGatherBitTorrentMetadata()
   SharedHandle<TorrentAttribute> 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);