1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/zypp_detail/ZYppImpl.cc
13 #include <sys/utsname.h>
17 #include "zypp/TmpPath.h"
18 #include "zypp/base/Logger.h"
19 #include "zypp/base/String.h"
21 #include "zypp/zypp_detail/ZYppImpl.h"
22 #include "zypp/solver/detail/Helper.h"
23 #include "zypp/target/TargetImpl.h"
24 #include "zypp/ZYpp.h"
25 #include "zypp/NVRAD.h"
26 #include "zypp/Language.h"
27 #include "zypp/DiskUsageCounter.h"
28 #include "zypp/NameKindProxy.h"
29 #include "zypp/Locks.h"
33 ///////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////
38 { /////////////////////////////////////////////////////////////////
40 /** The locale to be used for texts and messages.
42 * For the encoding to be used the preference is
44 * LC_ALL, LC_CTYPE, LANG
46 * For the language of the messages to be used, the preference is
48 * LANGUAGE, LC_ALL, LC_MESSAGES, LANG
50 * Note that LANGUAGE can contain more than one locale name, it can be
51 * a list of locale names like for example
53 * LANGUAGE=ja_JP.UTF-8:de_DE.UTF-8:fr_FR.UTF-8
55 * \todo Support dynamic fallbacklists defined by LANGUAGE
57 inline Locale defaultTextLocale()
60 const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
61 for ( const char ** envvar = envlist; *envvar; ++envvar )
63 const char * envlang = getenv( *envvar );
66 std::string envstr( envlang );
67 if ( envstr != "POSIX" && envstr != "C" )
69 Locale lang( envlang );
70 if ( ! lang.code().empty() )
81 Arch defaultArchitecture()
85 // detect the true architecture
87 if ( uname( &buf ) < 0 )
89 ERR << "Can't determine system architecture" << endl;
93 architecture = Arch( buf.machine );
94 DBG << "uname architecture is '" << buf.machine << "'" << endl;
96 // some CPUs report i686 but dont implement cx8 and cmov
97 // check for both flags in /proc/cpuinfo and downgrade
98 // to i586 if either is missing (cf bug #18885)
100 if ( architecture == Arch_i686 )
102 std::ifstream cpuinfo( "/proc/cpuinfo" );
105 ERR << "Cant open /proc/cpuinfo" << endl;
110 while ( cpuinfo.good() )
112 if ( !cpuinfo.getline( infoline, 1024, '\n' ) )
117 if ( strncmp( infoline, "flags", 5 ) == 0 )
119 std::string flagsline( infoline );
120 if ( flagsline.find( "cx8" ) == std::string::npos
121 || flagsline.find( "cmov" ) == std::string::npos )
123 architecture = Arch_i586;
124 DBG << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
128 } // read proc/cpuinfo
129 } // proc/cpuinfo opened
130 } // i686 extra flags check
133 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
135 architecture = Arch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
136 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses to: '" << architecture << "'" << endl;
141 ///////////////////////////////////////////////////////////////////
143 // METHOD NAME : ZYppImpl::ZYppImpl
144 // METHOD TYPE : Constructor
147 : _textLocale( defaultTextLocale() )
150 , _resolver( new Resolver(_pool.accessor()) )
151 , _architecture( defaultArchitecture() )
153 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
154 MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
155 MIL << "System architecture is '" << _architecture << "'" << endl;
157 MIL << "initializing keyring..." << std::endl;
158 //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
159 _keyring = new KeyRing(tmpPath());
162 ///////////////////////////////////////////////////////////////////
164 // METHOD NAME : ZYppImpl::~ZYppImpl
165 // METHOD TYPE : Destructor
167 ZYppImpl::~ZYppImpl()
170 //------------------------------------------------------------------------
171 // add/remove resolvables
173 void ZYppImpl::addResolvables (const ResStore& store, bool installed)
175 _pool.insert(store.begin(), store.end(), installed);
178 void ZYppImpl::removeResolvables (const ResStore& store)
180 for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
186 void ZYppImpl::removeInstalledResolvables ()
188 for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
190 ResPool::const_iterator next = it; ++next;
191 if (it->status().isInstalled())
197 DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
201 setPartitions( DiskUsageCounter::detectMountPoints() );
203 return _disk_usage->disk_usage(pool());
206 void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
208 _disk_usage.reset(new DiskUsageCounter());
209 _disk_usage->setMountPoints(mp);
212 DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
215 return _disk_usage->getMountPoints();
217 return DiskUsageCounter::detectMountPoints();
220 //------------------------------------------------------------------------
223 Target_Ptr ZYppImpl::target() const
226 ZYPP_THROW(Exception("Target not initialized."));
230 void ZYppImpl::initializeTarget(const Pathname & root)
232 MIL << "initTarget( " << root << endl;
234 if (_target->root() == root) {
235 MIL << "Repeated call to initializeTarget()" << endl;
238 removeInstalledResolvables( );
240 _target = new Target( root );
241 _target->enableStorage( root );
244 void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
246 MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
248 if (_target->root() == root) {
249 MIL << "Repeated call to initTarget()" << endl;
252 removeInstalledResolvables( );
254 _target = new Target( root );
255 _target->enableStorage( root );
258 addResolvables( _target->resolvables(), true );
263 void ZYppImpl::finishTarget()
266 removeInstalledResolvables();
270 //------------------------------------------------------------------------
273 /** \todo Remove workflow from target, lot's of it could be done here,
274 * and target used for transact. */
275 ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
277 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
279 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
282 MIL << "Attempt to commit (" << policy_r << ")" << endl;
284 ZYPP_THROW( Exception("Target not initialized.") );
286 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
288 if (! policy_r.dryRun() ) {
289 // Tag target data invalid, so they are reloaded on the next call to
290 // target->resolvables(). Actually the target should do this without
293 removeInstalledResolvables();
294 if ( policy_r.syncPoolAfterCommit() )
296 // reload new status from target
297 addResolvables( _target->resolvables(), true );
301 MIL << "Commit (" << policy_r << ") returned: "
306 void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
309 ZYPP_THROW( Exception("Target not initialized.") );
310 _target->_pimpl->installSrcPackage( srcPackage_r );
313 //------------------------------------------------------------------------
317 void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
319 ResPool mpool( pool() );
320 // assert all requested are available
321 for ( LocaleSet::const_iterator it = locales_r.begin();
322 it != locales_r.end(); ++it )
324 NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
325 if ( select.installedEmpty() && select.availableEmpty() )
326 _pool.insert( Language::availableInstance( *it ) );
330 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
331 it != mpool.byKindEnd<Language>(); ++it )
333 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
334 if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
336 // Language is requested
337 if ( select.installedEmpty() )
339 if ( select.availableEmpty() )
341 // no item ==> provide available to install
342 _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
343 select = nameKindProxy<Language>( mpool, (*it)->name() );
345 // available only ==> to install
346 select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
350 // installed ==> keep it
351 select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
352 if ( ! select.availableEmpty() )
354 // both items ==> keep
355 select.availableBegin()->status().resetTransact( ResStatus::USER );
361 // Language is NOT requested
362 if ( ! select.installedEmpty() )
363 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
364 if ( ! select.availableEmpty() )
365 select.availableBegin()->status().resetTransact( ResStatus::USER );
371 ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
374 ResPool mpool( pool() );
375 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
376 it != mpool.byKindEnd<Language>(); ++it )
378 if ( (*it).status().isUninstalled() ) // available!
379 ret.insert( Locale( (*it)->name() ) );
385 ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
388 ResPool mpool( pool() );
389 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
390 it != mpool.byKindEnd<Language>(); ++it )
392 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
393 if ( ! select.installedEmpty()
394 && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
395 ret.insert( Locale( (*it)->name() ) );
396 else if ( ! select.availableEmpty()
397 && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
398 ret.insert( Locale( (*it)->name() ) );
403 void ZYppImpl::availableLocale( const Locale & locale_r )
405 _pool.insert( Language::availableInstance( locale_r ) );
408 //------------------------------------------------------------------------
411 void ZYppImpl::setArchitecture( const Arch & arch )
413 _architecture = arch;
414 if (_resolver) _resolver->setArchitecture( arch );
417 //------------------------------------------------------------------------
420 Pathname ZYppImpl::homePath() const
421 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
423 void ZYppImpl::setHomePath( const Pathname & path )
424 { _home_path = path; }
426 Pathname ZYppImpl::tmpPath() const
428 static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
429 return zypp_tmp_dir.path();
432 int ZYppImpl::applyLocks()
434 Pathname locksrcPath( "/etc/zypp/locks" );
437 Target_Ptr trg( target() );
439 locksrcPath = trg->root() / locksrcPath;
443 // noop: Someone decided to let target() throw if the ptr is NULL ;(
447 PathInfo locksrc( locksrcPath );
448 if ( locksrc.isFile() )
450 MIL << "Reading locks from '" << locksrcPath << "'" << endl;
451 num = zypp::locks::readLocks( pool(), locksrcPath );
452 MIL << num << " items locked." << endl;
456 MIL << "No file '" << locksrcPath << "' to read locks from" << endl;
460 /******************************************************************
462 ** FUNCTION NAME : operator<<
463 ** FUNCTION TYPE : std::ostream &
465 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
467 return str << "ZYppImpl";
470 /////////////////////////////////////////////////////////////////
471 } // namespace zypp_detail
472 ///////////////////////////////////////////////////////////////////
473 /////////////////////////////////////////////////////////////////
475 ///////////////////////////////////////////////////////////////////