3bf1185afa6e4a127e0e4581aaf662ce4e899d9c
[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 std::endl;
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          ( "" )
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_focus                  ( ResolverFocus::Default )
330         , solver_onlyRequires           ( false )
331         , solver_allowVendorChange      ( false )
332         , solver_dupAllowDowngrade      ( true )
333         , solver_dupAllowNameChange     ( true )
334         , solver_dupAllowArchChange     ( true )
335         , solver_dupAllowVendorChange   ( true )
336         , solver_cleandepsOnRemove      ( false )
337         , solver_upgradeTestcasesToKeep ( 2 )
338         , solverUpgradeRemoveDroppedPackages( true )
339         , apply_locks_file              ( true )
340         , pluginsPath                   ( "/usr/lib/zypp/plugins" )
341       {
342         MIL << "libzypp: " LIBZYPP_VERSION_STRING << endl;
343         // override_r has higest prio
344         // ZYPP_CONF might override /etc/zypp/zypp.conf
345         if ( _parsedZyppConf.empty() )
346         {
347           _parsedZyppConf = _autodetectZyppConfPath();
348         }
349         else
350         {
351           // Inject this into ZConfig. Be shure this is
352           // allocated via new.
353           // ma: override_r might not be needed anymore since the
354           //     Vendor_test is now able to initialize VendorAttr directly.
355           INT << "Reconfigure to " << _parsedZyppConf << endl;
356           ZConfig::instance()._pimpl.reset( this );
357         }
358         if ( PathInfo(_parsedZyppConf).isExist() )
359         {
360           parser::IniDict dict( _parsedZyppConf );
361           for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
362                 sit != dict.sectionsEnd();
363                 ++sit )
364           {
365             std::string section(*sit);
366             //MIL << section << endl;
367             for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
368                   it != dict.entriesEnd(*sit);
369                   ++it )
370             {
371               std::string entry(it->first);
372               std::string value(it->second);
373               //DBG << (*it).first << "=" << (*it).second << endl;
374               if ( section == "main" )
375               {
376                 if ( entry == "arch" )
377                 {
378                   Arch carch( value );
379                   if ( carch != cfg_arch )
380                   {
381                     WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
382                     cfg_arch = carch;
383                   }
384                 }
385                 else if ( entry == "cachedir" )
386                 {
387                   cfg_cache_path = Pathname(value);
388                 }
389                 else if ( entry == "metadatadir" )
390                 {
391                   cfg_metadata_path = Pathname(value);
392                 }
393                 else if ( entry == "solvfilesdir" )
394                 {
395                   cfg_solvfiles_path = Pathname(value);
396                 }
397                 else if ( entry == "packagesdir" )
398                 {
399                   cfg_packages_path = Pathname(value);
400                 }
401                 else if ( entry == "configdir" )
402                 {
403                   cfg_config_path = Pathname(value);
404                 }
405                 else if ( entry == "reposdir" )
406                 {
407                   cfg_known_repos_path = Pathname(value);
408                 }
409                 else if ( entry == "servicesdir" )
410                 {
411                   cfg_known_services_path = Pathname(value);
412                 }
413                 else if ( entry == "varsdir" )
414                 {
415                   cfg_vars_path = Pathname(value);
416                 }
417                 else if ( entry == "repo.add.probe" )
418                 {
419                   repo_add_probe = str::strToBool( value, repo_add_probe );
420                 }
421                 else if ( entry == "repo.refresh.delay" )
422                 {
423                   str::strtonum(value, repo_refresh_delay);
424                 }
425                 else if ( entry == "repo.refresh.locales" )
426                 {
427                   std::vector<std::string> tmp;
428                   str::split( value, back_inserter( tmp ), ", \t" );
429
430                   boost::function<Locale(const std::string &)> transform(
431                     [](const std::string & str_r)->Locale{ return Locale(str_r); }
432                   );
433                   repoRefreshLocales.insert( make_transform_iterator( tmp.begin(), transform ),
434                                              make_transform_iterator( tmp.end(), transform ) );
435                 }
436                 else if ( entry == "download.use_deltarpm" )
437                 {
438                   download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
439                 }
440                 else if ( entry == "download.use_deltarpm.always" )
441                 {
442                   download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
443                 }
444                 else if ( entry == "download.media_preference" )
445                 {
446                   download_media_prefer_download.restoreToDefault( str::compareCI( value, "volatile" ) != 0 );
447                 }
448
449                 else if ( entry == "download.media_mountdir" )
450                 {
451                   download_mediaMountdir.restoreToDefault( Pathname(value) );
452                 }
453
454                 else if ( entry == "download.max_concurrent_connections" )
455                 {
456                   str::strtonum(value, download_max_concurrent_connections);
457                 }
458                 else if ( entry == "download.min_download_speed" )
459                 {
460                   str::strtonum(value, download_min_download_speed);
461                 }
462                 else if ( entry == "download.max_download_speed" )
463                 {
464                   str::strtonum(value, download_max_download_speed);
465                 }
466                 else if ( entry == "download.max_silent_tries" )
467                 {
468                   str::strtonum(value, download_max_silent_tries);
469                 }
470                 else if ( entry == "download.transfer_timeout" )
471                 {
472                   str::strtonum(value, download_transfer_timeout);
473                   if ( download_transfer_timeout < 0 )          download_transfer_timeout = 0;
474                   else if ( download_transfer_timeout > 3600 )  download_transfer_timeout = 3600;
475                 }
476                 else if ( entry == "commit.downloadMode" )
477                 {
478                   commit_downloadMode.set( deserializeDownloadMode( value ) );
479                 }
480                 else if ( entry == "gpgcheck" )
481                 {
482                   gpgCheck.restoreToDefault( str::strToBool( value, gpgCheck ) );
483                 }
484                 else if ( entry == "repo_gpgcheck" )
485                 {
486                   repoGpgCheck.restoreToDefault( str::strToTriBool( value ) );
487                 }
488                 else if ( entry == "pkg_gpgcheck" )
489                 {
490                   pkgGpgCheck.restoreToDefault( str::strToTriBool( value ) );
491                 }
492                 else if ( entry == "vendordir" )
493                 {
494                   cfg_vendor_path = Pathname(value);
495                 }
496                 else if ( entry == "multiversiondir" )
497                 {
498                   cfg_multiversion_path = Pathname(value);
499                 }
500                 else if ( entry == "multiversion.kernels" )
501                 {
502                   cfg_kernel_keep_spec = value;
503                 }
504                 else if ( entry == "solver.focus" )
505                 {
506                   fromString( value, solver_focus );
507                 }
508                 else if ( entry == "solver.onlyRequires" )
509                 {
510                   solver_onlyRequires.set( str::strToBool( value, solver_onlyRequires ) );
511                 }
512                 else if ( entry == "solver.allowVendorChange" )
513                 {
514                   solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange ) );
515                 }
516                 else if ( entry == "solver.dupAllowDowngrade" )
517                 {
518                   solver_dupAllowDowngrade.set( str::strToBool( value, solver_dupAllowDowngrade ) );
519                 }
520                 else if ( entry == "solver.dupAllowNameChange" )
521                 {
522                   solver_dupAllowNameChange.set( str::strToBool( value, solver_dupAllowNameChange ) );
523                 }
524                 else if ( entry == "solver.dupAllowArchChange" )
525                 {
526                   solver_dupAllowArchChange.set( str::strToBool( value, solver_dupAllowArchChange ) );
527                 }
528                 else if ( entry == "solver.dupAllowVendorChange" )
529                 {
530                   solver_dupAllowVendorChange.set( str::strToBool( value, solver_dupAllowVendorChange ) );
531                 }
532                 else if ( entry == "solver.cleandepsOnRemove" )
533                 {
534                   solver_cleandepsOnRemove.set( str::strToBool( value, solver_cleandepsOnRemove ) );
535                 }
536                 else if ( entry == "solver.upgradeTestcasesToKeep" )
537                 {
538                   solver_upgradeTestcasesToKeep.set( str::strtonum<unsigned>( value ) );
539                 }
540                 else if ( entry == "solver.upgradeRemoveDroppedPackages" )
541                 {
542                   solverUpgradeRemoveDroppedPackages.restoreToDefault( str::strToBool( value, solverUpgradeRemoveDroppedPackages.getDefault() ) );
543                 }
544                 else if ( entry == "solver.checkSystemFile" )
545                 {
546                   solver_checkSystemFile = Pathname(value);
547                 }
548                 else if ( entry == "solver.checkSystemFileDir" )
549                 {
550                   solver_checkSystemFileDir = Pathname(value);
551                 }
552                 else if ( entry == "multiversion" )
553                 {
554                   MultiversionSpec & defSpec( _multiversionMap.getDefaultSpec() );
555                   str::splitEscaped( value, std::inserter( defSpec, defSpec.end() ), ", \t" );
556                 }
557                 else if ( entry == "locksfile.path" )
558                 {
559                   locks_file = Pathname(value);
560                 }
561                 else if ( entry == "locksfile.apply" )
562                 {
563                   apply_locks_file = str::strToBool( value, apply_locks_file );
564                 }
565                 else if ( entry == "update.datadir" )
566                 {
567                   update_data_path = Pathname(value);
568                 }
569                 else if ( entry == "update.scriptsdir" )
570                 {
571                   update_scripts_path = Pathname(value);
572                 }
573                 else if ( entry == "update.messagessdir" )
574                 {
575                   update_messages_path = Pathname(value);
576                 }
577                 else if ( entry == "update.messages.notify" )
578                 {
579                   updateMessagesNotify.set( value );
580                 }
581                 else if ( entry == "rpm.install.excludedocs" )
582                 {
583                   rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS,
584                                            str::strToBool( value, false ) );
585                 }
586                 else if ( entry == "history.logfile" )
587                 {
588                   history_log_path = Pathname(value);
589                 }
590                 else if ( entry == "credentials.global.dir" )
591                 {
592                   credentials_global_dir_path = Pathname(value);
593                 }
594                 else if ( entry == "credentials.global.file" )
595                 {
596                   credentials_global_file_path = Pathname(value);
597                 }
598               }
599             }
600           }
601           //
602
603         }
604         else
605         {
606           MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
607           _parsedZyppConf = _parsedZyppConf.extend( " (NOT FOUND)" );
608         }
609
610         // legacy:
611         if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
612         {
613           Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
614           if ( carch != cfg_arch )
615           {
616             WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
617             cfg_arch = carch;
618           }
619         }
620         MIL << "ZConfig singleton created." << endl;
621       }
622
623       ~Impl()
624       {}
625
626     public:
627     /** Remember any parsed zypp.conf. */
628     Pathname _parsedZyppConf;
629
630     Arch     cfg_arch;
631     Locale   cfg_textLocale;
632
633     Pathname cfg_cache_path;
634     Pathname cfg_metadata_path;
635     Pathname cfg_solvfiles_path;
636     Pathname cfg_packages_path;
637
638     Pathname cfg_config_path;
639     Pathname cfg_known_repos_path;
640     Pathname cfg_known_services_path;
641     Pathname cfg_vars_path;
642     Pathname cfg_repo_mgr_root_path;
643
644     Pathname cfg_vendor_path;
645     Pathname cfg_multiversion_path;
646     std::string cfg_kernel_keep_spec;
647     Pathname locks_file;
648
649     Pathname update_data_path;
650     Pathname update_scripts_path;
651     Pathname update_messages_path;
652     DefaultOption<std::string> updateMessagesNotify;
653
654     bool        repo_add_probe;
655     unsigned    repo_refresh_delay;
656     LocaleSet   repoRefreshLocales;
657     bool        repoLabelIsAlias;
658
659     bool download_use_deltarpm;
660     bool download_use_deltarpm_always;
661     DefaultOption<bool> download_media_prefer_download;
662     DefaultOption<Pathname> download_mediaMountdir;
663
664     int download_max_concurrent_connections;
665     int download_min_download_speed;
666     int download_max_download_speed;
667     int download_max_silent_tries;
668     int download_transfer_timeout;
669
670     Option<DownloadMode> commit_downloadMode;
671
672     DefaultOption<bool>         gpgCheck;
673     DefaultOption<TriBool>      repoGpgCheck;
674     DefaultOption<TriBool>      pkgGpgCheck;
675
676     ResolverFocus       solver_focus;
677     Option<bool>        solver_onlyRequires;
678     Option<bool>        solver_allowVendorChange;
679     Option<bool>        solver_dupAllowDowngrade;
680     Option<bool>        solver_dupAllowNameChange;
681     Option<bool>        solver_dupAllowArchChange;
682     Option<bool>        solver_dupAllowVendorChange;
683     Option<bool>        solver_cleandepsOnRemove;
684     Option<unsigned>    solver_upgradeTestcasesToKeep;
685     DefaultOption<bool> solverUpgradeRemoveDroppedPackages;
686
687     Pathname solver_checkSystemFile;
688     Pathname solver_checkSystemFileDir;
689
690     MultiversionSpec &          multiversion()          { return getMultiversion(); }
691     const MultiversionSpec &    multiversion() const    { return getMultiversion(); }
692
693     bool apply_locks_file;
694
695     target::rpm::RpmInstFlags rpmInstallFlags;
696
697     Pathname history_log_path;
698     Pathname credentials_global_dir_path;
699     Pathname credentials_global_file_path;
700
701     std::string userData;
702
703     Option<Pathname> pluginsPath;
704
705   private:
706     // HACK for bnc#906096: let pool re-evaluate multiversion spec
707     // if target root changes. ZConfig returns data sensitive to
708     // current target root.
709     // TODO Actually we'd need to scan the target systems zypp.conf and
710     // overlay all system specific values.
711     struct MultiversionMap
712     {
713       typedef std::map<Pathname,MultiversionSpec> SpecMap;
714
715       MultiversionSpec & getSpec( Pathname root_r, const Impl & zConfImpl_r )   // from system at root
716       {
717         // _specMap[]     - the plain zypp.conf value
718         // _specMap[/]    - combine [] and multiversion.d scan
719         // _specMap[root] - scan root/zypp.conf and root/multiversion.d
720
721         if ( root_r.empty() )
722           root_r = "/";
723         bool cacheHit = _specMap.count( root_r );
724         MultiversionSpec & ret( _specMap[root_r] );     // creates new entry on the fly
725
726         if ( ! cacheHit )
727         {
728           if ( root_r == "/" )
729             ret.swap( _specMap[Pathname()] );           // original zypp.conf
730           else
731             scanConfAt( root_r, ret, zConfImpl_r );     // scan zypp.conf at root_r
732           scanDirAt( root_r, ret, zConfImpl_r );        // add multiversion.d at root_r
733           using zypp::operator<<;
734           MIL << "MultiversionSpec '" << root_r << "' = " << ret << endl;
735         }
736         return ret;
737       }
738
739       MultiversionSpec & getDefaultSpec()       // Spec from zypp.conf parsing; called before any getSpec
740       { return _specMap[Pathname()]; }
741
742     private:
743       void scanConfAt( const Pathname root_r, MultiversionSpec & spec_r, const Impl & zConfImpl_r )
744       {
745         static const str::regex rx( "^multiversion *= *(.*)" );
746         str::smatch what;
747         iostr::simpleParseFile( InputStream( Pathname::assertprefix( root_r, _autodetectZyppConfPath() ) ),
748                                 [&]( int num_r, std::string line_r )->bool
749                                 {
750                                   if ( line_r[0] == 'm' && str::regex_match( line_r, what, rx ) )
751                                   {
752                                     str::splitEscaped( what[1], std::inserter( spec_r, spec_r.end() ), ", \t" );
753                                     return false;       // stop after match
754                                   }
755                                   return true;
756                                 } );
757       }
758
759       void scanDirAt( const Pathname root_r, MultiversionSpec & spec_r, const Impl & zConfImpl_r )
760       {
761         // NOTE:  Actually we'd need to scan and use the root_r! zypp.conf values.
762         Pathname multiversionDir( zConfImpl_r.cfg_multiversion_path );
763         if ( multiversionDir.empty() )
764           multiversionDir = ( zConfImpl_r.cfg_config_path.empty()
765                             ? Pathname("/etc/zypp")
766                             : zConfImpl_r.cfg_config_path ) / "multiversion.d";
767
768         filesystem::dirForEach( Pathname::assertprefix( root_r, multiversionDir ),
769                                 [&spec_r]( const Pathname & dir_r, const char *const & name_r )->bool
770                                 {
771                                   MIL << "Parsing " << dir_r/name_r << endl;
772                                   iostr::simpleParseFile( InputStream( dir_r/name_r ),
773                                                           [&spec_r]( int num_r, std::string line_r )->bool
774                                                           {
775                                                             DBG << "  found " << line_r << endl;
776                                                             spec_r.insert( std::move(line_r) );
777                                                             return true;
778                                                           } );
779                                   return true;
780                                 } );
781       }
782
783     private:
784       SpecMap _specMap;
785     };
786
787     MultiversionSpec & getMultiversion() const
788     { return _multiversionMap.getSpec( _autodetectSystemRoot(), *this ); }
789
790     mutable MultiversionMap _multiversionMap;
791   };
792   ///////////////////////////////////////////////////////////////////
793
794   ///////////////////////////////////////////////////////////////////
795   //
796   //    METHOD NAME : ZConfig::instance
797   //    METHOD TYPE : ZConfig &
798   //
799   ZConfig & ZConfig::instance()
800   {
801     static ZConfig _instance; // The singleton
802     return _instance;
803   }
804
805   ///////////////////////////////////////////////////////////////////
806   //
807   //    METHOD NAME : ZConfig::ZConfig
808   //    METHOD TYPE : Ctor
809   //
810   ZConfig::ZConfig()
811   : _pimpl( new Impl )
812   {
813     about( MIL );
814   }
815
816   ///////////////////////////////////////////////////////////////////
817   //
818   //    METHOD NAME : ZConfig::~ZConfig
819   //    METHOD TYPE : Dtor
820   //
821   ZConfig::~ZConfig( )
822   {}
823
824   Pathname ZConfig::systemRoot() const
825   { return _autodetectSystemRoot(); }
826
827
828   Pathname ZConfig::repoManagerRoot() const
829   {
830     return ( _pimpl->cfg_repo_mgr_root_path.empty()
831              ? systemRoot() : _pimpl->cfg_repo_mgr_root_path );
832   }
833
834   void ZConfig::setRepoManagerRoot(const zypp::filesystem::Pathname &root)
835   { _pimpl->cfg_repo_mgr_root_path = root; }
836
837   ///////////////////////////////////////////////////////////////////
838   //
839   // system architecture
840   //
841   ///////////////////////////////////////////////////////////////////
842
843   Arch ZConfig::defaultSystemArchitecture()
844   {
845     static Arch _val( _autodetectSystemArchitecture() );
846     return _val;
847   }
848
849   Arch ZConfig::systemArchitecture() const
850   { return _pimpl->cfg_arch; }
851
852   void ZConfig::setSystemArchitecture( const Arch & arch_r )
853   {
854     if ( arch_r != _pimpl->cfg_arch )
855     {
856       WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
857       _pimpl->cfg_arch = arch_r;
858     }
859   }
860
861   ///////////////////////////////////////////////////////////////////
862   //
863   // text locale
864   //
865   ///////////////////////////////////////////////////////////////////
866
867   Locale ZConfig::defaultTextLocale()
868   {
869     static Locale _val( _autodetectTextLocale() );
870     return _val;
871   }
872
873   Locale ZConfig::textLocale() const
874   { return _pimpl->cfg_textLocale; }
875
876   void ZConfig::setTextLocale( const Locale & locale_r )
877   {
878     if ( locale_r != _pimpl->cfg_textLocale )
879     {
880       WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
881       _pimpl->cfg_textLocale = locale_r;
882 #warning prefer signal
883       sat::Pool::instance().setTextLocale( locale_r );
884     }
885   }
886
887   ///////////////////////////////////////////////////////////////////
888   // user data
889   ///////////////////////////////////////////////////////////////////
890
891   bool ZConfig::hasUserData() const
892   { return !_pimpl->userData.empty(); }
893
894   std::string ZConfig::userData() const
895   { return _pimpl->userData; }
896
897   bool ZConfig::setUserData( const std::string & str_r )
898   {
899     for_( ch, str_r.begin(), str_r.end() )
900     {
901       if ( *ch < ' ' && *ch != '\t' )
902       {
903         ERR << "New user data string rejectded: char " << (int)*ch << " at position " <<  (ch - str_r.begin()) << endl;
904         return false;
905       }
906     }
907     MIL << "Set user data string to '" << str_r << "'" << endl;
908     _pimpl->userData = str_r;
909     return true;
910   }
911
912   ///////////////////////////////////////////////////////////////////
913
914   Pathname ZConfig::repoCachePath() const
915   {
916     return ( _pimpl->cfg_cache_path.empty()
917              ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
918   }
919
920   Pathname ZConfig::pubkeyCachePath() const
921   {
922     return ( _pimpl->cfg_cache_path.empty()
923              ? Pathname("/var/cache/zypp/pubkeys") : _pimpl->cfg_cache_path/"pubkeys" );
924   }
925
926   void ZConfig::setRepoCachePath(const zypp::filesystem::Pathname &path_r)
927   {
928     _pimpl->cfg_cache_path = path_r;
929   }
930
931   Pathname ZConfig::repoMetadataPath() const
932   {
933     return ( _pimpl->cfg_metadata_path.empty()
934         ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
935   }
936
937   void ZConfig::setRepoMetadataPath(const zypp::filesystem::Pathname &path_r)
938   {
939     _pimpl->cfg_metadata_path = path_r;
940   }
941
942   Pathname ZConfig::repoSolvfilesPath() const
943   {
944     return ( _pimpl->cfg_solvfiles_path.empty()
945         ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
946   }
947
948   void ZConfig::setRepoSolvfilesPath(const zypp::filesystem::Pathname &path_r)
949   {
950     _pimpl->cfg_solvfiles_path = path_r;
951   }
952
953   Pathname ZConfig::repoPackagesPath() const
954   {
955     return ( _pimpl->cfg_packages_path.empty()
956         ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
957   }
958
959   void ZConfig::setRepoPackagesPath(const zypp::filesystem::Pathname &path_r)
960   {
961     _pimpl->cfg_packages_path = path_r;
962   }
963
964   ///////////////////////////////////////////////////////////////////
965
966   Pathname ZConfig::configPath() const
967   {
968     return ( _pimpl->cfg_config_path.empty()
969         ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
970   }
971
972   Pathname ZConfig::knownReposPath() const
973   {
974     return ( _pimpl->cfg_known_repos_path.empty()
975         ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
976   }
977
978   Pathname ZConfig::knownServicesPath() const
979   {
980     return ( _pimpl->cfg_known_services_path.empty()
981         ? (configPath()/"services.d") : _pimpl->cfg_known_services_path );
982   }
983
984   Pathname ZConfig::needrebootFile() const
985   { return configPath()/"needreboot"; }
986
987   Pathname ZConfig::needrebootPath() const
988   { return configPath()/"needreboot.d"; }
989
990   Pathname ZConfig::varsPath() const
991   {
992     return ( _pimpl->cfg_vars_path.empty()
993         ? (configPath()/"vars.d") : _pimpl->cfg_vars_path );
994   }
995
996   Pathname ZConfig::vendorPath() const
997   {
998     return ( _pimpl->cfg_vendor_path.empty()
999         ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
1000   }
1001
1002   Pathname ZConfig::locksFile() const
1003   {
1004     return ( _pimpl->locks_file.empty()
1005         ? (configPath()/"locks") : _pimpl->locks_file );
1006   }
1007
1008   ///////////////////////////////////////////////////////////////////
1009
1010   bool ZConfig::repo_add_probe() const
1011   { return _pimpl->repo_add_probe; }
1012
1013   unsigned ZConfig::repo_refresh_delay() const
1014   { return _pimpl->repo_refresh_delay; }
1015
1016   LocaleSet ZConfig::repoRefreshLocales() const
1017   { return _pimpl->repoRefreshLocales.empty() ? Target::requestedLocales("") :_pimpl->repoRefreshLocales; }
1018
1019   bool ZConfig::repoLabelIsAlias() const
1020   { return _pimpl->repoLabelIsAlias; }
1021
1022   void ZConfig::repoLabelIsAlias( bool yesno_r )
1023   { _pimpl->repoLabelIsAlias = yesno_r; }
1024
1025   bool ZConfig::download_use_deltarpm() const
1026   { return _pimpl->download_use_deltarpm; }
1027
1028   bool ZConfig::download_use_deltarpm_always() const
1029   { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
1030
1031   bool ZConfig::download_media_prefer_download() const
1032   { return _pimpl->download_media_prefer_download; }
1033
1034   void ZConfig::set_download_media_prefer_download( bool yesno_r )
1035   { _pimpl->download_media_prefer_download.set( yesno_r ); }
1036
1037   void ZConfig::set_default_download_media_prefer_download()
1038   { _pimpl->download_media_prefer_download.restoreToDefault(); }
1039
1040   long ZConfig::download_max_concurrent_connections() const
1041   { return _pimpl->download_max_concurrent_connections; }
1042
1043   long ZConfig::download_min_download_speed() const
1044   { return _pimpl->download_min_download_speed; }
1045
1046   long ZConfig::download_max_download_speed() const
1047   { return _pimpl->download_max_download_speed; }
1048
1049   long ZConfig::download_max_silent_tries() const
1050   { return _pimpl->download_max_silent_tries; }
1051
1052   long ZConfig::download_transfer_timeout() const
1053   { return _pimpl->download_transfer_timeout; }
1054
1055   Pathname ZConfig::download_mediaMountdir() const              { return _pimpl->download_mediaMountdir; }
1056   void ZConfig::set_download_mediaMountdir( Pathname newval_r ) { _pimpl->download_mediaMountdir.set( std::move(newval_r) ); }
1057   void ZConfig::set_default_download_mediaMountdir()            { _pimpl->download_mediaMountdir.restoreToDefault(); }
1058
1059   DownloadMode ZConfig::commit_downloadMode() const
1060   { return _pimpl->commit_downloadMode; }
1061
1062
1063   bool ZConfig::gpgCheck() const                        { return _pimpl->gpgCheck; }
1064   TriBool ZConfig::repoGpgCheck() const                 { return _pimpl->repoGpgCheck; }
1065   TriBool ZConfig::pkgGpgCheck() const                  { return _pimpl->pkgGpgCheck; }
1066
1067   void ZConfig::setGpgCheck( bool val_r )               { _pimpl->gpgCheck.set( val_r ); }
1068   void ZConfig::setRepoGpgCheck( TriBool val_r )        { _pimpl->repoGpgCheck.set( val_r ); }
1069   void ZConfig::setPkgGpgCheck( TriBool val_r )         { _pimpl->pkgGpgCheck.set( val_r ); }
1070
1071   void ZConfig::resetGpgCheck()                         { _pimpl->gpgCheck.restoreToDefault(); }
1072   void ZConfig::resetRepoGpgCheck()                     { _pimpl->repoGpgCheck.restoreToDefault(); }
1073   void ZConfig::resetPkgGpgCheck()                      { _pimpl->pkgGpgCheck.restoreToDefault(); }
1074
1075   ResolverFocus ZConfig::solver_focus() const           { return _pimpl->solver_focus; }
1076
1077   bool ZConfig::solver_onlyRequires() const
1078   { return _pimpl->solver_onlyRequires; }
1079
1080   bool ZConfig::solver_allowVendorChange() const
1081   { return _pimpl->solver_allowVendorChange; }
1082
1083   bool ZConfig::solver_dupAllowDowngrade() const        { return _pimpl->solver_dupAllowDowngrade; }
1084   bool ZConfig::solver_dupAllowNameChange() const       { return _pimpl->solver_dupAllowNameChange; }
1085   bool ZConfig::solver_dupAllowArchChange() const       { return _pimpl->solver_dupAllowArchChange; }
1086   bool ZConfig::solver_dupAllowVendorChange() const     { return _pimpl->solver_dupAllowVendorChange; }
1087
1088   bool ZConfig::solver_cleandepsOnRemove() const
1089   { return _pimpl->solver_cleandepsOnRemove; }
1090
1091   Pathname ZConfig::solver_checkSystemFile() const
1092   { return ( _pimpl->solver_checkSystemFile.empty()
1093       ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
1094
1095   Pathname ZConfig::solver_checkSystemFileDir() const
1096   { return ( _pimpl->solver_checkSystemFileDir.empty()
1097       ? (configPath()/"systemCheck.d") : _pimpl->solver_checkSystemFileDir ); }
1098
1099   unsigned ZConfig::solver_upgradeTestcasesToKeep() const
1100   { return _pimpl->solver_upgradeTestcasesToKeep; }
1101
1102   bool ZConfig::solverUpgradeRemoveDroppedPackages() const              { return _pimpl->solverUpgradeRemoveDroppedPackages; }
1103   void ZConfig::setSolverUpgradeRemoveDroppedPackages( bool val_r )     { _pimpl->solverUpgradeRemoveDroppedPackages.set( val_r ); }
1104   void ZConfig::resetSolverUpgradeRemoveDroppedPackages()               { _pimpl->solverUpgradeRemoveDroppedPackages.restoreToDefault(); }
1105
1106   namespace
1107   {
1108     inline void sigMultiversionSpecChanged()
1109     {
1110       sat::detail::PoolMember::myPool().multiversionSpecChanged();
1111     }
1112   }
1113
1114   const std::set<std::string> & ZConfig::multiversionSpec() const       { return _pimpl->multiversion(); }
1115   void ZConfig::multiversionSpec( std::set<std::string> new_r )         { _pimpl->multiversion().swap( new_r );         sigMultiversionSpecChanged(); }
1116   void ZConfig::clearMultiversionSpec()                                 { _pimpl->multiversion().clear();               sigMultiversionSpecChanged(); }
1117   void ZConfig::addMultiversionSpec( const std::string & name_r )       { _pimpl->multiversion().insert( name_r );      sigMultiversionSpecChanged(); }
1118   void ZConfig::removeMultiversionSpec( const std::string & name_r )    { _pimpl->multiversion().erase( name_r );       sigMultiversionSpecChanged(); }
1119
1120   bool ZConfig::apply_locks_file() const
1121   { return _pimpl->apply_locks_file; }
1122
1123   Pathname ZConfig::update_dataPath() const
1124   {
1125     return ( _pimpl->update_data_path.empty()
1126         ? Pathname("/var/adm") : _pimpl->update_data_path );
1127   }
1128
1129   Pathname ZConfig::update_messagesPath() const
1130   {
1131     return ( _pimpl->update_messages_path.empty()
1132              ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
1133   }
1134
1135   Pathname ZConfig::update_scriptsPath() const
1136   {
1137     return ( _pimpl->update_scripts_path.empty()
1138              ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
1139   }
1140
1141   std::string ZConfig::updateMessagesNotify() const
1142   { return _pimpl->updateMessagesNotify; }
1143
1144   void ZConfig::setUpdateMessagesNotify( const std::string & val_r )
1145   { _pimpl->updateMessagesNotify.set( val_r ); }
1146
1147   void ZConfig::resetUpdateMessagesNotify()
1148   { _pimpl->updateMessagesNotify.restoreToDefault(); }
1149
1150   ///////////////////////////////////////////////////////////////////
1151
1152   target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
1153   { return _pimpl->rpmInstallFlags; }
1154
1155
1156   Pathname ZConfig::historyLogFile() const
1157   {
1158     return ( _pimpl->history_log_path.empty() ?
1159         Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
1160   }
1161
1162   Pathname ZConfig::credentialsGlobalDir() const
1163   {
1164     return ( _pimpl->credentials_global_dir_path.empty() ?
1165         Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
1166   }
1167
1168   Pathname ZConfig::credentialsGlobalFile() const
1169   {
1170     return ( _pimpl->credentials_global_file_path.empty() ?
1171         Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
1172   }
1173
1174   ///////////////////////////////////////////////////////////////////
1175
1176   std::string ZConfig::distroverpkg() const
1177   { return "system-release"; }
1178
1179   ///////////////////////////////////////////////////////////////////
1180
1181   Pathname ZConfig::pluginsPath() const
1182   { return _pimpl->pluginsPath.get(); }
1183
1184   std::string ZConfig::multiversionKernels() const
1185   {
1186     return _pimpl->cfg_kernel_keep_spec;
1187   }
1188
1189   ///////////////////////////////////////////////////////////////////
1190
1191   std::ostream & ZConfig::about( std::ostream & str ) const
1192   {
1193     str << "libzypp: " LIBZYPP_VERSION_STRING << endl;
1194
1195     str << "libsolv: " << solv_version;
1196     if ( ::strcmp( solv_version, LIBSOLV_VERSION_STRING ) )
1197       str << " (built against " << LIBSOLV_VERSION_STRING << ")";
1198     str << endl;
1199
1200     str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
1201     str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
1202     str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
1203     return str;
1204   }
1205
1206   /////////////////////////////////////////////////////////////////
1207 } // namespace zypp
1208 ///////////////////////////////////////////////////////////////////