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 ///////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
39 { /////////////////////////////////////////////////////////////////
41 /** Determine system architecture evaluating \c uname and \c /proc/cpuinfo.
43 Arch _autodetectSystemArchitecture()
46 if ( ::uname( &buf ) < 0 )
48 ERR << "Can't determine system architecture" << endl;
52 Arch architecture( buf.machine );
53 MIL << "Uname architecture is '" << buf.machine << "'" << endl;
55 // some CPUs report i686 but dont implement cx8 and cmov
56 // check for both flags in /proc/cpuinfo and downgrade
57 // to i586 if either is missing (cf bug #18885)
58 if ( architecture == Arch_i686 )
60 std::ifstream cpuinfo( "/proc/cpuinfo" );
63 for( iostr::EachLine in( cpuinfo ); in; in.next() )
65 if ( str::hasPrefix( *in, "flags" ) )
67 if ( in->find( "cx8" ) == std::string::npos
68 || in->find( "cmov" ) == std::string::npos )
70 architecture = Arch_i586;
71 WAR << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
79 ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
85 /** The locale to be used for texts and messages.
87 * For the encoding to be used the preference is
89 * LC_ALL, LC_CTYPE, LANG
91 * For the language of the messages to be used, the preference is
93 * LANGUAGE, LC_ALL, LC_MESSAGES, LANG
95 * Note that LANGUAGE can contain more than one locale name, it can be
96 * a list of locale names like for example
98 * LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
100 * \todo Support dynamic fallbacklists defined by LANGUAGE
102 Locale _autodetectTextLocale()
105 const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
106 for ( const char ** envvar = envlist; *envvar; ++envvar )
108 const char * envlang = getenv( *envvar );
111 std::string envstr( envlang );
112 if ( envstr != "POSIX" && envstr != "C" )
114 Locale lang( envstr );
115 if ( ! lang.code().empty() )
117 MIL << "Found " << *envvar << "=" << envstr << endl;
124 MIL << "Default text locale is '" << ret << "'" << endl;
128 /////////////////////////////////////////////////////////////////
130 ///////////////////////////////////////////////////////////////////
132 ///////////////////////////////////////////////////////////////////
134 // CLASS NAME : ZConfig::Impl
136 /** ZConfig implementation.
137 * \todo Enrich section and entry definition by some comment
138 * (including the default setting and provide some method to
139 * write this into a sample zypp.conf.
144 Impl( const Pathname & override_r = Pathname() )
145 : cfg_arch ( defaultSystemArchitecture() )
146 , cfg_textLocale ( defaultTextLocale() )
147 , repo_add_probe ( false )
148 , repo_refresh_delay ( 10 )
149 , download_use_patchrpm ( true )
150 , download_use_deltarpm ( true )
151 , solver_onlyRequires ( false )
152 , apply_locks_file ( true )
155 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
157 // override_r has higest prio
158 // ZYPP_CONF might override /etc/zypp/zypp.conf
159 Pathname confpath( override_r );
160 if ( confpath.empty() )
162 const char *env_confpath = getenv( "ZYPP_CONF" );
163 confpath = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
167 // Inject this into ZConfig. Be shure this is
168 // allocated via new. See: reconfigureZConfig
169 INT << "Reconfigure to " << confpath << endl;
170 ZConfig::instance()._pimpl.reset( this );
172 if ( PathInfo(confpath).isExist() )
174 parser::IniDict dict( confpath );
175 //InputStream is(confpath);
177 for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
178 sit != dict.sectionsEnd();
181 string section(*sit);
182 //MIL << section << endl;
183 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
184 it != dict.entriesEnd(*sit);
187 string entry(it->first);
188 string value(it->second);
189 //DBG << (*it).first << "=" << (*it).second << endl;
190 if ( section == "main" )
192 if ( entry == "arch" )
195 if ( carch != cfg_arch )
197 WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
201 else if ( entry == "cachedir" )
203 cfg_cache_path = Pathname(value);
205 else if ( entry == "metadatadir" )
207 cfg_metadata_path = Pathname(value);
209 else if ( entry == "solvfilesdir" )
211 cfg_solvfiles_path = Pathname(value);
213 else if ( entry == "packagesdir" )
215 cfg_packages_path = Pathname(value);
217 else if ( entry == "configdir" )
219 cfg_config_path = Pathname(value);
221 else if ( entry == "reposdir" )
223 cfg_known_repos_path = Pathname(value);
225 else if ( entry == "repo.add.probe" )
227 repo_add_probe = str::strToBool( value, repo_add_probe );
229 else if ( entry == "repo.refresh.delay" )
231 str::strtonum(value, repo_refresh_delay);
233 else if ( entry == "download.use_patchrpm" )
235 download_use_patchrpm = str::strToBool( value, download_use_patchrpm );
237 else if ( entry == "download.use_deltarpm" )
239 download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
241 else if ( entry == "vendordir" )
243 cfg_vendor_path = Pathname(value);
245 else if ( entry == "productsdir" )
247 cfg_products_path = Pathname(value);
249 else if ( entry == "solver.onlyRequires" )
251 solver_onlyRequires = str::strToBool( value, solver_onlyRequires );
253 else if ( entry == "locksfile.path" )
255 locks_file = Pathname(value);
257 else if ( entry == "locksfile.apply" )
259 apply_locks_file = str::strToBool( value, apply_locks_file );
261 else if ( entry == "update.datadir" )
263 update_data_path = Pathname(value);
265 else if ( entry == "update.scriptsdir" )
267 update_scripts_path = Pathname(value);
269 else if ( entry == "update.messagessdir" )
271 update_messages_path = Pathname(value);
280 MIL << confpath << " not found, using defaults instead." << endl;
284 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
286 Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
287 if ( carch != cfg_arch )
289 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
294 MIL << "ZConfig singleton created." << endl;
295 MIL << "defaultTextLocale: '" << cfg_textLocale << "'" << endl;
296 MIL << "System architecture is '" << cfg_arch << "'" << endl;
304 Locale cfg_textLocale;
306 Pathname cfg_cache_path;
307 Pathname cfg_metadata_path;
308 Pathname cfg_solvfiles_path;
309 Pathname cfg_packages_path;
311 Pathname cfg_config_path;
312 Pathname cfg_known_repos_path;
313 Pathname cfg_vendor_path;
314 Pathname cfg_products_path;
317 Pathname update_data_path;
318 Pathname update_scripts_path;
319 Pathname update_messages_path;
322 unsigned repo_refresh_delay;
324 bool download_use_patchrpm;
325 bool download_use_deltarpm;
327 bool solver_onlyRequires;
329 bool apply_locks_file;
332 ///////////////////////////////////////////////////////////////////
334 // Backdoor to redirect ZConfig from within the running
335 // TEST-application. HANDLE WITH CARE!
336 void reconfigureZConfig( const Pathname & override_r )
338 // ctor puts itself unter smart pointer control.
339 new ZConfig::Impl( override_r );
342 ///////////////////////////////////////////////////////////////////
344 // METHOD NAME : ZConfig::instance
345 // METHOD TYPE : ZConfig &
347 ZConfig & ZConfig::instance()
349 static ZConfig _instance; // The singleton
353 ///////////////////////////////////////////////////////////////////
355 // METHOD NAME : ZConfig::ZConfig
356 // METHOD TYPE : Ctor
362 ///////////////////////////////////////////////////////////////////
364 // METHOD NAME : ZConfig::~ZConfig
365 // METHOD TYPE : Dtor
370 ///////////////////////////////////////////////////////////////////
372 // system architecture
374 ///////////////////////////////////////////////////////////////////
376 Arch ZConfig::defaultSystemArchitecture()
378 static Arch _val( _autodetectSystemArchitecture() );
382 Arch ZConfig::systemArchitecture() const
383 { return _pimpl->cfg_arch; }
385 void ZConfig::setSystemArchitecture( const Arch & arch_r )
387 if ( arch_r != _pimpl->cfg_arch )
389 WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
390 _pimpl->cfg_arch = arch_r;
394 ///////////////////////////////////////////////////////////////////
398 ///////////////////////////////////////////////////////////////////
400 Locale ZConfig::defaultTextLocale()
402 static Locale _val( _autodetectTextLocale() );
406 Locale ZConfig::textLocale() const
407 { return _pimpl->cfg_textLocale; }
409 void ZConfig::setTextLocale( const Locale & locale_r )
411 if ( locale_r != _pimpl->cfg_textLocale )
413 WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
414 _pimpl->cfg_textLocale = locale_r;
418 ///////////////////////////////////////////////////////////////////
420 Pathname ZConfig::repoCachePath() const
422 return ( _pimpl->cfg_cache_path.empty()
423 ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
426 Pathname ZConfig::repoMetadataPath() const
428 return ( _pimpl->cfg_metadata_path.empty()
429 ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
432 Pathname ZConfig::repoSolvfilesPath() const
434 return ( _pimpl->cfg_solvfiles_path.empty()
435 ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
438 Pathname ZConfig::repoPackagesPath() const
440 return ( _pimpl->cfg_packages_path.empty()
441 ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
444 ///////////////////////////////////////////////////////////////////
446 Pathname ZConfig::configPath() const
448 return ( _pimpl->cfg_config_path.empty()
449 ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
452 Pathname ZConfig::knownReposPath() const
454 return ( _pimpl->cfg_known_repos_path.empty()
455 ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
457 Pathname ZConfig::vendorPath() const
459 return ( _pimpl->cfg_vendor_path.empty()
460 ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
463 Pathname ZConfig::productsPath() const
465 return ( _pimpl->cfg_products_path.empty()
466 ? (configPath()/"products.d") : _pimpl->cfg_products_path );
469 Pathname ZConfig::locksFile() const
471 return ( _pimpl->locks_file.empty()
472 ? (configPath()/"locks") : _pimpl->locks_file );
475 ///////////////////////////////////////////////////////////////////
477 const std::string & ZConfig::cacheDBSplitJoinSeparator() const
479 static std::string s("!@$");
483 bool ZConfig::repo_add_probe() const
485 return _pimpl->repo_add_probe;
488 unsigned ZConfig::repo_refresh_delay() const
490 return _pimpl->repo_refresh_delay;
493 bool ZConfig::download_use_patchrpm() const
494 { return _pimpl->download_use_patchrpm; }
496 bool ZConfig::download_use_deltarpm() const
497 { return _pimpl->download_use_deltarpm; }
500 bool ZConfig::solver_onlyRequires() const
501 { return _pimpl->solver_onlyRequires; }
503 bool ZConfig::apply_locks_file() const
505 return _pimpl->apply_locks_file;
508 Pathname ZConfig::update_dataPath() const
510 return ( _pimpl->update_data_path.empty()
511 ? Pathname("/var/adm") : _pimpl->update_data_path );
514 Pathname ZConfig::update_messagesPath() const
516 return ( _pimpl->update_messages_path.empty()
517 ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
521 Pathname ZConfig::update_scriptsPath() const
523 return ( _pimpl->update_scripts_path.empty()
524 ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
527 /////////////////////////////////////////////////////////////////
529 ///////////////////////////////////////////////////////////////////