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 if( metadatapath.empty() )
85 rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl() );
87 rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl(), metadatapath );
89 std::vector<Url> rmurls = rmirrorlist->getUrls();
92 _baseUrls.insert(rmurls.begin(), rmurls.end());
97 std::set<Url> &baseUrls()
102 bool baseurl2dump() const
104 return !emptybaseurls && !_baseUrls.empty();
110 TriBool keeppackages;
115 std::string targetDistro;
116 Pathname metadatapath;
117 Pathname packagespath;
118 DefaultIntegral<unsigned,defaultPriority> priority;
119 mutable bool emptybaseurls;
120 repo::RepoVariablesUrlReplacer replacer;
124 mutable std::set<Url> _baseUrls;
126 friend Impl * rwcowClone<Impl>( const Impl * rhs );
127 /** clone for RWCOW_pointer */
129 { return new Impl( *this ); }
131 ///////////////////////////////////////////////////////////////////
133 /** \relates RepoInfo::Impl Stream output */
134 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
136 return str << "RepoInfo::Impl";
139 ///////////////////////////////////////////////////////////////////
141 // CLASS NAME : RepoInfo
143 ///////////////////////////////////////////////////////////////////
145 const RepoInfo RepoInfo::noRepo;
147 ///////////////////////////////////////////////////////////////////
149 // METHOD NAME : RepoInfo::RepoInfo
150 // METHOD TYPE : Ctor
153 : _pimpl( new Impl() )
156 ///////////////////////////////////////////////////////////////////
158 // METHOD NAME : RepoInfo::~RepoInfo
159 // METHOD TYPE : Dtor
161 RepoInfo::~RepoInfo()
166 unsigned RepoInfo::priority() const
167 { return _pimpl->priority; }
168 unsigned RepoInfo::defaultPriority()
169 { return Impl::defaultPriority; }
170 void RepoInfo::setPriority( unsigned newval_r )
172 _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
175 void RepoInfo::setGpgCheck( bool check )
177 _pimpl->gpgcheck = check;
180 void RepoInfo::setMirrorListUrl( const Url &url )
182 _pimpl->setmirrorListUrl() = url;
185 void RepoInfo::setGpgKeyUrl( const Url &url )
187 _pimpl->gpgkey_url = url;
190 void RepoInfo::addBaseUrl( const Url &url )
192 _pimpl->baseUrls().insert(url);
195 void RepoInfo::setBaseUrl( const Url &url )
197 _pimpl->baseUrls().clear();
201 void RepoInfo::setPath( const Pathname &path )
206 void RepoInfo::setType( const repo::RepoType &t )
211 void RepoInfo::setProbedType( const repo::RepoType &t ) const
212 { _pimpl->setProbedType( t ); }
215 void RepoInfo::setMetadataPath( const Pathname &path )
217 _pimpl->metadatapath = path;
220 void RepoInfo::setPackagesPath( const Pathname &path )
222 _pimpl->packagespath = path;
225 void RepoInfo::setKeepPackages( bool keep )
227 _pimpl->keeppackages = keep;
230 void RepoInfo::setService( const std::string& name )
232 _pimpl->service = name;
235 void RepoInfo::setTargetDistribution(
236 const std::string & targetDistribution)
238 _pimpl->targetDistro = targetDistribution;
241 bool RepoInfo::gpgCheck() const
242 { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
244 Pathname RepoInfo::metadataPath() const
245 { return _pimpl->metadatapath; }
247 Pathname RepoInfo::packagesPath() const
248 { return _pimpl->packagespath; }
250 repo::RepoType RepoInfo::type() const
251 { return _pimpl->type; }
253 Url RepoInfo::mirrorListUrl() const
255 return _pimpl->getmirrorListUrl();
258 Url RepoInfo::gpgKeyUrl() const
259 { return _pimpl->gpgkey_url; }
261 std::set<Url> RepoInfo::baseUrls() const
263 RepoInfo::url_set replaced_urls;
264 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
265 it != _pimpl->baseUrls().end();
268 replaced_urls.insert(_pimpl->replacer(*it));
270 return replaced_urls;
273 Pathname RepoInfo::path() const
274 { return _pimpl->path; }
276 std::string RepoInfo::service() const
277 { return _pimpl->service; }
279 std::string RepoInfo::targetDistribution() const
280 { return _pimpl->targetDistro; }
282 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
284 return make_transform_iterator( _pimpl->baseUrls().begin(),
286 //return _pimpl->baseUrls.begin();
289 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
291 //return _pimpl->baseUrls.end();
292 return make_transform_iterator( _pimpl->baseUrls().end(),
296 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
297 { return _pimpl->baseUrls().size(); }
299 bool RepoInfo::baseUrlsEmpty() const
300 { return _pimpl->baseUrls().empty(); }
302 // false by default (if not set by setKeepPackages)
303 bool RepoInfo::keepPackages() const
305 if (indeterminate(_pimpl->keeppackages))
307 if (_pimpl->baseUrls().empty())
309 else if ( baseUrlsBegin()->schemeIsDownloading() )
315 return (bool) _pimpl->keeppackages;
318 ///////////////////////////////////////////////////////////////////
320 bool RepoInfo::hasLicense() const
322 Pathname licenseTgz( _pimpl->licenseTgz() );
323 SEC << licenseTgz << endl;
324 SEC << PathInfo(licenseTgz) << endl;
326 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
329 std::string RepoInfo::getLicense( const Locale & lang_r )
331 LocaleSet avlocales( getLicenseLocales() );
332 if ( avlocales.empty() )
333 return std::string();
335 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
336 if ( getLang == Locale::noCode
337 && avlocales.find( Locale::noCode ) == avlocales.end() )
339 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
340 // Using the fist locale instead of returning no text at all.
341 // So the user might recognize that there is a license, even if he
343 getLang = *avlocales.begin();
346 // now extract the license file.
347 static const std::string licenseFileFallback( "license.txt" );
348 std::string licenseFile( getLang == Locale::noCode
349 ? licenseFileFallback
350 : str::form( "license.%s.txt", getLang.code().c_str() ) );
352 ExternalProgram::Arguments cmd;
353 cmd.push_back( "tar" );
354 cmd.push_back( "-x" );
355 cmd.push_back( "-z" );
356 cmd.push_back( "-O" );
357 cmd.push_back( "-f" );
358 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
359 cmd.push_back( licenseFile );
362 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
363 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
371 LocaleSet RepoInfo::getLicenseLocales() const
373 Pathname licenseTgz( _pimpl->licenseTgz() );
374 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
377 ExternalProgram::Arguments cmd;
378 cmd.push_back( "tar" );
379 cmd.push_back( "-t" );
380 cmd.push_back( "-z" );
381 cmd.push_back( "-f" );
382 cmd.push_back( licenseTgz.asString() );
385 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
386 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
388 static const C_Str license( "license." );
389 static const C_Str dotTxt( ".txt\n" );
390 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
392 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
393 ret.insert( Locale() );
395 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
399 WAR << " " << output;
406 ///////////////////////////////////////////////////////////////////
408 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
410 RepoInfoBase::dumpOn(str);
411 if ( _pimpl->baseurl2dump() )
413 for ( urls_const_iterator it = baseUrlsBegin();
417 str << "- url : " << *it << std::endl;
420 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
422 str << "- mirrorlist : " << _pimpl->getmirrorListUrl() << std::endl;
424 str << "- path : " << path() << std::endl;
425 str << "- type : " << type() << std::endl;
426 str << "- priority : " << priority() << std::endl;
428 str << "- gpgcheck : " << gpgCheck() << std::endl;
429 str << "- gpgkey : " << gpgKeyUrl() << std::endl;
430 str << "- keeppackages: " << keepPackages() << std::endl;
431 str << "- service : " << service() << std::endl;
433 if (!targetDistribution().empty())
434 str << "- targetdistro: " << targetDistribution() << std::endl;
436 if (!metadataPath().empty())
437 str << "- metadataPath: " << metadataPath() << std::endl;
439 if (!packagesPath().empty())
440 str << "- packagesPath: " << packagesPath() << std::endl;
445 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
447 RepoInfoBase::dumpAsIniOn(str);
449 if ( _pimpl->baseurl2dump() )
452 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
453 it != _pimpl->baseUrls().end();
460 if ( ! _pimpl->path.empty() )
461 str << "path="<< path() << endl;
463 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
464 str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
466 str << "type=" << type().asString() << endl;
468 if ( priority() != defaultPriority() )
469 str << "priority=" << priority() << endl;
471 if (!indeterminate(_pimpl->gpgcheck))
472 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
473 if ( ! (gpgKeyUrl().asString().empty()) )
474 str << "gpgkey=" <<gpgKeyUrl() << endl;
476 if (!indeterminate(_pimpl->keeppackages))
477 str << "keeppackages=" << keepPackages() << endl;
479 if( ! service().empty() )
480 str << "service=" << service() << endl;
485 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
486 { return dumpAsXMLOn(str, ""); }
488 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
493 << " alias=\"" << escape(alias()) << "\""
494 << " name=\"" << escape(name()) << "\"";
495 if (type() != repo::RepoType::NONE)
496 str << " type=\"" << type().asString() << "\"";
498 << " enabled=\"" << enabled() << "\""
499 << " autorefresh=\"" << autorefresh() << "\""
500 << " gpgcheck=\"" << gpgCheck() << "\"";
501 if (!(tmpstr = gpgKeyUrl().asString()).empty())
502 str << " gpgkey=\"" << escape(tmpstr) << "\"";
503 if (!(tmpstr = mirrorListUrl().asString()).empty())
504 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
507 if ( _pimpl->baseurl2dump() )
509 for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
510 urlit != baseUrlsEnd(); ++urlit)
511 str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
514 str << "</repo>" << endl;
519 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
521 return obj.dumpOn(str);
525 /////////////////////////////////////////////////////////////////
527 ///////////////////////////////////////////////////////////////////