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/repo/RepoMirrorList.h"
23 #include "zypp/ExternalProgram.h"
24 #include "zypp/media/MediaAccess.h"
26 #include "zypp/base/IOStream.h"
27 #include "zypp/base/InputStream.h"
28 #include "zypp/parser/xml/Reader.h"
31 using zypp::xml::escape;
33 ///////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
39 // CLASS NAME : RepoInfo::Impl
41 /** RepoInfo implementation. */
45 : gpgcheck(indeterminate)
46 , keeppackages(indeterminate)
47 , type(repo::RepoType::NONE_e)
48 , emptybaseurls(false)
55 static const unsigned defaultPriority = 99;
57 void setProbedType( const repo::RepoType & t ) const
59 if ( type == repo::RepoType::NONE
60 && t != repo::RepoType::NONE )
63 const_cast<Impl*>(this)->type = t;
68 Pathname licenseTgz() const
69 { return metadatapath.empty() ? Pathname() : metadatapath / path / "license.tar.gz"; }
71 const RepoVariablesReplacedUrlList & baseUrls() const
73 const Url & mlurl( _mirrorListUrl.transformed() ); // Variables replaced!
74 if ( _baseUrls.empty() && ! mlurl.asString().empty() )
77 DBG << "MetadataPath: " << metadatapath << endl;
78 repo::RepoMirrorList rmurls( mlurl, metadatapath );
79 _baseUrls.raw().insert( _baseUrls.raw().end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
84 RepoVariablesReplacedUrlList & baseUrls()
87 bool baseurl2dump() const
88 { return !emptybaseurls && !_baseUrls.empty(); }
91 void addContent( const std::string & keyword_r )
92 { _keywords.insert( keyword_r ); }
94 bool hasContent( const std::string & keyword_r ) const
96 if ( _keywords.empty() && ! metadatapath.empty() )
98 // HACK directly check master index file until RepoManager offers
99 // some content probing ans zypepr uses it.
100 /////////////////////////////////////////////////////////////////
101 MIL << "Empty keywords...." << metadatapath << endl;
103 if ( PathInfo( (master=metadatapath/"/repodata/repomd.xml") ).isFile() )
105 //MIL << "GO repomd.." << endl;
106 xml::Reader reader( master );
107 while ( reader.seekToNode( 2, "content" ) )
109 _keywords.insert( reader.nodeText().asString() );
110 reader.seekToEndNode( 2, "content" );
112 _keywords.insert( "" ); // valid content in _keywords even if empty
114 else if ( PathInfo( (master=metadatapath/"/content") ).isFile() )
116 //MIL << "GO content.." << endl;
117 iostr::forEachLine( InputStream( master ),
118 [this]( int num_r, std::string line_r )->bool
120 if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
122 std::vector<std::string> words;
123 if ( str::split( line_r, std::back_inserter(words) ) > 1
124 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
126 this->_keywords.insert( ++words.begin(), words.end() );
128 return true; // mult. occurrances are ok.
130 return( ! str::startsWith( line_r, "META " ) ); // no need to parse into META section.
132 _keywords.insert( "" );
134 /////////////////////////////////////////////////////////////////
136 return( _keywords.find( keyword_r ) != _keywords.end() );
141 TriBool keeppackages;
142 RepoVariablesReplacedUrl _gpgKeyUrl;
143 RepoVariablesReplacedUrl _mirrorListUrl;
147 std::string targetDistro;
148 Pathname metadatapath;
149 Pathname packagespath;
150 DefaultIntegral<unsigned,defaultPriority> priority;
151 mutable bool emptybaseurls;
152 repo::RepoVariablesUrlReplacer replacer;
155 mutable RepoVariablesReplacedUrlList _baseUrls;
156 mutable std::set<std::string> _keywords;
158 friend Impl * rwcowClone<Impl>( const Impl * rhs );
159 /** clone for RWCOW_pointer */
161 { return new Impl( *this ); }
163 ///////////////////////////////////////////////////////////////////
165 /** \relates RepoInfo::Impl Stream output */
166 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
168 return str << "RepoInfo::Impl";
171 ///////////////////////////////////////////////////////////////////
173 // CLASS NAME : RepoInfo
175 ///////////////////////////////////////////////////////////////////
177 const RepoInfo RepoInfo::noRepo;
179 ///////////////////////////////////////////////////////////////////
181 // METHOD NAME : RepoInfo::RepoInfo
182 // METHOD TYPE : Ctor
185 : _pimpl( new Impl() )
188 ///////////////////////////////////////////////////////////////////
190 // METHOD NAME : RepoInfo::~RepoInfo
191 // METHOD TYPE : Dtor
193 RepoInfo::~RepoInfo()
198 unsigned RepoInfo::priority() const
199 { return _pimpl->priority; }
201 unsigned RepoInfo::defaultPriority()
202 { return Impl::defaultPriority; }
204 void RepoInfo::setPriority( unsigned newval_r )
205 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
207 void RepoInfo::setGpgCheck( bool check )
208 { _pimpl->gpgcheck = check; }
210 void RepoInfo::setMirrorListUrl( const Url & url_r ) // Raw
211 { _pimpl->_mirrorListUrl.raw() = url_r; }
213 void RepoInfo::setGpgKeyUrl( const Url & url_r )
214 { _pimpl->_gpgKeyUrl.raw() = url_r; }
216 void RepoInfo::addBaseUrl( const Url & url_r )
218 for ( const auto & url : _pimpl->baseUrls().raw() ) // Raw unique!
221 _pimpl->baseUrls().raw().push_back( url_r );
224 void RepoInfo::setBaseUrl( const Url & url_r )
226 _pimpl->baseUrls().raw().clear();
227 _pimpl->baseUrls().raw().push_back( url_r );
230 void RepoInfo::setPath( const Pathname &path )
231 { _pimpl->path = path; }
233 void RepoInfo::setType( const repo::RepoType &t )
234 { _pimpl->type = t; }
236 void RepoInfo::setProbedType( const repo::RepoType &t ) const
237 { _pimpl->setProbedType( t ); }
240 void RepoInfo::setMetadataPath( const Pathname &path )
241 { _pimpl->metadatapath = path; }
243 void RepoInfo::setPackagesPath( const Pathname &path )
244 { _pimpl->packagespath = path; }
246 void RepoInfo::setKeepPackages( bool keep )
247 { _pimpl->keeppackages = keep; }
249 void RepoInfo::setService( const std::string& name )
250 { _pimpl->service = name; }
252 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
253 { _pimpl->targetDistro = targetDistribution; }
255 bool RepoInfo::gpgCheck() const
256 { return indeterminate(_pimpl->gpgcheck) ? true : (bool)_pimpl->gpgcheck; }
258 bool RepoInfo::keepPackages() const
259 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
261 Pathname RepoInfo::metadataPath() const
262 { return _pimpl->metadatapath; }
264 Pathname RepoInfo::packagesPath() const
265 { return _pimpl->packagespath; }
267 repo::RepoType RepoInfo::type() const
268 { return _pimpl->type; }
270 Url RepoInfo::mirrorListUrl() const // Variables replaced!
271 { return _pimpl->_mirrorListUrl.transformed(); }
273 Url RepoInfo::rawMirrorListUrl() const // Raw
274 { return _pimpl->_mirrorListUrl.raw(); }
276 Url RepoInfo::gpgKeyUrl() const // Variables replaced!
277 { return _pimpl->_gpgKeyUrl.transformed(); }
279 Url RepoInfo::rawGpgKeyUrl() const // Raw
280 { return _pimpl->_gpgKeyUrl.raw(); }
282 RepoInfo::url_set RepoInfo::baseUrls() const // Variables replaced!
283 { return _pimpl->baseUrls().transformed(); }
285 RepoInfo::url_set RepoInfo::rawBaseUrls() const // Raw
286 { return _pimpl->baseUrls().raw(); }
288 Pathname RepoInfo::path() const
289 { return _pimpl->path; }
291 std::string RepoInfo::service() const
292 { return _pimpl->service; }
294 std::string RepoInfo::targetDistribution() const
295 { return _pimpl->targetDistro; }
297 Url RepoInfo::rawUrl() const
298 { return( _pimpl->baseUrls().empty() ? Url() : *_pimpl->baseUrls().rawBegin() ); }
300 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
301 { return _pimpl->baseUrls().transformedBegin(); }
303 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
304 { return _pimpl->baseUrls().transformedEnd(); }
306 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
307 { return _pimpl->baseUrls().size(); }
309 bool RepoInfo::baseUrlsEmpty() const
310 { return _pimpl->baseUrls().empty(); }
312 bool RepoInfo::baseUrlSet() const
313 { return _pimpl->baseurl2dump(); }
316 void RepoInfo::addContent( const std::string & keyword_r )
317 { _pimpl->addContent( keyword_r ); }
319 bool RepoInfo::hasContent( const std::string & keyword_r ) const
320 { return _pimpl->hasContent( keyword_r ); }
322 ///////////////////////////////////////////////////////////////////
324 bool RepoInfo::hasLicense() const
326 Pathname licenseTgz( _pimpl->licenseTgz() );
327 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
330 bool RepoInfo::needToAcceptLicense() const
332 static const std::string noAcceptanceFile = "no-acceptance-needed\n";
335 Pathname licenseTgz( _pimpl->licenseTgz() );
336 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
337 return false; // no licenses at all
339 ExternalProgram::Arguments cmd;
340 cmd.push_back( "tar" );
341 cmd.push_back( "-t" );
342 cmd.push_back( "-z" );
343 cmd.push_back( "-f" );
344 cmd.push_back( licenseTgz.asString() );
346 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
347 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
349 if ( output == noAcceptanceFile )
354 MIL << "License for " << name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
358 std::string RepoInfo::getLicense( const Locale & lang_r )
359 { return const_cast<const RepoInfo *>(this)->getLicense( lang_r ); }
361 std::string RepoInfo::getLicense( const Locale & lang_r ) const
363 LocaleSet avlocales( getLicenseLocales() );
364 if ( avlocales.empty() )
365 return std::string();
367 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
368 if ( getLang == Locale::noCode
369 && avlocales.find( Locale::noCode ) == avlocales.end() )
371 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
372 // Using the fist locale instead of returning no text at all.
373 // So the user might recognize that there is a license, even if he
375 getLang = *avlocales.begin();
378 // now extract the license file.
379 static const std::string licenseFileFallback( "license.txt" );
380 std::string licenseFile( getLang == Locale::noCode
381 ? licenseFileFallback
382 : str::form( "license.%s.txt", getLang.code().c_str() ) );
384 ExternalProgram::Arguments cmd;
385 cmd.push_back( "tar" );
386 cmd.push_back( "-x" );
387 cmd.push_back( "-z" );
388 cmd.push_back( "-O" );
389 cmd.push_back( "-f" );
390 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
391 cmd.push_back( licenseFile );
394 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
395 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
403 LocaleSet RepoInfo::getLicenseLocales() const
405 Pathname licenseTgz( _pimpl->licenseTgz() );
406 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
409 ExternalProgram::Arguments cmd;
410 cmd.push_back( "tar" );
411 cmd.push_back( "-t" );
412 cmd.push_back( "-z" );
413 cmd.push_back( "-f" );
414 cmd.push_back( licenseTgz.asString() );
417 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
418 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
420 static const C_Str license( "license." );
421 static const C_Str dotTxt( ".txt\n" );
422 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
424 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
425 ret.insert( Locale() );
427 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
434 ///////////////////////////////////////////////////////////////////
436 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
438 RepoInfoBase::dumpOn(str);
439 if ( _pimpl->baseurl2dump() )
441 for ( const auto & url : _pimpl->baseUrls().raw() )
443 str << "- url : " << url << std::endl;
447 // print if non empty value
448 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
449 if ( ! value_r.empty() )
450 str << tag_r << value_r << std::endl;
453 strif( "- mirrorlist : ", rawMirrorListUrl().asString() );
454 strif( "- path : ", path().asString() );
455 str << "- type : " << type() << std::endl;
456 str << "- priority : " << priority() << std::endl;
457 str << "- gpgcheck : " << gpgCheck() << std::endl;
458 strif( "- gpgkey : ", rawGpgKeyUrl().asString() );
460 if ( ! indeterminate(_pimpl->keeppackages) )
461 str << "- keeppackages: " << keepPackages() << std::endl;
463 strif( "- service : ", service() );
464 strif( "- targetdistro: ", targetDistribution() );
465 strif( "- metadataPath: ", metadataPath().asString() );
466 strif( "- packagesPath: ", packagesPath().asString() );
471 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
473 RepoInfoBase::dumpAsIniOn(str);
475 if ( _pimpl->baseurl2dump() )
479 for ( const auto & url : _pimpl->baseUrls().raw() )
481 str << indent << url << endl;
482 if ( indent.empty() ) indent = " "; // "baseurl="
486 if ( ! _pimpl->path.empty() )
487 str << "path="<< path() << endl;
489 if ( ! (rawMirrorListUrl().asString().empty()) )
490 str << "mirrorlist=" << rawMirrorListUrl() << endl;
492 str << "type=" << type().asString() << endl;
494 if ( priority() != defaultPriority() )
495 str << "priority=" << priority() << endl;
497 if (!indeterminate(_pimpl->gpgcheck))
498 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
500 if ( ! (rawGpgKeyUrl().asString().empty()) )
501 str << "gpgkey=" << rawGpgKeyUrl() << endl;
503 if (!indeterminate(_pimpl->keeppackages))
504 str << "keeppackages=" << keepPackages() << endl;
506 if( ! service().empty() )
507 str << "service=" << service() << endl;
512 std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
517 << " alias=\"" << escape(alias()) << "\""
518 << " name=\"" << escape(name()) << "\"";
519 if (type() != repo::RepoType::NONE)
520 str << " type=\"" << type().asString() << "\"";
522 << " priority=\"" << priority() << "\""
523 << " enabled=\"" << enabled() << "\""
524 << " autorefresh=\"" << autorefresh() << "\""
525 << " gpgcheck=\"" << gpgCheck() << "\"";
526 if (!(tmpstr = gpgKeyUrl().asString()).empty())
527 str << " gpgkey=\"" << escape(tmpstr) << "\"";
528 if (!(tmpstr = mirrorListUrl().asString()).empty())
529 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
532 if ( _pimpl->baseurl2dump() )
534 for_( it, baseUrlsBegin(), baseUrlsEnd() ) // !transform iterator replaces variables
535 str << "<url>" << escape((*it).asString()) << "</url>" << endl;
538 str << "</repo>" << endl;
543 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
545 return obj.dumpOn(str);
549 /////////////////////////////////////////////////////////////////
551 ///////////////////////////////////////////////////////////////////