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 ///////////////////////////////////////////////////////////////////
139 // CLASS NAME : ZConfig::Impl
141 /** ZConfig implementation.
142 * \todo Enrich section and entry definition by some comment
143 * (including the default setting and provide some method to
144 * write this into a sample zypp.conf.
149 Impl( const Pathname & override_r = Pathname() )
150 : _parsedZyppConf ( override_r )
151 , cfg_arch ( defaultSystemArchitecture() )
152 , cfg_textLocale ( defaultTextLocale() )
153 , repo_add_probe ( false )
154 , repo_refresh_delay ( 10 )
155 , download_use_deltarpm ( true )
156 , download_use_deltarpm_always ( false )
157 , download_max_concurrent_connections(2)
158 , download_min_download_speed(0)
159 , download_max_download_speed(0)
160 , download_max_silent_tries(5)
161 , solver_onlyRequires ( false )
162 , apply_locks_file ( true )
165 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
166 // override_r has higest prio
167 // ZYPP_CONF might override /etc/zypp/zypp.conf
168 if ( _parsedZyppConf.empty() )
170 const char *env_confpath = getenv( "ZYPP_CONF" );
171 _parsedZyppConf = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
175 // Inject this into ZConfig. Be shure this is
176 // allocated via new. See: reconfigureZConfig
177 INT << "Reconfigure to " << _parsedZyppConf << endl;
178 ZConfig::instance()._pimpl.reset( this );
180 if ( PathInfo(_parsedZyppConf).isExist() )
182 parser::IniDict dict( _parsedZyppConf );
183 for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
184 sit != dict.sectionsEnd();
187 string section(*sit);
188 //MIL << section << endl;
189 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
190 it != dict.entriesEnd(*sit);
193 string entry(it->first);
194 string value(it->second);
195 //DBG << (*it).first << "=" << (*it).second << endl;
196 if ( section == "main" )
198 if ( entry == "arch" )
201 if ( carch != cfg_arch )
203 WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
207 else if ( entry == "cachedir" )
209 cfg_cache_path = Pathname(value);
211 else if ( entry == "metadatadir" )
213 cfg_metadata_path = Pathname(value);
215 else if ( entry == "solvfilesdir" )
217 cfg_solvfiles_path = Pathname(value);
219 else if ( entry == "packagesdir" )
221 cfg_packages_path = Pathname(value);
223 else if ( entry == "configdir" )
225 cfg_config_path = Pathname(value);
227 else if ( entry == "reposdir" )
229 cfg_known_repos_path = Pathname(value);
231 else if ( entry == "repo.add.probe" )
233 repo_add_probe = str::strToBool( value, repo_add_probe );
235 else if ( entry == "repo.refresh.delay" )
237 str::strtonum(value, repo_refresh_delay);
239 else if ( entry == "download.use_deltarpm" )
241 download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
243 else if ( entry == "download.use_deltarpm.always" )
245 download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
247 else if ( entry == "download.max_concurrent_connections" )
249 str::strtonum(value, download_max_concurrent_connections);
251 else if ( entry == "download.min_download_speed" )
253 str::strtonum(value, download_min_download_speed);
255 else if ( entry == "download.max_download_speed" )
257 str::strtonum(value, download_max_download_speed);
259 else if ( entry == "download.max_silent_tries" )
261 str::strtonum(value, download_max_silent_tries);
263 else if ( entry == "vendordir" )
265 cfg_vendor_path = Pathname(value);
267 else if ( entry == "productsdir" )
269 WAR << "Deprecated entry 'productsdir=': This locations is no longer used or supported." << endl;
270 cfg_products_path = Pathname(value);
272 else if ( entry == "solver.onlyRequires" )
274 solver_onlyRequires = str::strToBool( value, solver_onlyRequires );
276 else if ( entry == "solver.checkSystemFile" )
278 solver_checkSystemFile = Pathname(value);
280 else if ( entry == "multiversion" )
282 std::list<std::string> multi;
283 str::split( value, back_inserter(multi), ", \t" );
284 for ( std::list<string>::const_iterator it = multi.begin();
285 it != multi.end(); it++) {
286 multiversion.insert (IdString(*it));
289 else if ( entry == "locksfile.path" )
291 locks_file = Pathname(value);
293 else if ( entry == "locksfile.apply" )
295 apply_locks_file = str::strToBool( value, apply_locks_file );
297 else if ( entry == "update.datadir" )
299 update_data_path = Pathname(value);
301 else if ( entry == "update.scriptsdir" )
303 update_scripts_path = Pathname(value);
305 else if ( entry == "update.messagessdir" )
307 update_messages_path = Pathname(value);
309 else if ( entry == "rpm.install.excludedocs" )
311 rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS );
313 else if ( entry == "history.logfile" )
315 history_log_path = Pathname(value);
317 else if ( entry == "credentials.global.dir" )
319 credentials_global_dir_path = Pathname(value);
321 else if ( entry == "credentials.global.file" )
323 credentials_global_file_path = Pathname(value);
331 MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
332 _parsedZyppConf.extend( " (NOT FOUND)" );
336 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
338 Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
339 if ( carch != cfg_arch )
341 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
345 MIL << "ZConfig singleton created." << endl;
352 /** Remember any parsed zypp.conf. */
353 Pathname _parsedZyppConf;
356 Locale cfg_textLocale;
358 Pathname cfg_cache_path;
359 Pathname cfg_metadata_path;
360 Pathname cfg_solvfiles_path;
361 Pathname cfg_packages_path;
363 Pathname cfg_config_path;
364 Pathname cfg_known_repos_path;
365 Pathname cfg_known_services_path;
366 Pathname cfg_vendor_path;
367 Pathname cfg_products_path;
370 Pathname update_data_path;
371 Pathname update_scripts_path;
372 Pathname update_messages_path;
375 unsigned repo_refresh_delay;
377 bool download_use_deltarpm;
378 bool download_use_deltarpm_always;
380 int download_max_concurrent_connections;
381 int download_min_download_speed;
382 int download_max_download_speed;
383 int download_max_silent_tries;
385 bool solver_onlyRequires;
386 Pathname solver_checkSystemFile;
388 std::set<IdString> multiversion;
390 bool apply_locks_file;
392 target::rpm::RpmInstFlags rpmInstallFlags;
394 Pathname history_log_path;
395 Pathname credentials_global_dir_path;
396 Pathname credentials_global_file_path;
398 ///////////////////////////////////////////////////////////////////
400 // Backdoor to redirect ZConfig from within the running
401 // TEST-application. HANDLE WITH CARE!
402 void reconfigureZConfig( const Pathname & override_r )
404 // ctor puts itself unter smart pointer control.
405 new ZConfig::Impl( override_r );
408 ///////////////////////////////////////////////////////////////////
410 // METHOD NAME : ZConfig::instance
411 // METHOD TYPE : ZConfig &
413 ZConfig & ZConfig::instance()
415 static ZConfig _instance; // The singleton
419 ///////////////////////////////////////////////////////////////////
421 // METHOD NAME : ZConfig::ZConfig
422 // METHOD TYPE : Ctor
430 ///////////////////////////////////////////////////////////////////
432 // METHOD NAME : ZConfig::~ZConfig
433 // METHOD TYPE : Dtor
438 ///////////////////////////////////////////////////////////////////
440 // system architecture
442 ///////////////////////////////////////////////////////////////////
444 Arch ZConfig::defaultSystemArchitecture()
446 static Arch _val( _autodetectSystemArchitecture() );
450 Arch ZConfig::systemArchitecture() const
451 { return _pimpl->cfg_arch; }
453 void ZConfig::setSystemArchitecture( const Arch & arch_r )
455 if ( arch_r != _pimpl->cfg_arch )
457 WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
458 _pimpl->cfg_arch = arch_r;
462 ///////////////////////////////////////////////////////////////////
466 ///////////////////////////////////////////////////////////////////
468 Locale ZConfig::defaultTextLocale()
470 static Locale _val( _autodetectTextLocale() );
474 Locale ZConfig::textLocale() const
475 { return _pimpl->cfg_textLocale; }
477 void ZConfig::setTextLocale( const Locale & locale_r )
479 if ( locale_r != _pimpl->cfg_textLocale )
481 WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
482 _pimpl->cfg_textLocale = locale_r;
486 ///////////////////////////////////////////////////////////////////
488 Pathname ZConfig::repoCachePath() const
490 return ( _pimpl->cfg_cache_path.empty()
491 ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
494 Pathname ZConfig::repoMetadataPath() const
496 return ( _pimpl->cfg_metadata_path.empty()
497 ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
500 Pathname ZConfig::repoSolvfilesPath() const
502 return ( _pimpl->cfg_solvfiles_path.empty()
503 ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
506 Pathname ZConfig::repoPackagesPath() const
508 return ( _pimpl->cfg_packages_path.empty()
509 ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
512 ///////////////////////////////////////////////////////////////////
514 Pathname ZConfig::configPath() const
516 return ( _pimpl->cfg_config_path.empty()
517 ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
520 Pathname ZConfig::knownReposPath() const
522 return ( _pimpl->cfg_known_repos_path.empty()
523 ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
526 Pathname ZConfig::knownServicesPath() const
528 return ( _pimpl->cfg_known_services_path.empty()
529 ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
532 Pathname ZConfig::vendorPath() const
534 return ( _pimpl->cfg_vendor_path.empty()
535 ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
538 Pathname ZConfig::productsPath() const
540 return ( _pimpl->cfg_products_path.empty()
541 ? (configPath()/"products.d") : _pimpl->cfg_products_path );
544 Pathname ZConfig::locksFile() const
546 return ( _pimpl->locks_file.empty()
547 ? (configPath()/"locks") : _pimpl->locks_file );
550 ///////////////////////////////////////////////////////////////////
552 bool ZConfig::repo_add_probe() const
554 return _pimpl->repo_add_probe;
557 unsigned ZConfig::repo_refresh_delay() const
559 return _pimpl->repo_refresh_delay;
562 bool ZConfig::download_use_deltarpm() const
563 { return _pimpl->download_use_deltarpm; }
565 bool ZConfig::download_use_deltarpm_always() const
566 { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
568 long ZConfig::download_max_concurrent_connections() const
569 { return _pimpl->download_max_concurrent_connections; }
571 long ZConfig::download_min_download_speed() const
572 { return _pimpl->download_min_download_speed; }
574 long ZConfig::download_max_download_speed() const
575 { return _pimpl->download_max_download_speed; }
577 long ZConfig::download_max_silent_tries() const
578 { return _pimpl->download_max_silent_tries; }
580 bool ZConfig::solver_onlyRequires() const
581 { return _pimpl->solver_onlyRequires; }
583 Pathname ZConfig::solver_checkSystemFile() const
584 { return _pimpl->solver_checkSystemFile; }
586 std::set<IdString> ZConfig::multiversion() const
587 { return _pimpl->multiversion; }
589 void ZConfig::addMultiversion(std::string &name)
590 { _pimpl->multiversion.insert(IdString(name)); }
592 bool ZConfig::removeMultiversion(std::string &name)
593 { return _pimpl->multiversion.erase(IdString(name)); }
595 bool ZConfig::apply_locks_file() const
597 return _pimpl->apply_locks_file;
600 Pathname ZConfig::update_dataPath() const
602 return ( _pimpl->update_data_path.empty()
603 ? Pathname("/var/adm") : _pimpl->update_data_path );
606 Pathname ZConfig::update_messagesPath() const
608 return ( _pimpl->update_messages_path.empty()
609 ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
613 Pathname ZConfig::update_scriptsPath() const
615 return ( _pimpl->update_scripts_path.empty()
616 ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
619 ///////////////////////////////////////////////////////////////////
621 target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
622 { return _pimpl->rpmInstallFlags; }
625 Pathname ZConfig::historyLogFile() const
627 return ( _pimpl->history_log_path.empty() ?
628 Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
632 Pathname ZConfig::credentialsGlobalDir() const
634 return ( _pimpl->credentials_global_dir_path.empty() ?
635 Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
638 Pathname ZConfig::credentialsGlobalFile() const
640 return ( _pimpl->credentials_global_file_path.empty() ?
641 Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
644 ///////////////////////////////////////////////////////////////////
646 std::ostream & ZConfig::about( std::ostream & str ) const
648 str << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
649 str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
650 str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
651 str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
655 /////////////////////////////////////////////////////////////////
657 ///////////////////////////////////////////////////////////////////