1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoInfo.cc
16 #include "zypp/base/LogTools.h"
17 #include "zypp/base/DefaultIntegral.h"
18 #include "zypp/parser/xml/XmlEscape.h"
20 #include "zypp/ManagedFile.h"
21 #include "zypp/PublicKey.h"
22 #include "zypp/MediaSetAccess.h"
23 #include "zypp/RepoInfo.h"
24 #include "zypp/Glob.h"
25 #include "zypp/TriBool.h"
26 #include "zypp/Pathname.h"
27 #include "zypp/ZConfig.h"
28 #include "zypp/repo/RepoMirrorList.h"
29 #include "zypp/ExternalProgram.h"
30 #include "zypp/media/MediaAccess.h"
32 #include "zypp/base/IOStream.h"
33 #include "zypp/base/InputStream.h"
34 #include "zypp/parser/xml/Reader.h"
37 #include "zypp/base/StrMatcher.h"
38 #include "zypp/KeyRing.h"
39 #include "zypp/TmpPath.h"
40 #include "zypp/ZYppFactory.h"
41 #include "zypp/ZYppCallbacks.h"
44 using zypp::xml::escape;
46 ///////////////////////////////////////////////////////////////////
48 { /////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////
52 // CLASS NAME : RepoInfo::Impl
54 /** RepoInfo implementation. */
58 : _rawGpgCheck( indeterminate )
59 , _rawRepoGpgCheck( indeterminate )
60 , _rawPkgGpgCheck( indeterminate )
61 , _validRepoSignature( indeterminate )
62 , keeppackages(indeterminate)
63 , _mirrorListForceMetalink(false)
64 , type(repo::RepoType::NONE_e)
65 , emptybaseurls(false)
72 static const unsigned defaultPriority = 99;
73 static const unsigned noPriority = unsigned(-1);
75 void setProbedType( const repo::RepoType & t ) const
77 if ( type == repo::RepoType::NONE
78 && t != repo::RepoType::NONE )
81 const_cast<Impl*>(this)->type = t;
86 /** Path to a license tarball in case it exists in the repo. */
87 Pathname licenseTgz( const std::string & name_r ) const
90 if ( !metadataPath().empty() )
92 std::string licenseStem( "license" );
93 if ( !name_r.empty() )
96 licenseStem += name_r;
100 // TODO: REPOMD: this assumes we know the name of the tarball. In fact
101 // we'd need to get the file from repomd.xml (<data type="license[-name_r]">)
102 g.add( metadataPath() / path / ("repodata/*"+licenseStem+".tar.gz") );
104 g.add( metadataPath() / path / (licenseStem+".tar.gz") );
112 const RepoVariablesReplacedUrlList & baseUrls() const
114 const Url & mlurl( _mirrorListUrl.transformed() ); // Variables replaced!
115 if ( _baseUrls.empty() && ! mlurl.asString().empty() )
117 emptybaseurls = true;
118 DBG << "MetadataPath: " << metadataPath() << endl;
119 repo::RepoMirrorList rmurls( mlurl, metadataPath(), _mirrorListForceMetalink );
120 _baseUrls.raw().insert( _baseUrls.raw().end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
125 RepoVariablesReplacedUrlList & baseUrls()
126 { return _baseUrls; }
128 bool baseurl2dump() const
129 { return !emptybaseurls && !_baseUrls.empty(); }
132 const RepoVariablesReplacedUrlList & gpgKeyUrls() const
133 { return _gpgKeyUrls; }
135 RepoVariablesReplacedUrlList & gpgKeyUrls()
136 { return _gpgKeyUrls; }
139 const std::set<std::string> & contentKeywords() const
140 { hasContent()/*init if not yet done*/; return _keywords.second; }
142 void addContent( const std::string & keyword_r )
143 { _keywords.second.insert( keyword_r ); if ( ! hasContent() ) _keywords.first = true; }
145 bool hasContent() const
147 if ( !_keywords.first && ! metadataPath().empty() )
149 // HACK directly check master index file until RepoManager offers
150 // some content probing and zypper uses it.
151 /////////////////////////////////////////////////////////////////
152 MIL << "Empty keywords...." << metadataPath() << endl;
154 if ( PathInfo( (master=metadataPath()/"/repodata/repomd.xml") ).isFile() )
156 //MIL << "GO repomd.." << endl;
157 xml::Reader reader( master );
158 while ( reader.seekToNode( 2, "content" ) )
160 _keywords.second.insert( reader.nodeText().asString() );
161 reader.seekToEndNode( 2, "content" );
163 _keywords.first = true; // valid content in _keywords even if empty
165 else if ( PathInfo( (master=metadataPath()/"/content") ).isFile() )
167 //MIL << "GO content.." << endl;
168 iostr::forEachLine( InputStream( master ),
169 [this]( int num_r, std::string line_r )->bool
171 if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
173 std::vector<std::string> words;
174 if ( str::split( line_r, std::back_inserter(words) ) > 1
175 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
177 this->_keywords.second.insert( ++words.begin(), words.end() );
179 return true; // mult. occurrances are ok.
181 return( ! str::startsWith( line_r, "META " ) ); // no need to parse into META section.
183 _keywords.first = true; // valid content in _keywords even if empty
185 /////////////////////////////////////////////////////////////////
187 return _keywords.first;
190 bool hasContent( const std::string & keyword_r ) const
191 { return( hasContent() && _keywords.second.find( keyword_r ) != _keywords.second.end() ); }
193 /** Signature check result needs to be stored/retrieved from _metadataPath.
194 * Don't call them from outside validRepoSignature/setValidRepoSignature
197 TriBool internalValidRepoSignature() const
199 if ( ! indeterminate(_validRepoSignature) )
200 return _validRepoSignature;
202 if ( ! metadataPath().empty() )
204 // A missing ".repo_gpgcheck" might be plaindir(no Downloader) or not yet refreshed signed repo!
205 TriBool linkval = triBoolFromPath( metadataPath() / ".repo_gpgcheck" );
208 return indeterminate;
211 void internalSetValidRepoSignature( TriBool value_r )
213 if ( PathInfo(metadataPath()).isDir() )
215 Pathname gpgcheckFile( metadataPath() / ".repo_gpgcheck" );
216 if ( PathInfo(gpgcheckFile).isExist() )
218 TriBool linkval( indeterminate );
219 if ( triBoolFromPath( gpgcheckFile, linkval ) && linkval == value_r )
220 return; // existing symlink fits value_r
222 filesystem::unlink( gpgcheckFile ); // will write a new one
224 filesystem::symlink( asString(value_r), gpgcheckFile );
226 _validRepoSignature = value_r;
229 /** We definitely have a symlink pointing to "indeterminate" (for repoGpgCheckIsMandatory)?
230 * I.e. user accepted the unsigned repo in Downloader. A test whether `internalValidRepoSignature`
231 * is indeterminate would include not yet checked repos, which is unwanted here.
233 bool internalUnsignedConfirmed() const
235 TriBool linkval( true ); // want to see it being switched to indeterminate
236 return triBoolFromPath( metadataPath() / ".repo_gpgcheck", linkval ) && indeterminate(linkval);
239 bool triBoolFromPath( const Pathname & path_r, TriBool & ret_r ) const
241 static const Pathname truePath( "true" );
242 static const Pathname falsePath( "false" );
243 static const Pathname indeterminatePath( "indeterminate" );
246 static const ssize_t bufsiz = 63;
247 static char buf[bufsiz+1];
248 ssize_t ret = ::readlink( path_r.c_str(), buf, bufsiz );
249 buf[ret == -1 ? 0 : ret] = '\0';
251 Pathname linkval( buf );
254 if ( linkval == truePath )
256 else if ( linkval == falsePath )
258 else if ( linkval == indeterminatePath )
259 ret_r = indeterminate;
265 TriBool triBoolFromPath( const Pathname & path_r ) const
266 { TriBool ret(indeterminate); triBoolFromPath( path_r, ret ); return ret; }
271 TriBool _rawGpgCheck; ///< default gpgcheck behavior: Y/N/ZConf
272 TriBool _rawRepoGpgCheck; ///< need to check repo sign.: Y/N/(ZConf(Y/N/gpgCheck))
273 TriBool _rawPkgGpgCheck; ///< need to check pkg sign.: Y/N/(ZConf(Y/N/gpgCheck))
276 TriBool rawGpgCheck() const { return _rawGpgCheck; }
277 TriBool rawRepoGpgCheck() const { return _rawRepoGpgCheck; }
278 TriBool rawPkgGpgCheck() const { return _rawPkgGpgCheck; }
280 void rawGpgCheck( TriBool val_r ) { _rawGpgCheck = val_r; }
281 void rawRepoGpgCheck( TriBool val_r ) { _rawRepoGpgCheck = val_r; }
282 void rawPkgGpgCheck( TriBool val_r ) { _rawPkgGpgCheck = val_r; }
284 bool cfgGpgCheck() const
285 { return indeterminate(_rawGpgCheck) ? ZConfig::instance().gpgCheck() : (bool)_rawGpgCheck; }
286 TriBool cfgRepoGpgCheck() const
287 { return indeterminate(_rawGpgCheck) && indeterminate(_rawRepoGpgCheck) ? ZConfig::instance().repoGpgCheck() : _rawRepoGpgCheck; }
288 TriBool cfgPkgGpgCheck() const
289 { return indeterminate(_rawGpgCheck) && indeterminate(_rawPkgGpgCheck) ? ZConfig::instance().pkgGpgCheck() : _rawPkgGpgCheck; }
292 TriBool _validRepoSignature;///< have signed and valid repo metadata
294 TriBool keeppackages;
295 RepoVariablesReplacedUrl _mirrorListUrl;
296 bool _mirrorListForceMetalink;
300 std::string targetDistro;
302 void metadataPath( Pathname new_r )
303 { _metadataPath = std::move( new_r ); }
305 void packagesPath( Pathname new_r )
306 { _packagesPath = std::move( new_r ); }
308 bool usesAutoMethadataPaths() const
309 { return str::hasSuffix( _metadataPath.asString(), "/%AUTO%" ); }
311 Pathname metadataPath() const
313 if ( usesAutoMethadataPaths() )
314 return _metadataPath.dirname() / "%RAW%";
315 return _metadataPath;
318 Pathname packagesPath() const
320 if ( _packagesPath.empty() && usesAutoMethadataPaths() )
321 return _metadataPath.dirname() / "%PKG%";
322 return _packagesPath;
325 DefaultIntegral<unsigned,defaultPriority> priority;
326 mutable bool emptybaseurls;
329 Pathname _metadataPath;
330 Pathname _packagesPath;
332 mutable RepoVariablesReplacedUrlList _baseUrls;
333 mutable std::pair<FalseBool, std::set<std::string> > _keywords;
335 RepoVariablesReplacedUrlList _gpgKeyUrls;
337 friend Impl * rwcowClone<Impl>( const Impl * rhs );
338 /** clone for RWCOW_pointer */
340 { return new Impl( *this ); }
342 ///////////////////////////////////////////////////////////////////
344 /** \relates RepoInfo::Impl Stream output */
345 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
347 return str << "RepoInfo::Impl";
350 ///////////////////////////////////////////////////////////////////
352 // CLASS NAME : RepoInfo
354 ///////////////////////////////////////////////////////////////////
356 const RepoInfo RepoInfo::noRepo;
359 : _pimpl( new Impl() )
362 RepoInfo::~RepoInfo()
365 unsigned RepoInfo::priority() const
366 { return _pimpl->priority; }
368 unsigned RepoInfo::defaultPriority()
369 { return Impl::defaultPriority; }
371 unsigned RepoInfo::noPriority()
372 { return Impl::noPriority; }
374 void RepoInfo::setPriority( unsigned newval_r )
375 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
378 bool RepoInfo::gpgCheck() const
379 { return _pimpl->cfgGpgCheck(); }
381 void RepoInfo::setGpgCheck( TriBool value_r )
382 { _pimpl->rawGpgCheck( value_r ); }
384 void RepoInfo::setGpgCheck( bool value_r ) // deprecated legacy and for squid
385 { setGpgCheck( TriBool(value_r) ); }
388 bool RepoInfo::repoGpgCheck() const
389 { return gpgCheck() || _pimpl->cfgRepoGpgCheck(); }
391 bool RepoInfo::repoGpgCheckIsMandatory() const
393 bool ret = ( gpgCheck() && indeterminate(_pimpl->cfgRepoGpgCheck()) ) || _pimpl->cfgRepoGpgCheck();
394 if ( ret && _pimpl->internalUnsignedConfirmed() ) // relax if unsigned repo was confirmed in the past
399 void RepoInfo::setRepoGpgCheck( TriBool value_r )
400 { _pimpl->rawRepoGpgCheck( value_r ); }
403 bool RepoInfo::pkgGpgCheck() const
404 { return _pimpl->cfgPkgGpgCheck() || ( gpgCheck() && !bool(validRepoSignature())/*enforced*/ ) ; }
406 bool RepoInfo::pkgGpgCheckIsMandatory() const
407 { return _pimpl->cfgPkgGpgCheck() || ( gpgCheck() && indeterminate(_pimpl->cfgPkgGpgCheck()) && !bool(validRepoSignature())/*enforced*/ ); }
409 void RepoInfo::setPkgGpgCheck( TriBool value_r )
410 { _pimpl->rawPkgGpgCheck( value_r ); }
413 void RepoInfo::getRawGpgChecks( TriBool & g_r, TriBool & r_r, TriBool & p_r ) const
415 g_r = _pimpl->rawGpgCheck();
416 r_r = _pimpl->rawRepoGpgCheck();
417 p_r = _pimpl->rawPkgGpgCheck();
421 TriBool RepoInfo::validRepoSignature() const
423 TriBool ret( _pimpl->internalValidRepoSignature() );
424 if ( ret && !repoGpgCheck() ) ret = false; // invalidate any old signature if repoGpgCheck is off
428 void RepoInfo::setValidRepoSignature( TriBool value_r )
429 { _pimpl->internalSetValidRepoSignature( value_r ); }
431 ///////////////////////////////////////////////////////////////////
434 inline bool changeGpgCheckTo( TriBool & lhs, TriBool rhs )
435 { if ( ! sameTriboolState( lhs, rhs ) ) { lhs = rhs; return true; } return false; }
437 inline bool changeGpgCheckTo( TriBool ogpg[3], TriBool g, TriBool r, TriBool p )
439 bool changed = false;
440 if ( changeGpgCheckTo( ogpg[0], g ) ) changed = true;
441 if ( changeGpgCheckTo( ogpg[1], r ) ) changed = true;
442 if ( changeGpgCheckTo( ogpg[2], p ) ) changed = true;
446 ///////////////////////////////////////////////////////////////////
447 bool RepoInfo::setGpgCheck( GpgCheck mode_r )
449 TriBool ogpg[3]; // Gpg RepoGpg PkgGpg
450 getRawGpgChecks( ogpg[0], ogpg[1], ogpg[2] );
452 bool changed = false;
456 changed = changeGpgCheckTo( ogpg, true, indeterminate, indeterminate );
458 case GpgCheck::Strict:
459 changed = changeGpgCheckTo( ogpg, true, true, true );
461 case GpgCheck::AllowUnsigned:
462 changed = changeGpgCheckTo( ogpg, true, false, false );
464 case GpgCheck::AllowUnsignedRepo:
465 changed = changeGpgCheckTo( ogpg, true, false, indeterminate );
467 case GpgCheck::AllowUnsignedPackage:
468 changed = changeGpgCheckTo( ogpg, true, indeterminate, false );
470 case GpgCheck::Default:
471 changed = changeGpgCheckTo( ogpg, indeterminate, indeterminate, indeterminate );
474 changed = changeGpgCheckTo( ogpg, false, indeterminate, indeterminate );
476 case GpgCheck::indeterminate: // no change
482 setGpgCheck ( ogpg[0] );
483 setRepoGpgCheck( ogpg[1] );
484 setPkgGpgCheck ( ogpg[2] );
489 void RepoInfo::setMirrorListUrl( const Url & url_r ) // Raw
490 { _pimpl->_mirrorListUrl.raw() = url_r; _pimpl->_mirrorListForceMetalink = false; }
492 void RepoInfo::setMetalinkUrl( const Url & url_r ) // Raw
493 { _pimpl->_mirrorListUrl.raw() = url_r; _pimpl->_mirrorListForceMetalink = true; }
495 void RepoInfo::setGpgKeyUrls( url_set urls )
496 { _pimpl->gpgKeyUrls().raw().swap( urls ); }
498 void RepoInfo::setGpgKeyUrl( const Url & url_r )
500 _pimpl->gpgKeyUrls().raw().clear();
501 _pimpl->gpgKeyUrls().raw().push_back( url_r );
504 Pathname RepoInfo::provideKey(const std::string &keyID_r, const Pathname &targetDirectory_r)
506 MIL << "Check for " << keyID_r << " at " << targetDirectory_r << endl;
507 std::string keyIDStr( keyID_r.size() > 8 ? keyID_r.substr( keyID_r.size()-8 ) : keyID_r ); // print short ID in Jobreports
508 filesystem::TmpDir tmpKeyRingDir;
509 KeyRing tempKeyRing(tmpKeyRingDir.path());
511 // translator: %1% is a gpg key ID like 3DBDC284
512 // %2% is a cache directories path
513 JobReport::info( str::Format(_("Looking for gpg key ID %1% in cache %2%.") ) % keyIDStr % targetDirectory_r );
514 filesystem::dirForEach(targetDirectory_r,
515 StrMatcher(".key", Match::STRINGEND),
516 [&tempKeyRing]( const Pathname & dir_r, const std::string & str_r ){
519 // deprecate a month old keys
520 PathInfo fileInfo ( dir_r/str_r );
521 if ( Date::now() - fileInfo.mtime() > Date::month ) {
522 //if unlink fails, the file will be overriden in the next step, no need
524 filesystem::unlink( dir_r/str_r );
526 tempKeyRing.multiKeyImport(dir_r/str_r, true);
528 } catch (const KeyRingException& e) {
530 ERR << "Error importing cached key from file '"<<dir_r/str_r<<"'."<<endl;
535 // no key in the cache is what we are looking for, lets download
536 // all keys specified in gpgkey= entries
537 if ( !tempKeyRing.isKeyTrusted(keyID_r) ) {
538 if ( ! gpgKeyUrlsEmpty() ) {
539 // translator: %1% is a gpg key ID like 3DBDC284
540 // %2% is a repositories name
541 JobReport::info( str::Format(_("Looking for gpg key ID %1% in repository %2%.") ) % keyIDStr % asUserString() );
542 for ( const Url &url : gpgKeyUrls() ) {
544 JobReport::info( " gpgkey=" + url.asString() );
545 ManagedFile f = MediaSetAccess::provideOptionalFileFromUrl( url );
550 if ( !key.isValid() )
553 // import all keys into our temporary keyring
554 tempKeyRing.multiKeyImport(f, true);
556 } catch ( const std::exception & e ) {
557 //ignore and continue to next url
559 MIL << "Key import from url:'"<<url<<"' failed." << endl;
564 // translator: %1% is a repositories name
565 JobReport::info( str::Format(_("Repository %1% does not define additional 'gpgkey=' URLs.") ) % asUserString() );
569 filesystem::assert_dir( targetDirectory_r );
571 //now write all keys into their own files in cache, override existing ones to always have
572 //up to date key data
573 for ( const auto & key: tempKeyRing.trustedPublicKeyData()) {
574 MIL << "KEY ID in KEYRING: " << key.id() << endl;
576 Pathname keyFile = targetDirectory_r/(str::Format("%1%.key") % key.rpmName()).asString();
578 std::ofstream fout( keyFile.c_str(), std::ios_base::out | std::ios_base::trunc );
581 ZYPP_THROW(Exception(str::form("Cannot open file %s",keyFile.c_str())));
583 tempKeyRing.dumpTrustedPublicKey( key.id(), fout );
586 // key is STILL not known, we give up
587 if ( !tempKeyRing.isKeyTrusted(keyID_r) ) {
591 PublicKeyData keyData( tempKeyRing.trustedPublicKeyData( keyID_r ) );
593 ERR << "Error when exporting key from temporary keychain." << endl;
597 return targetDirectory_r/(str::Format("%1%.key") % keyData.rpmName()).asString();
600 void RepoInfo::addBaseUrl( const Url & url_r )
602 for ( const auto & url : _pimpl->baseUrls().raw() ) // Raw unique!
605 _pimpl->baseUrls().raw().push_back( url_r );
608 void RepoInfo::setBaseUrl( const Url & url_r )
610 _pimpl->baseUrls().raw().clear();
611 _pimpl->baseUrls().raw().push_back( url_r );
614 void RepoInfo::setBaseUrls( url_set urls )
615 { _pimpl->baseUrls().raw().swap( urls ); }
617 void RepoInfo::setPath( const Pathname &path )
618 { _pimpl->path = path; }
620 void RepoInfo::setType( const repo::RepoType &t )
621 { _pimpl->type = t; }
623 void RepoInfo::setProbedType( const repo::RepoType &t ) const
624 { _pimpl->setProbedType( t ); }
627 void RepoInfo::setMetadataPath( const Pathname &path )
628 { _pimpl->metadataPath( path ); }
630 void RepoInfo::setPackagesPath( const Pathname &path )
631 { _pimpl->packagesPath( path ); }
633 void RepoInfo::setKeepPackages( bool keep )
634 { _pimpl->keeppackages = keep; }
636 void RepoInfo::setService( const std::string& name )
637 { _pimpl->service = name; }
639 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
640 { _pimpl->targetDistro = targetDistribution; }
642 bool RepoInfo::keepPackages() const
643 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
645 Pathname RepoInfo::metadataPath() const
646 { return _pimpl->metadataPath(); }
648 Pathname RepoInfo::packagesPath() const
649 { return _pimpl->packagesPath(); }
651 bool RepoInfo::usesAutoMethadataPaths() const
652 { return _pimpl->usesAutoMethadataPaths(); }
654 repo::RepoType RepoInfo::type() const
655 { return _pimpl->type; }
657 Url RepoInfo::mirrorListUrl() const // Variables replaced!
658 { return _pimpl->_mirrorListUrl.transformed(); }
660 Url RepoInfo::rawMirrorListUrl() const // Raw
661 { return _pimpl->_mirrorListUrl.raw(); }
663 bool RepoInfo::gpgKeyUrlsEmpty() const
664 { return _pimpl->gpgKeyUrls().empty(); }
666 RepoInfo::urls_size_type RepoInfo::gpgKeyUrlsSize() const
667 { return _pimpl->gpgKeyUrls().size(); }
669 RepoInfo::url_set RepoInfo::gpgKeyUrls() const // Variables replaced!
670 { return _pimpl->gpgKeyUrls().transformed(); }
672 RepoInfo::url_set RepoInfo::rawGpgKeyUrls() const // Raw
673 { return _pimpl->gpgKeyUrls().raw(); }
675 Url RepoInfo::gpgKeyUrl() const // Variables replaced!
676 { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().transformedBegin() ); }
678 Url RepoInfo::rawGpgKeyUrl() const // Raw
679 { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().rawBegin() ) ; }
681 RepoInfo::url_set RepoInfo::baseUrls() const // Variables replaced!
682 { return _pimpl->baseUrls().transformed(); }
684 RepoInfo::url_set RepoInfo::rawBaseUrls() const // Raw
685 { return _pimpl->baseUrls().raw(); }
687 Pathname RepoInfo::path() const
688 { return _pimpl->path; }
690 std::string RepoInfo::service() const
691 { return _pimpl->service; }
693 std::string RepoInfo::targetDistribution() const
694 { return _pimpl->targetDistro; }
696 Url RepoInfo::rawUrl() const
697 { return( _pimpl->baseUrls().empty() ? Url() : *_pimpl->baseUrls().rawBegin() ); }
699 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
700 { return _pimpl->baseUrls().transformedBegin(); }
702 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
703 { return _pimpl->baseUrls().transformedEnd(); }
705 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
706 { return _pimpl->baseUrls().size(); }
708 bool RepoInfo::baseUrlsEmpty() const
709 { return _pimpl->baseUrls().empty(); }
711 bool RepoInfo::baseUrlSet() const
712 { return _pimpl->baseurl2dump(); }
714 const std::set<std::string> & RepoInfo::contentKeywords() const
715 { return _pimpl->contentKeywords(); }
717 void RepoInfo::addContent( const std::string & keyword_r )
718 { _pimpl->addContent( keyword_r ); }
720 bool RepoInfo::hasContent() const
721 { return _pimpl->hasContent(); }
723 bool RepoInfo::hasContent( const std::string & keyword_r ) const
724 { return _pimpl->hasContent( keyword_r ); }
726 ///////////////////////////////////////////////////////////////////
728 bool RepoInfo::hasLicense() const
729 { return hasLicense( std::string() ); }
731 bool RepoInfo::hasLicense( const std::string & name_r ) const
732 { return !_pimpl->licenseTgz( name_r ).empty(); }
735 bool RepoInfo::needToAcceptLicense() const
736 { return needToAcceptLicense( std::string() ); }
738 bool RepoInfo::needToAcceptLicense( const std::string & name_r ) const
740 const Pathname & licenseTgz( _pimpl->licenseTgz( name_r ) );
741 if ( licenseTgz.empty() )
742 return false; // no licenses at all
744 ExternalProgram::Arguments cmd;
745 cmd.push_back( "tar" );
746 cmd.push_back( "-t" );
747 cmd.push_back( "-z" );
748 cmd.push_back( "-f" );
749 cmd.push_back( licenseTgz.asString() );
750 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
753 static const std::string noAcceptanceFile = "no-acceptance-needed\n";
754 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
756 if ( output == noAcceptanceFile )
762 MIL << "License(" << name_r << ") in " << name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
767 std::string RepoInfo::getLicense( const Locale & lang_r )
768 { return const_cast<const RepoInfo *>(this)->getLicense( std::string(), lang_r ); }
770 std::string RepoInfo::getLicense( const Locale & lang_r ) const
771 { return getLicense( std::string(), lang_r ); }
773 std::string RepoInfo::getLicense( const std::string & name_r, const Locale & lang_r ) const
775 LocaleSet avlocales( getLicenseLocales( name_r ) );
776 if ( avlocales.empty() )
777 return std::string();
779 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
780 if ( !getLang && avlocales.find( Locale::noCode ) == avlocales.end() )
782 WAR << "License(" << name_r << ") in " << name() << " contains no fallback text!" << endl;
783 // Using the fist locale instead of returning no text at all.
784 // So the user might recognize that there is a license, even if he
786 getLang = *avlocales.begin();
789 // now extract the license file.
790 static const std::string licenseFileFallback( "license.txt" );
791 std::string licenseFile( !getLang ? licenseFileFallback
792 : str::form( "license.%s.txt", getLang.c_str() ) );
794 ExternalProgram::Arguments cmd;
795 cmd.push_back( "tar" );
796 cmd.push_back( "-x" );
797 cmd.push_back( "-z" );
798 cmd.push_back( "-O" );
799 cmd.push_back( "-f" );
800 cmd.push_back( _pimpl->licenseTgz( name_r ).asString() ); // if it not exists, avlocales was empty.
801 cmd.push_back( licenseFile );
804 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
805 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
814 LocaleSet RepoInfo::getLicenseLocales() const
815 { return getLicenseLocales( std::string() ); }
817 LocaleSet RepoInfo::getLicenseLocales( const std::string & name_r ) const
819 const Pathname & licenseTgz( _pimpl->licenseTgz( name_r ) );
820 if ( licenseTgz.empty() )
823 ExternalProgram::Arguments cmd;
824 cmd.push_back( "tar" );
825 cmd.push_back( "-t" );
826 cmd.push_back( "-z" );
827 cmd.push_back( "-f" );
828 cmd.push_back( licenseTgz.asString() );
831 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
832 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
834 static const C_Str license( "license." );
835 static const C_Str dotTxt( ".txt\n" );
836 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
838 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
839 ret.insert( Locale() );
841 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
848 ///////////////////////////////////////////////////////////////////
850 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
852 RepoInfoBase::dumpOn(str);
853 if ( _pimpl->baseurl2dump() )
855 for ( const auto & url : _pimpl->baseUrls().raw() )
857 str << "- url : " << url << std::endl;
861 // print if non empty value
862 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
863 if ( ! value_r.empty() )
864 str << tag_r << value_r << std::endl;
867 strif( (_pimpl->_mirrorListForceMetalink ? "- metalink : " : "- mirrorlist : "), rawMirrorListUrl().asString() );
868 strif( "- path : ", path().asString() );
869 str << "- type : " << type() << std::endl;
870 str << "- priority : " << priority() << std::endl;
872 // Yes No Default(Y) Default(N)
873 #define OUTS(T,B) ( indeterminate(T) ? (std::string("D(")+(B?"Y":"N")+")") : ((bool)T?"Y":"N") )
874 str << "- gpgcheck : " << OUTS(_pimpl->rawGpgCheck(),gpgCheck())
875 << " repo" << OUTS(_pimpl->rawRepoGpgCheck(),repoGpgCheck()) << (repoGpgCheckIsMandatory() ? "* ": " " )
876 << "sig" << asString( validRepoSignature(), "?", "Y", "N" )
877 << " pkg" << OUTS(_pimpl->rawPkgGpgCheck(),pkgGpgCheck()) << (pkgGpgCheckIsMandatory() ? "* ": " " )
881 for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
883 str << "- gpgkey : " << url << std::endl;
886 if ( ! indeterminate(_pimpl->keeppackages) )
887 str << "- keeppackages: " << keepPackages() << std::endl;
889 strif( "- service : ", service() );
890 strif( "- targetdistro: ", targetDistribution() );
891 strif( "- filePath: ", filepath().asString() );
892 strif( "- metadataPath: ", metadataPath().asString() );
893 strif( "- packagesPath: ", packagesPath().asString() );
898 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
900 RepoInfoBase::dumpAsIniOn(str);
902 if ( _pimpl->baseurl2dump() )
906 for ( const auto & url : _pimpl->baseUrls().raw() )
908 str << indent << url << endl;
909 if ( indent.empty() ) indent = " "; // "baseurl="
913 if ( ! _pimpl->path.empty() )
914 str << "path="<< path() << endl;
916 if ( ! (rawMirrorListUrl().asString().empty()) )
917 str << (_pimpl->_mirrorListForceMetalink ? "metalink=" : "mirrorlist=") << rawMirrorListUrl() << endl;
919 str << "type=" << type().asString() << endl;
921 if ( priority() != defaultPriority() )
922 str << "priority=" << priority() << endl;
924 if ( ! indeterminate(_pimpl->rawGpgCheck()) )
925 str << "gpgcheck=" << (_pimpl->rawGpgCheck() ? "1" : "0") << endl;
927 if ( ! indeterminate(_pimpl->rawRepoGpgCheck()) )
928 str << "repo_gpgcheck=" << (_pimpl->rawRepoGpgCheck() ? "1" : "0") << endl;
930 if ( ! indeterminate(_pimpl->rawPkgGpgCheck()) )
931 str << "pkg_gpgcheck=" << (_pimpl->rawPkgGpgCheck() ? "1" : "0") << endl;
934 std::string indent( "gpgkey=");
935 for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
937 str << indent << url << endl;
938 if ( indent[0] != ' ' )
943 if (!indeterminate(_pimpl->keeppackages))
944 str << "keeppackages=" << keepPackages() << endl;
946 if( ! service().empty() )
947 str << "service=" << service() << endl;
952 std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
957 << " alias=\"" << escape(alias()) << "\""
958 << " name=\"" << escape(name()) << "\"";
959 if (type() != repo::RepoType::NONE)
960 str << " type=\"" << type().asString() << "\"";
962 << " priority=\"" << priority() << "\""
963 << " enabled=\"" << enabled() << "\""
964 << " autorefresh=\"" << autorefresh() << "\""
965 << " gpgcheck=\"" << gpgCheck() << "\""
966 << " repo_gpgcheck=\"" << repoGpgCheck() << "\""
967 << " pkg_gpgcheck=\"" << pkgGpgCheck() << "\"";
968 if (!(tmpstr = gpgKeyUrl().asString()).empty())
969 str << " gpgkey=\"" << escape(tmpstr) << "\"";
970 if (!(tmpstr = mirrorListUrl().asString()).empty())
971 str << (_pimpl->_mirrorListForceMetalink ? " metalink=\"" : " mirrorlist=\"") << escape(tmpstr) << "\"";
974 if ( _pimpl->baseurl2dump() )
976 for_( it, baseUrlsBegin(), baseUrlsEnd() ) // !transform iterator replaces variables
977 str << "<url>" << escape((*it).asString()) << "</url>" << endl;
980 str << "</repo>" << endl;
985 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
987 return obj.dumpOn(str);
990 std::ostream & operator<<( std::ostream & str, const RepoInfo::GpgCheck & obj )
994 #define OUTS( V ) case RepoInfo::V: return str << #V; break
995 OUTS( GpgCheck::On );
996 OUTS( GpgCheck::Strict );
997 OUTS( GpgCheck::AllowUnsigned );
998 OUTS( GpgCheck::AllowUnsignedRepo );
999 OUTS( GpgCheck::AllowUnsignedPackage );
1000 OUTS( GpgCheck::Default );
1001 OUTS( GpgCheck::Off );
1002 OUTS( GpgCheck::indeterminate );
1005 return str << "GpgCheck::UNKNOWN";
1008 /////////////////////////////////////////////////////////////////
1010 ///////////////////////////////////////////////////////////////////