Author: dmacvicar
Date: Mon May 21 13:34:12 2007
New Revision: 5579
URL: http://svn.opensuse.org/viewcvs/zypp?rev=5579&view=rev
Log:
- implement signature and checksum checker in
the fetcher layer
also support for any kind of check mechanism.
See fetcher documentation.
Modified:
trunk/libzypp/zypp/Fetcher.cc
trunk/libzypp/zypp/Fetcher.h
trunk/libzypp/zypp/MediaSetAccess.cc
trunk/libzypp/zypp/MediaSetAccess.h
trunk/libzypp/zypp/source/yum/YUMDownloader.cc
Modified: trunk/libzypp/zypp/Fetcher.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/Fetcher.cc?rev=5579&r1=5578&r2=5579&view=diff
==============================================================================
--- trunk/libzypp/zypp/Fetcher.cc (original)
+++ trunk/libzypp/zypp/Fetcher.cc Mon May 21 13:34:12 2007
@@ -14,8 +14,9 @@
#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;
@@ -23,6 +24,97 @@
namespace zypp
{ /////////////////////////////////////////////////////////////////
+ Fetcher::ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
+ : _checksum(checksum)
+ {
+ }
+
+ bool Fetcher::ChecksumFileChecker::operator()( const Pathname &file )
+ {
+ 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 )
+ {
+ return true;
+ }
+
+ bool Fetcher::CompositeFileChecker::operator()(const Pathname &file )
+ {
+ bool result = true;
+ for ( listFetcher::FileChecker::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 )
+ {
+ ZYpp::Ptr z = getZYpp();
+ MIL << "checking " << file << " file vailidity 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
+ */
struct FetcherJob
{
FetcherJob( const OnMediaLocation &loc )
@@ -32,6 +124,7 @@
}
OnMediaLocation location;
+ Fetcher::CompositeFileChecker checkers;
};
///////////////////////////////////////////////////////////////////
@@ -44,7 +137,8 @@
public:
- void enqueue( const OnMediaLocation &resource );
+ void enqueue( const OnMediaLocation &resource, const Fetcher::FileChecker &checker );
+ void enqueueDigested( const OnMediaLocation &resource, const Fetcher::FileChecker &checker );
void addCachePath( const Pathname &cache_dir );
void reset();
void start( const Pathname &dest_dir, MediaSetAccess &media );
@@ -68,11 +162,21 @@
///////////////////////////////////////////////////////////////////
- void Fetcher::Impl::enqueue( const OnMediaLocation &resource )
+ void Fetcher::Impl::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
{
- _resources.push_back(FetcherJob(resource));
+ CompositeFileChecker composite;
+ composite.add(ChecksumFileChecker(resource.checksum()));
+ composite.add(checker);
+ enqueue(resource, composite);
}
+ void Fetcher::Impl::enqueue( const OnMediaLocation &resource, const FileChecker &checker )
+ {
+ FetcherJob job(resource);
+ job.checkers.add(checker);
+ _resources.push_back(resource);
+ }
+
void Fetcher::Impl::reset()
{
_resources.clear();
@@ -197,9 +301,14 @@
Fetcher::~Fetcher()
{}
- void Fetcher::enqueue( const OnMediaLocation &resource )
+ void Fetcher::enqueueDigested( const OnMediaLocation &resource, const Fetcher::FileChecker &checker )
{
- _pimpl->enqueue(resource);
+ _pimpl->enqueue(resource, checker);
+ }
+
+ void Fetcher::enqueue( const OnMediaLocation &resource, const Fetcher::FileChecker &checker )
+ {
+ _pimpl->enqueue(resource, checker);
}
void Fetcher::addCachePath( const Pathname &cache_dir )
@@ -232,23 +341,3 @@
} // namespace zypp
///////////////////////////////////////////////////////////////////
-// callback::SendReport<DigestReport> report;
-// if ( checksum.empty() )
-// {
-// MIL << "File " << file_url << " has no checksum available." << std::endl;
-// if ( report->askUserToAcceptNoDigest(file_to_download) )
-// {
-// MIL << "User accepted " << file_url << " with no checksum." << std::endl;
-// return;
-// }
-// else
-// {
-// ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" miss checksum.") ));
-// }
-// }
-// else
-// {
-// if (! is_checksum( destination, checksum))
-// ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" fails checksum verification.") ));
-// }
-
Modified: trunk/libzypp/zypp/Fetcher.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/Fetcher.h?rev=5579&r1=5578&r2=5579&view=diff
==============================================================================
--- trunk/libzypp/zypp/Fetcher.h (original)
+++ trunk/libzypp/zypp/Fetcher.h Mon May 21 13:34:12 2007
@@ -13,11 +13,13 @@
#define ZYPP_FETCHER_H
#include <iosfwd>
+#include <list>
#include "zypp/base/PtrTypes.h"
#include "zypp/Pathname.h"
#include "zypp/Url.h"
#include "zypp/OnMediaLocation.h"
+#include "zypp/Digest.h"
#include "zypp/MediaSetAccess.h"
///////////////////////////////////////////////////////////////////
@@ -25,8 +27,14 @@
{ /////////////////////////////////////////////////////////////////
/**
- * This class allows to retrieve a group of files which can
- * be cached already on the local disk.
+ * This class allows to retrieve a group of files in a confortable
+ * way, providing some smartness that does not belong to the
+ * media layer like:
+ *
+ * \li Configurable local caches to retrieve already
+ * donwloaded files.
+ * \li File checkers that can check for right checksums
+ * digital signatures, etc.
*
* \code
* MediaSetAccess access(url, path);
@@ -36,6 +44,20 @@
* fetcher.start( "/download-dir, access );
* fetcher.reset();
* \endcode
+ *
+ * To use the checkers. just create a functor implementing
+ * bool operator()(const Pathname &file) \see FileChecker.
+ * Pass the necessary validation data in the constructor
+ * of the functor, and pass the object to the \ref enqueue
+ * method.
+ *
+ * \code
+ * ChecksumFileChecker checker(CheckSum("sha1", "....");
+ * fetcher.enqueue( location, checker);
+ * \endcode
+ *
+ * If you need to use more than one checker
+ * \see CompositeFileChecker
*/
class Fetcher
{
@@ -45,6 +67,105 @@
/** Implementation */
class Impl;
+ /**
+ * Functor signature used to check files.
+ * \param file File to check.
+ */
+ typedef boost::function