allowing installation of packages with the same name; defined in zypp.conf
[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_deltarpm         ( true )
150         , solver_onlyRequires           ( false )
151         , apply_locks_file              ( true )
152
153       {
154         MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " <<  __TIME__ << endl;
155
156         // override_r has higest prio
157         // ZYPP_CONF might override /etc/zypp/zypp.conf
158         Pathname confpath( override_r );
159         if ( confpath.empty() )
160         {
161           const char *env_confpath = getenv( "ZYPP_CONF" );
162           confpath = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
163         }
164         else
165         {
166           // Inject this into ZConfig. Be shure this is
167           // allocated via new. See: reconfigureZConfig
168           INT << "Reconfigure to " << confpath << endl;
169           ZConfig::instance()._pimpl.reset( this );
170         }
171         if ( PathInfo(confpath).isExist() )
172         {
173           parser::IniDict dict( confpath );
174           //InputStream is(confpath);
175
176           for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
177                 sit != dict.sectionsEnd();
178                 ++sit )
179           {
180             string section(*sit);
181             //MIL << section << endl;
182             for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
183                   it != dict.entriesEnd(*sit);
184                   ++it )
185             {
186               string entry(it->first);
187               string value(it->second);
188               //DBG << (*it).first << "=" << (*it).second << endl;
189               if ( section == "main" )
190               {
191                 if ( entry == "arch" )
192                 {
193                   Arch carch( value );
194                   if ( carch != cfg_arch )
195                   {
196                     WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
197                     cfg_arch = carch;
198                   }
199                 }
200                 else if ( entry == "cachedir" )
201                 {
202                   cfg_cache_path = Pathname(value);
203                 }
204                 else if ( entry == "metadatadir" )
205                 {
206                   cfg_metadata_path = Pathname(value);
207                 }
208                 else if ( entry == "solvfilesdir" )
209                 {
210                   cfg_solvfiles_path = Pathname(value);
211                 }
212                 else if ( entry == "packagesdir" )
213                 {
214                   cfg_packages_path = Pathname(value);
215                 }
216                 else if ( entry == "configdir" )
217                 {
218                   cfg_config_path = Pathname(value);
219                 }
220                 else if ( entry == "reposdir" )
221                 {
222                   cfg_known_repos_path = Pathname(value);
223                 }
224                 else if ( entry == "repo.add.probe" )
225                 {
226                   repo_add_probe = str::strToBool( value, repo_add_probe );
227                 }
228                 else if ( entry == "repo.refresh.delay" )
229                 {
230                   str::strtonum(value, repo_refresh_delay);
231                 }
232                 else if ( entry == "download.use_deltarpm" )
233                 {
234                   download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
235                 }
236                 else if ( entry == "vendordir" )
237                 {
238                   cfg_vendor_path = Pathname(value);
239                 }
240                 else if ( entry == "productsdir" )
241                 {
242                   cfg_products_path = Pathname(value);
243                 }
244                 else if ( entry == "solver.onlyRequires" )
245                 {
246                   solver_onlyRequires = str::strToBool( value, solver_onlyRequires );
247                 }
248                 else if ( entry == "solver.checkSystemFile" )
249                 {
250                   solver_checkSystemFile = Pathname(value);
251                 }
252                 else if ( entry == "parallelInstallable" )
253                 {
254                   std::list<std::string> parallel;  
255                   str::split( value, back_inserter(parallel), ", \t" );
256                   for ( std::list<string>::const_iterator it = parallel.begin();
257                         it != parallel.end(); it++) {
258                       parallelInstallable.insert (IdString(*it));  
259                   }
260                 }               
261                 else if ( entry == "locksfile.path" )
262                 {
263                   locks_file = Pathname(value);
264                 }
265                 else if ( entry == "locksfile.apply" )
266                 {
267                   apply_locks_file = str::strToBool( value, apply_locks_file );
268                 }
269                 else if ( entry == "update.datadir" )
270                 {
271                   update_data_path = Pathname(value);
272                 }
273                 else if ( entry == "update.scriptsdir" )
274                 {
275                   update_scripts_path = Pathname(value);
276                 }
277                 else if ( entry == "update.messagessdir" )
278                 {
279                   update_messages_path = Pathname(value);
280                 }
281
282               }
283             }
284           }
285         }
286         else
287         {
288           MIL << confpath << " not found, using defaults instead." << endl;
289         }
290
291         // legacy:
292         if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
293         {
294           Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
295           if ( carch != cfg_arch )
296           {
297             WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
298             cfg_arch = carch;
299           }
300         }
301
302         MIL << "ZConfig singleton created." << endl;
303         MIL << "defaultTextLocale: '" << cfg_textLocale << "'" << endl;
304         MIL << "System architecture is '" << cfg_arch << "'" << endl;
305       }
306
307       ~Impl()
308       {}
309
310     public:
311     Arch     cfg_arch;
312     Locale   cfg_textLocale;
313
314     Pathname cfg_cache_path;
315     Pathname cfg_metadata_path;
316     Pathname cfg_solvfiles_path;
317     Pathname cfg_packages_path;
318
319     Pathname cfg_config_path;
320     Pathname cfg_known_repos_path;
321     Pathname cfg_known_services_path;
322     Pathname cfg_vendor_path;
323     Pathname cfg_products_path;
324     Pathname locks_file;
325
326     Pathname update_data_path;
327     Pathname update_scripts_path;
328     Pathname update_messages_path;
329
330     bool repo_add_probe;
331     unsigned repo_refresh_delay;
332
333     bool download_use_deltarpm;
334
335     bool solver_onlyRequires;
336     Pathname solver_checkSystemFile;
337       
338     std::set<IdString> parallelInstallable;  
339
340     bool apply_locks_file;
341
342   };
343   ///////////////////////////////////////////////////////////////////
344
345   // Backdoor to redirect ZConfig from within the running
346   // TEST-application. HANDLE WITH CARE!
347   void reconfigureZConfig( const Pathname & override_r )
348   {
349     // ctor puts itself unter smart pointer control.
350     new ZConfig::Impl( override_r );
351   }
352
353   ///////////////////////////////////////////////////////////////////
354   //
355   //    METHOD NAME : ZConfig::instance
356   //    METHOD TYPE : ZConfig &
357   //
358   ZConfig & ZConfig::instance()
359   {
360     static ZConfig _instance; // The singleton
361     return _instance;
362   }
363
364   ///////////////////////////////////////////////////////////////////
365   //
366   //    METHOD NAME : ZConfig::ZConfig
367   //    METHOD TYPE : Ctor
368   //
369   ZConfig::ZConfig()
370   : _pimpl( new Impl )
371   {}
372
373   ///////////////////////////////////////////////////////////////////
374   //
375   //    METHOD NAME : ZConfig::~ZConfig
376   //    METHOD TYPE : Dtor
377   //
378   ZConfig::~ZConfig( )
379   {}
380
381   ///////////////////////////////////////////////////////////////////
382   //
383   // system architecture
384   //
385   ///////////////////////////////////////////////////////////////////
386
387   Arch ZConfig::defaultSystemArchitecture()
388   {
389     static Arch _val( _autodetectSystemArchitecture() );
390     return _val;
391   }
392
393   Arch ZConfig::systemArchitecture() const
394   { return _pimpl->cfg_arch; }
395
396   void ZConfig::setSystemArchitecture( const Arch & arch_r )
397   {
398     if ( arch_r != _pimpl->cfg_arch )
399     {
400       WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
401       _pimpl->cfg_arch = arch_r;
402     }
403   }
404
405   ///////////////////////////////////////////////////////////////////
406   //
407   // text locale
408   //
409   ///////////////////////////////////////////////////////////////////
410
411   Locale ZConfig::defaultTextLocale()
412   {
413     static Locale _val( _autodetectTextLocale() );
414     return _val;
415   }
416
417   Locale ZConfig::textLocale() const
418   { return _pimpl->cfg_textLocale; }
419
420   void ZConfig::setTextLocale( const Locale & locale_r )
421   {
422     if ( locale_r != _pimpl->cfg_textLocale )
423     {
424       WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
425       _pimpl->cfg_textLocale = locale_r;
426     }
427   }
428
429   ///////////////////////////////////////////////////////////////////
430
431   Pathname ZConfig::repoCachePath() const
432   {
433     return ( _pimpl->cfg_cache_path.empty()
434         ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
435   }
436
437   Pathname ZConfig::repoMetadataPath() const
438   {
439     return ( _pimpl->cfg_metadata_path.empty()
440         ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
441   }
442
443   Pathname ZConfig::repoSolvfilesPath() const
444   {
445     return ( _pimpl->cfg_solvfiles_path.empty()
446         ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
447   }
448
449   Pathname ZConfig::repoPackagesPath() const
450   {
451     return ( _pimpl->cfg_packages_path.empty()
452         ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
453   }
454
455   ///////////////////////////////////////////////////////////////////
456
457   Pathname ZConfig::configPath() const
458   {
459     return ( _pimpl->cfg_config_path.empty()
460         ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
461   }
462
463   Pathname ZConfig::knownReposPath() const
464   {
465     return ( _pimpl->cfg_known_repos_path.empty()
466         ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
467   }
468
469   Pathname ZConfig::knownServicesPath() const
470   {
471     return ( _pimpl->cfg_known_services_path.empty()
472         ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
473   }
474
475   Pathname ZConfig::vendorPath() const
476   {
477     return ( _pimpl->cfg_vendor_path.empty()
478         ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
479   }
480
481   Pathname ZConfig::productsPath() const
482   {
483     return ( _pimpl->cfg_products_path.empty()
484         ? (configPath()/"products.d") : _pimpl->cfg_products_path );
485   }
486
487   Pathname ZConfig::locksFile() const
488   {
489     return ( _pimpl->locks_file.empty()
490         ? (configPath()/"locks") : _pimpl->locks_file );
491   }
492
493   ///////////////////////////////////////////////////////////////////
494
495   const std::string & ZConfig::cacheDBSplitJoinSeparator() const
496   {
497     static std::string s("!@$");
498     return s;
499   }
500
501   bool ZConfig::repo_add_probe() const
502   {
503     return _pimpl->repo_add_probe;
504   }
505
506   unsigned ZConfig::repo_refresh_delay() const
507   {
508     return _pimpl->repo_refresh_delay;
509   }
510
511   bool ZConfig::download_use_deltarpm() const
512   { return _pimpl->download_use_deltarpm; }
513
514
515   bool ZConfig::solver_onlyRequires() const
516   { return _pimpl->solver_onlyRequires; }
517
518   Pathname ZConfig::solver_checkSystemFile() const
519   { return _pimpl->solver_checkSystemFile; }
520
521
522   std::set<IdString> ZConfig::parallelInstallable() const
523   { return _pimpl->parallelInstallable; }
524
525   void ZConfig::addParallelInstallable(std::string &name)
526   { _pimpl->parallelInstallable.insert(IdString(name)); }
527     
528   bool ZConfig::removeParallelInstallable(std::string &name)
529   { return _pimpl->parallelInstallable.erase(IdString(name)); }      
530
531   bool ZConfig::apply_locks_file() const
532   {
533     return _pimpl->apply_locks_file;
534   }
535
536   Pathname ZConfig::update_dataPath() const
537   {
538     return ( _pimpl->update_data_path.empty()
539         ? Pathname("/var/adm") : _pimpl->update_data_path );
540   }
541
542   Pathname ZConfig::update_messagesPath() const
543   {
544     return ( _pimpl->update_messages_path.empty()
545              ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
546   }
547
548
549   Pathname ZConfig::update_scriptsPath() const
550   {
551     return ( _pimpl->update_scripts_path.empty()
552              ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
553   }
554
555   /////////////////////////////////////////////////////////////////
556 } // namespace zypp
557 ///////////////////////////////////////////////////////////////////