- make FileChecker use exceptions instead of bool
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 22 May 2007 16:04:20 +0000 (16:04 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 22 May 2007 16:04:20 +0000 (16:04 +0000)
- Move it out of Fetcher
- port..

zypp/CMakeLists.txt
zypp/Fetcher.cc
zypp/Fetcher.h
zypp/FileChecker.cc [new file with mode: 0644]
zypp/FileChecker.h [new file with mode: 0644]
zypp/source/yum/YUMDownloader.cc
zypp/source/yum/YUMResourceType.h

index 51e6559..c837437 100644 (file)
@@ -86,6 +86,7 @@ SET( zypp_SRCS
   ProvideFilePolicy.cc
   OnMediaLocation.cc
   Fetcher.cc
+  FileChecker.cc
 )
 
 SET( zypp_HEADERSZ
@@ -182,6 +183,7 @@ SET( zypp_HEADERSZ
   ZYppCommitPolicy.h
   ZYppCommitResult.h
   ZYppFactory.h
+  FileChecker.h
 )
 
 INSTALL(  FILES ${zypp_HEADERS} DESTINATION "${CMAKE_INSTALL_PREFIX}/include/zypp" )
index 66dd180..46ecdb2 100644 (file)
@@ -14,9 +14,7 @@
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/DefaultIntegral.h"
-#include "zypp/ZYppFactory.h"
 #include "zypp/Fetcher.h"
-#include "zypp/KeyRing.h"
 
 using namespace std;
 
@@ -24,96 +22,9 @@ using namespace std;
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
-  Fetcher::ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
-    : _checksum(checksum)
-  {
-  }
-
-  bool Fetcher::ChecksumFileChecker::operator()( const Pathname &file ) const
-  {
-    callback::SendReport<DigestReport> report;
-    CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
-    
-    if ( _checksum.empty() )
-    {
-      MIL << "File " <<  file << " has no checksum available." << std::endl;
-      if ( report->askUserToAcceptNoDigest(file) )
-      {
-        MIL << "User accepted " <<  file << " with no checksum." << std::endl;
-        return true;
-      }
-      else
-      {
-        return false;
-      }
-    }
-    else
-    {
-      if ( (real_checksum == _checksum) )
-      {
-        if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
-        {
-          WAR << "User accepted " <<  file << " with WRONG CHECKSUM." << std::endl;
-          return true;
-        }
-        else
-        {
-          return false;
-        }
-      }
-      else
-      {
-        return true;
-      }
-    }
-  }
-
-  bool Fetcher::NullFileChecker::operator()(const Pathname &file ) const
-  {
-    return true;
-  }
-
-  bool Fetcher::CompositeFileChecker::operator()(const Pathname &file ) const
-  {
-    bool result = true;
-    for ( list<Fetcher::FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
-    {
-      result = result && (*it)(file);
-    }
-    return result;
-  }
-  
-  void Fetcher::CompositeFileChecker::add( const FileChecker &checker )
-  {
-    _checkers.push_back(checker);
-  }
-
-  Fetcher::SignatureFileChecker::SignatureFileChecker( const Pathname &signature )
-    : _signature(signature)
-  {
-  }
-  
-  Fetcher::SignatureFileChecker::SignatureFileChecker()
-  {
-  }
-  
-  void Fetcher::SignatureFileChecker::addPublicKey( const Pathname &publickey )
-  {
-    ZYpp::Ptr z = getZYpp();
-    z->keyRing()->importKey(publickey, false);
-  }
-  
-  bool Fetcher::SignatureFileChecker::operator()(const Pathname &file ) const
-  {
-    ZYpp::Ptr z = getZYpp();
-    MIL << "checking " << file << " file validity using digital signature.." << endl;
-    bool valid = z->keyRing()->verifyFileSignatureWorkflow( file, string(), _signature);
-    return valid;
-  }
-  
   /**
    * Class to encapsulate the \ref OnMediaLocation object
-   * and the \ref Fetcher::FileChcker together
+   * and the \ref FileChcker together
    */
   struct FetcherJob
   {
@@ -124,7 +35,7 @@ namespace zypp
     }
 
     OnMediaLocation location;
-    Fetcher::CompositeFileChecker checkers;
+    CompositeFileChecker checkers;
   };
 
   ///////////////////////////////////////////////////////////////////
@@ -137,8 +48,8 @@ namespace zypp
 
   public:
 
-    void enqueue( const OnMediaLocation &resource, const Fetcher::FileChecker &checker  );
-    void enqueueDigested( const OnMediaLocation &resource, const Fetcher::FileChecker &checker );
+    void enqueue( const OnMediaLocation &resource, const FileChecker &checker  );
+    void enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker );
     void addCachePath( const Pathname &cache_dir );
     void reset();
     void start( const Pathname &dest_dir, MediaSetAccess &media );
@@ -275,13 +186,21 @@ namespace zypp
       // no matter where did we got the file, try to validate it:
        Pathname localfile = dest_dir + (*it_res).location.filename();
        // call the checker function
-       bool good = (*it_res).checkers(localfile);
-       if (!good)
+       try {
+         (*it_res).checkers(localfile);
+       }
+       catch ( const FileCheckException &e )
        {
-         //FIXME better message
-         ZYPP_THROW(Exception("File " + (*it_res).location.filename().asString() + " does not validate." ));
+          ZYPP_RETHROW(e);
+       }
+       catch ( const Exception &e )
+       {
+          ZYPP_RETHROW(e);
+       }
+       catch (...)
+       {
+          ZYPP_THROW(Exception("Unknown error while validating " + (*it_res).location.filename().asString()));
        }
-      
     } // for each job
   }
 
