Fix Werrors with GCC-14.1.0
[platform/upstream/libzypp.git] / devel / devel.ma / NewPool.cc
1 #include "Tools.h"
2
3 #include <zypp/PoolQueryResult.h>
4
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>
17
18 #include <zypp/ResPoolProxy.h>
19
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>
29
30
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>
36
37 #include <zypp/ResPoolProxy.h>
38
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>
49
50 #include <zypp/target/rpm/RpmDb.h>
51 #include <zypp/target/rpm/RpmHeader.h>
52 #include <zypp/target/rpm/librpmDb.h>
53
54 #include <boost/mpl/int.hpp>
55
56 using namespace zypp;
57 using namespace zypp::functor;
58 using namespace zypp::ui;
59
60 ///////////////////////////////////////////////////////////////////
61
62 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
63
64 ///////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
66 namespace zypp
67 { /////////////////////////////////////////////////////////////////
68
69 bool queryInstalledEditionHelper( const std::string & name_r,
70                                   const Edition &     ed_r,
71                                   const Arch &        arch_r )
72 {
73   if ( ed_r == Edition::noedition )
74     return true;
75   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
76     return true;
77   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
78     return true;
79
80   return false;
81 }
82
83
84 ManagedFile repoProvidePackage( const PoolItem & pi )
85 {
86   ResPool _pool( getZYpp()->pool() );
87   repo::RepoMediaAccess _access;
88
89   // Redirect PackageProvider queries for installed editions
90   // (in case of patch/delta rpm processing) to rpmDb.
91   repo::PackageProviderPolicy packageProviderPolicy;
92   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
93
94   Package::constPtr p = asKind<Package>(pi.resolvable());
95
96   // Build a repository list for repos
97   // contributing to the pool
98   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
99                                                            _pool.knownRepositoriesEnd() ) );
100   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
101   return pkgProvider.providePackage();
102 }
103
104   /////////////////////////////////////////////////////////////////
105 } // namespace zypp
106 ///////////////////////////////////////////////////////////////////
107 ///////////////////////////////////////////////////////////////////
108
109 namespace zypp
110 {
111   template <class TLIterator, class TRIterator, class TFunction>
112       inline int invokeOnEach( TLIterator lbegin_r, TLIterator lend_r,
113                                TRIterator rbegin_r, TRIterator rend_r,
114                                TFunction fnc_r )
115       {
116         int cnt = 0;
117         for ( TLIterator lit = lbegin_r; lit != lend_r; ++lit )
118         {
119           for ( TRIterator rit = rbegin_r; rit != rend_r; ++rit )
120           {
121             ++cnt;
122             if ( ! fnc_r( *lit, *rit ) )
123               return -cnt;
124           }
125         }
126         return cnt;
127       }
128 }
129
130
131 ///////////////////////////////////////////////////////////////////
132
133 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
134 {
135   str << pi << endl;
136   Package::constPtr p( asKind<Package>(pi) );
137   if ( p )
138   {
139 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
140     Locale l( "de" );
141     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
142     l = Locale( "fr" );
143     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
144     l = Locale( "dsdf" );
145     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
146     OUTS( summary );
147     OUTS( installSize );
148     OUTS( downloadSize );
149     OUTS( sourcePkgName );
150     OUTS( sourcePkgEdition );
151     OUTS( checksum );
152     OUTS( location );
153 #undef OUTS
154
155
156   }
157   return str;
158 }
159
160 struct Xprint
161 {
162   bool operator()( const PoolItem & obj_r )
163   {
164     //MIL << obj_r << endl;
165     //DBG << " -> " << obj_r->satSolvable() << endl;
166
167     return true;
168   }
169
170   bool operator()( const sat::Solvable & obj_r )
171   {
172     //dumpOn( MIL, obj_r ) << endl;
173     return true;
174   }
175 };
176
177 ///////////////////////////////////////////////////////////////////
178 struct SetTransactValue
179 {
180   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
181   : _newVal( newVal_r )
182   , _causer( causer_r )
183   {}
184
185   ResStatus::TransactValue   _newVal;
186   ResStatus::TransactByValue _causer;
187
188   bool operator()( const PoolItem & pi ) const
189   {
190     bool ret = pi.status().setTransactValue( _newVal, _causer );
191     if ( ! ret )
192       ERR << _newVal <<  _causer << " " << pi << endl;
193     return ret;
194   }
195 };
196
197 struct StatusReset : public SetTransactValue
198 {
199   StatusReset()
200   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
201   {}
202 };
203
204 struct StatusInstall : public SetTransactValue
205 {
206   StatusInstall()
207   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
208   {}
209 };
210
211 ///////////////////////////////////////////////////////////////////
212
213 bool solve()
214 {
215   bool rres = false;
216   {
217     //zypp::base::LogControl::TmpLineWriter shutUp;
218     rres = getZYpp()->resolver()->resolvePool();
219   }
220   if ( ! rres )
221   {
222     ERR << "resolve " << rres << endl;
223     return false;
224   }
225   MIL << "resolve " << rres << endl;
226   return true;
227 }
228
229 bool install()
230 {
231   ZYppCommitPolicy pol;
232   pol.dryRun(true);
233   pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
234   SEC << getZYpp()->commit( pol ) << endl;
235   return true;
236 }
237
238 void testcase()
239 {
240   getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
241 }
242
243 ///////////////////////////////////////////////////////////////////
244
245 struct DigestReceive : public callback::ReceiveReport<DigestReport>
246 {
247   DigestReceive()
248   {
249     connect();
250   }
251
252   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
253   {
254     USR << endl;
255     return false;
256   }
257   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
258   {
259     USR << endl;
260     return false;
261   }
262   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
263   {
264     USR << "fle " << PathInfo(file) << endl;
265     USR << "req " << requested << endl;
266     USR << "fnd " << found << endl;
267     return false;
268   }
269 };
270
271 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
272 {
273   KeyRingSignalsReceive()
274   {
275     connect();
276   }
277   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
278   {
279     USR << endl;
280   }
281   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
282   {
283     USR << endl;
284   }
285 };
286
287 ///////////////////////////////////////////////////////////////////
288
289 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
290 {
291   virtual Action requestMedia( Url & source
292                                , unsigned mediumNr
293                                , const std::string & label
294                                , Error error
295                                , const std::string & description
296                                , const std::vector<std::string> & devices
297                                , unsigned int & dev_current )
298   {
299     SEC << __FUNCTION__ << endl
300     << "  " << source << endl
301     << "  " << mediumNr << endl
302     << "  " << label << endl
303     << "  " << error << endl
304     << "  " << description << endl
305     << "  " << devices << endl
306     << "  " << dev_current << endl;
307     return IGNORE;
308   }
309 };
310
311 ///////////////////////////////////////////////////////////////////
312
313 namespace container
314 {
315   template<class Tp>
316     bool isIn( const std::set<Tp> & cont, const typename std::set<Tp>::value_type & val )
317     { return cont.find( val ) != cont.end(); }
318 }
319 ///////////////////////////////////////////////////////////////////
320
321 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
322 {
323   SEC << *l << " - " << *r << endl;
324   INT << "== " << (l==r) << endl;
325   INT << "!= " << (l!=r) << endl;
326 }
327
328 bool isTrue()  { return true; }
329 bool isFalse() { return false; }
330
331 void dumpIdStr()
332 {
333   for ( int i = -3; i < 30; ++i )
334   {
335     DBG << i << '\t' << IdString( i ) << endl;
336   }
337 }
338
339 void ttt( const char * lhs, const char * rhs )
340 {
341   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
342 }
343
344 namespace zypp
345 {
346 namespace filter
347 {
348   template <class TMemFun, class TValue>
349   class HasValue
350   {
351     public:
352       HasValue( TMemFun fun_r, TValue val_r )
353       : _fun( fun_r ), _val( val_r )
354       {}
355       template <class Tp>
356       bool operator()( const Tp & obj_r ) const
357       { return( _fun && (obj_r.*_fun)() == _val ); }
358     private:
359       TMemFun _fun;
360       TValue  _val;
361   };
362
363   template <class TMemFun, class TValue>
364   HasValue<TMemFun, TValue> byValue( TMemFun fun_r, TValue val_r )
365   { return HasValue<TMemFun, TValue>( fun_r, val_r ); }
366 }
367
368 }
369
370 template <class L>
371 struct TestO { TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
372
373 template <class L>
374 std::ostream & operator<<( std::ostream & str, const TestO<L> & obj )
375 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
376
377 template <class L>
378 TestO<L> testO( const L & lhs )
379 { return TestO<L>( lhs ); }
380
381 template <class L, class R>
382 void testCMP( const L & lhs, const R & rhs )
383 {
384   MIL << "LHS " << testO(lhs) << endl;
385   MIL << "RHS " << rhs << endl;
386
387 #define OUTS(S) DBG << #S << ": " << (S) << endl
388   OUTS( lhs.compare(rhs) );
389   OUTS( lhs != rhs );
390   OUTS( lhs <  rhs );
391   OUTS( lhs <= rhs );
392   OUTS( lhs == rhs );
393   OUTS( lhs >= rhs );
394   OUTS( lhs >  rhs );
395 #undef OUTS
396 }
397
398 #include <zypp/Locks.h>
399 #include <zypp/target/HardLocksFile.h>
400 inline PoolQuery makeTrivialQuery( IdString ident_r )
401 {
402   sat::Solvable::SplitIdent ident( ident_r );
403
404   PoolQuery q;
405   q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
406   q.addKind( ident.kind() );
407   q.setMatchExact();
408   q.setCaseSensitive(true);
409   return q;
410 }
411 inline PoolQuery makeTrivialQuery( const char * ch )
412 { return makeTrivialQuery( IdString(ch) ); }
413 void lktest()
414 {
415   static unsigned i = 0;
416   ResPool pool( ResPool::instance() );
417   target::HardLocksFile::Data newdata;
418   pool.getHardLockQueries( newdata );
419   SEC << '[' << i++ << ']' << newdata << endl;
420 }
421
422
423 Capability guessPackageSpec( const std::string & str_r )
424 {
425   return Capability::guessPackageSpec( str_r );
426 }
427
428
429
430
431 void cut( const Capability & cap )
432 {
433   CapDetail detail( cap.detail() );
434   if ( detail.isSimple() )
435   {
436     MIL << detail.kind() << ": " << detail.name();
437     if ( detail.hasArch() )
438       MIL << " (" << detail.arch() << ")";
439     if ( detail.isVersioned() )
440       MIL << " " << detail.op() << " " << detail.ed();
441     MIL << endl;
442   }
443   else
444   {
445     MIL << "--- ??? ---" << endl;
446   }
447 }
448
449 namespace zypp { namespace target {
450   void XRunUpdateMessages( const Pathname & root_r,
451                            const Pathname & messagesPath_r,
452                            const std::vector<sat::Solvable> & checkPackages_r,
453                            ZYppCommitResult & result_r );
454
455 }}
456 using zypp::target::XRunUpdateMessages;
457
458 /******************************************************************
459 **
460 **      FUNCTION NAME : main
461 **      FUNCTION TYPE : int
462 */
463 int main( int argc, char * argv[] )
464 try {
465   --argc,++argv;
466   if (0) {
467     // download the repo index file
468     media::MediaManager mediamanager;
469     media::MediaAccessId mid = mediamanager.open( Url("http://download.opensuse.org") );
470     mediamanager.attach( mid );
471   }
472   zypp::base::LogControl::instance().logToStdErr();
473   INT << "===[START]==========================================" << endl;
474   ZConfig::instance();
475
476   ResPool   pool( ResPool::instance() );
477   sat::Pool satpool( sat::Pool::instance() );
478
479   if ( 0 )
480   {
481     Measure x( "INIT TARGET" );
482     {
483       {
484         //zypp::base::LogControl::TmpLineWriter shutUp;
485         getZYpp()->initializeTarget( sysRoot );
486       }
487       getZYpp()->target()->load();
488       USR << "baseproduct:               " << getZYpp()->target()->baseProduct() << endl;
489       USR << "targetDistribution:        " << getZYpp()->target()->targetDistribution() << endl;
490       USR << "targetDistributionRelease: " << getZYpp()->target()->targetDistributionRelease() << endl;
491       dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
492     }
493   }
494
495   if ( 0 )
496   {
497     RepoManager repoManager( makeRepoManager( sysRoot ) );
498     ServiceInfoList services = repoManager.knownServices();
499
500     for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
501     {
502       ServiceInfo & nservice( *it );
503       SEC << nservice << endl;
504
505       if ( ! nservice.enabled() )
506         continue;
507
508       repoManager.refreshService( nservice );
509     }
510   }
511
512   if ( 1 )
513   {
514     RepoManager repoManager( makeRepoManager( sysRoot ) );
515     RepoInfoList repos = repoManager.knownRepositories();
516
517     // launch repos
518     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
519     {
520       RepoInfo & nrepo( *it );
521       SEC << nrepo << endl;
522
523       if ( ! nrepo.enabled() )
524         continue;
525
526       if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
527       {
528         if ( repoManager.isCached( nrepo ) )
529         {
530           SEC << "cleanCache" << endl;
531           repoManager.cleanCache( nrepo );
532         }
533         SEC << "refreshMetadata" << endl;
534         //repoManager.refreshMetadata( nrepo );
535         SEC << "buildCache" << endl;
536         repoManager.buildCache( nrepo );
537       }
538     }
539
540     // create from cache:
541     {
542       Measure x( "CREATE FROM CACHE" );
543       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
544       {
545         RepoInfo & nrepo( *it );
546         if ( ! nrepo.enabled() )
547           continue;
548
549         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
550         try
551         {
552           repoManager.loadFromCache( nrepo );
553         }
554         catch ( const Exception & exp )
555         {
556           MIL << "Try to rebuild cache..." << endl;
557           SEC << "cleanCache" << endl;
558           repoManager.cleanCache( nrepo );
559           SEC << "buildCache" << endl;
560           repoManager.buildCache( nrepo );
561           SEC << "Create from cache" << endl;
562           repoManager.loadFromCache( nrepo );
563         }
564
565         //USR << "pool: " << pool << endl;
566       }
567     }
568   }
569
570   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
571   USR << "pool: " << pool << endl;
572
573   ///////////////////////////////////////////////////////////////////
574
575   if ( 0 )
576   {
577     Measure x( "Upgrade" );
578     getZYpp()->resolver()->doUpgrade();
579   }
580
581   ///////////////////////////////////////////////////////////////////
582
583
584
585
586
587 #if 1
588   getZYpp()->resolver()->addRequire( Capability("amarok") );
589   pool.byKindBegin<Package>()->status().setTransact( true, ResStatus::USER );
590   solve();
591   vdumpPoolStats( USR << "Transacting:"<< endl,
592                   make_filter_begin<resfilter::ByTransact>(pool),
593                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
594 #endif
595
596   //////////////////////////////////////////////////////////////////
597   INT << "===[END]============================================" << endl << endl;
598   zypp::base::LogControl::instance().logNothing();
599   return 0;
600 }
601 catch ( const Exception & exp )
602 {
603   INT << exp << endl << exp.historyAsString();
604   throw;
605 }
606 catch (...)
607 {
608   throw;
609 }
610
611