From a9c76ed35e3b6025b68f85d8e1d1efd339e735f6 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 16 Oct 2012 00:18:34 +0900 Subject: [PATCH] Added --rpc-save-upload-metadata option If true is given, which is default, save the uploaded torrent or metalink metadata in the directory specified by --dir option. The filename consists of SHA1-hash hex string of metadata plus extension. For torrent, the extension is '.torrent'. For metalink, it is '.meta4'. If false is given to this option, the downloads added by aria2.addTorrent or aria2.addMetalink will not be saved by --save-session option. --- src/OptionHandlerFactory.cc | 11 +++++++ src/RpcMethodImpl.cc | 63 +++++++++++++++++++++---------------- src/prefs.cc | 2 ++ src/prefs.h | 2 ++ src/usage_text.h | 11 +++++++ test/RpcMethodTest.cc | 29 +++++++++++++++-- 6 files changed, 88 insertions(+), 30 deletions(-) diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index 562f947b..d0fe9f3e 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -792,6 +792,17 @@ std::vector OptionHandlerFactory::createOptionHandlers() op->addTag(TAG_RPC); handlers.push_back(op); } + { + OptionHandler* op(new BooleanOptionHandler + (PREF_RPC_SAVE_UPLOAD_METADATA, + TEXT_RPC_SAVE_UPLOAD_METADATA, + A2_V_TRUE, + OptionHandler::OPT_ARG)); + op->addTag(TAG_RPC); + op->setInitialOption(true); + op->setChangeGlobalOption(true); + handlers.push_back(op); + } { OptionHandler* op(new BooleanOptionHandler (PREF_RPC_SECURE, diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index adffb584..196a8324 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -278,19 +278,23 @@ SharedHandle AddTorrentRpcMethod::process bool posGiven = checkPosParam(posParam); size_t pos = posGiven ? posParam->i() : 0; - std::string filename = util::applyDir - (requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent"); - std::vector > result; - // Save uploaded data in order to save this download in - // --save-session file. - if(util::saveAs(filename, torrentParam->s(), true)) { - A2_LOG_INFO(fmt("Uploaded torrent data was saved as %s", filename.c_str())); - requestOption->put(PREF_TORRENT_FILE, filename); - } else { - A2_LOG_INFO(fmt("Uploaded torrent data was not saved." - " Failed to write file %s", filename.c_str())); - filename.clear(); + std::string filename; + if(requestOption->getAsBool(PREF_RPC_SAVE_UPLOAD_METADATA)) { + filename = util::applyDir + (requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent"); + // Save uploaded data in order to save this download in + // --save-session file. + if(util::saveAs(filename, torrentParam->s(), true)) { + A2_LOG_INFO(fmt("Uploaded torrent data was saved as %s", + filename.c_str())); + requestOption->put(PREF_TORRENT_FILE, filename); + } else { + A2_LOG_INFO(fmt("Uploaded torrent data was not saved." + " Failed to write file %s", filename.c_str())); + filename.clear(); + } } + std::vector > result; createRequestGroupForBitTorrent(result, requestOption, uris, filename, torrentParam->s()); @@ -325,22 +329,27 @@ SharedHandle AddMetalinkRpcMethod::process std::vector > result; #ifdef ENABLE_MESSAGE_DIGEST - // TODO RFC5854 Metalink has the extension .meta4 and Metalink - // Version 3 uses .metalink extension. We use .meta4 for both - // RFC5854 Metalink and Version 3. aria2 can detect which of which - // by reading content rather than extension. - std::string filename = util::applyDir - (requestOption->get(PREF_DIR), getHexSha1(metalinkParam->s())+".meta4"); - // Save uploaded data in order to save this download in - // --save-session file. - if(util::saveAs(filename, metalinkParam->s(), true)) { - A2_LOG_INFO(fmt("Uploaded metalink data was saved as %s", - filename.c_str())); - requestOption->put(PREF_METALINK_FILE, filename); - createRequestGroupForMetalink(result, requestOption); + std::string filename; + if(requestOption->getAsBool(PREF_RPC_SAVE_UPLOAD_METADATA)) { + // TODO RFC5854 Metalink has the extension .meta4 and Metalink + // Version 3 uses .metalink extension. We use .meta4 for both + // RFC5854 Metalink and Version 3. aria2 can detect which of which + // by reading content rather than extension. + filename = util::applyDir + (requestOption->get(PREF_DIR), getHexSha1(metalinkParam->s())+".meta4"); + // Save uploaded data in order to save this download in + // --save-session file. + if(util::saveAs(filename, metalinkParam->s(), true)) { + A2_LOG_INFO(fmt("Uploaded metalink data was saved as %s", + filename.c_str())); + requestOption->put(PREF_METALINK_FILE, filename); + createRequestGroupForMetalink(result, requestOption); + } else { + A2_LOG_INFO(fmt("Uploaded metalink data was not saved." + " Failed to write file %s", filename.c_str())); + createRequestGroupForMetalink(result, requestOption, metalinkParam->s()); + } } else { - A2_LOG_INFO(fmt("Uploaded metalink data was not saved." - " Failed to write file %s", filename.c_str())); createRequestGroupForMetalink(result, requestOption, metalinkParam->s()); } #else // !ENABLE_MESSAGE_DIGEST diff --git a/src/prefs.cc b/src/prefs.cc index 70a9a0c2..052e4526 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -277,6 +277,8 @@ const Pref* PREF_RPC_PRIVATE_KEY = makePref("rpc-private-key"); // value: true | false const Pref* PREF_RPC_SECURE = makePref("rpc-secure"); // value: true | false +const Pref* PREF_RPC_SAVE_UPLOAD_METADATA = makePref("rpc-save-upload-metadata"); +// value: true | false const Pref* PREF_DRY_RUN = makePref("dry-run"); // value: true | false const Pref* PREF_REUSE_URI = makePref("reuse-uri"); diff --git a/src/prefs.h b/src/prefs.h index 7339b566..47a3aced 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -220,6 +220,8 @@ extern const Pref* PREF_RPC_PRIVATE_KEY; // value: true | false extern const Pref* PREF_RPC_SECURE; // value: true | false +extern const Pref* PREF_RPC_SAVE_UPLOAD_METADATA; +// value: true | false extern const Pref* PREF_DRY_RUN; // value: true | false extern const Pref* PREF_REUSE_URI; diff --git a/src/usage_text.h b/src/usage_text.h index a3c50e02..11d0baf7 100644 --- a/src/usage_text.h +++ b/src/usage_text.h @@ -898,3 +898,14 @@ " scheme. Use --rpc-certificate and\n" \ " --rpc-private-key options to specify the\n" \ " server certificate and private key.") +#define TEXT_RPC_SAVE_UPLOAD_METADATA \ + _(" --rpc-save-upload-metadata[=true|false] Save the uploaded torrent or\n" \ + " metalink metadata in the directory specified\n" \ + " by --dir option. The filename consists of\n" \ + " SHA1-hash hex string of metadata plus\n" \ + " extension. For torrent, the extension is\n" \ + " '.torrent'. For metalink, it is '.meta4'.\n" \ + " If false is given to this option, the\n" \ + " downloads added by aria2.addTorrent or\n" \ + " aria2.addMetalink will not be saved by\n" \ + " --save-session option.") diff --git a/test/RpcMethodTest.cc b/test/RpcMethodTest.cc index aeda43db..a095993a 100644 --- a/test/RpcMethodTest.cc +++ b/test/RpcMethodTest.cc @@ -264,13 +264,23 @@ void RpcMethodTest::testAddTorrent() SharedHandle uris = List::g(); uris->append("http://localhost/aria2-0.8.2.tar.bz2"); req.params->append(uris); + { + // Saving upload metadata is disabled by option. + RpcResponse res = m.execute(req, e_.get()); + CPPUNIT_ASSERT + (!File(e_->getOption()->get(PREF_DIR)+ + "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists()); + CPPUNIT_ASSERT_EQUAL(0, res.code); + CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast(res.param)->s()); + } + e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE); { RpcResponse res = m.execute(req, e_.get()); CPPUNIT_ASSERT (File(e_->getOption()->get(PREF_DIR)+ "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists()); CPPUNIT_ASSERT_EQUAL(0, res.code); - CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast(res.param)->s()); + CPPUNIT_ASSERT_EQUAL(std::string("2"), downcast(res.param)->s()); SharedHandle group = e_->getRequestGroupMan()->findReservedGroup(1); @@ -296,7 +306,7 @@ void RpcMethodTest::testAddTorrent() CPPUNIT_ASSERT_EQUAL(0, res.code); CPPUNIT_ASSERT_EQUAL (dir+"/aria2-0.8.2.tar.bz2", - e_->getRequestGroupMan()->findReservedGroup(2)->getFirstFilePath()); + e_->getRequestGroupMan()->findReservedGroup(3)->getFirstFilePath()); CPPUNIT_ASSERT (File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists()); } @@ -352,12 +362,25 @@ void RpcMethodTest::testAddMetalink() RpcRequest req(AddMetalinkRpcMethod::getMethodName(), List::g()); req.params->append(readFile(A2_TEST_DIR"/2files.metalink")); { + // Saving upload metadata is disabled by option. RpcResponse res = m.execute(req, e_.get()); CPPUNIT_ASSERT_EQUAL(0, res.code); const List* resParams = downcast(res.param); CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size()); CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast(resParams->get(0))->s()); CPPUNIT_ASSERT_EQUAL(std::string("2"), downcast(resParams->get(1))->s()); + CPPUNIT_ASSERT + (!File(e_->getOption()->get(PREF_DIR)+ + "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists()); + } + e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE); + { + RpcResponse res = m.execute(req, e_.get()); + CPPUNIT_ASSERT_EQUAL(0, res.code); + const List* resParams = downcast(res.param); + CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size()); + CPPUNIT_ASSERT_EQUAL(std::string("3"), downcast(resParams->get(0))->s()); + CPPUNIT_ASSERT_EQUAL(std::string("4"), downcast(resParams->get(1))->s()); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT (File(e_->getOption()->get(PREF_DIR)+ @@ -386,7 +409,7 @@ void RpcMethodTest::testAddMetalink() RpcResponse res = m.execute(req, e_.get()); CPPUNIT_ASSERT_EQUAL(0, res.code); CPPUNIT_ASSERT_EQUAL(dir+"/aria2-5.0.0.tar.bz2", - e_->getRequestGroupMan()->findReservedGroup(3)-> + e_->getRequestGroupMan()->findReservedGroup(5)-> getFirstFilePath()); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT