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"
31 using namespace zypp::filesystem;
32 using namespace zypp::parser;
34 #undef ZYPP_BASE_LOGGER_LOGGROUP
35 #define ZYPP_BASE_LOGGER_LOGGROUP "zconfig"
37 ///////////////////////////////////////////////////////////////////
39 { /////////////////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////
43 { /////////////////////////////////////////////////////////////////
45 /** Determine system architecture evaluating \c uname and \c /proc/cpuinfo.
47 Arch _autodetectSystemArchitecture()
50 if ( ::uname( &buf ) < 0 )
52 ERR << "Can't determine system architecture" << endl;
56 Arch architecture( buf.machine );
57 MIL << "Uname architecture is '" << buf.machine << "'" << endl;
59 // some CPUs report i686 but dont implement cx8 and cmov
60 // check for both flags in /proc/cpuinfo and downgrade
61 // to i586 if either is missing (cf bug #18885)
62 if ( architecture == Arch_i686 )
64 std::ifstream cpuinfo( "/proc/cpuinfo" );
67 for( iostr::EachLine in( cpuinfo ); in; in.next() )
69 if ( str::hasPrefix( *in, "flags" ) )
71 if ( in->find( "cx8" ) == std::string::npos
72 || in->find( "cmov" ) == std::string::npos )
74 architecture = Arch_i586;
75 WAR << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
83 ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
89 /** The locale to be used for texts and messages.
91 * For the encoding to be used the preference is
93 * LC_ALL, LC_CTYPE, LANG
95 * For the language of the messages to be used, the preference is
97 * LANGUAGE, LC_ALL, LC_MESSAGES, LANG
99 * Note that LANGUAGE can contain more than one locale name, it can be
100 * a list of locale names like for example
102 * LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
104 * \todo Support dynamic fallbacklists defined by LANGUAGE
106 Locale _autodetectTextLocale()
109 const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
110 for ( const char ** envvar = envlist; *envvar; ++envvar )
112 const char * envlang = getenv( *envvar );
115 std::string envstr( envlang );
116 if ( envstr != "POSIX" && envstr != "C" )
118 Locale lang( envstr );
119 if ( ! lang.code().empty() )
121 MIL << "Found " << *envvar << "=" << envstr << endl;
128 MIL << "Default text locale is '" << ret << "'" << endl;
129 #warning HACK AROUND BOOST_TEST_CATCH_SYSTEM_ERRORS
130 setenv( "BOOST_TEST_CATCH_SYSTEM_ERRORS", "no", 1 );
134 /////////////////////////////////////////////////////////////////
136 ///////////////////////////////////////////////////////////////////
138 /** Mutable option with initial value. */
139 template<class _Tp, _Tp _Initial>
142 typedef _Tp value_type;
144 /** Default ctor sets _Initial. */
149 /** Get the value. */
150 value_type get() const
153 /** Set a new value. */
154 void set( const value_type & newval_r )
161 /** Mutable option with initial value also remembering a config value. */
162 template<class _Tp, _Tp _Initial>
163 struct DefaultOption : public Option<_Tp,_Initial>
165 typedef _Tp value_type;
166 typedef Option<_Tp,_Initial> option_type;
168 /** Reset value to the current default. */
169 void restoreToDefault()
170 { this->set( _default.get() ); }
172 /** Reset value to a new default. */
173 void restoreToDefault( const value_type & newval_r )
174 { setDefault( newval_r ); restoreToDefault(); }
176 /** Get the current default value. */
177 value_type getDefault() const
178 { return _default.get(); }
180 /** Set a new default value. */
181 void setDefault( const value_type & newval_r )
182 { _default.set( newval_r ); }
185 option_type _default;
188 ///////////////////////////////////////////////////////////////////
190 // CLASS NAME : ZConfig::Impl
192 /** ZConfig implementation.
193 * \todo Enrich section and entry definition by some comment
194 * (including the default setting and provide some method to
195 * write this into a sample zypp.conf.
200 Impl( const Pathname & override_r = Pathname() )
201 : _parsedZyppConf ( override_r )
202 , cfg_arch ( defaultSystemArchitecture() )
203 , cfg_textLocale ( defaultTextLocale() )
204 , repo_add_probe ( false )
205 , repo_refresh_delay ( 10 )
206 , download_use_deltarpm ( true )
207 , download_use_deltarpm_always ( false )
208 , download_max_concurrent_connections(2)
209 , download_min_download_speed(0)
210 , download_max_download_speed(0)
211 , download_max_silent_tries(5)
212 , apply_locks_file ( true )
215 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
216 // override_r has higest prio
217 // ZYPP_CONF might override /etc/zypp/zypp.conf
218 if ( _parsedZyppConf.empty() )
220 const char *env_confpath = getenv( "ZYPP_CONF" );
221 _parsedZyppConf = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
225 // Inject this into ZConfig. Be shure this is
226 // allocated via new. See: reconfigureZConfig
227 INT << "Reconfigure to " << _parsedZyppConf << endl;
228 ZConfig::instance()._pimpl.reset( this );
230 if ( PathInfo(_parsedZyppConf).isExist() )
232 parser::IniDict dict( _parsedZyppConf );
233 for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
234 sit != dict.sectionsEnd();
237 string section(*sit);
238 //MIL << section << endl;
239 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
240 it != dict.entriesEnd(*sit);
243 string entry(it->first);
244 string value(it->second);
245 //DBG << (*it).first << "=" << (*it).second << endl;
246 if ( section == "main" )
248 if ( entry == "arch" )
251 if ( carch != cfg_arch )
253 WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
257 else if ( entry == "cachedir" )
259 cfg_cache_path = Pathname(value);
261 else if ( entry == "metadatadir" )
263 cfg_metadata_path = Pathname(value);
265 else if ( entry == "solvfilesdir" )
267 cfg_solvfiles_path = Pathname(value);
269 else if ( entry == "packagesdir" )
271 cfg_packages_path = Pathname(value);
273 else if ( entry == "configdir" )
275 cfg_config_path = Pathname(value);
277 else if ( entry == "reposdir" )
279 cfg_known_repos_path = Pathname(value);
281 else if ( entry == "servicesdir" )
283 cfg_known_services_path = Pathname(value);
285 else if ( entry == "repo.add.probe" )
287 repo_add_probe = str::strToBool( value, repo_add_probe );
289 else if ( entry == "repo.refresh.delay" )
291 str::strtonum(value, repo_refresh_delay);
293 else if ( entry == "download.use_deltarpm" )
295 download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
297 else if ( entry == "download.use_deltarpm.always" )
299 download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
301 else if ( entry == "download.media_preference" )
303 download_media_prefer_download.restoreToDefault( str::compareCI( value, "volatile" ) != 0 );
305 else if ( entry == "download.max_concurrent_connections" )
307 str::strtonum(value, download_max_concurrent_connections);
309 else if ( entry == "download.min_download_speed" )
311 str::strtonum(value, download_min_download_speed);
313 else if ( entry == "download.max_download_speed" )
315 str::strtonum(value, download_max_download_speed);
317 else if ( entry == "download.max_silent_tries" )
319 str::strtonum(value, download_max_silent_tries);
321 else if ( entry == "vendordir" )
323 cfg_vendor_path = Pathname(value);
325 else if ( entry == "solver.onlyRequires" )
327 solver_onlyRequires.set( str::strToBool( value, solver_onlyRequires.get() ) );
329 else if ( entry == "solver.allowVendorChange" )
331 solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange.get() ) );
333 else if ( entry == "solver.upgradeTestcasesToKeep" )
335 solver_upgradeTestcasesToKeep.set( str::strtonum<unsigned>( value ) );
337 else if ( entry == "solver.checkSystemFile" )
339 solver_checkSystemFile = Pathname(value);
341 else if ( entry == "multiversion" )
343 std::list<std::string> multi;
344 str::split( value, back_inserter(multi), ", \t" );
345 for ( std::list<string>::const_iterator it = multi.begin();
346 it != multi.end(); it++) {
347 multiversion.insert (IdString(*it));
350 else if ( entry == "locksfile.path" )
352 locks_file = Pathname(value);
354 else if ( entry == "locksfile.apply" )
356 apply_locks_file = str::strToBool( value, apply_locks_file );
358 else if ( entry == "update.datadir" )
360 update_data_path = Pathname(value);
362 else if ( entry == "update.scriptsdir" )
364 update_scripts_path = Pathname(value);
366 else if ( entry == "update.messagessdir" )
368 update_messages_path = Pathname(value);
370 else if ( entry == "rpm.install.excludedocs" )
372 rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS,
373 str::strToBool( value, false ) );
375 else if ( entry == "history.logfile" )
377 history_log_path = Pathname(value);
379 else if ( entry == "credentials.global.dir" )
381 credentials_global_dir_path = Pathname(value);
383 else if ( entry == "credentials.global.file" )
385 credentials_global_file_path = Pathname(value);
393 MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
394 _parsedZyppConf = _parsedZyppConf.extend( " (NOT FOUND)" );
398 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
400 Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
401 if ( carch != cfg_arch )
403 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
407 MIL << "ZConfig singleton created." << endl;
414 /** Remember any parsed zypp.conf. */
415 Pathname _parsedZyppConf;
418 Locale cfg_textLocale;
420 Pathname cfg_cache_path;
421 Pathname cfg_metadata_path;
422 Pathname cfg_solvfiles_path;
423 Pathname cfg_packages_path;
425 Pathname cfg_config_path;
426 Pathname cfg_known_repos_path;
427 Pathname cfg_known_services_path;
428 Pathname cfg_vendor_path;
431 Pathname update_data_path;
432 Pathname update_scripts_path;
433 Pathname update_messages_path;
436 unsigned repo_refresh_delay;
438 bool download_use_deltarpm;
439 bool download_use_deltarpm_always;
440 DefaultOption<bool,true> download_media_prefer_download;
442 int download_max_concurrent_connections;
443 int download_min_download_speed;
444 int download_max_download_speed;
445 int download_max_silent_tries;
447 Option<bool,false> solver_onlyRequires;
448 Option<bool,false> solver_allowVendorChange;
449 Option<unsigned,2U> solver_upgradeTestcasesToKeep;
450 Pathname solver_checkSystemFile;
452 std::set<IdString> multiversion;
454 bool apply_locks_file;
456 target::rpm::RpmInstFlags rpmInstallFlags;
458 Pathname history_log_path;
459 Pathname credentials_global_dir_path;
460 Pathname credentials_global_file_path;
462 ///////////////////////////////////////////////////////////////////
464 // Backdoor to redirect ZConfig from within the running
465 // TEST-application. HANDLE WITH CARE!
466 void reconfigureZConfig( const Pathname & override_r )
468 // ctor puts itself unter smart pointer control.
469 new ZConfig::Impl( override_r );
472 ///////////////////////////////////////////////////////////////////
474 // METHOD NAME : ZConfig::instance
475 // METHOD TYPE : ZConfig &
477 ZConfig & ZConfig::instance()
479 static ZConfig _instance; // The singleton
483 ///////////////////////////////////////////////////////////////////
485 // METHOD NAME : ZConfig::ZConfig
486 // METHOD TYPE : Ctor
494 ///////////////////////////////////////////////////////////////////
496 // METHOD NAME : ZConfig::~ZConfig
497 // METHOD TYPE : Dtor
502 ///////////////////////////////////////////////////////////////////
504 // system architecture
506 ///////////////////////////////////////////////////////////////////
508 Arch ZConfig::defaultSystemArchitecture()
510 static Arch _val( _autodetectSystemArchitecture() );
514 Arch ZConfig::systemArchitecture() const
515 { return _pimpl->cfg_arch; }
517 void ZConfig::setSystemArchitecture( const Arch & arch_r )
519 if ( arch_r != _pimpl->cfg_arch )
521 WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
522 _pimpl->cfg_arch = arch_r;
526 ///////////////////////////////////////////////////////////////////
530 ///////////////////////////////////////////////////////////////////
532 Locale ZConfig::defaultTextLocale()
534 static Locale _val( _autodetectTextLocale() );
538 Locale ZConfig::textLocale() const
539 { return _pimpl->cfg_textLocale; }
541 void ZConfig::setTextLocale( const Locale & locale_r )
543 if ( locale_r != _pimpl->cfg_textLocale )
545 WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
546 _pimpl->cfg_textLocale = locale_r;
550 ///////////////////////////////////////////////////////////////////
552 Pathname ZConfig::repoCachePath() const
554 return ( _pimpl->cfg_cache_path.empty()
555 ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
558 Pathname ZConfig::repoMetadataPath() const
560 return ( _pimpl->cfg_metadata_path.empty()
561 ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
564 Pathname ZConfig::repoSolvfilesPath() const
566 return ( _pimpl->cfg_solvfiles_path.empty()
567 ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
570 Pathname ZConfig::repoPackagesPath() const
572 return ( _pimpl->cfg_packages_path.empty()
573 ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
576 ///////////////////////////////////////////////////////////////////
578 Pathname ZConfig::configPath() const
580 return ( _pimpl->cfg_config_path.empty()
581 ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
584 Pathname ZConfig::knownReposPath() const
586 return ( _pimpl->cfg_known_repos_path.empty()
587 ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
590 Pathname ZConfig::knownServicesPath() const
592 return ( _pimpl->cfg_known_services_path.empty()
593 ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
596 Pathname ZConfig::vendorPath() const
598 return ( _pimpl->cfg_vendor_path.empty()
599 ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
602 Pathname ZConfig::locksFile() const
604 return ( _pimpl->locks_file.empty()
605 ? (configPath()/"locks") : _pimpl->locks_file );
608 ///////////////////////////////////////////////////////////////////
610 bool ZConfig::repo_add_probe() const
612 return _pimpl->repo_add_probe;
615 unsigned ZConfig::repo_refresh_delay() const
617 return _pimpl->repo_refresh_delay;
620 bool ZConfig::download_use_deltarpm() const
621 { return _pimpl->download_use_deltarpm; }
623 bool ZConfig::download_use_deltarpm_always() const
624 { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
626 bool ZConfig::download_media_prefer_download() const
627 { return _pimpl->download_media_prefer_download.get(); }
629 void ZConfig::set_download_media_prefer_download( bool yesno_r )
630 { _pimpl->download_media_prefer_download.set( yesno_r ); }
632 void ZConfig::set_default_download_media_prefer_download()
633 { _pimpl->download_media_prefer_download.restoreToDefault(); }
635 long ZConfig::download_max_concurrent_connections() const
636 { return _pimpl->download_max_concurrent_connections; }
638 long ZConfig::download_min_download_speed() const
639 { return _pimpl->download_min_download_speed; }
641 long ZConfig::download_max_download_speed() const
642 { return _pimpl->download_max_download_speed; }
644 long ZConfig::download_max_silent_tries() const
645 { return _pimpl->download_max_silent_tries; }
648 bool ZConfig::solver_onlyRequires() const
649 { return _pimpl->solver_onlyRequires.get(); }
651 bool ZConfig::solver_allowVendorChange() const
652 { return _pimpl->solver_allowVendorChange.get(); }
654 Pathname ZConfig::solver_checkSystemFile() const
655 { return ( _pimpl->solver_checkSystemFile.empty()
656 ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
658 unsigned ZConfig::solver_upgradeTestcasesToKeep() const
659 { return _pimpl->solver_upgradeTestcasesToKeep.get(); }
661 std::set<IdString> ZConfig::multiversion() const
662 { return _pimpl->multiversion; }
664 void ZConfig::addMultiversion(std::string &name)
665 { _pimpl->multiversion.insert(IdString(name)); }
667 bool ZConfig::removeMultiversion(std::string &name)
668 { return _pimpl->multiversion.erase(IdString(name)); }
670 bool ZConfig::apply_locks_file() const
671 { return _pimpl->apply_locks_file; }
673 Pathname ZConfig::update_dataPath() const
675 return ( _pimpl->update_data_path.empty()
676 ? Pathname("/var/adm") : _pimpl->update_data_path );
679 Pathname ZConfig::update_messagesPath() const
681 return ( _pimpl->update_messages_path.empty()
682 ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
686 Pathname ZConfig::update_scriptsPath() const
688 return ( _pimpl->update_scripts_path.empty()
689 ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
692 ///////////////////////////////////////////////////////////////////
694 target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
695 { return _pimpl->rpmInstallFlags; }
698 Pathname ZConfig::historyLogFile() const
700 return ( _pimpl->history_log_path.empty() ?
701 Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
705 Pathname ZConfig::credentialsGlobalDir() const
707 return ( _pimpl->credentials_global_dir_path.empty() ?
708 Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
711 Pathname ZConfig::credentialsGlobalFile() const
713 return ( _pimpl->credentials_global_file_path.empty() ?
714 Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
717 ///////////////////////////////////////////////////////////////////
719 std::string ZConfig::distroverpkg() const
720 { return "redhat-release"; }
722 ///////////////////////////////////////////////////////////////////
724 std::ostream & ZConfig::about( std::ostream & str ) const
726 str << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
728 str << "satsolver: " << sat_version;
729 if ( ::strcmp( sat_version, SATSOLVER_VERSION_STRING ) )
730 str << " (built against " << SATSOLVER_VERSION_STRING << ")";
733 str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
734 str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
735 str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
739 /////////////////////////////////////////////////////////////////
741 ///////////////////////////////////////////////////////////////////