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 const char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
43 for ( const char ** envvar = envlist; *envvar; ++envvar )
45 const 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 << "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()
182 { return _disk_usage.disk_usage(pool()); }
184 void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
185 { _disk_usage.setMountPoints(mp); }
187 DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
188 { return _disk_usage.getMountPoints(); }
190 //------------------------------------------------------------------------
193 Target_Ptr ZYppImpl::target() const
196 ZYPP_THROW(Exception("Target not initialized."));
200 void ZYppImpl::initializeTarget(const Pathname & root)
202 MIL << "initTarget( " << root << endl;
204 if (_target->root() == root) {
205 MIL << "Repeated call to initializeTarget()" << endl;
208 removeInstalledResolvables( );
210 _target = new Target( root );
211 _target->enableStorage( root );
214 void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
216 MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
218 if (_target->root() == root) {
219 MIL << "Repeated call to initTarget()" << endl;
222 removeInstalledResolvables( );
224 _target = new Target( root );
225 _target->enableStorage( root );
228 addResolvables( _target->resolvables(), true );
233 void ZYppImpl::finishTarget()
236 removeInstalledResolvables();
240 //------------------------------------------------------------------------
243 /** \todo Remove workflow from target, lot's of it could be done here,
244 * and target used for transact. */
245 ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
247 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
249 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
252 MIL << "Attempt to commit (" << policy_r << ")" << endl;
254 ZYPP_THROW( Exception("Target not initialized.") );
256 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
258 if (! policy_r.dryRun() ) {
259 // Tag target data invalid, so they are reloaded on the next call to
260 // target->resolvables(). Actually the target should do this without
263 removeInstalledResolvables();
264 if ( policy_r.syncPoolAfterCommit() )
266 // reload new status from target
267 addResolvables( _target->resolvables(), true );
271 MIL << "Commit (" << policy_r << ") returned: "
277 //------------------------------------------------------------------------
281 void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
283 ResPool mpool( pool() );
284 // assert all requested are available
285 for ( LocaleSet::const_iterator it = locales_r.begin();
286 it != locales_r.end(); ++it )
288 NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
289 if ( select.installedEmpty() && select.availableEmpty() )
290 _pool.insert( Language::availableInstance( *it ) );
294 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
295 it != mpool.byKindEnd<Language>(); ++it )
297 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
298 if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
300 // Language is requested
301 if ( select.installedEmpty() )
303 if ( select.availableEmpty() )
305 // no item ==> provide available to install
306 _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
307 select = nameKindProxy<Language>( mpool, (*it)->name() );
309 // available only ==> to install
310 select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
314 // installed ==> keep it
315 select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
316 if ( ! select.availableEmpty() )
318 // both items ==> keep
319 select.availableBegin()->status().resetTransact( ResStatus::USER );
325 // Language is NOT requested
326 if ( ! select.installedEmpty() )
327 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
328 if ( ! select.availableEmpty() )
329 select.availableBegin()->status().resetTransact( ResStatus::USER );
335 ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
338 ResPool mpool( pool() );
339 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
340 it != mpool.byKindEnd<Language>(); ++it )
342 if ( (*it).status().isUninstalled() ) // available!
343 ret.insert( Locale( (*it)->name() ) );
349 ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
352 ResPool mpool( pool() );
353 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
354 it != mpool.byKindEnd<Language>(); ++it )
356 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
357 if ( ! select.installedEmpty()
358 && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
359 ret.insert( Locale( (*it)->name() ) );
360 else if ( ! select.availableEmpty()
361 && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
362 ret.insert( Locale( (*it)->name() ) );
367 void ZYppImpl::availableLocale( const Locale & locale_r )
369 _pool.insert( Language::availableInstance( locale_r ) );
372 //------------------------------------------------------------------------
375 void ZYppImpl::setArchitecture( const Arch & arch )
377 _architecture = arch;
378 if (_resolver) _resolver->setArchitecture( arch );
381 //------------------------------------------------------------------------
384 Pathname ZYppImpl::homePath() const
385 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
387 void ZYppImpl::setHomePath( const Pathname & path )
388 { _home_path = path; }
390 Pathname ZYppImpl::tmpPath() const
392 static TmpDir zypp_tmp_dir("/var/tmp", "zypp.");
393 return zypp_tmp_dir.path();
396 /******************************************************************
398 ** FUNCTION NAME : operator<<
399 ** FUNCTION TYPE : std::ostream &
401 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
403 return str << "ZYppImpl";
406 /////////////////////////////////////////////////////////////////
407 } // namespace zypp_detail
408 ///////////////////////////////////////////////////////////////////
409 /////////////////////////////////////////////////////////////////
411 ///////////////////////////////////////////////////////////////////