1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoInfo.cc
15 #include "zypp/base/Logger.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 Url getmirrorListUrl() const
72 { return replacer(mirrorlist_url); }
74 Url &setmirrorListUrl()
75 { return mirrorlist_url; }
77 const url_set & baseUrls() const
79 if ( _baseUrls.empty() && ! getmirrorListUrl().asString().empty() )
82 DBG << "MetadataPath: " << metadatapath << endl;
83 const std::vector<Url> & rmurls( ( metadatapath.empty()
84 ? repo::RepoMirrorList( getmirrorListUrl() )
85 : repo::RepoMirrorList( getmirrorListUrl(), metadatapath ) ).getUrls() );
86 _baseUrls.insert( _baseUrls.end(), rmurls.begin(), rmurls.end() );
94 bool baseurl2dump() const
95 { return !emptybaseurls && !_baseUrls.empty(); }
98 void addContent( const std::string & keyword_r )
99 { _keywords.insert( keyword_r ); }
101 bool hasContent( const std::string & keyword_r ) const
103 if ( _keywords.empty() && ! metadatapath.empty() )
105 // HACK directly check master index file until RepoManager offers
106 // some content probing ans zypepr uses it.
107 /////////////////////////////////////////////////////////////////
108 MIL << "Empty keywords...." << metadatapath << endl;
110 if ( PathInfo( (master=metadatapath/"/repodata/repomd.xml") ).isFile() )
112 //MIL << "GO repomd.." << endl;
113 xml::Reader reader( master );
114 while ( reader.seekToNode( 2, "content" ) )
116 _keywords.insert( reader.nodeText().asString() );
117 reader.seekToEndNode( 2, "content" );
119 _keywords.insert( "" ); // valid content in _keywords even if empty
121 else if ( PathInfo( (master=metadatapath/"/content") ).isFile() )
123 //MIL << "GO content.." << endl;
124 iostr::forEachLine( InputStream( master ),
125 [this]( int num_r, std::string line_r )->bool
127 if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
129 std::vector<std::string> words;
130 if ( str::split( line_r, std::back_inserter(words) ) > 1
131 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
133 this->_keywords.insert( ++words.begin(), words.end() );
135 return true; // mult. occurrances are ok.
137 return( ! str::startsWith( line_r, "META " ) ); // no need to parse into META section.
139 _keywords.insert( "" );
141 /////////////////////////////////////////////////////////////////
143 return( _keywords.find( keyword_r ) != _keywords.end() );
149 TriBool keeppackages;
154 std::string targetDistro;
155 Pathname metadatapath;
156 Pathname packagespath;
157 DefaultIntegral<unsigned,defaultPriority> priority;
158 mutable bool emptybaseurls;
159 repo::RepoVariablesUrlReplacer replacer;
163 mutable url_set _baseUrls;
164 mutable std::set<std::string> _keywords;
166 friend Impl * rwcowClone<Impl>( const Impl * rhs );
167 /** clone for RWCOW_pointer */
169 { return new Impl( *this ); }
171 ///////////////////////////////////////////////////////////////////
173 /** \relates RepoInfo::Impl Stream output */
174 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
176 return str << "RepoInfo::Impl";
179 ///////////////////////////////////////////////////////////////////
181 // CLASS NAME : RepoInfo
183 ///////////////////////////////////////////////////////////////////
185 const RepoInfo RepoInfo::noRepo;
187 ///////////////////////////////////////////////////////////////////
189 // METHOD NAME : RepoInfo::RepoInfo
190 // METHOD TYPE : Ctor
193 : _pimpl( new Impl() )
196 ///////////////////////////////////////////////////////////////////
198 // METHOD NAME : RepoInfo::~RepoInfo
199 // METHOD TYPE : Dtor
201 RepoInfo::~RepoInfo()
206 unsigned RepoInfo::priority() const
207 { return _pimpl->priority; }
209 unsigned RepoInfo::defaultPriority()
210 { return Impl::defaultPriority; }
212 void RepoInfo::setPriority( unsigned newval_r )
213 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
215 void RepoInfo::setGpgCheck( bool check )
216 { _pimpl->gpgcheck = check; }
218 void RepoInfo::setMirrorListUrl( const Url & url_r )
219 { _pimpl->setmirrorListUrl() = url_r; }
221 void RepoInfo::setGpgKeyUrl( const Url & url_r )
222 { _pimpl->gpgkey_url = url_r; }
224 void RepoInfo::addBaseUrl( const Url & url_r )
226 for ( const auto & url : _pimpl->baseUrls() ) // unique!
229 _pimpl->baseUrls().push_back( url_r );
232 void RepoInfo::setBaseUrl( const Url & url_r )
234 _pimpl->baseUrls().clear();
235 _pimpl->baseUrls().push_back( url_r );
238 void RepoInfo::setPath( const Pathname &path )
239 { _pimpl->path = path; }
241 void RepoInfo::setType( const repo::RepoType &t )
242 { _pimpl->type = t; }
244 void RepoInfo::setProbedType( const repo::RepoType &t ) const
245 { _pimpl->setProbedType( t ); }
248 void RepoInfo::setMetadataPath( const Pathname &path )
249 { _pimpl->metadatapath = path; }
251 void RepoInfo::setPackagesPath( const Pathname &path )
252 { _pimpl->packagespath = path; }
254 void RepoInfo::setKeepPackages( bool keep )
255 { _pimpl->keeppackages = keep; }
257 void RepoInfo::setService( const std::string& name )
258 { _pimpl->service = name; }
260 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
261 { _pimpl->targetDistro = targetDistribution; }
263 bool RepoInfo::gpgCheck() const
264 { return indeterminate(_pimpl->gpgcheck) ? true : (bool)_pimpl->gpgcheck; }
266 bool RepoInfo::keepPackages() const
267 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
269 Pathname RepoInfo::metadataPath() const
270 { return _pimpl->metadatapath; }
272 Pathname RepoInfo::packagesPath() const
273 { return _pimpl->packagespath; }
275 repo::RepoType RepoInfo::type() const
276 { return _pimpl->type; }
278 Url RepoInfo::mirrorListUrl() const
279 { return _pimpl->getmirrorListUrl(); }
281 Url RepoInfo::gpgKeyUrl() const
282 { return _pimpl->gpgkey_url; }
284 RepoInfo::url_set RepoInfo::baseUrls() const
285 { return url_set( baseUrlsBegin(), baseUrlsEnd() ); } // Variables replaced!
287 Pathname RepoInfo::path() const
288 { return _pimpl->path; }
290 std::string RepoInfo::service() const
291 { return _pimpl->service; }
293 std::string RepoInfo::targetDistribution() const
294 { return _pimpl->targetDistro; }
296 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
298 return make_transform_iterator( _pimpl->baseUrls().begin(),
302 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
304 return make_transform_iterator( _pimpl->baseUrls().end(),
308 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
309 { return _pimpl->baseUrls().size(); }
311 bool RepoInfo::baseUrlsEmpty() const
312 { return _pimpl->baseUrls().empty(); }
314 bool RepoInfo::baseUrlSet() const
315 { return _pimpl->baseurl2dump(); }
318 void RepoInfo::addContent( const std::string & keyword_r )
319 { _pimpl->addContent( keyword_r ); }
321 bool RepoInfo::hasContent( const std::string & keyword_r ) const
322 { return _pimpl->hasContent( keyword_r ); }
324 ///////////////////////////////////////////////////////////////////
326 bool RepoInfo::hasLicense() const
328 Pathname licenseTgz( _pimpl->licenseTgz() );
329 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
332 bool RepoInfo::needToAcceptLicense() const
334 static const std::string noAcceptanceFile = "no-acceptance-needed\n";
337 Pathname licenseTgz( _pimpl->licenseTgz() );
338 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
339 return false; // no licenses at all
341 ExternalProgram::Arguments cmd;
342 cmd.push_back( "tar" );
343 cmd.push_back( "-t" );
344 cmd.push_back( "-z" );
345 cmd.push_back( "-f" );
346 cmd.push_back( licenseTgz.asString() );
348 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
349 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
351 if ( output == noAcceptanceFile )
356 MIL << "License for " << this->name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
360 std::string RepoInfo::getLicense( const Locale & lang_r )
362 LocaleSet avlocales( getLicenseLocales() );
363 if ( avlocales.empty() )
364 return std::string();
366 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
367 if ( getLang == Locale::noCode
368 && avlocales.find( Locale::noCode ) == avlocales.end() )
370 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
371 // Using the fist locale instead of returning no text at all.
372 // So the user might recognize that there is a license, even if he
374 getLang = *avlocales.begin();
377 // now extract the license file.
378 static const std::string licenseFileFallback( "license.txt" );
379 std::string licenseFile( getLang == Locale::noCode
380 ? licenseFileFallback
381 : str::form( "license.%s.txt", getLang.code().c_str() ) );
383 ExternalProgram::Arguments cmd;
384 cmd.push_back( "tar" );
385 cmd.push_back( "-x" );
386 cmd.push_back( "-z" );
387 cmd.push_back( "-O" );
388 cmd.push_back( "-f" );
389 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
390 cmd.push_back( licenseFile );
393 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
394 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
402 LocaleSet RepoInfo::getLicenseLocales() const
404 Pathname licenseTgz( _pimpl->licenseTgz() );
405 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
408 ExternalProgram::Arguments cmd;
409 cmd.push_back( "tar" );
410 cmd.push_back( "-t" );
411 cmd.push_back( "-z" );
412 cmd.push_back( "-f" );
413 cmd.push_back( licenseTgz.asString() );
416 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
417 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
419 static const C_Str license( "license." );
420 static const C_Str dotTxt( ".txt\n" );
421 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
423 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
424 ret.insert( Locale() );
426 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
433 ///////////////////////////////////////////////////////////////////
435 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
437 RepoInfoBase::dumpOn(str);
438 if ( _pimpl->baseurl2dump() )
440 for ( const auto & url : _pimpl->baseUrls() )
442 str << "- url : " << url << std::endl;
446 // print if non empty value
447 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
448 if ( ! value_r.empty() )
449 str << tag_r << value_r << std::endl;
452 strif( "- mirrorlist : ", _pimpl->getmirrorListUrl().asString() );
453 strif( "- path : ", path().asString() );
454 str << "- type : " << type() << std::endl;
455 str << "- priority : " << priority() << std::endl;
456 str << "- gpgcheck : " << gpgCheck() << std::endl;
457 strif( "- gpgkey : ", gpgKeyUrl().asString() );
459 if ( ! indeterminate(_pimpl->keeppackages) )
460 str << "- keeppackages: " << keepPackages() << std::endl;
462 strif( "- service : ", service() );
463 strif( "- targetdistro: ", targetDistribution() );
464 strif( "- metadataPath: ", metadataPath().asString() );
465 strif( "- packagesPath: ", packagesPath().asString() );
470 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
472 RepoInfoBase::dumpAsIniOn(str);
474 if ( _pimpl->baseurl2dump() )
478 for ( const auto & url : _pimpl->baseUrls() )
480 str << indent << url << endl;
481 if ( indent.empty() ) indent = " "; // "baseurl="
485 if ( ! _pimpl->path.empty() )
486 str << "path="<< path() << endl;
488 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
489 str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
491 str << "type=" << type().asString() << endl;
493 if ( priority() != defaultPriority() )
494 str << "priority=" << priority() << endl;
496 if (!indeterminate(_pimpl->gpgcheck))
497 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
498 if ( ! (gpgKeyUrl().asString().empty()) )
499 str << "gpgkey=" <<gpgKeyUrl() << endl;
501 if (!indeterminate(_pimpl->keeppackages))
502 str << "keeppackages=" << keepPackages() << endl;
504 if( ! service().empty() )
505 str << "service=" << service() << endl;
510 std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
515 << " alias=\"" << escape(alias()) << "\""
516 << " name=\"" << escape(name()) << "\"";
517 if (type() != repo::RepoType::NONE)
518 str << " type=\"" << type().asString() << "\"";
520 << " priority=\"" << priority() << "\""
521 << " enabled=\"" << enabled() << "\""
522 << " autorefresh=\"" << autorefresh() << "\""
523 << " gpgcheck=\"" << gpgCheck() << "\"";
524 if (!(tmpstr = gpgKeyUrl().asString()).empty())
525 str << " gpgkey=\"" << escape(tmpstr) << "\"";
526 if (!(tmpstr = mirrorListUrl().asString()).empty())
527 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
530 if ( _pimpl->baseurl2dump() )
532 for ( const auto & url : _pimpl->baseUrls() )
533 str << "<url>" << escape(url.asString()) << "</url>" << endl;
536 str << "</repo>" << endl;
541 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
543 return obj.dumpOn(str);
547 /////////////////////////////////////////////////////////////////
549 ///////////////////////////////////////////////////////////////////