From 4c8917048804cbe82febfadcf0b1747ecd888333 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 11 Jan 2010 14:01:20 +0000 Subject: [PATCH] 2010-01-11 Tatsuhiro Tsujikawa Treat --dir="" as --dir="." * src/FtpNegotiationCommand.cc * src/HttpResponseCommand.cc * src/Metalink2RequestGroup.cc * src/UTMetadataPostDownloadHandler.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/util.cc * src/util.h * test/UtilTest.cc --- ChangeLog | 13 +++++++++++++ src/FtpNegotiationCommand.cc | 4 ++-- src/HttpResponseCommand.cc | 4 ++-- src/Metalink2RequestGroup.cc | 2 +- src/UTMetadataPostDownloadHandler.cc | 19 +++++++++---------- src/bittorrent_helper.cc | 6 +++--- src/download_helper.cc | 4 ++-- src/util.cc | 11 +++++++++++ src/util.h | 7 +++++++ test/UtilTest.cc | 10 ++++++++++ 10 files changed, 60 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 41ac6e9b..bccbb6d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2010-01-11 Tatsuhiro Tsujikawa + + Treat --dir="" as --dir="." + * src/FtpNegotiationCommand.cc + * src/HttpResponseCommand.cc + * src/Metalink2RequestGroup.cc + * src/UTMetadataPostDownloadHandler.cc + * src/bittorrent_helper.cc + * src/download_helper.cc + * src/util.cc + * src/util.h + * test/UtilTest.cc + 2010-01-10 Tatsuhiro Tsujikawa Code clean up. diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index e660efe2..c1a5e9df 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -336,8 +336,8 @@ bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength) _fileEntry->setLength(totalLength); if(_fileEntry->getPath().empty()) { _fileEntry->setPath - (strconcat(getDownloadContext()->getDir(), - "/", util::urldecode(req->getFile()))); + (util::applyDir + (getDownloadContext()->getDir(), util::urldecode(req->getFile()))); } _requestGroup->preDownloadProcessing(); if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) { diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index ca96d488..2af07bce 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -131,8 +131,8 @@ bool HttpResponseCommand::executeInternal() _fileEntry->setLength(totalLength); if(_fileEntry->getPath().empty()) { _fileEntry->setPath - (strconcat(getDownloadContext()->getDir(), - "/", httpResponse->determinFilename())); + (util::applyDir + (getDownloadContext()->getDir(), httpResponse->determinFilename())); } _fileEntry->setContentType(httpResponse->getContentType()); _requestGroup->preDownloadProcessing(); diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc index 8ecf109e..d04b4cba 100644 --- a/src/Metalink2RequestGroup.cc +++ b/src/Metalink2RequestGroup.cc @@ -218,7 +218,7 @@ Metalink2RequestGroup::createRequestGroup (new DownloadContext (pieceLength, entry->getLength(), - strconcat(option->get(PREF_DIR), "/", entry->file->getPath()))); + util::applyDir(option->get(PREF_DIR), entry->file->getPath()))); dctx->setDir(option->get(PREF_DIR)); dctx->getFirstFileEntry()->setUris(uris); if(option->getAsBool(PREF_METALINK_ENABLE_UNIQUE_PROTOCOL)) { diff --git a/src/UTMetadataPostDownloadHandler.cc b/src/UTMetadataPostDownloadHandler.cc index fd67cf95..d3230ad2 100644 --- a/src/UTMetadataPostDownloadHandler.cc +++ b/src/UTMetadataPostDownloadHandler.cc @@ -82,23 +82,22 @@ void UTMetadataPostDownloadHandler::getNextRequestGroups util::toString(requestGroup->getPieceStorage()->getDiskAdaptor()); std::string torrent = bittorrent::metadata2Torrent(metadata, attrs); - std::deque > newRgs; - createRequestGroupForBitTorrent(newRgs, requestGroup->getOption(), - std::deque(), torrent); - requestGroup->followedBy(newRgs.begin(), newRgs.end()); - groups.insert(groups.end(), newRgs.begin(), newRgs.end()); - if(requestGroup->getOption()->getAsBool(PREF_BT_SAVE_METADATA)) { - std::string filename = requestGroup->getOption()->get(PREF_DIR); - filename += A2STR::SLASH_C; - filename += util::toHex(attrs[bittorrent::INFO_HASH].s()); - filename += ".torrent"; + std::string filename = + util::applyDir(requestGroup->getOption()->get(PREF_DIR), + util::toHex(attrs[bittorrent::INFO_HASH].s())+".torrent"); if(util::saveAs(filename, torrent)) { _logger->notice(MSG_METADATA_SAVED, filename.c_str()); } else { _logger->notice(MSG_METADATA_NOT_SAVED, filename.c_str()); } } + std::deque > newRgs; + createRequestGroupForBitTorrent(newRgs, requestGroup->getOption(), + std::deque(), torrent); + + requestGroup->followedBy(newRgs.begin(), newRgs.end()); + groups.insert(groups.end(), newRgs.begin(), newRgs.end()); } } // namespace aria2 diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 7c523f2f..fe2365f7 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -260,7 +260,7 @@ static void extractFileEntries std::deque uris; createUri(urlList.begin(), urlList.end(), std::back_inserter(uris), path); SharedHandle fileEntry - (new FileEntry(strconcat(ctx->getDir(), "/", path), + (new FileEntry(util::applyDir(ctx->getDir(), path), fileLengthData.i(), offset, uris)); fileEntries.push_back(fileEntry); @@ -289,13 +289,13 @@ static void extractFileEntries } SharedHandle fileEntry - (new FileEntry(strconcat(ctx->getDir(), "/", name),totalLength, 0, + (new FileEntry(util::applyDir(ctx->getDir(), name),totalLength, 0, uris)); fileEntries.push_back(fileEntry); } ctx->setFileEntries(fileEntries.begin(), fileEntries.end()); if(torrent[MODE].s() == MULTI) { - ctx->setBasePath(strconcat(ctx->getDir(), "/", name)); + ctx->setBasePath(util::applyDir(ctx->getDir(), name)); } } diff --git a/src/download_helper.cc b/src/download_helper.cc index bf02bf19..bbec38fd 100644 --- a/src/download_helper.cc +++ b/src/download_helper.cc @@ -190,7 +190,7 @@ static SharedHandle createRequestGroup (option->getAsInt(PREF_SEGMENT_SIZE), 0, useOutOption&&!option->blank(PREF_OUT)? - strconcat(option->get(PREF_DIR), "/", option->get(PREF_OUT)):A2STR::NIL)); + util::applyDir(option->get(PREF_DIR), option->get(PREF_OUT)):A2STR::NIL)); dctx->setDir(option->get(PREF_DIR)); dctx->getFirstFileEntry()->setUris(uris); rg->setDownloadContext(dctx); @@ -223,7 +223,7 @@ createBtRequestGroup(const std::string& torrentFilePath, for(std::map::const_iterator i = indexPathMap.begin(); i != indexPathMap.end(); ++i) { dctx->setFilePathWithIndex - ((*i).first, strconcat(dctx->getDir(), "/", (*i).second)); + ((*i).first, util::applyDir(dctx->getDir(), (*i).second)); } rg->setDownloadContext(dctx); return rg; diff --git a/src/util.cc b/src/util.cc index 943353dc..515fb6a8 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1000,6 +1000,17 @@ bool saveAs return File(tempFilename).renameTo(filename); } +std::string applyDir(const std::string& dir, const std::string& relPath) +{ + if(dir.empty()) { + return strconcat(A2STR::DOT_C, A2STR::SLASH_C, relPath); + } else if(dir == A2STR::SLASH_C) { + return strconcat(A2STR::SLASH_C, relPath); + } else { + return strconcat(dir, A2STR::SLASH_C, relPath); + } +} + } // namespace util } // namespace aria2 diff --git a/src/util.h b/src/util.h index d9658e70..9ac4aea1 100644 --- a/src/util.h +++ b/src/util.h @@ -355,6 +355,13 @@ void generateRandomData(unsigned char* data, size_t length); bool saveAs (const std::string& filename, const std::string& data, bool overwrite=false); +// Prepend dir to relPath. If dir is empty, it prepends "." to relPath. +// +// dir = "/dir", relPath = "foo" => "/dir/foo" +// dir = "", relPath = "foo" => "./foo" +// dir = "/", relPath = "foo" => "/foo" +std::string applyDir(const std::string& dir, const std::string& relPath); + } // namespace util } // namespace aria2 diff --git a/test/UtilTest.cc b/test/UtilTest.cc index d8170ce6..47d13598 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -58,6 +58,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGenerateRandomData); CPPUNIT_TEST(testFromHex); CPPUNIT_TEST(testParsePrioritizePieceRange); + CPPUNIT_TEST(testApplyDir); CPPUNIT_TEST_SUITE_END(); private: @@ -104,6 +105,7 @@ public: void testGenerateRandomData(); void testFromHex(); void testParsePrioritizePieceRange(); + void testApplyDir(); }; @@ -900,4 +902,12 @@ void UtilTest::testParsePrioritizePieceRange() CPPUNIT_ASSERT(result.empty()); } +void UtilTest::testApplyDir() +{ + CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred")); + CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred")); + CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred")); + CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred")); +} + } // namespace aria2