revert isLocal metod and use downloads instead to decide when cache packages.
[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   public:
50     bool enabled;
51     bool autorefresh;
52     bool gpgcheck;
53     bool keeppackages;
54     Url gpgkey_url;
55     repo::RepoType type;
56     Url mirrorlist_url;
57     std::set<Url> baseUrls;
58     Pathname path;
59     std::string alias;
60     std::string escaped_alias;
61     std::string name;
62     Pathname filepath;
63     Pathname metadatapath;
64     Pathname packagespath;
65     DefaultIntegral<unsigned,defaultPriority> priority;
66   public:
67
68   private:
69     friend Impl * rwcowClone<Impl>( const Impl * rhs );
70     /** clone for RWCOW_pointer */
71     Impl * clone() const
72     { return new Impl( *this ); }
73   };
74   ///////////////////////////////////////////////////////////////////
75
76   /** \relates RepoInfo::Impl Stream output */
77   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
78   {
79     return str << "RepoInfo::Impl";
80   }
81
82   ///////////////////////////////////////////////////////////////////
83   //
84   //    CLASS NAME : RepoInfo
85   //
86   ///////////////////////////////////////////////////////////////////
87
88   ///////////////////////////////////////////////////////////////////
89   //
90   //    METHOD NAME : RepoInfo::RepoInfo
91   //    METHOD TYPE : Ctor
92   //
93   RepoInfo::RepoInfo()
94   : _pimpl( new Impl() )
95   {}
96
97   ///////////////////////////////////////////////////////////////////
98   //
99   //    METHOD NAME : RepoInfo::~RepoInfo
100   //    METHOD TYPE : Dtor
101   //
102   RepoInfo::~RepoInfo()
103   {
104     //MIL << std::endl;
105   }
106
107   unsigned RepoInfo::priority() const
108   { return _pimpl->priority; }
109   unsigned RepoInfo::defaultPriority()
110   { return Impl::defaultPriority; }
111   RepoInfo & RepoInfo::setPriority( unsigned newval_r )
112   {
113     _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
114     return *this;
115   }
116
117
118   RepoInfo & RepoInfo::setEnabled( bool enabled )
119   {
120     _pimpl->enabled = enabled;
121     return *this;
122   }
123
124   RepoInfo & RepoInfo::setAutorefresh( bool autorefresh )
125   {
126     _pimpl->autorefresh = autorefresh;
127     return *this;
128   }
129
130   RepoInfo & RepoInfo::setGpgCheck( bool check )
131   {
132     _pimpl->gpgcheck = check;
133     return *this;
134   }
135
136   RepoInfo & RepoInfo::setMirrorListUrl( const Url &url )
137   {
138     _pimpl->mirrorlist_url = url;
139     return *this;
140   }
141
142   RepoInfo & RepoInfo::setGpgKeyUrl( const Url &url )
143   {
144     _pimpl->gpgkey_url = url;
145     return *this;
146   }
147
148   RepoInfo & RepoInfo::addBaseUrl( const Url &url )
149   {
150     if ( _pimpl->baseUrls.size()==0 ) //first url
151     {
152       if ( media::MediaAccess::downloads( url ) )
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 ///////////////////////////////////////////////////////////////////