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/detail/ResImplTraits.h"
23 #include "zypp/solver/detail/Helper.h"
24 #include "zypp/target/TargetImpl.h"
25 #include "zypp/ZYpp.h"
26 #include "zypp/NVRAD.h"
27 #include "zypp/Language.h"
28 #include "zypp/DiskUsageCounter.h"
29 #include "zypp/NameKindProxy.h"
30 #include "zypp/Locks.h"
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
39 { /////////////////////////////////////////////////////////////////
41 inline Locale defaultTextLocale()
44 const char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
45 for ( const char ** envvar = envlist; *envvar; ++envvar )
47 const char * envlang = getenv( *envvar );
50 std::string envstr( envlang );
51 if ( envstr != "POSIX" && envstr != "C" )
53 Locale lang( envlang );
54 if ( lang != Locale::noCode )
65 Arch defaultArchitecture()
69 // detect the true architecture
71 if ( uname( &buf ) < 0 )
73 ERR << "Can't determine system architecture" << endl;
77 architecture = Arch( buf.machine );
78 DBG << "uname architecture is '" << buf.machine << "'" << endl;
80 // some CPUs report i686 but dont implement cx8 and cmov
81 // check for both flags in /proc/cpuinfo and downgrade
82 // to i586 if either is missing (cf bug #18885)
84 if ( architecture == Arch_i686 )
86 std::ifstream cpuinfo( "/proc/cpuinfo" );
89 ERR << "Cant open /proc/cpuinfo" << endl;
94 while ( cpuinfo.good() )
96 if ( !cpuinfo.getline( infoline, 1024, '\n' ) )
101 if ( strncmp( infoline, "flags", 5 ) == 0 )
103 std::string flagsline( infoline );
104 if ( flagsline.find( "cx8" ) == std::string::npos
105 || flagsline.find( "cmov" ) == std::string::npos )
107 architecture = Arch_i586;
108 DBG << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
112 } // read proc/cpuinfo
113 } // proc/cpuinfo opened
114 } // i686 extra flags check
117 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
119 architecture = Arch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
120 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses to: '" << architecture << "'" << endl;
125 ///////////////////////////////////////////////////////////////////
127 // METHOD NAME : ZYppImpl::ZYppImpl
128 // METHOD TYPE : Constructor
131 : _textLocale( defaultTextLocale() )
134 , _resolver( new Resolver(_pool.accessor()) )
135 , _architecture( defaultArchitecture() )
137 MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " << __TIME__ << endl;
138 MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
139 MIL << "System architecture is '" << _architecture << "'" << endl;
141 MIL << "initializing keyring..." << std::endl;
142 //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
143 _keyring = new KeyRing(tmpPath());
146 ///////////////////////////////////////////////////////////////////
148 // METHOD NAME : ZYppImpl::~ZYppImpl
149 // METHOD TYPE : Destructor
151 ZYppImpl::~ZYppImpl()
154 //------------------------------------------------------------------------
155 // add/remove resolvables
157 void ZYppImpl::addResolvables (const ResStore& store, bool installed)
159 _pool.insert(store.begin(), store.end(), installed);
162 void ZYppImpl::removeResolvables (const ResStore& store)
164 for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
170 void ZYppImpl::removeInstalledResolvables ()
172 for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
174 ResPool::const_iterator next = it; ++next;
175 if (it->status().isInstalled())
181 DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
184 _disk_usage.reset(new DiskUsageCounter());
185 return _disk_usage->disk_usage(pool());
188 void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
190 _disk_usage.reset(new DiskUsageCounter());
191 _disk_usage->setMountPoints(mp);
194 DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
197 return _disk_usage->getMountPoints();
199 return DiskUsageCounter::detectMountPoints();
202 //------------------------------------------------------------------------
205 Target_Ptr ZYppImpl::target() const
208 ZYPP_THROW(Exception("Target not initialized."));
212 void ZYppImpl::initializeTarget(const Pathname & root)
214 MIL << "initTarget( " << root << endl;
216 if (_target->root() == root) {
217 MIL << "Repeated call to initializeTarget()" << endl;
220 removeInstalledResolvables( );
222 _target = new Target( root );
223 _target->enableStorage( root );
226 void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
228 MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
230 if (_target->root() == root) {
231 MIL << "Repeated call to initTarget()" << endl;
234 removeInstalledResolvables( );
236 _target = new Target( root );
237 _target->enableStorage( root );
240 addResolvables( _target->resolvables(), true );
245 void ZYppImpl::finishTarget()
248 removeInstalledResolvables();
252 //------------------------------------------------------------------------
255 /** \todo Remove workflow from target, lot's of it could be done here,
256 * and target used for transact. */
257 ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
259 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
261 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
264 MIL << "Attempt to commit (" << policy_r << ")" << endl;
266 ZYPP_THROW( Exception("Target not initialized.") );
268 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
270 if (! policy_r.dryRun() ) {
271 // Tag target data invalid, so they are reloaded on the next call to
272 // target->resolvables(). Actually the target should do this without
275 removeInstalledResolvables();
276 if ( policy_r.syncPoolAfterCommit() )
278 // reload new status from target
279 addResolvables( _target->resolvables(), true );
283 MIL << "Commit (" << policy_r << ") returned: "
288 void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
291 ZYPP_THROW( Exception("Target not initialized.") );
292 _target->_pimpl->installSrcPackage( srcPackage_r );
295 //------------------------------------------------------------------------
299 void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
301 ResPool mpool( pool() );
302 // assert all requested are available
303 for ( LocaleSet::const_iterator it = locales_r.begin();
304 it != locales_r.end(); ++it )
306 NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
307 if ( select.installedEmpty() && select.availableEmpty() )
308 _pool.insert( Language::availableInstance( *it ) );
312 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
313 it != mpool.byKindEnd<Language>(); ++it )
315 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
316 if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
318 // Language is requested
319 if ( select.installedEmpty() )
321 if ( select.availableEmpty() )
323 // no item ==> provide available to install
324 _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
325 select = nameKindProxy<Language>( mpool, (*it)->name() );
327 // available only ==> to install
328 select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
332 // installed ==> keep it
333 select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
334 if ( ! select.availableEmpty() )
336 // both items ==> keep
337 select.availableBegin()->status().resetTransact( ResStatus::USER );
343 // Language is NOT requested
344 if ( ! select.installedEmpty() )
345 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
346 if ( ! select.availableEmpty() )
347 select.availableBegin()->status().resetTransact( ResStatus::USER );
353 ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
356 ResPool mpool( pool() );
357 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
358 it != mpool.byKindEnd<Language>(); ++it )
360 if ( (*it).status().isUninstalled() ) // available!
361 ret.insert( Locale( (*it)->name() ) );
367 ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
370 ResPool mpool( pool() );
371 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
372 it != mpool.byKindEnd<Language>(); ++it )
374 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
375 if ( ! select.installedEmpty()
376 && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
377 ret.insert( Locale( (*it)->name() ) );
378 else if ( ! select.availableEmpty()
379 && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
380 ret.insert( Locale( (*it)->name() ) );
385 void ZYppImpl::availableLocale( const Locale & locale_r )
387 _pool.insert( Language::availableInstance( locale_r ) );
390 //------------------------------------------------------------------------
393 void ZYppImpl::setArchitecture( const Arch & arch )
395 _architecture = arch;
396 if (_resolver) _resolver->setArchitecture( arch );
399 //------------------------------------------------------------------------
402 Pathname ZYppImpl::homePath() const
403 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
405 void ZYppImpl::setHomePath( const Pathname & path )
406 { _home_path = path; }
408 Pathname ZYppImpl::tmpPath() const
410 static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
411 return zypp_tmp_dir.path();
414 int ZYppImpl::applyLocks()
416 Pathname locksrcPath( "/etc/zypp/locks" );
419 Target_Ptr trg( target() );
421 locksrcPath = trg->root() / locksrcPath;
425 // noop: Someone decided to let target() throw if the ptr is NULL ;(
429 PathInfo locksrc( locksrcPath );
430 if ( locksrc.isFile() )
432 MIL << "Reading locks from '" << locksrcPath << "'" << endl;
433 num = zypp::locks::readLocks( pool(), locksrcPath );
434 MIL << num << " items locked." << endl;
438 MIL << "No file '" << locksrcPath << "' to read locks from" << endl;
442 /******************************************************************
444 ** FUNCTION NAME : operator<<
445 ** FUNCTION TYPE : std::ostream &
447 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
449 return str << "ZYppImpl";
452 /////////////////////////////////////////////////////////////////
453 } // namespace zypp_detail
454 ///////////////////////////////////////////////////////////////////
455 /////////////////////////////////////////////////////////////////
457 ///////////////////////////////////////////////////////////////////