1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ZConfig.cc
14 #include <sys/utsname.h>
19 #include "zypp/base/Logger.h"
20 #include "zypp/base/IOStream.h"
21 #include "zypp/base/InputStream.h"
22 #include "zypp/base/String.h"
24 #include "zypp/ZConfig.h"
25 #include "zypp/ZYppFactory.h"
26 #include "zypp/PathInfo.h"
27 #include "zypp/parser/IniDict.h"
30 using namespace zypp::filesystem;
31 using namespace zypp::parser;
33 #undef ZYPP_BASE_LOGGER_LOGGROUP
34 #define ZYPP_BASE_LOGGER_LOGGROUP "zconfig"
36 ///////////////////////////////////////////////////////////////////
38 { /////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////
42 { /////////////////////////////////////////////////////////////////
44 /** Determine system architecture evaluating \c uname and \c /proc/cpuinfo.
46 Arch _autodetectSystemArchitecture()
49 if ( ::uname( &buf ) < 0 )
51 ERR << "Can't determine system architecture" << endl;
55 Arch architecture( buf.machine );
56 MIL << "Uname architecture is '" << buf.machine << "'" << endl;
58 // some CPUs report i686 but dont implement cx8 and cmov
59 // check for both flags in /proc/cpuinfo and downgrade
60 // to i586 if either is missing (cf bug #18885)
61 if ( architecture == Arch_i686 )
63 std::ifstream cpuinfo( "/proc/cpuinfo" );
66 for( iostr::EachLine in( cpuinfo ); in; in.next() )
68 if ( str::hasPrefix( *in, "flags" ) )
70 if ( in->find( "cx8" ) == std::string::npos
71 || in->find( "cmov" ) == std::string::npos )
73 architecture = Arch_i586;
74 WAR << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
82 ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
88 /** The locale to be used for texts and messages.
90 * For the encoding to be used the preference is
92 * LC_ALL, LC_CTYPE, LANG
94 * For the language of the messages to be used, the preference is
96 * LANGUAGE, LC_ALL, LC_MESSAGES, LANG
98 * Note that LANGUAGE can contain more than one locale name, it can be
99 * a list of locale names like for example
101 * LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
103 * \todo Support dynamic fallbacklists defined by LANGUAGE
105 Locale _autodetectTextLocale()
108 const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
109 for ( const char ** envvar = envlist; *envvar; ++envvar )
111 const char * envlang = getenv( *envvar );
114 std::string envstr( envlang );
115 if ( envstr != "POSIX" && envstr != "C" )
117 Locale lang( envstr );
118 if ( ! lang.code().empty() )
120 MIL << "Found " << *envvar << "=" << envstr << endl;
127 MIL << "Default text locale is '" << ret << "'" << endl;
128 #warning HACK AROUND BOOST_TEST_CATCH_SYSTEM_ERRORS
129 setenv( "BOOST_TEST_CATCH_SYSTEM_ERRORS", "no", 1 );
133 /////////////////////////////////////////////////////////////////
135 ///////////////////////////////////////////////////////////////////
137 /** Mutable option with initial value. */
138 template<class _Tp, _Tp _Initial>
141 typedef _Tp value_type;
143 /** Default ctor sets _Initial. */
148 /** Get the value. */
149 value_type get() const
152 /** Set a new value. */
153 void set( const value_type & newval_r )
160 /** Mutable option with initial value also remembering a config value. */
161 template<class _Tp, _Tp _Initial>
162 struct DefaultOption : public Option<_Tp,_Initial>
164 typedef _Tp value_type;
165 typedef Option<_Tp,_Initial> option_type;
167 /** Reset value to the current default. */
168 void restoreToDefault()
169 { this->set( _default.get() ); }
171 /** Reset value to a new default. */
172 void restoreToDefault( const value_type & newval_r )
173 { setDefault( newval_r ); restoreToDefault(); }
175 /** Get the current default value. */
176 value_type getDefault() const
177 { return _default.get(); }
179 /** Set a new default value. */
180 void setDefault( const value_type & newval_r )
181 { _default.set( newval_r ); }
184 option_type _default;
187 ///////////////////////////////////////////////////////////////////
189 // CLASS NAME : ZConfig::Impl
191 /** ZConfig implementation.
192 * \todo Enrich section and entry definition by some comment
193 * (including the default setting and provide some method to
194 * write this into a sample zypp.conf.
199 Impl( const Pathname & override_r = Pathname() )
200 : _parsedZyppConf ( override_r )
201 , cfg_arch ( defaultSystemArchitecture() )
202 , cfg_textLocale ( defaultTextLocale() )
203 , repo_add_probe ( false )
204 , repo_refresh_delay ( 10 )
205 , download_use_deltarpm ( true )
206 , download_use_deltarpm_always ( false )
207 , download_max_concurrent_connections(2)
208 , download_min_download_speed(0)
209 , download_max_download_speed(0)
210 , download_max_silent_tries(5)
211 , apply_locks_file ( true )
214 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
215 // override_r has higest prio
216 // ZYPP_CONF might override /etc/zypp/zypp.conf
217 if ( _parsedZyppConf.empty() )
219 const char *env_confpath = getenv( "ZYPP_CONF" );
220 _parsedZyppConf = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
224 // Inject this into ZConfig. Be shure this is
225 // allocated via new. See: reconfigureZConfig
226 INT << "Reconfigure to " << _parsedZyppConf << endl;
227 ZConfig::instance()._pimpl.reset( this );
229 if ( PathInfo(_parsedZyppConf).isExist() )
231 parser::IniDict dict( _parsedZyppConf );
232 for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
233 sit != dict.sectionsEnd();
236 string section(*sit);
237 //MIL << section << endl;
238 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
239 it != dict.entriesEnd(*sit);
242 string entry(it->first);
243 string value(it->second);
244 //DBG << (*it).first << "=" << (*it).second << endl;
245 if ( section == "main" )
247 if ( entry == "arch" )
250 if ( carch != cfg_arch )
252 WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
256 else if ( entry == "cachedir" )
258 cfg_cache_path = Pathname(value);
260 else if ( entry == "metadatadir" )
262 cfg_metadata_path = Pathname(value);
264 else if ( entry == "solvfilesdir" )
266 cfg_solvfiles_path = Pathname(value);
268 else if ( entry == "packagesdir" )
270 cfg_packages_path = Pathname(value);
272 else if ( entry == "configdir" )
274 cfg_config_path = Pathname(value);
276 else if ( entry == "reposdir" )
278 cfg_known_repos_path = Pathname(value);
280 else if ( entry == "servicesdir" )
282 cfg_known_services_path = Pathname(value);
284 else if ( entry == "repo.add.probe" )
286 repo_add_probe = str::strToBool( value, repo_add_probe );
288 else if ( entry == "repo.refresh.delay" )
290 str::strtonum(value, repo_refresh_delay);
292 else if ( entry == "download.use_deltarpm" )
294 download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
296 else if ( entry == "download.use_deltarpm.always" )
298 download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
300 else if ( entry == "download.media_preference" )
302 download_media_prefer_download.restoreToDefault( str::compareCI( value, "volatile" ) != 0 );
304 else if ( entry == "download.max_concurrent_connections" )
306 str::strtonum(value, download_max_concurrent_connections);
308 else if ( entry == "download.min_download_speed" )
310 str::strtonum(value, download_min_download_speed);
312 else if ( entry == "download.max_download_speed" )
314 str::strtonum(value, download_max_download_speed);
316 else if ( entry == "download.max_silent_tries" )
318 str::strtonum(value, download_max_silent_tries);
320 else if ( entry == "vendordir" )
322 cfg_vendor_path = Pathname(value);
324 else if ( entry == "productsdir" )
326 WAR << "Deprecated entry 'productsdir=': This locations is no longer used or supported." << endl;
327 cfg_products_path = Pathname(value);
329 else if ( entry == "solver.onlyRequires" )
331 solver_onlyRequires.set( str::strToBool( value, solver_onlyRequires.get() ) );
333 else if ( entry == "solver.allowVendorChange" )
335 solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange.get() ) );
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 );
374 else if ( entry == "history.logfile" )
376 history_log_path = Pathname(value);
378 else if ( entry == "credentials.global.dir" )
380 credentials_global_dir_path = Pathname(value);
382 else if ( entry == "credentials.global.file" )
384 credentials_global_file_path = Pathname(value);
392 MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
393 _parsedZyppConf.extend( " (NOT FOUND)" );
397 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
399 Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
400 if ( carch != cfg_arch )
402 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
406 MIL << "ZConfig singleton created." << endl;
413 /** Remember any parsed zypp.conf. */
414 Pathname _parsedZyppConf;
417 Locale cfg_textLocale;
419 Pathname cfg_cache_path;
420 Pathname cfg_metadata_path;
421 Pathname cfg_solvfiles_path;
422 Pathname cfg_packages_path;
424 Pathname cfg_config_path;
425 Pathname cfg_known_repos_path;
426 Pathname cfg_known_services_path;
427 Pathname cfg_vendor_path;
428 Pathname cfg_products_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 Pathname solver_checkSystemFile;
451 std::set<IdString> multiversion;
453 bool apply_locks_file;
455 target::rpm::RpmInstFlags rpmInstallFlags;
457 Pathname history_log_path;
458 Pathname credentials_global_dir_path;
459 Pathname credentials_global_file_path;
461 ///////////////////////////////////////////////////////////////////
463 // Backdoor to redirect ZConfig from within the running
464 // TEST-application. HANDLE WITH CARE!
465 void reconfigureZConfig( const Pathname & override_r )
467 // ctor puts itself unter smart pointer control.
468 new ZConfig::Impl( override_r );
471 ///////////////////////////////////////////////////////////////////
473 // METHOD NAME : ZConfig::instance
474 // METHOD TYPE : ZConfig &
476 ZConfig & ZConfig::instance()
478 static ZConfig _instance; // The singleton
482 ///////////////////////////////////////////////////////////////////
484 // METHOD NAME : ZConfig::ZConfig
485 // METHOD TYPE : Ctor
493 ///////////////////////////////////////////////////////////////////
495 // METHOD NAME : ZConfig::~ZConfig
496 // METHOD TYPE : Dtor
501 ///////////////////////////////////////////////////////////////////
503 // system architecture
505 ///////////////////////////////////////////////////////////////////
507 Arch ZConfig::defaultSystemArchitecture()
509 static Arch _val( _autodetectSystemArchitecture() );
513 Arch ZConfig::systemArchitecture() const
514 { return _pimpl->cfg_arch; }
516 void ZConfig::setSystemArchitecture( const Arch & arch_r )
518 if ( arch_r != _pimpl->cfg_arch )
520 WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
521 _pimpl->cfg_arch = arch_r;
525 ///////////////////////////////////////////////////////////////////
529 ///////////////////////////////////////////////////////////////////
531 Locale ZConfig::defaultTextLocale()
533 static Locale _val( _autodetectTextLocale() );
537 Locale ZConfig::textLocale() const
538 { return _pimpl->cfg_textLocale; }
540 void ZConfig::setTextLocale( const Locale & locale_r )
542 if ( locale_r != _pimpl->cfg_textLocale )
544 WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
545 _pimpl->cfg_textLocale = locale_r;
549 ///////////////////////////////////////////////////////////////////
551 Pathname ZConfig::repoCachePath() const
553 return ( _pimpl->cfg_cache_path.empty()
554 ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
557 Pathname ZConfig::repoMetadataPath() const
559 return ( _pimpl->cfg_metadata_path.empty()
560 ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
563 Pathname ZConfig::repoSolvfilesPath() const
565 return ( _pimpl->cfg_solvfiles_path.empty()
566 ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
569 Pathname ZConfig::repoPackagesPath() const
571 return ( _pimpl->cfg_packages_path.empty()
572 ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
575 ///////////////////////////////////////////////////////////////////
577 Pathname ZConfig::configPath() const
579 return ( _pimpl->cfg_config_path.empty()
580 ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
583 Pathname ZConfig::knownReposPath() const
585 return ( _pimpl->cfg_known_repos_path.empty()
586 ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
589 Pathname ZConfig::knownServicesPath() const
591 return ( _pimpl->cfg_known_services_path.empty()
592 ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
595 Pathname ZConfig::vendorPath() const
597 return ( _pimpl->cfg_vendor_path.empty()
598 ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
601 Pathname ZConfig::productsPath() const
603 return ( _pimpl->cfg_products_path.empty()
604 ? (configPath()/"products.d") : _pimpl->cfg_products_path );
607 Pathname ZConfig::locksFile() const
609 return ( _pimpl->locks_file.empty()
610 ? (configPath()/"locks") : _pimpl->locks_file );
613 ///////////////////////////////////////////////////////////////////
615 bool ZConfig::repo_add_probe() const
617 return _pimpl->repo_add_probe;
620 unsigned ZConfig::repo_refresh_delay() const
622 return _pimpl->repo_refresh_delay;
625 bool ZConfig::download_use_deltarpm() const
626 { return _pimpl->download_use_deltarpm; }
628 bool ZConfig::download_use_deltarpm_always() const
629 { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
631 bool ZConfig::download_media_prefer_download() const
632 { return _pimpl->download_media_prefer_download.get(); }
634 void ZConfig::set_download_media_prefer_download( bool yesno_r )
635 { _pimpl->download_media_prefer_download.set( yesno_r ); }
637 void ZConfig::set_default_download_media_prefer_download()
638 { _pimpl->download_media_prefer_download.restoreToDefault(); }
640 long ZConfig::download_max_concurrent_connections() const
641 { return _pimpl->download_max_concurrent_connections; }
643 long ZConfig::download_min_download_speed() const
644 { return _pimpl->download_min_download_speed; }
646 long ZConfig::download_max_download_speed() const
647 { return _pimpl->download_max_download_speed; }
649 long ZConfig::download_max_silent_tries() const
650 { return _pimpl->download_max_silent_tries; }
652 bool ZConfig::solver_onlyRequires() const
653 { return _pimpl->solver_onlyRequires.get(); }
655 bool ZConfig::solver_allowVendorChange() const
656 { return _pimpl->solver_allowVendorChange.get(); }
658 Pathname ZConfig::solver_checkSystemFile() const
659 { return ( _pimpl->solver_checkSystemFile.empty()
660 ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
662 std::set<IdString> ZConfig::multiversion() const
663 { return _pimpl->multiversion; }
665 void ZConfig::addMultiversion(std::string &name)
666 { _pimpl->multiversion.insert(IdString(name)); }
668 bool ZConfig::removeMultiversion(std::string &name)
669 { return _pimpl->multiversion.erase(IdString(name)); }
671 bool ZConfig::apply_locks_file() const
672 { return _pimpl->apply_locks_file; }
674 Pathname ZConfig::update_dataPath() const
676 return ( _pimpl->update_data_path.empty()
677 ? Pathname("/var/adm") : _pimpl->update_data_path );
680 Pathname ZConfig::update_messagesPath() const
682 return ( _pimpl->update_messages_path.empty()
683 ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
687 Pathname ZConfig::update_scriptsPath() const
689 return ( _pimpl->update_scripts_path.empty()
690 ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
693 ///////////////////////////////////////////////////////////////////
695 target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
696 { return _pimpl->rpmInstallFlags; }
699 Pathname ZConfig::historyLogFile() const
701 return ( _pimpl->history_log_path.empty() ?
702 Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
706 Pathname ZConfig::credentialsGlobalDir() const
708 return ( _pimpl->credentials_global_dir_path.empty() ?
709 Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
712 Pathname ZConfig::credentialsGlobalFile() const
714 return ( _pimpl->credentials_global_file_path.empty() ?
715 Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
718 ///////////////////////////////////////////////////////////////////
720 std::ostream & ZConfig::about( std::ostream & str ) const
722 str << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
723 str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
724 str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
725 str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
729 /////////////////////////////////////////////////////////////////
731 ///////////////////////////////////////////////////////////////////