1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/zypp_detail/ZYppImpl.cc
13 #include <sys/utsname.h>
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/String.h"
20 #include "zypp/zypp_detail/ZYppImpl.h"
21 #include "zypp/detail/ResImplTraits.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"
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////
37 { /////////////////////////////////////////////////////////////////
39 inline Locale defaultTextLocale()
42 char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
43 for ( char ** envvar = envlist; *envvar; ++envvar )
45 char * envlang = getenv( *envvar );
48 std::string envstr( envlang );
49 if ( envstr != "POSIX" && envstr != "C" )
51 Locale lang( envlang );
52 if ( lang != Locale::noCode )
63 Arch defaultArchitecture()
67 // detect the true architecture
69 if ( uname( &buf ) < 0 )
71 ERR << "Can't determine system architecture" << endl;
75 architecture = Arch( buf.machine );
76 DBG << "uname architecture is '" << buf.machine << "'" << endl;
78 // some CPUs report i686 but dont implement cx8 and cmov
79 // check for both flags in /proc/cpuinfo and downgrade
80 // to i586 if either is missing (cf bug #18885)
82 if ( architecture == Arch_i686 )
84 std::ifstream cpuinfo( "/proc/cpuinfo" );
87 ERR << "Cant open /proc/cpuinfo" << endl;
92 while ( cpuinfo.good() )
94 if ( !cpuinfo.getline( infoline, 1024, '\n' ) )
99 if ( strncmp( infoline, "flags", 5 ) == 0 )
101 std::string flagsline( infoline );
102 if ( flagsline.find( "cx8" ) == std::string::npos
103 || flagsline.find( "cmov" ) == std::string::npos )
105 architecture = Arch_i586;
106 DBG << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
110 } // read proc/cpuinfo
111 } // proc/cpuinfo opened
112 } // i686 extra flags check
115 if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
117 architecture = Arch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
118 WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses to: '" << architecture << "'" << endl;
123 ///////////////////////////////////////////////////////////////////
125 // METHOD NAME : ZYppImpl::ZYppImpl
126 // METHOD TYPE : Constructor
129 : _textLocale( defaultTextLocale() )
131 , _sourceFeed( _pool )
133 , _resolver( new Resolver(_pool.accessor()) )
134 , _architecture( defaultArchitecture() )
137 MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
138 MIL << "System architecture is '" << _architecture << "'" << endl;
140 MIL << "initializing keyring..." << std::endl;
141 //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
142 _keyring = new KeyRing(tmpPath());
145 ///////////////////////////////////////////////////////////////////
147 // METHOD NAME : ZYppImpl::~ZYppImpl
148 // METHOD TYPE : Destructor
150 ZYppImpl::~ZYppImpl()
153 //------------------------------------------------------------------------
154 // add/remove resolvables
156 void ZYppImpl::addResolvables (const ResStore& store, bool installed)
158 _pool.insert(store.begin(), store.end(), installed);
161 void ZYppImpl::removeResolvables (const ResStore& store)
163 for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
169 void ZYppImpl::removeInstalledResolvables ()
171 for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
173 ResPool::const_iterator next = it; ++next;
174 if (it->status().isInstalled())
180 DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
181 { return _disk_usage.disk_usage(pool()); }
183 void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
184 { _disk_usage.setMountPoints(mp); }
186 //------------------------------------------------------------------------
189 Target_Ptr ZYppImpl::target() const
192 ZYPP_THROW(Exception("Target not initialized."));
196 void ZYppImpl::initializeTarget(const Pathname & root)
198 MIL << "initTarget( " << root << endl;
200 if (_target->root() == root) {
201 MIL << "Repeated call to initializeTarget()" << endl;
204 removeInstalledResolvables( );
206 _target = new Target( root );
207 _target->enableStorage( root );
210 void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
212 MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
214 if (_target->root() == root) {
215 MIL << "Repeated call to initTarget()" << endl;
218 removeInstalledResolvables( );
220 _target = new Target( root );
221 _target->enableStorage( root );
224 addResolvables( _target->resolvables(), true );
229 void ZYppImpl::finishTarget()
232 removeInstalledResolvables();
236 //------------------------------------------------------------------------
239 /** \todo Remove workflow from target, lot's of it could be done here,
240 * and target used for transact. */
241 ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
243 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
245 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
248 MIL << "Attempt to commit (" << policy_r << ")" << endl;
250 ZYPP_THROW( Exception("Target not initialized.") );
252 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
254 if (! policy_r.dryRun() ) {
255 // reload new status from target
256 removeInstalledResolvables();
258 addResolvables( _target->resolvables(), true );
261 MIL << "Commit (" << policy_r << ") returned: "
267 //------------------------------------------------------------------------
271 void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
273 ResPool mpool( pool() );
274 // assert all requested are available
275 for ( LocaleSet::const_iterator it = locales_r.begin();
276 it != locales_r.end(); ++it )
278 NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
279 if ( select.installedEmpty() && select.availableEmpty() )
280 _pool.insert( Language::availableInstance( *it ) );
284 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
285 it != mpool.byKindEnd<Language>(); ++it )
287 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
288 if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
290 // Language is requested
291 if ( select.installedEmpty() )
293 if ( select.availableEmpty() )
295 // no item ==> provide available to install
296 _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
297 select = nameKindProxy<Language>( mpool, (*it)->name() );
299 // available only ==> to install
300 select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
304 // installed ==> keep it
305 select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
306 if ( ! select.availableEmpty() )
308 // both items ==> keep
309 select.availableBegin()->status().resetTransact( ResStatus::USER );
315 // Language is NOT requested
316 if ( ! select.installedEmpty() )
317 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
318 if ( ! select.availableEmpty() )
319 select.availableBegin()->status().resetTransact( ResStatus::USER );
325 ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
328 ResPool mpool( pool() );
329 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
330 it != mpool.byKindEnd<Language>(); ++it )
332 if ( (*it).status().isUninstalled() ) // available!
333 ret.insert( Locale( (*it)->name() ) );
339 ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
342 ResPool mpool( pool() );
343 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
344 it != mpool.byKindEnd<Language>(); ++it )
346 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
347 if ( ! select.installedEmpty()
348 && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
349 ret.insert( Locale( (*it)->name() ) );
350 else if ( ! select.availableEmpty()
351 && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
352 ret.insert( Locale( (*it)->name() ) );
357 void ZYppImpl::availableLocale( const Locale & locale_r )
359 _pool.insert( Language::availableInstance( locale_r ) );
362 //------------------------------------------------------------------------
365 void ZYppImpl::setArchitecture( const Arch & arch )
367 _architecture = arch;
368 if (_resolver) _resolver->setArchitecture( arch );
371 //------------------------------------------------------------------------
374 Pathname ZYppImpl::homePath() const
375 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
377 void ZYppImpl::setHomePath( const Pathname & path )
378 { _home_path = path; }
380 Pathname ZYppImpl::tmpPath() const
382 static TmpDir zypp_tmp_dir("/var/tmp", "zypp.");
383 return zypp_tmp_dir.path();
386 /******************************************************************
388 ** FUNCTION NAME : operator<<
389 ** FUNCTION TYPE : std::ostream &
391 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
393 return str << "ZYppImpl";
396 /////////////////////////////////////////////////////////////////
397 } // namespace zypp_detail
398 ///////////////////////////////////////////////////////////////////
399 /////////////////////////////////////////////////////////////////
401 ///////////////////////////////////////////////////////////////////