Prechádzať zdrojové kódy

Add bittorrent key to aria2.tellStopped status

Tatsuhiro Tsujikawa 9 rokov pred
rodič
commit
ca634a82bd

+ 12 - 7
src/DownloadContext.cc

@@ -153,24 +153,23 @@ void DownloadContext::setFileFilter(SegList<int> sgl)
 }
 
 void DownloadContext::setAttribute(ContextAttributeType key,
-                                   std::unique_ptr<ContextAttribute> value)
+                                   std::shared_ptr<ContextAttribute> value)
 {
   assert(key < MAX_CTX_ATTR);
   attrs_[key] = std::move(value);
 }
 
-const std::unique_ptr<ContextAttribute>&
+const std::shared_ptr<ContextAttribute>&
 DownloadContext::getAttribute(ContextAttributeType key)
 {
   assert(key < MAX_CTX_ATTR);
-  const std::unique_ptr<ContextAttribute>& attr = attrs_[key];
-  if (attr) {
-    return attr;
-  }
-  else {
+  const auto& attr = attrs_[key];
+  if (!attr) {
     throw DL_ABORT_EX(
         fmt("No attribute named %s", strContextAttributeType(key)));
   }
+
+  return attr;
 }
 
 bool DownloadContext::hasAttribute(ContextAttributeType key) const
@@ -179,6 +178,12 @@ bool DownloadContext::hasAttribute(ContextAttributeType key) const
   return attrs_[key].get();
 }
 
