12 #include <zypp/Digest.h>
13 #include <zypp/KeyRing.h>
15 #include <zypp/base/Logger.h>
16 #include <zypp/base/LogControl.h>
17 #include <zypp/base/String.h>
18 #include <zypp/base/Exception.h>
19 #include <zypp/base/PtrTypes.h>
20 #include <zypp/base/Iterator.h>
21 #include <zypp/base/Algorithm.h>
22 #include <zypp/base/Functional.h>
23 #include <zypp/base/ProvideNumericId.h>
24 #include <zypp/base/ProvideNumericId.h>
26 #include "zypp/NVRAD.h"
27 #include "zypp/ResPool.h"
28 #include "zypp/ResFilters.h"
29 #include "zypp/CapFilters.h"
30 #include "zypp/Package.h"
31 #include "zypp/Language.h"
32 #include "zypp/NameKindProxy.h"
34 #include <zypp/SourceManager.h>
35 #include <zypp/SourceFactory.h>
36 #include <zypp/source/susetags/SuseTagsImpl.h>
38 #include "zypp/ZYppFactory.h"
39 #include "zypp/ResPoolProxy.h"
40 #include "zypp/ResPoolProxy.h"
41 #include "zypp/target/rpm/RpmDb.h"
45 using namespace zypp::ui;
46 using namespace zypp::functor;
48 ///////////////////////////////////////////////////////////////////
50 static const Pathname sysRoot( "/Local/ROOT" );
51 static const Url instSrc( "dir:/Local/SLES10" );
52 //static const Url instSrc( "dir:/Local/FACTORY" );
54 ///////////////////////////////////////////////////////////////////
59 bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
60 { return cont.find( val ) != cont.end(); }
63 ///////////////////////////////////////////////////////////////////
65 template<class _Condition>
68 SetTrue( _Condition cond_r )
73 bool operator()( _Tp t ) const
82 template<class _Condition>
83 inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
85 return SetTrue<_Condition>( cond_r );
88 template <class _Iterator, class _Filter, class _Function>
89 inline _Function for_each_if( _Iterator begin_r, _Iterator end_r,
93 for ( _Iterator it = begin_r; it != end_r; ++it )
95 if ( filter_r( *it ) )
105 void operator()( const PoolItem & pi ) const
107 USR << "S" << pi->source().numericId()
108 << "/M" << mediaId(pi)
109 << " " << pi << endl;
111 unsigned mediaId( const PoolItem & pi ) const
113 Package::constPtr pkg( asKind<Package>(pi.resolvable()) );
115 return pkg->mediaId();
120 template <class _Iterator>
121 std::ostream & vdumpPoolStats( std::ostream & str,
122 _Iterator begin_r, _Iterator end_r )
124 pool::PoolStats stats;
125 std::for_each( begin_r, end_r,
127 functor::chain( setTrue_c(PrintPoolItem()),
128 setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
134 struct PoolItemSelect
136 void operator()( const PoolItem & pi ) const
138 if ( pi->source().numericId() == 2 )
139 pi.status().setTransact( true, ResStatus::USER );
143 ///////////////////////////////////////////////////////////////////
144 typedef std::list<PoolItem> PoolItemList;
145 typedef std::set<PoolItem> PoolItemSet;
146 #include "zypp/solver/detail/InstallOrder.h"
147 using zypp::solver::detail::InstallOrder;
150 ///////////////////////////////////////////////////////////////////
153 struct CollectTransacting
155 typedef std::list<PoolItem> PoolItemList;
157 void operator()( const PoolItem & pi )
159 if ( pi.status().isToBeInstalled() )
161 _toInstall.insert( pi );
163 else if ( pi.status().isToBeUninstalled() )
165 if ( pi.status().isToBeUninstalledDueToObsolete()
166 || pi.status().isToBeUninstalledDueToUpgrade() )
167 _skipToDelete.insert( pi );
169 _toDelete.insert( pi );
173 PoolItemSet _toInstall;
174 PoolItemSet _toDelete;
175 PoolItemSet _skipToDelete;
178 std::ostream & operator<<( std::ostream & str, const CollectTransacting & obj )
180 str << "CollectTransacting:" << endl;
181 dumpPoolStats( str << " toInstall: ",
182 obj._toInstall.begin(), obj._toInstall.end() ) << endl;
183 dumpPoolStats( str << " toDelete: ",
184 obj._toDelete.begin(), obj._toDelete.end() ) << endl;
185 dumpPoolStats( str << " skipToDelete: ",
186 obj._skipToDelete.begin(), obj._skipToDelete.end() ) << endl;
191 ///////////////////////////////////////////////////////////////////
193 template<class _InstIterator, class _DelIterator, class _OutputIterator>
194 void strip_obsoleted_to_delete( _InstIterator instBegin_r, _InstIterator instEnd_r,
195 _DelIterator delBegin_r, _DelIterator delEnd_r,
196 _OutputIterator skip_r )
198 if ( instBegin_r == instEnd_r
199 || delBegin_r == delEnd_r )
200 return; // ---> nothing to do
202 // build obsoletes from inst
204 for ( /**/; instBegin_r != instEnd_r; ++instBegin_r )
207 //PoolItem_Ref item( *it );
208 //obsoletes.insert( item->dep(Dep::OBSOLETES).begin(), item->dep(Dep::OBSOLETES).end() );
210 if ( obsoletes.size() == 0 )
211 return; // ---> nothing to do
214 PoolItemList undelayed;
215 // forall applDelete Packages...
216 for ( PoolItemList::iterator it = deleteList_r.begin();
217 it != deleteList_r.end(); ++it )
219 PoolItem_Ref ipkg( *it );
220 bool delayPkg = false;
221 // ...check whether an obsoletes....
222 for ( CapSet::iterator obs = obsoletes.begin();
223 ! delayPkg && obs != obsoletes.end(); ++obs )
225 // ...matches anything provided by the package?
226 for ( CapSet::const_iterator prov = ipkg->dep(Dep::PROVIDES).begin();
227 prov != ipkg->dep(Dep::PROVIDES).end(); ++prov )
229 if ( obs->matches( *prov ) == CapMatch::yes )
231 // if so, delay package deletion
232 DBG << "Ignore appl_delete (should be obsoleted): " << ipkg << endl;
234 ipkg.status().setTransact( false, ResStatus::USER );
240 DBG << "undelayed " << ipkg << endl;
241 undelayed.push_back( ipkg );
245 deleteList_r.swap( undelayed );
249 ///////////////////////////////////////////////////////////////////
251 struct SetTransactValue
253 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
254 : _newVal( newVal_r )
255 , _causer( causer_r )
258 ResStatus::TransactValue _newVal;
259 ResStatus::TransactByValue _causer;
261 bool operator()( const PoolItem & pi ) const
262 { return pi.status().setTransactValue( _newVal, _causer ); }
265 struct StatusReset : public SetTransactValue
268 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
273 inline bool keyDef( bool def )
276 struct DigestReceive : public zypp::callback::ReceiveReport<zypp::DigestReport>
278 virtual bool askUserToAcceptNoDigest( const zypp::Pathname & file )
280 bool def = zypp::DigestReport::askUserToAcceptNoDigest( file );
281 SEC << "AcceptNoDigest " << file << " (" << def << ')' << endl;
282 return keyDef( def );
285 struct KeyRingReceive : public zypp::callback::ReceiveReport<zypp::KeyRingReport>
287 virtual bool askUserToTrustKey( const std::string & keyid,
288 const std::string & keyname,
289 const std::string & keydetails )
291 bool def = zypp::KeyRingReport::askUserToTrustKey( keyid, keyname, keydetails );
292 SEC << "TrustKey " << keyid << ' ' << keyname << ' ' << keydetails << " (" << def << ')' << endl;
293 return keyDef( def );
296 virtual bool askUserToAcceptUnknownKey( const zypp::Pathname & path,
297 const std::string & keyid,
298 const std::string & keyname )
300 bool def = zypp::KeyRingReport::askUserToAcceptUnknownKey( path, keyid, keyname );
301 SEC << "AcceptUnknownKey " << path << ' ' << keyid << ' ' << keyname << " (" << def << ')' << endl;
302 return keyDef( def );
305 virtual bool askUserToAcceptUnsignedFile( const zypp::Pathname & file )
307 bool def = zypp::KeyRingReport::askUserToAcceptUnsignedFile( file );
308 SEC << "AcceptUnsignedFile " << file << " (" << def << ')' << endl;
309 return keyDef( def );
312 virtual bool askUserToAcceptVerificationFailed( const zypp::Pathname & file,
313 const std::string & keyid,
314 const std::string & keyname )
316 bool def = zypp::KeyRingReport::askUserToAcceptVerificationFailed( file, keyid, keyname );
317 SEC << "AcceptVerificationFailed " << file << ' ' << keyid << ' ' << keyname << " (" << def << ')' << endl;
318 return keyDef( def );
322 struct KeyRingCallbacks
326 _digestReceive.connect();
327 _keyRingReceive.connect();
331 _digestReceive.disconnect();
332 _keyRingReceive.disconnect();
335 DigestReceive _digestReceive;
336 KeyRingReceive _keyRingReceive;
339 static KeyRingCallbacks cbs;
341 void checkSource( const Url & url )
343 Source_Ref src( createSource( url ) );
345 void checkSource( const std::string & urlstr )
346 { checkSource( Url(urlstr) ); }
349 /******************************************************************
351 ** FUNCTION NAME : main
352 ** FUNCTION TYPE : int
354 int main( int argc, char * argv[] )
356 //zypp::base::LogControl::instance().logfile( "xxx" );
357 INT << "===[START]==========================================" << endl;
358 ResPool pool( getZYpp()->pool() );
360 checkSource( "ftp://ftp.gwdg.de/pub/linux/misc/suser-guru/rpm/10.0" );
365 Measure x( "initTarget " + sysRoot.asString() );
366 getZYpp()->initTarget( sysRoot );
367 getZYpp()->addResolvables( getZYpp()->target()->resolvables(), true );
368 INT << "Added target: " << pool << endl;
372 SourceManager::sourceManager()->restore( sysRoot );
373 if ( SourceManager::sourceManager()->allSources().empty() )
375 Source_Ref src( createSource( instSrc ) );
376 SourceManager::sourceManager()->addSource( src );
377 SourceManager::sourceManager()->store( sysRoot, true );
380 Source_Ref src( *SourceManager::sourceManager()->Source_begin() );
381 getZYpp()->addResolvables( src.resolvables() );
382 INT << "Added source: " << pool << endl;
386 Source_Ref src2( createSource( "dir:/Local/SUSE-Linux-10.1-Build_830-i386/CD1" ) );
387 Source_Ref src1( createSource( "dir:/Local/SUSE-Linux-10.1-Build_830-Addon-BiArch/CD1" ) );
388 INT << "Pool: " << pool << endl;
389 getZYpp()->addResolvables( src1.resolvables() );
390 INT << "Added source1: " << pool << endl;
391 getZYpp()->addResolvables( src2.resolvables() );
392 INT << "Added source2: " << pool << endl;
395 make_filter_begin<resfilter::ByTransact>(pool),
396 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
398 std::for_each( pool.begin(), pool.end(), SetTransactValue( ResStatus::TRANSACT, ResStatus::USER ) );
400 make_filter_begin<resfilter::ByTransact>(pool),
401 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
403 std::for_each( pool.begin(), pool.end(), StatusReset() );
405 make_filter_begin<resfilter::ByTransact>(pool),
406 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
411 NameKindProxy s( nameKindProxy<Selection>( pool, "default" ) );
413 if ( ! s.availableEmpty() )
415 PoolItem def( * s.availableBegin() );
416 def.status().setTransact( true, ResStatus::USER );
421 zypp::base::LogControl::TmpLineWriter shutUp;
422 eres = getZYpp()->resolver()->establishPool();
423 rres = getZYpp()->resolver()->resolvePool();
425 MIL << "est " << eres << " slv " << rres << endl;
428 for_each( pool.byKindBegin<Package>(), pool.byKindEnd<Package>(),
430 INT << "FIN: " << pool << endl;
432 make_filter_begin<resfilter::ByTransact>(pool),
433 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
437 PoolItemList errors_r;
438 PoolItemList remaining_r;
439 PoolItemList srcremaining_r;
440 commit( pool, 0, errors_r, remaining_r, srcremaining_r, false );
442 dumpPoolStats( WAR << "remaining_r ", remaining_r.begin(), remaining_r.end() ) << endl;
443 dumpPoolStats( WAR << "srcremaining_r ", srcremaining_r.begin(), srcremaining_r.end() ) << endl;
447 CollectTransacting toTransact;
448 std::for_each( make_filter_begin<resfilter::ByTransact>(pool),
449 make_filter_end<resfilter::ByTransact>(pool),
450 functor::functorRef<void,PoolItem>(toTransact) );
455 Source_Ref src( *SourceManager::sourceManager()->Source_begin() );
456 const std::list<Pathname> srcKeys( src.publicKeys() );
458 DBG << srcKeys << endl;
460 target::rpm::RpmDb rpm;
461 rpm.initDatabase( sysRoot );
462 std::set<Edition> rpmKeys( rpm.pubkeys() );
464 DBG << rpmKeys << endl;
466 ResPool pool( getZYpp()->pool() );
467 getZYpp()->addResolvables( src.resolvables() );
473 INT << "===[END]============================================" << endl << endl;