Downloader.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00009 
00010 #include <fstream>
00011 #include "zypp/base/String.h"
00012 #include "zypp/base/Logger.h"
00013 #include "zypp/base/Function.h"
00014 
00015 #include "zypp/Date.h"
00016 
00017 #include "zypp/parser/yum/RepomdFileReader.h"
00018 #include "zypp/parser/yum/PatchesFileReader.h"
00019 #include "Downloader.h"
00020 #include "zypp/repo/MediaInfoDownloader.h"
00021 #include "zypp/base/UserRequestException.h"
00022 #include "zypp/parser/xml/Reader.h"
00023 #include "zypp/KeyContext.h"
00024 
00025 using namespace std;
00026 using namespace zypp::xml;
00027 using namespace zypp::parser::yum;
00028 
00029 namespace zypp
00030 {
00031 namespace repo
00032 {
00033 namespace yum
00034 {
00035 
00036 Downloader::Downloader( const RepoInfo &repoinfo )
00037   : repo::Downloader(repoinfo), _media_ptr(0L)
00038 {
00039 }
00040 
00041 
00042 RepoStatus Downloader::status( MediaSetAccess &media )
00043 {
00044   Pathname repomd = media.provideFile( repoInfo().path() + "/repodata/repomd.xml");
00045   return RepoStatus(repomd);
00046 }
00047 
00048 static OnMediaLocation
00049 loc_with_path_prefix(const OnMediaLocation & loc,
00050                      const Pathname & prefix)
00051 {
00052   if (prefix.empty() || prefix == "/")
00053     return loc;
00054 
00055   OnMediaLocation loc_with_path(loc);
00056   loc_with_path.changeFilename(prefix / loc.filename());
00057   return loc_with_path;
00058 }
00059 
00060 
00061 bool Downloader::patches_Callback( const OnMediaLocation &loc,
00062                                    const string &id )
00063 {
00064   OnMediaLocation loc_with_path(loc_with_path_prefix(loc, repoInfo().path()));
00065   MIL << id << " : " << loc_with_path << endl;
00066   this->enqueueDigested(loc_with_path);
00067   return true;
00068 }
00069 
00070 
00071 bool Downloader::repomd_Callback( const OnMediaLocation &loc,
00072                                   const ResourceType &dtype )
00073 {
00074   OnMediaLocation loc_with_path(loc_with_path_prefix(loc, repoInfo().path()));
00075   MIL << dtype << " : " << loc_with_path << endl;
00076 
00078   // skip other
00079   if ( dtype == ResourceType::OTHER )
00080   {
00081     MIL << "Skipping other.xml" << endl;
00082     return true;
00083   }
00084   // skip filelists
00085   if ( dtype == ResourceType::FILELISTS )
00086   {
00087     MIL << "Skipping filelists.xml.gz" << endl;
00088     return true;
00089   }
00090 
00091   this->enqueueDigested(loc_with_path);
00092 
00093   // We got a patches file we need to read, to add patches listed
00094   // there, so we transfer what we have in the queue, and
00095   // queue the patches in the patches callback
00096   if ( dtype == ResourceType::PATCHES )
00097   {
00098     this->start( _dest_dir, *_media_ptr );
00099     // now the patches.xml file must exists
00100     PatchesFileReader( _dest_dir + repoInfo().path() + loc.filename(),
00101                        bind( &Downloader::patches_Callback, this, _1, _2));
00102   }
00103 
00104   return true;
00105 }
00106 
00107 void Downloader::download( MediaSetAccess &media,
00108                            const Pathname &dest_dir,
00109                            const ProgressData::ReceiverFnc & progressrcv )
00110 {
00111   Pathname repomdpath =  repoInfo().path() + "/repodata/repomd.xml";
00112   Pathname keypath =  repoInfo().path() + "/repodata/repomd.xml.key";
00113   Pathname sigpath =  repoInfo().path() + "/repodata/repomd.xml.asc";
00114 
00115   _media_ptr = (&media);
00116 
00117   ProgressData progress;
00118   progress.sendTo(progressrcv);
00119   progress.toMin();
00120 
00121   //downloadMediaInfo( dest_dir, _media );
00122 
00123   _dest_dir = dest_dir;
00124 
00125 #warning Do we need SignatureFileChecker(string descr)?
00126   SignatureFileChecker sigchecker/*(repoInfo().name())*/;
00127 
00128   this->enqueue( OnMediaLocation(sigpath,1).setOptional(true) );
00129   this->start( dest_dir, *_media_ptr);
00130   // only add the signature if it exists
00131   if ( PathInfo(dest_dir / sigpath).isExist() )
00132       sigchecker = SignatureFileChecker(dest_dir / sigpath);
00133   this->reset();
00134 
00135   this->enqueue( OnMediaLocation(keypath,1).setOptional(true) );
00136   this->start( dest_dir, *_media_ptr);
00137 
00138   KeyContext context;
00139   context.setRepoInfo(repoInfo());
00140   // only add the key if it exists
00141   if ( PathInfo(dest_dir / keypath).isExist() )
00142     sigchecker.addPublicKey(dest_dir + keypath, context);
00143   // set the checker context even if the key is not known (unsigned repo, key
00144   // file missing; bnc #495977)
00145   else
00146     sigchecker.setKeyContext(context);
00147 
00148   this->reset();
00149 
00150   if ( ! progress.tick() )
00151     ZYPP_THROW(AbortRequestException());
00152 
00153   if ( ! repoInfo().gpgCheck() )
00154     WAR << "Signature checking disabled in config of repository " << repoInfo().alias() << endl;
00155 
00156   this->enqueue( OnMediaLocation(repomdpath,1),
00157                  repoInfo().gpgCheck() ? FileChecker(sigchecker) : FileChecker(NullFileChecker()) );
00158   this->start( dest_dir, *_media_ptr);
00159 
00160   if ( ! progress.tick() )
00161         ZYPP_THROW(AbortRequestException());
00162 
00163   this->reset();
00164 
00165   Reader reader( dest_dir + repoInfo().path() + "/repodata/repomd.xml" );
00166   RepomdFileReader( dest_dir + repoInfo().path() + "/repodata/repomd.xml", bind( &Downloader::repomd_Callback, this, _1, _2));
00167 
00168   // ready, go!
00169   this->start( dest_dir, *_media_ptr);
00170   progress.toMax();
00171 }
00172 
00173 }// ns yum
00174 }// ns source
00175 } // ns zypp
00176 
00177 
00178 

doxygen