+const std::vector<std::shared_ptr<ContextAttribute>>&
+DownloadContext::getAttributes() const
+{
+  return attrs_;
+}
+
 void DownloadContext::releaseRuntimeResource()
 {
   for (std::vector<std::shared_ptr<FileEntry>>::const_iterator

+ 5 - 3
src/DownloadContext.h

@@ -61,7 +61,7 @@ private:
 
   RequestGroup* ownerRequestGroup_;
 
-  std::vector<std::unique_ptr<ContextAttribute>> attrs_;
+  std::vector<std::shared_ptr<ContextAttribute>> attrs_;
 
   std::vector<std::shared_ptr<FileEntry>> fileEntries_;
 
@@ -197,13 +197,15 @@ public:
   void setChecksumVerified(bool f) { checksumVerified_ = f; }
 
   void setAttribute(ContextAttributeType key,
-                    std::unique_ptr<ContextAttribute> value);
+                    std::shared_ptr<ContextAttribute> value);
 
-  const std::unique_ptr<ContextAttribute>&
+  const std::shared_ptr<ContextAttribute>&
   getAttribute(ContextAttributeType key);
 
   bool hasAttribute(ContextAttributeType key) const;
 
+  const std::vector<std::shared_ptr<ContextAttribute>>& getAttributes() const;
+
   void resetDownloadStartTime();
 
   void resetDownloadStopTime();

+ 5 - 2
src/DownloadResult.h

@@ -45,6 +45,7 @@
 
 #include "error_code.h"
 #include "RequestGroup.h"
+#include "ContextAttribute.h"
 
 namespace aria2 {
 
@@ -73,6 +74,8 @@ struct DownloadResult {
 
   std::shared_ptr<MetadataInfo> metadataInfo;
 
+  std::vector<std::shared_ptr<ContextAttribute>> attrs;
+
   std::vector<std::shared_ptr<FileEntry>> fileEntries;
 
   // This field contains GIDs. See comment in
@@ -102,8 +105,8 @@ struct DownloadResult {
   ~DownloadResult();
 
   // Don't allow copying
-  DownloadResult(const DownloadResult& c);
-  DownloadResult& operator=(const DownloadResult& c);
+  DownloadResult(const DownloadResult& c) = delete;
+  DownloadResult& operator=(const DownloadResult& c) = delete;
 };
 
 } // namespace aria2

+ 1 - 0
src/RequestGroup.cc

@@ -1110,6 +1110,7 @@ std::shared_ptr<DownloadResult> RequestGroup::createDownloadResult() const
   TransferStat st = calculateStat();
   auto res = std::make_shared<DownloadResult>();
   res->gid = gid_;
+  res->attrs = downloadContext_->getAttributes();
   res->fileEntries = downloadContext_->getFileEntries();
   res->inMemoryDownload = inMemoryDownload_;
   res->sessionDownloadLength = st.sessionDownloadLength;

+ 12 - 0
src/RpcMethodImpl.cc

@@ -907,6 +907,18 @@ void gatherStoppedDownload(Dict* entryDict,
   if (requested_key(keys, KEY_DIR)) {
     entryDict->put(KEY_DIR, ds->dir);
   }
+
+#ifdef ENABLE_BITTORRENT
+  if (ds->attrs.size() > CTX_ATTR_BT && ds->attrs[CTX_ATTR_BT]) {
+    const auto attrs =
+        static_cast<TorrentAttribute*>(ds->attrs[CTX_ATTR_BT].get());
+    if (requested_key(keys, KEY_BITTORRENT)) {
+      auto btDict = Dict::g();
+      gatherBitTorrentMetadata(btDict.get(), attrs);
+      entryDict->put(KEY_BITTORRENT, std::move(btDict));
+    }
+  }
+#endif // ENABLE_BITTORRENT
 }
 
 std::unique_ptr<ValueBase> GetFilesRpcMethod::process(const RpcRequest& req,

+ 2 - 2
src/TorrentAttribute.h

@@ -64,8 +64,8 @@ struct TorrentAttribute : public ContextAttribute {
   ~TorrentAttribute();
 
   // Don't allow copying
-  TorrentAttribute(const TorrentAttribute&);
-  TorrentAttribute& operator=(const TorrentAttribute&);
+  TorrentAttribute(const TorrentAttribute&) = delete;
+  TorrentAttribute& operator=(const TorrentAttribute&) = delete;
 };
 
 } // namespace aria2

+ 1 - 1
src/ValueBase.cc

@@ -165,7 +165,7 @@ void Dict::put(std::string key, std::unique_ptr<ValueBase> vlb)
   auto p = std::make_pair(std::move(key), std::move(vlb));
   auto r = dict_.insert(std::move(p));
   if (!r.second) {
-    (*r.first).second = std::move(vlb);
+    (*r.first).second = std::move(p.second);
   }
 }
 

+ 1 - 1
src/bittorrent_helper.cc

@@ -424,7 +424,7 @@ void processRootDictionary(const std::shared_ptr<DownloadContext>& ctx,
     throw DL_ABORT_EX2(fmt(MSG_MISSING_BT_INFO, C_INFO),
                        error_code::BITTORRENT_PARSE_ERROR);
   }
-  auto torrent = make_unique<TorrentAttribute>();
+  auto torrent = std::make_shared<TorrentAttribute>();
 
   // retrieve infoHash
   std::string encodedInfoDict = bencode2::encode(infoDict);

+ 29 - 0
test/RpcMethodTest.cc

@@ -67,6 +67,9 @@ class RpcMethodTest : public CppUnit::TestFixture {
   CPPUNIT_TEST(testGetVersion);
   CPPUNIT_TEST(testNoSuchMethod);
   CPPUNIT_TEST(testGatherStoppedDownload);
+#ifdef ENABLE_BITTORRENT
+  CPPUNIT_TEST(testGatherStoppedDownload_bt);
+#endif // ENABLE_BITTORRENT
   CPPUNIT_TEST(testGatherProgressCommon);
 #ifdef ENABLE_BITTORRENT
   CPPUNIT_TEST(testGatherBitTorrentMetadata);
@@ -134,6 +137,9 @@ public:
   void testGetVersion();
   void testNoSuchMethod();
   void testGatherStoppedDownload();
+#ifdef ENABLE_BITTORRENT
+  void testGatherStoppedDownload_bt();
+#endif // ENABLE_BITTORRENT
   void testGatherProgressCommon();
 #ifdef ENABLE_BITTORRENT
   void testGatherBitTorrentMetadata();
@@ -950,6 +956,29 @@ void RpcMethodTest::testGatherStoppedDownload()
   CPPUNIT_ASSERT(entry->containsKey("gid"));
 }
 
+#ifdef ENABLE_BITTORRENT
+void RpcMethodTest::testGatherStoppedDownload_bt()
+{
+  auto d = std::make_shared<DownloadResult>();
+  d->gid = GroupId::create();
+  d->infoHash = "2089b05ecca3d829cee5497d2703803b52216d19";
+  d->attrs = std::vector<std::shared_ptr<ContextAttribute>>(MAX_CTX_ATTR);
+
+  auto torrentAttr = std::make_shared<TorrentAttribute>();
+  torrentAttr->creationDate = 1000000007;
+  d->attrs[CTX_ATTR_BT] = torrentAttr;
+
+  auto entry = Dict::g();
+  gatherStoppedDownload(entry.get(), d, {});
+
+  auto btDict = downcast<Dict>(entry->get("bittorrent"));
+  CPPUNIT_ASSERT(btDict);
+
+  CPPUNIT_ASSERT_EQUAL((int64_t)1000000007,
+                       downcast<Integer>(btDict->get("creationDate"))->i());
+}
+#endif // ENABLE_BITTORRENT
+
 void RpcMethodTest::testGatherProgressCommon()
 {
   auto dctx = std::make_shared<DownloadContext>(0, 0, "aria2.tar.bz2");