3 #include <zypp/base/PtrTypes.h>
4 #include <zypp/base/Exception.h>
5 #include <zypp/base/Gettext.h>
6 #include <zypp/base/LogTools.h>
7 #include <zypp/base/Debug.h>
8 #include <zypp/base/Functional.h>
9 #include <zypp/base/IOStream.h>
10 #include <zypp/base/ProvideNumericId.h>
11 #include <zypp/AutoDispose.h>
13 #include "zypp/ResPoolProxy.h"
15 #include "zypp/ZYppCallbacks.h"
16 #include "zypp/NVRAD.h"
17 #include "zypp/ResPool.h"
18 #include "zypp/ResFilters.h"
19 #include "zypp/ResObjects.h"
20 #include "zypp/Digest.h"
21 #include "zypp/PackageKeyword.h"
22 #include "zypp/TmpPath.h"
23 #include "zypp/ManagedFile.h"
24 #include "zypp/NameKindProxy.h"
25 #include "zypp/pool/GetResolvablesToInsDel.h"
27 #include "zypp/RepoManager.h"
28 #include "zypp/Repository.h"
29 #include "zypp/RepoInfo.h"
31 #include "zypp/repo/PackageProvider.h"
33 #include "zypp/ui/PatchContents.h"
34 #include "zypp/ResPoolProxy.h"
36 #include "zypp/sat/Pool.h"
37 #include "zypp/sat/LocaleSupport.h"
39 #include <zypp/base/GzStream.h>
41 #include <boost/mpl/int.hpp>
45 using namespace zypp::functor;
46 using namespace zypp::ui;
48 ///////////////////////////////////////////////////////////////////
50 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
52 ///////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////
55 { /////////////////////////////////////////////////////////////////
57 bool queryInstalledEditionHelper( const std::string & name_r,
61 if ( ed_r == Edition::noedition )
63 if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
65 if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
72 ManagedFile repoProvidePackage( const PoolItem & pi )
74 ResPool _pool( getZYpp()->pool() );
75 repo::RepoMediaAccess _access;
77 // Redirect PackageProvider queries for installed editions
78 // (in case of patch/delta rpm processing) to rpmDb.
79 repo::PackageProviderPolicy packageProviderPolicy;
80 packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
82 Package::constPtr p = asKind<Package>(pi.resolvable());
84 // Build a repository list for repos
85 // contributing to the pool
86 repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
87 _pool.knownRepositoriesEnd() ) );
88 repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
89 return pkgProvider.providePackage();
92 /////////////////////////////////////////////////////////////////
94 ///////////////////////////////////////////////////////////////////
95 ///////////////////////////////////////////////////////////////////
99 template <class _LIterator, class _RIterator, class _Function>
100 inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
101 _RIterator rbegin_r, _RIterator rend_r,
105 for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
107 for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
110 if ( ! fnc_r( *lit, *rit ) )
119 void dbgDu( Selectable::Ptr sel )
121 if ( sel->installedPoolItem() )
123 DBG << "i: " << sel->installedPoolItem() << endl
124 << sel->installedPoolItem()->diskusage() << endl;
126 if ( sel->candidatePoolItem() )
128 DBG << "c: " << sel->candidatePoolItem() << endl
129 << sel->candidatePoolItem()->diskusage() << endl;
132 << getZYpp()->diskUsage() << endl;
135 ///////////////////////////////////////////////////////////////////
139 bool operator()( const PoolItem & obj_r )
141 //MIL << obj_r << endl;
142 //DBG << " -> " << obj_r->satSolvable() << endl;
147 bool operator()( const sat::Solvable & obj_r )
149 //dumpOn( MIL, obj_r ) << endl;
154 ///////////////////////////////////////////////////////////////////
155 struct SetTransactValue
157 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
158 : _newVal( newVal_r )
159 , _causer( causer_r )
162 ResStatus::TransactValue _newVal;
163 ResStatus::TransactByValue _causer;
165 bool operator()( const PoolItem & pi ) const
167 bool ret = pi.status().setTransactValue( _newVal, _causer );
169 ERR << _newVal << _causer << " " << pi << endl;
174 struct StatusReset : public SetTransactValue
177 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
181 struct StatusInstall : public SetTransactValue
184 : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
188 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
190 if ( nkp.availableEmpty() )
192 ERR << "No Item to select: " << nkp << endl;
194 ZYPP_THROW( Exception("No Item to select") );
197 if ( arch != Arch() )
199 typeof( nkp.availableBegin() ) it = nkp.availableBegin();
200 for ( ; it != nkp.availableEnd(); ++it )
202 if ( (*it)->arch() == arch )
203 return (*it).status().setTransact( true, ResStatus::USER );
207 return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
210 ///////////////////////////////////////////////////////////////////
216 //zypp::base::LogControl::TmpLineWriter shutUp;
217 rres = getZYpp()->resolver()->resolvePool();
221 ERR << "resolve " << rres << endl;
224 MIL << "resolve " << rres << endl;
230 SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
234 ///////////////////////////////////////////////////////////////////
236 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
238 virtual void start( const Resolvable::constPtr & script_r,
239 const Pathname & path_r,
242 SEC << __FUNCTION__ << endl
243 << " " << script_r << endl
244 << " " << path_r << endl
245 << " " << task_r << endl;
248 virtual bool progress( Notify notify_r, const std::string & text_r )
250 SEC << __FUNCTION__ << endl
251 << " " << notify_r << endl
252 << " " << text_r << endl;
256 virtual void problem( const std::string & description_r )
258 SEC << __FUNCTION__ << endl
259 << " " << description_r << endl;
262 virtual void finish()
264 SEC << __FUNCTION__ << endl;
268 ///////////////////////////////////////////////////////////////////
270 struct DigestReceive : public callback::ReceiveReport<DigestReport>
277 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
282 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
287 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
289 USR << "fle " << PathInfo(file) << endl;
290 USR << "req " << requested << endl;
291 USR << "fnd " << found << endl;
296 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
298 KeyRingSignalsReceive()
302 virtual void trustedKeyAdded( const PublicKey &/*key*/ )
306 virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
312 ///////////////////////////////////////////////////////////////////
314 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
316 virtual Action requestMedia( Url & source
318 , const std::string & label
320 , const std::string & description
321 , const std::vector<std::string> & devices
322 , unsigned int & dev_current )
324 SEC << __FUNCTION__ << endl
325 << " " << source << endl
326 << " " << mediumNr << endl
327 << " " << label << endl
328 << " " << error << endl
329 << " " << description << endl
330 << " " << devices << endl
331 << " " << dev_current << endl;
336 ///////////////////////////////////////////////////////////////////
341 bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
342 { return cont.find( val ) != cont.end(); }
344 ///////////////////////////////////////////////////////////////////
346 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
348 SEC << *l << " - " << *r << endl;
349 INT << "== " << (l==r) << endl;
350 INT << "!= " << (l!=r) << endl;
353 bool isTrue() { return true; }
354 bool isFalse() { return false; }
358 for ( int i = -3; i < 30; ++i )
360 DBG << i << '\t' << IdString( i ) << endl;
364 void ttt( const char * lhs, const char * rhs )
366 DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
373 template <class _MemFun, class _Value>
377 HasValue( _MemFun fun_r, _Value val_r )
378 : _fun( fun_r ), _val( val_r )
381 bool operator()( const _Tp & obj_r ) const
382 { return( _fun && (obj_r.*_fun)() == _val ); }
388 template <class _MemFun, class _Value>
389 HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
390 { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
396 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
399 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
400 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
403 _TestO<L> testO( const L & lhs )
404 { return _TestO<L>( lhs ); }
406 template <class L, class R>
407 void testCMP( const L & lhs, const R & rhs )
409 MIL << "LHS " << testO(lhs) << endl;
410 MIL << "RHS " << rhs << endl;
412 #define OUTS(S) DBG << #S << ": " << (S) << endl
413 OUTS( lhs.compare(rhs) );
428 void tt( const std::string & name_r, ResKind kind_r = ResKind::package )
430 Capability cap( name_r, kind_r );
431 sat::WhatProvides possibleProviders(cap);
432 (possibleProviders.empty()?WAR:MIL) << name_r << endl;
433 for_(iter, possibleProviders.begin(), possibleProviders.end())
435 MIL << name_r << endl;
436 PoolItem provider = ResPool::instance().find(*iter);
440 /******************************************************************
442 ** FUNCTION NAME : main
443 ** FUNCTION TYPE : int
445 int main( int argc, char * argv[] )
447 zypp::base::LogControl::instance().logToStdErr();
448 INT << "===[START]==========================================" << endl;
450 sat::Pool satpool( sat::Pool::instance() );
451 ResPool pool( ResPool::instance() );
452 USR << "pool: " << pool << endl;
456 RepoManager repoManager( makeRepoManager( sysRoot ) );
457 RepoInfoList repos = repoManager.knownRepositories();
460 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
462 RepoInfo & nrepo( *it );
463 SEC << nrepo << endl;
465 if ( ! nrepo.enabled() )
468 if ( ! repoManager.isCached( nrepo ) || /*force*/false )
470 if ( repoManager.isCached( nrepo ) )
472 SEC << "cleanCache" << endl;
473 repoManager.cleanCache( nrepo );
475 SEC << "refreshMetadata" << endl;
476 repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
477 SEC << "buildCache" << endl;
478 repoManager.buildCache( nrepo );
482 // create from cache:
484 Measure x( "CREATE FROM CACHE" );
485 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
487 RepoInfo & nrepo( *it );
488 if ( ! nrepo.enabled() )
491 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
494 repoManager.loadFromCache( nrepo );
496 catch ( const Exception & exp )
498 MIL << "Try to rebuild cache..." << endl;
499 SEC << "cleanCache" << endl;
500 repoManager.cleanCache( nrepo );
501 SEC << "buildCache" << endl;
502 repoManager.buildCache( nrepo );
503 SEC << "Create from cache" << endl;
504 repoManager.loadFromCache( nrepo );
507 USR << "pool: " << pool << endl;
514 Measure x( "INIT TARGET" );
516 getZYpp()->initializeTarget( sysRoot );
517 getZYpp()->target()->load();
521 USR << "pool: " << pool << endl;
523 ///////////////////////////////////////////////////////////////////
525 // Id keyname = std2id (pool, "susetags:datadir");
529 // dataiterator_init(&di, repo, 0, keyname, 0, SEARCH_NO_STORAGE_SOLVABLE);
530 // if (dataiterator_step(&di))
531 // printf ("datadir: %s\n", di.kv.str);
534 sat::LocaleSupport myLocale( Locale("de") );
536 if ( myLocale.isAvailable() )
538 MIL << "Support for locale '" << myLocale.locale() << "' is available." << endl;
540 if ( ! myLocale.isRequested() )
542 MIL << "Will enable support for locale '" << myLocale.locale() << "'." << endl;
543 myLocale.setRequested( true );
545 MIL << "Packages supporting locale '" << myLocale.locale() << "':" << endl;
546 for_( it, myLocale.begin(), myLocale.end() )
548 // iterate over sat::Solvables
549 MIL << " " << *it << endl;
550 // or get the PoolItems
551 DBG << " " << PoolItem(*it) << endl;
555 // for_( it, poolItemIterator(myLocale.begin()), poolItemIterator(myLocale.end()) )
557 // iterate over PoolItem
558 // MIL << " " << *it << endl;
563 sat::SolvAttr flist( "solvable:filelist" );
565 for_( it, pool.byIdentBegin<Package>("zypper"), pool.byIdentEnd<Package>("zypper") )
568 sat::Solvable s( it->satSolvable() );
569 MIL << sat::SolvAttr::summary << endl;
570 MIL << s.lookupStrAttribute( sat::SolvAttr::summary ) << endl;
571 MIL << s.lookupStrAttribute( sat::SolvAttr::noAttr ) << endl;
574 ::dataiterator_init( &di, 0, s.id(), flist.id(), 0, SEARCH_NO_STORAGE_SOLVABLE );
575 while ( ::dataiterator_step( &di ) )
584 for_( it, pool.byKindBegin<SrcPackage>(), pool.byKindEnd<SrcPackage>() )
587 //tt( (*it)->name() );
590 IdString id ("amarok");
591 sat::WhatProvides w( Capability(id.id()) );
593 for_( it, w.begin(), w.end() )
596 MIL << PoolItem(*it) << endl;
597 Package_Ptr p( asKind<Package>(PoolItem(*it)) );
601 OnMediaLocation l( p->location() );
607 sat::Solvable a(65241);
610 p.status().setTransact( true, ResStatus::USER );
612 USR << PoolItem() << endl;
617 Measure x( "Upgrade" );
619 getZYpp()->resolver()->doUpgrade( u );
624 PoolItem pi ( getPi<Package>("amarok") );
628 pi.status().setTransact( true, ResStatus::USER );
630 vdumpPoolStats( USR << "Transacting:"<< endl,
631 make_filter_begin<resfilter::ByTransact>(pool),
632 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
636 //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
639 //std::for_each( pool.begin(), pool.end(), Xprint() );
641 ///////////////////////////////////////////////////////////////////
642 INT << "===[END]============================================" << endl << endl;
643 zypp::base::LogControl::instance().logNothing();
646 catch ( const Exception & exp )
648 INT << exp << endl << exp.historyAsString();