Imported Upstream version 16.3.2
[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 <features.h>
15 #include <sys/utsname.h>
16 #if __GLIBC_PREREQ (2,16)
17 #include <sys/auxv.h>   // getauxval for PPC64P7 detection
18 #endif
19 #include <unistd.h>
20 #include <solv/solvversion.h>
21 }
22 #include <iostream>
23 #include <fstream>
24 #include "zypp/base/LogTools.h"
25 #include "zypp/base/IOStream.h"
26 #include "zypp/base/InputStream.h"
27 #include "zypp/base/String.h"
28 #include "zypp/base/Regex.h"
29
30 #include "zypp/ZConfig.h"
31 #include "zypp/ZYppFactory.h"
32 #include "zypp/PathInfo.h"
33 #include "zypp/parser/IniDict.h"
34
35 #include "zypp/sat/Pool.h"
36 #include "zypp/sat/detail/PoolImpl.h"
37
38 using namespace std;
39 using namespace zypp::filesystem;
40 using namespace zypp::parser;
41
42 #undef ZYPP_BASE_LOGGER_LOGGROUP
43 #define ZYPP_BASE_LOGGER_LOGGROUP "zconfig"
44
45 ///////////////////////////////////////////////////////////////////
46 namespace zypp
47 { /////////////////////////////////////////////////////////////////
48   /** \addtogroup ZyppConfig Zypp Configuration Options
49    *
50    * The global \c zypp.conf configuration file is per default located in \c /etc/zypp/.
51    * An alternate config file can be set using the environment varaible \c ZYPP_CONF=<PATH>
52    * (see \ref zypp-envars).
53    *
54    * \section ZyppConfig_ZyppConfSample Sample zypp.conf
55    * \include ../zypp.conf
56    */
57   ///////////////////////////////////////////////////////////////////
58   namespace
59   { /////////////////////////////////////////////////////////////////
60
61     /** Determine system architecture evaluating \c uname and \c /proc/cpuinfo.
62     */
63     Arch _autodetectSystemArchitecture()
64     {
65       struct ::utsname buf;
66       if ( ::uname( &buf ) < 0 )
67       {
68         ERR << "Can't determine system architecture" << endl;
69         return Arch_noarch;
70       }
71
72       Arch architecture( buf.machine );
73       MIL << "Uname architecture is '" << buf.machine << "'" << endl;
74
75       if ( architecture == Arch_i686 )
76       {
77         // some CPUs report i686 but dont implement cx8 and cmov
78         // check for both flags in /proc/cpuinfo and downgrade
79         // to i586 if either is missing (cf bug #18885)
80         std::ifstream cpuinfo( "/proc/cpuinfo" );
81         if ( cpuinfo )
82         {
83           for( iostr::EachLine in( cpuinfo ); in; in.next() )
84           {
85             if ( str::hasPrefix( *in, "flags" ) )
86             {
87               if (    in->find( "cx8" ) == std::string::npos
88                    || in->find( "cmov" ) == std::string::npos )
89               {
90                 architecture = Arch_i586;
91                 WAR << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
92               }
93               break;
94             }
95           }
96         }
97         else
98         {
99           ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
100         }
101       }
102       else if ( architecture == Arch_sparc || architecture == Arch_sparc64 )
103       {
104         // Check for sun4[vum] to get the real arch. (bug #566291)
105         std::ifstream cpuinfo( "/proc/cpuinfo" );
106         if ( cpuinfo )
107         {
108           for( iostr::EachLine in( cpuinfo ); in; in.next() )
109           {
110             if ( str::hasPrefix( *in, "type" ) )
111             {
112               if ( in->find( "sun4v" ) != std::string::npos )
113               {
114                 architecture = ( architecture == Arch_sparc64 ? Arch_sparc64v : Arch_sparcv9v );
115                 WAR << "CPU has 'sun4v': architecture upgraded to '" << architecture << "'" << endl;
116               }
117               else if ( in->find( "sun4u" ) != std::string::npos )
118               {
119                 architecture = ( architecture == Arch_sparc64 ? Arch_sparc64 : Arch_sparcv9 );
120                 WAR << "CPU has 'sun4u': architecture upgraded to '" << architecture << "'" << endl;
121               }
122               else if ( in->find( "sun4m" ) != std::string::npos )
123               {
124                 architecture = Arch_sparcv8;
125                 WAR << "CPU has 'sun4m': architecture upgraded to '" << architecture << "'" << endl;
126               }
127               break;
128             }
129           }
130         }
131         else
132         {
133           ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
134         }
135       }
136       else if ( architecture == Arch_armv7l || architecture == Arch_armv6l )
137       {
138         std::ifstream platform( "/etc/rpm/platform" );
139         if (platform)
140         {
141           for( iostr::EachLine in( platform ); in; in.next() )
142           {
143             if ( str::hasPrefix( *in, "armv7hl-" ) )
144             {
145               architecture = Arch_armv7hl;
146               WAR << "/etc/rpm/platform contains armv7hl-: architecture upgraded to '" << architecture << "'" << endl;
147               break;
148             }
149             if ( str::hasPrefix( *in, "armv6hl-" ) )
150             {
151               architecture = Arch_armv6hl;
152               WAR << "/etc/rpm/platform contains armv6hl-: architecture upgraded to '" << architecture << "'" << endl;
153               break;
154             }
155           }
156         }
157       }
158 #if __GLIBC_PREREQ (2,16)
159       else if ( architecture == Arch_ppc64 )
160       {
161         const char * platform = (const char *)getauxval( AT_PLATFORM );
162         int powerlvl;
163         if ( platform && sscanf( platform, "power%d", &powerlvl ) == 1 && powerlvl > 6 )
164           architecture = Arch_ppc64p7;
165       }
166 #endif
167       return architecture;
168     }
169
170      /** The locale to be used for texts and messages.
171      *
172      * For the encoding to be used the preference is
173      *
174      *    LC_ALL, LC_CTYPE, LANG
175      *
176      * For the language of the messages to be used, the preference is
177      *
178      *    LANGUAGE, LC_ALL, LC_MESSAGES, LANG
179      *
180      * Note that LANGUAGE can contain more than one locale name, it can be
181      * a list of locale names like for example
182      *
183      *    LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
184
185      * \todo Support dynamic fallbacklists defined by LANGUAGE
186      */
187     Locale _autodetectTextLocale()
188     {
189       Locale ret( Locale::enCode );
190       const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
191       for ( const char ** envvar = envlist; *envvar; ++envvar )
192       {
193         const char * envlang = getenv( *envvar );
194         if ( envlang )
195         {
196           std::string envstr( envlang );
197           if ( envstr != "POSIX" && envstr != "C" )
198           {
199             Locale lang( envstr );
200             if ( lang )
201             {
202               MIL << "Found " << *envvar << "=" << envstr << endl;
203               ret = lang;
204               break;
205             }
206           }
207         }
208       }
209       MIL << "Default text locale is '" << ret << "'" << endl;
210 #warning HACK AROUND BOOST_TEST_CATCH_SYSTEM_ERRORS
211       setenv( "BOOST_TEST_CATCH_SYSTEM_ERRORS", "no", 1 );
212       return ret;
213     }
214
215
216     inline Pathname _autodetectSystemRoot()
217     {
218       Target_Ptr target( getZYpp()->getTarget() );
219       return target ? target->root() : Pathname();
220     }
221
222     inline Pathname _autodetectZyppConfPath()
223     {
224       const char *env_confpath = getenv( "ZYPP_CONF" );
225       return env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
226     }
227
228    /////////////////////////////////////////////////////////////////
229   } // namespace zypp
230   ///////////////////////////////////////////////////////////////////
231
232   /** Mutable option. */
233   template<class Tp>
234       struct Option
235       {
236         typedef Tp value_type;
237
238         /** No default ctor, explicit initialisation! */
239         Option( const value_type & initial_r )
240           : _val( initial_r )
241         {}
242
243         /** Get the value.  */
244         const value_type & get() const
245         { return _val; }
246
247         /** Autoconversion to value_type.  */
248         operator const value_type &() const
249         { return _val; }
250
251         /** Set a new value.  */
252         void set( const value_type & newval_r )
253         { _val = newval_r; }
254
255         /** Non-const reference to set a new value. */
256         value_type & ref()
257         { return _val; }
258
259         private:
260           value_type _val;
261       };
262
263   /** Mutable option with initial value also remembering a config value. */
264   template<class Tp>
265       struct DefaultOption : public Option<Tp>
266       {
267         typedef Tp         value_type;
268         typedef Option<Tp> option_type;
269
270         DefaultOption( const value_type & initial_r )
271           : Option<Tp>( initial_r ), _default( initial_r )
272         {}
273
274         /** Reset value to the current default. */
275         void restoreToDefault()
276         { this->set( _default.get() ); }
277
278         /** Reset value to a new default. */
279         void restoreToDefault( const value_type & newval_r )
280         { setDefault( newval_r ); restoreToDefault(); }
281
282         /** Get the current default value. */
283         const value_type & getDefault() const
284         { return _default.get(); }
285
286         /** Set a new default value. */
287         void setDefault( const value_type & newval_r )
288         { _default.set( newval_r ); }
289
290         private:
291           option_type _default;
292       };
293
294   ///////////////////////////////////////////////////////////////////
295   //
296   //    CLASS NAME : ZConfig::Impl
297   //
298   /** ZConfig implementation.
299    * \todo Enrich section and entry definition by some comment
300    * (including the default setting and provide some method to
301    * write this into a sample zypp.conf.
302   */
303   class ZConfig::Impl
304   {
305     typedef std::set<std::string> MultiversionSpec;
306
307     public:
308       Impl( const Pathname & override_r = Pathname() )
309         : _parsedZyppConf               ( override_r )
310         , cfg_arch                      ( defaultSystemArchitecture() )
311         , cfg_textLocale                ( defaultTextLocale() )
312         , updateMessagesNotify          ( "single | /usr/lib/zypp/notify-message -p %p" )
313         , repo_add_probe                ( false )
314         , repo_refresh_delay            ( 10 )
315         , repoLabelIsAlias              ( false )
316         , download_use_deltarpm         ( true )
317         , download_use_deltarpm_always  ( false )
318         , download_media_prefer_download( true )
319         , download_mediaMountdir        ( "/var/adm/mount" )
320         , download_max_concurrent_connections( 5 )
321         , download_min_download_speed   ( 0 )
322         , download_max_download_speed   ( 0 )
323         , download_max_silent_tries     ( 5 )
324         , download_transfer_timeout     ( 180 )
325         , commit_downloadMode           ( DownloadDefault )
326         , gpgCheck                      ( true )
327         , repoGpgCheck                  ( indeterminate )
328         , pkgGpgCheck                   ( indeterminate )
329         , solver_onlyRequires           ( false )
330         , solver_allowVendorChange      ( false )
331         , solver_dupAllowDowngrade      ( true )
332         , solver_dupAllowNameChange     ( true )
333         , solver_dupAllowArchChange     ( true )
334         , solver_dupAllowVendorChange   ( true )
335         , solver_cleandepsOnRemove      ( false )
336         , solver_upgradeTestcasesToKeep ( 2 )
337         , solverUpgradeRemoveDroppedPackages( true )
338         , apply_locks_file              ( true )
339         , pluginsPath                   ( "/usr/lib/zypp/plugins" )
340       {
341         MIL << "libzypp: " << VERSION << endl;
342         // override_r has higest prio
343         // ZYPP_CONF might override /etc/zypp/zypp.conf
344         if ( _parsedZyppConf.empty() )
345         {
346           _parsedZyppConf = _autodetectZyppConfPath();
347         }
348         else
349         {
350           // Inject this into ZConfig. Be shure this is
351           // allocated via new. See: reconfigureZConfig
352           INT << "Reconfigure to " << _parsedZyppConf << endl;
353           ZConfig::instance()._pimpl.reset( this );
354         }
355         if ( PathInfo(_parsedZyppConf).isExist() )
356         {
357           parser::IniDict dict( _parsedZyppConf );
358           for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
359                 sit != dict.sectionsEnd();
360                 ++sit )
361           {
362             string section(*sit);
363             //MIL << section << endl;
364             for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
365                   it != dict.entriesEnd(*sit);
366                   ++it )
367             {
368               string entry(it->first);
369               string value(it->second);
370               //DBG << (*it).first << "=" << (*it).second << endl;
371               if ( section == "main" )
372               {
373                 if ( entry == "arch" )
374                 {
375                   Arch carch( value );
376                   if ( carch != cfg_arch )
377                   {
378                     WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
379                     cfg_arch = carch;
380                   }
381                 }
382                 else if ( entry == "cachedir" )
383                 {
384                   cfg_cache_path = Pathname(value);
385                 }
386                 else if ( entry == "metadatadir" )
387                 {
388                   cfg_metadata_path = Pathname(value);
389                 }
390                 else if ( entry == "solvfilesdir" )
391                 {
392                   cfg_solvfiles_path = Pathname(value);
393                 }
394                 else if ( entry == "packagesdir" )
395                 {
396                   cfg_packages_path = Pathname(value);
397                 }
398                 else if ( entry == "configdir" )
399                 {
400                   cfg_config_path = Pathname(value);
401                 }
402                 else if ( entry == "reposdir" )
403                 {
404                   cfg_known_repos_path = Pathname(value);
405                 }
406                 else if ( entry == "servicesdir" )
407                 {
408                   cfg_known_services_path = Pathname(value);
409                 }
410                 else if ( entry == "repo.add.probe" )
411                 {
412                   repo_add_probe = str::strToBool( value, repo_add_probe );
413                 }
414                 else if ( entry == "repo.refresh.delay" )
415                 {
416                   str::strtonum(value, repo_refresh_delay);
417                 }
418                 else if ( entry == "repo.refresh.locales" )
419                 {
420                   std::vector<std::string> tmp;
421                   str::split( value, back_inserter( tmp ), ", \t" );
422
423                   boost::function<Locale(const std::string &)> transform(
424                     [](const std::string & str_r)->Locale{ return Locale(str_r); }
425                   );
426                   repoRefreshLocales.insert( make_transform_iterator( tmp.begin(), transform ),
427                                              make_transform_iterator( tmp.end(), transform ) );
428                 }
429                 else if ( entry == "download.use_deltarpm" )
430                 {
431                   download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
432                 }
433                 else if ( entry == "download.use_deltarpm.always" )
434                 {
435                   download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
436                 }
437                 else if ( entry == "download.media_preference" )
438                 {
439                   download_media_prefer_download.restoreToDefault( str::compareCI( value, "volatile" ) != 0 );
440                 }
441
442                 else if ( entry == "download.media_mountdir" )
443                 {
444                   download_mediaMountdir.restoreToDefault( Pathname(value) );
445                 }
446
447                 else if ( entry == "download.max_concurrent_connections" )
448                 {
449                   str::strtonum(value, download_max_concurrent_connections);
450                 }
451                 else if ( entry == "download.min_download_speed" )
452                 {
453                   str::strtonum(value, download_min_download_speed);
454                 }
455                 else if ( entry == "download.max_download_speed" )
456                 {
457                   str::strtonum(value, download_max_download_speed);
458                 }
459                 else if ( entry == "download.max_silent_tries" )
460                 {
461                   str::strtonum(value, download_max_silent_tries);
462                 }
463                 else if ( entry == "download.transfer_timeout" )
464                 {
465                   str::strtonum(value, download_transfer_timeout);
466                   if ( download_transfer_timeout < 0 )          download_transfer_timeout = 0;
467                   else if ( download_transfer_timeout > 3600 )  download_transfer_timeout = 3600;
468                 }
469                 else if ( entry == "commit.downloadMode" )
470                 {
471                   commit_downloadMode.set( deserializeDownloadMode( value ) );
472                 }
473                 else if ( entry == "gpgcheck" )
474                 {
475                   gpgCheck.set( str::strToBool( value, gpgCheck ) );
476                 }
477                 else if ( entry == "repo_gpgcheck" )
478                 {
479                   repoGpgCheck.set( str::strToTriBool( value ) );
480                 }
481                 else if ( entry == "pkg_gpgcheck" )
482                 {
483                   pkgGpgCheck.set( str::strToTriBool( value ) );
484                 }
485                 else if ( entry == "vendordir" )
486                 {
487                   cfg_vendor_path = Pathname(value);
488                 }
489                 else if ( entry == "multiversiondir" )
490                 {
491                   cfg_multiversion_path = Pathname(value);
492                 }
493                 else if ( entry == "solver.onlyRequires" )
494                 {
495                   solver_onlyRequires.set( str::strToBool( value, solver_onlyRequires ) );
496                 }
497                 else if ( entry == "solver.allowVendorChange" )
498                 {
499                   solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange ) );
500                 }
501                 else if ( entry == "solver.dupAllowDowngrade" )
502                 {
503                   solver_dupAllowDowngrade.set( str::strToBool( value, solver_dupAllowDowngrade ) );
504                 }
505                 else if ( entry == "solver.dupAllowNameChange" )
506                 {
507                   solver_dupAllowNameChange.set( str::strToBool( value, solver_dupAllowNameChange ) );
508                 }
509                 else if ( entry == "solver.dupAllowArchChange" )
510                 {
511                   solver_dupAllowArchChange.set( str::strToBool( value, solver_dupAllowArchChange ) );
512                 }
513                 else if ( entry == "solver.dupAllowVendorChange" )
514                 {
515                   solver_dupAllowVendorChange.set( str::strToBool( value, solver_dupAllowVendorChange ) );
516                 }
517                 else if ( entry == "solver.cleandepsOnRemove" )
518                 {
519                   solver_cleandepsOnRemove.set( str::strToBool( value, solver_cleandepsOnRemove ) );
520                 }
521                 else if ( entry == "solver.upgradeTestcasesToKeep" )
522                 {
523                   solver_upgradeTestcasesToKeep.set( str::strtonum<unsigned>( value ) );
524                 }
525                 else if ( entry == "solver.upgradeRemoveDroppedPackages" )
526                 {
527                   solverUpgradeRemoveDroppedPackages.restoreToDefault( str::strToBool( value, solverUpgradeRemoveDroppedPackages.getDefault() ) );
528                 }
529                 else if ( entry == "solver.checkSystemFile" )
530                 {
531                   solver_checkSystemFile = Pathname(value);
532                 }
533                 else if ( entry == "solver.checkSystemFileDir" )
534                 {
535                   solver_checkSystemFileDir = Pathname(value);
536                 }
537                 else if ( entry == "multiversion" )
538                 {
539                   MultiversionSpec & defSpec( _multiversionMap.getDefaultSpec() );
540                   str::splitEscaped( value, std::inserter( defSpec, defSpec.end() ), ", \t" );
541                 }
542                 else if ( entry == "locksfile.path" )
543                 {
544                   locks_file = Pathname(value);
545                 }
546                 else if ( entry == "locksfile.apply" )
547                 {
548                   apply_locks_file = str::strToBool( value, apply_locks_file );
549                 }
550                 else if ( entry == "update.datadir" )
551                 {
552                   update_data_path = Pathname(value);
553                 }
554                 else if ( entry == "update.scriptsdir" )
555                 {
556                   update_scripts_path = Pathname(value);
557                 }
558                 else if ( entry == "update.messagessdir" )
559                 {
560                   update_messages_path = Pathname(value);
561                 }
562                 else if ( entry == "update.messages.notify" )
563                 {
564                   updateMessagesNotify.set( value );
565                 }
566                 else if ( entry == "rpm.install.excludedocs" )
567                 {
568                   rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS,
569                                            str::strToBool( value, false ) );
570                 }
571                 else if ( entry == "history.logfile" )
572                 {
573                   history_log_path = Pathname(value);
574                 }
575                 else if ( entry == "credentials.global.dir" )
576                 {
577                   credentials_global_dir_path = Pathname(value);
578                 }
579                 else if ( entry == "credentials.global.file" )
580                 {
581                   credentials_global_file_path = Pathname(value);
582                 }
583               }
584             }
585           }
586           //
587
588         }
589         else
590         {
591           MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
592           _parsedZyppConf = _parsedZyppConf.extend( " (NOT FOUND)" );
593         }
594
595         // legacy:
596         if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
597         {
598           Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
599           if ( carch != cfg_arch )
600           {
601             WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
602             cfg_arch = carch;
603           }
604         }
605         MIL << "ZConfig singleton created." << endl;
606       }
607
608       ~Impl()
609       {}
610
611     public:
612     /** Remember any parsed zypp.conf. */
613     Pathname _parsedZyppConf;
614
615     Arch     cfg_arch;
616     Locale   cfg_textLocale;
617
618     Pathname cfg_cache_path;
619     Pathname cfg_metadata_path;
620     Pathname cfg_solvfiles_path;
621     Pathname cfg_packages_path;
622
623     Pathname cfg_config_path;
624     Pathname cfg_known_repos_path;
625     Pathname cfg_known_services_path;
626
627     Pathname cfg_vendor_path;
628     Pathname cfg_multiversion_path;
629     Pathname locks_file;
630
631     Pathname update_data_path;
632     Pathname update_scripts_path;
633     Pathname update_messages_path;
634     DefaultOption<std::string> updateMessagesNotify;
635
636     bool        repo_add_probe;
637     unsigned    repo_refresh_delay;
638     LocaleSet   repoRefreshLocales;
639     bool        repoLabelIsAlias;
640
641     bool download_use_deltarpm;
642     bool download_use_deltarpm_always;
643     DefaultOption<bool> download_media_prefer_download;
644     DefaultOption<Pathname> download_mediaMountdir;
645
646     int download_max_concurrent_connections;
647     int download_min_download_speed;
648     int download_max_download_speed;
649     int download_max_silent_tries;
650     int download_transfer_timeout;
651
652     Option<DownloadMode> commit_downloadMode;
653
654     Option<bool>        gpgCheck;
655     Option<TriBool>     repoGpgCheck;
656     Option<TriBool>     pkgGpgCheck;
657
658     Option<bool>        solver_onlyRequires;
659     Option<bool>        solver_allowVendorChange;
660     Option<bool>        solver_dupAllowDowngrade;
661     Option<bool>        solver_dupAllowNameChange;
662     Option<bool>        solver_dupAllowArchChange;
663     Option<bool>        solver_dupAllowVendorChange;
664     Option<bool>        solver_cleandepsOnRemove;
665     Option<unsigned>    solver_upgradeTestcasesToKeep;
666     DefaultOption<bool> solverUpgradeRemoveDroppedPackages;
667
668     Pathname solver_checkSystemFile;
669     Pathname solver_checkSystemFileDir;
670
671     MultiversionSpec &          multiversion()          { return getMultiversion(); }
672     const MultiversionSpec &    multiversion() const    { return getMultiversion(); }
673
674     bool apply_locks_file;
675
676     target::rpm::RpmInstFlags rpmInstallFlags;
677
678     Pathname history_log_path;
679     Pathname credentials_global_dir_path;
680     Pathname credentials_global_file_path;
681
682     std::string userData;
683
684     Option<Pathname> pluginsPath;
685
686   private:
687     // HACK for bnc#906096: let pool re-evaluate multiversion spec
688     // if target root changes. ZConfig returns data sensitive to
689     // current target root.
690     // TODO Actually we'd need to scan the target systems zypp.conf and
691     // overlay all system specific values.
692     struct MultiversionMap
693     {
694       typedef std::map<Pathname,MultiversionSpec> SpecMap;
695
696       MultiversionSpec & getSpec( Pathname root_r, const Impl & zConfImpl_r )   // from system at root
697       {
698         // _specMap[]     - the plain zypp.conf value
699         // _specMap[/]    - combine [] and multiversion.d scan
700         // _specMap[root] - scan root/zypp.conf and root/multiversion.d
701
702         if ( root_r.empty() )
703           root_r == "/";
704         bool cacheHit = _specMap.count( root_r );
705         MultiversionSpec & ret( _specMap[root_r] );     // creates new entry on the fly
706
707         if ( ! cacheHit )
708         {
709           if ( root_r == "/" )
710             ret.swap( _specMap[Pathname()] );           // original zypp.conf
711           else
712             scanConfAt( root_r, ret, zConfImpl_r );     // scan zypp.conf at root_r
713           scanDirAt( root_r, ret, zConfImpl_r );        // add multiversion.d at root_r
714           using zypp::operator<<;
715           MIL << "MultiversionSpec '" << root_r << "' = " << ret << endl;
716         }
717         return ret;
718       }
719
720       MultiversionSpec & getDefaultSpec()       // Spec from zypp.conf parsing; called before any getSpec
721       { return _specMap[Pathname()]; }
722
723     private:
724       void scanConfAt( const Pathname root_r, MultiversionSpec & spec_r, const Impl & zConfImpl_r )
725       {
726         static const str::regex rx( "^multiversion *= *(.*)" );
727         str::smatch what;
728         iostr::simpleParseFile( InputStream( Pathname::assertprefix( root_r, _autodetectZyppConfPath() ) ),
729                                 [&]( int num_r, std::string line_r )->bool
730                                 {
731                                   if ( line_r[0] == 'm' && str::regex_match( line_r, what, rx ) )
732                                   {
733                                     str::splitEscaped( what[1], std::inserter( spec_r, spec_r.end() ), ", \t" );
734                                     return false;       // stop after match
735                                   }
736                                   return true;
737                                 } );
738       }
739
740       void scanDirAt( const Pathname root_r, MultiversionSpec & spec_r, const Impl & zConfImpl_r )
741       {
742         // NOTE:  Actually we'd need to scan and use the root_r! zypp.conf values.
743         Pathname multiversionDir( zConfImpl_r.cfg_multiversion_path );
744         if ( multiversionDir.empty() )
745           multiversionDir = ( zConfImpl_r.cfg_config_path.empty()
746                             ? Pathname("/etc/zypp")
747                             : zConfImpl_r.cfg_config_path ) / "multiversion.d";
748
749         filesystem::dirForEach( Pathname::assertprefix( root_r, multiversionDir ),
750                                 [&spec_r]( const Pathname & dir_r, const char *const & name_r )->bool
751                                 {
752                                   MIL << "Parsing " << dir_r/name_r << endl;
753                                   iostr::simpleParseFile( InputStream( dir_r/name_r ),
754                                                           [&spec_r]( int num_r, std::string line_r )->bool
755                                                           {
756                                                             DBG << "  found " << line_r << endl;
757                                                             spec_r.insert( std::move(line_r) );
758                                                             return true;
759                                                           } );
760                                   return true;
761                                 } );
762       }
763
764     private:
765       SpecMap _specMap;
766     };
767
768     MultiversionSpec & getMultiversion() const
769     { return _multiversionMap.getSpec( _autodetectSystemRoot(), *this ); }
770
771     mutable MultiversionMap _multiversionMap;
772   };
773   ///////////////////////////////////////////////////////////////////
774
775   // Backdoor to redirect ZConfig from within the running
776   // TEST-application. HANDLE WITH CARE!
777   void reconfigureZConfig( const Pathname & override_r )
778   {
779     // ctor puts itself unter smart pointer control.
780     new ZConfig::Impl( override_r );
781   }
782
783   ///////////////////////////////////////////////////////////////////
784   //
785   //    METHOD NAME : ZConfig::instance
786   //    METHOD TYPE : ZConfig &
787   //
788   ZConfig & ZConfig::instance()
789   {
790     static ZConfig _instance; // The singleton
791     return _instance;
792   }
793
794   ///////////////////////////////////////////////////////////////////
795   //
796   //    METHOD NAME : ZConfig::ZConfig
797   //    METHOD TYPE : Ctor
798   //
799   ZConfig::ZConfig()
800   : _pimpl( new Impl )
801   {
802     about( MIL );
803   }
804
805   ///////////////////////////////////////////////////////////////////
806   //
807   //    METHOD NAME : ZConfig::~ZConfig
808   //    METHOD TYPE : Dtor
809   //
810   ZConfig::~ZConfig( )
811   {}
812
813   Pathname ZConfig::systemRoot() const
814   { return _autodetectSystemRoot(); }
815
816   ///////////////////////////////////////////////////////////////////
817   //
818   // system architecture
819   //
820   ///////////////////////////////////////////////////////////////////
821
822   Arch ZConfig::defaultSystemArchitecture()
823   {
824     static Arch _val( _autodetectSystemArchitecture() );
825     return _val;
826   }
827
828   Arch ZConfig::systemArchitecture() const
829   { return _pimpl->cfg_arch; }
830
831   void ZConfig::setSystemArchitecture( const Arch & arch_r )
832   {
833     if ( arch_r != _pimpl->cfg_arch )
834     {
835       WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
836       _pimpl->cfg_arch = arch_r;
837     }
838   }
839
840   ///////////////////////////////////////////////////////////////////
841   //
842   // text locale
843   //
844   ///////////////////////////////////////////////////////////////////
845
846   Locale ZConfig::defaultTextLocale()
847   {
848     static Locale _val( _autodetectTextLocale() );
849     return _val;
850   }
851
852   Locale ZConfig::textLocale() const
853   { return _pimpl->cfg_textLocale; }
854
855   void ZConfig::setTextLocale( const Locale & locale_r )
856   {
857     if ( locale_r != _pimpl->cfg_textLocale )
858     {
859       WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
860       _pimpl->cfg_textLocale = locale_r;
861 #warning prefer signal
862       sat::Pool::instance().setTextLocale( locale_r );
863     }
864   }
865
866   ///////////////////////////////////////////////////////////////////
867   // user data
868   ///////////////////////////////////////////////////////////////////
869
870   bool ZConfig::hasUserData() const
871   { return !_pimpl->userData.empty(); }
872
873   std::string ZConfig::userData() const
874   { return _pimpl->userData; }
875
876   bool ZConfig::setUserData( const std::string & str_r )
877   {
878     for_( ch, str_r.begin(), str_r.end() )
879     {
880       if ( *ch < ' ' && *ch != '\t' )
881       {
882         ERR << "New user data string rejectded: char " << (int)*ch << " at position " <<  (ch - str_r.begin()) << endl;
883         return false;
884       }
885     }
886     MIL << "Set user data string to '" << str_r << "'" << endl;
887     _pimpl->userData = str_r;
888     return true;
889   }
890
891   ///////////////////////////////////////////////////////////////////
892
893   Pathname ZConfig::repoCachePath() const
894   {
895     return ( _pimpl->cfg_cache_path.empty()
896         ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
897   }
898
899   Pathname ZConfig::repoMetadataPath() const
900   {
901     return ( _pimpl->cfg_metadata_path.empty()
902         ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
903   }
904
905   Pathname ZConfig::repoSolvfilesPath() const
906   {
907     return ( _pimpl->cfg_solvfiles_path.empty()
908         ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
909   }
910
911   Pathname ZConfig::repoPackagesPath() const
912   {
913     return ( _pimpl->cfg_packages_path.empty()
914         ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
915   }
916
917   ///////////////////////////////////////////////////////////////////
918
919   Pathname ZConfig::configPath() const
920   {
921     return ( _pimpl->cfg_config_path.empty()
922         ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
923   }
924
925   Pathname ZConfig::knownReposPath() const
926   {
927     return ( _pimpl->cfg_known_repos_path.empty()
928         ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
929   }
930
931   Pathname ZConfig::knownServicesPath() const
932   {
933     return ( _pimpl->cfg_known_services_path.empty()
934         ? (configPath()/"services.d") : _pimpl->cfg_known_services_path );
935   }
936
937   Pathname ZConfig::vendorPath() const
938   {
939     return ( _pimpl->cfg_vendor_path.empty()
940         ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
941   }
942
943   Pathname ZConfig::locksFile() const
944   {
945     return ( _pimpl->locks_file.empty()
946         ? (configPath()/"locks") : _pimpl->locks_file );
947   }
948
949   ///////////////////////////////////////////////////////////////////
950
951   bool ZConfig::repo_add_probe() const
952   { return _pimpl->repo_add_probe; }
953
954   unsigned ZConfig::repo_refresh_delay() const
955   { return _pimpl->repo_refresh_delay; }
956
957   LocaleSet ZConfig::repoRefreshLocales() const
958   { return _pimpl->repoRefreshLocales.empty() ? Target::requestedLocales("") :_pimpl->repoRefreshLocales; }
959
960   bool ZConfig::repoLabelIsAlias() const
961   { return _pimpl->repoLabelIsAlias; }
962
963   void ZConfig::repoLabelIsAlias( bool yesno_r )
964   { _pimpl->repoLabelIsAlias = yesno_r; }
965
966   bool ZConfig::download_use_deltarpm() const
967   { return _pimpl->download_use_deltarpm; }
968
969   bool ZConfig::download_use_deltarpm_always() const
970   { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
971
972   bool ZConfig::download_media_prefer_download() const
973   { return _pimpl->download_media_prefer_download; }
974
975   void ZConfig::set_download_media_prefer_download( bool yesno_r )
976   { _pimpl->download_media_prefer_download.set( yesno_r ); }
977
978   void ZConfig::set_default_download_media_prefer_download()
979   { _pimpl->download_media_prefer_download.restoreToDefault(); }
980
981   long ZConfig::download_max_concurrent_connections() const
982   { return _pimpl->download_max_concurrent_connections; }
983
984   long ZConfig::download_min_download_speed() const
985   { return _pimpl->download_min_download_speed; }
986
987   long ZConfig::download_max_download_speed() const
988   { return _pimpl->download_max_download_speed; }
989
990   long ZConfig::download_max_silent_tries() const
991   { return _pimpl->download_max_silent_tries; }
992
993   long ZConfig::download_transfer_timeout() const
994   { return _pimpl->download_transfer_timeout; }
995
996   Pathname ZConfig::download_mediaMountdir() const              { return _pimpl->download_mediaMountdir; }
997   void ZConfig::set_download_mediaMountdir( Pathname newval_r ) { _pimpl->download_mediaMountdir.set( std::move(newval_r) ); }
998   void ZConfig::set_default_download_mediaMountdir()            { _pimpl->download_mediaMountdir.restoreToDefault(); }
999
1000   DownloadMode ZConfig::commit_downloadMode() const
1001   { return _pimpl->commit_downloadMode; }
1002
1003   bool ZConfig::gpgCheck() const
1004   { return _pimpl->gpgCheck; }
1005
1006   TriBool ZConfig::repoGpgCheck() const
1007   { return _pimpl->repoGpgCheck; }
1008
1009   TriBool ZConfig::pkgGpgCheck() const
1010   { return _pimpl->pkgGpgCheck; }
1011
1012   bool ZConfig::solver_onlyRequires() const
1013   { return _pimpl->solver_onlyRequires; }
1014
1015   bool ZConfig::solver_allowVendorChange() const
1016   { return _pimpl->solver_allowVendorChange; }
1017
1018   bool ZConfig::solver_dupAllowDowngrade() const        { return _pimpl->solver_dupAllowDowngrade; }
1019   bool ZConfig::solver_dupAllowNameChange() const       { return _pimpl->solver_dupAllowNameChange; }
1020   bool ZConfig::solver_dupAllowArchChange() const       { return _pimpl->solver_dupAllowArchChange; }
1021   bool ZConfig::solver_dupAllowVendorChange() const     { return _pimpl->solver_dupAllowVendorChange; }
1022
1023   bool ZConfig::solver_cleandepsOnRemove() const
1024   { return _pimpl->solver_cleandepsOnRemove; }
1025
1026   Pathname ZConfig::solver_checkSystemFile() const
1027   { return ( _pimpl->solver_checkSystemFile.empty()
1028       ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
1029
1030   Pathname ZConfig::solver_checkSystemFileDir() const
1031   { return ( _pimpl->solver_checkSystemFileDir.empty()
1032       ? (configPath()/"systemCheck.d") : _pimpl->solver_checkSystemFileDir ); }
1033
1034   unsigned ZConfig::solver_upgradeTestcasesToKeep() const
1035   { return _pimpl->solver_upgradeTestcasesToKeep; }
1036
1037   bool ZConfig::solverUpgradeRemoveDroppedPackages() const              { return _pimpl->solverUpgradeRemoveDroppedPackages; }
1038   void ZConfig::setSolverUpgradeRemoveDroppedPackages( bool val_r )     { _pimpl->solverUpgradeRemoveDroppedPackages.set( val_r ); }
1039   void ZConfig::resetSolverUpgradeRemoveDroppedPackages()               { _pimpl->solverUpgradeRemoveDroppedPackages.restoreToDefault(); }
1040
1041   namespace
1042   {
1043     inline void sigMultiversionSpecChanged()
1044     {
1045       sat::detail::PoolMember::myPool().multiversionSpecChanged();
1046     }
1047   }
1048
1049   const std::set<std::string> & ZConfig::multiversionSpec() const       { return _pimpl->multiversion(); }
1050   void ZConfig::multiversionSpec( std::set<std::string> new_r )         { _pimpl->multiversion().swap( new_r );         sigMultiversionSpecChanged(); }
1051   void ZConfig::clearMultiversionSpec()                                 { _pimpl->multiversion().clear();               sigMultiversionSpecChanged(); }
1052   void ZConfig::addMultiversionSpec( const std::string & name_r )       { _pimpl->multiversion().insert( name_r );      sigMultiversionSpecChanged(); }
1053   void ZConfig::removeMultiversionSpec( const std::string & name_r )    { _pimpl->multiversion().erase( name_r );       sigMultiversionSpecChanged(); }
1054
1055   bool ZConfig::apply_locks_file() const
1056   { return _pimpl->apply_locks_file; }
1057
1058   Pathname ZConfig::update_dataPath() const
1059   {
1060     return ( _pimpl->update_data_path.empty()
1061         ? Pathname("/var/adm") : _pimpl->update_data_path );
1062   }
1063
1064   Pathname ZConfig::update_messagesPath() const
1065   {
1066     return ( _pimpl->update_messages_path.empty()
1067              ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
1068   }
1069
1070   Pathname ZConfig::update_scriptsPath() const
1071   {
1072     return ( _pimpl->update_scripts_path.empty()
1073              ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
1074   }
1075
1076   std::string ZConfig::updateMessagesNotify() const
1077   { return _pimpl->updateMessagesNotify; }
1078
1079   void ZConfig::setUpdateMessagesNotify( const std::string & val_r )
1080   { _pimpl->updateMessagesNotify.set( val_r ); }
1081
1082   void ZConfig::resetUpdateMessagesNotify()
1083   { _pimpl->updateMessagesNotify.restoreToDefault(); }
1084
1085   ///////////////////////////////////////////////////////////////////
1086
1087   target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
1088   { return _pimpl->rpmInstallFlags; }
1089
1090
1091   Pathname ZConfig::historyLogFile() const
1092   {
1093     return ( _pimpl->history_log_path.empty() ?
1094         Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
1095   }
1096
1097   Pathname ZConfig::credentialsGlobalDir() const
1098   {
1099     return ( _pimpl->credentials_global_dir_path.empty() ?
1100         Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
1101   }
1102
1103   Pathname ZConfig::credentialsGlobalFile() const
1104   {
1105     return ( _pimpl->credentials_global_file_path.empty() ?
1106         Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
1107   }
1108
1109   ///////////////////////////////////////////////////////////////////
1110
1111   std::string ZConfig::distroverpkg() const
1112   { return "redhat-release"; }
1113
1114   ///////////////////////////////////////////////////////////////////
1115
1116   Pathname ZConfig::pluginsPath() const
1117   { return _pimpl->pluginsPath.get(); }
1118
1119   ///////////////////////////////////////////////////////////////////
1120
1121   std::ostream & ZConfig::about( std::ostream & str ) const
1122   {
1123     str << "libzypp: " << VERSION << endl;
1124
1125     str << "libsolv: " << solv_version;
1126     if ( ::strcmp( solv_version, LIBSOLV_VERSION_STRING ) )
1127       str << " (built against " << LIBSOLV_VERSION_STRING << ")";
1128     str << endl;
1129
1130     str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
1131     str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
1132     str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
1133     return str;
1134   }
1135
1136   /////////////////////////////////////////////////////////////////
1137 } // namespace zypp
1138 ///////////////////////////////////////////////////////////////////