reduce logging
[platform/upstream/libzypp.git] / zypp / RepoInfo.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoInfo.cc
10  *
11 */
12 #include <iostream>
13
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/DefaultIntegral.h"
16 #include "zypp/parser/xml/XmlEscape.h"
17
18 #include "zypp/RepoInfo.h"
19 #include "zypp/repo/RepoInfoBaseImpl.h"
20 #include "zypp/ExternalProgram.h"
21 #include "zypp/media/MediaAccess.h"
22
23 using namespace std;
24 using zypp::xml::escape;
25
26 ///////////////////////////////////////////////////////////////////
27 namespace zypp
28 { /////////////////////////////////////////////////////////////////
29
30   ///////////////////////////////////////////////////////////////////
31   //
32   //    CLASS NAME : RepoInfo::Impl
33   //
34   /** RepoInfo implementation. */
35   struct RepoInfo::Impl : public repo::RepoInfoBase::Impl
36   {
37     Impl()
38       : repo::RepoInfoBase::Impl()
39       , gpgcheck(indeterminate)
40       , keeppackages(indeterminate)
41       , type(repo::RepoType::NONE_e)
42     {}
43
44     ~Impl()
45     {}
46
47   public:
48     static const unsigned defaultPriority = 99;
49
50     void setProbedType( const repo::RepoType & t ) const
51     {
52       if ( type == repo::RepoType::NONE
53            && t != repo::RepoType::NONE )
54       {
55         // lazy init!
56         const_cast<Impl*>(this)->type = t;
57       }
58     }
59
60   public:
61     Pathname licenseTgz() const
62     { return metadatapath.empty() ? Pathname() : metadatapath / "license.tar.gz"; }
63
64
65   public:
66     TriBool gpgcheck;
67     TriBool keeppackages;
68     Url gpgkey_url;
69     repo::RepoType type;
70     Url mirrorlist_url;
71     std::set<Url> baseUrls;
72     Pathname path;
73     std::string service;
74     std::string targetDistro;
75     Pathname metadatapath;
76     Pathname packagespath;
77     DefaultIntegral<unsigned,defaultPriority> priority;
78   public:
79
80   private:
81     friend Impl * rwcowClone<Impl>( const Impl * rhs );
82     /** clone for RWCOW_pointer */
83     Impl * clone() const
84     { return new Impl( *this ); }
85   };
86   ///////////////////////////////////////////////////////////////////
87
88   /** \relates RepoInfo::Impl Stream output */
89   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
90   {
91     return str << "RepoInfo::Impl";
92   }
93
94   ///////////////////////////////////////////////////////////////////
95   //
96   //    CLASS NAME : RepoInfo
97   //
98   ///////////////////////////////////////////////////////////////////
99
100   const RepoInfo RepoInfo::noRepo;
101
102   ///////////////////////////////////////////////////////////////////
103   //
104   //    METHOD NAME : RepoInfo::RepoInfo
105   //    METHOD TYPE : Ctor
106   //
107   RepoInfo::RepoInfo()
108   : _pimpl( new Impl() )
109   {}
110
111   ///////////////////////////////////////////////////////////////////
112   //
113   //    METHOD NAME : RepoInfo::~RepoInfo
114   //    METHOD TYPE : Dtor
115   //
116   RepoInfo::~RepoInfo()
117   {
118     //MIL << std::endl;
119   }
120
121   unsigned RepoInfo::priority() const
122   { return _pimpl->priority; }
123   unsigned RepoInfo::defaultPriority()
124   { return Impl::defaultPriority; }
125   void RepoInfo::setPriority( unsigned newval_r )
126   {
127     _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
128   }
129
130   void RepoInfo::setGpgCheck( bool check )
131   {
132     _pimpl->gpgcheck = check;
133   }
134
135   void RepoInfo::setMirrorListUrl( const Url &url )
136   {
137     _pimpl->mirrorlist_url = url;
138   }
139
140   void RepoInfo::setGpgKeyUrl( const Url &url )
141   {
142     _pimpl->gpgkey_url = url;
143   }
144
145   void RepoInfo::addBaseUrl( const Url &url )
146   {
147     _pimpl->baseUrls.insert(url);
148   }
149
150   void RepoInfo::setBaseUrl( const Url &url )
151   {
152     _pimpl->baseUrls.clear();
153     addBaseUrl(url);
154   }
155
156   void RepoInfo::setPath( const Pathname &path )
157   {
158     _pimpl->path = path;
159   }
160
161   void RepoInfo::setType( const repo::RepoType &t )
162   {
163     _pimpl->type = t;
164   }
165
166   void RepoInfo::setProbedType( const repo::RepoType &t ) const
167   { _pimpl->setProbedType( t ); }
168
169
170   void RepoInfo::setMetadataPath( const Pathname &path )
171   {
172     _pimpl->metadatapath = path;
173   }
174
175   void RepoInfo::setPackagesPath( const Pathname &path )
176   {
177     _pimpl->packagespath = path;
178   }
179
180   void RepoInfo::setKeepPackages( bool keep )
181   {
182     _pimpl->keeppackages = keep;
183   }
184
185   void RepoInfo::setService( const std::string& name )
186   {
187     _pimpl->service = name;
188   }
189
190   void RepoInfo::setTargetDistribution(
191       const std::string & targetDistribution)
192   {
193     _pimpl->targetDistro = targetDistribution;
194   }
195
196   bool RepoInfo::gpgCheck() const
197   { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
198
199   Pathname RepoInfo::metadataPath() const
200   { return _pimpl->metadatapath; }
201
202   Pathname RepoInfo::packagesPath() const
203   { return _pimpl->packagespath; }
204
205   repo::RepoType RepoInfo::type() const
206   { return _pimpl->type; }
207
208   Url RepoInfo::mirrorListUrl() const
209   { return _pimpl->mirrorlist_url; }
210
211   Url RepoInfo::gpgKeyUrl() const
212   { return _pimpl->gpgkey_url; }
213
214   std::set<Url> RepoInfo::baseUrls() const
215   {
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();
220           ++it )
221     {
222       replaced_urls.insert(replacer(*it));
223     }
224     return replaced_urls;
225
226     return _pimpl->baseUrls;
227   }
228
229   Pathname RepoInfo::path() const
230   { return _pimpl->path; }
231
232   std::string RepoInfo::service() const
233   { return _pimpl->service; }
234
235   std::string RepoInfo::targetDistribution() const
236   { return _pimpl->targetDistro; }
237
238   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
239   {
240     return make_transform_iterator( _pimpl->baseUrls.begin(),
241                                     repo::RepoVariablesUrlReplacer() );
242     //return _pimpl->baseUrls.begin();
243   }
244
245   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
246   {
247     //return _pimpl->baseUrls.end();
248     return make_transform_iterator( _pimpl->baseUrls.end(),
249                                     repo::RepoVariablesUrlReplacer() );
250   }
251
252   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
253   { return _pimpl->baseUrls.size(); }
254
255   bool RepoInfo::baseUrlsEmpty() const
256   { return _pimpl->baseUrls.empty(); }
257
258   // false by default (if not set by setKeepPackages)
259   bool RepoInfo::keepPackages() const
260   {
261     if (indeterminate(_pimpl->keeppackages))
262     {
263       if (_pimpl->baseUrls.empty())
264         return false;
265       else if ( media::MediaAccess::downloads( *baseUrlsBegin() ) )
266         return true;
267       else
268         return false;
269     }
270
271     return (bool) _pimpl->keeppackages;
272   }
273
274   ///////////////////////////////////////////////////////////////////
275
276   bool RepoInfo::hasLicense() const
277   {
278     Pathname licenseTgz( _pimpl->licenseTgz() );
279     return ! licenseTgz.empty() &&  PathInfo(licenseTgz).isFile();
280   }
281
282   std::string RepoInfo::getLicense( const Locale & lang_r )
283   {
284     LocaleSet avlocales( getLicenseLocales() );
285     if ( avlocales.empty() )
286       return std::string();
287
288     Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
289     if ( getLang == Locale::noCode
290          && avlocales.find( Locale::noCode ) == avlocales.end() )
291     {
292       WAR << "License.tar.gz contains no fallback text! " << *this << endl;
293       // Using the fist locale instead of returning no text at all.
294       // So the user might recognize that there is a license, even if he
295       // can't read it.
296       getLang = *avlocales.begin();
297     }
298
299     // now extract the license file.
300     static const std::string licenseFileFallback( "license.txt" );
301     std::string licenseFile( getLang == Locale::noCode
302                              ? licenseFileFallback
303                              : str::form( "license.%s.txt", getLang.code().c_str() ) );
304
305     ExternalProgram::Arguments cmd;
306     cmd.push_back( "tar" );
307     cmd.push_back( "-x" );
308     cmd.push_back( "-z" );
309     cmd.push_back( "-O" );
310     cmd.push_back( "-f" );
311     cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
312     cmd.push_back( licenseFile );
313
314     std::string ret;
315     ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
316     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
317     {
318       ret += output;
319     }
320     prog.close();
321     return ret;
322   }
323
324   LocaleSet RepoInfo::getLicenseLocales() const
325   {
326     Pathname licenseTgz( _pimpl->licenseTgz() );
327     if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
328       return LocaleSet();
329
330     ExternalProgram::Arguments cmd;
331     cmd.push_back( "tar" );
332     cmd.push_back( "-t" );
333     cmd.push_back( "-z" );
334     cmd.push_back( "-f" );
335     cmd.push_back( licenseTgz.asString() );
336
337     LocaleSet ret;
338     ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
339     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
340     {
341       static const C_Str license( "license." );
342       static const C_Str dotTxt( ".txt\n" );
343       if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
344       {
345         if ( output.size() <= license.size() +  dotTxt.size() ) // license.txt
346           ret.insert( Locale() );
347         else
348           ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
349       }
350       else
351       {
352         WAR << "  " << output;
353       }
354     }
355     prog.close();
356     return ret;
357   }
358
359   ///////////////////////////////////////////////////////////////////
360
361   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
362   {
363     RepoInfoBase::dumpOn(str);
364     for ( urls_const_iterator it = baseUrlsBegin();
365           it != baseUrlsEnd();
366           ++it )
367     {
368       str << "- url         : " << *it << std::endl;
369     }
370     str << "- path        : " << path() << std::endl;
371     str << "- type        : " << type() << std::endl;
372     str << "- priority    : " << priority() << std::endl;
373
374     str << "- gpgcheck    : " << gpgCheck() << std::endl;
375     str << "- gpgkey      : " << gpgKeyUrl() << std::endl;
376     str << "- keeppackages: " << keepPackages() << std::endl;
377     str << "- service     : " << service() << std::endl;
378
379     if (!targetDistribution().empty())
380       str << "- targetdistro: " << targetDistribution() << std::endl;
381
382     if (!metadataPath().empty())
383       str << "- metadataPath: " << metadataPath() << std::endl;
384
385     return str;
386   }
387
388   std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
389   {
390     RepoInfoBase::dumpAsIniOn(str);
391
392     if ( ! _pimpl->baseUrls.empty() )
393       str << "baseurl=";
394     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
395           it != _pimpl->baseUrls.end();
396           ++it )
397     {
398       str << *it << endl;
399     }
400
401     if ( ! _pimpl->path.empty() )
402       str << "path="<< path() << endl;
403
404     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
405       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
406
407     str << "type=" << type().asString() << endl;
408
409     if ( priority() != defaultPriority() )
410       str << "priority=" << priority() << endl;
411
412     if (!indeterminate(_pimpl->gpgcheck))
413       str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
414     if ( ! (gpgKeyUrl().asString().empty()) )
415       str << "gpgkey=" <<gpgKeyUrl() << endl;
416
417     if (!indeterminate(_pimpl->keeppackages))
418       str << "keeppackages=" << keepPackages() << endl;
419
420     if( ! service().empty() )
421       str << "service=" << service() << endl;
422
423     return str;
424   }
425
426   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
427   { return dumpAsXMLOn(str, ""); }
428
429   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
430   {
431     string tmpstr;
432     str
433       << "<repo"
434       << " alias=\"" << escape(alias()) << "\""
435       << " name=\"" << escape(name()) << "\"";
436     if (type() != repo::RepoType::NONE)
437       str << " type=\"" << type().asString() << "\"";
438     str
439       << " enabled=\"" << enabled() << "\""
440       << " autorefresh=\"" << autorefresh() << "\""
441       << " gpgcheck=\"" << gpgCheck() << "\"";
442     if (!(tmpstr = gpgKeyUrl().asString()).empty())
443       str << " gpgkey=\"" << escape(tmpstr) << "\"";
444     if (!(tmpstr = mirrorListUrl().asString()).empty())
445       str << " mirrorlist=\"" << escape(tmpstr) << "\"";
446     str << ">" << endl;
447
448     for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
449          urlit != baseUrlsEnd(); ++urlit)
450       str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
451
452     str << "</repo>" << endl;
453     return str;
454   }
455
456
457   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
458   {
459     return obj.dumpOn(str);
460   }
461
462
463   /////////////////////////////////////////////////////////////////
464 } // namespace zypp
465 ///////////////////////////////////////////////////////////////////