Browse Source

2008-07-13 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Added the ability to save signature when download is completed 
if
	signature is available. The filename of signature file is the 
path to
	download file followed by ".sig". If it already exists, then 
signature
	will not be saved.
	* src/RequestGroupMan.cc
	* src/Signature.cc
	* test/SignatureTest.cc
Tatsuhiro Tsujikawa 17 years ago
parent
commit
aec1e9e7e1
6 changed files with 97 additions and 9 deletions
  1. 10 0
      ChangeLog
  2. 19 1
      src/RequestGroupMan.cc
  3. 14 2
      src/Signature.cc
  4. 2 1
      test/Makefile.am
  5. 7 5
      test/Makefile.in
  6. 45 0
      test/SignatureTest.cc

+ 10 - 0
ChangeLog

@@ -1,3 +1,13 @@
+2008-07-13  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Added the ability to save signature when download is completed if
+	signature is available. The filename of signature file is the path to
+	download file followed by ".sig". If it already exists, then signature
+	will not be saved.
+	* src/RequestGroupMan.cc
+	* src/Signature.cc
+	* test/SignatureTest.cc
+
 2008-07-12  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Updated the descriptions for --bt-seed-unverified.

+ 19 - 1
src/RequestGroupMan.cc

@@ -42,6 +42,7 @@
 #include "message.h"
 #include "a2functional.h"
 #include "DownloadResult.h"
+#include "DownloadContext.h"
 #include <iomanip>
 #include <sstream>
 #include <ostream>
@@ -111,6 +112,23 @@ private:
   std::deque<SharedHandle<RequestGroup> >& _reservedGroups;
   std::deque<SharedHandle<DownloadResult> >& _downloadResults;
   Logger* _logger;
+
+  void saveSignature(const SharedHandle<RequestGroup>& group)
+  {
+    SharedHandle<Signature> sig =
+      group->getDownloadContext()->getSignature();
+    if(!sig.isNull() && !sig->getBody().empty()) {
+      // filename of signature file is the path to download file followed by
+      // ".sig".
+      std::string signatureFile = group->getFilePath()+".sig";
+      if(sig->save(signatureFile)) {
+	_logger->notice("Saved signature as %s", signatureFile.c_str());
+      } else {
+	_logger->notice("Saving signature as %s failed. Maybe file"
+			" already exists.", signatureFile.c_str());
+      }
+    }
+  }
 public:
   ProcessStoppedRequestGroup
   (std::deque<SharedHandle<RequestGroup> >& reservedGroups,
@@ -128,6 +146,7 @@ public:
 	  group->reportDownloadFinished();
 	  if(group->allDownloadFinished()) {
 	    group->getProgressInfoFile()->removeFile();
+	    saveSignature(group);
 	  } else {
 	    group->getProgressInfoFile()->save();
 	  }
@@ -148,7 +167,6 @@ public:
       _downloadResults.push_back(group->createDownloadResult());
     }
   }
