1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoInfo.cc
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/LogTools.h"
18 #include "zypp/base/DefaultIntegral.h"
19 #include "zypp/parser/xml/XmlEscape.h"
21 #include "zypp/ManagedFile.h"
22 #include "zypp/PublicKey.h"
23 #include "zypp/MediaSetAccess.h"
24 #include "zypp/RepoInfo.h"
25 #include "zypp/Glob.h"
26 #include "zypp/TriBool.h"
27 #include "zypp/Pathname.h"
28 #include "zypp/ZConfig.h"
29 #include "zypp/repo/RepoMirrorList.h"
30 #include "zypp/ExternalProgram.h"
31 #include "zypp/media/MediaAccess.h"
33 #include "zypp/base/IOStream.h"
34 #include "zypp/base/InputStream.h"
35 #include "zypp/parser/xml/Reader.h"
38 #include "zypp/base/StrMatcher.h"
39 #include "zypp/KeyRing.h"
40 #include "zypp/TmpPath.h"
41 #include "zypp/ZYppFactory.h"
42 #include "zypp/ZYppCallbacks.h"
45 using zypp::xml::escape;
47 ///////////////////////////////////////////////////////////////////
49 { /////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////////////////////////////////
53 // CLASS NAME : RepoInfo::Impl
55 /** RepoInfo implementation. */
59 : _rawGpgCheck( indeterminate )
60 , _rawRepoGpgCheck( indeterminate )
61 , _rawPkgGpgCheck( indeterminate )
62 , _validRepoSignature( indeterminate )
63 , keeppackages(indeterminate)
64 , _mirrorListForceMetalink(false)
65 , type(repo::RepoType::NONE_e)
66 , emptybaseurls(false)
73 static const unsigned defaultPriority = 99;
74 static const unsigned noPriority = unsigned(-1);
76 void setProbedType( const repo::RepoType & t ) const
78 if ( type == repo::RepoType::NONE
79 && t != repo::RepoType::NONE )
82 const_cast<Impl*>(this)->type = t;
87 /** Path to a license tarball in case it exists in the repo. */
88 Pathname licenseTgz( const std::string & name_r ) const
91 if ( !metadataPath().empty() )
93 std::string licenseStem( "license" );
94 if ( !name_r.empty() )
97 licenseStem += name_r;
101 // TODO: REPOMD: this assumes we know the name of the tarball. In fact
102 // we'd need to get the file from repomd.xml (<data type="license[-name_r]">)
103 g.add( metadataPath() / path / ("repodata/*"+licenseStem+".tar.gz") );
105 g.add( metadataPath() / path / (licenseStem+".tar.gz") );
113 const RepoVariablesReplacedUrlList & baseUrls() const
115 const Url & mlurl( _mirrorListUrl.transformed() ); // Variables replaced!
116 if ( _baseUrls.empty() && ! mlurl.asString().empty() )
118 emptybaseurls = true;
119 DBG << "MetadataPath: " << metadataPath() << endl;
120 repo::RepoMirrorList rmurls( mlurl, metadataPath(), _mirrorListForceMetalink );
121 _baseUrls.raw().insert( _baseUrls.raw().end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
126 RepoVariablesReplacedUrlList & baseUrls()
127 { return _baseUrls; }
129 bool baseurl2dump() const
130 { return !emptybaseurls && !_baseUrls.empty(); }
133 const RepoVariablesReplacedUrlList & gpgKeyUrls() const
134 { return _gpgKeyUrls; }
136 RepoVariablesReplacedUrlList & gpgKeyUrls()
137 { return _gpgKeyUrls; }
140 const std::set<std::string> & contentKeywords() const
141 { hasContent()/*init if not yet done*/; return _keywords.second; }
143 void addContent( const std::string & keyword_r )
144 { _keywords.second.insert( keyword_r ); if ( ! hasContent() ) _keywords.first = true; }
146 bool hasContent() const
148 if ( !_keywords.first && ! metadataPath().empty() )
150 // HACK directly check master index file until RepoManager offers
151 // some content probing and zypper uses it.
152 /////////////////////////////////////////////////////////////////
153 MIL << "Empty keywords...." << metadataPath() << endl;
155 if ( PathInfo( (master=metadataPath()/"/repodata/repomd.xml") ).isFile() )
157 //MIL << "GO repomd.." << endl;
158 xml::Reader reader( master );
159 while ( reader.seekToNode( 2, "content" ) )
161 _keywords.second.insert( reader.nodeText().asString() );
162 reader.seekToEndNode( 2, "content" );
164 _keywords.first = true; // valid content in _keywords even if empty
166 else if ( PathInfo( (master=metadataPath()/"/content") ).isFile() )
168 //MIL << "GO content.." << endl;
169 iostr::forEachLine( InputStream( master ),
170 [this]( int num_r, std::string line_r )->bool
172 if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
174 std::vector<std::string> words;
175 if ( str::split( line_r, std::back_inserter(words) ) > 1
176 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
178 this->_keywords.second.insert( ++words.begin(), words.end() );
180 return true; // mult. occurrances are ok.
182 return( ! str::startsWith( line_r, "META " ) ); // no need to parse into META section.
184 _keywords.first = true; // valid content in _keywords even if empty
186 /////////////////////////////////////////////////////////////////
188 return _keywords.first;
191 bool hasContent( const std::string & keyword_r ) const
192 { return( hasContent() && _keywords.second.find( keyword_r ) != _keywords.second.end() ); }
194 /** Signature check result needs to be stored/retrieved from _metadataPath.
195 * Don't call them from outside validRepoSignature/setValidRepoSignature
198 TriBool internalValidRepoSignature() const
200 if ( ! indeterminate(_validRepoSignature) )
201 return _validRepoSignature;
203 if ( ! metadataPath().empty() )
205 // A missing ".repo_gpgcheck" might be plaindir(no Downloader) or not yet refreshed signed repo!
206 TriBool linkval = triBoolFromPath( metadataPath() / ".repo_gpgcheck" );
209 return indeterminate;
212 void internalSetValidRepoSignature( TriBool value_r )
214 if ( PathInfo(metadataPath()).isDir() )
216 Pathname gpgcheckFile( metadataPath() / ".repo_gpgcheck" );
217 if ( PathInfo(gpgcheckFile).isExist() )
219 TriBool linkval( indeterminate );
220 if ( triBoolFromPath( gpgcheckFile, linkval ) && linkval == value_r )
221 return; // existing symlink fits value_r
223 filesystem::unlink( gpgcheckFile ); // will write a new one
225 filesystem::symlink( asString(value_r), gpgcheckFile );
227 _validRepoSignature = value_r;
230 /** We definitely have a symlink pointing to "indeterminate" (for repoGpgCheckIsMandatory)?
231 * I.e. user accepted the unsigned repo in Downloader. A test whether `internalValidRepoSignature`
232 * is indeterminate would include not yet checked repos, which is unwanted here.
234 bool internalUnsignedConfirmed() const
236 TriBool linkval( true ); // want to see it being switched to indeterminate
237 return triBoolFromPath( metadataPath() / ".repo_gpgcheck", linkval ) && indeterminate(linkval);
240 bool triBoolFromPath( const Pathname & path_r, TriBool & ret_r ) const
242 static const Pathname truePath( "true" );
243 static const Pathname falsePath( "false" );
244 static const Pathname indeterminatePath( "indeterminate" );
247 static const ssize_t bufsiz = 63;
248 static char buf[bufsiz+1];
249 ssize_t ret = ::readlink( path_r.c_str(), buf, bufsiz );
250 buf[ret == -1 ? 0 : ret] = '\0';
252 Pathname linkval( buf );
255 if ( linkval == truePath )
257 else if ( linkval == falsePath )
259 else if ( linkval == indeterminatePath )
260 ret_r = indeterminate;
266 TriBool triBoolFromPath( const Pathname & path_r ) const
267 { TriBool ret(indeterminate); triBoolFromPath( path_r, ret ); return ret; }
272 TriBool _rawGpgCheck; ///< default gpgcheck behavior: Y/N/ZConf
273 TriBool _rawRepoGpgCheck; ///< need to check repo sign.: Y/N/(ZConf(Y/N/gpgCheck))
274 TriBool _rawPkgGpgCheck; ///< need to check pkg sign.: Y/N/(ZConf(Y/N/gpgCheck))
277 TriBool rawGpgCheck() const { return _rawGpgCheck; }
278 TriBool rawRepoGpgCheck() const { return _rawRepoGpgCheck; }
279 TriBool rawPkgGpgCheck() const { return _rawPkgGpgCheck; }
281 void rawGpgCheck( TriBool val_r ) { _rawGpgCheck = val_r; }
282 void rawRepoGpgCheck( TriBool val_r ) { _rawRepoGpgCheck = val_r; }
283 void rawPkgGpgCheck( TriBool val_r ) { _rawPkgGpgCheck = val_r; }
285 bool cfgGpgCheck() const
286 { return indeterminate(_rawGpgCheck) ? ZConfig::instance().gpgCheck() : (bool)_rawGpgCheck; }
287 TriBool cfgRepoGpgCheck() const
288 { return indeterminate(_rawGpgCheck) && indeterminate(_rawRepoGpgCheck) ? ZConfig::instance().repoGpgCheck() : _rawRepoGpgCheck; }
289 TriBool cfgPkgGpgCheck() const
290 { return indeterminate(_rawGpgCheck) && indeterminate(_rawPkgGpgCheck) ? ZConfig::instance().pkgGpgCheck() : _rawPkgGpgCheck; }
293 TriBool _validRepoSignature;///< have signed and valid repo metadata
295 TriBool keeppackages;
296 RepoVariablesReplacedUrl _mirrorListUrl;
297 bool _mirrorListForceMetalink;
301 std::string targetDistro;
303 void metadataPath( Pathname new_r )
304 { _metadataPath = std::move( new_r ); }
306 void packagesPath( Pathname new_r )
307 { _packagesPath = std::move( new_r ); }
309 bool usesAutoMethadataPaths() const
310 { return str::hasSuffix( _metadataPath.asString(), "/%AUTO%" ); }
312 Pathname metadataPath() const
314 if ( usesAutoMethadataPaths() )
315 return _metadataPath.dirname() / "%RAW%";
316 return _metadataPath;
319 Pathname packagesPath() const
321 if ( _packagesPath.empty() && usesAutoMethadataPaths() )
322 return _metadataPath.dirname() / "%PKG%";
323 return _packagesPath;
326 DefaultIntegral<unsigned,defaultPriority> priority;
327 mutable bool emptybaseurls;
330 Pathname _metadataPath;
331 Pathname _packagesPath;
333 mutable RepoVariablesReplacedUrlList _baseUrls;
334 mutable std::pair<FalseBool, std::set<std::string> > _keywords;
336 RepoVariablesReplacedUrlList _gpgKeyUrls;
338 friend Impl * rwcowClone<Impl>( const Impl * rhs );
339 /** clone for RWCOW_pointer */
341 { return new Impl( *this ); }
343 ///////////////////////////////////////////////////////////////////
345 /** \relates RepoInfo::Impl Stream output */
346 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
348 return str << "RepoInfo::Impl";
351 ///////////////////////////////////////////////////////////////////
353 // CLASS NAME : RepoInfo
355 ///////////////////////////////////////////////////////////////////
357 const RepoInfo RepoInfo::noRepo;
360 : _pimpl( new Impl() )
363 RepoInfo::~RepoInfo()
366 unsigned RepoInfo::priority() const
367 { return _pimpl->priority; }
369 unsigned RepoInfo::defaultPriority()
370 { return Impl::defaultPriority; }
372 unsigned RepoInfo::noPriority()
373 { return Impl::noPriority; }
375 void RepoInfo::setPriority( unsigned newval_r )
376 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
379 bool RepoInfo::gpgCheck() const
380 { return _pimpl->cfgGpgCheck(); }
382 void RepoInfo::setGpgCheck( TriBool value_r )
383 { _pimpl->rawGpgCheck( value_r ); }
385 void RepoInfo::setGpgCheck( bool value_r ) // deprecated legacy and for squid
386 { setGpgCheck( TriBool(value_r) ); }
389 bool RepoInfo::repoGpgCheck() const
390 { return gpgCheck() || bool(_pimpl->cfgRepoGpgCheck()); }
392 bool RepoInfo::repoGpgCheckIsMandatory() const
394 bool ret = ( gpgCheck() && indeterminate(_pimpl->cfgRepoGpgCheck()) ) || bool(_pimpl->cfgRepoGpgCheck());
395 if ( ret && _pimpl->internalUnsignedConfirmed() ) // relax if unsigned repo was confirmed in the past
400 void RepoInfo::setRepoGpgCheck( TriBool value_r )
401 { _pimpl->rawRepoGpgCheck( value_r ); }
404 bool RepoInfo::pkgGpgCheck() const
405 { return bool(_pimpl->cfgPkgGpgCheck()) || ( gpgCheck() && !bool(validRepoSignature())/*enforced*/ ) ; }
407 bool RepoInfo::pkgGpgCheckIsMandatory() const
408 { return bool(_pimpl->cfgPkgGpgCheck()) || ( gpgCheck() && indeterminate(_pimpl->cfgPkgGpgCheck()) && !bool(validRepoSignature())/*enforced*/ ); }
410 void RepoInfo::setPkgGpgCheck( TriBool value_r )
411 { _pimpl->rawPkgGpgCheck( value_r ); }
414 void RepoInfo::getRawGpgChecks( TriBool & g_r, TriBool & r_r, TriBool & p_r ) const
416 g_r = _pimpl->rawGpgCheck();
417 r_r = _pimpl->rawRepoGpgCheck();
418 p_r = _pimpl->rawPkgGpgCheck();
422 TriBool RepoInfo::validRepoSignature() const
424 TriBool ret( _pimpl->internalValidRepoSignature() );
425 if ( ret && !repoGpgCheck() ) ret = false; // invalidate any old signature if repoGpgCheck is off
429 void RepoInfo::setValidRepoSignature( TriBool value_r )
430 { _pimpl->internalSetValidRepoSignature( value_r ); }
432 ///////////////////////////////////////////////////////////////////
435 inline bool changeGpgCheckTo( TriBool & lhs, TriBool rhs )
436 { if ( ! sameTriboolState( lhs, rhs ) ) { lhs = rhs; return true; } return false; }
438 inline bool changeGpgCheckTo( TriBool ogpg[3], TriBool g, TriBool r, TriBool p )
440 bool changed = false;
441 if ( changeGpgCheckTo( ogpg[0], g ) ) changed = true;
442 if ( changeGpgCheckTo( ogpg[1], r ) ) changed = true;
443 if ( changeGpgCheckTo( ogpg[2], p ) ) changed = true;
447 ///////////////////////////////////////////////////////////////////
448 bool RepoInfo::setGpgCheck( GpgCheck mode_r )
450 TriBool ogpg[3]; // Gpg RepoGpg PkgGpg
451 getRawGpgChecks( ogpg[0], ogpg[1], ogpg[2] );
453 bool changed = false;
457 changed = changeGpgCheckTo( ogpg, true, indeterminate, indeterminate );
459 case GpgCheck::Strict:
460 changed = changeGpgCheckTo( ogpg, true, true, true );
462 case GpgCheck::AllowUnsigned:
463 changed = changeGpgCheckTo( ogpg, true, false, false );
465 case GpgCheck::AllowUnsignedRepo:
466 changed = changeGpgCheckTo( ogpg, true, false, indeterminate );
468 case GpgCheck::AllowUnsignedPackage:
469 changed = changeGpgCheckTo( ogpg, true, indeterminate, false );
471 case GpgCheck::Default:
472 changed = changeGpgCheckTo( ogpg, indeterminate, indeterminate, indeterminate );
475 changed = changeGpgCheckTo( ogpg, false, indeterminate, indeterminate );
477 case GpgCheck::indeterminate: // no change
483 setGpgCheck ( ogpg[0] );
484 setRepoGpgCheck( ogpg[1] );
485 setPkgGpgCheck ( ogpg[2] );
490 void RepoInfo::setMirrorListUrl( const Url & url_r ) // Raw
491 { _pimpl->_mirrorListUrl.raw() = url_r; _pimpl->_mirrorListForceMetalink = false; }
493 void RepoInfo::setMetalinkUrl( const Url & url_r ) // Raw
494 { _pimpl->_mirrorListUrl.raw() = url_r; _pimpl->_mirrorListForceMetalink = true; }
496 void RepoInfo::setGpgKeyUrls( url_set urls )
497 { _pimpl->gpgKeyUrls().raw().swap( urls ); }
499 void RepoInfo::setGpgKeyUrl( const Url & url_r )
501 _pimpl->gpgKeyUrls().raw().clear();
502 _pimpl->gpgKeyUrls().raw().push_back( url_r );
505 Pathname RepoInfo::provideKey(const std::string &keyID_r, const Pathname &targetDirectory_r) const
507 if ( keyID_r.empty() )
510 MIL << "Check for " << keyID_r << " at " << targetDirectory_r << endl;
511 std::string keyIDStr( keyID_r.size() > 8 ? keyID_r.substr( keyID_r.size()-8 ) : keyID_r ); // print short ID in Jobreports
512 filesystem::TmpDir tmpKeyRingDir;
513 KeyRing tempKeyRing(tmpKeyRingDir.path());
515 // translator: %1% is a gpg key ID like 3DBDC284
516 // %2% is a cache directories path
517 JobReport::info( str::Format(_("Looking for gpg key ID %1% in cache %2%.") ) % keyIDStr % targetDirectory_r );
518 filesystem::dirForEach(targetDirectory_r,
519 StrMatcher(".key", Match::STRINGEND),
520 [&tempKeyRing]( const Pathname & dir_r, const std::string & str_r ){
523 // deprecate a month old keys
524 PathInfo fileInfo ( dir_r/str_r );
525 if ( Date::now() - fileInfo.mtime() > Date::month ) {
526 //if unlink fails, the file will be overriden in the next step, no need
528 filesystem::unlink( dir_r/str_r );
530 tempKeyRing.multiKeyImport(dir_r/str_r, true);
532 } catch (const KeyRingException& e) {
534 ERR << "Error importing cached key from file '"<<dir_r/str_r<<"'."<<endl;
539 // no key in the cache is what we are looking for, lets download
540 // all keys specified in gpgkey= entries
541 if ( !tempKeyRing.isKeyTrusted(keyID_r) ) {
542 if ( ! gpgKeyUrlsEmpty() ) {
543 // translator: %1% is a gpg key ID like 3DBDC284
544 // %2% is a repositories name
545 JobReport::info( str::Format(_("Looking for gpg key ID %1% in repository %2%.") ) % keyIDStr % asUserString() );
546 for ( const Url &url : gpgKeyUrls() ) {
548 JobReport::info( " gpgkey=" + url.asString() );
549 ManagedFile f = MediaSetAccess::provideOptionalFileFromUrl( url );
554 if ( !key.isValid() )
557 // import all keys into our temporary keyring
558 tempKeyRing.multiKeyImport(f, true);
560 } catch ( const std::exception & e ) {
561 //ignore and continue to next url
563 MIL << "Key import from url:'"<<url<<"' failed." << endl;
568 // translator: %1% is a repositories name
569 JobReport::info( str::Format(_("Repository %1% does not define additional 'gpgkey=' URLs.") ) % asUserString() );
573 filesystem::assert_dir( targetDirectory_r );
575 //now write all keys into their own files in cache, override existing ones to always have
576 //up to date key data
577 for ( const auto & key: tempKeyRing.trustedPublicKeyData()) {
578 MIL << "KEY ID in KEYRING: " << key.id() << endl;
580 Pathname keyFile = targetDirectory_r/(str::Format("%1%.key") % key.rpmName()).asString();
582 std::ofstream fout( keyFile.c_str(), std::ios_base::out | std::ios_base::trunc );
585 ZYPP_THROW(Exception(str::form("Cannot open file %s",keyFile.c_str())));
587 tempKeyRing.dumpTrustedPublicKey( key.id(), fout );
590 // key is STILL not known, we give up
591 if ( !tempKeyRing.isKeyTrusted(keyID_r) ) {
595 PublicKeyData keyData( tempKeyRing.trustedPublicKeyData( keyID_r ) );
597 ERR << "Error when exporting key from temporary keychain." << endl;
601 return targetDirectory_r/(str::Format("%1%.key") % keyData.rpmName()).asString();
604 void RepoInfo::addBaseUrl( const Url & url_r )
606 for ( const auto & url : _pimpl->baseUrls().raw() ) // Raw unique!
609 _pimpl->baseUrls().raw().push_back( url_r );
612 void RepoInfo::setBaseUrl( const Url & url_r )
614 _pimpl->baseUrls().raw().clear();
615 _pimpl->baseUrls().raw().push_back( url_r );
618 void RepoInfo::setBaseUrls( url_set urls )
619 { _pimpl->baseUrls().raw().swap( urls ); }
621 void RepoInfo::setPath( const Pathname &path )
622 { _pimpl->path = path; }
624 void RepoInfo::setType( const repo::RepoType &t )
625 { _pimpl->type = t; }
627 void RepoInfo::setProbedType( const repo::RepoType &t ) const
628 { _pimpl->setProbedType( t ); }
631 void RepoInfo::setMetadataPath( const Pathname &path )
632 { _pimpl->metadataPath( path ); }
634 void RepoInfo::setPackagesPath( const Pathname &path )
635 { _pimpl->packagesPath( path ); }
637 void RepoInfo::setKeepPackages( bool keep )
638 { _pimpl->keeppackages = keep; }
640 void RepoInfo::setService( const std::string& name )
641 { _pimpl->service = name; }
643 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
644 { _pimpl->targetDistro = targetDistribution; }
646 bool RepoInfo::keepPackages() const
647 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
649 Pathname RepoInfo::metadataPath() const
650 { return _pimpl->metadataPath(); }
652 Pathname RepoInfo::packagesPath() const
653 { return _pimpl->packagesPath(); }
655 bool RepoInfo::usesAutoMethadataPaths() const
656 { return _pimpl->usesAutoMethadataPaths(); }
658 repo::RepoType RepoInfo::type() const
659 { return _pimpl->type; }
661 Url RepoInfo::mirrorListUrl() const // Variables replaced!
662 { return _pimpl->_mirrorListUrl.transformed(); }
664 Url RepoInfo::rawMirrorListUrl() const // Raw
665 { return _pimpl->_mirrorListUrl.raw(); }
667 bool RepoInfo::gpgKeyUrlsEmpty() const
668 { return _pimpl->gpgKeyUrls().empty(); }
670 RepoInfo::urls_size_type RepoInfo::gpgKeyUrlsSize() const
671 { return _pimpl->gpgKeyUrls().size(); }
673 RepoInfo::url_set RepoInfo::gpgKeyUrls() const // Variables replaced!
674 { return _pimpl->gpgKeyUrls().transformed(); }
676 RepoInfo::url_set RepoInfo::rawGpgKeyUrls() const // Raw
677 { return _pimpl->gpgKeyUrls().raw(); }
679 Url RepoInfo::gpgKeyUrl() const // Variables replaced!
680 { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().transformedBegin() ); }
682 Url RepoInfo::rawGpgKeyUrl() const // Raw
683 { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().rawBegin() ) ; }
685 RepoInfo::url_set RepoInfo::baseUrls() const // Variables replaced!
686 { return _pimpl->baseUrls().transformed(); }
688 RepoInfo::url_set RepoInfo::rawBaseUrls() const // Raw
689 { return _pimpl->baseUrls().raw(); }
691 Pathname RepoInfo::path() const
692 { return _pimpl->path; }
694 std::string RepoInfo::service() const
695 { return _pimpl->service; }
697 std::string RepoInfo::targetDistribution() const
698 { return _pimpl->targetDistro; }
700 Url RepoInfo::rawUrl() const
701 { return( _pimpl->baseUrls().empty() ? Url() : *_pimpl->baseUrls().rawBegin() ); }
703 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
704 { return _pimpl->baseUrls().transformedBegin(); }
706 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
707 { return _pimpl->baseUrls().transformedEnd(); }
709 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
710 { return _pimpl->baseUrls().size(); }
712 bool RepoInfo::baseUrlsEmpty() const
713 { return _pimpl->baseUrls().empty(); }
715 bool RepoInfo::baseUrlSet() const
716 { return _pimpl->baseurl2dump(); }
718 const std::set<std::string> & RepoInfo::contentKeywords() const
719 { return _pimpl->contentKeywords(); }
721 void RepoInfo::addContent( const std::string & keyword_r )
722 { _pimpl->addContent( keyword_r ); }
724 bool RepoInfo::hasContent() const
725 { return _pimpl->hasContent(); }
727 bool RepoInfo::hasContent( const std::string & keyword_r ) const
728 { return _pimpl->hasContent( keyword_r ); }
730 ///////////////////////////////////////////////////////////////////
732 bool RepoInfo::hasLicense() const
733 { return hasLicense( std::string() ); }
735 bool RepoInfo::hasLicense( const std::string & name_r ) const
736 { return !_pimpl->licenseTgz( name_r ).empty(); }
739 bool RepoInfo::needToAcceptLicense() const
740 { return needToAcceptLicense( std::string() ); }
742 bool RepoInfo::needToAcceptLicense( const std::string & name_r ) const
744 const Pathname & licenseTgz( _pimpl->licenseTgz( name_r ) );
745 if ( licenseTgz.empty() )
746 return false; // no licenses at all
748 ExternalProgram::Arguments cmd;
749 cmd.push_back( "tar" );
750 cmd.push_back( "-t" );
751 cmd.push_back( "-z" );
752 cmd.push_back( "-f" );
753 cmd.push_back( licenseTgz.asString() );
754 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
757 static const std::string noAcceptanceFile = "no-acceptance-needed\n";
758 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
760 if ( output == noAcceptanceFile )
766 MIL << "License(" << name_r << ") in " << name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
771 std::string RepoInfo::getLicense( const Locale & lang_r )
772 { return const_cast<const RepoInfo *>(this)->getLicense( std::string(), lang_r ); }
774 std::string RepoInfo::getLicense( const Locale & lang_r ) const
775 { return getLicense( std::string(), lang_r ); }
777 std::string RepoInfo::getLicense( const std::string & name_r, const Locale & lang_r ) const
779 LocaleSet avlocales( getLicenseLocales( name_r ) );
780 if ( avlocales.empty() )
781 return std::string();
783 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
784 if ( !getLang && avlocales.find( Locale::noCode ) == avlocales.end() )
786 WAR << "License(" << name_r << ") in " << name() << " contains no fallback text!" << endl;
787 // Using the fist locale instead of returning no text at all.
788 // So the user might recognize that there is a license, even if he
790 getLang = *avlocales.begin();
793 // now extract the license file.
794 static const std::string licenseFileFallback( "license.txt" );
795 std::string licenseFile( !getLang ? licenseFileFallback
796 : str::form( "license.%s.txt", getLang.c_str() ) );
798 ExternalProgram::Arguments cmd;
799 cmd.push_back( "tar" );
800 cmd.push_back( "-x" );
801 cmd.push_back( "-z" );
802 cmd.push_back( "-O" );
803 cmd.push_back( "-f" );
804 cmd.push_back( _pimpl->licenseTgz( name_r ).asString() ); // if it not exists, avlocales was empty.
805 cmd.push_back( licenseFile );
808 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
809 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
818 LocaleSet RepoInfo::getLicenseLocales() const
819 { return getLicenseLocales( std::string() ); }
821 LocaleSet RepoInfo::getLicenseLocales( const std::string & name_r ) const
823 const Pathname & licenseTgz( _pimpl->licenseTgz( name_r ) );
824 if ( licenseTgz.empty() )
827 ExternalProgram::Arguments cmd;
828 cmd.push_back( "tar" );
829 cmd.push_back( "-t" );
830 cmd.push_back( "-z" );
831 cmd.push_back( "-f" );
832 cmd.push_back( licenseTgz.asString() );
835 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
836 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
838 static const C_Str license( "license." );
839 static const C_Str dotTxt( ".txt\n" );
840 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
842 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
843 ret.insert( Locale() );
845 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
852 ///////////////////////////////////////////////////////////////////
854 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
856 RepoInfoBase::dumpOn(str);
857 if ( _pimpl->baseurl2dump() )
859 for ( const auto & url : _pimpl->baseUrls().raw() )
861 str << "- url : " << url << std::endl;
865 // print if non empty value
866 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
867 if ( ! value_r.empty() )
868 str << tag_r << value_r << std::endl;
871 strif( (_pimpl->_mirrorListForceMetalink ? "- metalink : " : "- mirrorlist : "), rawMirrorListUrl().asString() );
872 strif( "- path : ", path().asString() );
873 str << "- type : " << type() << std::endl;
874 str << "- priority : " << priority() << std::endl;
876 // Yes No Default(Y) Default(N)
877 #define OUTS(T,B) ( indeterminate(T) ? (std::string("D(")+(B?"Y":"N")+")") : ((bool)T?"Y":"N") )
878 str << "- gpgcheck : " << OUTS(_pimpl->rawGpgCheck(),gpgCheck())
879 << " repo" << OUTS(_pimpl->rawRepoGpgCheck(),repoGpgCheck()) << (repoGpgCheckIsMandatory() ? "* ": " " )
880 << "sig" << asString( validRepoSignature(), "?", "Y", "N" )
881 << " pkg" << OUTS(_pimpl->rawPkgGpgCheck(),pkgGpgCheck()) << (pkgGpgCheckIsMandatory() ? "* ": " " )
885 for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
887 str << "- gpgkey : " << url << std::endl;
890 if ( ! indeterminate(_pimpl->keeppackages) )
891 str << "- keeppackages: " << keepPackages() << std::endl;
893 strif( "- service : ", service() );
894 strif( "- targetdistro: ", targetDistribution() );
895 strif( "- filePath: ", filepath().asString() );
896 strif( "- metadataPath: ", metadataPath().asString() );
897 strif( "- packagesPath: ", packagesPath().asString() );
902 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
904 RepoInfoBase::dumpAsIniOn(str);
906 if ( _pimpl->baseurl2dump() )
910 for ( const auto & url : _pimpl->baseUrls().raw() )
912 str << indent << url << endl;
913 if ( indent.empty() ) indent = " "; // "baseurl="
917 if ( ! _pimpl->path.empty() )
918 str << "path="<< path() << endl;
920 if ( ! (rawMirrorListUrl().asString().empty()) )
921 str << (_pimpl->_mirrorListForceMetalink ? "metalink=" : "mirrorlist=") << rawMirrorListUrl() << endl;
923 str << "type=" << type().asString() << endl;
925 if ( priority() != defaultPriority() )
926 str << "priority=" << priority() << endl;
928 if ( ! indeterminate(_pimpl->rawGpgCheck()) )
929 str << "gpgcheck=" << (_pimpl->rawGpgCheck() ? "1" : "0") << endl;
931 if ( ! indeterminate(_pimpl->rawRepoGpgCheck()) )
932 str << "repo_gpgcheck=" << (_pimpl->rawRepoGpgCheck() ? "1" : "0") << endl;
934 if ( ! indeterminate(_pimpl->rawPkgGpgCheck()) )
935 str << "pkg_gpgcheck=" << (_pimpl->rawPkgGpgCheck() ? "1" : "0") << endl;
938 std::string indent( "gpgkey=");
939 for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
941 str << indent << url << endl;
942 if ( indent[0] != ' ' )
947 if (!indeterminate(_pimpl->keeppackages))
948 str << "keeppackages=" << keepPackages() << endl;
950 if( ! service().empty() )
951 str << "service=" << service() << endl;
956 std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
961 << " alias=\"" << escape(alias()) << "\""
962 << " name=\"" << escape(name()) << "\"";
963 if (type() != repo::RepoType::NONE)
964 str << " type=\"" << type().asString() << "\"";
966 << " priority=\"" << priority() << "\""
967 << " enabled=\"" << enabled() << "\""
968 << " autorefresh=\"" << autorefresh() << "\""
969 << " gpgcheck=\"" << gpgCheck() << "\""
970 << " repo_gpgcheck=\"" << repoGpgCheck() << "\""
971 << " pkg_gpgcheck=\"" << pkgGpgCheck() << "\"";
972 if (!(tmpstr = gpgKeyUrl().asString()).empty())
973 str << " gpgkey=\"" << escape(tmpstr) << "\"";
974 if (!(tmpstr = mirrorListUrl().asString()).empty())
975 str << (_pimpl->_mirrorListForceMetalink ? " metalink=\"" : " mirrorlist=\"") << escape(tmpstr) << "\"";
978 if ( _pimpl->baseurl2dump() )
980 for_( it, baseUrlsBegin(), baseUrlsEnd() ) // !transform iterator replaces variables
981 str << "<url>" << escape((*it).asString()) << "</url>" << endl;
984 str << "</repo>" << endl;
989 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
991 return obj.dumpOn(str);
994 std::ostream & operator<<( std::ostream & str, const RepoInfo::GpgCheck & obj )
998 #define OUTS( V ) case RepoInfo::V: return str << #V; break
999 OUTS( GpgCheck::On );
1000 OUTS( GpgCheck::Strict );
1001 OUTS( GpgCheck::AllowUnsigned );
1002 OUTS( GpgCheck::AllowUnsignedRepo );
1003 OUTS( GpgCheck::AllowUnsignedPackage );
1004 OUTS( GpgCheck::Default );
1005 OUTS( GpgCheck::Off );
1006 OUTS( GpgCheck::indeterminate );
1009 return str << "GpgCheck::UNKNOWN";
1012 /////////////////////////////////////////////////////////////////
1014 ///////////////////////////////////////////////////////////////////