@@ -314,12 +233,12 @@ namespace zypp
   Fetcher::~Fetcher()
   {}
 
-  void Fetcher::enqueueDigested( const OnMediaLocation &resource, const Fetcher::FileChecker &checker )
+  void Fetcher::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
   {
     _pimpl->enqueue(resource, checker);
   }
   
-  void Fetcher::enqueue( const OnMediaLocation &resource, const Fetcher::FileChecker &checker  )
+  void Fetcher::enqueue( const OnMediaLocation &resource, const FileChecker &checker  )
   {
     _pimpl->enqueue(resource, checker);
   }
index 6b2f4d5..d7a340d 100644 (file)
@@ -21,6 +21,7 @@
 #include "zypp/OnMediaLocation.h"
 #include "zypp/Digest.h"
 #include "zypp/MediaSetAccess.h"
+#include "zypp/FileChecker.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -66,106 +67,6 @@ namespace zypp
   public:
     /** Implementation  */
     class Impl;
-
-  /**
-   * Functor signature used to check files.
-   * \param file File to check.
-   */
-  typedef boost::function<bool ( const Pathname &file )> FileChecker;
-  
-  /**
-   * Built in file checkers
-   */
-  
-  /**
-   * \short Checks for a valid checksum and interacts with the user.
-   */
-   class ChecksumFileChecker
-   {
-   public:
-     /**
-      * Constructor.
-      * \param checksum Checksum that validates the file
-      */
-     ChecksumFileChecker( const CheckSum &checksum );
-     /**
-      * \short Try to validate the file
-      * \param file File to validate.
-      */
-     bool operator()( const Pathname &file ) const;
-     
-   private:
-     CheckSum _checksum;
-   };
-   
-   /**
-    * \short Checks for the validity of a signature
-    */
-   class SignatureFileChecker
-   {
-     public:
-      /**
-      * Constructor.
-      * \param signature Signature that validates the file
-      */
-      SignatureFileChecker( const Pathname &signature );
-      
-      /**
-      * Default Constructor.
-      * \short Signature for unsigned files
-      * Use it when you dont have a signature but you want
-      * to check the user to accept an unsigned file.
-      */
-      SignatureFileChecker();
-      
-      
-      /**
-       * add a public key to the list of known keys
-       */
-      void addPublicKey( const Pathname &publickey );
-      /**
-      * \short Try to validate the file
-      * \param file File to validate.
-      */
-      bool operator()( const Pathname &file ) const;
-     
-     private:
-      Pathname _signature;
-   };
-   
-   /**
-   * \short Checks for nothing
-   * Used as the default checker
-   */
-   class NullFileChecker
-   {
-   public:
-     bool operator()( const Pathname &file )  const;
-   };
-    
-   /**
-    * \short Checker composed of more checkers.
-    * 
-    * Allows to create a checker composed of various
-    * checkers altothether. It will only
-    * validate if all the checkers validate.
-    *
-    * \code
-    * CompositeFileChecker com;
-    * com.add(checker1);
-    * com.add(checker2);
-    * fetcher.enqueue(location, com);
-    * \endcode
-    */
-   class CompositeFileChecker
-   {
-   public:
-     void add( const FileChecker &checker );
-     bool operator()( const Pathname &file ) const;
-   private:
-     std::list<FileChecker> _checkers;
-   };
-   
   public:
     /** Default ctor */
     Fetcher();
