libzypp  12.16.5
RepoInfo.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <vector>
14 
15 #include "zypp/base/Logger.h"
18 
19 #include "zypp/RepoInfo.h"
22 #include "zypp/ExternalProgram.h"
23 #include "zypp/media/MediaAccess.h"
24 
25 using namespace std;
26 using zypp::xml::escape;
27 
29 namespace zypp
30 {
31 
33  //
34  // CLASS NAME : RepoInfo::Impl
35  //
38  {
39  Impl()
40  : repo::RepoInfoBase::Impl()
41  , gpgcheck(indeterminate)
42  , keeppackages(indeterminate)
43  , type(repo::RepoType::NONE_e)
44  , emptybaseurls(false)
45  {}
46 
47  ~Impl()
48  {}
49 
50  public:
51  static const unsigned defaultPriority = 99;
52 
53  void setProbedType( const repo::RepoType & t ) const
54  {
55  if ( type == repo::RepoType::NONE
56  && t != repo::RepoType::NONE )
57  {
58  // lazy init!
59  const_cast<Impl*>(this)->type = t;
60  }
61  }
62 
63  public:
64  Pathname licenseTgz() const
65  { return metadatapath.empty() ? Pathname() : metadatapath / path / "license.tar.gz"; }
66 
67  Url getmirrorListUrl() const
68  { return replacer(mirrorlist_url); }
69 
70  Url &setmirrorListUrl()
71  { return mirrorlist_url; }
72 
73  const std::set<Url> &baseUrls() const
74  {
75  if ( _baseUrls.empty() && ! (getmirrorListUrl().asString().empty()) )
76  {
77  emptybaseurls = true;
78  repo::RepoMirrorList *rmirrorlist = NULL;
79 
80  DBG << "MetadataPath: " << metadatapath << endl;
81  if( metadatapath.empty() )
82  rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl() );
83  else
84  rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl(), metadatapath );
85 
86  std::vector<Url> rmurls = rmirrorlist->getUrls();
87  delete rmirrorlist;
88  rmirrorlist = NULL;
89  _baseUrls.insert(rmurls.begin(), rmurls.end());
90  }
91  return _baseUrls;
92  }
93 
94  std::set<Url> &baseUrls()
95  { return _baseUrls; }
96 
97  bool baseurl2dump() const
98  { return !emptybaseurls && !_baseUrls.empty(); }
99 
100  public:
105  Pathname path;
106  std::string service;
107  std::string targetDistro;
108  Pathname metadatapath;
109  Pathname packagespath;
111  mutable bool emptybaseurls;
113 
114  private:
116  mutable std::set<Url> _baseUrls;
117 
118  friend Impl * rwcowClone<Impl>( const Impl * rhs );
120  Impl * clone() const
121  { return new Impl( *this ); }
122  };
124 
126  inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
127  {
128  return str << "RepoInfo::Impl";
129  }
130 
132  //
133  // CLASS NAME : RepoInfo
134  //
136 
137  const RepoInfo RepoInfo::noRepo;
138 
140  //
141  // METHOD NAME : RepoInfo::RepoInfo
142  // METHOD TYPE : Ctor
143  //
144  RepoInfo::RepoInfo()
145  : _pimpl( new Impl() )
146  {}
147 
149  //
150  // METHOD NAME : RepoInfo::~RepoInfo
151  // METHOD TYPE : Dtor
152  //
154  {
155  //MIL << std::endl;
156  }
157 
158  unsigned RepoInfo::priority() const
159  { return _pimpl->priority; }
160 
162  { return Impl::defaultPriority; }
163 
164  void RepoInfo::setPriority( unsigned newval_r )
165  { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
166 
168  { _pimpl->gpgcheck = check; }
169 
171  { _pimpl->setmirrorListUrl() = url; }
172 
174  { _pimpl->gpgkey_url = url; }
175 
176  void RepoInfo::addBaseUrl( const Url &url )
177  { _pimpl->baseUrls().insert(url); }
178 
179  void RepoInfo::setBaseUrl( const Url &url )
180  {
181  _pimpl->baseUrls().clear();
182  addBaseUrl(url);
183  }
184 
185  void RepoInfo::setPath( const Pathname &path )
186  { _pimpl->path = path; }
187 
189  { _pimpl->type = t; }
190 
192  { _pimpl->setProbedType( t ); }
193 
194 
195  void RepoInfo::setMetadataPath( const Pathname &path )
196  { _pimpl->metadatapath = path; }
197 
198  void RepoInfo::setPackagesPath( const Pathname &path )
199  { _pimpl->packagespath = path; }
200 
201  void RepoInfo::setKeepPackages( bool keep )
202  { _pimpl->keeppackages = keep; }
203 
204  void RepoInfo::setService( const std::string& name )
205  { _pimpl->service = name; }
206 
207  void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
209 
210  bool RepoInfo::gpgCheck() const
211  { return indeterminate(_pimpl->gpgcheck) ? true : (bool)_pimpl->gpgcheck; }
212 
214  { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
215 
216  Pathname RepoInfo::metadataPath() const
217  { return _pimpl->metadatapath; }
218 
219  Pathname RepoInfo::packagesPath() const
220  { return _pimpl->packagespath; }
221 
223  { return _pimpl->type; }
224 
226  { return _pimpl->getmirrorListUrl(); }
227 
229  { return _pimpl->gpgkey_url; }
230 
231  std::set<Url> RepoInfo::baseUrls() const
232  {
233  RepoInfo::url_set replaced_urls;
234  for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
235  it != _pimpl->baseUrls().end();
236  ++it )
237  {
238  replaced_urls.insert(_pimpl->replacer(*it));
239  }
240  return replaced_urls;
241  }
242 
243  Pathname RepoInfo::path() const
244  { return _pimpl->path; }
245 
246  std::string RepoInfo::service() const
247  { return _pimpl->service; }
248 
249  std::string RepoInfo::targetDistribution() const
250  { return _pimpl->targetDistro; }
251 
253  {
254  return make_transform_iterator( _pimpl->baseUrls().begin(),
255  _pimpl->replacer );
256  //return _pimpl->baseUrls.begin();
257  }
258 
260  {
261  //return _pimpl->baseUrls.end();
262  return make_transform_iterator( _pimpl->baseUrls().end(),
263  _pimpl->replacer );
264  }
265 
267  { return _pimpl->baseUrls().size(); }
268 
270  { return _pimpl->baseUrls().empty(); }
271 
272  bool RepoInfo::baseUrlSet() const
273  { return _pimpl->baseurl2dump(); }
274 
276 
277  bool RepoInfo::hasLicense() const
278  {
279  Pathname licenseTgz( _pimpl->licenseTgz() );
280  SEC << licenseTgz << endl;
281  SEC << PathInfo(licenseTgz) << endl;
282 
283  return ! licenseTgz.empty() && PathInfo(licenseTgz).isFile();
284  }
285 
287  {
288  static const std::string noAcceptanceFile = "no-acceptance-needed\n";
289  bool accept = true;
290 
291  Pathname licenseTgz( _pimpl->licenseTgz() );
292  if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
293  return false; // no licenses at all
294 
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() );
301 
303  for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
304  {
305  if ( output == noAcceptanceFile )
306  {
307  accept = false;
308  }
309  }
310  MIL << "License for " << this->name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
311  return accept;
312  }
313 
314  std::string RepoInfo::getLicense( const Locale & lang_r )
315  {
316  LocaleSet avlocales( getLicenseLocales() );
317  if ( avlocales.empty() )
318  return std::string();
319 
320  Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
321  if ( getLang == Locale::noCode
322  && avlocales.find( Locale::noCode ) == avlocales.end() )
323  {
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
327  // can't read it.
328  getLang = *avlocales.begin();
329  }
330 
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() ) );
336 
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 );
345 
346  std::string ret;
348  for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
349  {
350  ret += output;
351  }
352  prog.close();
353  return ret;
354  }
355 
357  {
358  Pathname licenseTgz( _pimpl->licenseTgz() );
359  if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
360  return LocaleSet();
361 
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() );
368 
369  LocaleSet ret;
371  for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
372  {
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 ) )
376  {
377  if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
378  ret.insert( Locale() );
379  else
380  ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
381  }
382  }
383  prog.close();
384  return ret;
385  }
386 
388 
389  std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
390  {
392  if ( _pimpl->baseurl2dump() )
393  {
394  for ( urls_const_iterator it = baseUrlsBegin();
395  it != baseUrlsEnd();
396  ++it )
397  {
398  str << "- url : " << *it << std::endl;
399  }
400  }
401 
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;
406  });
407 
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() );
414 
415  if ( ! indeterminate(_pimpl->keeppackages) )
416  str << "- keeppackages: " << keepPackages() << std::endl;
417 
418  strif( "- service : ", service() );
419  strif( "- targetdistro: ", targetDistribution() );
420  strif( "- metadataPath: ", metadataPath().asString() );
421  strif( "- packagesPath: ", packagesPath().asString() );
422 
423  return str;
424  }
425 
426  std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
427  {
428  RepoInfoBase::dumpAsIniOn(str);
429 
430  if ( _pimpl->baseurl2dump() )
431  {
432  str << "baseurl=";
433  for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
434  it != _pimpl->baseUrls().end();
435  ++it )
436  {
437  str << *it << endl;
438  }
439  }
440 
441  if ( ! _pimpl->path.empty() )
442  str << "path="<< path() << endl;
443 
444  if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
445  str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
446 
447  str << "type=" << type().asString() << endl;
448 
449  if ( priority() != defaultPriority() )
450  str << "priority=" << priority() << endl;
451 
452  if (!indeterminate(_pimpl->gpgcheck))
453  str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
454  if ( ! (gpgKeyUrl().asString().empty()) )
455  str << "gpgkey=" <<gpgKeyUrl() << endl;
456 
457  if (!indeterminate(_pimpl->keeppackages))
458  str << "keeppackages=" << keepPackages() << endl;
459 
460  if( ! service().empty() )
461  str << "service=" << service() << endl;
462 
463  return str;
464  }
465 
466  std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
467  { return dumpAsXMLOn(str, ""); }
468 
469  std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
470  {
471  string tmpstr;
472  str
473  << "<repo"
474  << " alias=\"" << escape(alias()) << "\""
475  << " name=\"" << escape(name()) << "\"";
476  if (type() != repo::RepoType::NONE)
477  str << " type=\"" << type().asString() << "\"";
478  str
479  << " priority=\"" << priority() << "\""
480  << " enabled=\"" << enabled() << "\""
481  << " autorefresh=\"" << autorefresh() << "\""
482  << " gpgcheck=\"" << gpgCheck() << "\"";
483  if (!(tmpstr = gpgKeyUrl().asString()).empty())
484  str << " gpgkey=\"" << escape(tmpstr) << "\"";
485  if (!(tmpstr = mirrorListUrl().asString()).empty())
486  str << " mirrorlist=\"" << escape(tmpstr) << "\"";
487  str << ">" << endl;
488 
489  if ( _pimpl->baseurl2dump() )
490  {
492  urlit != baseUrlsEnd(); ++urlit)
493  str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
494  }
495 
496  str << "</repo>" << endl;
497  return str;
498  }
499 
500 
501  std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
502  {
503  return obj.dumpOn(str);
504  }
505 
506 
508 } // namespace zypp