1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoInfo.cc
15 #include "zypp/base/LogTools.h"
16 #include "zypp/base/DefaultIntegral.h"
17 #include "zypp/parser/xml/XmlEscape.h"
19 #include "zypp/RepoInfo.h"
20 #include "zypp/TriBool.h"
21 #include "zypp/Pathname.h"
22 #include "zypp/ZConfig.h"
23 #include "zypp/repo/RepoMirrorList.h"
24 #include "zypp/ExternalProgram.h"
25 #include "zypp/media/MediaAccess.h"
27 #include "zypp/base/IOStream.h"
28 #include "zypp/base/InputStream.h"
29 #include "zypp/parser/xml/Reader.h"
32 using zypp::xml::escape;
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////
40 // CLASS NAME : RepoInfo::Impl
42 /** RepoInfo implementation. */
46 : _gpgCheck( indeterminate )
47 , _repoGpgCheck( indeterminate )
48 , _pkgGpgCheck( indeterminate )
49 , _validRepoSignature( indeterminate )
50 , keeppackages(indeterminate)
51 , type(repo::RepoType::NONE_e)
52 , emptybaseurls(false)
59 static const unsigned defaultPriority = 99;
61 void setProbedType( const repo::RepoType & t ) const
63 if ( type == repo::RepoType::NONE
64 && t != repo::RepoType::NONE )
67 const_cast<Impl*>(this)->type = t;
72 Pathname licenseTgz() const
73 { return metadatapath.empty() ? Pathname() : metadatapath / path / "license.tar.gz"; }
75 const RepoVariablesReplacedUrlList & baseUrls() const
77 const Url & mlurl( _mirrorListUrl.transformed() ); // Variables replaced!
78 if ( _baseUrls.empty() && ! mlurl.asString().empty() )
81 DBG << "MetadataPath: " << metadatapath << endl;
82 repo::RepoMirrorList rmurls( mlurl, metadatapath );
83 _baseUrls.raw().insert( _baseUrls.raw().end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
88 RepoVariablesReplacedUrlList & baseUrls()
91 bool baseurl2dump() const
92 { return !emptybaseurls && !_baseUrls.empty(); }
95 const RepoVariablesReplacedUrlList & gpgKeyUrls() const
96 { return _gpgKeyUrls; }
98 RepoVariablesReplacedUrlList & gpgKeyUrls()
99 { return _gpgKeyUrls; }
101 void addContent( const std::string & keyword_r )
102 { _keywords.insert( keyword_r ); }
104 bool hasContent( const std::string & keyword_r ) const
106 if ( _keywords.empty() && ! metadatapath.empty() )
108 // HACK directly check master index file until RepoManager offers
109 // some content probing ans zypepr uses it.
110 /////////////////////////////////////////////////////////////////
111 MIL << "Empty keywords...." << metadatapath << endl;
113 if ( PathInfo( (master=metadatapath/"/repodata/repomd.xml") ).isFile() )
115 //MIL << "GO repomd.." << endl;
116 xml::Reader reader( master );
117 while ( reader.seekToNode( 2, "content" ) )
119 _keywords.insert( reader.nodeText().asString() );
120 reader.seekToEndNode( 2, "content" );
122 _keywords.insert( "" ); // valid content in _keywords even if empty
124 else if ( PathInfo( (master=metadatapath/"/content") ).isFile() )
126 //MIL << "GO content.." << endl;
127 iostr::forEachLine( InputStream( master ),
128 [this]( int num_r, std::string line_r )->bool
130 if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
132 std::vector<std::string> words;
133 if ( str::split( line_r, std::back_inserter(words) ) > 1
134 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
136 this->_keywords.insert( ++words.begin(), words.end() );
138 return true; // mult. occurrances are ok.
140 return( ! str::startsWith( line_r, "META " ) ); // no need to parse into META section.
142 _keywords.insert( "" );
144 /////////////////////////////////////////////////////////////////
146 return( _keywords.find( keyword_r ) != _keywords.end() );
149 /** Signature check result needs to be stored/retrieved from _metadatapath.
150 * Don't call them from outside validRepoSignature/setValidRepoSignature
153 TriBool internalValidRepoSignature() const
155 if ( ! indeterminate(_validRepoSignature) ) return _validRepoSignature;
157 if ( ! metadatapath.empty() )
159 //TODO: a missing ".repo_gpgcheck" might be plaindir(no Downloader) or not yet refreshed signed repo!
160 TriBool linkval = triBoolFromPath( metadatapath / ".repo_gpgcheck" );
163 return indeterminate;
166 void internalSetValidRepoSignature( TriBool value_r )
168 if ( PathInfo(metadatapath).isDir() )
170 Pathname gpgcheckFile( metadatapath / ".repo_gpgcheck" );
171 if ( PathInfo(gpgcheckFile).isExist() )
173 TriBool linkval( indeterminate );
174 if ( triBoolFromPath( gpgcheckFile, linkval ) && linkval == value_r )
175 return; // existing symlink fits value_r
177 filesystem::unlink( gpgcheckFile ); // will write a new one
179 filesystem::symlink( asString(value_r), gpgcheckFile );
181 _validRepoSignature = value_r;
184 bool triBoolFromPath( const Pathname & path_r, TriBool & ret_r ) const
186 static const Pathname truePath( "true" );
187 static const Pathname falsePath( "false" );
188 static const Pathname indeterminatePath( "indeterminate" );
189 Pathname linkval( filesystem::readlink( path_r ) );
191 if ( linkval == truePath )
193 else if ( linkval == falsePath )
195 else if ( linkval == indeterminatePath )
196 ret_r = indeterminate;
202 TriBool triBoolFromPath( const Pathname & path_r ) const
203 { TriBool ret(indeterminate); triBoolFromPath( path_r, ret ); return ret; }
208 TriBool _gpgCheck; ///< default gpgcheck behavior: Y/N/ZConf
209 TriBool _repoGpgCheck; ///< need to check repo sign.: Y/N/(ZConf(Y/N/gpgCheck))
210 TriBool _pkgGpgCheck; ///< need to check pkg sign.: Y/N/(ZConf(Y/N/gpgCheck && no valid repo sign.))
212 TriBool _validRepoSignature;///< have signed and valid repo metadata
214 TriBool keeppackages;
215 RepoVariablesReplacedUrl _mirrorListUrl;
219 std::string targetDistro;
220 Pathname metadatapath;
221 Pathname packagespath;
222 DefaultIntegral<unsigned,defaultPriority> priority;
223 mutable bool emptybaseurls;
224 repo::RepoVariablesUrlReplacer replacer;
227 mutable RepoVariablesReplacedUrlList _baseUrls;
228 mutable std::set<std::string> _keywords;
230 RepoVariablesReplacedUrlList _gpgKeyUrls;
232 friend Impl * rwcowClone<Impl>( const Impl * rhs );
233 /** clone for RWCOW_pointer */
235 { return new Impl( *this ); }
237 ///////////////////////////////////////////////////////////////////
239 /** \relates RepoInfo::Impl Stream output */
240 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
242 return str << "RepoInfo::Impl";
245 ///////////////////////////////////////////////////////////////////
247 // CLASS NAME : RepoInfo
249 ///////////////////////////////////////////////////////////////////
251 const RepoInfo RepoInfo::noRepo;
254 : _pimpl( new Impl() )
257 RepoInfo::~RepoInfo()
260 unsigned RepoInfo::priority() const
261 { return _pimpl->priority; }
263 unsigned RepoInfo::defaultPriority()
264 { return Impl::defaultPriority; }
266 void RepoInfo::setPriority( unsigned newval_r )
267 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
270 bool RepoInfo::gpgCheck() const
271 { return indeterminate(_pimpl->_gpgCheck) ? ZConfig::instance().gpgCheck() : (bool)_pimpl->_gpgCheck; }
273 void RepoInfo::setGpgCheck( TriBool value_r )
274 { _pimpl->_gpgCheck = value_r; }
276 void RepoInfo::setGpgCheck( bool value_r ) // deprecated legacy and for squid
277 { setGpgCheck( TriBool(value_r) ); }
280 bool RepoInfo::repoGpgCheck() const
282 if ( ! indeterminate(_pimpl->_repoGpgCheck) ) return _pimpl->_repoGpgCheck;
283 if ( ! indeterminate(ZConfig::instance().repoGpgCheck()) ) return ZConfig::instance().repoGpgCheck();
284 return gpgCheck(); // no preference: follow gpgCheck
287 void RepoInfo::setRepoGpgCheck( TriBool value_r )
288 { _pimpl->_repoGpgCheck = value_r; }
291 bool RepoInfo::pkgGpgCheck() const
293 if ( ! indeterminate(_pimpl->_pkgGpgCheck) ) return _pimpl->_pkgGpgCheck;
294 if ( ! indeterminate(ZConfig::instance().pkgGpgCheck()) ) return ZConfig::instance().pkgGpgCheck();
295 // no preference: follow gpgCheck and check package if repo signature not available or not checked
296 return gpgCheck() && ( !repoGpgCheck() || !(bool)validRepoSignature() ); // !(bool)TriBool ==> false or indeterminate
299 void RepoInfo::setPkgGpgCheck( TriBool value_r )
300 { _pimpl->_pkgGpgCheck = value_r; }
302 void RepoInfo::getRawGpgChecks( TriBool & g_r, TriBool & r_r, TriBool & p_r ) const
304 g_r = _pimpl->_gpgCheck;
305 r_r = _pimpl->_repoGpgCheck;
306 p_r = _pimpl->_pkgGpgCheck;
309 TriBool RepoInfo::validRepoSignature() const
311 TriBool ret = _pimpl->internalValidRepoSignature();
312 // keep indeterminate(=unsigned) but invalidate any signature if !repoGpgCheck
313 if ( !indeterminate(ret) && !repoGpgCheck() )
318 void RepoInfo::setValidRepoSignature( TriBool value_r )
319 { _pimpl->internalSetValidRepoSignature( value_r ); }
322 void RepoInfo::setMirrorListUrl( const Url & url_r ) // Raw
323 { _pimpl->_mirrorListUrl.raw() = url_r; }
325 void RepoInfo::setGpgKeyUrls( url_set urls )
326 { _pimpl->gpgKeyUrls().raw().swap( urls ); }
328 void RepoInfo::setGpgKeyUrl( const Url & url_r )
330 _pimpl->gpgKeyUrls().raw().clear();
331 _pimpl->gpgKeyUrls().raw().push_back( url_r );
334 void RepoInfo::addBaseUrl( const Url & url_r )
336 for ( const auto & url : _pimpl->baseUrls().raw() ) // Raw unique!
339 _pimpl->baseUrls().raw().push_back( url_r );
342 void RepoInfo::setBaseUrl( const Url & url_r )
344 _pimpl->baseUrls().raw().clear();
345 _pimpl->baseUrls().raw().push_back( url_r );
348 void RepoInfo::setBaseUrls( url_set urls )
349 { _pimpl->baseUrls().raw().swap( urls ); }
351 void RepoInfo::setPath( const Pathname &path )
352 { _pimpl->path = path; }
354 void RepoInfo::setType( const repo::RepoType &t )
355 { _pimpl->type = t; }
357 void RepoInfo::setProbedType( const repo::RepoType &t ) const
358 { _pimpl->setProbedType( t ); }
361 void RepoInfo::setMetadataPath( const Pathname &path )
362 { _pimpl->metadatapath = path; }
364 void RepoInfo::setPackagesPath( const Pathname &path )
365 { _pimpl->packagespath = path; }
367 void RepoInfo::setKeepPackages( bool keep )
368 { _pimpl->keeppackages = keep; }
370 void RepoInfo::setService( const std::string& name )
371 { _pimpl->service = name; }
373 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
374 { _pimpl->targetDistro = targetDistribution; }
376 bool RepoInfo::keepPackages() const
377 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
379 Pathname RepoInfo::metadataPath() const
380 { return _pimpl->metadatapath; }
382 Pathname RepoInfo::packagesPath() const
383 { return _pimpl->packagespath; }
385 repo::RepoType RepoInfo::type() const
386 { return _pimpl->type; }
388 Url RepoInfo::mirrorListUrl() const // Variables replaced!
389 { return _pimpl->_mirrorListUrl.transformed(); }
391 Url RepoInfo::rawMirrorListUrl() const // Raw
392 { return _pimpl->_mirrorListUrl.raw(); }
394 bool RepoInfo::gpgKeyUrlsEmpty() const
395 { return _pimpl->gpgKeyUrls().empty(); }
397 RepoInfo::urls_size_type RepoInfo::gpgKeyUrlsSize() const
398 { return _pimpl->gpgKeyUrls().size(); }
400 RepoInfo::url_set RepoInfo::gpgKeyUrls() const // Variables replaced!
401 { return _pimpl->gpgKeyUrls().transformed(); }
403 RepoInfo::url_set RepoInfo::rawGpgKeyUrls() const // Raw
404 { return _pimpl->gpgKeyUrls().raw(); }
406 Url RepoInfo::gpgKeyUrl() const // Variables replaced!
407 { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().transformedBegin() ); }
409 Url RepoInfo::rawGpgKeyUrl() const // Raw
410 { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().rawBegin() ) ; }
412 RepoInfo::url_set RepoInfo::baseUrls() const // Variables replaced!
413 { return _pimpl->baseUrls().transformed(); }
415 RepoInfo::url_set RepoInfo::rawBaseUrls() const // Raw
416 { return _pimpl->baseUrls().raw(); }
418 Pathname RepoInfo::path() const
419 { return _pimpl->path; }
421 std::string RepoInfo::service() const
422 { return _pimpl->service; }
424 std::string RepoInfo::targetDistribution() const
425 { return _pimpl->targetDistro; }
427 Url RepoInfo::rawUrl() const
428 { return( _pimpl->baseUrls().empty() ? Url() : *_pimpl->baseUrls().rawBegin() ); }
430 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
431 { return _pimpl->baseUrls().transformedBegin(); }
433 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
434 { return _pimpl->baseUrls().transformedEnd(); }
436 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
437 { return _pimpl->baseUrls().size(); }
439 bool RepoInfo::baseUrlsEmpty() const
440 { return _pimpl->baseUrls().empty(); }
442 bool RepoInfo::baseUrlSet() const
443 { return _pimpl->baseurl2dump(); }
446 void RepoInfo::addContent( const std::string & keyword_r )
447 { _pimpl->addContent( keyword_r ); }
449 bool RepoInfo::hasContent( const std::string & keyword_r ) const
450 { return _pimpl->hasContent( keyword_r ); }
452 ///////////////////////////////////////////////////////////////////
454 bool RepoInfo::hasLicense() const
456 Pathname licenseTgz( _pimpl->licenseTgz() );
457 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
460 bool RepoInfo::needToAcceptLicense() const
462 static const std::string noAcceptanceFile = "no-acceptance-needed\n";
465 Pathname licenseTgz( _pimpl->licenseTgz() );
466 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
467 return false; // no licenses at all
469 ExternalProgram::Arguments cmd;
470 cmd.push_back( "tar" );
471 cmd.push_back( "-t" );
472 cmd.push_back( "-z" );
473 cmd.push_back( "-f" );
474 cmd.push_back( licenseTgz.asString() );
476 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
477 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
479 if ( output == noAcceptanceFile )
485 MIL << "License for " << name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
489 std::string RepoInfo::getLicense( const Locale & lang_r )
490 { return const_cast<const RepoInfo *>(this)->getLicense( lang_r ); }
492 std::string RepoInfo::getLicense( const Locale & lang_r ) const
494 LocaleSet avlocales( getLicenseLocales() );
495 if ( avlocales.empty() )
496 return std::string();
498 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
499 if ( getLang == Locale::noCode
500 && avlocales.find( Locale::noCode ) == avlocales.end() )
502 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
503 // Using the fist locale instead of returning no text at all.
504 // So the user might recognize that there is a license, even if he
506 getLang = *avlocales.begin();
509 // now extract the license file.
510 static const std::string licenseFileFallback( "license.txt" );
511 std::string licenseFile( getLang == Locale::noCode
512 ? licenseFileFallback
513 : str::form( "license.%s.txt", getLang.code().c_str() ) );
515 ExternalProgram::Arguments cmd;
516 cmd.push_back( "tar" );
517 cmd.push_back( "-x" );
518 cmd.push_back( "-z" );
519 cmd.push_back( "-O" );
520 cmd.push_back( "-f" );
521 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
522 cmd.push_back( licenseFile );
525 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
526 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
534 LocaleSet RepoInfo::getLicenseLocales() const
536 Pathname licenseTgz( _pimpl->licenseTgz() );
537 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
540 ExternalProgram::Arguments cmd;
541 cmd.push_back( "tar" );
542 cmd.push_back( "-t" );
543 cmd.push_back( "-z" );
544 cmd.push_back( "-f" );
545 cmd.push_back( licenseTgz.asString() );
548 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
549 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
551 static const C_Str license( "license." );
552 static const C_Str dotTxt( ".txt\n" );
553 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
555 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
556 ret.insert( Locale() );
558 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
565 ///////////////////////////////////////////////////////////////////
567 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
569 RepoInfoBase::dumpOn(str);
570 if ( _pimpl->baseurl2dump() )
572 for ( const auto & url : _pimpl->baseUrls().raw() )
574 str << "- url : " << url << std::endl;
578 // print if non empty value
579 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
580 if ( ! value_r.empty() )
581 str << tag_r << value_r << std::endl;
584 strif( "- mirrorlist : ", rawMirrorListUrl().asString() );
585 strif( "- path : ", path().asString() );
586 str << "- type : " << type() << std::endl;
587 str << "- priority : " << priority() << std::endl;
589 // Yes No Default(Y) Default(N)
590 #define OUTS(T,B) ( indeterminate(T) ? (std::string("D(")+(B?"Y":"N")+")") : ((bool)T?"Y":"N") )
591 str << "- gpgcheck : " << OUTS(_pimpl->_gpgCheck,gpgCheck())
592 << " repo" << OUTS(_pimpl->_repoGpgCheck,repoGpgCheck())
593 << " sig" << asString( validRepoSignature(), "?", "Y", "N" )
594 << " pkg" << OUTS(_pimpl->_pkgGpgCheck,pkgGpgCheck())
598 for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
600 str << "- gpgkey : " << url << std::endl;
603 if ( ! indeterminate(_pimpl->keeppackages) )
604 str << "- keeppackages: " << keepPackages() << std::endl;
606 strif( "- service : ", service() );
607 strif( "- targetdistro: ", targetDistribution() );
608 strif( "- metadataPath: ", metadataPath().asString() );
609 strif( "- packagesPath: ", packagesPath().asString() );
614 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
616 RepoInfoBase::dumpAsIniOn(str);
618 if ( _pimpl->baseurl2dump() )
622 for ( const auto & url : _pimpl->baseUrls().raw() )
624 str << indent << url << endl;
625 if ( indent.empty() ) indent = " "; // "baseurl="
629 if ( ! _pimpl->path.empty() )
630 str << "path="<< path() << endl;
632 if ( ! (rawMirrorListUrl().asString().empty()) )
633 str << "mirrorlist=" << rawMirrorListUrl() << endl;
635 str << "type=" << type().asString() << endl;
637 if ( priority() != defaultPriority() )
638 str << "priority=" << priority() << endl;
640 if ( ! indeterminate(_pimpl->_gpgCheck) )
641 str << "gpgcheck=" << (_pimpl->_gpgCheck ? "1" : "0") << endl;
643 if ( ! indeterminate(_pimpl->_repoGpgCheck) )
644 str << "repo_gpgcheck=" << (_pimpl->_repoGpgCheck ? "1" : "0") << endl;
646 if ( ! indeterminate(_pimpl->_pkgGpgCheck) )
647 str << "pkg_gpgcheck=" << (_pimpl->_pkgGpgCheck ? "1" : "0") << endl;
650 std::string indent( "gpgkey=");
651 for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
653 str << indent << url << endl;
654 if ( indent[0] != ' ' )
659 if (!indeterminate(_pimpl->keeppackages))
660 str << "keeppackages=" << keepPackages() << endl;
662 if( ! service().empty() )
663 str << "service=" << service() << endl;
668 std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
673 << " alias=\"" << escape(alias()) << "\""
674 << " name=\"" << escape(name()) << "\"";
675 if (type() != repo::RepoType::NONE)
676 str << " type=\"" << type().asString() << "\"";
678 << " priority=\"" << priority() << "\""
679 << " enabled=\"" << enabled() << "\""
680 << " autorefresh=\"" << autorefresh() << "\""
681 << " gpgcheck=\"" << gpgCheck() << "\""
682 << " repo_gpgcheck=\"" << repoGpgCheck() << "\""
683 << " pkg_gpgcheck=\"" << pkgGpgCheck() << "\"";
684 if (!(tmpstr = gpgKeyUrl().asString()).empty())
685 str << " gpgkey=\"" << escape(tmpstr) << "\"";
686 if (!(tmpstr = mirrorListUrl().asString()).empty())
687 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
690 if ( _pimpl->baseurl2dump() )
692 for_( it, baseUrlsBegin(), baseUrlsEnd() ) // !transform iterator replaces variables
693 str << "<url>" << escape((*it).asString()) << "</url>" << endl;
696 str << "</repo>" << endl;
701 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
703 return obj.dumpOn(str);
707 /////////////////////////////////////////////////////////////////
709 ///////////////////////////////////////////////////////////////////