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