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/repo/RepoInfoBaseImpl.h"
21 #include "zypp/repo/RepoMirrorList.h"
22 #include "zypp/ExternalProgram.h"
23 #include "zypp/media/MediaAccess.h"
26 using zypp::xml::escape;
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////
34 // CLASS NAME : RepoInfo::Impl
36 /** RepoInfo implementation. */
37 struct RepoInfo::Impl : public repo::RepoInfoBase::Impl
40 : repo::RepoInfoBase::Impl()
41 , gpgcheck(indeterminate)
42 , keeppackages(indeterminate)
43 , type(repo::RepoType::NONE_e)
44 , emptybaseurls(false)
51 static const unsigned defaultPriority = 99;
53 void setProbedType( const repo::RepoType & t ) const
55 if ( type == repo::RepoType::NONE
56 && t != repo::RepoType::NONE )
59 const_cast<Impl*>(this)->type = t;
64 Pathname licenseTgz() const
65 { return metadatapath.empty() ? Pathname() : metadatapath / path / "license.tar.gz"; }
67 Url getmirrorListUrl() const
69 return replacer(mirrorlist_url);
72 Url &setmirrorListUrl()
74 return mirrorlist_url;
77 const std::set<Url> &baseUrls() const
79 if ( _baseUrls.empty() && ! (getmirrorListUrl().asString().empty()) )
82 repo::RepoMirrorList *rmirrorlist = NULL;
84 DBG << "MetadataPath: " << metadatapath << endl;
85 if( metadatapath.empty() )
86 rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl() );
88 rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl(), metadatapath );
90 std::vector<Url> rmurls = rmirrorlist->getUrls();
93 _baseUrls.insert(rmurls.begin(), rmurls.end());
98 std::set<Url> &baseUrls()
103 bool baseurl2dump() const
105 return !emptybaseurls && !_baseUrls.empty();
111 TriBool keeppackages;
116 std::string targetDistro;
117 Pathname metadatapath;
118 Pathname packagespath;
119 DefaultIntegral<unsigned,defaultPriority> priority;
120 mutable bool emptybaseurls;
121 repo::RepoVariablesUrlReplacer replacer;
125 mutable std::set<Url> _baseUrls;
127 friend Impl * rwcowClone<Impl>( const Impl * rhs );
128 /** clone for RWCOW_pointer */
130 { return new Impl( *this ); }
132 ///////////////////////////////////////////////////////////////////
134 /** \relates RepoInfo::Impl Stream output */
135 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
137 return str << "RepoInfo::Impl";
140 ///////////////////////////////////////////////////////////////////
142 // CLASS NAME : RepoInfo
144 ///////////////////////////////////////////////////////////////////
146 const RepoInfo RepoInfo::noRepo;
148 ///////////////////////////////////////////////////////////////////
150 // METHOD NAME : RepoInfo::RepoInfo
151 // METHOD TYPE : Ctor
154 : _pimpl( new Impl() )
157 ///////////////////////////////////////////////////////////////////
159 // METHOD NAME : RepoInfo::~RepoInfo
160 // METHOD TYPE : Dtor
162 RepoInfo::~RepoInfo()
167 unsigned RepoInfo::priority() const
168 { return _pimpl->priority; }
169 unsigned RepoInfo::defaultPriority()
170 { return Impl::defaultPriority; }
171 void RepoInfo::setPriority( unsigned newval_r )
173 _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
176 void RepoInfo::setGpgCheck( bool check )
178 _pimpl->gpgcheck = check;
181 void RepoInfo::setMirrorListUrl( const Url &url )
183 _pimpl->setmirrorListUrl() = url;
186 void RepoInfo::setGpgKeyUrl( const Url &url )
188 _pimpl->gpgkey_url = url;
191 void RepoInfo::addBaseUrl( const Url &url )
193 _pimpl->baseUrls().insert(url);
196 void RepoInfo::setBaseUrl( const Url &url )
198 _pimpl->baseUrls().clear();
202 void RepoInfo::setPath( const Pathname &path )
207 void RepoInfo::setType( const repo::RepoType &t )
212 void RepoInfo::setProbedType( const repo::RepoType &t ) const
213 { _pimpl->setProbedType( t ); }
216 void RepoInfo::setMetadataPath( const Pathname &path )
218 _pimpl->metadatapath = path;
221 void RepoInfo::setPackagesPath( const Pathname &path )
223 _pimpl->packagespath = path;
226 void RepoInfo::setKeepPackages( bool keep )
228 _pimpl->keeppackages = keep;
231 void RepoInfo::setService( const std::string& name )
233 _pimpl->service = name;
236 void RepoInfo::setTargetDistribution(
237 const std::string & targetDistribution)
239 _pimpl->targetDistro = targetDistribution;
242 bool RepoInfo::gpgCheck() const
243 { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
245 Pathname RepoInfo::metadataPath() const
246 { return _pimpl->metadatapath; }
248 Pathname RepoInfo::packagesPath() const
249 { return _pimpl->packagespath; }
251 repo::RepoType RepoInfo::type() const
252 { return _pimpl->type; }
254 Url RepoInfo::mirrorListUrl() const
256 return _pimpl->getmirrorListUrl();
259 Url RepoInfo::gpgKeyUrl() const
260 { return _pimpl->gpgkey_url; }
262 std::set<Url> RepoInfo::baseUrls() const
264 RepoInfo::url_set replaced_urls;
265 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
266 it != _pimpl->baseUrls().end();
269 replaced_urls.insert(_pimpl->replacer(*it));
271 return replaced_urls;
274 Pathname RepoInfo::path() const
275 { return _pimpl->path; }
277 std::string RepoInfo::service() const
278 { return _pimpl->service; }
280 std::string RepoInfo::targetDistribution() const
281 { return _pimpl->targetDistro; }
283 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
285 return make_transform_iterator( _pimpl->baseUrls().begin(),
287 //return _pimpl->baseUrls.begin();
290 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
292 //return _pimpl->baseUrls.end();
293 return make_transform_iterator( _pimpl->baseUrls().end(),
297 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
298 { return _pimpl->baseUrls().size(); }
300 bool RepoInfo::baseUrlsEmpty() const
301 { return _pimpl->baseUrls().empty(); }
303 // false by default (if not set by setKeepPackages)
304 bool RepoInfo::keepPackages() const
306 if (indeterminate(_pimpl->keeppackages))
308 if (_pimpl->baseUrls().empty())
310 if ( _pimpl->getmirrorListUrl().schemeIsDownloading() )
315 else if ( baseUrlsBegin()->schemeIsDownloading() )
321 return (bool) _pimpl->keeppackages;
324 ///////////////////////////////////////////////////////////////////
326 bool RepoInfo::hasLicense() const
328 Pathname licenseTgz( _pimpl->licenseTgz() );
329 SEC << licenseTgz << endl;
330 SEC << PathInfo(licenseTgz) << endl;
332 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
335 std::string RepoInfo::getLicense( const Locale & lang_r )
337 LocaleSet avlocales( getLicenseLocales() );
338 if ( avlocales.empty() )
339 return std::string();
341 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
342 if ( getLang == Locale::noCode
343 && avlocales.find( Locale::noCode ) == avlocales.end() )
345 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
346 // Using the fist locale instead of returning no text at all.
347 // So the user might recognize that there is a license, even if he
349 getLang = *avlocales.begin();
352 // now extract the license file.
353 static const std::string licenseFileFallback( "license.txt" );
354 std::string licenseFile( getLang == Locale::noCode
355 ? licenseFileFallback
356 : str::form( "license.%s.txt", getLang.code().c_str() ) );
358 ExternalProgram::Arguments cmd;
359 cmd.push_back( "tar" );
360 cmd.push_back( "-x" );
361 cmd.push_back( "-z" );
362 cmd.push_back( "-O" );
363 cmd.push_back( "-f" );
364 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
365 cmd.push_back( licenseFile );
368 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
369 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
377 LocaleSet RepoInfo::getLicenseLocales() const
379 Pathname licenseTgz( _pimpl->licenseTgz() );
380 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
383 ExternalProgram::Arguments cmd;
384 cmd.push_back( "tar" );
385 cmd.push_back( "-t" );
386 cmd.push_back( "-z" );
387 cmd.push_back( "-f" );
388 cmd.push_back( licenseTgz.asString() );
391 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
392 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
394 static const C_Str license( "license." );
395 static const C_Str dotTxt( ".txt\n" );
396 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
398 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
399 ret.insert( Locale() );
401 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
405 WAR << " " << output;
412 ///////////////////////////////////////////////////////////////////
414 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
416 RepoInfoBase::dumpOn(str);
417 if ( _pimpl->baseurl2dump() )
419 for ( urls_const_iterator it = baseUrlsBegin();
423 str << "- url : " << *it << std::endl;
426 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
428 str << "- mirrorlist : " << _pimpl->getmirrorListUrl() << std::endl;
430 str << "- path : " << path() << std::endl;
431 str << "- type : " << type() << std::endl;
432 str << "- priority : " << priority() << std::endl;
434 str << "- gpgcheck : " << gpgCheck() << std::endl;
435 str << "- gpgkey : " << gpgKeyUrl() << std::endl;
437 if (!indeterminate(_pimpl->keeppackages))
438 str << "- keeppackages: " << keepPackages() << std::endl;
440 str << "- service : " << service() << std::endl;
442 if (!targetDistribution().empty())
443 str << "- targetdistro: " << targetDistribution() << std::endl;
445 if (!metadataPath().empty())
446 str << "- metadataPath: " << metadataPath() << std::endl;
448 if (!packagesPath().empty())
449 str << "- packagesPath: " << packagesPath() << std::endl;
454 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
456 RepoInfoBase::dumpAsIniOn(str);
458 if ( _pimpl->baseurl2dump() )
461 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
462 it != _pimpl->baseUrls().end();
469 if ( ! _pimpl->path.empty() )
470 str << "path="<< path() << endl;
472 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
473 str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
475 str << "type=" << type().asString() << endl;
477 if ( priority() != defaultPriority() )
478 str << "priority=" << priority() << endl;
480 if (!indeterminate(_pimpl->gpgcheck))
481 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
482 if ( ! (gpgKeyUrl().asString().empty()) )
483 str << "gpgkey=" <<gpgKeyUrl() << endl;
485 if (!indeterminate(_pimpl->keeppackages))
486 str << "keeppackages=" << keepPackages() << endl;
488 if( ! service().empty() )
489 str << "service=" << service() << endl;
494 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
495 { return dumpAsXMLOn(str, ""); }
497 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
502 << " alias=\"" << escape(alias()) << "\""
503 << " name=\"" << escape(name()) << "\"";
504 if (type() != repo::RepoType::NONE)
505 str << " type=\"" << type().asString() << "\"";
507 << " enabled=\"" << enabled() << "\""
508 << " autorefresh=\"" << autorefresh() << "\""
509 << " gpgcheck=\"" << gpgCheck() << "\"";
510 if (!(tmpstr = gpgKeyUrl().asString()).empty())
511 str << " gpgkey=\"" << escape(tmpstr) << "\"";
512 if (!(tmpstr = mirrorListUrl().asString()).empty())
513 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
516 if ( _pimpl->baseurl2dump() )
518 for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
519 urlit != baseUrlsEnd(); ++urlit)
520 str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
523 str << "</repo>" << endl;
528 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
530 return obj.dumpOn(str);
534 /////////////////////////////////////////////////////////////////
536 ///////////////////////////////////////////////////////////////////