1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ZConfig.cc
14 #include <sys/utsname.h>
16 #include <satsolver/satversion.h>
20 #include "zypp/base/Logger.h"
21 #include "zypp/base/IOStream.h"
22 #include "zypp/base/InputStream.h"
23 #include "zypp/base/String.h"
25 #include "zypp/ZConfig.h"
26 #include "zypp/ZYppFactory.h"
27 #include "zypp/PathInfo.h"
28 #include "zypp/parser/IniDict.h"
30 #include "zypp/sat/Pool.h"
33 using namespace zypp::filesystem;
34 using namespace zypp::parser;
36 #undef ZYPP_BASE_LOGGER_LOGGROUP
37 #define ZYPP_BASE_LOGGER_LOGGROUP "zconfig"
39 ///////////////////////////////////////////////////////////////////
41 { /////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////
45 { /////////////////////////////////////////////////////////////////
47 /** Determine system architecture evaluating \c uname and \c /proc/cpuinfo.
49 Arch _autodetectSystemArchitecture()
52 if ( ::uname( &buf ) < 0 )
54 ERR << "Can't determine system architecture" << endl;
58 Arch architecture( buf.machine );
59 MIL << "Uname architecture is '" << buf.machine << "'" << endl;
61 // some CPUs report i686 but dont implement cx8 and cmov
62 // check for both flags in /proc/cpuinfo and downgrade
63 // to i586 if either is missing (cf bug #18885)
64 if ( architecture == Arch_i686 )
66 std::ifstream cpuinfo( "/proc/cpuinfo" );
69 for( iostr::EachLine in( cpuinfo ); in; in.next() )
71 if ( str::hasPrefix( *in, "flags" ) )
73 if ( in->find( "cx8" ) == std::string::npos
74 || in->find( "cmov" ) == std::string::npos )
76 architecture = Arch_i586;
77 WAR << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
85 ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
91 /** The locale to be used for texts and messages.
93 * For the encoding to be used the preference is
95 * LC_ALL, LC_CTYPE, LANG
97 * For the language of the messages to be used, the preference is
99 * LANGUAGE, LC_ALL, LC_MESSAGES, LANG
101 * Note that LANGUAGE can contain more than one locale name, it can be
102 * a list of locale names like for example
104 * LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
106 * \todo Support dynamic fallbacklists defined by LANGUAGE
108 Locale _autodetectTextLocale()
111 const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
112 for ( const char ** envvar = envlist; *envvar; ++envvar )
114 const char * envlang = getenv( *envvar );
117 std::string envstr( envlang );
118 if ( envstr != "POSIX" && envstr != "C" )
120 Locale lang( envstr );
121 if ( ! lang.code().empty() )
123 MIL << "Found " << *envvar << "=" << envstr << endl;
130 MIL << "Default text locale is '" << ret << "'" << endl;
131 #warning HACK AROUND BOOST_TEST_CATCH_SYSTEM_ERRORS
132 setenv( "BOOST_TEST_CATCH_SYSTEM_ERRORS", "no", 1 );
136 /////////////////////////////////////////////////////////////////
138 ///////////////////////////////////////////////////////////////////
140 /** Mutable option. */
144 typedef _Tp value_type;
146 /** No default ctor, explicit initialisation! */
147 Option( const value_type & initial_r )
151 /** Get the value. */
152 const value_type & get() const
155 /** Autoconversion to value_type. */
156 operator const value_type &() const
159 /** Set a new value. */
160 void set( const value_type & newval_r )
163 /** Non-const reference to set a new value. */
171 /** Mutable option with initial value also remembering a config value. */
173 struct DefaultOption : public Option<_Tp>
175 typedef _Tp value_type;
176 typedef Option<_Tp> option_type;
178 DefaultOption( const value_type & initial_r )
179 : Option<_Tp>( initial_r ), _default( initial_r )
182 /** Reset value to the current default. */
183 void restoreToDefault()
184 { this->set( _default.get() ); }
186 /** Reset value to a new default. */
187 void restoreToDefault( const value_type & newval_r )
188 { setDefault( newval_r ); restoreToDefault(); }
190 /** Get the current default value. */
191 const value_type & getDefault() const
192 { return _default.get(); }
194 /** Set a new default value. */
195 void setDefault( const value_type & newval_r )
196 { _default.set( newval_r ); }
199 option_type _default;
202 ///////////////////////////////////////////////////////////////////
204 // CLASS NAME : ZConfig::Impl
206 /** ZConfig implementation.
207 * \todo Enrich section and entry definition by some comment
208 * (including the default setting and provide some method to
209 * write this into a sample zypp.conf.
214 Impl( const Pathname & override_r = Pathname() )
215 : _parsedZyppConf ( override_r )
216 , cfg_arch ( defaultSystemArchitecture() )
217 , cfg_textLocale ( defaultTextLocale() )
218 , updateMessagesNotify ( "single | /usr/lib/zypp/notify-message -p %p" )
219 , repo_add_probe ( false )
220 , repo_refresh_delay ( 10 )
221 , repoLabelIsAlias ( false )
222 , download_use_deltarpm ( true )
223 , download_use_deltarpm_always ( false )
224 , download_media_prefer_download( true )
225 , download_max_concurrent_connections( 2 )
226 , download_min_download_speed ( 0 )
227 , download_max_download_speed ( 0 )
228 , download_max_silent_tries ( 5 )
229 , commit_downloadMode ( DownloadDefault )
230 , solver_onlyRequires ( false )
231 , solver_allowVendorChange ( false )
232 , solver_upgradeTestcasesToKeep ( 2 )
233 , solverUpgradeRemoveDroppedPackages( true )
234 , apply_locks_file ( true )
237 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
238 // override_r has higest prio
239 // ZYPP_CONF might override /etc/zypp/zypp.conf
240 if ( _parsedZyppConf.empty() )
242 const char *env_confpath = getenv( "ZYPP_CONF" );
243 _parsedZyppConf = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
247 // Inject this into ZConfig. Be shure this is
248 // allocated via new. See: reconfigureZConfig
249 INT << "Reconfigure to " << _parsedZyppConf << endl;
250 ZConfig::instance()._pimpl.reset( this );
252 if ( PathInfo(_parsedZyppConf).isExist() )
254 parser::IniDict dict( _parsedZyppConf );
255 for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
256 sit != dict.sectionsEnd();
259 string section(*sit);
260 //MIL << section << endl;
261 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
262 it != dict.entriesEnd(*sit);
265 string entry(it->first);
266 string value(it->second);
267 //DBG << (*it).first << "=" << (*it).second << endl;
268 if ( section == "main" )
270 if ( entry == "arch" )
273 if ( carch != cfg_arch )
275 WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
279 else if ( entry == "cachedir" )
281 cfg_cache_path = Pathname(value);
283 else if ( entry == "metadatadir" )
285 cfg_metadata_path = Pathname(value);
287 else if ( entry == "solvfilesdir" )
289 cfg_solvfiles_path = Pathname(value);
291 else if ( entry == "packagesdir" )
293 cfg_packages_path = Pathname(value);
295 else if ( entry == "configdir" )
297 cfg_config_path = Pathname(value);
299 else if ( entry == "reposdir" )
301 cfg_known_repos_path = Pathname(value);
303 else if ( entry == "servicesdir" )
305 cfg_known_services_path = Pathname(value);
307 else if ( entry == "repo.add.probe" )
309 repo_add_probe = str::strToBool( value, repo_add_probe );
311 else if ( entry == "repo.refresh.delay" )
313 str::strtonum(value, repo_refresh_delay);
315 else if ( entry == "download.use_deltarpm" )
317 download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
319 else if ( entry == "download.use_deltarpm.always" )
321 download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
323 else if ( entry == "download.media_preference" )
325 download_media_prefer_download.restoreToDefault( str::compareCI( value, "volatile" ) != 0 );
327 else if ( entry == "download.max_concurrent_connections" )
329 str::strtonum(value, download_max_concurrent_connections);
331 else if ( entry == "download.min_download_speed" )
333 str::strtonum(value, download_min_download_speed);
335 else if ( entry == "download.max_download_speed" )
337 str::strtonum(value, download_max_download_speed);
339 else if ( entry == "download.max_silent_tries" )
341 str::strtonum(value, download_max_silent_tries);
343 else if ( entry == "commit.downloadMode" )
345 commit_downloadMode.set( deserializeDownloadMode( value ) );
347 else if ( entry == "vendordir" )
349 cfg_vendor_path = Pathname(value);
351 else if ( entry == "solver.onlyRequires" )
353 solver_onlyRequires.set( str::strToBool( value, solver_onlyRequires ) );
355 else if ( entry == "solver.allowVendorChange" )
357 solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange ) );
359 else if ( entry == "solver.upgradeTestcasesToKeep" )
361 solver_upgradeTestcasesToKeep.set( str::strtonum<unsigned>( value ) );
363 else if ( entry == "solver.upgradeRemoveDroppedPackages" )
365 solverUpgradeRemoveDroppedPackages.restoreToDefault( str::strToBool( value, solverUpgradeRemoveDroppedPackages.getDefault() ) );
367 else if ( entry == "solver.checkSystemFile" )
369 solver_checkSystemFile = Pathname(value);
371 else if ( entry == "multiversion" )
373 str::split( value, inserter( multiversion, multiversion.end() ), ", \t" );
375 else if ( entry == "locksfile.path" )
377 locks_file = Pathname(value);
379 else if ( entry == "locksfile.apply" )
381 apply_locks_file = str::strToBool( value, apply_locks_file );
383 else if ( entry == "update.datadir" )
385 update_data_path = Pathname(value);
387 else if ( entry == "update.scriptsdir" )
389 update_scripts_path = Pathname(value);
391 else if ( entry == "update.messagessdir" )
393 update_messages_path = Pathname(value);
395 else if ( entry == "update.messages.notify" )
397 updateMessagesNotify.set( value );
399 else if ( entry == "rpm.install.excludedocs" )
401 rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS,
402 str::strToBool( value, false ) );
404 else if ( entry == "history.logfile" )
406 history_log_path = Pathname(value);
408 else if ( entry == "credentials.global.dir" )
410 credentials_global_dir_path = Pathname(value);
412 else if ( entry == "credentials.global.file" )
414 credentials_global_file_path = Pathname(value);
422 MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
423 _parsedZyppConf = _parsedZyppConf.extend( " (NOT FOUND)" );
427 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
429 Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
430 if ( carch != cfg_arch )
432 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
436 MIL << "ZConfig singleton created." << endl;
443 /** Remember any parsed zypp.conf. */
444 Pathname _parsedZyppConf;
447 Locale cfg_textLocale;
449 Pathname cfg_cache_path;
450 Pathname cfg_metadata_path;
451 Pathname cfg_solvfiles_path;
452 Pathname cfg_packages_path;
454 Pathname cfg_config_path;
455 Pathname cfg_known_repos_path;
456 Pathname cfg_known_services_path;
457 Pathname cfg_vendor_path;
460 Pathname update_data_path;
461 Pathname update_scripts_path;
462 Pathname update_messages_path;
463 DefaultOption<std::string> updateMessagesNotify;
466 unsigned repo_refresh_delay;
467 bool repoLabelIsAlias;
469 bool download_use_deltarpm;
470 bool download_use_deltarpm_always;
471 DefaultOption<bool> download_media_prefer_download;
473 int download_max_concurrent_connections;
474 int download_min_download_speed;
475 int download_max_download_speed;
476 int download_max_silent_tries;
478 Option<DownloadMode> commit_downloadMode;
480 Option<bool> solver_onlyRequires;
481 Option<bool> solver_allowVendorChange;
482 Option<unsigned> solver_upgradeTestcasesToKeep;
483 DefaultOption<bool> solverUpgradeRemoveDroppedPackages;
485 Pathname solver_checkSystemFile;
487 std::set<std::string> multiversion;
489 bool apply_locks_file;
491 target::rpm::RpmInstFlags rpmInstallFlags;
493 Pathname history_log_path;
494 Pathname credentials_global_dir_path;
495 Pathname credentials_global_file_path;
497 ///////////////////////////////////////////////////////////////////
499 // Backdoor to redirect ZConfig from within the running
500 // TEST-application. HANDLE WITH CARE!
501 void reconfigureZConfig( const Pathname & override_r )
503 // ctor puts itself unter smart pointer control.
504 new ZConfig::Impl( override_r );
507 ///////////////////////////////////////////////////////////////////
509 // METHOD NAME : ZConfig::instance
510 // METHOD TYPE : ZConfig &
512 ZConfig & ZConfig::instance()
514 static ZConfig _instance; // The singleton
518 ///////////////////////////////////////////////////////////////////
520 // METHOD NAME : ZConfig::ZConfig
521 // METHOD TYPE : Ctor
529 ///////////////////////////////////////////////////////////////////
531 // METHOD NAME : ZConfig::~ZConfig
532 // METHOD TYPE : Dtor
537 Pathname ZConfig::systemRoot() const
539 Target_Ptr target( getZYpp()->getTarget() );
540 return target ? target->root() : Pathname();
543 ///////////////////////////////////////////////////////////////////
545 // system architecture
547 ///////////////////////////////////////////////////////////////////
549 Arch ZConfig::defaultSystemArchitecture()
551 static Arch _val( _autodetectSystemArchitecture() );
555 Arch ZConfig::systemArchitecture() const
556 { return _pimpl->cfg_arch; }
558 void ZConfig::setSystemArchitecture( const Arch & arch_r )
560 if ( arch_r != _pimpl->cfg_arch )
562 WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
563 _pimpl->cfg_arch = arch_r;
567 ///////////////////////////////////////////////////////////////////
571 ///////////////////////////////////////////////////////////////////
573 Locale ZConfig::defaultTextLocale()
575 static Locale _val( _autodetectTextLocale() );
579 Locale ZConfig::textLocale() const
580 { return _pimpl->cfg_textLocale; }
582 void ZConfig::setTextLocale( const Locale & locale_r )
584 if ( locale_r != _pimpl->cfg_textLocale )
586 WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
587 _pimpl->cfg_textLocale = locale_r;
588 #warning prefer signal
589 sat::Pool::instance().setTextLocale( locale_r );
593 ///////////////////////////////////////////////////////////////////
595 Pathname ZConfig::repoCachePath() const
597 return ( _pimpl->cfg_cache_path.empty()
598 ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
601 Pathname ZConfig::repoMetadataPath() const
603 return ( _pimpl->cfg_metadata_path.empty()
604 ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
607 Pathname ZConfig::repoSolvfilesPath() const
609 return ( _pimpl->cfg_solvfiles_path.empty()
610 ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
613 Pathname ZConfig::repoPackagesPath() const
615 return ( _pimpl->cfg_packages_path.empty()
616 ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
619 ///////////////////////////////////////////////////////////////////
621 Pathname ZConfig::configPath() const
623 return ( _pimpl->cfg_config_path.empty()
624 ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
627 Pathname ZConfig::knownReposPath() const
629 return ( _pimpl->cfg_known_repos_path.empty()
630 ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
633 Pathname ZConfig::knownServicesPath() const
635 return ( _pimpl->cfg_known_services_path.empty()
636 ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
639 Pathname ZConfig::vendorPath() const
641 return ( _pimpl->cfg_vendor_path.empty()
642 ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
645 Pathname ZConfig::locksFile() const
647 return ( _pimpl->locks_file.empty()
648 ? (configPath()/"locks") : _pimpl->locks_file );
651 ///////////////////////////////////////////////////////////////////
653 bool ZConfig::repo_add_probe() const
655 return _pimpl->repo_add_probe;
658 unsigned ZConfig::repo_refresh_delay() const
660 return _pimpl->repo_refresh_delay;
663 bool ZConfig::repoLabelIsAlias() const
664 { return _pimpl->repoLabelIsAlias; }
666 void ZConfig::repoLabelIsAlias( bool yesno_r )
667 { _pimpl->repoLabelIsAlias = yesno_r; }
669 bool ZConfig::download_use_deltarpm() const
670 { return _pimpl->download_use_deltarpm; }
672 bool ZConfig::download_use_deltarpm_always() const
673 { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
675 bool ZConfig::download_media_prefer_download() const
676 { return _pimpl->download_media_prefer_download; }
678 void ZConfig::set_download_media_prefer_download( bool yesno_r )
679 { _pimpl->download_media_prefer_download.set( yesno_r ); }
681 void ZConfig::set_default_download_media_prefer_download()
682 { _pimpl->download_media_prefer_download.restoreToDefault(); }
684 long ZConfig::download_max_concurrent_connections() const
685 { return _pimpl->download_max_concurrent_connections; }
687 long ZConfig::download_min_download_speed() const
688 { return _pimpl->download_min_download_speed; }
690 long ZConfig::download_max_download_speed() const
691 { return _pimpl->download_max_download_speed; }
693 long ZConfig::download_max_silent_tries() const
694 { return _pimpl->download_max_silent_tries; }
696 DownloadMode ZConfig::commit_downloadMode() const
697 { return _pimpl->commit_downloadMode; }
699 bool ZConfig::solver_onlyRequires() const
700 { return _pimpl->solver_onlyRequires; }
702 bool ZConfig::solver_allowVendorChange() const
703 { return _pimpl->solver_allowVendorChange; }
705 Pathname ZConfig::solver_checkSystemFile() const
706 { return ( _pimpl->solver_checkSystemFile.empty()
707 ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
709 unsigned ZConfig::solver_upgradeTestcasesToKeep() const
710 { return _pimpl->solver_upgradeTestcasesToKeep; }
712 bool ZConfig::solverUpgradeRemoveDroppedPackages() const { return _pimpl->solverUpgradeRemoveDroppedPackages; }
713 void ZConfig::setSolverUpgradeRemoveDroppedPackages( bool val_r ) { _pimpl->solverUpgradeRemoveDroppedPackages.set( val_r ); }
714 void ZConfig::resetSolverUpgradeRemoveDroppedPackages() { _pimpl->solverUpgradeRemoveDroppedPackages.restoreToDefault(); }
716 const std::set<std::string> & ZConfig::multiversionSpec() const { return _pimpl->multiversion; }
717 void ZConfig::addMultiversionSpec( const std::string & name_r ) { _pimpl->multiversion.insert( name_r ); }
718 void ZConfig::removeMultiversionSpec( const std::string & name_r ) { _pimpl->multiversion.erase( name_r ); }
720 bool ZConfig::apply_locks_file() const
721 { return _pimpl->apply_locks_file; }
723 Pathname ZConfig::update_dataPath() const
725 return ( _pimpl->update_data_path.empty()
726 ? Pathname("/var/adm") : _pimpl->update_data_path );
729 Pathname ZConfig::update_messagesPath() const
731 return ( _pimpl->update_messages_path.empty()
732 ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
735 Pathname ZConfig::update_scriptsPath() const
737 return ( _pimpl->update_scripts_path.empty()
738 ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
741 std::string ZConfig::updateMessagesNotify() const
742 { return _pimpl->updateMessagesNotify; }
744 void ZConfig::setUpdateMessagesNotify( const std::string & val_r )
745 { _pimpl->updateMessagesNotify.set( val_r ); }
747 void ZConfig::resetUpdateMessagesNotify()
748 { _pimpl->updateMessagesNotify.restoreToDefault(); }
750 ///////////////////////////////////////////////////////////////////
752 target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
753 { return _pimpl->rpmInstallFlags; }
756 Pathname ZConfig::historyLogFile() const
758 return ( _pimpl->history_log_path.empty() ?
759 Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
763 Pathname ZConfig::credentialsGlobalDir() const
765 return ( _pimpl->credentials_global_dir_path.empty() ?
766 Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
769 Pathname ZConfig::credentialsGlobalFile() const
771 return ( _pimpl->credentials_global_file_path.empty() ?
772 Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
775 ///////////////////////////////////////////////////////////////////
777 std::string ZConfig::distroverpkg() const
778 { return "redhat-release"; }
780 ///////////////////////////////////////////////////////////////////
782 std::ostream & ZConfig::about( std::ostream & str ) const
784 str << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
786 str << "satsolver: " << sat_version;
787 if ( ::strcmp( sat_version, SATSOLVER_VERSION_STRING ) )
788 str << " (built against " << SATSOLVER_VERSION_STRING << ")";
791 str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
792 str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
793 str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
797 /////////////////////////////////////////////////////////////////
799 ///////////////////////////////////////////////////////////////////