1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoInfo.cc
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/DefaultIntegral.h"
16 #include "zypp/parser/xml/XmlEscape.h"
18 #include "zypp/RepoInfo.h"
19 #include "zypp/repo/RepoInfoBaseImpl.h"
20 #include "zypp/ExternalProgram.h"
21 #include "zypp/media/MediaAccess.h"
24 using zypp::xml::escape;
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////
32 // CLASS NAME : RepoInfo::Impl
34 /** RepoInfo implementation. */
35 struct RepoInfo::Impl : public repo::RepoInfoBase::Impl
38 : repo::RepoInfoBase::Impl()
39 , gpgcheck(indeterminate)
40 , keeppackages(indeterminate)
41 , type(repo::RepoType::NONE_e)
48 static const unsigned defaultPriority = 99;
50 void setProbedType( const repo::RepoType & t ) const
52 if ( type == repo::RepoType::NONE
53 && t != repo::RepoType::NONE )
56 const_cast<Impl*>(this)->type = t;
61 Pathname licenseTgz() const
62 { return metadatapath.empty() ? Pathname() : metadatapath / path / "license.tar.gz"; }
71 std::set<Url> baseUrls;
74 std::string targetDistro;
75 Pathname metadatapath;
76 Pathname packagespath;
77 DefaultIntegral<unsigned,defaultPriority> priority;
81 friend Impl * rwcowClone<Impl>( const Impl * rhs );
82 /** clone for RWCOW_pointer */
84 { return new Impl( *this ); }
86 ///////////////////////////////////////////////////////////////////
88 /** \relates RepoInfo::Impl Stream output */
89 inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
91 return str << "RepoInfo::Impl";
94 ///////////////////////////////////////////////////////////////////
96 // CLASS NAME : RepoInfo
98 ///////////////////////////////////////////////////////////////////
100 const RepoInfo RepoInfo::noRepo;
102 ///////////////////////////////////////////////////////////////////
104 // METHOD NAME : RepoInfo::RepoInfo
105 // METHOD TYPE : Ctor
108 : _pimpl( new Impl() )
111 ///////////////////////////////////////////////////////////////////
113 // METHOD NAME : RepoInfo::~RepoInfo
114 // METHOD TYPE : Dtor
116 RepoInfo::~RepoInfo()
121 unsigned RepoInfo::priority() const
122 { return _pimpl->priority; }
123 unsigned RepoInfo::defaultPriority()
124 { return Impl::defaultPriority; }
125 void RepoInfo::setPriority( unsigned newval_r )
127 _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
130 void RepoInfo::setGpgCheck( bool check )
132 _pimpl->gpgcheck = check;
135 void RepoInfo::setMirrorListUrl( const Url &url )
137 _pimpl->mirrorlist_url = url;
140 void RepoInfo::setGpgKeyUrl( const Url &url )
142 _pimpl->gpgkey_url = url;
145 void RepoInfo::addBaseUrl( const Url &url )
147 _pimpl->baseUrls.insert(url);
150 void RepoInfo::setBaseUrl( const Url &url )
152 _pimpl->baseUrls.clear();
156 void RepoInfo::setPath( const Pathname &path )
161 void RepoInfo::setType( const repo::RepoType &t )
166 void RepoInfo::setProbedType( const repo::RepoType &t ) const
167 { _pimpl->setProbedType( t ); }
170 void RepoInfo::setMetadataPath( const Pathname &path )
172 _pimpl->metadatapath = path;
175 void RepoInfo::setPackagesPath( const Pathname &path )
177 _pimpl->packagespath = path;
180 void RepoInfo::setKeepPackages( bool keep )
182 _pimpl->keeppackages = keep;
185 void RepoInfo::setService( const std::string& name )
187 _pimpl->service = name;
190 void RepoInfo::setTargetDistribution(
191 const std::string & targetDistribution)
193 _pimpl->targetDistro = targetDistribution;
196 bool RepoInfo::gpgCheck() const
197 { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
199 Pathname RepoInfo::metadataPath() const
200 { return _pimpl->metadatapath; }
202 Pathname RepoInfo::packagesPath() const
203 { return _pimpl->packagespath; }
205 repo::RepoType RepoInfo::type() const
206 { return _pimpl->type; }
208 Url RepoInfo::mirrorListUrl() const
209 { return _pimpl->mirrorlist_url; }
211 Url RepoInfo::gpgKeyUrl() const
212 { return _pimpl->gpgkey_url; }
214 std::set<Url> RepoInfo::baseUrls() const
216 RepoInfo::url_set replaced_urls;
217 repo::RepoVariablesUrlReplacer replacer;
218 for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
219 it != _pimpl->baseUrls.end();
222 replaced_urls.insert(replacer(*it));
224 return replaced_urls;
226 return _pimpl->baseUrls;
229 Pathname RepoInfo::path() const
230 { return _pimpl->path; }
232 std::string RepoInfo::service() const
233 { return _pimpl->service; }
235 std::string RepoInfo::targetDistribution() const
236 { return _pimpl->targetDistro; }
238 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
240 return make_transform_iterator( _pimpl->baseUrls.begin(),
241 repo::RepoVariablesUrlReplacer() );
242 //return _pimpl->baseUrls.begin();
245 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
247 //return _pimpl->baseUrls.end();
248 return make_transform_iterator( _pimpl->baseUrls.end(),
249 repo::RepoVariablesUrlReplacer() );
252 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
253 { return _pimpl->baseUrls.size(); }
255 bool RepoInfo::baseUrlsEmpty() const
256 { return _pimpl->baseUrls.empty(); }
258 // false by default (if not set by setKeepPackages)
259 bool RepoInfo::keepPackages() const
261 if (indeterminate(_pimpl->keeppackages))
263 if (_pimpl->baseUrls.empty())
265 else if ( baseUrlsBegin()->schemeIsDownloading() )
271 return (bool) _pimpl->keeppackages;
274 ///////////////////////////////////////////////////////////////////
276 bool RepoInfo::hasLicense() const
278 Pathname licenseTgz( _pimpl->licenseTgz() );
279 SEC << licenseTgz << endl;
280 SEC << PathInfo(licenseTgz) << endl;
282 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
285 std::string RepoInfo::getLicense( const Locale & lang_r )
287 LocaleSet avlocales( getLicenseLocales() );
288 if ( avlocales.empty() )
289 return std::string();
291 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
292 if ( getLang == Locale::noCode
293 && avlocales.find( Locale::noCode ) == avlocales.end() )
295 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
296 // Using the fist locale instead of returning no text at all.
297 // So the user might recognize that there is a license, even if he
299 getLang = *avlocales.begin();
302 // now extract the license file.
303 static const std::string licenseFileFallback( "license.txt" );
304 std::string licenseFile( getLang == Locale::noCode
305 ? licenseFileFallback
306 : str::form( "license.%s.txt", getLang.code().c_str() ) );
308 ExternalProgram::Arguments cmd;
309 cmd.push_back( "tar" );
310 cmd.push_back( "-x" );
311 cmd.push_back( "-z" );
312 cmd.push_back( "-O" );
313 cmd.push_back( "-f" );
314 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
315 cmd.push_back( licenseFile );
318 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
319 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
327 LocaleSet RepoInfo::getLicenseLocales() const
329 Pathname licenseTgz( _pimpl->licenseTgz() );
330 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
333 ExternalProgram::Arguments cmd;
334 cmd.push_back( "tar" );
335 cmd.push_back( "-t" );
336 cmd.push_back( "-z" );
337 cmd.push_back( "-f" );
338 cmd.push_back( licenseTgz.asString() );
341 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
342 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
344 static const C_Str license( "license." );
345 static const C_Str dotTxt( ".txt\n" );
346 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
348 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
349 ret.insert( Locale() );
351 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
355 WAR << " " << output;
362 ///////////////////////////////////////////////////////////////////
364 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
366 RepoInfoBase::dumpOn(str);
367 for ( urls_const_iterator it = baseUrlsBegin();
371 str << "- url : " << *it << std::endl;
373 str << "- path : " << path() << std::endl;
374 str << "- type : " << type() << std::endl;
375 str << "- priority : " << priority() << std::endl;
377 str << "- gpgcheck : " << gpgCheck() << std::endl;
378 str << "- gpgkey : " << gpgKeyUrl() << std::endl;
379 str << "- keeppackages: " << keepPackages() << std::endl;
380 str << "- service : " << service() << std::endl;
382 if (!targetDistribution().empty())
383 str << "- targetdistro: " << targetDistribution() << std::endl;
385 if (!metadataPath().empty())
386 str << "- metadataPath: " << metadataPath() << std::endl;
388 if (!packagesPath().empty())
389 str << "- packagesPath: " << packagesPath() << std::endl;
394 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
396 RepoInfoBase::dumpAsIniOn(str);
398 if ( ! _pimpl->baseUrls.empty() )
400 for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
401 it != _pimpl->baseUrls.end();
407 if ( ! _pimpl->path.empty() )
408 str << "path="<< path() << endl;
410 if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
411 str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
413 str << "type=" << type().asString() << endl;
415 if ( priority() != defaultPriority() )
416 str << "priority=" << priority() << endl;
418 if (!indeterminate(_pimpl->gpgcheck))
419 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
420 if ( ! (gpgKeyUrl().asString().empty()) )
421 str << "gpgkey=" <<gpgKeyUrl() << endl;
423 if (!indeterminate(_pimpl->keeppackages))
424 str << "keeppackages=" << keepPackages() << endl;
426 if( ! service().empty() )
427 str << "service=" << service() << endl;
432 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
433 { return dumpAsXMLOn(str, ""); }
435 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
440 << " alias=\"" << escape(alias()) << "\""
441 << " name=\"" << escape(name()) << "\"";
442 if (type() != repo::RepoType::NONE)
443 str << " type=\"" << type().asString() << "\"";
445 << " enabled=\"" << enabled() << "\""
446 << " autorefresh=\"" << autorefresh() << "\""
447 << " gpgcheck=\"" << gpgCheck() << "\"";
448 if (!(tmpstr = gpgKeyUrl().asString()).empty())
449 str << " gpgkey=\"" << escape(tmpstr) << "\"";
450 if (!(tmpstr = mirrorListUrl().asString()).empty())
451 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
454 for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
455 urlit != baseUrlsEnd(); ++urlit)
456 str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
458 str << "</repo>" << endl;
463 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
465 return obj.dumpOn(str);
469 /////////////////////////////////////////////////////////////////
471 ///////////////////////////////////////////////////////////////////