1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/zypp_detail/ZYppImpl.cc
13 #include <sys/utsname.h>
16 #include "zypp/base/Logger.h"
18 #include "zypp/zypp_detail/ZYppImpl.h"
19 #include "zypp/detail/ResImplTraits.h"
20 #include "zypp/solver/detail/Helper.h"
21 #include "zypp/target/TargetImpl.h"
22 #include "zypp/ZYpp.h"
23 #include "zypp/NVRAD.h"
24 #include "zypp/Language.h"
25 #include "zypp/DiskUsageCounter.h"
26 #include "zypp/NameKindProxy.h"
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
37 inline Locale defaultTextLocale()
40 char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
41 for ( char ** envvar = envlist; *envvar; ++envvar )
43 char * envlang = getenv( *envvar );
46 std::string envstr( envlang );
47 if ( envstr != "POSIX" && envstr != "C" )
49 Locale lang( envlang );
50 if ( lang != Locale::noCode )
61 ///////////////////////////////////////////////////////////////////
63 // METHOD NAME : ZYppImpl::ZYppImpl
64 // METHOD TYPE : Constructor
67 : _textLocale( defaultTextLocale() )
69 , _sourceFeed( _pool )
71 , _resolver( new Resolver(_pool.accessor()) )
74 MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
76 MIL << "initializing keyring..." << std::endl;
77 //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
78 _keyring = new KeyRing();
80 // detect the true architecture
82 if (uname (&buf) < 0) {
83 ERR << "Can't determine system architecture" << endl;
86 _architecture = Arch( buf.machine );
88 MIL << "uname architecture is '" << buf.machine << "'" << endl;
90 // some CPUs report i686 but dont implement cx8 and cmov
91 // check for both flags in /proc/cpuinfo and downgrade
92 // to i586 if either is missing (cf bug #18885)
94 if (_architecture == Arch_i686)
96 std::ifstream cpuinfo ("/proc/cpuinfo");
99 ERR << "Cant open /proc/cpuinfo" << endl;
104 while (cpuinfo.good())
106 if (!cpuinfo.getline (infoline, 1024, '\n'))
111 if (strncmp (infoline, "flags", 5) == 0)
113 std::string flagsline (infoline);
114 if ((flagsline.find( "cx8" ) == std::string::npos)
115 || (flagsline.find( "cmov" ) == std::string::npos))
117 _architecture = Arch_i586;
121 } // read proc/cpuinfo
122 } // proc/cpuinfo opened
123 } // i686 extra flags check
125 MIL << "System architecture is '" << _architecture << "'" << endl;
129 std::string fakearch(getenv("ZYPP_TESTSUITE_FAKE_ARCH"));
130 if ( fakearch.size() > 0 )
132 _architecture = Arch( fakearch );
133 MIL << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses (warning! commit will be disabled!) to: '" << _architecture << "'" << endl;
137 ///////////////////////////////////////////////////////////////////
139 // METHOD NAME : ZYppImpl::~ZYppImpl
140 // METHOD TYPE : Destructor
142 ZYppImpl::~ZYppImpl()
145 //------------------------------------------------------------------------
146 // add/remove resolvables
148 void ZYppImpl::addResolvables (const ResStore& store, bool installed)
150 _pool.insert(store.begin(), store.end(), installed);
153 void ZYppImpl::removeResolvables (const ResStore& store)
155 for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
161 void ZYppImpl::removeInstalledResolvables ()
163 for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
165 ResPool::const_iterator next = it; ++next;
166 if (it->status().isInstalled())
172 DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
173 { return _disk_usage.disk_usage(pool()); }
175 void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
176 { _disk_usage.setMountPoints(mp); }
178 //------------------------------------------------------------------------
181 Target_Ptr ZYppImpl::target() const
184 ZYPP_THROW(Exception("Target not initialized."));
188 void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
190 MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
192 if (_target->root() == root) {
193 MIL << "Repeated call to initTarget()" << endl;
196 removeInstalledResolvables( );
198 _target = new Target( root );
201 _target->enableStorage( root );
202 addResolvables( _target->resolvables(), true );
206 void ZYppImpl::finishTarget()
209 removeInstalledResolvables();
213 //------------------------------------------------------------------------
216 /** \todo Remove workflow from target, lot's of it could be done here,
217 * and target used for transact. */
218 ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
220 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
222 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
225 MIL << "Attempt to commit (" << policy_r << ")" << endl;
227 ZYPP_THROW( Exception("Target not initialized.") );
229 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
231 if (! policy_r.dryRun() ) {
232 // reload new status from target
233 removeInstalledResolvables();
234 addResolvables( _target->resolvables(), true );
237 MIL << "Commit (" << policy_r << ") returned: "
243 //------------------------------------------------------------------------
247 void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
249 ResPool mpool( pool() );
250 // assert all requested are available
251 for ( LocaleSet::const_iterator it = locales_r.begin();
252 it != locales_r.end(); ++it )
254 NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
255 if ( select.installedEmpty() && select.availableEmpty() )
256 _pool.insert( Language::availableInstance( *it ) );
260 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
261 it != mpool.byKindEnd<Language>(); ++it )
263 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
264 if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
266 // Language is requested
267 if ( select.installedEmpty() )
269 if ( select.availableEmpty() )
271 // no item ==> provide available to install
272 _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
273 select = nameKindProxy<Language>( mpool, (*it)->name() );
275 // available only ==> to install
276 select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
280 // installed ==> keep it
281 select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
282 if ( ! select.availableEmpty() )
284 // both items ==> keep
285 select.availableBegin()->status().resetTransact( ResStatus::USER );
291 // Language is NOT requested
292 if ( ! select.installedEmpty() )
293 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
294 if ( ! select.availableEmpty() )
295 select.availableBegin()->status().resetTransact( ResStatus::USER );
301 ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
304 ResPool mpool( pool() );
305 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
306 it != mpool.byKindEnd<Language>(); ++it )
308 if ( (*it).status().isUninstalled() ) // available!
309 ret.insert( Locale( (*it)->name() ) );
315 ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
318 ResPool mpool( pool() );
319 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
320 it != mpool.byKindEnd<Language>(); ++it )
322 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
323 if ( ! select.installedEmpty()
324 && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
325 ret.insert( Locale( (*it)->name() ) );
326 else if ( ! select.availableEmpty()
327 && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
328 ret.insert( Locale( (*it)->name() ) );
333 void ZYppImpl::availableLocale( const Locale & locale_r )
335 _pool.insert( Language::availableInstance( locale_r ) );
338 //------------------------------------------------------------------------
341 void ZYppImpl::setArchitecture( const Arch & arch )
343 _architecture = arch;
344 if (_resolver) _resolver->setArchitecture( arch );
347 //------------------------------------------------------------------------
350 Pathname ZYppImpl::homePath() const
351 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
353 void ZYppImpl::setHomePath( const Pathname & path )
354 { _home_path = path; }
356 /******************************************************************
358 ** FUNCTION NAME : operator<<
359 ** FUNCTION TYPE : std::ostream &
361 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
363 return str << "ZYppImpl";
366 /////////////////////////////////////////////////////////////////
367 } // namespace zypp_detail
368 ///////////////////////////////////////////////////////////////////
369 /////////////////////////////////////////////////////////////////
371 ///////////////////////////////////////////////////////////////////