1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/detail/PoolImpl.cc
14 #include <boost/mpl/int.hpp>
16 #include "zypp/base/Easy.h"
17 #include "zypp/base/LogTools.h"
18 #include "zypp/base/Gettext.h"
19 #include "zypp/base/Exception.h"
20 #include "zypp/base/Measure.h"
21 #include "zypp/base/WatchFile.h"
22 #include "zypp/base/Sysconfig.h"
23 #include "zypp/base/IOStream.h"
25 #include "zypp/ZConfig.h"
27 #include "zypp/sat/detail/PoolImpl.h"
28 #include "zypp/sat/SolvableSet.h"
29 #include "zypp/sat/Pool.h"
30 #include "zypp/Capability.h"
31 #include "zypp/Locale.h"
32 #include "zypp/PoolItem.h"
34 #include "zypp/target/modalias/Modalias.h"
35 #include "zypp/media/MediaPriority.h"
39 // Workaround libsolv project not providing a common include
40 // directory. (the -devel package does, but the git repo doesn't).
41 // #include <solv/repo_helix.h>
42 int repo_add_helix( ::Repo *repo, FILE *fp, int flags );
47 #undef ZYPP_BASE_LOGGER_LOGGROUP
48 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::satpool"
50 // ///////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////
57 inline int LIBSOLV_DEBUGMASK()
59 const char * envp = getenv("LIBSOLV_DEBUGMASK");
60 return envp ? str::strtonum<int>( envp ) : 0;
63 ///////////////////////////////////////////////////////////////////
65 { /////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////
69 { /////////////////////////////////////////////////////////////////
71 // MPL checks for satlib constants we redefine to avoid
72 // includes and defines.
73 BOOST_MPL_ASSERT_RELATION( noId, ==, STRID_NULL );
74 BOOST_MPL_ASSERT_RELATION( emptyId, ==, STRID_EMPTY );
76 BOOST_MPL_ASSERT_RELATION( noSolvableId, ==, ID_NULL );
77 BOOST_MPL_ASSERT_RELATION( systemSolvableId, ==, SYSTEMSOLVABLE );
79 BOOST_MPL_ASSERT_RELATION( solvablePrereqMarker, ==, SOLVABLE_PREREQMARKER );
80 BOOST_MPL_ASSERT_RELATION( solvableFileMarker, ==, SOLVABLE_FILEMARKER );
82 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_AND, ==, REL_AND );
83 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_OR, ==, REL_OR );
84 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_WITH, ==, REL_WITH );
85 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_NAMESPACE, ==, REL_NAMESPACE );
86 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_ARCH, ==, REL_ARCH );
88 BOOST_MPL_ASSERT_RELATION( namespaceModalias, ==, NAMESPACE_MODALIAS );
89 BOOST_MPL_ASSERT_RELATION( namespaceLanguage, ==, NAMESPACE_LANGUAGE );
90 BOOST_MPL_ASSERT_RELATION( namespaceFilesystem, ==, NAMESPACE_FILESYSTEM );
92 /////////////////////////////////////////////////////////////////
94 const bool resusePoolIDs = true;
96 const std::string & PoolImpl::systemRepoAlias()
98 static const std::string _val( "@System" );
102 const Pathname & sysconfigStoragePath()
104 static const Pathname _val( "/etc/sysconfig/storage" );
108 /////////////////////////////////////////////////////////////////
110 static void logSat( CPool *, void *data, int type, const char *logString )
112 if ( type & (SOLV_FATAL|SOLV_ERROR) ) {
113 L_ERR("libsolv") << logString;
114 } else if ( type & SOLV_DEBUG_STATS ) {
115 L_DBG("libsolv") << logString;
117 L_MIL("libsolv") << logString;
121 detail::IdType PoolImpl::nsCallback( CPool *, void * data, detail::IdType lhs, detail::IdType rhs )
123 // lhs: the namespace identifier, e.g. NAMESPACE:MODALIAS
124 // rhs: the value, e.g. pci:v0000104Cd0000840[01]sv*sd*bc*sc*i*
125 // return: 0 if not supportded
126 // 1 if supported by the system
127 // -1 AFAIK it's also possible to return a list of solvables that support it, but don't know how.
129 static const detail::IdType RET_unsupported = 0;
130 static const detail::IdType RET_systemProperty = 1;
133 case NAMESPACE_LANGUAGE:
135 const TrackedLocaleIds & localeIds( reinterpret_cast<PoolImpl*>(data)->trackedLocaleIds() );
136 return localeIds.contains( IdString(rhs) ) ? RET_systemProperty : RET_unsupported;
140 case NAMESPACE_MODALIAS:
142 // modalias strings in capability may be hexencoded because rpm does not allow
143 // ',', ' ' or other special chars.
144 return target::Modalias::instance().query( str::hexdecode( IdString(rhs).c_str() ) )
150 case NAMESPACE_FILESYSTEM:
152 const std::set<std::string> & requiredFilesystems( reinterpret_cast<PoolImpl*>(data)->requiredFilesystems() );
153 return requiredFilesystems.find( IdString(rhs).asString() ) != requiredFilesystems.end() ? RET_systemProperty : RET_unsupported;
159 WAR << "Unhandled " << Capability( lhs ) << " vs. " << Capability( rhs ) << endl;
160 return RET_unsupported;
163 ///////////////////////////////////////////////////////////////////
165 // METHOD NAME : PoolMember::myPool
166 // METHOD TYPE : PoolImpl
168 PoolImpl & PoolMember::myPool()
170 static PoolImpl _global;
174 ///////////////////////////////////////////////////////////////////
176 // METHOD NAME : PoolImpl::PoolImpl
177 // METHOD TYPE : Ctor
180 : _pool( ::pool_create() )
182 MIL << "Creating sat-pool." << endl;
185 ZYPP_THROW( Exception( _("Can not create sat-pool.") ) );
187 // by now we support only a RPM backend
188 ::pool_setdisttype(_pool, DISTTYPE_RPM );
190 // initialialize logging
191 if ( env::LIBSOLV_DEBUGMASK() )
193 ::pool_setdebugmask(_pool, env::LIBSOLV_DEBUGMASK() );
197 if ( getenv("ZYPP_LIBSOLV_FULLLOG") || getenv("ZYPP_LIBSAT_FULLLOG") )
198 ::pool_setdebuglevel( _pool, 3 );
199 else if ( getenv("ZYPP_FULLLOG") )
200 ::pool_setdebuglevel( _pool, 2 );
202 ::pool_setdebugmask(_pool, SOLV_DEBUG_JOB|SOLV_DEBUG_STATS );
205 ::pool_setdebugcallback( _pool, logSat, NULL );
207 // set namespace callback
208 _pool->nscallback = &nsCallback;
209 _pool->nscallbackdata = (void*)this;
212 ///////////////////////////////////////////////////////////////////
214 // METHOD NAME : PoolImpl::~PoolImpl
215 // METHOD TYPE : Dtor
217 PoolImpl::~PoolImpl()
219 ::pool_free( _pool );
222 ///////////////////////////////////////////////////////////////////
224 void PoolImpl::setDirty( const char * a1, const char * a2, const char * a3 )
228 if ( a3 ) MIL << a1 << " " << a2 << " " << a3 << endl;
229 else if ( a2 ) MIL << a1 << " " << a2 << endl;
230 else MIL << a1 << endl;
232 _serial.setDirty(); // pool content change
233 _availableLocalesPtr.reset(); // available locales may change
234 _multiversionListPtr.reset(); // re-evaluate ZConfig::multiversionSpec.
236 depSetDirty(); // invaldate dependency/namespace related indices
239 void PoolImpl::localeSetDirty( const char * a1, const char * a2, const char * a3 )
243 if ( a3 ) MIL << a1 << " " << a2 << " " << a3 << endl;
244 else if ( a2 ) MIL << a1 << " " << a2 << endl;
245 else MIL << a1 << endl;
247 _trackedLocaleIdsPtr.reset(); // requested locales changed
248 depSetDirty(); // invaldate dependency/namespace related indices
251 void PoolImpl::depSetDirty( const char * a1, const char * a2, const char * a3 )
255 if ( a3 ) MIL << a1 << " " << a2 << " " << a3 << endl;
256 else if ( a2 ) MIL << a1 << " " << a2 << endl;
257 else MIL << a1 << endl;
259 ::pool_freewhatprovides( _pool );
262 void PoolImpl::prepare() const
264 // additional /etc/sysconfig/storage check:
265 static WatchFile sysconfigFile( sysconfigStoragePath(), WatchFile::NO_INIT );
266 if ( sysconfigFile.hasChanged() )
268 _requiredFilesystemsPtr.reset(); // recreated on demand
269 const_cast<PoolImpl*>(this)->depSetDirty( "/etc/sysconfig/storage change" );
271 if ( _watcher.remember( _serial ) )
273 // After repo/solvable add/remove:
274 // set pool architecture
275 ::pool_setarch( _pool, ZConfig::instance().systemArchitecture().asString().c_str() );
277 if ( ! _pool->whatprovides )
279 MIL << "pool_createwhatprovides..." << endl;
281 ::pool_addfileprovides( _pool );
282 ::pool_createwhatprovides( _pool );
284 if ( ! _pool->languages )
287 const_cast<PoolImpl*>(this)->setTextLocale( ZConfig::instance().textLocale() );
291 ///////////////////////////////////////////////////////////////////
293 CRepo * PoolImpl::_createRepo( const std::string & name_r )
295 setDirty(__FUNCTION__, name_r.c_str() );
296 CRepo * ret = ::repo_create( _pool, name_r.c_str() );
297 if ( ret && name_r == systemRepoAlias() )
298 ::pool_set_installed( _pool, ret );
302 void PoolImpl::_deleteRepo( CRepo * repo_r )
304 setDirty(__FUNCTION__, repo_r->name );
305 if ( isSystemRepo( repo_r ) )
306 _autoinstalled.clear();
307 eraseRepoInfo( repo_r );
308 ::repo_free( repo_r, resusePoolIDs );
309 if ( resusePoolIDs && !_pool->urepos ) // If the last repo is removed clear the pool to actually reuse all IDs.
310 ::pool_freeallrepos( _pool, resusePoolIDs );
313 int PoolImpl::_addSolv( CRepo * repo_r, FILE * file_r )
315 setDirty(__FUNCTION__, repo_r->name );
316 int ret = ::repo_add_solv( repo_r, file_r, 0 );
318 _postRepoAdd( repo_r );
322 int PoolImpl::_addHelix( CRepo * repo_r, FILE * file_r )
324 setDirty(__FUNCTION__, repo_r->name );
325 int ret = ::repo_add_helix( repo_r, file_r, 0 );
327 _postRepoAdd( repo_r );
331 void PoolImpl::_postRepoAdd( CRepo * repo_r )
333 if ( ! isSystemRepo( repo_r ) )
335 // Filter out unwanted archs
336 std::set<detail::IdType> sysids;
338 Arch::CompatSet sysarchs( Arch::compatSet( ZConfig::instance().systemArchitecture() ) );
339 for_( it, sysarchs.begin(), sysarchs.end() )
340 sysids.insert( it->id() );
342 // unfortunately libsolv treats src/nosrc as architecture:
343 sysids.insert( ARCH_SRC );
344 sysids.insert( ARCH_NOSRC );
347 detail::IdType blockBegin = 0;
348 unsigned blockSize = 0;
349 for ( detail::IdType i = repo_r->start; i < repo_r->end; ++i )
351 CSolvable * s( _pool->solvables + i );
352 if ( s->repo == repo_r && sysids.find( s->arch ) == sysids.end() )
354 // Remember an unwanted arch entry:
359 else if ( blockSize )
361 // Free remembered entries
362 ::repo_free_solvable_block( repo_r, blockBegin, blockSize, resusePoolIDs );
363 blockBegin = blockSize = 0;
368 // Free remembered entries
369 ::repo_free_solvable_block( repo_r, blockBegin, blockSize, resusePoolIDs );
370 blockBegin = blockSize = 0;
375 detail::SolvableIdType PoolImpl::_addSolvables( CRepo * repo_r, unsigned count_r )
377 setDirty(__FUNCTION__, repo_r->name );
378 return ::repo_add_solvable_block( repo_r, count_r );
381 void PoolImpl::setRepoInfo( RepoIdType id_r, const RepoInfo & info_r )
383 CRepo * repo( getRepo( id_r ) );
388 // libsolv priority is based on '<', while yum's repoinfo
389 // uses 1(highest)->99(lowest). Thus we use -info_r.priority.
390 if ( repo->priority != int(-info_r.priority()) )
392 repo->priority = -info_r.priority();
396 // subpriority is used to e.g. prefer http over dvd iff
397 // both have same priority.
398 int mediaPriority( media::MediaPriority( info_r.url() ) );
399 if ( repo->subpriority != mediaPriority )
401 repo->subpriority = mediaPriority;
406 setDirty(__FUNCTION__, info_r.alias().c_str() );
408 _repoinfos[id_r] = info_r;
411 ///////////////////////////////////////////////////////////////////
413 void PoolImpl::setTextLocale( const Locale & locale_r )
415 std::vector<std::string> fallbacklist;
416 for ( Locale l( locale_r ); l; l = l.fallback() )
418 fallbacklist.push_back( l.code() );
420 dumpRangeLine( MIL << "pool_set_languages: ", fallbacklist.begin(), fallbacklist.end() ) << endl;
422 std::vector<const char *> fallbacklist_cstr;
423 for_( it, fallbacklist.begin(), fallbacklist.end() )
425 fallbacklist_cstr.push_back( it->c_str() );
427 ::pool_set_languages( _pool, &fallbacklist_cstr.front(), fallbacklist_cstr.size() );
430 void PoolImpl::initRequestedLocales( const LocaleSet & locales_r )
432 if ( _requestedLocalesTracker.setInitial( locales_r ) )
434 localeSetDirty( "initRequestedLocales" );
435 MIL << "Init RequestedLocales: " << _requestedLocalesTracker << " =" << locales_r << endl;
439 void PoolImpl::setRequestedLocales( const LocaleSet & locales_r )
441 if ( _requestedLocalesTracker.set( locales_r ) )
443 localeSetDirty( "setRequestedLocales" );
444 MIL << "New RequestedLocales: " << _requestedLocalesTracker << " =" << locales_r << endl;
448 bool PoolImpl::addRequestedLocale( const Locale & locale_r )
450 bool done = _requestedLocalesTracker.add( locale_r );
453 localeSetDirty( "addRequestedLocale", locale_r.code().c_str() );
454 MIL << "New RequestedLocales: " << _requestedLocalesTracker << " +" << locale_r << endl;
459 bool PoolImpl::eraseRequestedLocale( const Locale & locale_r )
461 bool done = _requestedLocalesTracker.remove( locale_r );
464 localeSetDirty( "addRequestedLocale", locale_r.code().c_str() );
465 MIL << "New RequestedLocales: " << _requestedLocalesTracker << " -" << locale_r << endl;
471 const PoolImpl::TrackedLocaleIds & PoolImpl::trackedLocaleIds() const
473 if ( ! _trackedLocaleIdsPtr )
475 _trackedLocaleIdsPtr.reset( new TrackedLocaleIds );
477 const base::SetTracker<LocaleSet> & localesTracker( _requestedLocalesTracker );
478 TrackedLocaleIds & localeIds( *_trackedLocaleIdsPtr );
480 // Add current locales+fallback except for added ones
481 for ( Locale lang: localesTracker.current() )
483 if ( localesTracker.wasAdded( lang ) )
485 for ( ; lang; lang = lang.fallback() )
486 { localeIds.current().insert( IdString(lang) ); }
489 // Add added locales+fallback except they are already in current
490 for ( Locale lang: localesTracker.added() )
492 for ( ; lang && localeIds.current().insert( IdString(lang) ).second; lang = lang.fallback() )
493 { localeIds.added().insert( IdString(lang) ); }
496 // Add removed locales+fallback except they are still in current
497 for ( Locale lang: localesTracker.removed() )
499 for ( ; lang && ! localeIds.current().count( IdString(lang) ); lang = lang.fallback() )
500 { localeIds.removed().insert( IdString(lang) ); }
503 // Assert that TrackedLocaleIds::current is not empty.
504 // If, so fill in LanguageCode::enCode as last resort.
505 if ( localeIds.current().empty() )
506 { localeIds.current().insert( IdString(Locale::enCode) ); }
508 return *_trackedLocaleIdsPtr;
512 static void _getLocaleDeps( const Capability & cap_r, LocaleSet & store_r )
514 // Collect locales from any 'namespace:language(lang)' dependency
515 CapDetail detail( cap_r );
516 if ( detail.kind() == CapDetail::EXPRESSION )
518 switch ( detail.capRel() )
520 case CapDetail::CAP_AND:
521 case CapDetail::CAP_OR:
523 _getLocaleDeps( detail.lhs(), store_r );
524 _getLocaleDeps( detail.rhs(), store_r );
527 case CapDetail::CAP_NAMESPACE:
528 if ( detail.lhs().id() == NAMESPACE_LANGUAGE )
530 store_r.insert( Locale( IdString(detail.rhs().id()) ) );
534 case CapDetail::REL_NONE:
535 case CapDetail::CAP_WITH:
536 case CapDetail::CAP_ARCH:
542 const LocaleSet & PoolImpl::getAvailableLocales() const
544 if ( !_availableLocalesPtr )
546 _availableLocalesPtr.reset( new LocaleSet );
547 LocaleSet & localeSet( *_availableLocalesPtr );
549 for ( const Solvable & pi : Pool::instance().solvables() )
551 for ( const Capability & cap : pi.supplements() )
553 _getLocaleDeps( cap, localeSet );
557 return *_availableLocalesPtr;
560 ///////////////////////////////////////////////////////////////////
562 void PoolImpl::multiversionListInit() const
564 _multiversionListPtr.reset( new MultiversionList );
565 MultiversionList & multiversionList( *_multiversionListPtr );
567 MultiversionList::size_type size = 0;
568 for ( const std::string & spec : ZConfig::instance().multiversionSpec() )
570 static const std::string prefix( "provides:" );
571 bool provides = str::hasPrefix( spec, prefix );
573 for ( Solvable solv : WhatProvides( Capability( provides ? spec.c_str() + prefix.size() : spec.c_str() ) ) )
575 if ( provides || solv.ident() == spec )
576 multiversionList.insert( solv );
579 MultiversionList::size_type nsize = multiversionList.size();
580 MIL << "Multiversion install " << spec << ": " << (nsize-size) << " matches" << endl;
585 void PoolImpl::multiversionSpecChanged()
586 { _multiversionListPtr.reset(); }
588 const PoolImpl::MultiversionList & PoolImpl::multiversionList() const
590 if ( ! _multiversionListPtr )
591 multiversionListInit();
592 return *_multiversionListPtr;
595 bool PoolImpl::isMultiversion( const Solvable & solv_r ) const
596 { return multiversionList().contains( solv_r ); }
598 ///////////////////////////////////////////////////////////////////
600 const std::set<std::string> & PoolImpl::requiredFilesystems() const
602 if ( ! _requiredFilesystemsPtr )
604 _requiredFilesystemsPtr.reset( new std::set<std::string> );
605 std::set<std::string> & requiredFilesystems( *_requiredFilesystemsPtr );
606 str::split( base::sysconfig::read( sysconfigStoragePath() )["USED_FS_LIST"],
607 std::inserter( requiredFilesystems, requiredFilesystems.end() ) );
609 return *_requiredFilesystemsPtr;
612 /////////////////////////////////////////////////////////////////
613 } // namespace detail
614 ///////////////////////////////////////////////////////////////////
615 /////////////////////////////////////////////////////////////////
617 ///////////////////////////////////////////////////////////////////
618 /////////////////////////////////////////////////////////////////
620 ///////////////////////////////////////////////////////////////////