libzypp  11.13.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 
286  std::string RepoInfo::getLicense( const Locale & lang_r )
287  {
288  LocaleSet avlocales( getLicenseLocales() );
289  if ( avlocales.empty() )
290  return std::string();
291 
292  Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
293  if ( getLang == Locale::noCode
294  && avlocales.find( Locale::noCode ) == avlocales.end() )
295  {
296  WAR << "License.tar.gz contains no fallback text! " << *this << endl;
297  // Using the fist locale instead of returning no text at all.
298  // So the user might recognize that there is a license, even if he
299  // can't read it.
300  getLang = *avlocales.begin();
301  }
302 
303  // now extract the license file.
304  static const std::string licenseFileFallback( "license.txt" );
305  std::string licenseFile( getLang == Locale::noCode
306  ? licenseFileFallback
307  : str::form( "license.%s.txt", getLang.code().c_str() ) );
308 
310  cmd.push_back( "tar" );
311  cmd.push_back( "-x" );
312  cmd.push_back( "-z" );
313  cmd.push_back( "-O" );
314  cmd.push_back( "-f" );
315  cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
316  cmd.push_back( licenseFile );
317 
318  std::string ret;
320  for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
321  {
322  ret += output;
323  }
324  prog.close();
325  return ret;
326  }
327 
329  {
330  Pathname licenseTgz( _pimpl->licenseTgz() );
331  if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
332  return LocaleSet();
333 
335  cmd.push_back( "tar" );
336  cmd.push_back( "-t" );
337  cmd.push_back( "-z" );
338  cmd.push_back( "-f" );
339  cmd.push_back( licenseTgz.asString() );
340 
341  LocaleSet ret;
343  for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
344  {
345  static const C_Str license( "license." );
346  static const C_Str dotTxt( ".txt\n" );
347  if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
348  {
349  if ( output.size() <= license.size() + dotTxt.size() ) // license.txt
350  ret.insert( Locale() );
351  else
352  ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
353  }
354  else
355  {
356  WAR << " " << output;
357  }
358  }
359  prog.close();
360  return ret;
361  }
362 
364 
365  std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
366  {
368  if ( _pimpl->baseurl2dump() )
369  {
370  for ( urls_const_iterator it = baseUrlsBegin();
371  it != baseUrlsEnd();
372  ++it )
373  {
374  str << "- url : " << *it << std::endl;
375  }
376  }
377 
378  // print if non empty value
379  auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
380  if ( ! value_r.empty() )
381  str << tag_r << value_r << std::endl;
382  });
383 
384  strif( "- mirrorlist : ", _pimpl->getmirrorListUrl().asString() );
385  strif( "- path : ", path().asString() );
386  str << "- type : " << type() << std::endl;
387  str << "- priority : " << priority() << std::endl;
388  str << "- gpgcheck : " << gpgCheck() << std::endl;
389  strif( "- gpgkey : ", gpgKeyUrl().asString() );
390 
391  if ( ! indeterminate(_pimpl->keeppackages) )
392  str << "- keeppackages: " << keepPackages() << std::endl;
393 
394  strif( "- service : ", service() );
395  strif( "- targetdistro: ", targetDistribution() );
396  strif( "- metadataPath: ", metadataPath().asString() );
397  strif( "- packagesPath: ", packagesPath().asString() );
398 
399  return str;
400  }
401 
402  std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
403  {
405 
406  if ( _pimpl->baseurl2dump() )
407  {
408  str << "baseurl=";
409  for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
410  it != _pimpl->baseUrls().end();
411  ++it )
412  {
413  str << *it << endl;
414  }
415  }
416 
417  if ( ! _pimpl->path.empty() )
418  str << "path="<< path() << endl;
419 
420  if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
421  str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
422 
423  str << "type=" << type().asString() << endl;
424 
425  if ( priority() != defaultPriority() )
426  str << "priority=" << priority() << endl;
427 
428  if (!indeterminate(_pimpl->gpgcheck))
429  str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
430  if ( ! (gpgKeyUrl().asString().empty()) )
431  str << "gpgkey=" <<gpgKeyUrl() << endl;
432 
433  if (!indeterminate(_pimpl->keeppackages))
434  str << "keeppackages=" << keepPackages() << endl;
435 
436  if( ! service().empty() )
437  str << "service=" << service() << endl;
438 
439  return str;
440  }
441 
442  std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
443  { return dumpAsXMLOn(str, ""); }
444 
445  std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
446  {
447  string tmpstr;
448  str
449  << "<repo"
450  << " alias=\"" << escape(alias()) << "\""
451  << " name=\"" << escape(name()) << "\"";
452  if (type() != repo::RepoType::NONE)
453  str << " type=\"" << type().asString() << "\"";
454  str
455  << " priority=\"" << priority() << "\""
456  << " enabled=\"" << enabled() << "\""
457  << " autorefresh=\"" << autorefresh() << "\""
458  << " gpgcheck=\"" << gpgCheck() << "\"";
459  if (!(tmpstr = gpgKeyUrl().asString()).empty())
460  str << " gpgkey=\"" << escape(tmpstr) << "\"";
461  if (!(tmpstr = mirrorListUrl().asString()).empty())
462  str << " mirrorlist=\"" << escape(tmpstr) << "\"";
463  str << ">" << endl;
464 
465  if ( _pimpl->baseurl2dump() )
466  {
468  urlit != baseUrlsEnd(); ++urlit)
469  str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
470  }
471 
472  str << "</repo>" << endl;
473  return str;
474  }
475 
476 
477  std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
478  {
479  return obj.dumpOn(str);
480  }
481 
482 
484 } // namespace zypp