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 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 repo::RepoMirrorList rmurls( getmirrorListUrl(), metadatapath );
84 _baseUrls.insert( _baseUrls.end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
92 bool baseurl2dump() const
93 { return !emptybaseurls && !_baseUrls.empty(); }
96 void addContent( const std::string & keyword_r )
97 { _keywords.insert( keyword_r ); }
99 bool hasContent( const std::string & keyword_r ) const
101 if ( _keywords.empty() && ! metadatapath.empty() )
103 // HACK directly check master index file until RepoManager offers
104 // some content probing ans zypepr uses it.
105 /////////////////////////////////////////////////////////////////
106 MIL << "Empty keywords...." << metadatapath << endl;
108 if ( PathInfo( (master=metadatapath/"/repodata/repomd.xml") ).isFile() )
110 //MIL << "GO repomd.." << endl;
111 xml::Reader reader( master );
112 while ( reader.seekToNode( 2, "content" ) )
114 _keywords.insert( reader.nodeText().asString() );
115 reader.seekToEndNode( 2, "content" );
117 _keywords.insert( "" ); // valid content in _keywords even if empty
119 else if ( PathInfo( (master=metadatapath/"/content") ).isFile() )
121 //MIL << "GO content.." << endl;
122 iostr::forEachLine( InputStream( master ),
123 [this]( int num_r, std::string line_r )->bool
125 if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
127 std::vector<std::string> words;
128 if ( str::split( line_r, std::back_inserter(words) ) > 1
129 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
131 this->_keywords.insert( ++words.begin(), words.end() );
133 return true; // mult. occurrances are ok.
135 return( ! str::startsWith( line_r, "META " ) ); // no need to parse into META section.
137 _keywords.insert( "" );
139 /////////////////////////////////////////////////////////////////
141 return( _keywords.find( keyword_r ) != _keywords.end() );
147 TriBool keeppackages;
152 std::string targetDistro;
153 Pathname metadatapath;
154 Pathname packagespath;
155 DefaultIntegral<unsigned,defaultPriority> priority;
156 mutable bool emptybaseurls;
157 repo::RepoVariablesUrlReplacer replacer;
161 mutable url_set _baseUrls;
162 mutable std::set<std::string> _keywords;
164 friend Impl * rwcowClone<Impl>( const Impl * rhs );
165 /** clone for RWCOW_pointer */
167 { return new Impl( *this ); }
169 ///////////////////////////////////////////////////////////////////
171 /** \relates RepoInfo::Impl Stream output */
172 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
174 return str << "RepoInfo::Impl";
177 ///////////////////////////////////////////////////////////////////
179 // CLASS NAME : RepoInfo
181 ///////////////////////////////////////////////////////////////////
183 const RepoInfo RepoInfo::noRepo;
185 ///////////////////////////////////////////////////////////////////
187 // METHOD NAME : RepoInfo::RepoInfo
188 // METHOD TYPE : Ctor
191 : _pimpl( new Impl() )
194 ///////////////////////////////////////////////////////////////////
196 // METHOD NAME : RepoInfo::~RepoInfo
197 // METHOD TYPE : Dtor
199 RepoInfo::~RepoInfo()
204 unsigned RepoInfo::priority() const
205 { return _pimpl->priority; }
207 unsigned RepoInfo::defaultPriority()
208 { return Impl::defaultPriority; }
210 void RepoInfo::setPriority( unsigned newval_r )
211 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
213 void RepoInfo::setGpgCheck( bool check )
214 { _pimpl->gpgcheck = check; }
216 void RepoInfo::setMirrorListUrl( const Url & url_r )
217 { _pimpl->setmirrorListUrl() = url_r; }
219 void RepoInfo::setGpgKeyUrl( const Url & url_r )
220 { _pimpl->gpgkey_url = url_r; }
222 void RepoInfo::addBaseUrl( const Url & url_r )
224 for ( const auto & url : _pimpl->baseUrls() ) // unique!
227 _pimpl->baseUrls().push_back( url_r );
230 void RepoInfo::setBaseUrl( const Url & url_r )
232 _pimpl->baseUrls().clear();
233 _pimpl->baseUrls().push_back( url_r );
236 void RepoInfo::setPath( const Pathname &path )
237 { _pimpl->path = path; }
239 void RepoInfo::setType( const repo::RepoType &t )
240 { _pimpl->type = t; }
242 void RepoInfo::setProbedType( const repo::RepoType &t ) const
243 { _pimpl->setProbedType( t ); }
246 void RepoInfo::setMetadataPath( const Pathname &path )
247 { _pimpl->metadatapath = path; }
249 void RepoInfo::setPackagesPath( const Pathname &path )
250 { _pimpl->packagespath = path; }
252 void RepoInfo::setKeepPackages( bool keep )
253 { _pimpl->keeppackages = keep; }
255 void RepoInfo::setService( const std::string& name )
256 { _pimpl->service = name; }
258 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
259 { _pimpl->targetDistro = targetDistribution; }
261 bool RepoInfo::gpgCheck() const
262 { return indeterminate(_pimpl->gpgcheck) ? true : (bool)_pimpl->gpgcheck; }
264 bool RepoInfo::keepPackages() const
265 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
267 Pathname RepoInfo::metadataPath() const
268 { return _pimpl->metadatapath; }
270 Pathname RepoInfo::packagesPath() const
271 { return _pimpl->packagespath; }
273 repo::RepoType RepoInfo::type() const
274 { return _pimpl->type; }
276 Url RepoInfo::mirrorListUrl() const
277 { return _pimpl->getmirrorListUrl(); }
279 Url RepoInfo::gpgKeyUrl() const
280 { return _pimpl->gpgkey_url; }
282 RepoInfo::url_set RepoInfo::baseUrls() const
283 { return url_set( baseUrlsBegin(), baseUrlsEnd() ); } // Variables replaced!
285 Pathname RepoInfo::path() const
286 { return _pimpl->path; }
288 std::string RepoInfo::service() const
289 { return _pimpl->service; }
291 std::string RepoInfo::targetDistribution() const
292 { return _pimpl->targetDistro; }
294 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
296 return make_transform_iterator( _pimpl->baseUrls().begin(),
300 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
302 return make_transform_iterator( _pimpl->baseUrls().end(),
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 " << this->name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
358 std::string RepoInfo::getLicense( const Locale & lang_r )
360 LocaleSet avlocales( getLicenseLocales() );
361 if ( avlocales.empty() )
362 return std::string();
364 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
365 if ( getLang == Locale::noCode
366 && avlocales.find( Locale::noCode ) == avlocales.end() )
368 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
369 // Using the fist locale instead of returning no text at all.
370 // So the user might recognize that there is a license, even if he
372 getLang = *avlocales.begin();
375 // now extract the license file.
376 static const std::string licenseFileFallback( "license.txt" );
377 std::string licenseFile( getLang == Locale::noCode
378 ? licenseFileFallback
379 : str::form( "license.%s.txt", getLang.code().c_str() ) );
381 ExternalProgram::Arguments cmd;
382 cmd.push_back( "tar" );
383 cmd.push_back( "-x" );
384 cmd.push_back( "-z" );
385 cmd.push_back( "-O" );
386 cmd.push_back( "-f" );
387 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
388 cmd.push_back( licenseFile );
391 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
392 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
400 LocaleSet RepoInfo::getLicenseLocales() const
402 Pathname licenseTgz( _pimpl->licenseTgz() );
403 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
406 ExternalProgram::Arguments cmd;
407 cmd.push_back( "tar" );
408 cmd.push_back( "-t" );
409 cmd.push_back( "-z" );
410 cmd.push_back( "-f" );
411 cmd.push_back( licenseTgz.asString() );
414 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
415 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
417 static const C_Str license( "license." );
418 static const C_Str dotTxt( ".txt\n" );
419 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
421 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
422 ret.insert( Locale() );
424 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
431 ///////////////////////////////////////////////////////////////////
433 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
435 RepoInfoBase::dumpOn(str);
436 if ( _pimpl->baseurl2dump() )
438 for ( const auto & url : _pimpl->baseUrls() )
440 str << "- url : " << url << std::endl;
444 // print if non empty value
445 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
446 if ( ! value_r.empty() )
447 str << tag_r << value_r << std::endl;
450 strif( "- mirrorlist : ", _pimpl->getmirrorListUrl().asString() );
451 strif( "- path : ", path().asString() );
452 str << "- type : " << type() << std::endl;
453 str << "- priority : " << priority() << std::endl;
454 str << "- gpgcheck : " << gpgCheck() << std::endl;
455 strif( "- gpgkey : ", gpgKeyUrl().asString() );
457 if ( ! indeterminate(_pimpl->keeppackages) )
458 str << "- keeppackages: " << keepPackages() << std::endl;
460 strif( "- service : ", service() );
461 strif( "- targetdistro: ", targetDistribution() );
462 strif( "- metadataPath: ", metadataPath().asString() );
463 strif( "- packagesPath: ", packagesPath().asString() );
468 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
470 RepoInfoBase::dumpAsIniOn(str);
472 if ( _pimpl->baseurl2dump() )
476 for ( const auto & url : _pimpl->baseUrls() )
478 str << indent << url << endl;
479 if ( indent.empty() ) indent = " "; // "baseurl="
483 if ( ! _pimpl->path.empty() )
484 str << "path="<< path() << endl;
486 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
487 str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
489 str << "type=" << type().asString() << endl;
491 if ( priority() != defaultPriority() )
492 str << "priority=" << priority() << endl;
494 if (!indeterminate(_pimpl->gpgcheck))
495 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
496 if ( ! (gpgKeyUrl().asString().empty()) )
497 str << "gpgkey=" <<gpgKeyUrl() << endl;
499 if (!indeterminate(_pimpl->keeppackages))
500 str << "keeppackages=" << keepPackages() << endl;
502 if( ! service().empty() )
503 str << "service=" << service() << endl;
508 std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
513 << " alias=\"" << escape(alias()) << "\""
514 << " name=\"" << escape(name()) << "\"";
515 if (type() != repo::RepoType::NONE)
516 str << " type=\"" << type().asString() << "\"";
518 << " priority=\"" << priority() << "\""
519 << " enabled=\"" << enabled() << "\""
520 << " autorefresh=\"" << autorefresh() << "\""
521 << " gpgcheck=\"" << gpgCheck() << "\"";
522 if (!(tmpstr = gpgKeyUrl().asString()).empty())
523 str << " gpgkey=\"" << escape(tmpstr) << "\"";
524 if (!(tmpstr = mirrorListUrl().asString()).empty())
525 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
528 if ( _pimpl->baseurl2dump() )
530 for ( const auto & url : _pimpl->baseUrls() )
531 str << "<url>" << escape(url.asString()) << "</url>" << endl;
534 str << "</repo>" << endl;
539 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
541 return obj.dumpOn(str);
545 /////////////////////////////////////////////////////////////////
547 ///////////////////////////////////////////////////////////////////