From 874714609fa53299c454d8b3c23f30816ca14ba0 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 5 May 2008 10:10:15 +0000 Subject: [PATCH] 2008-05-05 Tatsuhiro Tsujikawa Fixed the bug that the control file(.aria2 file) is not renamed according to tryAutoFileRenaming(). tryAutoFileRenaming() was rewritten so that if both renamed file and its control file exist, use them and continue download. The old implementation didn't take into account of control file's existence, so basically you couldn't continue download of renamed file. * src/BtProgressInfoFile.h * src/DefaultBtProgressInfoFile.cc * src/DefaultBtProgressInfoFile.h * src/NullProgressInfoFile.h * src/RequestGroup.cc * test/DefaultBtProgressInfoFileTest.cc * test/MockBtProgressInfoFile.h --- ChangeLog | 16 ++++++ src/BtProgressInfoFile.h | 3 ++ src/DefaultBtProgressInfoFile.cc | 17 +++++-- src/DefaultBtProgressInfoFile.h | 3 ++ src/NullProgressInfoFile.h | 2 + src/RequestGroup.cc | 70 ++++++++++++++++----------- test/DefaultBtProgressInfoFileTest.cc | 19 ++++++++ test/MockBtProgressInfoFile.h | 2 + 8 files changed, 101 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index d4760a57..0a0f48bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2008-05-05 Tatsuhiro Tsujikawa + + Fixed the bug that the control file(.aria2 file) is not renamed + according to tryAutoFileRenaming(). + tryAutoFileRenaming() was rewritten so that if both renamed file and + its control file exist, use them and continue download. + The old implementation didn't take into account of control file's + existence, so basically you couldn't continue download of renamed file. + * src/BtProgressInfoFile.h + * src/DefaultBtProgressInfoFile.cc + * src/DefaultBtProgressInfoFile.h + * src/NullProgressInfoFile.h + * src/RequestGroup.cc + * test/DefaultBtProgressInfoFileTest.cc + * test/MockBtProgressInfoFile.h + 2008-05-05 Tatsuhiro Tsujikawa Change the default value of --metalink-servers option from `5' to `1'. diff --git a/src/BtProgressInfoFile.h b/src/BtProgressInfoFile.h index 9ffc0b6e..94b8c9c2 100644 --- a/src/BtProgressInfoFile.h +++ b/src/BtProgressInfoFile.h @@ -54,6 +54,9 @@ public: virtual void load() = 0; virtual void removeFile() = 0; + + // re-set filename + virtual void updateFilename() = 0; }; typedef SharedHandle BtProgressInfoFileHandle; diff --git a/src/DefaultBtProgressInfoFile.cc b/src/DefaultBtProgressInfoFile.cc index 6cc2dc31..052eb474 100644 --- a/src/DefaultBtProgressInfoFile.cc +++ b/src/DefaultBtProgressInfoFile.cc @@ -58,19 +58,28 @@ namespace aria2 { +static std::string createFilename(const SharedHandle& dctx) +{ + return dctx->getActualBasePath()+".aria2"; +} + DefaultBtProgressInfoFile::DefaultBtProgressInfoFile(const DownloadContextHandle& dctx, const PieceStorageHandle& pieceStorage, const Option* option): _dctx(dctx), _pieceStorage(pieceStorage), _option(option), - _logger(LogFactory::getInstance()) -{ - _filename = _dctx->getActualBasePath()+".aria2"; -} + _logger(LogFactory::getInstance()), + _filename(createFilename(_dctx)) +{} DefaultBtProgressInfoFile::~DefaultBtProgressInfoFile() {} +void DefaultBtProgressInfoFile::updateFilename() +{ + _filename = createFilename(_dctx); +} + bool DefaultBtProgressInfoFile::isTorrentDownload() { return !dynamic_pointer_cast(_dctx).isNull(); diff --git a/src/DefaultBtProgressInfoFile.h b/src/DefaultBtProgressInfoFile.h index be7566e3..c7ab556c 100644 --- a/src/DefaultBtProgressInfoFile.h +++ b/src/DefaultBtProgressInfoFile.h @@ -71,6 +71,9 @@ public: virtual void removeFile(); + // re-set filename using current _dctx. + virtual void updateFilename(); + }; } // namespace aria2 diff --git a/src/NullProgressInfoFile.h b/src/NullProgressInfoFile.h index d4f58093..0eb09881 100644 --- a/src/NullProgressInfoFile.h +++ b/src/NullProgressInfoFile.h @@ -55,6 +55,8 @@ public: virtual void load() {} virtual void removeFile() {} + + virtual void updateFilename() {} }; typedef SharedHandle NullProgressInfoFileHandle; diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 82e8bc52..04dffaa9 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -364,36 +364,44 @@ void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoF progressInfoFile->getFilename().c_str(), _pieceStorage->getDiskAdaptor()->getFilePath().c_str()); } - if(progressInfoFile->exists()) { - progressInfoFile->load(); - _pieceStorage->getDiskAdaptor()->openExistingFile(); - } else { - File outfile(getFilePath()); - if(outfile.exists() && _option->get(PREF_CONTINUE) == V_TRUE) { - if(getTotalLength() < outfile.size()) { - throw DlAbortEx - (StringFormat(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE, - getFilePath().c_str(), - Util::itos(outfile.size()).c_str(), - Util::itos(getTotalLength()).c_str()).str()); - } + while(1) { + if(progressInfoFile->exists()) { + progressInfoFile->load(); _pieceStorage->getDiskAdaptor()->openExistingFile(); - _pieceStorage->markPiecesDone(outfile.size()); } else { -#ifdef ENABLE_MESSAGE_DIGEST - if(outfile.exists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) { + File outfile(getFilePath()); + if(outfile.exists() && _option->get(PREF_CONTINUE) == V_TRUE) { + if(getTotalLength() < outfile.size()) { + throw DlAbortEx + (StringFormat(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE, + getFilePath().c_str(), + Util::itos(outfile.size()).c_str(), + Util::itos(getTotalLength()).c_str()).str()); + } _pieceStorage->getDiskAdaptor()->openExistingFile(); + _pieceStorage->markPiecesDone(outfile.size()); } else { - shouldCancelDownloadForSafety(); - _pieceStorage->getDiskAdaptor()->initAndOpenFile(); - } -#else // ENABLE_MESSAGE_DIGEST - shouldCancelDownloadForSafety(); - _pieceStorage->getDiskAdaptor()->initAndOpenFile(); +#ifdef ENABLE_MESSAGE_DIGEST + if(outfile.exists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) { + _pieceStorage->getDiskAdaptor()->openExistingFile(); + } else { #endif // ENABLE_MESSAGE_DIGEST + shouldCancelDownloadForSafety(); + // call updateFilename here in case when filename is renamed + // by tryAutoFileRenaming() + progressInfoFile->updateFilename(); + if(progressInfoFile->exists()) { + continue; + } + _pieceStorage->getDiskAdaptor()->initAndOpenFile(); +#ifdef ENABLE_MESSAGE_DIGEST + } +#endif // ENABLE_MESSAGE_DIGEST + } } + setProgressInfoFile(progressInfoFile); + break; } - setProgressInfoFile(progressInfoFile); } catch(RecoverableException& e) { throw DownloadFailureException (StringFormat(EX_DOWNLOAD_ABORTED).str(), e); @@ -430,12 +438,20 @@ bool RequestGroup::tryAutoFileRenaming() if(filepath.empty()) { return false; } + SingleFileDownloadContextHandle ctx = + dynamic_pointer_cast(_downloadContext); + // Make a copy of ctx. + SingleFileDownloadContextHandle tempCtx(new SingleFileDownloadContext(*ctx.get())); + + DefaultBtProgressInfoFile tempInfoFile(tempCtx, SharedHandle(), 0); + for(unsigned int i = 1; i < 10000; ++i) { File newfile(filepath+"."+Util::uitos(i)); - if(!newfile.exists()) { - SingleFileDownloadContextHandle ctx = - dynamic_pointer_cast(_downloadContext); - ctx->setUFilename(newfile.getBasename()); + std::string newFilename = newfile.getBasename(); + tempCtx->setUFilename(newFilename); + tempInfoFile.updateFilename(); + if(!newfile.exists() || (newfile.exists() && tempInfoFile.exists())) { + ctx->setUFilename(newFilename); return true; } } diff --git a/test/DefaultBtProgressInfoFileTest.cc b/test/DefaultBtProgressInfoFileTest.cc index 42408385..b4a9216a 100644 --- a/test/DefaultBtProgressInfoFileTest.cc +++ b/test/DefaultBtProgressInfoFileTest.cc @@ -24,6 +24,7 @@ class DefaultBtProgressInfoFileTest:public CppUnit::TestFixture { CPPUNIT_TEST(testLoad); CPPUNIT_TEST(testLoad_nonBt); CPPUNIT_TEST(testLoad_nonBt_pieceLengthShorter); + CPPUNIT_TEST(testUpdateFilename); CPPUNIT_TEST_SUITE_END(); private: SharedHandle _btContext; @@ -77,6 +78,7 @@ public: void testSave_nonBt(); void testLoad_nonBt(); void testLoad_nonBt_pieceLengthShorter(); + void testUpdateFilename(); }; #undef BLOCK_LENGTH @@ -383,4 +385,21 @@ void DefaultBtProgressInfoFileTest::testSave() } +void DefaultBtProgressInfoFileTest::testUpdateFilename() +{ + SharedHandle dctx + (new SingleFileDownloadContext(1024, 81920, "file1")); + + DefaultBtProgressInfoFile infoFile(dctx, SharedHandle(), 0); + CPPUNIT_ASSERT_EQUAL(std::string("./file1.aria2"), infoFile.getFilename()); + + dctx->setUFilename("file1.1"); + + CPPUNIT_ASSERT_EQUAL(std::string("./file1.aria2"), infoFile.getFilename()); + + infoFile.updateFilename(); + + CPPUNIT_ASSERT_EQUAL(std::string("./file1.1.aria2"), infoFile.getFilename()); +} + } // namespace aria2 diff --git a/test/MockBtProgressInfoFile.h b/test/MockBtProgressInfoFile.h index 8676d749..7858636f 100644 --- a/test/MockBtProgressInfoFile.h +++ b/test/MockBtProgressInfoFile.h @@ -29,6 +29,8 @@ public: virtual void load() {} virtual void removeFile() {} + + virtual void updateFilename() {} }; } // namespace aria2