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