1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Fetcher.cc
17 #include "zypp/base/Easy.h"
18 #include "zypp/base/Logger.h"
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/base/DefaultIntegral.h"
21 #include "zypp/base/String.h"
22 #include "zypp/Fetcher.h"
23 #include "zypp/CheckSum.h"
24 #include "zypp/base/UserRequestException.h"
25 #include "zypp/parser/susetags/ContentFileReader.h"
26 #include "zypp/parser/susetags/RepoIndex.h"
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
35 * class that represents indexes which add metadata
36 * to fetcher jobs and therefore need to be retrieved
41 FetcherIndex( const OnMediaLocation &loc )
46 OnMediaLocation location;
48 typedef shared_ptr<FetcherIndex> FetcherIndex_Ptr;
51 * Class to encapsulate the \ref OnMediaLocation object
52 * and the \ref FileChecker together
61 RecursiveDirectory = Directory | Recursive,
62 // check checksums even if there is no such
63 // checksum (warns of no checksum)
64 AlwaysVerifyChecksum = 0x0004,
66 ZYPP_DECLARE_FLAGS(Flags, Flag);
69 FetcherJob( const OnMediaLocation &loc )
73 //MIL << location << endl;
78 //MIL << location << " | * " << checkers.size() << endl;
81 OnMediaLocation location;
82 //CompositeFileChecker checkers;
83 list<FileChecker> checkers;
86 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(FetcherJob::Flags);
87 typedef shared_ptr<FetcherJob> FetcherJob_Ptr;
89 std::ostream & operator<<( std::ostream & str, const FetcherJob_Ptr & obj )
91 return str << obj->location;
95 ///////////////////////////////////////////////////////////////////
97 // CLASS NAME : Fetcher::Impl
99 /** Fetcher implementation. */
102 friend std::ostream & operator<<( std::ostream & str, const Fetcher::Impl & obj );
110 void setOptions( Fetcher::Options options );
111 Fetcher::Options options() const;
113 void addIndex( const OnMediaLocation &resource );
115 void enqueueDir( const OnMediaLocation &resource, bool recursive, const FileChecker &checker = FileChecker() );
116 void enqueueDigestedDir( const OnMediaLocation &resource, bool recursive, const FileChecker &checker = FileChecker() );
118 void enqueue( const OnMediaLocation &resource, const FileChecker &checker = FileChecker() );
119 void enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker = FileChecker() );
120 void addCachePath( const Pathname &cache_dir );
122 void start( const Pathname &dest_dir,
123 MediaSetAccess &media,
124 const ProgressData::ReceiverFnc & progress_receiver );
126 /** Offer default Impl. */
127 static shared_ptr<Impl> nullimpl()
129 static shared_ptr<Impl> _nullimpl( new Impl );
134 * download the indexes and reads them
136 void readIndexes( MediaSetAccess &media, const Pathname &dest_dir);
139 * reads a downloaded index file and updates internal
142 * The index lists files relative to a directory, which is
143 * normally the same as the index file is located.
145 void readIndex( const Pathname &index, const Pathname &basedir );
147 /** specific version of \ref readIndex for SHA1SUMS file */
148 void readSha1sumsIndex( const Pathname &index, const Pathname &basedir );
150 /** specific version of \ref readIndex for SHA1SUMS file */
151 void readContentFileIndex( const Pathname &index, const Pathname &basedir );
154 * tries to provide the file represented by job into dest_dir by
155 * looking at the cache. If success, returns true, and the desired
156 * file should be available on dest_dir
158 bool provideFromCache( const OnMediaLocation &resource, const Pathname &dest_dir );
160 * Validates the job against is checkers, by using the file instance
164 void validate( const OnMediaLocation &resource, const Pathname &dest_dir, const list<FileChecker> &checkers );
167 * scan the directory and adds the individual jobs
169 void addDirJobs( MediaSetAccess &media, const OnMediaLocation &resource,
170 const Pathname &dest_dir, FetcherJob::Flags flags );
173 * auto discovery and reading of indexes
175 void autoaddIndexes( const filesystem::DirContent &content,
176 MediaSetAccess &media,
177 const OnMediaLocation &resource,
178 const Pathname &dest_dir );
180 * Provide the resource to \ref dest_dir
182 void provideToDest( MediaSetAccess &media, const OnMediaLocation &resource, const Pathname &dest_dir );
185 friend Impl * rwcowClone<Impl>( const Impl * rhs );
186 /** clone for RWCOW_pointer */
188 { return new Impl( *this ); }
190 list<FetcherJob_Ptr> _resources;
191 list<FetcherIndex_Ptr> _indexes;
192 list<Pathname> _caches;
193 // checksums read from the indexes
194 map<string, CheckSum> _checksums;
195 Fetcher::Options _options;
197 ///////////////////////////////////////////////////////////////////
199 void Fetcher::Impl::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
202 job.reset(new FetcherJob(resource));
203 job->flags |= FetcherJob:: AlwaysVerifyChecksum;
204 _resources.push_back(job);
207 Fetcher::Impl::Impl()
212 void Fetcher::Impl::setOptions( Fetcher::Options options )
213 { _options = options; }
215 Fetcher::Options Fetcher::Impl::options() const
218 void Fetcher::Impl::enqueueDir( const OnMediaLocation &resource,
220 const FileChecker &checker )
223 job.reset(new FetcherJob(resource));
225 job->checkers.push_back(checker);
227 job->flags |= FetcherJob::Recursive;
228 job->flags |= FetcherJob::Directory;
230 _resources.push_back(job);
233 void Fetcher::Impl::enqueueDigestedDir( const OnMediaLocation &resource,
235 const FileChecker &checker )
238 job.reset(new FetcherJob(resource));
240 job->checkers.push_back(checker);
242 job->flags |= FetcherJob::Recursive;
243 job->flags |= FetcherJob::Directory;
244 job->flags |= FetcherJob::AlwaysVerifyChecksum;
246 _resources.push_back(job);
250 void Fetcher::Impl::enqueue( const OnMediaLocation &resource, const FileChecker &checker )
253 job.reset(new FetcherJob(resource));
255 job->checkers.push_back(checker);
256 _resources.push_back(job);
259 void Fetcher::Impl::addIndex( const OnMediaLocation &resource )
261 MIL << "adding index " << resource << endl;
262 FetcherIndex_Ptr index;
263 index.reset(new FetcherIndex(resource));
264 _indexes.push_back(index);
268 void Fetcher::Impl::reset()
274 void Fetcher::Impl::addCachePath( const Pathname &cache_dir )
276 PathInfo info(cache_dir);
277 if ( info.isExist() )
281 DBG << "Adding fetcher cache: '" << cache_dir << "'." << endl;
282 _caches.push_back(cache_dir);
286 // don't add bad cache directory, just log the error
287 ERR << "Not adding cache: '" << cache_dir << "'. Not a directory." << endl;
292 ERR << "Not adding cache '" << cache_dir << "'. Path does not exists." << endl;
297 // tries to provide resource to dest_dir from any of the configured additional
298 // cache paths where the file may already be present. returns true if the
299 // file was provided from the cache.
300 bool Fetcher::Impl::provideFromCache( const OnMediaLocation &resource, const Pathname &dest_dir )
302 Pathname dest_full_path = dest_dir + resource.filename();
304 // first check in the destination directory
305 if ( PathInfo(dest_full_path).isExist() )
307 if ( is_checksum( dest_full_path, resource.checksum() )
308 && (! resource.checksum().empty() ) )
312 MIL << "start fetcher with " << _caches.size() << " cache directories." << endl;
313 for_ ( it_cache, _caches.begin(), _caches.end() )
315 // does the current file exists in the current cache?
316 Pathname cached_file = *it_cache + resource.filename();
317 if ( PathInfo( cached_file ).isExist() )
319 DBG << "File '" << cached_file << "' exist, testing checksum " << resource.checksum() << endl;
320 // check the checksum
321 if ( is_checksum( cached_file, resource.checksum() ) && (! resource.checksum().empty() ) )
324 MIL << "file " << resource.filename() << " found in previous cache. Using cached copy." << endl;
325 // checksum is already checked.
326 // we could later implement double failover and try to download if file copy fails.
327 // replicate the complete path in the target directory
328 if( dest_full_path != cached_file )
330 if ( assert_dir( dest_full_path.dirname() ) != 0 )
331 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
333 if ( filesystem::hardlink(cached_file, dest_full_path ) != 0 )
335 WAR << "Can't hardlink '" << cached_file << "' to '" << dest_dir << "'. Trying copying." << endl;
336 if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
338 ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
348 } // iterate over caches
352 void Fetcher::Impl::validate( const OnMediaLocation &resource, const Pathname &dest_dir, const list<FileChecker> &checkers )
354 // no matter where did we got the file, try to validate it:
355 Pathname localfile = dest_dir + resource.filename();
356 // call the checker function
359 MIL << "Checking job [" << localfile << "] (" << checkers.size() << " checkers )" << endl;
361 for ( list<FileChecker>::const_iterator it = checkers.begin();
362 it != checkers.end();
371 ERR << "Invalid checker for '" << localfile << "'" << endl;
376 catch ( const FileCheckException &e )
380 catch ( const Exception &e )
386 ZYPP_THROW(Exception("Unknown error while validating " + resource.filename().asString()));
390 void Fetcher::Impl::autoaddIndexes( const filesystem::DirContent &content,
391 MediaSetAccess &media,
392 const OnMediaLocation &resource,
393 const Pathname &dest_dir )
395 if ( _options & AutoAddSha1sumsIndexes )
397 // only try to add an index if it exists
398 filesystem::DirEntry shafile;
399 shafile.name = "SHA1SUMS"; shafile.type = filesystem::FT_FILE;
400 if ( find( content.begin(), content.end(), shafile ) != content.end() )
402 // add the index of this directory
403 OnMediaLocation indexloc(resource);
404 indexloc.changeFilename(resource.filename() + "SHA1SUMS");
406 // we need to read it now
407 readIndexes(media, dest_dir);
410 if ( _options & AutoAddContentFileIndexes )
412 // only try to add an index if it exists
413 filesystem::DirEntry contentfile;
414 contentfile.name = "content"; contentfile.type = filesystem::FT_FILE;
415 if ( find( content.begin(), content.end(), contentfile ) != content.end() )
417 // add the index of this directory
418 OnMediaLocation indexloc(resource);
419 indexloc.changeFilename(resource.filename() + "content");
421 // we need to read it now
422 readIndexes(media, dest_dir);
427 void Fetcher::Impl::addDirJobs( MediaSetAccess &media,
428 const OnMediaLocation &resource,
429 const Pathname &dest_dir, FetcherJob::Flags flags )
431 // first get the content of the directory so we can add
432 // individual transfer jobs
433 MIL << "Adding directory " << resource.filename() << endl;
434 filesystem::DirContent content;
435 media.dirInfo( content, resource.filename(), false /* dots */, resource.medianr());
437 // this method test for the option flags so indexes are added
438 // only if the options are enabled
439 autoaddIndexes(content, media, resource, dest_dir);
441 for ( filesystem::DirContent::const_iterator it = content.begin();
445 // skip SHA1SUMS* as they were already retrieved
446 if ( str::hasPrefix(it->name, "SHA1SUMS") )
449 Pathname filename = resource.filename() + it->name;
453 case filesystem::FT_NOT_AVAIL: // old directory.yast contains no typeinfo at all
454 case filesystem::FT_FILE:
456 CheckSum chksm(resource.checksum());
457 if ( _checksums.find(filename.asString()) != _checksums.end() )
459 // the checksum can be replaced with the one in the index.
460 chksm = _checksums[filename.asString()];
461 //MIL << "resource " << filename << " has checksum in the index file." << endl;
464 WAR << "Resource " << filename << " has no checksum in the index either." << endl;
465 if ( flags & FetcherJob::AlwaysVerifyChecksum )
466 enqueueDigested(OnMediaLocation(filename, resource.medianr()).setChecksum(chksm));
468 enqueue(OnMediaLocation(filename, resource.medianr()).setChecksum(chksm));
471 case filesystem::FT_DIR: // newer directory.yast contain at least directory info
472 if ( flags & FetcherJob::Recursive )
473 addDirJobs(media, filename, dest_dir, flags);
476 // don't provide devices, sockets, etc.
482 void Fetcher::Impl::provideToDest( MediaSetAccess &media, const OnMediaLocation &resource, const Pathname &dest_dir )
484 bool got_from_cache = false;
486 // start look in cache
487 got_from_cache = provideFromCache(resource, dest_dir);
489 if ( ! got_from_cache )
491 MIL << "Not found in cache, downloading" << endl;
493 // try to get the file from the net
496 Pathname tmp_file = media.provideFile(resource);
497 Pathname dest_full_path = dest_dir + resource.filename();
498 if ( assert_dir( dest_full_path.dirname() ) != 0 )
499 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
500 if ( filesystem::copy(tmp_file, dest_full_path ) != 0 )
502 ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
505 media.releaseFile(resource); //not needed anymore, only eat space
507 catch (Exception & excpt_r)
509 ZYPP_CAUGHT(excpt_r);
510 excpt_r.remember("Can't provide " + resource.filename().asString() + " : " + excpt_r.msg());
511 ZYPP_RETHROW(excpt_r);
516 // We got the file from cache
517 // continue with next file
522 // helper class to consume a content file
523 struct ContentReaderHelper : public parser::susetags::ContentFileReader
525 ContentReaderHelper()
527 setRepoIndexConsumer( bind( &ContentReaderHelper::consumeIndex, this, _1 ) );
530 void consumeIndex( const parser::susetags::RepoIndex_Ptr & data_r )
531 { _repoindex = data_r; }
533 parser::susetags::RepoIndex_Ptr _repoindex;
536 // generic function for reading indexes
537 void Fetcher::Impl::readIndex( const Pathname &index, const Pathname &basedir )
539 if ( index.basename() == "SHA1SUMS" )
540 readSha1sumsIndex(index, basedir);
541 else if ( index.basename() == "content" )
542 readContentFileIndex(index, basedir);
544 WAR << index << ": index file format not known" << endl;
547 // reads a content file index
548 void Fetcher::Impl::readContentFileIndex( const Pathname &index, const Pathname &basedir )
550 ContentReaderHelper reader;
552 MIL << index << " contains " << reader._repoindex->mediaFileChecksums.size() << " checksums." << endl;
553 for_( it, reader._repoindex->mediaFileChecksums.begin(), reader._repoindex->mediaFileChecksums.end() )
555 // content file entries don't start with /
556 _checksums[(basedir + it->first).asString()] = it->second;
560 // reads a SHA1SUMS file index
561 void Fetcher::Impl::readSha1sumsIndex( const Pathname &index, const Pathname &basedir )
563 std::ifstream in( index.c_str() );
567 while ( getline(in, buffer) )
569 vector<string> words;
570 str::split( buffer, back_inserter(words) );
571 if ( words.size() != 2 )
572 ZYPP_THROW(Exception("Wrong format for SHA1SUMS file"));
573 //MIL << "check: '" << words[0] << "' | '" << words[1] << "'" << endl;
574 if ( ! words[1].empty() )
575 _checksums[(basedir + words[1]).asString()] = CheckSum::sha1(words[0]);
579 ZYPP_THROW(Exception("Can't open SHA1SUMS file: " + index.asString()));
582 // this method takes all the user pointed indexes, gets them and also tries to
583 // download their signature, and verify them. After that, its parses each one
584 // to fill the checksum cache.
585 void Fetcher::Impl::readIndexes( MediaSetAccess &media, const Pathname &dest_dir)
587 // if there is no indexes, then just return to avoid
588 // the directory listing
589 if ( _indexes.empty() )
591 MIL << "No indexes to read." << endl;
595 // create a new fetcher with a different state to transfer the
596 // file containing checksums and its signature
598 // signature checker for index. We havent got the signature from
600 SignatureFileChecker sigchecker;
602 for ( list<FetcherIndex_Ptr>::const_iterator it_idx = _indexes.begin();
603 it_idx != _indexes.end(); ++it_idx )
605 MIL << "reading index " << (*it_idx)->location << endl;
606 // build the name of the index and the signature
607 OnMediaLocation idxloc((*it_idx)->location);
608 OnMediaLocation sigloc((*it_idx)->location.setOptional(true));
609 OnMediaLocation keyloc((*it_idx)->location.setOptional(true));
611 // calculate signature and key name
612 sigloc.changeFilename( sigloc.filename().extend(".asc") );
613 keyloc.changeFilename( keyloc.filename().extend(".key") );
615 //assert_dir(dest_dir + idxloc.filename().dirname());
617 // transfer the signature
618 fetcher.enqueue(sigloc);
619 fetcher.start( dest_dir, media );
620 // if we get the signature, update the checker
621 sigchecker = SignatureFileChecker(dest_dir + sigloc.filename());
625 fetcher.enqueue(keyloc);
626 fetcher.start( dest_dir, media );
629 // now the index itself
630 fetcher.enqueue( idxloc, FileChecker(sigchecker) );
631 fetcher.start( dest_dir, media );
634 // now we have the indexes in dest_dir
635 readIndex( dest_dir + idxloc.filename(), idxloc.filename().dirname() );
637 MIL << "done reading indexes" << endl;
640 // start processing all fetcher jobs.
641 // it processes any user pointed index first
642 void Fetcher::Impl::start( const Pathname &dest_dir,
643 MediaSetAccess &media,
644 const ProgressData::ReceiverFnc & progress_receiver )
646 ProgressData progress(_resources.size());
647 progress.sendTo(progress_receiver);
649 readIndexes(media, dest_dir);
651 for ( list<FetcherJob_Ptr>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
654 if ( (*it_res)->flags & FetcherJob::Directory )
656 const OnMediaLocation location((*it_res)->location);
657 addDirJobs(media, location, dest_dir, (*it_res)->flags);
661 provideToDest(media, (*it_res)->location, dest_dir);
663 // if the checksum is empty, but the checksum is in one of the
664 // indexes checksum, then add a checker
665 if ( (*it_res)->location.checksum().empty() )
667 if ( _checksums.find((*it_res)->location.filename().asString())
668 != _checksums.end() )
670 CheckSum chksm = _checksums[(*it_res)->location.filename().asString()];
671 ChecksumFileChecker digest_check(chksm);
672 (*it_res)->checkers.push_back(digest_check);
676 // if the index checksum is empty too, we only add the checker
677 // if the AlwaysVerifyChecksum option is set on
678 if ( (*it_res)->flags & FetcherJob::AlwaysVerifyChecksum )
680 // add the checker with the empty checksum
681 ChecksumFileChecker digest_check((*it_res)->location.checksum());
682 (*it_res)->checkers.push_back(digest_check);
688 // checksum is not empty, so add a checksum checker
689 ChecksumFileChecker digest_check((*it_res)->location.checksum());
690 (*it_res)->checkers.push_back(digest_check);
693 // validate job, this throws if not valid
694 validate((*it_res)->location, dest_dir, (*it_res)->checkers);
696 if ( ! progress.incr() )
697 ZYPP_THROW(AbortRequestException());
701 /** \relates Fetcher::Impl Stream output */
702 inline std::ostream & operator<<( std::ostream & str, const Fetcher::Impl & obj )
704 for ( list<FetcherJob_Ptr>::const_iterator it_res = obj._resources.begin(); it_res != obj._resources.end(); ++it_res )
712 : _pimpl( new Impl() )
718 void Fetcher::setOptions( Fetcher::Options options )
720 _pimpl->setOptions(options);
723 Fetcher::Options Fetcher::options() const
725 return _pimpl->options();
728 void Fetcher::enqueueDigested( const OnMediaLocation &resource, const FileChecker &checker )
730 _pimpl->enqueueDigested(resource, checker);
733 void Fetcher::enqueueDir( const OnMediaLocation &resource,
735 const FileChecker &checker )
737 _pimpl->enqueueDir(resource, recursive, checker);
740 void Fetcher::enqueueDigestedDir( const OnMediaLocation &resource,
742 const FileChecker &checker )
744 _pimpl->enqueueDigestedDir(resource, recursive, checker);
748 void Fetcher::addIndex( const OnMediaLocation &resource )
750 _pimpl->addIndex(resource);
754 void Fetcher::enqueue( const OnMediaLocation &resource, const FileChecker &checker )
756 _pimpl->enqueue(resource, checker);
759 void Fetcher::addCachePath( const Pathname &cache_dir )
761 _pimpl->addCachePath(cache_dir);
764 void Fetcher::reset()
769 void Fetcher::start( const Pathname &dest_dir,
770 MediaSetAccess &media,
771 const ProgressData::ReceiverFnc & progress_receiver )
773 _pimpl->start(dest_dir, media, progress_receiver);
776 std::ostream & operator<<( std::ostream & str, const Fetcher & obj )
778 return str << *obj._pimpl;
781 /////////////////////////////////////////////////////////////////
783 ///////////////////////////////////////////////////////////////////