1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Fetcher.cc
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/DefaultIntegral.h"
17 #include "zypp/Fetcher.h"
18 #include "zypp/base/UserRequestException.h"
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
27 * Class to encapsulate the \ref OnMediaLocation object
28 * and the \ref FileChecker together
32 FetcherJob( const OnMediaLocation &loc )
38 OnMediaLocation location;
39 CompositeFileChecker checkers;
42 ///////////////////////////////////////////////////////////////////
44 // CLASS NAME : Fetcher::Impl
46 /** Fetcher implementation. */
52 void enqueue( const OnMediaLocation &resource, const FileChecker &checker );
53 void enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker );
54 void addCachePath( const Pathname &cache_dir );
56 void start( const Pathname &dest_dir,
57 MediaSetAccess &media,
58 const ProgressData::ReceiverFnc & progress_receiver );
60 /** Offer default Impl. */
61 static shared_ptr<Impl> nullimpl()
63 static shared_ptr<Impl> _nullimpl( new Impl );
68 friend Impl * rwcowClone<Impl>( const Impl * rhs );
69 /** clone for RWCOW_pointer */
71 { return new Impl( *this ); }
73 std::list<FetcherJob> _resources;
74 std::list<Pathname> _caches;
76 ///////////////////////////////////////////////////////////////////
79 void Fetcher::Impl::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
81 CompositeFileChecker composite;
82 composite.add(ChecksumFileChecker(resource.checksum()));
83 composite.add(checker);
84 enqueue(resource, composite);
87 void Fetcher::Impl::enqueue( const OnMediaLocation &resource, const FileChecker &checker )
89 FetcherJob job(resource);
90 job.checkers.add(checker);
91 _resources.push_back(resource);
94 void Fetcher::Impl::reset()
100 void Fetcher::Impl::addCachePath( const Pathname &cache_dir )
102 _caches.push_back(cache_dir);
105 void Fetcher::Impl::start( const Pathname &dest_dir,
106 MediaSetAccess &media,
107 const ProgressData::ReceiverFnc & progress_receiver )
109 ProgressData progress(_resources.size());
110 progress.sendTo(progress_receiver);
112 for ( list<FetcherJob>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
114 bool got_from_cache = false;
115 for ( list<Pathname>::const_iterator it_cache = _caches.begin(); it_cache != _caches.end(); ++it_cache )
117 // Pathinfos could be cached to avoid too many stats?
118 PathInfo info(*it_cache);
121 // does the current file exists in the current cache?
122 Pathname cached_file = *it_cache + (*it_res).location.filename();
123 if ( PathInfo( cached_file ).isExist() )
125 // check the checksum
126 if ( is_checksum( cached_file, (*it_res).location.checksum() ) )
129 MIL << "file " << (*it_res).location.filename() << " found in previous cache. Using cached copy." << endl;
130 // checksum is already checked.
131 // we could later implement double failover and try to download if file copy fails.
133 // replicate the complete path in the target directory
134 Pathname dest_full_path = dest_dir + (*it_res).location.filename();
135 if ( assert_dir( dest_full_path.dirname() ) != 0 )
136 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
138 if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
140 //ZYPP_THROW(SourceIOException("Can't copy " + cached_file.asString() + " to " + destination.asString()));
141 ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
146 got_from_cache = true;
152 // File exists in cache but with a different checksum
153 // so just try next cache
159 // skip bad cache directory and try with next one
160 ERR << "Skipping cache : " << *it_cache << endl;
165 if ( ! got_from_cache )
167 // try to get the file from the net
170 Pathname tmp_file = media.provideFile((*it_res).location);
171 Pathname dest_full_path = dest_dir + (*it_res).location.filename();
172 if ( assert_dir( dest_full_path.dirname() ) != 0 )
173 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
174 if ( filesystem::copy(tmp_file, dest_full_path ) != 0 )
176 ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
181 catch (const Exception & excpt_r)
183 ZYPP_CAUGHT(excpt_r);
184 ZYPP_THROW(Exception("Can't provide " + (*it_res).location.filename().asString() + " : " + excpt_r.msg() ));
189 // We got the file from cache
190 // continue with next file
194 // no matter where did we got the file, try to validate it:
195 Pathname localfile = dest_dir + (*it_res).location.filename();
196 // call the checker function
198 (*it_res).checkers(localfile);
200 catch ( const FileCheckException &e )
204 catch ( const Exception &e )
210 ZYPP_THROW(Exception("Unknown error while validating " + (*it_res).location.filename().asString()));
213 if ( ! progress.incr() )
214 ZYPP_THROW(AbortRequestException());
218 /** \relates Fetcher::Impl Stream output */
219 inline std::ostream & operator<<( std::ostream & str, const Fetcher::Impl & obj )
221 return str << "Fetcher::Impl";
224 ///////////////////////////////////////////////////////////////////
226 // CLASS NAME : Fetcher
228 ///////////////////////////////////////////////////////////////////
230 ///////////////////////////////////////////////////////////////////
232 // METHOD NAME : Fetcher::Fetcher
233 // METHOD TYPE : Ctor
236 : _pimpl( Impl::nullimpl() )
239 ///////////////////////////////////////////////////////////////////
241 // METHOD NAME : Fetcher::~Fetcher
242 // METHOD TYPE : Dtor
247 void Fetcher::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
249 _pimpl->enqueue(resource, checker);
252 void Fetcher::enqueue( const OnMediaLocation &resource, const FileChecker &checker )
254 _pimpl->enqueue(resource, checker);
257 void Fetcher::addCachePath( const Pathname &cache_dir )
259 _pimpl->addCachePath(cache_dir);
262 void Fetcher::reset()
267 void Fetcher::start( const Pathname &dest_dir,
268 MediaSetAccess &media,
269 const ProgressData::ReceiverFnc & progress_receiver )
271 _pimpl->start(dest_dir, media, progress_receiver);
275 /******************************************************************
277 ** FUNCTION NAME : operator<<
278 ** FUNCTION TYPE : std::ostream &
280 std::ostream & operator<<( std::ostream & str, const Fetcher & obj )
282 return str << *obj._pimpl;
285 /////////////////////////////////////////////////////////////////
287 ///////////////////////////////////////////////////////////////////