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 ///////////////////////////////////////////////////////////////////
65 // METHOD NAME : ZYppImpl::ZYppImpl
66 // METHOD TYPE : Constructor
69 : _textLocale( defaultTextLocale() )
71 , _sourceFeed( _pool )
73 , _resolver( new Resolver(_pool.accessor()) )
76 MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
78 MIL << "initializing keyring..." << std::endl;
79 //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
80 _keyring = new KeyRing(tmpPath());
82 // detect the true architecture
84 if (uname (&buf) < 0) {
85 ERR << "Can't determine system architecture" << endl;
88 _architecture = Arch( buf.machine );
90 MIL << "uname architecture is '" << buf.machine << "'" << endl;
92 // some CPUs report i686 but dont implement cx8 and cmov
93 // check for both flags in /proc/cpuinfo and downgrade
94 // to i586 if either is missing (cf bug #18885)
96 if (_architecture == Arch_i686)
98 std::ifstream cpuinfo ("/proc/cpuinfo");
101 ERR << "Cant open /proc/cpuinfo" << endl;
106 while (cpuinfo.good())
108 if (!cpuinfo.getline (infoline, 1024, '\n'))
113 if (strncmp (infoline, "flags", 5) == 0)
115 std::string flagsline (infoline);
116 if ((flagsline.find( "cx8" ) == std::string::npos)
117 || (flagsline.find( "cmov" ) == std::string::npos))
119 _architecture = Arch_i586;
123 } // read proc/cpuinfo
124 } // proc/cpuinfo opened
125 } // i686 extra flags check
127 MIL << "System architecture is '" << _architecture << "'" << endl;
132 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
134 Arch already_set = _architecture;
136 std::string fakearch(getenv("ZYPP_TESTSUITE_FAKE_ARCH"));
139 _architecture = Arch( fakearch );
140 MIL << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses (warning! commit will be disabled!) to: '" << _architecture << "'" << endl;
144 ERR << "ZYPP_TESTSUITE_FAKE_ARCH: Wrong architecture specified on env variable, using: '" << _architecture << "'" << endl;
149 ///////////////////////////////////////////////////////////////////
151 // METHOD NAME : ZYppImpl::~ZYppImpl
152 // METHOD TYPE : Destructor
154 ZYppImpl::~ZYppImpl()
157 //------------------------------------------------------------------------
158 // add/remove resolvables
160 void ZYppImpl::addResolvables (const ResStore& store, bool installed)
162 _pool.insert(store.begin(), store.end(), installed);
165 void ZYppImpl::removeResolvables (const ResStore& store)
167 for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
173 void ZYppImpl::removeInstalledResolvables ()
175 for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
177 ResPool::const_iterator next = it; ++next;
178 if (it->status().isInstalled())
184 DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
185 { return _disk_usage.disk_usage(pool()); }
187 void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
188 { _disk_usage.setMountPoints(mp); }
190 //------------------------------------------------------------------------
193 Target_Ptr ZYppImpl::target() const
196 ZYPP_THROW(Exception("Target not initialized."));
200 void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
202 MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
204 if (_target->root() == root) {
205 MIL << "Repeated call to initTarget()" << endl;
208 removeInstalledResolvables( );
210 _target = new Target( root );
213 _target->enableStorage( root );
214 addResolvables( _target->resolvables(), true );
218 void ZYppImpl::finishTarget()
221 removeInstalledResolvables();
225 //------------------------------------------------------------------------
228 /** \todo Remove workflow from target, lot's of it could be done here,
229 * and target used for transact. */
230 ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
232 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
234 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
237 MIL << "Attempt to commit (" << policy_r << ")" << endl;
239 ZYPP_THROW( Exception("Target not initialized.") );
241 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
243 if (! policy_r.dryRun() ) {
244 // reload new status from target
245 removeInstalledResolvables();
246 addResolvables( _target->resolvables(), true );
249 MIL << "Commit (" << policy_r << ") returned: "
255 //------------------------------------------------------------------------
259 void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
261 ResPool mpool( pool() );
262 // assert all requested are available
263 for ( LocaleSet::const_iterator it = locales_r.begin();
264 it != locales_r.end(); ++it )
266 NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
267 if ( select.installedEmpty() && select.availableEmpty() )
268 _pool.insert( Language::availableInstance( *it ) );
272 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
273 it != mpool.byKindEnd<Language>(); ++it )
275 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
276 if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
278 // Language is requested
279 if ( select.installedEmpty() )
281 if ( select.availableEmpty() )
283 // no item ==> provide available to install
284 _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
285 select = nameKindProxy<Language>( mpool, (*it)->name() );
287 // available only ==> to install
288 select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
292 // installed ==> keep it
293 select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
294 if ( ! select.availableEmpty() )
296 // both items ==> keep
297 select.availableBegin()->status().resetTransact( ResStatus::USER );
303 // Language is NOT requested
304 if ( ! select.installedEmpty() )
305 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
306 if ( ! select.availableEmpty() )
307 select.availableBegin()->status().resetTransact( ResStatus::USER );
313 ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
316 ResPool mpool( pool() );
317 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
318 it != mpool.byKindEnd<Language>(); ++it )
320 if ( (*it).status().isUninstalled() ) // available!
321 ret.insert( Locale( (*it)->name() ) );
327 ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
330 ResPool mpool( pool() );
331 for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
332 it != mpool.byKindEnd<Language>(); ++it )
334 NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
335 if ( ! select.installedEmpty()
336 && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
337 ret.insert( Locale( (*it)->name() ) );
338 else if ( ! select.availableEmpty()
339 && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
340 ret.insert( Locale( (*it)->name() ) );
345 void ZYppImpl::availableLocale( const Locale & locale_r )
347 _pool.insert( Language::availableInstance( locale_r ) );
350 //------------------------------------------------------------------------
353 void ZYppImpl::setArchitecture( const Arch & arch )
355 _architecture = arch;
356 if (_resolver) _resolver->setArchitecture( arch );
359 //------------------------------------------------------------------------
362 Pathname ZYppImpl::homePath() const
363 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
365 void ZYppImpl::setHomePath( const Pathname & path )
366 { _home_path = path; }
368 Pathname ZYppImpl::tmpPath() const
370 static TmpDir zypp_tmp_dir("/var/tmp", "zypp.");
371 return zypp_tmp_dir.path();
374 /******************************************************************
376 ** FUNCTION NAME : operator<<
377 ** FUNCTION TYPE : std::ostream &
379 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
381 return str << "ZYppImpl";
384 /////////////////////////////////////////////////////////////////
385 } // namespace zypp_detail
386 ///////////////////////////////////////////////////////////////////
387 /////////////////////////////////////////////////////////////////
389 ///////////////////////////////////////////////////////////////////