-
 };
 
 class FindStoppedRequestGroup {

+ 14 - 2
src/Signature.cc

@@ -33,6 +33,8 @@
  */
 /* copyright --> */
 #include "Signature.h"
+#include "File.h"
+#include <fstream>
 
 namespace aria2 {
 
@@ -72,8 +74,18 @@ const std::string& Signature::getBody() const
 
 bool Signature::save(const std::string& filepath) const
 {
-  // TODO not yet implemented
-  return false;
+  if(File(filepath).exists()) {
+    return false;
+  }
+  std::ofstream out(filepath.c_str());
+  try {
+    out.exceptions(std::ios::failbit);
+    out << _body;
+    out.close();
+    return true;
+  } catch(const std::ios::failure& exception) {
+    return false;
+  }
 }
 
 } // namespace aria2

+ 2 - 1
test/Makefile.am

@@ -52,7 +52,8 @@ aria2c_SOURCES = AllTest.cc\
 	StringFormatTest.cc\
 	ExceptionTest.cc\
 	DownloadHandlerFactoryTest.cc\
-	ChunkedDecoderTest.cc
+	ChunkedDecoderTest.cc\
+	SignatureTest.cc
 
 if HAVE_LIBZ
 aria2c_SOURCES += GZipDecoderTest.cc

+ 7 - 5
test/Makefile.in

@@ -188,7 +188,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \
 	MultiFileAllocationIteratorTest.cc FixedNumberRandomizer.h \
 	ProtocolDetectorTest.cc StringFormatTest.cc ExceptionTest.cc \
 	DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \
-	GZipDecoderTest.cc MessageDigestHelperTest.cc \
+	SignatureTest.cc GZipDecoderTest.cc MessageDigestHelperTest.cc \
 	IteratableChunkChecksumValidatorTest.cc \
 	IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
 	BtBitfieldMessageTest.cc BtCancelMessageTest.cc \
@@ -352,8 +352,9 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) SocketCoreTest.$(OBJEXT) \
 	MultiFileAllocationIteratorTest.$(OBJEXT) \
 	ProtocolDetectorTest.$(OBJEXT) StringFormatTest.$(OBJEXT) \
 	ExceptionTest.$(OBJEXT) DownloadHandlerFactoryTest.$(OBJEXT) \
-	ChunkedDecoderTest.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
-	$(am__objects_3) $(am__objects_4)
+	ChunkedDecoderTest.$(OBJEXT) SignatureTest.$(OBJEXT) \
+	$(am__objects_1) $(am__objects_2) $(am__objects_3) \
+	$(am__objects_4)
 aria2c_OBJECTS = $(am_aria2c_OBJECTS)
 am__DEPENDENCIES_1 =
 aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
@@ -568,8 +569,8 @@ aria2c_SOURCES = AllTest.cc SocketCoreTest.cc array_funTest.cc \
 	MultiFileAllocationIteratorTest.cc FixedNumberRandomizer.h \
 	ProtocolDetectorTest.cc StringFormatTest.cc ExceptionTest.cc \
 	DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \
-	$(am__append_1) $(am__append_2) $(am__append_3) \
-	$(am__append_4)
+	SignatureTest.cc $(am__append_1) $(am__append_2) \
+	$(am__append_3) $(am__append_4)
 
 #aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
 #aria2c_LDFLAGS = ${CPPUNIT_LIBS}
@@ -774,6 +775,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SequenceTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ShareRatioSeedCriteriaTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SharedHandleTest.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SignatureTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileAllocationIteratorTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileDownloadContextTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingletonHolderTest.Po@am__quote@

+ 45 - 0
test/SignatureTest.cc

@@ -0,0 +1,45 @@
+#include "Signature.h"
+#include "Exception.h"
+#include "File.h"
+#include <iostream>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace aria2 {
+
+class SignatureTest:public CppUnit::TestFixture {
+
+  CPPUNIT_TEST_SUITE(SignatureTest);
+  CPPUNIT_TEST(testSave);
+  CPPUNIT_TEST_SUITE_END();
+public:
+  void setUp() {}
+
+  void tearDown() {}
+
+  void testSave();
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION(SignatureTest);
+
+void SignatureTest::testSave()
+{
+  Signature sig;
+  sig.setBody("SIGNATURE");
+  std::string filepath = "/tmp/aria2_SignatureTest_testSave";
+  File outfile(filepath);
+  if(outfile.exists()) {
+    outfile.remove();
+  } 
+  CPPUNIT_ASSERT(sig.save(filepath));
+  {
+    std::ifstream in(filepath.c_str());
+    std::string fileContent;
+    in >> fileContent;
+    CPPUNIT_ASSERT_EQUAL(sig.getBody(), fileContent);
+  }
+  // second attempt to save will fail because file already exists.
+  CPPUNIT_ASSERT(!sig.save(filepath));
+}
+
+} // namespace aria2