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 PathInfo info(cache_dir);
105 _caches.push_back(cache_dir);
109 // don't add bad cache directory, just log the error
110 ERR << "Not adding cache: '" << cache_dir << "'. Not a direcotry." << endl;
114 void Fetcher::Impl::start( const Pathname &dest_dir,
115 MediaSetAccess &media,
116 const ProgressData::ReceiverFnc & progress_receiver )
118 ProgressData progress(_resources.size());
119 progress.sendTo(progress_receiver);
121 for ( list<FetcherJob>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
123 bool got_from_cache = false;
124 for ( list<Pathname>::const_iterator it_cache = _caches.begin(); it_cache != _caches.end(); ++it_cache )
126 // does the current file exists in the current cache?
127 Pathname cached_file = *it_cache + (*it_res).location.filename();
128 if ( PathInfo( cached_file ).isExist() )
130 // check the checksum
131 if ( is_checksum( cached_file, (*it_res).location.checksum() ) )
134 MIL << "file " << (*it_res).location.filename() << " found in previous cache. Using cached copy." << endl;
135 // checksum is already checked.
136 // we could later implement double failover and try to download if file copy fails.
138 // replicate the complete path in the target directory
139 Pathname dest_full_path = dest_dir + (*it_res).location.filename();
140 if ( assert_dir( dest_full_path.dirname() ) != 0 )
141 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
143 if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
145 //ZYPP_THROW(SourceIOException("Can't copy " + cached_file.asString() + " to " + destination.asString()));
146 ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
151 got_from_cache = true;
157 if ( ! got_from_cache )
159 // try to get the file from the net
162 Pathname tmp_file = media.provideFile((*it_res).location);
163 Pathname dest_full_path = dest_dir + (*it_res).location.filename();
164 if ( assert_dir( dest_full_path.dirname() ) != 0 )
165 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
166 if ( filesystem::copy(tmp_file, dest_full_path ) != 0 )
168 ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
173 catch (const Exception & excpt_r)
175 ZYPP_CAUGHT(excpt_r);
176 ZYPP_THROW(Exception("Can't provide " + (*it_res).location.filename().asString() + " : " + excpt_r.msg() ));
181 // We got the file from cache
182 // continue with next file
186 // no matter where did we got the file, try to validate it:
187 Pathname localfile = dest_dir + (*it_res).location.filename();
188 // call the checker function
190 (*it_res).checkers(localfile);
192 catch ( const FileCheckException &e )
196 catch ( const Exception &e )
202 ZYPP_THROW(Exception("Unknown error while validating " + (*it_res).location.filename().asString()));
205 if ( ! progress.incr() )
206 ZYPP_THROW(AbortRequestException());
210 /** \relates Fetcher::Impl Stream output */
211 inline std::ostream & operator<<( std::ostream & str, const Fetcher::Impl & obj )
213 return str << "Fetcher::Impl";
216 ///////////////////////////////////////////////////////////////////
218 // CLASS NAME : Fetcher
220 ///////////////////////////////////////////////////////////////////
222 ///////////////////////////////////////////////////////////////////
224 // METHOD NAME : Fetcher::Fetcher
225 // METHOD TYPE : Ctor
228 : _pimpl( Impl::nullimpl() )
231 ///////////////////////////////////////////////////////////////////
233 // METHOD NAME : Fetcher::~Fetcher
234 // METHOD TYPE : Dtor
239 void Fetcher::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
241 _pimpl->enqueue(resource, checker);
244 void Fetcher::enqueue( const OnMediaLocation &resource, const FileChecker &checker )
246 _pimpl->enqueue(resource, checker);
249 void Fetcher::addCachePath( const Pathname &cache_dir )
251 _pimpl->addCachePath(cache_dir);
254 void Fetcher::reset()
259 void Fetcher::start( const Pathname &dest_dir,
260 MediaSetAccess &media,
261 const ProgressData::ReceiverFnc & progress_receiver )
263 _pimpl->start(dest_dir, media, progress_receiver);
267 /******************************************************************
269 ** FUNCTION NAME : operator<<
270 ** FUNCTION TYPE : std::ostream &
272 std::ostream & operator<<( std::ostream & str, const Fetcher & obj )
274 return str << *obj._pimpl;
277 /////////////////////////////////////////////////////////////////
279 ///////////////////////////////////////////////////////////////////