3 #include "zypp/PoolQueryResult.h"
5 #include <zypp/base/PtrTypes.h>
6 #include <zypp/base/Exception.h>
7 #include <zypp/base/Gettext.h>
8 #include <zypp/base/LogTools.h>
9 #include <zypp/base/Debug.h>
10 #include <zypp/base/Functional.h>
11 #include <zypp/base/IOStream.h>
12 #include <zypp/base/InputStream.h>
13 #include <zypp/base/ProvideNumericId.h>
14 #include <zypp/base/Flags.h>
15 #include <zypp/AutoDispose.h>
17 #include "zypp/ResPoolProxy.h"
19 #include "zypp/ZYppCallbacks.h"
20 #include "zypp/ResPool.h"
21 #include "zypp/ResFilters.h"
22 #include "zypp/ResObjects.h"
23 #include "zypp/Digest.h"
24 #include "zypp/PackageKeyword.h"
25 #include "zypp/TmpPath.h"
26 #include "zypp/ManagedFile.h"
27 #include "zypp/MediaSetAccess.h"
30 #include "zypp/RepoManager.h"
31 #include "zypp/Repository.h"
32 #include "zypp/RepoInfo.h"
33 #include "zypp/TriBool.h"
34 #include "zypp/repo/PackageProvider.h"
36 #include "zypp/ResPoolProxy.h"
38 #include "zypp/sat/Pool.h"
39 #include "zypp/sat/LocaleSupport.h"
40 #include "zypp/sat/LookupAttr.h"
41 #include "zypp/sat/AttrMatcher.h"
42 #include "zypp/sat/SolvableSet.h"
43 #include "zypp/sat/SolvIterMixin.h"
44 #include "zypp/sat/detail/PoolImpl.h"
45 #include "zypp/sat/WhatObsoletes.h"
46 #include "zypp/PoolQuery.h"
47 #include "zypp/ServiceInfo.h"
48 #include "zypp/media/MediaPriority.h"
50 #include "zypp/target/rpm/RpmDb.h"
51 #include "zypp/target/rpm/RpmHeader.h"
52 #include "zypp/target/rpm/librpmDb.h"
54 #include <boost/mpl/int.hpp>
58 using namespace zypp::functor;
59 using namespace zypp::ui;
61 ///////////////////////////////////////////////////////////////////
63 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
65 ///////////////////////////////////////////////////////////////////
66 ///////////////////////////////////////////////////////////////////
68 { /////////////////////////////////////////////////////////////////
70 bool queryInstalledEditionHelper( const std::string & name_r,
74 if ( ed_r == Edition::noedition )
76 if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
78 if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
85 ManagedFile repoProvidePackage( const PoolItem & pi )
87 ResPool _pool( getZYpp()->pool() );
88 repo::RepoMediaAccess _access;
90 // Redirect PackageProvider queries for installed editions
91 // (in case of patch/delta rpm processing) to rpmDb.
92 repo::PackageProviderPolicy packageProviderPolicy;
93 packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
95 Package::constPtr p = asKind<Package>(pi.resolvable());
97 // Build a repository list for repos
98 // contributing to the pool
99 repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
100 _pool.knownRepositoriesEnd() ) );
101 repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
102 return pkgProvider.providePackage();
105 /////////////////////////////////////////////////////////////////
107 ///////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////
112 template <class _LIterator, class _RIterator, class _Function>
113 inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
114 _RIterator rbegin_r, _RIterator rend_r,
118 for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
120 for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
123 if ( ! fnc_r( *lit, *rit ) )
132 void dbgDu( Selectable::Ptr sel )
134 if ( sel->installedObj() )
136 DBG << "i: " << sel->installedObj() << endl
137 << sel->installedObj()->diskusage() << endl;
139 if ( sel->candidateObj() )
141 DBG << "c: " << sel->candidateObj() << endl
142 << sel->candidateObj()->diskusage() << endl;
145 << getZYpp()->diskUsage() << endl;
148 ///////////////////////////////////////////////////////////////////
150 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
153 Package::constPtr p( asKind<Package>(pi) );
156 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
158 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
160 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
161 l = Locale( "dsdf" );
162 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
165 OUTS( downloadSize );
166 OUTS( sourcePkgName );
167 OUTS( sourcePkgEdition );
179 bool operator()( const PoolItem & obj_r )
181 //MIL << obj_r << endl;
182 //DBG << " -> " << obj_r->satSolvable() << endl;
187 bool operator()( const sat::Solvable & obj_r )
189 //dumpOn( MIL, obj_r ) << endl;
194 ///////////////////////////////////////////////////////////////////
195 struct SetTransactValue
197 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
198 : _newVal( newVal_r )
199 , _causer( causer_r )
202 ResStatus::TransactValue _newVal;
203 ResStatus::TransactByValue _causer;
205 bool operator()( const PoolItem & pi ) const
207 bool ret = pi.status().setTransactValue( _newVal, _causer );
209 ERR << _newVal << _causer << " " << pi << endl;
214 struct StatusReset : public SetTransactValue
217 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
221 struct StatusInstall : public SetTransactValue
224 : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
228 ///////////////////////////////////////////////////////////////////
234 //zypp::base::LogControl::TmpLineWriter shutUp;
235 rres = getZYpp()->resolver()->resolvePool();
239 ERR << "resolve " << rres << endl;
242 MIL << "resolve " << rres << endl;
248 ZYppCommitPolicy pol;
250 pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
251 SEC << getZYpp()->commit( pol ) << endl;
257 getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
260 ///////////////////////////////////////////////////////////////////
262 struct DigestReceive : public callback::ReceiveReport<DigestReport>
269 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
274 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
279 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
281 USR << "fle " << PathInfo(file) << endl;
282 USR << "req " << requested << endl;
283 USR << "fnd " << found << endl;
288 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
290 KeyRingSignalsReceive()
294 virtual void trustedKeyAdded( const PublicKey &/*key*/ )
298 virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
304 ///////////////////////////////////////////////////////////////////
306 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
308 virtual Action requestMedia( Url & source
310 , const std::string & label
312 , const std::string & description
313 , const std::vector<std::string> & devices
314 , unsigned int & dev_current )
316 SEC << __FUNCTION__ << endl
317 << " " << source << endl
318 << " " << mediumNr << endl
319 << " " << label << endl
320 << " " << error << endl
321 << " " << description << endl
322 << " " << devices << endl
323 << " " << dev_current << endl;
328 ///////////////////////////////////////////////////////////////////
333 bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
334 { return cont.find( val ) != cont.end(); }
336 ///////////////////////////////////////////////////////////////////
338 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
340 SEC << *l << " - " << *r << endl;
341 INT << "== " << (l==r) << endl;
342 INT << "!= " << (l!=r) << endl;
345 bool isTrue() { return true; }
346 bool isFalse() { return false; }
350 for ( int i = -3; i < 30; ++i )
352 DBG << i << '\t' << IdString( i ) << endl;
356 void ttt( const char * lhs, const char * rhs )
358 DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
365 template <class _MemFun, class _Value>
369 HasValue( _MemFun fun_r, _Value val_r )
370 : _fun( fun_r ), _val( val_r )
373 bool operator()( const _Tp & obj_r ) const
374 { return( _fun && (obj_r.*_fun)() == _val ); }
380 template <class _MemFun, class _Value>
381 HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
382 { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
388 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
391 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
392 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
395 _TestO<L> testO( const L & lhs )
396 { return _TestO<L>( lhs ); }
398 template <class L, class R>
399 void testCMP( const L & lhs, const R & rhs )
401 MIL << "LHS " << testO(lhs) << endl;
402 MIL << "RHS " << rhs << endl;
404 #define OUTS(S) DBG << #S << ": " << (S) << endl
405 OUTS( lhs.compare(rhs) );
415 #include "zypp/Locks.h"
416 #include "zypp/target/HardLocksFile.h"
417 inline PoolQuery makeTrivialQuery( IdString ident_r )
419 sat::Solvable::SplitIdent ident( ident_r );
422 q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
423 q.addKind( ident.kind() );
425 q.setCaseSensitive(true);
428 inline PoolQuery makeTrivialQuery( const char * ch )
429 { return makeTrivialQuery( IdString(ch) ); }
432 static unsigned i = 0;
433 ResPool pool( ResPool::instance() );
434 target::HardLocksFile::Data newdata;
435 pool.getHardLockQueries( newdata );
436 SEC << '[' << i++ << ']' << newdata << endl;
440 Capability guessPackageSpec( const std::string & str_r )
442 return Capability::guessPackageSpec( str_r );
448 void cut( const Capability & cap )
450 CapDetail detail( cap.detail() );
451 if ( detail.isSimple() )
453 MIL << detail.kind() << ": " << detail.name();
454 if ( detail.hasArch() )
455 MIL << " (" << detail.arch() << ")";
456 if ( detail.isVersioned() )
457 MIL << " " << detail.op() << " " << detail.ed();
462 MIL << "---???---" << endl;
466 namespace zypp { namespace target {
467 void XRunUpdateMessages( const Pathname & root_r,
468 const Pathname & messagesPath_r,
469 const std::vector<sat::Solvable> & checkPackages_r,
470 ZYppCommitResult & result_r );
473 using zypp::target::XRunUpdateMessages;
475 /******************************************************************
477 ** FUNCTION NAME : main
478 ** FUNCTION TYPE : int
480 int main( int argc, char * argv[] )
484 // download the repo index file
485 media::MediaManager mediamanager;
486 media::MediaAccessId mid = mediamanager.open( Url("http://download.opensuse.org") );
487 mediamanager.attach( mid );
489 zypp::base::LogControl::instance().logToStdErr();
490 INT << "===[START]==========================================" << endl;
493 ResPool pool( ResPool::instance() );
494 sat::Pool satpool( sat::Pool::instance() );
498 Measure x( "INIT TARGET" );
501 //zypp::base::LogControl::TmpLineWriter shutUp;
502 getZYpp()->initializeTarget( sysRoot );
504 getZYpp()->target()->load();
505 USR << "baseproduct: " << getZYpp()->target()->baseProduct() << endl;
506 USR << "targetDistribution: " << getZYpp()->target()->targetDistribution() << endl;
507 USR << "targetDistributionRelease: " << getZYpp()->target()->targetDistributionRelease() << endl;
508 dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
514 RepoManager repoManager( makeRepoManager( sysRoot ) );
515 ServiceInfoList services = repoManager.knownServices();
517 for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
519 ServiceInfo & nservice( *it );
520 SEC << nservice << endl;
522 if ( ! nservice.enabled() )
525 repoManager.refreshService( nservice );
531 RepoManager repoManager( makeRepoManager( sysRoot ) );
532 RepoInfoList repos = repoManager.knownRepositories();
535 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
537 RepoInfo & nrepo( *it );
538 SEC << nrepo << endl;
540 if ( ! nrepo.enabled() )
543 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
545 if ( repoManager.isCached( nrepo ) )
547 SEC << "cleanCache" << endl;
548 repoManager.cleanCache( nrepo );
550 SEC << "refreshMetadata" << endl;
551 //repoManager.refreshMetadata( nrepo );
552 SEC << "buildCache" << endl;
553 repoManager.buildCache( nrepo );
557 // create from cache:
559 Measure x( "CREATE FROM CACHE" );
560 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
562 RepoInfo & nrepo( *it );
563 if ( ! nrepo.enabled() )
566 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
569 repoManager.loadFromCache( nrepo );
571 catch ( const Exception & exp )
573 MIL << "Try to rebuild cache..." << endl;
574 SEC << "cleanCache" << endl;
575 repoManager.cleanCache( nrepo );
576 SEC << "buildCache" << endl;
577 repoManager.buildCache( nrepo );
578 SEC << "Create from cache" << endl;
579 repoManager.loadFromCache( nrepo );
582 //USR << "pool: " << pool << endl;
587 dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
588 USR << "pool: " << pool << endl;
590 ///////////////////////////////////////////////////////////////////
594 Measure x( "Upgrade" );
595 getZYpp()->resolver()->doUpgrade();
598 ///////////////////////////////////////////////////////////////////
605 getZYpp()->resolver()->addRequire( Capability("amarok") );
606 pool.byKindBegin<Package>()->status().setTransact( true, ResStatus::USER );
608 vdumpPoolStats( USR << "Transacting:"<< endl,
609 make_filter_begin<resfilter::ByTransact>(pool),
610 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
613 //////////////////////////////////////////////////////////////////
614 INT << "===[END]============================================" << endl << endl;
615 zypp::base::LogControl::instance().logNothing();
618 catch ( const Exception & exp )
620 INT << exp << endl << exp.historyAsString();