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 repo::RepoVariablesUrlReplacer replacer;
70 return replacer(mirrorlist_url);
73 Url &setmirrorListUrl()
75 return mirrorlist_url;
78 const std::set<Url> &baseUrls() const
80 if ( _baseUrls.empty() && ! (getmirrorListUrl().asString().empty()) )
83 repo::RepoMirrorList rmirrorlist (getmirrorListUrl());
84 std::vector<Url> rmurls = rmirrorlist.getUrls();
85 _baseUrls.insert(rmurls.begin(), rmurls.end());
90 std::set<Url> &baseUrls()
95 bool baseurl2dump() const
97 return !emptybaseurls && !_baseUrls.empty();
103 TriBool keeppackages;
108 std::string targetDistro;
109 Pathname metadatapath;
110 Pathname packagespath;
111 DefaultIntegral<unsigned,defaultPriority> priority;
112 mutable bool emptybaseurls;
116 mutable std::set<Url> _baseUrls;
118 friend Impl * rwcowClone<Impl>( const Impl * rhs );
119 /** clone for RWCOW_pointer */
121 { return new Impl( *this ); }
123 ///////////////////////////////////////////////////////////////////
125 /** \relates RepoInfo::Impl Stream output */
126 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
128 return str << "RepoInfo::Impl";
131 ///////////////////////////////////////////////////////////////////
133 // CLASS NAME : RepoInfo
135 ///////////////////////////////////////////////////////////////////
137 const RepoInfo RepoInfo::noRepo;
139 ///////////////////////////////////////////////////////////////////
141 // METHOD NAME : RepoInfo::RepoInfo
142 // METHOD TYPE : Ctor
145 : _pimpl( new Impl() )
148 ///////////////////////////////////////////////////////////////////
150 // METHOD NAME : RepoInfo::~RepoInfo
151 // METHOD TYPE : Dtor
153 RepoInfo::~RepoInfo()
158 unsigned RepoInfo::priority() const
159 { return _pimpl->priority; }
160 unsigned RepoInfo::defaultPriority()
161 { return Impl::defaultPriority; }
162 void RepoInfo::setPriority( unsigned newval_r )
164 _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
167 void RepoInfo::setGpgCheck( bool check )
169 _pimpl->gpgcheck = check;
172 void RepoInfo::setMirrorListUrl( const Url &url )
174 _pimpl->setmirrorListUrl() = url;
177 void RepoInfo::setGpgKeyUrl( const Url &url )
179 _pimpl->gpgkey_url = url;
182 void RepoInfo::addBaseUrl( const Url &url )
184 _pimpl->baseUrls().insert(url);
187 void RepoInfo::setBaseUrl( const Url &url )
189 _pimpl->baseUrls().clear();
193 void RepoInfo::setPath( const Pathname &path )
198 void RepoInfo::setType( const repo::RepoType &t )
203 void RepoInfo::setProbedType( const repo::RepoType &t ) const
204 { _pimpl->setProbedType( t ); }
207 void RepoInfo::setMetadataPath( const Pathname &path )
209 _pimpl->metadatapath = path;
212 void RepoInfo::setPackagesPath( const Pathname &path )
214 _pimpl->packagespath = path;
217 void RepoInfo::setKeepPackages( bool keep )
219 _pimpl->keeppackages = keep;
222 void RepoInfo::setService( const std::string& name )
224 _pimpl->service = name;
227 void RepoInfo::setTargetDistribution(
228 const std::string & targetDistribution)
230 _pimpl->targetDistro = targetDistribution;
233 bool RepoInfo::gpgCheck() const
234 { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
236 Pathname RepoInfo::metadataPath() const
237 { return _pimpl->metadatapath; }
239 Pathname RepoInfo::packagesPath() const
240 { return _pimpl->packagespath; }
242 repo::RepoType RepoInfo::type() const
243 { return _pimpl->type; }
245 Url RepoInfo::mirrorListUrl() const
247 return _pimpl->getmirrorListUrl();
250 Url RepoInfo::gpgKeyUrl() const
251 { return _pimpl->gpgkey_url; }
253 std::set<Url> RepoInfo::baseUrls() const
255 RepoInfo::url_set replaced_urls;
256 repo::RepoVariablesUrlReplacer replacer;
257 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
258 it != _pimpl->baseUrls().end();
261 replaced_urls.insert(replacer(*it));
263 return replaced_urls;
266 Pathname RepoInfo::path() const
267 { return _pimpl->path; }
269 std::string RepoInfo::service() const
270 { return _pimpl->service; }
272 std::string RepoInfo::targetDistribution() const
273 { return _pimpl->targetDistro; }
275 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
277 return make_transform_iterator( _pimpl->baseUrls().begin(),
278 repo::RepoVariablesUrlReplacer() );
279 //return _pimpl->baseUrls.begin();
282 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
284 //return _pimpl->baseUrls.end();
285 return make_transform_iterator( _pimpl->baseUrls().end(),
286 repo::RepoVariablesUrlReplacer() );
289 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
290 { return _pimpl->baseUrls().size(); }
292 bool RepoInfo::baseUrlsEmpty() const
293 { return _pimpl->baseUrls().empty(); }
295 // false by default (if not set by setKeepPackages)
296 bool RepoInfo::keepPackages() const
298 if (indeterminate(_pimpl->keeppackages))
300 if (_pimpl->baseUrls().empty())
302 else if ( baseUrlsBegin()->schemeIsDownloading() )
308 return (bool) _pimpl->keeppackages;
311 ///////////////////////////////////////////////////////////////////
313 bool RepoInfo::hasLicense() const
315 Pathname licenseTgz( _pimpl->licenseTgz() );
316 SEC << licenseTgz << endl;
317 SEC << PathInfo(licenseTgz) << endl;
319 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
322 std::string RepoInfo::getLicense( const Locale & lang_r )
324 LocaleSet avlocales( getLicenseLocales() );
325 if ( avlocales.empty() )
326 return std::string();
328 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
329 if ( getLang == Locale::noCode
330 && avlocales.find( Locale::noCode ) == avlocales.end() )
332 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
333 // Using the fist locale instead of returning no text at all.
334 // So the user might recognize that there is a license, even if he
336 getLang = *avlocales.begin();
339 // now extract the license file.
340 static const std::string licenseFileFallback( "license.txt" );
341 std::string licenseFile( getLang == Locale::noCode
342 ? licenseFileFallback
343 : str::form( "license.%s.txt", getLang.code().c_str() ) );
345 ExternalProgram::Arguments cmd;
346 cmd.push_back( "tar" );
347 cmd.push_back( "-x" );
348 cmd.push_back( "-z" );
349 cmd.push_back( "-O" );
350 cmd.push_back( "-f" );
351 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
352 cmd.push_back( licenseFile );
355 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
356 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
364 LocaleSet RepoInfo::getLicenseLocales() const
366 Pathname licenseTgz( _pimpl->licenseTgz() );
367 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
370 ExternalProgram::Arguments cmd;
371 cmd.push_back( "tar" );
372 cmd.push_back( "-t" );
373 cmd.push_back( "-z" );
374 cmd.push_back( "-f" );
375 cmd.push_back( licenseTgz.asString() );
378 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
379 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
381 static const C_Str license( "license." );
382 static const C_Str dotTxt( ".txt\n" );
383 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
385 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
386 ret.insert( Locale() );
388 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
392 WAR << " " << output;
399 ///////////////////////////////////////////////////////////////////
401 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
403 RepoInfoBase::dumpOn(str);
404 if ( _pimpl->baseurl2dump() )
406 for ( urls_const_iterator it = baseUrlsBegin();
410 str << "- url : " << *it << std::endl;
413 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
415 str << "- mirrorlist : " << _pimpl->getmirrorListUrl() << std::endl;
417 str << "- path : " << path() << std::endl;
418 str << "- type : " << type() << std::endl;
419 str << "- priority : " << priority() << std::endl;
421 str << "- gpgcheck : " << gpgCheck() << std::endl;
422 str << "- gpgkey : " << gpgKeyUrl() << std::endl;
423 str << "- keeppackages: " << keepPackages() << std::endl;
424 str << "- service : " << service() << std::endl;
426 if (!targetDistribution().empty())
427 str << "- targetdistro: " << targetDistribution() << std::endl;
429 if (!metadataPath().empty())
430 str << "- metadataPath: " << metadataPath() << std::endl;
432 if (!packagesPath().empty())
433 str << "- packagesPath: " << packagesPath() << std::endl;
438 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
440 RepoInfoBase::dumpAsIniOn(str);
442 if ( _pimpl->baseurl2dump() )
445 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
446 it != _pimpl->baseUrls().end();
453 if ( ! _pimpl->path.empty() )
454 str << "path="<< path() << endl;
456 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
457 str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
459 str << "type=" << type().asString() << endl;
461 if ( priority() != defaultPriority() )
462 str << "priority=" << priority() << endl;
464 if (!indeterminate(_pimpl->gpgcheck))
465 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
466 if ( ! (gpgKeyUrl().asString().empty()) )
467 str << "gpgkey=" <<gpgKeyUrl() << endl;
469 if (!indeterminate(_pimpl->keeppackages))
470 str << "keeppackages=" << keepPackages() << endl;
472 if( ! service().empty() )
473 str << "service=" << service() << endl;
478 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
479 { return dumpAsXMLOn(str, ""); }
481 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
486 << " alias=\"" << escape(alias()) << "\""
487 << " name=\"" << escape(name()) << "\"";
488 if (type() != repo::RepoType::NONE)
489 str << " type=\"" << type().asString() << "\"";
491 << " enabled=\"" << enabled() << "\""
492 << " autorefresh=\"" << autorefresh() << "\""
493 << " gpgcheck=\"" << gpgCheck() << "\"";
494 if (!(tmpstr = gpgKeyUrl().asString()).empty())
495 str << " gpgkey=\"" << escape(tmpstr) << "\"";
496 if (!(tmpstr = mirrorListUrl().asString()).empty())
497 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
500 if ( _pimpl->baseurl2dump() )
502 for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
503 urlit != baseUrlsEnd(); ++urlit)
504 str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
507 str << "</repo>" << endl;
512 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
514 return obj.dumpOn(str);
518 /////////////////////////////////////////////////////////////////
520 ///////////////////////////////////////////////////////////////////