added testsuite backdoor
[platform/upstream/libzypp.git] / zypp / ZConfig.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ZConfig.cc
10  *
11 */
12 extern "C"
13 {
14 #include <sys/utsname.h>
15 #include <unistd.h>
16 }
17 #include <iostream>
18 #include <fstream>
19 #include "zypp/base/Logger.h"
20 #include "zypp/base/IOStream.h"
21 #include "zypp/base/InputStream.h"
22 #include "zypp/base/String.h"
23
24 #include "zypp/ZConfig.h"
25 #include "zypp/ZYppFactory.h"
26 #include "zypp/PathInfo.h"
27 #include "zypp/parser/IniDict.h"
28
29 using namespace std;
30 using namespace zypp::filesystem;
31 using namespace zypp::parser;
32
33 ///////////////////////////////////////////////////////////////////
34 namespace zypp
35 { /////////////////////////////////////////////////////////////////
36
37   ///////////////////////////////////////////////////////////////////
38   namespace
39   { /////////////////////////////////////////////////////////////////
40
41     /** Determine system architecture evaluating \c uname and \c /proc/cpuinfo.
42     */
43     Arch _autodetectSystemArchitecture()
44     {
45       struct ::utsname buf;
46       if ( ::uname( &buf ) < 0 )
47       {
48         ERR << "Can't determine system architecture" << endl;
49         return Arch_noarch;
50       }
51
52       Arch architecture( buf.machine );
53       MIL << "Uname architecture is '" << buf.machine << "'" << endl;
54
55       // some CPUs report i686 but dont implement cx8 and cmov
56       // check for both flags in /proc/cpuinfo and downgrade
57       // to i586 if either is missing (cf bug #18885)
58       if ( architecture == Arch_i686 )
59       {
60         std::ifstream cpuinfo( "/proc/cpuinfo" );
61         if ( cpuinfo )
62         {
63           for( iostr::EachLine in( cpuinfo ); in; in.next() )
64           {
65             if ( str::hasPrefix( *in, "flags" ) )
66             {
67               if (    in->find( "cx8" ) == std::string::npos
68                    || in->find( "cmov" ) == std::string::npos )
69               {
70                 architecture = Arch_i586;
71                 WAR << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
72               }
73               break;
74             }
75           }
76         }
77         else
78         {
79           ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
80         }
81       }
82       return architecture;
83     }
84
85      /** The locale to be used for texts and messages.
86      *
87      * For the encoding to be used the preference is
88      *
89      *    LC_ALL, LC_CTYPE, LANG
90      *
91      * For the language of the messages to be used, the preference is
92      *
93      *    LANGUAGE, LC_ALL, LC_MESSAGES, LANG
94      *
95      * Note that LANGUAGE can contain more than one locale name, it can be
96      * a list of locale names like for example
97      *
98      *    LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
99
100      * \todo Support dynamic fallbacklists defined by LANGUAGE
101      */
102     Locale _autodetectTextLocale()
103     {
104       Locale ret( "en" );
105       const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
106       for ( const char ** envvar = envlist; *envvar; ++envvar )
107       {
108         const char * envlang = getenv( *envvar );
109         if ( envlang )
110         {
111           std::string envstr( envlang );
112           if ( envstr != "POSIX" && envstr != "C" )
113           {
114             Locale lang( envstr );
115             if ( ! lang.code().empty() )
116             {
117               MIL << "Found " << *envvar << "=" << envstr << endl;
118               ret = lang;
119               break;
120             }
121           }
122         }
123       }
124       MIL << "Default text locale is '" << ret << "'" << endl;
125       return ret;
126     }
127
128    /////////////////////////////////////////////////////////////////
129   } // namespace zypp
130   ///////////////////////////////////////////////////////////////////
131
132   ///////////////////////////////////////////////////////////////////
133   //
134   //    CLASS NAME : ZConfig::Impl
135   //
136   /** ZConfig implementation.
137    * \todo Enrich section and entry definition by some comment
138    * (including the default setting and provide some method to
139    * write this into a sample zypp.conf.
140   */
141   class ZConfig::Impl
142   {
143     public:
144       Impl( const Pathname & override_r = Pathname() )
145         : cfg_arch                ( defaultSystemArchitecture() )
146         , cfg_textLocale          ( defaultTextLocale() )
147         , repo_add_probe          ( false )
148         , repo_refresh_delay      ( 10 )
149         , download_use_patchrpm   ( true )
150         , download_use_deltarpm   ( true )
151
152       {
153         MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " <<  __TIME__ << endl;
154
155         // override_r has higest prio
156         // ZYPP_CONF might override /etc/zypp/zypp.conf
157         Pathname confpath( override_r );
158         if ( confpath.empty() )
159         {
160           const char *env_confpath = getenv( "ZYPP_CONF" );
161           confpath = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
162         }
163         else
164         {
165           // Inject this into ZConfig. Be shure this is
166           // allocated via new. See: reconfigureZConfig
167           INT << "Reconfigure to " << confpath << endl;
168           ZConfig::instance()._pimpl.reset( this );
169         }
170         if ( PathInfo(confpath).isExist() )
171         {
172           parser::IniDict dict( confpath );
173           //InputStream is(confpath);
174
175           for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
176                 sit != dict.sectionsEnd();
177                 ++sit )
178           {
179             string section(*sit);
180             //MIL << section << endl;
181             for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
182                   it != dict.entriesEnd(*sit);
183                   ++it )
184             {
185               string entry(it->first);
186               string value(it->second);
187               //DBG << (*it).first << "=" << (*it).second << endl;
188               if ( section == "main" )
189               {
190                 if ( entry == "arch" )
191                 {
192                   Arch carch( value );
193                   if ( carch != cfg_arch )
194                   {
195                     WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
196                     cfg_arch = carch;
197                   }
198                 }
199                 else if ( entry == "metadatadir" )
200                 {
201                   cfg_metadata_path = Pathname(value);
202                 }
203                 else if ( entry == "reposdir" )
204                 {
205                   cfg_known_repos_path = Pathname(value);
206                 }
207                 else if ( entry == "cachedir" )
208                 {
209                   cfg_cache_path = Pathname(value);
210                 }
211                 else if ( entry == "repo.add.probe" )
212                 {
213                   repo_add_probe = str::strToBool( value, repo_add_probe );
214                 }
215                 else if ( entry == "repo.refresh.delay" )
216                 {
217                   str::strtonum(value, repo_refresh_delay);
218                 }
219                 else if ( entry == "download.use_patchrpm" )
220                 {
221                   download_use_patchrpm = str::strToBool( value, download_use_patchrpm );
222                 }
223                 else if ( entry == "download.use_deltarpm" )
224                 {
225                   download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
226                 }
227                 else if ( entry == "vendordir" )
228                 {
229                   cfg_vendor_path = Pathname(value);
230                 }
231                 else if ( entry == "packagesdir" )
232                 {
233                   cfg_packages_path = Pathname(value);
234                 }
235               }
236             }
237           }
238         }
239         else
240         {
241           MIL << confpath << " not found, using defaults instead." << endl;
242         }
243
244         // legacy:
245         if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
246         {
247           Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
248           if ( carch != cfg_arch )
249           {
250             WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
251             cfg_arch = carch;
252           }
253         }
254
255         MIL << "ZConfig singleton created." << endl;
256         MIL << "defaultTextLocale: '" << cfg_textLocale << "'" << endl;
257         MIL << "System architecture is '" << cfg_arch << "'" << endl;
258       }
259
260       ~Impl()
261       {}
262
263     public:
264     Arch     cfg_arch;
265     Locale   cfg_textLocale;
266
267     Pathname cfg_metadata_path;
268     Pathname cfg_packages_path;
269     Pathname cfg_cache_path;
270     Pathname cfg_known_repos_path;
271     Pathname cfg_vendor_path;
272
273     bool repo_add_probe;
274     unsigned repo_refresh_delay;
275
276     bool download_use_patchrpm;
277     bool download_use_deltarpm;
278
279
280   };
281   ///////////////////////////////////////////////////////////////////
282
283   // Backdoor to redirect ZConfig from within the running
284   // TEST-application. HANDLE WITH CARE!
285   void reconfigureZConfig( const Pathname & override_r )
286   {
287     // ctor puts itself unter smart pointer control.
288     new ZConfig::Impl( override_r );
289   }
290
291   ///////////////////////////////////////////////////////////////////
292   //
293   //    METHOD NAME : ZConfig::instance
294   //    METHOD TYPE : ZConfig &
295   //
296   ZConfig & ZConfig::instance()
297   {
298     static ZConfig _instance; // The singleton
299     return _instance;
300   }
301
302   ///////////////////////////////////////////////////////////////////
303   //
304   //    METHOD NAME : ZConfig::ZConfig
305   //    METHOD TYPE : Ctor
306   //
307   ZConfig::ZConfig()
308   : _pimpl( new Impl )
309   {}
310
311   ///////////////////////////////////////////////////////////////////
312   //
313   //    METHOD NAME : ZConfig::~ZConfig
314   //    METHOD TYPE : Dtor
315   //
316   ZConfig::~ZConfig( )
317   {}
318
319   ///////////////////////////////////////////////////////////////////
320   //
321   // system architecture
322   //
323   ///////////////////////////////////////////////////////////////////
324
325   Arch ZConfig::defaultSystemArchitecture()
326   {
327     static Arch _val( _autodetectSystemArchitecture() );
328     return _val;
329   }
330
331   Arch ZConfig::systemArchitecture() const
332   { return _pimpl->cfg_arch; }
333
334   void ZConfig::setSystemArchitecture( const Arch & arch_r )
335   {
336     if ( arch_r != _pimpl->cfg_arch )
337     {
338       WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
339       _pimpl->cfg_arch = arch_r;
340     }
341   }
342
343   ///////////////////////////////////////////////////////////////////
344   //
345   // text locale
346   //
347   ///////////////////////////////////////////////////////////////////
348
349   Locale ZConfig::defaultTextLocale()
350   {
351     static Locale _val( _autodetectTextLocale() );
352     return _val;
353   }
354
355   Locale ZConfig::textLocale() const
356   { return _pimpl->cfg_textLocale; }
357
358   void ZConfig::setTextLocale( const Locale & locale_r )
359   {
360     if ( locale_r != _pimpl->cfg_textLocale )
361     {
362       WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
363       _pimpl->cfg_textLocale = locale_r;
364     }
365   }
366
367   ///////////////////////////////////////////////////////////////////
368
369   Pathname ZConfig::repoMetadataPath() const
370   {
371     return ( _pimpl->cfg_metadata_path.empty()
372         ? Pathname("/var/cache/zypp/raw") : _pimpl->cfg_metadata_path );
373   }
374
375   Pathname ZConfig::repoPackagesPath() const
376   {
377     return ( _pimpl->cfg_packages_path.empty()
378         ? Pathname("/var/cache/zypp/packages") : _pimpl->cfg_packages_path );
379   }
380
381   Pathname ZConfig::repoCachePath() const
382   {
383     return ( _pimpl->cfg_cache_path.empty()
384         ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
385   }
386
387   Pathname ZConfig::knownReposPath() const
388   {
389     return ( _pimpl->cfg_known_repos_path.empty()
390         ? Pathname("/etc/zypp/repos.d") : _pimpl->cfg_known_repos_path );
391   }
392
393   const std::string & ZConfig::cacheDBSplitJoinSeparator() const
394   {
395     static std::string s("!@$");
396     return s;
397   }
398
399   bool ZConfig::repo_add_probe() const
400   {
401     return _pimpl->repo_add_probe;
402   }
403
404   unsigned ZConfig::repo_refresh_delay() const
405   {
406     return _pimpl->repo_refresh_delay;
407   }
408
409   bool ZConfig::download_use_patchrpm() const
410   { return _pimpl->download_use_patchrpm; }
411
412   bool ZConfig::download_use_deltarpm() const
413   { return _pimpl->download_use_deltarpm; }
414
415   Pathname ZConfig::vendorPath() const
416   {
417     return ( _pimpl->cfg_vendor_path.empty()
418         ? Pathname("/etc/zypp/vendors.d") : _pimpl->cfg_vendor_path );
419   }
420
421   /////////////////////////////////////////////////////////////////
422 } // namespace zypp
423 ///////////////////////////////////////////////////////////////////