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. */
142 typedef _Tp value_type;
144 /** No default ctor, explicit initialisation! */
145 Option( const value_type & initial_r )
149 /** Get the value. */
150 const value_type & get() const
153 /** Autoconversion to value_type. */
154 operator const value_type &() const
157 /** Set a new value. */
158 void set( const value_type & newval_r )
161 /** Non-const reference to set a new value. */
169 /** Mutable option with initial value also remembering a config value. */
171 struct DefaultOption : public Option<_Tp>
173 typedef _Tp value_type;
174 typedef Option<_Tp> option_type;
176 DefaultOption( const value_type & initial_r )
177 : Option<_Tp>( initial_r ), _default( initial_r )
180 /** Reset value to the current default. */
181 void restoreToDefault()
182 { this->set( _default.get() ); }
184 /** Reset value to a new default. */
185 void restoreToDefault( const value_type & newval_r )
186 { setDefault( newval_r ); restoreToDefault(); }
188 /** Get the current default value. */
189 const value_type & getDefault() const
190 { return _default.get(); }
192 /** Set a new default value. */
193 void setDefault( const value_type & newval_r )
194 { _default.set( newval_r ); }
197 option_type _default;
200 ///////////////////////////////////////////////////////////////////
202 // CLASS NAME : ZConfig::Impl
204 /** ZConfig implementation.
205 * \todo Enrich section and entry definition by some comment
206 * (including the default setting and provide some method to
207 * write this into a sample zypp.conf.
212 Impl( const Pathname & override_r = Pathname() )
213 : _parsedZyppConf ( override_r )
214 , cfg_arch ( defaultSystemArchitecture() )
215 , cfg_textLocale ( defaultTextLocale() )
216 , updateMessagesNotify ( "single | /usr/lib/zypp/notify-message -p %p" )
217 , repo_add_probe ( false )
218 , repo_refresh_delay ( 10 )
219 , download_use_deltarpm ( true )
220 , download_use_deltarpm_always ( false )
221 , download_media_prefer_download( true )
222 , download_max_concurrent_connections( 2 )
223 , download_min_download_speed ( 0 )
224 , download_max_download_speed ( 0 )
225 , download_max_silent_tries ( 5 )
226 , commit_downloadMode ( DownloadDefault )
227 , solver_onlyRequires ( false )
228 , solver_allowVendorChange ( false )
229 , solver_upgradeTestcasesToKeep ( 2 )
230 , solverUpgradeRemoveDropedPackages( true )
231 , apply_locks_file ( true )
234 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
235 // override_r has higest prio
236 // ZYPP_CONF might override /etc/zypp/zypp.conf
237 if ( _parsedZyppConf.empty() )
239 const char *env_confpath = getenv( "ZYPP_CONF" );
240 _parsedZyppConf = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
244 // Inject this into ZConfig. Be shure this is
245 // allocated via new. See: reconfigureZConfig
246 INT << "Reconfigure to " << _parsedZyppConf << endl;
247 ZConfig::instance()._pimpl.reset( this );
249 if ( PathInfo(_parsedZyppConf).isExist() )
251 parser::IniDict dict( _parsedZyppConf );
252 for ( IniDict::section_const_iterator sit = dict.sectionsBegin();
253 sit != dict.sectionsEnd();
256 string section(*sit);
257 //MIL << section << endl;
258 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit);
259 it != dict.entriesEnd(*sit);
262 string entry(it->first);
263 string value(it->second);
264 //DBG << (*it).first << "=" << (*it).second << endl;
265 if ( section == "main" )
267 if ( entry == "arch" )
270 if ( carch != cfg_arch )
272 WAR << "Overriding system architecture (" << cfg_arch << "): " << carch << endl;
276 else if ( entry == "cachedir" )
278 cfg_cache_path = Pathname(value);
280 else if ( entry == "metadatadir" )
282 cfg_metadata_path = Pathname(value);
284 else if ( entry == "solvfilesdir" )
286 cfg_solvfiles_path = Pathname(value);
288 else if ( entry == "packagesdir" )
290 cfg_packages_path = Pathname(value);
292 else if ( entry == "configdir" )
294 cfg_config_path = Pathname(value);
296 else if ( entry == "reposdir" )
298 cfg_known_repos_path = Pathname(value);
300 else if ( entry == "servicesdir" )
302 cfg_known_services_path = Pathname(value);
304 else if ( entry == "repo.add.probe" )
306 repo_add_probe = str::strToBool( value, repo_add_probe );
308 else if ( entry == "repo.refresh.delay" )
310 str::strtonum(value, repo_refresh_delay);
312 else if ( entry == "download.use_deltarpm" )
314 download_use_deltarpm = str::strToBool( value, download_use_deltarpm );
316 else if ( entry == "download.use_deltarpm.always" )
318 download_use_deltarpm_always = str::strToBool( value, download_use_deltarpm_always );
320 else if ( entry == "download.media_preference" )
322 download_media_prefer_download.restoreToDefault( str::compareCI( value, "volatile" ) != 0 );
324 else if ( entry == "download.max_concurrent_connections" )
326 str::strtonum(value, download_max_concurrent_connections);
328 else if ( entry == "download.min_download_speed" )
330 str::strtonum(value, download_min_download_speed);
332 else if ( entry == "download.max_download_speed" )
334 str::strtonum(value, download_max_download_speed);
336 else if ( entry == "download.max_silent_tries" )
338 str::strtonum(value, download_max_silent_tries);
340 else if ( entry == "commit.downloadMode" )
342 commit_downloadMode.set( deserializeDownloadMode( value ) );
344 else if ( entry == "vendordir" )
346 cfg_vendor_path = Pathname(value);
348 else if ( entry == "solver.onlyRequires" )
350 solver_onlyRequires.set( str::strToBool( value, solver_onlyRequires ) );
352 else if ( entry == "solver.allowVendorChange" )
354 solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange ) );
356 else if ( entry == "solver.upgradeTestcasesToKeep" )
358 solver_upgradeTestcasesToKeep.set( str::strtonum<unsigned>( value ) );
360 else if ( entry == "solver.upgradeRemoveDropedPackages" )
362 solverUpgradeRemoveDropedPackages.restoreToDefault( str::strToBool( value, solverUpgradeRemoveDropedPackages.getDefault() ) );
364 else if ( entry == "solver.checkSystemFile" )
366 solver_checkSystemFile = Pathname(value);
368 else if ( entry == "multiversion" )
370 std::list<std::string> multi;
371 str::split( value, back_inserter(multi), ", \t" );
372 for ( std::list<string>::const_iterator it = multi.begin();
373 it != multi.end(); it++) {
374 multiversion.insert (IdString(*it));
377 else if ( entry == "locksfile.path" )
379 locks_file = Pathname(value);
381 else if ( entry == "locksfile.apply" )
383 apply_locks_file = str::strToBool( value, apply_locks_file );
385 else if ( entry == "update.datadir" )
387 update_data_path = Pathname(value);
389 else if ( entry == "update.scriptsdir" )
391 update_scripts_path = Pathname(value);
393 else if ( entry == "update.messagessdir" )
395 update_messages_path = Pathname(value);
397 else if ( entry == "update.messages.notify" )
399 updateMessagesNotify.set( value );
401 else if ( entry == "rpm.install.excludedocs" )
403 rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS,
404 str::strToBool( value, false ) );
406 else if ( entry == "history.logfile" )
408 history_log_path = Pathname(value);
410 else if ( entry == "credentials.global.dir" )
412 credentials_global_dir_path = Pathname(value);
414 else if ( entry == "credentials.global.file" )
416 credentials_global_file_path = Pathname(value);
424 MIL << _parsedZyppConf << " not found, using defaults instead." << endl;
425 _parsedZyppConf = _parsedZyppConf.extend( " (NOT FOUND)" );
429 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
431 Arch carch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
432 if ( carch != cfg_arch )
434 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Overriding system architecture (" << cfg_arch << "): " << carch << endl;
438 MIL << "ZConfig singleton created." << endl;
445 /** Remember any parsed zypp.conf. */
446 Pathname _parsedZyppConf;
449 Locale cfg_textLocale;
451 Pathname cfg_cache_path;
452 Pathname cfg_metadata_path;
453 Pathname cfg_solvfiles_path;
454 Pathname cfg_packages_path;
456 Pathname cfg_config_path;
457 Pathname cfg_known_repos_path;
458 Pathname cfg_known_services_path;
459 Pathname cfg_vendor_path;
462 Pathname update_data_path;
463 Pathname update_scripts_path;
464 Pathname update_messages_path;
465 DefaultOption<std::string> updateMessagesNotify;
468 unsigned repo_refresh_delay;
470 bool download_use_deltarpm;
471 bool download_use_deltarpm_always;
472 DefaultOption<bool> download_media_prefer_download;
474 int download_max_concurrent_connections;
475 int download_min_download_speed;
476 int download_max_download_speed;
477 int download_max_silent_tries;
479 Option<DownloadMode> commit_downloadMode;
481 Option<bool> solver_onlyRequires;
482 Option<bool> solver_allowVendorChange;
483 Option<unsigned> solver_upgradeTestcasesToKeep;
484 DefaultOption<bool> solverUpgradeRemoveDropedPackages;
486 Pathname solver_checkSystemFile;
488 std::set<IdString> multiversion;
490 bool apply_locks_file;
492 target::rpm::RpmInstFlags rpmInstallFlags;
494 Pathname history_log_path;
495 Pathname credentials_global_dir_path;
496 Pathname credentials_global_file_path;
498 ///////////////////////////////////////////////////////////////////
500 // Backdoor to redirect ZConfig from within the running
501 // TEST-application. HANDLE WITH CARE!
502 void reconfigureZConfig( const Pathname & override_r )
504 // ctor puts itself unter smart pointer control.
505 new ZConfig::Impl( override_r );
508 ///////////////////////////////////////////////////////////////////
510 // METHOD NAME : ZConfig::instance
511 // METHOD TYPE : ZConfig &
513 ZConfig & ZConfig::instance()
515 static ZConfig _instance; // The singleton
519 ///////////////////////////////////////////////////////////////////
521 // METHOD NAME : ZConfig::ZConfig
522 // METHOD TYPE : Ctor
530 ///////////////////////////////////////////////////////////////////
532 // METHOD NAME : ZConfig::~ZConfig
533 // METHOD TYPE : Dtor
538 Pathname ZConfig::systemRoot() const
540 Target_Ptr target( getZYpp()->getTarget() );
541 return target ? target->root() : Pathname();
544 ///////////////////////////////////////////////////////////////////
546 // system architecture
548 ///////////////////////////////////////////////////////////////////
550 Arch ZConfig::defaultSystemArchitecture()
552 static Arch _val( _autodetectSystemArchitecture() );
556 Arch ZConfig::systemArchitecture() const
557 { return _pimpl->cfg_arch; }
559 void ZConfig::setSystemArchitecture( const Arch & arch_r )
561 if ( arch_r != _pimpl->cfg_arch )
563 WAR << "Overriding system architecture (" << _pimpl->cfg_arch << "): " << arch_r << endl;
564 _pimpl->cfg_arch = arch_r;
568 ///////////////////////////////////////////////////////////////////
572 ///////////////////////////////////////////////////////////////////
574 Locale ZConfig::defaultTextLocale()
576 static Locale _val( _autodetectTextLocale() );
580 Locale ZConfig::textLocale() const
581 { return _pimpl->cfg_textLocale; }
583 void ZConfig::setTextLocale( const Locale & locale_r )
585 if ( locale_r != _pimpl->cfg_textLocale )
587 WAR << "Overriding text locale (" << _pimpl->cfg_textLocale << "): " << locale_r << endl;
588 _pimpl->cfg_textLocale = locale_r;
592 ///////////////////////////////////////////////////////////////////
594 Pathname ZConfig::repoCachePath() const
596 return ( _pimpl->cfg_cache_path.empty()
597 ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
600 Pathname ZConfig::repoMetadataPath() const
602 return ( _pimpl->cfg_metadata_path.empty()
603 ? (repoCachePath()/"raw") : _pimpl->cfg_metadata_path );
606 Pathname ZConfig::repoSolvfilesPath() const
608 return ( _pimpl->cfg_solvfiles_path.empty()
609 ? (repoCachePath()/"solv") : _pimpl->cfg_solvfiles_path );
612 Pathname ZConfig::repoPackagesPath() const
614 return ( _pimpl->cfg_packages_path.empty()
615 ? (repoCachePath()/"packages") : _pimpl->cfg_packages_path );
618 ///////////////////////////////////////////////////////////////////
620 Pathname ZConfig::configPath() const
622 return ( _pimpl->cfg_config_path.empty()
623 ? Pathname("/etc/zypp") : _pimpl->cfg_config_path );
626 Pathname ZConfig::knownReposPath() const
628 return ( _pimpl->cfg_known_repos_path.empty()
629 ? (configPath()/"repos.d") : _pimpl->cfg_known_repos_path );
632 Pathname ZConfig::knownServicesPath() const
634 return ( _pimpl->cfg_known_services_path.empty()
635 ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
638 Pathname ZConfig::vendorPath() const
640 return ( _pimpl->cfg_vendor_path.empty()
641 ? (configPath()/"vendors.d") : _pimpl->cfg_vendor_path );
644 Pathname ZConfig::locksFile() const
646 return ( _pimpl->locks_file.empty()
647 ? (configPath()/"locks") : _pimpl->locks_file );
650 ///////////////////////////////////////////////////////////////////
652 bool ZConfig::repo_add_probe() const
654 return _pimpl->repo_add_probe;
657 unsigned ZConfig::repo_refresh_delay() const
659 return _pimpl->repo_refresh_delay;
662 bool ZConfig::download_use_deltarpm() const
663 { return _pimpl->download_use_deltarpm; }
665 bool ZConfig::download_use_deltarpm_always() const
666 { return download_use_deltarpm() && _pimpl->download_use_deltarpm_always; }
668 bool ZConfig::download_media_prefer_download() const
669 { return _pimpl->download_media_prefer_download; }
671 void ZConfig::set_download_media_prefer_download( bool yesno_r )
672 { _pimpl->download_media_prefer_download.set( yesno_r ); }
674 void ZConfig::set_default_download_media_prefer_download()
675 { _pimpl->download_media_prefer_download.restoreToDefault(); }
677 long ZConfig::download_max_concurrent_connections() const
678 { return _pimpl->download_max_concurrent_connections; }
680 long ZConfig::download_min_download_speed() const
681 { return _pimpl->download_min_download_speed; }
683 long ZConfig::download_max_download_speed() const
684 { return _pimpl->download_max_download_speed; }
686 long ZConfig::download_max_silent_tries() const
687 { return _pimpl->download_max_silent_tries; }
689 DownloadMode ZConfig::commit_downloadMode() const
690 { return _pimpl->commit_downloadMode; }
692 bool ZConfig::solver_onlyRequires() const
693 { return _pimpl->solver_onlyRequires; }
695 bool ZConfig::solver_allowVendorChange() const
696 { return _pimpl->solver_allowVendorChange; }
698 Pathname ZConfig::solver_checkSystemFile() const
699 { return ( _pimpl->solver_checkSystemFile.empty()
700 ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
702 unsigned ZConfig::solver_upgradeTestcasesToKeep() const
703 { return _pimpl->solver_upgradeTestcasesToKeep; }
705 bool ZConfig::solverUpgradeRemoveDropedPackages() const { return _pimpl->solverUpgradeRemoveDropedPackages; }
706 void ZConfig::setSolverUpgradeRemoveDropedPackages( bool val_r ) { _pimpl->solverUpgradeRemoveDropedPackages.set( val_r ); }
707 void ZConfig::resetSolverUpgradeRemoveDropedPackages() { _pimpl->solverUpgradeRemoveDropedPackages.restoreToDefault(); }
709 std::set<IdString> ZConfig::multiversion() const
710 { return _pimpl->multiversion; }
712 void ZConfig::addMultiversion(std::string &name)
713 { _pimpl->multiversion.insert(IdString(name)); }
715 bool ZConfig::removeMultiversion(std::string &name)
716 { return _pimpl->multiversion.erase(IdString(name)); }
718 bool ZConfig::apply_locks_file() const
719 { return _pimpl->apply_locks_file; }
721 Pathname ZConfig::update_dataPath() const
723 return ( _pimpl->update_data_path.empty()
724 ? Pathname("/var/adm") : _pimpl->update_data_path );
727 Pathname ZConfig::update_messagesPath() const
729 return ( _pimpl->update_messages_path.empty()
730 ? Pathname(update_dataPath()/"update-messages") : _pimpl->update_messages_path );
733 Pathname ZConfig::update_scriptsPath() const
735 return ( _pimpl->update_scripts_path.empty()
736 ? Pathname(update_dataPath()/"update-scripts") : _pimpl->update_scripts_path );
739 std::string ZConfig::updateMessagesNotify() const
740 { return _pimpl->updateMessagesNotify; }
742 void ZConfig::setUpdateMessagesNotify( const std::string & val_r )
743 { _pimpl->updateMessagesNotify.set( val_r ); }
745 void ZConfig::resetUpdateMessagesNotify()
746 { _pimpl->updateMessagesNotify.restoreToDefault(); }
748 ///////////////////////////////////////////////////////////////////
750 target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
751 { return _pimpl->rpmInstallFlags; }
754 Pathname ZConfig::historyLogFile() const
756 return ( _pimpl->history_log_path.empty() ?
757 Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
761 Pathname ZConfig::credentialsGlobalDir() const
763 return ( _pimpl->credentials_global_dir_path.empty() ?
764 Pathname("/etc/zypp/credentials.d") : _pimpl->credentials_global_dir_path );
767 Pathname ZConfig::credentialsGlobalFile() const
769 return ( _pimpl->credentials_global_file_path.empty() ?
770 Pathname("/etc/zypp/credentials.cat") : _pimpl->credentials_global_file_path );
773 ///////////////////////////////////////////////////////////////////
775 std::string ZConfig::distroverpkg() const
776 { return "redhat-release"; }
778 ///////////////////////////////////////////////////////////////////
780 std::ostream & ZConfig::about( std::ostream & str ) const
782 str << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
784 str << "satsolver: " << sat_version;
785 if ( ::strcmp( sat_version, SATSOLVER_VERSION_STRING ) )
786 str << " (built against " << SATSOLVER_VERSION_STRING << ")";
789 str << "zypp.conf: '" << _pimpl->_parsedZyppConf << "'" << endl;
790 str << "TextLocale: '" << textLocale() << "' (" << defaultTextLocale() << ")" << endl;
791 str << "SystemArchitecture: '" << systemArchitecture() << "' (" << defaultSystemArchitecture() << ")" << endl;
795 /////////////////////////////////////////////////////////////////
797 ///////////////////////////////////////////////////////////////////