libzypp  13.10.6
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 
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 
68  { return replacer(mirrorlist_url); }
69 
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 )
208  { _pimpl->targetDistro = 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
Url & setmirrorListUrl()
Definition: RepoInfo.cc:70
static const Locale noCode
No or empty code.
Definition: Locale.h:71
LocaleSet getLicenseLocales() const
Return the locales the license is available for.
Definition: RepoInfo.cc:356
std::string name() const
Repository short label.
std::string targetDistribution() const
Distribution for which is this repository meant.
Definition: RepoInfo.cc:249
#define MIL
Definition: Logger.h:47
void setGpgKeyUrl(const Url &gpgkey)
Key to use for gpg checking of this repository.
Definition: RepoInfo.cc:173
static unsigned defaultPriority()
The default priority (99).
Definition: RepoInfo.cc:161
std::string alias() const
unique identifier for this source.
virtual std::ostream & dumpAsIniOn(std::ostream &str) const
Write this RepoInfo object into str in a .repo file format.
Definition: RepoInfo.cc:426
void setPriority(unsigned newval_r)
Set repository priority for solver.
Definition: RepoInfo.cc:164
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: RepoInfo.h:360
void setMirrorListUrl(const Url &url)
Set mirror list url.
Definition: RepoInfo.cc:170
virtual std::ostream & dumpAsXMLOn(std::ostream &str) const
Write an XML representation of this RepoInfo object.
Definition: RepoInfo.cc:466
repo::RepoVariablesUrlReplacer replacer
Definition: RepoInfo.cc:112
urls_const_iterator baseUrlsBegin() const
iterator that points at begin of repository urls
Definition: RepoInfo.cc:252
std::string getLicense(const Locale &lang_r=Locale())
Return the best license for the current (or a specified) locale.
Definition: RepoInfo.cc:314
std::set< Url > & baseUrls()
Definition: RepoInfo.cc:94
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Definition: String.cc:339
Pathname metadataPath() const
Path where this repo metadata was read from.
Definition: RepoInfo.cc:216
std::ostream & operator<<(std::ostream &str, const RepoInfo::Impl &obj)
Definition: RepoInfo.cc:126
Pathname metadatapath
Definition: RepoInfo.cc:108
void setProbedType(const repo::RepoType &t) const
This allows to adjust the RepoType lazy, from NONE to some probed value, even for const objects...
Definition: RepoInfo.cc:191
What is known about a repository.
Definition: RepoInfo.h:66
void setBaseUrl(const Url &url)
Clears current base URL list and adds url.
Definition: RepoInfo.cc:179
bool enabled() const
If enabled is false, then this repository must be ignored as if does not exists, except when checking...
urls_const_iterator baseUrlsEnd() const
iterator that points at end of repository urls
Definition: RepoInfo.cc:259
Pathname packagesPath() const
Path where this repo packages are cached.
Definition: RepoInfo.cc:219
unsigned priority() const
Repository priority for solver.
Definition: RepoInfo.cc:158
std::vector< std::string > Arguments
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: TriBool.h:39
transform_iterator< repo::RepoVariablesUrlReplacer, url_set::const_iterator > urls_const_iterator
Definition: RepoInfo.h:96
RepoInfo implementation.
Definition: RepoInfo.cc:37
std::string escape(const std::string &in_r)
Escape xml special charaters (&amp; -&gt; &amp;; from IoBind library).
Definition: XmlEscape.cc:106
bool keepPackages() const
Whether packages downloaded from this repository will be kept in local cache.
Definition: RepoInfo.cc:213
bool needToAcceptLicense() const
Whether the repo license has to be accepted, e.g.
Definition: RepoInfo.cc:286
virtual ~RepoInfo()
Definition: RepoInfo.cc:153
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
Url mirrorListUrl() const
Url of a file which contains a list of Urls If empty, the base url will be used.
Definition: RepoInfo.cc:225
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:81
void setPath(const Pathname &path)
set the product path.
Definition: RepoInfo.cc:185
void setService(const std::string &name)
sets service which added this repository
Definition: RepoInfo.cc:204
#define WAR
Definition: Logger.h:48
void setMetadataPath(const Pathname &path)
set the path where the local metadata is stored
Definition: RepoInfo.cc:195
bool gpgCheck() const
Whether to check or not this repository with gpg.
Definition: RepoInfo.cc:210
std::set< Url > _baseUrls
Definition: RepoInfo.cc:116
#define SEC
Definition: Logger.h:50
void setType(const repo::RepoType &t)
set the repository type
Definition: RepoInfo.cc:188
bool baseUrlSet() const
whether there are manualy configured repository urls
Definition: RepoInfo.cc:272
Impl * clone() const
clone for RWCOW_pointer
Definition: RepoInfo.cc:120
void setKeepPackages(bool keep)
Set if packaqes downloaded from this repository will be kept in local cache.
Definition: RepoInfo.cc:201
std::string service() const
Gets name of the service to which this repository belongs or empty string if it has been added manual...
Definition: RepoInfo.cc:246
bool baseurl2dump() const
Definition: RepoInfo.cc:97
zypp::Url url
Definition: MediaCurl.cc:193
const std::string & asString() const
Definition: RepoType.cc:56
std::tr1::unordered_set< Locale > LocaleSet
Definition: Locale.h:28
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:435
void addBaseUrl(const Url &url)
Add a base url.
Definition: RepoInfo.cc:176
std::string receiveLine()
Read one line from the input stream.
static const RepoType NONE
Definition: RepoType.h:32
std::string asString(const Patch::SeverityFlag &obj)
Definition: Patch.cc:149
void setPackagesPath(const Pathname &path)
set the path where the local packages are stored
Definition: RepoInfo.cc:198
int close()
Wait for the progamm to complete.
bool baseUrlsEmpty() const
whether repository urls are available
Definition: RepoInfo.cc:269
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
repo::RepoType type() const
Type of repository,.
Definition: RepoInfo.cc:222
void setProbedType(const repo::RepoType &t) const
Definition: RepoInfo.cc:53
std::string code() const
Return the locale code.
Definition: Locale.cc:207
Pathname licenseTgz() const
Definition: RepoInfo.cc:64
url_set::size_type urls_size_type
Definition: RepoInfo.h:95
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:836
void setTargetDistribution(const std::string &targetDistribution)
Sets the distribution for which is this repository meant.
Definition: RepoInfo.cc:207
Url getmirrorListUrl() const
Definition: RepoInfo.cc:67
bool autorefresh() const
If true, the repostory must be refreshed before creating resolvables from it.
std::set< Url > baseUrls() const
A Url under which the metadata are located, or a set of mirrors.
Definition: RepoInfo.cc:231
Pathname packagespath
Definition: RepoInfo.cc:109
bool hasLicense() const
Whether there is a license associated with the repo.
Definition: RepoInfo.cc:277
Url gpgKeyUrl() const
Key to use for gpg checking of this repository.
Definition: RepoInfo.cc:228
DefaultIntegral< unsigned, defaultPriority > priority
Definition: RepoInfo.cc:110
Url url() const
Pars pro toto: The first repository url.
Definition: RepoInfo.h:120
void setGpgCheck(bool check)
Whether to check or not this repository with gpg.
Definition: RepoInfo.cc:167
std::string targetDistro
Definition: RepoInfo.cc:107
const std::set< Url > & baseUrls() const
Definition: RepoInfo.cc:73
bool check(const std::string &sequenceinfo_r, bool quick_r)
Check via sequence info.
size_type size() const
Definition: String.h:95
repo::RepoType type
Definition: RepoInfo.cc:104
std::vector< Url > getUrls() const
Functor replacing repository variables.
Definition: RepoVariables.h:45
urls_size_type baseUrlsSize() const
number of repository urls
Definition: RepoInfo.cc:266
static Locale bestMatch(const LocaleSet &avLocales_r, const Locale &requested_r=Locale())
Return the best match for Locale requested_r within the available avLocales_r.
Definition: Locale.cc:229
static const unsigned defaultPriority
Definition: RepoInfo.cc:51
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:828
Url manipulation class.
Definition: Url.h:87
Pathname path() const
Repository path.
Definition: RepoInfo.cc:243
virtual std::ostream & dumpOn(std::ostream &str) const
Write a human-readable representation of this RepoInfo object into the str stream.
Definition: RepoInfo.cc:389
#define DBG
Definition: Logger.h:46
std::string service
Definition: RepoInfo.cc:106
Repository type enumeration.
Definition: RepoType.h:27
std::set< Url > url_set
Definition: RepoInfo.h:94