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/base/StrMatcher.h>
16 #include <zypp/AutoDispose.h>
18 #include "zypp/ResPoolProxy.h"
20 #include "zypp/ZYppCallbacks.h"
21 #include "zypp/ResPool.h"
22 #include "zypp/ResFilters.h"
23 #include "zypp/ResObjects.h"
24 #include "zypp/Digest.h"
25 #include "zypp/PackageKeyword.h"
26 #include "zypp/TmpPath.h"
27 #include "zypp/ManagedFile.h"
28 #include "zypp/MediaSetAccess.h"
31 #include "zypp/RepoManager.h"
32 #include "zypp/Repository.h"
33 #include "zypp/RepoInfo.h"
34 #include "zypp/TriBool.h"
35 #include "zypp/repo/PackageProvider.h"
37 #include "zypp/ResPoolProxy.h"
39 #include "zypp/sat/Pool.h"
40 #include "zypp/sat/LocaleSupport.h"
41 #include "zypp/sat/LookupAttr.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 TLIterator, class TRIterator, class TFunction>
113 inline int invokeOnEach( TLIterator lbegin_r, TLIterator lend_r,
114 TRIterator rbegin_r, TRIterator rend_r,
118 for ( TLIterator lit = lbegin_r; lit != lend_r; ++lit )
120 for ( TRIterator rit = rbegin_r; rit != rend_r; ++rit )
123 if ( ! fnc_r( *lit, *rit ) )
132 ///////////////////////////////////////////////////////////////////
134 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
137 Package::constPtr p( asKind<Package>(pi) );
140 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
142 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
144 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
145 l = Locale( "dsdf" );
146 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
149 OUTS( downloadSize );
150 OUTS( sourcePkgName );
151 OUTS( sourcePkgEdition );
163 bool operator()( const PoolItem & obj_r )
165 //MIL << obj_r << endl;
166 //DBG << " -> " << obj_r->satSolvable() << endl;
171 bool operator()( const sat::Solvable & obj_r )
173 //dumpOn( MIL, obj_r ) << endl;
178 ///////////////////////////////////////////////////////////////////
179 struct SetTransactValue
181 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
182 : _newVal( newVal_r )
183 , _causer( causer_r )
186 ResStatus::TransactValue _newVal;
187 ResStatus::TransactByValue _causer;
189 bool operator()( const PoolItem & pi ) const
191 bool ret = pi.status().setTransactValue( _newVal, _causer );
193 ERR << _newVal << _causer << " " << pi << endl;
198 struct StatusReset : public SetTransactValue
201 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
205 struct StatusInstall : public SetTransactValue
208 : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
212 ///////////////////////////////////////////////////////////////////
218 //zypp::base::LogControl::TmpLineWriter shutUp;
219 rres = getZYpp()->resolver()->resolvePool();
223 ERR << "resolve " << rres << endl;
226 MIL << "resolve " << rres << endl;
232 ZYppCommitPolicy pol;
234 pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
235 SEC << getZYpp()->commit( pol ) << endl;
241 getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
244 ///////////////////////////////////////////////////////////////////
246 struct DigestReceive : public callback::ReceiveReport<DigestReport>
253 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
258 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
263 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
265 USR << "fle " << PathInfo(file) << endl;
266 USR << "req " << requested << endl;
267 USR << "fnd " << found << endl;
272 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
274 KeyRingSignalsReceive()
278 virtual void trustedKeyAdded( const PublicKey &/*key*/ )
282 virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
288 ///////////////////////////////////////////////////////////////////
290 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
292 virtual Action requestMedia( Url & source
294 , const std::string & label
296 , const std::string & description
297 , const std::vector<std::string> & devices
298 , unsigned int & dev_current )
300 SEC << __FUNCTION__ << endl
301 << " " << source << endl
302 << " " << mediumNr << endl
303 << " " << label << endl
304 << " " << error << endl
305 << " " << description << endl
306 << " " << devices << endl
307 << " " << dev_current << endl;
312 ///////////////////////////////////////////////////////////////////
317 bool isIn( const std::set<Tp> & cont, const typename std::set<Tp>::value_type & val )
318 { return cont.find( val ) != cont.end(); }
320 ///////////////////////////////////////////////////////////////////
322 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
324 SEC << *l << " - " << *r << endl;
325 INT << "== " << (l==r) << endl;
326 INT << "!= " << (l!=r) << endl;
329 bool isTrue() { return true; }
330 bool isFalse() { return false; }
334 for ( int i = -3; i < 30; ++i )
336 DBG << i << '\t' << IdString( i ) << endl;
340 void ttt( const char * lhs, const char * rhs )
342 DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
349 template <class TMemFun, class TValue>
353 HasValue( TMemFun fun_r, TValue val_r )
354 : _fun( fun_r ), _val( val_r )
357 bool operator()( const Tp & obj_r ) const
358 { return( _fun && (obj_r.*_fun)() == _val ); }
364 template <class TMemFun, class TValue>
365 HasValue<TMemFun, TValue> byValue( TMemFun fun_r, TValue val_r )
366 { return HasValue<TMemFun, TValue>( fun_r, val_r ); }
372 struct TestO { TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
375 std::ostream & operator<<( std::ostream & str, const TestO<L> & obj )
376 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
379 TestO<L> testO( const L & lhs )
380 { return TestO<L>( lhs ); }
382 template <class L, class R>
383 void testCMP( const L & lhs, const R & rhs )
385 MIL << "LHS " << testO(lhs) << endl;
386 MIL << "RHS " << rhs << endl;
388 #define OUTS(S) DBG << #S << ": " << (S) << endl
389 OUTS( lhs.compare(rhs) );
399 #include "zypp/Locks.h"
400 #include "zypp/target/HardLocksFile.h"
401 inline PoolQuery makeTrivialQuery( IdString ident_r )
403 sat::Solvable::SplitIdent ident( ident_r );
406 q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
407 q.addKind( ident.kind() );
409 q.setCaseSensitive(true);
412 inline PoolQuery makeTrivialQuery( const char * ch )
413 { return makeTrivialQuery( IdString(ch) ); }
416 static unsigned i = 0;
417 ResPool pool( ResPool::instance() );
418 target::HardLocksFile::Data newdata;
419 pool.getHardLockQueries( newdata );
420 SEC << '[' << i++ << ']' << newdata << endl;
424 Capability guessPackageSpec( const std::string & str_r )
426 return Capability::guessPackageSpec( str_r );
432 void cut( const Capability & cap )
434 CapDetail detail( cap.detail() );
435 if ( detail.isSimple() )
437 MIL << detail.kind() << ": " << detail.name();
438 if ( detail.hasArch() )
439 MIL << " (" << detail.arch() << ")";
440 if ( detail.isVersioned() )
441 MIL << " " << detail.op() << " " << detail.ed();
446 MIL << "--- ??? ---" << endl;
450 namespace zypp { namespace target {
451 void XRunUpdateMessages( const Pathname & root_r,
452 const Pathname & messagesPath_r,
453 const std::vector<sat::Solvable> & checkPackages_r,
454 ZYppCommitResult & result_r );
457 using zypp::target::XRunUpdateMessages;
459 /******************************************************************
461 ** FUNCTION NAME : main
462 ** FUNCTION TYPE : int
464 int main( int argc, char * argv[] )
468 // download the repo index file
469 media::MediaManager mediamanager;
470 media::MediaAccessId mid = mediamanager.open( Url("http://download.opensuse.org") );
471 mediamanager.attach( mid );
473 zypp::base::LogControl::instance().logToStdErr();
474 INT << "===[START]==========================================" << endl;
477 ResPool pool( ResPool::instance() );
478 sat::Pool satpool( sat::Pool::instance() );
482 Measure x( "INIT TARGET" );
485 //zypp::base::LogControl::TmpLineWriter shutUp;
486 getZYpp()->initializeTarget( sysRoot );
488 getZYpp()->target()->load();
489 USR << "baseproduct: " << getZYpp()->target()->baseProduct() << endl;
490 USR << "targetDistribution: " << getZYpp()->target()->targetDistribution() << endl;
491 USR << "targetDistributionRelease: " << getZYpp()->target()->targetDistributionRelease() << endl;
492 dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
498 RepoManager repoManager( makeRepoManager( sysRoot ) );
499 ServiceInfoList services = repoManager.knownServices();
501 for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
503 ServiceInfo & nservice( *it );
504 SEC << nservice << endl;
506 if ( ! nservice.enabled() )
509 repoManager.refreshService( nservice );
515 RepoManager repoManager( makeRepoManager( sysRoot ) );
516 RepoInfoList repos = repoManager.knownRepositories();
519 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
521 RepoInfo & nrepo( *it );
522 SEC << nrepo << endl;
524 if ( ! nrepo.enabled() )
527 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
529 if ( repoManager.isCached( nrepo ) )
531 SEC << "cleanCache" << endl;
532 repoManager.cleanCache( nrepo );
534 SEC << "refreshMetadata" << endl;
535 //repoManager.refreshMetadata( nrepo );
536 SEC << "buildCache" << endl;
537 repoManager.buildCache( nrepo );
541 // create from cache:
543 Measure x( "CREATE FROM CACHE" );
544 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
546 RepoInfo & nrepo( *it );
547 if ( ! nrepo.enabled() )
550 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
553 repoManager.loadFromCache( nrepo );
555 catch ( const Exception & exp )
557 MIL << "Try to rebuild cache..." << endl;
558 SEC << "cleanCache" << endl;
559 repoManager.cleanCache( nrepo );
560 SEC << "buildCache" << endl;
561 repoManager.buildCache( nrepo );
562 SEC << "Create from cache" << endl;
563 repoManager.loadFromCache( nrepo );
566 //USR << "pool: " << pool << endl;
571 dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
572 USR << "pool: " << pool << endl;
574 ///////////////////////////////////////////////////////////////////
578 Measure x( "Upgrade" );
579 getZYpp()->resolver()->doUpgrade();
582 ///////////////////////////////////////////////////////////////////
589 getZYpp()->resolver()->addRequire( Capability("amarok") );
590 pool.byKindBegin<Package>()->status().setTransact( true, ResStatus::USER );
592 vdumpPoolStats( USR << "Transacting:"<< endl,
593 make_filter_begin<resfilter::ByTransact>(pool),
594 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
597 //////////////////////////////////////////////////////////////////
598 INT << "===[END]============================================" << endl << endl;
599 zypp::base::LogControl::instance().logNothing();
602 catch ( const Exception & exp )
604 INT << exp << endl << exp.historyAsString();