diff --git a/zypp/FileChecker.cc b/zypp/FileChecker.cc
new file mode 100644 (file)
index 0000000..4ec16c8
--- /dev/null
@@ -0,0 +1,119 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/FileChecker.cc
+ *
+*/
+#include <iostream>
+#include "zypp/base/Logger.h"
+#include "zypp/FileChecker.h"
+#include "zypp/ZYppFactory.h"
+#include "zypp/Digest.h"
+#include "zypp/KeyRing.h"
+
+using namespace std;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
+    : _checksum(checksum)
+  {
+  }
+
+  void ChecksumFileChecker::operator()( const Pathname &file ) const
+  {
+    callback::SendReport<DigestReport> report;
+    CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
+    
+    if ( _checksum.empty() )
+    {
+      MIL << "File " <<  file << " has no checksum available." << std::endl;
+      if ( report->askUserToAcceptNoDigest(file) )
+      {
+        MIL << "User accepted " <<  file << " with no checksum." << std::endl;
+        return;
+      }
+      else
+      {
+        ZYPP_THROW(FileCheckException("No checksum available"));
+      }
+    }
+    else
+    {
+      if ( (real_checksum == _checksum) )
+      {
+        if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
+        {
+          WAR << "User accepted " <<  file << " with WRONG CHECKSUM." << std::endl;
+          return;
+        }
+        else
+        {
+          ZYPP_THROW(FileCheckException("Wrong checksum"));
+        }
+      }
+    }
+  }
+
+  void NullFileChecker::operator()(const Pathname &file ) const
+  {
+    return;
+  }
+
+  void CompositeFileChecker::operator()(const Pathname &file ) const
+  {
+    for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
+    {
+      (*it)(file);
+    }
+  }
+  
+  void CompositeFileChecker::add( const FileChecker &checker )
+  {
+    _checkers.push_back(checker);
+  }
+
+  SignatureFileChecker::SignatureFileChecker( const Pathname &signature )
+    : _signature(signature)
+  {
+  }
+  
+  SignatureFileChecker::SignatureFileChecker()
+  {
+  }
+  
+  void SignatureFileChecker::addPublicKey( const Pathname &publickey )
+  {
+    ZYpp::Ptr z = getZYpp();
+    z->keyRing()->importKey(publickey, false);
+  }
+  
+  void SignatureFileChecker::operator()(const Pathname &file ) const
+  {
+    ZYpp::Ptr z = getZYpp();
+    MIL << "checking " << file << " file validity using digital signature.." << endl;
+    bool valid = z->keyRing()->verifyFileSignatureWorkflow( file, string(), _signature);
+    if (!valid)
+      ZYPP_THROW(FileCheckException("Signature verification failed"));
+  }
+
+  /******************************************************************
+  **
+  **   FUNCTION NAME : operator<<
+  **   FUNCTION TYPE : std::ostream &
+  */
+  std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
+  {
+    return str;
+  }
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zypp/FileChecker.h b/zypp/FileChecker.h
new file mode 100644 (file)
index 0000000..af06161
--- /dev/null
@@ -0,0 +1,158 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/FileChecker.h
+ *
+*/
+#ifndef ZYPP_FILECHECKER_H
+#define ZYPP_FILECHECKER_H
+
+#include <iosfwd>
+#include <list>
+#include "zypp/base/Exception.h"
+#include "zypp/base/Function.h"
+#include "zypp/Pathname.h"
+#include "zypp/CheckSum.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  /**
+   * Functor signature used to check files.
+   * \param file File to check.
+   *
+   * \throws FileCheckException when the file does not
+   * validate and the user don't want to continue.
+   */
+  typedef function<void ( const Pathname &file )> FileChecker;
+  
+  class FileCheckException : public Exception
+  {
+  public:
+    FileCheckException(const std::string &msg)
+      : Exception(msg)
+    {}
+  };
+  
+  class CheckSumCheckException : public FileCheckException
+  {
+    //TODO
+  };
+  
+  class SignatureCheckException : public FileCheckException
+  {
+    //TODO
+  };
+  
+  /**
+   * Built in file checkers
+   */
+  
+  /**
+   * \short Checks for a valid checksum and interacts with the user.
+   */
+   class ChecksumFileChecker
+   {
+   public:
+     /**
+      * Constructor.
+      * \param checksum Checksum that validates the file
+      */
+     ChecksumFileChecker( const CheckSum &checksum );
+     /**
+      * \short Try to validate the file
+      * \param file File to validate.
+      *
+      * \throws CheckSumCheckException if validation fails
+      */
+     void operator()( const Pathname &file ) const;
+   private:
+     CheckSum _checksum;
+   };
+   
+   /**
+    * \short Checks for the validity of a signature
+    */
+   class SignatureFileChecker
+   {
+     public:
+      /**
+      * Constructor.
+      * \param signature Signature that validates the file
+      */
+      SignatureFileChecker( const Pathname &signature );
+      
+      /**
+      * Default Constructor.
+      * \short Signature for unsigned files
+      * Use it when you dont have a signature but you want
+      * to check the user to accept an unsigned file.
+      */
+      SignatureFileChecker();
+      
+      
+      /**
+       * add a public key to the list of known keys
+       */
+      void addPublicKey( const Pathname &publickey );
+      /**
+      * \short Try to validate the file
+      * \param file File to validate.
+      *
+      * \throws SignatureCheckException if validation fails
+      */
+      void operator()( const Pathname &file ) const;
+     
+     private:
+      Pathname _signature;
+   };
+   
+   /**
+   * \short Checks for nothing
+   * Used as the default checker
+   */
+   class NullFileChecker
+   {
+   public:
+     void operator()( const Pathname &file )  const;
+   };
+    
+   /**
+    * \short Checker composed of more checkers.
+    * 
+    * Allows to create a checker composed of various
+    * checkers altothether. It will only
+    * validate if all the checkers validate.
+    *
+    * \code
+    * CompositeFileChecker com;
+    * com.add(checker1);
+    * com.add(checker2);
+    * fetcher.enqueue(location, com);
+    * \endcode
+    */
+   class CompositeFileChecker
+   {
+   public:
+     void add( const FileChecker &checker );
+    /**
+     * \throws FileCheckException if validation fails
+     */
+     void operator()( const Pathname &file ) const;
+   private:
+     std::list<FileChecker> _checkers;
+   };
+
+  /** \relates FileChecker Stream output */
+  std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_FILECHECKER_H
index d4ec95f..34e6faa 100644 (file)
@@ -45,6 +45,14 @@ bool YUMDownloader::patches_Callback( const OnMediaLocation &loc, const string &
 bool YUMDownloader::repomd_Callback( const OnMediaLocation &loc, const YUMResourceType &dtype )
 {
   MIL << dtype << " : " << loc << endl;
+  
+  // skip other
+  if ( dtype == YUMResourceType::OTHER )
+  {
+    MIL << "Skipping other.xml" << endl;
+    return true;
+  }
+  
   _fetcher.enqueueDigested(loc);
   
   // We got a patches file we need to read, to add patches listed
@@ -56,6 +64,7 @@ bool YUMDownloader::repomd_Callback( const OnMediaLocation &loc, const YUMResour
     // now the patches.xml file must exists
     PatchesFileReader( _dest_dir + loc.filename(), bind( &YUMDownloader::patches_Callback, this, _1, _2));
   }
+    
   return true;
 }
 
@@ -75,10 +84,10 @@ void YUMDownloader::download( const Pathname &dest_dir )
   
   _fetcher.start( dest_dir, _media );
   
-  Fetcher::SignatureFileChecker sigchecker;
+  SignatureFileChecker sigchecker;
   
   if ( PathInfo( dest_dir + sigpath ).isExist() )
-    sigchecker = Fetcher::SignatureFileChecker(dest_dir + sigpath);
+    sigchecker = SignatureFileChecker(dest_dir + sigpath);
   
   if ( PathInfo( dest_dir + keypath ).isExist() )
     sigchecker.addPublicKey(dest_dir + keypath );
index 597ef88..4861c8e 100644 (file)
@@ -10,9 +10,9 @@
 #ifndef YUMRESOURCETYPE_H_
 #define YUMRESOURCETYPE_H_
 
+#include <iosfwd>
 #include<string>
 
-
 namespace zypp
 {
   namespace source