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
68 { return replacer(mirrorlist_url); }
70 Url &setmirrorListUrl()
71 { return mirrorlist_url; }
73 const std::set<Url> &baseUrls() const
75 if ( _baseUrls.empty() && ! (getmirrorListUrl().asString().empty()) )
78 repo::RepoMirrorList *rmirrorlist = NULL;
80 DBG << "MetadataPath: " << metadatapath << endl;
81 if( metadatapath.empty() )
82 rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl() );
84 rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl(), metadatapath );
86 std::vector<Url> rmurls = rmirrorlist->getUrls();
89 _baseUrls.insert(rmurls.begin(), rmurls.end());
94 std::set<Url> &baseUrls()
97 bool baseurl2dump() const
98 { return !emptybaseurls && !_baseUrls.empty(); }
102 TriBool keeppackages;
107 std::string targetDistro;
108 Pathname metadatapath;
109 Pathname packagespath;
110 DefaultIntegral<unsigned,defaultPriority> priority;
111 mutable bool emptybaseurls;
112 repo::RepoVariablesUrlReplacer replacer;
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; }
161 unsigned RepoInfo::defaultPriority()
162 { return Impl::defaultPriority; }
164 void RepoInfo::setPriority( unsigned newval_r )
165 { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
167 void RepoInfo::setGpgCheck( bool check )
168 { _pimpl->gpgcheck = check; }
170 void RepoInfo::setMirrorListUrl( const Url &url )
171 { _pimpl->setmirrorListUrl() = url; }
173 void RepoInfo::setGpgKeyUrl( const Url &url )
174 { _pimpl->gpgkey_url = url; }
176 void RepoInfo::addBaseUrl( const Url &url )
177 { _pimpl->baseUrls().insert(url); }
179 void RepoInfo::setBaseUrl( const Url &url )
181 _pimpl->baseUrls().clear();
185 void RepoInfo::setPath( const Pathname &path )
186 { _pimpl->path = path; }
188 void RepoInfo::setType( const repo::RepoType &t )
189 { _pimpl->type = t; }
191 void RepoInfo::setProbedType( const repo::RepoType &t ) const
192 { _pimpl->setProbedType( t ); }
195 void RepoInfo::setMetadataPath( const Pathname &path )
196 { _pimpl->metadatapath = path; }
198 void RepoInfo::setPackagesPath( const Pathname &path )
199 { _pimpl->packagespath = path; }
201 void RepoInfo::setKeepPackages( bool keep )
202 { _pimpl->keeppackages = keep; }
204 void RepoInfo::setService( const std::string& name )
205 { _pimpl->service = name; }
207 void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
208 { _pimpl->targetDistro = targetDistribution; }
210 bool RepoInfo::gpgCheck() const
211 { return indeterminate(_pimpl->gpgcheck) ? true : (bool)_pimpl->gpgcheck; }
213 bool RepoInfo::keepPackages() const
214 { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
216 Pathname RepoInfo::metadataPath() const
217 { return _pimpl->metadatapath; }
219 Pathname RepoInfo::packagesPath() const
220 { return _pimpl->packagespath; }
222 repo::RepoType RepoInfo::type() const
223 { return _pimpl->type; }
225 Url RepoInfo::mirrorListUrl() const
226 { return _pimpl->getmirrorListUrl(); }
228 Url RepoInfo::gpgKeyUrl() const
229 { return _pimpl->gpgkey_url; }
231 std::set<Url> RepoInfo::baseUrls() const
233 RepoInfo::url_set replaced_urls;
234 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
235 it != _pimpl->baseUrls().end();
238 replaced_urls.insert(_pimpl->replacer(*it));
240 return replaced_urls;
243 Pathname RepoInfo::path() const
244 { return _pimpl->path; }
246 std::string RepoInfo::service() const
247 { return _pimpl->service; }
249 std::string RepoInfo::targetDistribution() const
250 { return _pimpl->targetDistro; }
252 RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
254 return make_transform_iterator( _pimpl->baseUrls().begin(),
256 //return _pimpl->baseUrls.begin();
259 RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
261 //return _pimpl->baseUrls.end();
262 return make_transform_iterator( _pimpl->baseUrls().end(),
266 RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
267 { return _pimpl->baseUrls().size(); }
269 bool RepoInfo::baseUrlsEmpty() const
270 { return _pimpl->baseUrls().empty(); }
272 bool RepoInfo::baseUrlSet() const
273 { return _pimpl->baseurl2dump(); }
275 ///////////////////////////////////////////////////////////////////
277 bool RepoInfo::hasLicense() const
279 Pathname licenseTgz( _pimpl->licenseTgz() );
280 SEC << licenseTgz << endl;
281 SEC << PathInfo(licenseTgz) << endl;
283 return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
286 bool RepoInfo::needToAcceptLicense() const
288 static const std::string noAcceptanceFile = "no-acceptance-needed\n";
291 Pathname licenseTgz( _pimpl->licenseTgz() );
292 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
293 return false; // no licenses at all
295 ExternalProgram::Arguments cmd;
296 cmd.push_back( "tar" );
297 cmd.push_back( "-t" );
298 cmd.push_back( "-z" );
299 cmd.push_back( "-f" );
300 cmd.push_back( licenseTgz.asString() );
302 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
303 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
305 if ( output == noAcceptanceFile )
310 MIL << "License for " << this->name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
314 std::string RepoInfo::getLicense( const Locale & lang_r )
316 LocaleSet avlocales( getLicenseLocales() );
317 if ( avlocales.empty() )
318 return std::string();
320 Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
321 if ( getLang == Locale::noCode
322 && avlocales.find( Locale::noCode ) == avlocales.end() )
324 WAR << "License.tar.gz contains no fallback text! " << *this << endl;
325 // Using the fist locale instead of returning no text at all.
326 // So the user might recognize that there is a license, even if he
328 getLang = *avlocales.begin();
331 // now extract the license file.
332 static const std::string licenseFileFallback( "license.txt" );
333 std::string licenseFile( getLang == Locale::noCode
334 ? licenseFileFallback
335 : str::form( "license.%s.txt", getLang.code().c_str() ) );
337 ExternalProgram::Arguments cmd;
338 cmd.push_back( "tar" );
339 cmd.push_back( "-x" );
340 cmd.push_back( "-z" );
341 cmd.push_back( "-O" );
342 cmd.push_back( "-f" );
343 cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
344 cmd.push_back( licenseFile );
347 ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
348 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
356 LocaleSet RepoInfo::getLicenseLocales() const
358 Pathname licenseTgz( _pimpl->licenseTgz() );
359 if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
362 ExternalProgram::Arguments cmd;
363 cmd.push_back( "tar" );
364 cmd.push_back( "-t" );
365 cmd.push_back( "-z" );
366 cmd.push_back( "-f" );
367 cmd.push_back( licenseTgz.asString() );
370 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
371 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
373 static const C_Str license( "license." );
374 static const C_Str dotTxt( ".txt\n" );
375 if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
377 if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
378 ret.insert( Locale() );
380 ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
387 ///////////////////////////////////////////////////////////////////
389 std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
391 RepoInfoBase::dumpOn(str);
392 if ( _pimpl->baseurl2dump() )
394 for ( urls_const_iterator it = baseUrlsBegin();
398 str << "- url : " << *it << std::endl;
402 // print if non empty value
403 auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
404 if ( ! value_r.empty() )
405 str << tag_r << value_r << std::endl;
408 strif( "- mirrorlist : ", _pimpl->getmirrorListUrl().asString() );
409 strif( "- path : ", path().asString() );
410 str << "- type : " << type() << std::endl;
411 str << "- priority : " << priority() << std::endl;
412 str << "- gpgcheck : " << gpgCheck() << std::endl;
413 strif( "- gpgkey : ", gpgKeyUrl().asString() );
415 if ( ! indeterminate(_pimpl->keeppackages) )
416 str << "- keeppackages: " << keepPackages() << std::endl;
418 strif( "- service : ", service() );
419 strif( "- targetdistro: ", targetDistribution() );
420 strif( "- metadataPath: ", metadataPath().asString() );
421 strif( "- packagesPath: ", packagesPath().asString() );
426 std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
428 RepoInfoBase::dumpAsIniOn(str);
430 if ( _pimpl->baseurl2dump() )
433 for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
434 it != _pimpl->baseUrls().end();
441 if ( ! _pimpl->path.empty() )
442 str << "path="<< path() << endl;
444 if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
445 str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
447 str << "type=" << type().asString() << endl;
449 if ( priority() != defaultPriority() )
450 str << "priority=" << priority() << endl;
452 if (!indeterminate(_pimpl->gpgcheck))
453 str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
454 if ( ! (gpgKeyUrl().asString().empty()) )
455 str << "gpgkey=" <<gpgKeyUrl() << endl;
457 if (!indeterminate(_pimpl->keeppackages))
458 str << "keeppackages=" << keepPackages() << endl;
460 if( ! service().empty() )
461 str << "service=" << service() << endl;
466 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
467 { return dumpAsXMLOn(str, ""); }
469 std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
474 << " alias=\"" << escape(alias()) << "\""
475 << " name=\"" << escape(name()) << "\"";
476 if (type() != repo::RepoType::NONE)
477 str << " type=\"" << type().asString() << "\"";
479 << " enabled=\"" << enabled() << "\""
480 << " autorefresh=\"" << autorefresh() << "\""
481 << " gpgcheck=\"" << gpgCheck() << "\"";
482 if (!(tmpstr = gpgKeyUrl().asString()).empty())
483 str << " gpgkey=\"" << escape(tmpstr) << "\"";
484 if (!(tmpstr = mirrorListUrl().asString()).empty())
485 str << " mirrorlist=\"" << escape(tmpstr) << "\"";
488 if ( _pimpl->baseurl2dump() )
490 for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
491 urlit != baseUrlsEnd(); ++urlit)
492 str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
495 str << "</repo>" << endl;
500 std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
502 return obj.dumpOn(str);
506 /////////////////////////////////////////////////////////////////
508 ///////////////////////////////////////////////////////////////////