1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/source/RepoProvideFile.cc
17 #include "zypp/base/Logger.h"
18 #include "zypp/repo/RepoProvideFile.h"
19 #include "zypp/ZYppCallbacks.h"
20 #include "zypp/MediaSetAccess.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////
40 { /////////////////////////////////////////////////////////////////
42 /** Hack to extract progress information from source::DownloadFileReport.
43 * We redirect the static report triggered from Repository::provideFile
44 * to feed the ProvideFilePolicy callbacks.
46 struct DownloadFileReportHack : public callback::ReceiveReport<repo::RepoReport>
48 virtual bool progress( int value )
51 return _redirect( value );
54 function<bool ( int )> _redirect;
57 /** ManagedFile Dispose functor.
58 * The Pathname argument is ignored, as Repository::releaseFile expects the filename
59 * relative to the medias root (i.e. same args as to provideFile).
61 struct RepoReleaseFile
63 RepoReleaseFile( Repository repo_r, const Pathname & location_r, unsigned mediaNr_r )
64 : _repo( repo_r ), _location( location_r ), _medianr( mediaNr_r )
67 void operator()( const Pathname & /*UNUSED*/ )
69 //_repo.releaseFile( _location, _medianr );
77 /////////////////////////////////////////////////////////////////
79 ///////////////////////////////////////////////////////////////////
81 ManagedFile provideFile( Repository repo_r,
82 const OnMediaLocation & loc_r,
83 const ProvideFilePolicy & policy_r )
85 MIL << "provideFile " << loc_r << endl;
86 // Arrange DownloadFileReportHack to recieve the source::DownloadFileReport
87 // and redirect download progress triggers to call the ProvideFilePolicy
89 DownloadFileReportHack dumb;
90 dumb._redirect = bind( mem_fun_ref( &ProvideFilePolicy::progress ),
91 ref( policy_r ), _1 );
92 callback::TempConnect<repo::RepoReport> temp( dumb );
95 RepoInfo info = repo_r.info();
96 set<Url> urls = info.baseUrls();
98 ZYPP_THROW(Exception(_("No url in repository.")));
100 for ( RepoInfo::urls_const_iterator it = urls.begin();
108 MediaSetAccess access(url);
110 ManagedFile ret( access.provideFile(loc_r),
111 RepoReleaseFile( repo_r, loc_r.filename(), loc_r.medianr() ) );
113 if ( loc_r.checksum().empty() )
115 // no checksum in metadata
116 WAR << "No checksum in metadata " << loc_r << endl;
120 std::ifstream input( ret->asString().c_str() );
121 CheckSum retChecksum( loc_r.checksum().type(), input );
124 if ( loc_r.checksum() != retChecksum )
126 // failed integity check
127 std::ostringstream err;
128 err << "File " << ret << " fails integrity check. Expected: [" << loc_r.checksum() << "] Got: [";
129 if ( retChecksum.empty() )
130 err << "Failed to compute checksum";
135 if ( policy_r.failOnChecksumError() )
136 ZYPP_THROW( Exception( err.str() ) );
138 WAR << "NO failOnChecksumError: " << err.str() << endl;
142 MIL << "provideFile at " << ret << endl;
145 catch ( const Exception &e )
148 WAR << "Trying next url" << endl;
151 } // iteration over urls
153 ZYPP_THROW(Exception(_("No more urls in repository.")));
154 return ManagedFile(); // not reached
157 /////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////
160 /////////////////////////////////////////////////////////////////
162 ///////////////////////////////////////////////////////////////////