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