remove useless double return
[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 / path / "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
227   Pathname RepoInfo::path() const
228   { return _pimpl->path; }
229
230   std::string RepoInfo::service() const
231   { return _pimpl->service; }
232
233   std::string RepoInfo::targetDistribution() const
234   { return _pimpl->targetDistro; }
235
236   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
237   {
238     return make_transform_iterator( _pimpl->baseUrls.begin(),
239                                     repo::RepoVariablesUrlReplacer() );
240     //return _pimpl->baseUrls.begin();
241   }
242
243   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
244   {
245     //return _pimpl->baseUrls.end();
246     return make_transform_iterator( _pimpl->baseUrls.end(),
247                                     repo::RepoVariablesUrlReplacer() );
248   }
249
250   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
251   { return _pimpl->baseUrls.size(); }
252
253   bool RepoInfo::baseUrlsEmpty() const
254   { return _pimpl->baseUrls.empty(); }
255
256   // false by default (if not set by setKeepPackages)
257   bool RepoInfo::keepPackages() const
258   {
259     if (indeterminate(_pimpl->keeppackages))
260     {
261       if (_pimpl->baseUrls.empty())
262         return false;
263       else if ( baseUrlsBegin()->schemeIsDownloading() )
264         return true;
265       else
266         return false;
267     }
268
269     return (bool) _pimpl->keeppackages;
270   }
271
272   ///////////////////////////////////////////////////////////////////
273
274   bool RepoInfo::hasLicense() const
275   {
276     Pathname licenseTgz( _pimpl->licenseTgz() );
277     SEC << licenseTgz << endl;
278     SEC << PathInfo(licenseTgz) << endl;
279
280     return ! licenseTgz.empty() &&  PathInfo(licenseTgz).isFile();
281   }
282
283   std::string RepoInfo::getLicense( const Locale & lang_r )
284   {
285     LocaleSet avlocales( getLicenseLocales() );
286     if ( avlocales.empty() )
287       return std::string();
288
289     Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
290     if ( getLang == Locale::noCode
291          && avlocales.find( Locale::noCode ) == avlocales.end() )
292     {
293       WAR << "License.tar.gz contains no fallback text! " << *this << endl;
294       // Using the fist locale instead of returning no text at all.
295       // So the user might recognize that there is a license, even if he
296       // can't read it.
297       getLang = *avlocales.begin();
298     }
299
300     // now extract the license file.
301     static const std::string licenseFileFallback( "license.txt" );
302     std::string licenseFile( getLang == Locale::noCode
303                              ? licenseFileFallback
304                              : str::form( "license.%s.txt", getLang.code().c_str() ) );
305
306     ExternalProgram::Arguments cmd;
307     cmd.push_back( "tar" );
308     cmd.push_back( "-x" );
309     cmd.push_back( "-z" );
310     cmd.push_back( "-O" );
311     cmd.push_back( "-f" );
312     cmd.push_back( _pimpl->licenseTgz().asString() ); // if it not exists, avlocales was empty.
313     cmd.push_back( licenseFile );
314
315     std::string ret;
316     ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
317     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
318     {
319       ret += output;
320     }
321     prog.close();
322     return ret;
323   }
324
325   LocaleSet RepoInfo::getLicenseLocales() const
326   {
327     Pathname licenseTgz( _pimpl->licenseTgz() );
328     if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
329       return LocaleSet();
330
331     ExternalProgram::Arguments cmd;
332     cmd.push_back( "tar" );
333     cmd.push_back( "-t" );
334     cmd.push_back( "-z" );
335     cmd.push_back( "-f" );
336     cmd.push_back( licenseTgz.asString() );
337
338     LocaleSet ret;
339     ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
340     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
341     {
342       static const C_Str license( "license." );
343       static const C_Str dotTxt( ".txt\n" );
344       if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
345       {
346         if ( output.size() <= license.size() +  dotTxt.size() ) // license.txt
347           ret.insert( Locale() );
348         else
349           ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
350       }
351       else
352       {
353         WAR << "  " << output;
354       }
355     }
356     prog.close();
357     return ret;
358   }
359
360   ///////////////////////////////////////////////////////////////////
361
362   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
363   {
364     RepoInfoBase::dumpOn(str);
365     for ( urls_const_iterator it = baseUrlsBegin();
366           it != baseUrlsEnd();
367           ++it )
368     {
369       str << "- url         : " << *it << std::endl;
370     }
371     str << "- path        : " << path() << std::endl;
372     str << "- type        : " << type() << std::endl;
373     str << "- priority    : " << priority() << std::endl;
374
375     str << "- gpgcheck    : " << gpgCheck() << std::endl;
376     str << "- gpgkey      : " << gpgKeyUrl() << std::endl;
377     str << "- keeppackages: " << keepPackages() << std::endl;
378     str << "- service     : " << service() << std::endl;
379
380     if (!targetDistribution().empty())
381       str << "- targetdistro: " << targetDistribution() << std::endl;
382
383     if (!metadataPath().empty())
384       str << "- metadataPath: " << metadataPath() << std::endl;
385
386     if (!packagesPath().empty())
387       str << "- packagesPath: " << packagesPath() << std::endl;
388
389     return str;
390   }
391
392   std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
393   {
394     RepoInfoBase::dumpAsIniOn(str);
395
396     if ( ! _pimpl->baseUrls.empty() )
397       str << "baseurl=";
398     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
399           it != _pimpl->baseUrls.end();
400           ++it )
401     {
402       str << *it << endl;
403     }
404
405     if ( ! _pimpl->path.empty() )
406       str << "path="<< path() << endl;
407
408     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
409       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
410
411     str << "type=" << type().asString() << endl;
412
413     if ( priority() != defaultPriority() )
414       str << "priority=" << priority() << endl;
415
416     if (!indeterminate(_pimpl->gpgcheck))
417       str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
418     if ( ! (gpgKeyUrl().asString().empty()) )
419       str << "gpgkey=" <<gpgKeyUrl() << endl;
420
421     if (!indeterminate(_pimpl->keeppackages))
422       str << "keeppackages=" << keepPackages() << endl;
423
424     if( ! service().empty() )
425       str << "service=" << service() << endl;
426
427     return str;
428   }
429
430   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
431   { return dumpAsXMLOn(str, ""); }
432
433   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
434   {
435     string tmpstr;
436     str
437       << "<repo"
438       << " alias=\"" << escape(alias()) << "\""
439       << " name=\"" << escape(name()) << "\"";
440     if (type() != repo::RepoType::NONE)
441       str << " type=\"" << type().asString() << "\"";
442     str
443       << " enabled=\"" << enabled() << "\""
444       << " autorefresh=\"" << autorefresh() << "\""
445       << " gpgcheck=\"" << gpgCheck() << "\"";
446     if (!(tmpstr = gpgKeyUrl().asString()).empty())
447       str << " gpgkey=\"" << escape(tmpstr) << "\"";
448     if (!(tmpstr = mirrorListUrl().asString()).empty())
449       str << " mirrorlist=\"" << escape(tmpstr) << "\"";
450     str << ">" << endl;
451
452     for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
453          urlit != baseUrlsEnd(); ++urlit)
454       str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
455
456     str << "</repo>" << endl;
457     return str;
458   }
459
460
461   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
462   {
463     return obj.dumpOn(str);
464   }
465
466
467   /////////////////////////////////////////////////////////////////
468 } // namespace zypp
469 ///////////////////////////////////////////////////////////////////