Forráskód Böngészése

2008-05-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	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
Tatsuhiro Tsujikawa 17 éve
szülő
commit
874714609f

+ 16 - 0
ChangeLog

@@ -1,3 +1,19 @@
+2008-05-05  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	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  <tujikawa at rednoah dot com>
 
 	Change the default value of --metalink-servers option from `5' to `1'.

+ 3 - 0
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<BtProgressInfoFile> BtProgressInfoFileHandle;

+ 13 - 4
src/DefaultBtProgressInfoFile.cc

@@ -58,19 +58,28 @@
 
 namespace aria2 {
 
+static std::string createFilename(const SharedHandle<DownloadContext>& 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<BtContext>(_dctx).isNull();

+ 3 - 0
src/DefaultBtProgressInfoFile.h

@@ -71,6 +71,9 @@ public:
 
   virtual void removeFile();
 
+  // re-set filename using current _dctx.
+  virtual void updateFilename();
+
 };
 
 } // namespace aria2

+ 2 - 0
src/NullProgressInfoFile.h

@@ -55,6 +55,8 @@ public:
   virtual void load() {}
 
   virtual void removeFile() {}
+
+  virtual void updateFilename() {}
 };
 
 typedef SharedHandle<NullProgressInfoFile> NullProgressInfoFileHandle;

+ 43 - 27
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<SingleFileDownloadContext>(_downloadContext);
+  // Make a copy of ctx.
+  SingleFileDownloadContextHandle tempCtx(new SingleFileDownloadContext(*ctx.get()));
+
+  DefaultBtProgressInfoFile tempInfoFile(tempCtx, SharedHandle<PieceStorage>(), 0);
+
   for(unsigned int i = 1; i < 10000; ++i) {
     File newfile(filepath+"."+Util::uitos(i));
-    if(!newfile.exists()) {
-      SingleFileDownloadContextHandle ctx =
-	dynamic_pointer_cast<SingleFileDownloadContext>(_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;
     }
   }

+ 19 - 0
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<MockBtContext> _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<SingleFileDownloadContext> dctx
+    (new SingleFileDownloadContext(1024, 81920, "file1"));
+
+  DefaultBtProgressInfoFile infoFile(dctx, SharedHandle<MockPieceStorage>(), 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

+ 2 - 0
test/MockBtProgressInfoFile.h

@@ -29,6 +29,8 @@ public:
   virtual void load() {}
 
   virtual void removeFile() {}
+
+  virtual void updateFilename() {}
 };
 
 } // namespace aria2