Fix failed package download due to unkown repository type (bnc #386386)
[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/media/MediaAccess.h"
17
18 #include "zypp/RepoInfo.h"
19
20 using namespace std;
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   ///////////////////////////////////////////////////////////////////
27   //
28   //    CLASS NAME : RepoInfo::Impl
29   //
30   /** RepoInfo implementation. */
31   struct RepoInfo::Impl
32   {
33
34     Impl()
35       : enabled (false),
36         autorefresh(false),
37         gpgcheck(true),
38         keeppackages(false),
39         type(repo::RepoType::NONE_e)
40     {}
41
42     ~Impl()
43     {
44       //MIL << std::endl;
45     }
46   public:
47     static const unsigned defaultPriority = 99;
48
49     void setProbedType( const repo::RepoType & t ) const
50     {
51       if ( type == repo::RepoType::NONE
52            && t != repo::RepoType::NONE )
53       {
54         // lazy init!
55         const_cast<Impl*>(this)->type = t;
56       }
57     }
58
59   public:
60     bool enabled;
61     bool autorefresh;
62     bool gpgcheck;
63     bool keeppackages;
64     Url gpgkey_url;
65     repo::RepoType type;
66     Url mirrorlist_url;
67     std::set<Url> baseUrls;
68     Pathname path;
69     std::string alias;
70     std::string escaped_alias;
71     std::string name;
72     Pathname filepath;
73     Pathname metadatapath;
74     Pathname packagespath;
75     DefaultIntegral<unsigned,defaultPriority> priority;
76   public:
77
78   private:
79     friend Impl * rwcowClone<Impl>( const Impl * rhs );
80     /** clone for RWCOW_pointer */
81     Impl * clone() const
82     { return new Impl( *this ); }
83   };
84   ///////////////////////////////////////////////////////////////////
85
86   /** \relates RepoInfo::Impl Stream output */
87   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
88   {
89     return str << "RepoInfo::Impl";
90   }
91
92   ///////////////////////////////////////////////////////////////////
93   //
94   //    CLASS NAME : RepoInfo
95   //
96   ///////////////////////////////////////////////////////////////////
97
98   ///////////////////////////////////////////////////////////////////
99   //
100   //    METHOD NAME : RepoInfo::RepoInfo
101   //    METHOD TYPE : Ctor
102   //
103   RepoInfo::RepoInfo()
104   : _pimpl( new Impl() )
105   {}
106
107   ///////////////////////////////////////////////////////////////////
108   //
109   //    METHOD NAME : RepoInfo::~RepoInfo
110   //    METHOD TYPE : Dtor
111   //
112   RepoInfo::~RepoInfo()
113   {
114     //MIL << std::endl;
115   }
116
117   unsigned RepoInfo::priority() const
118   { return _pimpl->priority; }
119   unsigned RepoInfo::defaultPriority()
120   { return Impl::defaultPriority; }
121   RepoInfo & RepoInfo::setPriority( unsigned newval_r )
122   {
123     _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
124     return *this;
125   }
126
127
128   RepoInfo & RepoInfo::setEnabled( bool enabled )
129   {
130     _pimpl->enabled = enabled;
131     return *this;
132   }
133
134   RepoInfo & RepoInfo::setAutorefresh( bool autorefresh )
135   {
136     _pimpl->autorefresh = autorefresh;
137     return *this;
138   }
139
140   RepoInfo & RepoInfo::setGpgCheck( bool check )
141   {
142     _pimpl->gpgcheck = check;
143     return *this;
144   }
145
146   RepoInfo & RepoInfo::setMirrorListUrl( const Url &url )
147   {
148     _pimpl->mirrorlist_url = url;
149     return *this;
150   }
151
152   RepoInfo & RepoInfo::setGpgKeyUrl( const Url &url )
153   {
154     _pimpl->gpgkey_url = url;
155     return *this;
156   }
157
158   RepoInfo & RepoInfo::addBaseUrl( const Url &url )
159   {
160     if ( _pimpl->baseUrls.size()==0 ) //first url
161     {
162       if ( media::MediaAccess::downloads( url ) )
163         setKeepPackages(true);
164       else
165         setKeepPackages(false);
166     }
167
168     _pimpl->baseUrls.insert(url);
169     return *this;
170   }
171
172   RepoInfo & RepoInfo::setBaseUrl( const Url &url )
173   {
174     _pimpl->baseUrls.clear();
175     addBaseUrl(url);
176     return *this;
177   }
178
179   RepoInfo & RepoInfo::setPath( const Pathname &path )
180   {
181     _pimpl->path = path;
182     return *this;
183   }
184
185   RepoInfo & RepoInfo::setAlias( const std::string &alias )
186   {
187     _pimpl->alias = alias;
188     // replace slashes with underscores
189     std::string fnd="/";
190     std::string rep="_";
191     std::string escaped_alias = alias;
192     size_t pos = escaped_alias.find(fnd);
193     while(pos!=string::npos)
194     {
195       escaped_alias.replace(pos,fnd.length(),rep);
196       pos = escaped_alias.find(fnd,pos+rep.length());
197     }
198     _pimpl->escaped_alias = escaped_alias;
199     return *this;
200   }
201
202   RepoInfo & RepoInfo::setType( const repo::RepoType &t )
203   {
204     _pimpl->type = t;
205     return *this;
206   }
207
208   void RepoInfo::setProbedType( const repo::RepoType &t ) const
209   { _pimpl->setProbedType( t ); }
210
211   RepoInfo & RepoInfo::setName( const std::string &name )
212   {
213     _pimpl->name = name;
214     return *this;
215   }
216
217   RepoInfo & RepoInfo::setFilepath( const Pathname &filepath )
218   {
219     _pimpl->filepath = filepath;
220     return *this;
221   }
222
223   RepoInfo & RepoInfo::setMetadataPath( const Pathname &path )
224   {
225     _pimpl->metadatapath = path;
226     return *this;
227   }
228
229   RepoInfo & RepoInfo::setPackagesPath( const Pathname &path )
230   {
231     _pimpl->packagespath = path;
232     return *this;
233   }
234
235   RepoInfo & RepoInfo::setKeepPackages( bool keep )
236   {
237     _pimpl->keeppackages = keep;
238     return *this;
239   }
240
241   bool RepoInfo::enabled() const
242   { return _pimpl->enabled; }
243
244   bool RepoInfo::autorefresh() const
245   { return _pimpl->autorefresh; }
246
247   bool RepoInfo::gpgCheck() const
248   { return _pimpl->gpgcheck; }
249
250   std::string RepoInfo::alias() const
251   { return _pimpl->alias; }
252
253   std::string RepoInfo::escaped_alias() const
254   { return _pimpl->escaped_alias; }
255
256   std::string RepoInfo::name() const
257   {
258     if ( _pimpl->name.empty() )
259     {
260       return alias();
261     }
262
263     repo::RepoVariablesStringReplacer replacer;
264     return replacer(_pimpl->name);
265   }
266
267   Pathname RepoInfo::filepath() const
268   { return _pimpl->filepath; }
269
270   Pathname RepoInfo::metadataPath() const
271   { return _pimpl->metadatapath; }
272
273   Pathname RepoInfo::packagesPath() const
274   { return _pimpl->packagespath; }
275
276   repo::RepoType RepoInfo::type() const
277   { return _pimpl->type; }
278
279   Url RepoInfo::mirrorListUrl() const
280   { return _pimpl->mirrorlist_url; }
281
282   Url RepoInfo::gpgKeyUrl() const
283   { return _pimpl->gpgkey_url; }
284
285   std::set<Url> RepoInfo::baseUrls() const
286   {
287     RepoInfo::url_set replaced_urls;
288     repo::RepoVariablesUrlReplacer replacer;
289     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
290           it != _pimpl->baseUrls.end();
291           ++it )
292     {
293       replaced_urls.insert(replacer(*it));
294     }
295     return replaced_urls;
296
297     return _pimpl->baseUrls;
298   }
299
300   Pathname RepoInfo::path() const
301   { return _pimpl->path; }
302
303   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
304   {
305     return make_transform_iterator( _pimpl->baseUrls.begin(),
306                                     repo::RepoVariablesUrlReplacer() );
307     //return _pimpl->baseUrls.begin();
308   }
309
310   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
311   {
312     //return _pimpl->baseUrls.end();
313     return make_transform_iterator( _pimpl->baseUrls.end(),
314                                     repo::RepoVariablesUrlReplacer() );
315   }
316
317   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
318   { return _pimpl->baseUrls.size(); }
319
320   bool RepoInfo::baseUrlsEmpty() const
321   { return _pimpl->baseUrls.empty(); }
322
323   bool RepoInfo::keepPackages() const
324   { return _pimpl->keeppackages; }
325
326   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
327   {
328     str << "--------------------------------------" << std::endl;
329     str << "- alias       : " << alias() << std::endl;
330     for ( urls_const_iterator it = baseUrlsBegin();
331           it != baseUrlsEnd();
332           ++it )
333     {
334       str << "- url         : " << *it << std::endl;
335     }
336     str << "- path        : " << path() << std::endl;
337     str << "- type        : " << type() << std::endl;
338     str << "- enabled     : " << enabled() << std::endl;
339     str << "- priority    : " << priority() << std::endl;
340
341     str << "- autorefresh : " << autorefresh() << std::endl;
342     str << "- gpgcheck    : " << gpgCheck() << std::endl;
343     str << "- gpgkey      : " << gpgKeyUrl() << std::endl;
344     str << "- keeppackages: " << keepPackages() << std::endl;
345
346     return str;
347   }
348
349   std::ostream & RepoInfo::dumpRepoOn( std::ostream & str ) const
350   {
351     // we save the original data without variable replacement
352     str << "[" << alias() << "]" << endl;
353     str << "name=" << _pimpl->name << endl;
354
355     if ( ! _pimpl->baseUrls.empty() )
356       str << "baseurl=";
357     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
358           it != _pimpl->baseUrls.end();
359           ++it )
360     {
361       str << *it << endl;
362     }
363
364     if ( ! _pimpl->path.empty() )
365       str << "path="<< path() << endl;
366
367     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
368       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
369
370     str << "type=" << type().asString() << endl;
371     str << "enabled=" << (enabled() ? "1" : "0") << endl;
372
373     if ( priority() != defaultPriority() )
374       str << "priority=" << priority() << endl;
375
376     str << "autorefresh=" << (autorefresh() ? "1" : "0") << endl;
377     str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
378     if ( ! (gpgKeyUrl().asString().empty()) )
379       str << "gpgkey=" <<gpgKeyUrl() << endl;
380
381     str << "keeppackages=" << keepPackages() << endl;
382
383     return str;
384   }
385
386   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
387   {
388     return obj.dumpOn(str);
389   }
390
391   /////////////////////////////////////////////////////////////////
392 } // namespace zypp
393 ///////////////////////////////////////////////////////////////////